blob: 05502831f6a8bc55c3bd7bb0c16170602ba65ba1 [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"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010052#include "qapi/qmp/qint.h"
53#include "qapi/qmp/qfloat.h"
54#include "qapi/qmp/qlist.h"
55#include "qapi/qmp/qbool.h"
56#include "qapi/qmp/qstring.h"
57#include "qapi/qmp/qjson.h"
58#include "qapi/qmp/json-streamer.h"
59#include "qapi/qmp/json-parser.h"
Hani Benhabiles1094fd32014-02-06 23:30:13 +010060#include <qom/object_interfaces.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010061#include "qemu/osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000062#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010063#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020064#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020065#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020066#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053067#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010068#include "exec/memory.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010069#include "exec/cpu_ldst.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050070#include "qmp-commands.h"
71#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010072#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010073#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020074#include "qapi/qmp-event.h"
75#include "qapi-event.h"
Fam Zheng55606252015-03-02 19:36:46 +080076#include "sysemu/block-backend.h"
ths6a5bd302007-12-03 17:05:38 +000077
Markus Armbruster1ce6be22015-02-06 14:18:24 +010078/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020079#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010080#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020081#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010082#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020083
bellard9dc39cb2004-03-14 21:38:27 +000084//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000085//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000086
bellard9307c4c2004-04-04 12:57:25 +000087/*
88 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000089 *
bellard9307c4c2004-04-04 12:57:25 +000090 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000091 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000092 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080093 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010094 * 'O' option string of the form NAME=VALUE,...
95 * parsed according to QemuOptsList given by its name
96 * Example: 'device:O' uses qemu_device_opts.
97 * Restriction: only lists with empty desc are supported
98 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000099 * 'i' 32 bit integer
100 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300101 * 'M' Non-negative target long (32 or 64 bit), in user mode the
102 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200103 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200104 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
105 * K, k suffix, which multiplies the value by 2^60 for suffixes E
106 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
107 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +0100108 * 'T' double
109 * user mode accepts an optional ms, us, ns suffix,
110 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000111 * '/' optional gdb-like print format (like "/10x")
112 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300113 * '?' optional type (for all types, except '/')
114 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100115 * 'b' boolean
116 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300117 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000118 *
119 */
120
Anthony Liguoric227f092009-10-01 16:12:16 -0500121typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000122 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000123 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000124 const char *params;
125 const char *help;
Luiz Capitulino910df892009-10-07 13:41:51 -0300126 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300127 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200128 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300129 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800130 /* @sub_table is a list of 2nd level of commands. If it do not exist,
131 * mhandler should be used. If it exist, sub_table[?].mhandler should be
132 * used, and mhandler of 1st level plays the role of help function.
133 */
134 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100135 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500136} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000137
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100138/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500139typedef struct mon_fd_t mon_fd_t;
140struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100141 char *name;
142 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500143 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100144};
145
Corey Bryantba1c0482012-08-14 16:43:43 -0400146/* file descriptor associated with a file descriptor set */
147typedef struct MonFdsetFd MonFdsetFd;
148struct MonFdsetFd {
149 int fd;
150 bool removed;
151 char *opaque;
152 QLIST_ENTRY(MonFdsetFd) next;
153};
154
155/* file descriptor set containing fds passed via SCM_RIGHTS */
156typedef struct MonFdset MonFdset;
157struct MonFdset {
158 int64_t id;
159 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400160 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400161 QLIST_ENTRY(MonFdset) next;
162};
163
Markus Armbruster74358f22015-03-06 19:35:59 +0100164typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200165 QObject *id;
166 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100167 /*
168 * When a client connects, we're in capabilities negotiation mode.
169 * When command qmp_capabilities succeeds, we go into command
170 * mode.
171 */
172 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100173} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200174
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100175/*
176 * To prevent flooding clients, events can be throttled. The
177 * throttling is calculated globally, rather than per-Monitor
178 * instance.
179 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200180typedef struct MonitorQAPIEventState {
181 QAPIEvent event; /* Event being tracked */
182 int64_t rate; /* Minimum time (in ns) between two events */
183 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100184 QEMUTimer *timer; /* Timer for handling delayed events */
185 QObject *data; /* Event pending delayed dispatch */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200186} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100187
aliguori87127162009-03-05 23:01:29 +0000188struct Monitor {
189 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200190 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000191 int flags;
192 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400193 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200194
195 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400196 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200197 guint out_watch;
198
199 /* Read under either BQL or out_lock, written with BQL+out_lock. */
200 int mux_out;
201
aliguori731b0362009-03-05 23:01:42 +0000202 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100203 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200204 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200205 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000206 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800207 mon_cmd_t *cmd_table;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200208 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500209 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000210 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000211};
212
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300213/* QMP checker flags */
214#define QMP_ACCEPT_UNKNOWNS 1
215
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200216/* Protects mon_list, monitor_event_state. */
217static QemuMutex monitor_lock;
218
Blue Swirl72cf2d42009-09-12 07:36:22 +0000219static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400220static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400221static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000222
Wayne Xia816f8922011-10-12 11:32:41 +0800223static mon_cmd_t mon_cmds[];
224static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000225
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300226static const mon_cmd_t qmp_cmds[];
227
Markus Armbruster8631b602010-02-18 11:41:55 +0100228Monitor *cur_mon;
229Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000230
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100231static void monitor_command_cb(void *opaque, const char *cmdline,
232 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000233
Luiz Capitulino418173c2009-11-26 22:58:51 -0200234/* Return true if in control mode, false otherwise */
235static inline int monitor_ctrl_mode(const Monitor *mon)
236{
237 return (mon->flags & MONITOR_USE_CONTROL);
238}
239
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100240/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
241int monitor_cur_is_qmp(void)
242{
243 return cur_mon && monitor_ctrl_mode(cur_mon);
244}
245
Anthony Liguori7060b472011-09-02 12:34:50 -0500246void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000247{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200248 if (!mon->rs)
249 return;
250
aliguori731b0362009-03-05 23:01:42 +0000251 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
252 if (show_prompt)
253 readline_show_prompt(mon->rs);
254}
bellard6a00d602005-11-21 23:25:50 +0000255
Anthony Liguori7060b472011-09-02 12:34:50 -0500256int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
257 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000258{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100259 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000260 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
261 /* prompt is printed on return from the command handler */
262 return 0;
263 } else {
264 monitor_printf(mon, "terminal does not support password prompting\n");
265 return -ENOTTY;
266 }
aliguoribb5fc202009-03-05 23:01:15 +0000267}
268
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200269static void monitor_flush_locked(Monitor *mon);
270
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100271static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
272 void *opaque)
273{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200274 Monitor *mon = opaque;
275
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200276 qemu_mutex_lock(&mon->out_lock);
277 mon->out_watch = 0;
278 monitor_flush_locked(mon);
279 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100280 return FALSE;
281}
282
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200283/* Called with mon->out_lock held. */
284static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000285{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100286 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400287 size_t len;
288 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100289
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400290 if (mon->skip_flush) {
291 return;
292 }
293
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400294 buf = qstring_get_str(mon->outbuf);
295 len = qstring_get_length(mon->outbuf);
296
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200297 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400298 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200299 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
300 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400301 QDECREF(mon->outbuf);
302 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100303 return;
304 }
305 if (rc > 0) {
306 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400307 QString *tmp = qstring_from_str(buf + rc);
308 QDECREF(mon->outbuf);
309 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100310 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200311 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200312 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200313 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200314 }
bellard7e2515e2004-08-01 21:52:19 +0000315 }
316}
317
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200318void monitor_flush(Monitor *mon)
319{
320 qemu_mutex_lock(&mon->out_lock);
321 monitor_flush_locked(mon);
322 qemu_mutex_unlock(&mon->out_lock);
323}
324
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400325/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000326static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000327{
ths60fe76f2007-12-16 03:02:09 +0000328 char c;
aliguori731b0362009-03-05 23:01:42 +0000329
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200330 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000331 for(;;) {
332 c = *str++;
333 if (c == '\0')
334 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400335 if (c == '\n') {
336 qstring_append_chr(mon->outbuf, '\r');
337 }
338 qstring_append_chr(mon->outbuf, c);
339 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200340 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400341 }
bellard7e2515e2004-08-01 21:52:19 +0000342 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200343 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000344}
345
aliguori376253e2009-03-05 23:01:23 +0000346void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000347{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400348 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200349
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200350 if (!mon)
351 return;
352
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200353 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200354 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200355 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200356
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400357 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200358 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400359 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000360}
361
aliguori376253e2009-03-05 23:01:23 +0000362void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000363{
364 va_list ap;
365 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000366 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000367 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000368}
369
Stefan Weil8b7968f2010-09-23 21:28:05 +0200370static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
371 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000372{
373 va_list ap;
374 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000375 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000376 va_end(ap);
377 return 0;
378}
379
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200380static void monitor_json_emitter(Monitor *mon, const QObject *data)
381{
382 QString *json;
383
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200384 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
385 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200386 assert(json != NULL);
387
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200388 qstring_append_chr(json, '\n');
389 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200390
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200391 QDECREF(json);
392}
393
Markus Armbruster710aec92015-03-06 11:28:00 +0100394static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300395{
396 QObject *obj;
397
Markus Armbruster710aec92015-03-06 11:28:00 +0100398 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
399 ErrorClass_lookup[error_get_class(err)],
400 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300401
402 return qobject_to_qdict(obj);
403}
404
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100405static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100406 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200407{
408 QDict *qmp;
409
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100410 trace_monitor_protocol_emitter(mon);
411
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100412 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200413 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300414 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200415 if (data) {
416 qobject_incref(data);
417 qdict_put_obj(qmp, "return", data);
418 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200419 /* return an empty QDict by default */
420 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200421 }
422 } else {
423 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100424 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200425 }
426
Markus Armbruster74358f22015-03-06 19:35:59 +0100427 if (mon->qmp.id) {
428 qdict_put_obj(qmp, "id", mon->qmp.id);
429 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200430 }
431
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200432 monitor_json_emitter(mon, QOBJECT(qmp));
433 QDECREF(qmp);
434}
435
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200436
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200437static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100438
439/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200440 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200441 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100442 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200443static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100444{
445 Monitor *mon;
446
447 trace_monitor_protocol_event_emit(event, data);
448 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbrusterf994b252015-03-06 19:51:51 +0100449 if (monitor_ctrl_mode(mon) && mon->qmp.in_command_mode) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100450 monitor_json_emitter(mon, data);
451 }
452 }
453}
454
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100455/*
456 * Queue a new event for emission to Monitor instances,
457 * applying any rate limiting if required.
458 */
459static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200460monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100461{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200462 MonitorQAPIEventState *evstate;
463 assert(event < QAPI_EVENT_MAX);
Alex Blighbc72ad62013-08-21 16:03:08 +0100464 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100465
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200466 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100467 trace_monitor_protocol_event_queue(event,
468 data,
469 evstate->rate,
470 evstate->last,
471 now);
472
473 /* Rate limit of 0 indicates no throttling */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200474 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100475 if (!evstate->rate) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200476 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100477 evstate->last = now;
478 } else {
479 int64_t delta = now - evstate->last;
480 if (evstate->data ||
481 delta < evstate->rate) {
482 /* If there's an existing event pending, replace
483 * it with the new event, otherwise schedule a
484 * timer for delayed emission
485 */
486 if (evstate->data) {
487 qobject_decref(evstate->data);
488 } else {
489 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100490 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100491 }
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200492 evstate->data = QOBJECT(data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100493 qobject_incref(evstate->data);
494 } else {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200495 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100496 evstate->last = now;
497 }
498 }
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200499 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100500}
501
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100502/*
503 * The callback invoked by QemuTimer when a delayed
504 * event is ready to be emitted
505 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200506static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100507{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200508 MonitorQAPIEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100509 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100510
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100511 trace_monitor_protocol_event_handler(evstate->event,
512 evstate->data,
513 evstate->last,
514 now);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200515 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100516 if (evstate->data) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200517 monitor_qapi_event_emit(evstate->event, evstate->data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100518 qobject_decref(evstate->data);
519 evstate->data = NULL;
520 }
521 evstate->last = now;
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200522 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100523}
524
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100525/*
526 * @event: the event ID to be limited
527 * @rate: the rate limit in milliseconds
528 *
529 * Sets a rate limit on a particular event, so no
530 * more than 1 event will be emitted within @rate
531 * milliseconds
532 */
533static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200534monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100535{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200536 MonitorQAPIEventState *evstate;
537 assert(event < QAPI_EVENT_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100538
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200539 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100540
541 trace_monitor_protocol_event_throttle(event, rate);
542 evstate->event = event;
Wenchao Xia2f44a082014-06-24 16:34:00 -0700543 assert(rate * SCALE_MS <= INT64_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100544 evstate->rate = rate * SCALE_MS;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100545 evstate->last = 0;
546 evstate->data = NULL;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200547 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
548 SCALE_MS,
549 monitor_qapi_event_handler,
550 evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100551}
552
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200553static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100554{
Wenchao Xiae010ad82014-06-18 08:43:41 +0200555 /* Limit guest-triggerable events to 1 per second */
556 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
Wenchao Xia99eaf092014-06-18 08:43:42 +0200557 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
Wenchao Xiaaef9d312014-06-18 08:43:51 +0200558 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
Wenchao Xiafe069d92014-06-18 08:43:53 +0200559 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
560 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
Laszlo Erseke2ae6152014-06-26 17:50:02 +0200561 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100562
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200563 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200564}
565
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200566static int do_qmp_capabilities(Monitor *mon, const QDict *params,
567 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200568{
Markus Armbrusterf994b252015-03-06 19:51:51 +0100569 mon->qmp.in_command_mode = true;
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200570 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200571}
572
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100573static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200574
Wenchao Xiab01fe892013-08-27 20:38:19 +0800575static void monitor_data_init(Monitor *mon)
576{
577 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200578 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800579 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800580 /* Use *mon_cmds by default. */
581 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800582}
583
584static void monitor_data_destroy(Monitor *mon)
585{
586 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200587 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800588}
589
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200590char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
591 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200592{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200593 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200594 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200595
Wenchao Xiab01fe892013-08-27 20:38:19 +0800596 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400597 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200598
599 old_mon = cur_mon;
600 cur_mon = &hmp;
601
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200602 if (has_cpu_index) {
603 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200604 if (ret < 0) {
605 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200606 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
607 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200608 goto out;
609 }
610 }
611
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100612 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200613 cur_mon = old_mon;
614
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200615 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400616 if (qstring_get_length(hmp.outbuf) > 0) {
617 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200618 } else {
619 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200620 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200621 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200622
623out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800624 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200625 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200626}
627
bellard9dc39cb2004-03-14 21:38:27 +0000628static int compare_cmd(const char *name, const char *list)
629{
630 const char *p, *pstart;
631 int len;
632 len = strlen(name);
633 p = list;
634 for(;;) {
635 pstart = p;
636 p = strchr(p, '|');
637 if (!p)
638 p = pstart + strlen(pstart);
639 if ((p - pstart) == len && !memcmp(pstart, name, len))
640 return 1;
641 if (*p == '\0')
642 break;
643 p++;
644 }
645 return 0;
646}
647
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800648static int get_str(char *buf, int buf_size, const char **pp)
649{
650 const char *p;
651 char *q;
652 int c;
653
654 q = buf;
655 p = *pp;
656 while (qemu_isspace(*p)) {
657 p++;
658 }
659 if (*p == '\0') {
660 fail:
661 *q = '\0';
662 *pp = p;
663 return -1;
664 }
665 if (*p == '\"') {
666 p++;
667 while (*p != '\0' && *p != '\"') {
668 if (*p == '\\') {
669 p++;
670 c = *p++;
671 switch (c) {
672 case 'n':
673 c = '\n';
674 break;
675 case 'r':
676 c = '\r';
677 break;
678 case '\\':
679 case '\'':
680 case '\"':
681 break;
682 default:
683 qemu_printf("unsupported escape code: '\\%c'\n", c);
684 goto fail;
685 }
686 if ((q - buf) < buf_size - 1) {
687 *q++ = c;
688 }
689 } else {
690 if ((q - buf) < buf_size - 1) {
691 *q++ = *p;
692 }
693 p++;
694 }
695 }
696 if (*p != '\"') {
697 qemu_printf("unterminated string\n");
698 goto fail;
699 }
700 p++;
701 } else {
702 while (*p != '\0' && !qemu_isspace(*p)) {
703 if ((q - buf) < buf_size - 1) {
704 *q++ = *p;
705 }
706 p++;
707 }
708 }
709 *q = '\0';
710 *pp = p;
711 return 0;
712}
713
714#define MAX_ARGS 16
715
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800716static void free_cmdline_args(char **args, int nb_args)
717{
718 int i;
719
720 assert(nb_args <= MAX_ARGS);
721
722 for (i = 0; i < nb_args; i++) {
723 g_free(args[i]);
724 }
725
726}
727
728/*
729 * Parse the command line to get valid args.
730 * @cmdline: command line to be parsed.
731 * @pnb_args: location to store the number of args, must NOT be NULL.
732 * @args: location to store the args, which should be freed by caller, must
733 * NOT be NULL.
734 *
735 * Returns 0 on success, negative on failure.
736 *
737 * NOTE: this parser is an approximate form of the real command parser. Number
738 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
739 * return with failure.
740 */
741static int parse_cmdline(const char *cmdline,
742 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800743{
744 const char *p;
745 int nb_args, ret;
746 char buf[1024];
747
748 p = cmdline;
749 nb_args = 0;
750 for (;;) {
751 while (qemu_isspace(*p)) {
752 p++;
753 }
754 if (*p == '\0') {
755 break;
756 }
757 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800758 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800759 }
760 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800761 if (ret < 0) {
762 goto fail;
763 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800764 args[nb_args] = g_strdup(buf);
765 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800766 }
767 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800768 return 0;
769
770 fail:
771 free_cmdline_args(args, nb_args);
772 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800773}
774
Wenchao Xia66855492013-08-27 20:38:23 +0800775static void help_cmd_dump_one(Monitor *mon,
776 const mon_cmd_t *cmd,
777 char **prefix_args,
778 int prefix_args_nb)
779{
780 int i;
781
782 for (i = 0; i < prefix_args_nb; i++) {
783 monitor_printf(mon, "%s ", prefix_args[i]);
784 }
785 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
786}
787
788/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500789static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800790 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000791{
Anthony Liguoric227f092009-10-01 16:12:16 -0500792 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000793
Wenchao Xia66855492013-08-27 20:38:23 +0800794 /* No valid arg need to compare with, dump all in *cmds */
795 if (arg_index >= nb_args) {
796 for (cmd = cmds; cmd->name != NULL; cmd++) {
797 help_cmd_dump_one(mon, cmd, args, arg_index);
798 }
799 return;
800 }
801
802 /* Find one entry to dump */
803 for (cmd = cmds; cmd->name != NULL; cmd++) {
804 if (compare_cmd(args[arg_index], cmd->name)) {
805 if (cmd->sub_table) {
806 /* continue with next arg */
807 help_cmd_dump(mon, cmd->sub_table,
808 args, nb_args, arg_index + 1);
809 } else {
810 help_cmd_dump_one(mon, cmd, args, arg_index);
811 }
812 break;
813 }
bellard9dc39cb2004-03-14 21:38:27 +0000814 }
815}
816
aliguori376253e2009-03-05 23:01:23 +0000817static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000818{
Wenchao Xia66855492013-08-27 20:38:23 +0800819 char *args[MAX_ARGS];
820 int nb_args = 0;
821
822 /* 1. parse user input */
823 if (name) {
824 /* special case for log, directly dump and return */
825 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000826 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000827 monitor_printf(mon, "Log items (comma separated):\n");
828 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000829 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000830 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000831 }
Wenchao Xia66855492013-08-27 20:38:23 +0800832 return;
833 }
834
835 if (parse_cmdline(name, &nb_args, args) < 0) {
836 return;
bellardf193c792004-03-21 17:06:25 +0000837 }
bellard9dc39cb2004-03-14 21:38:27 +0000838 }
Wenchao Xia66855492013-08-27 20:38:23 +0800839
840 /* 2. dump the contents according to parsed args */
841 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
842
843 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000844}
845
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300846static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300847{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300848 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300849}
850
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100851static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530852{
853 const char *tp_name = qdict_get_str(qdict, "name");
854 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200855 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000856
Lluís Vilanova14101d02014-08-25 13:20:03 +0200857 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
858 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100859 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000860 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530861}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100862
Michael Rothc45a8162011-10-02 08:44:37 -0500863#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100864static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100865{
866 const char *op = qdict_get_try_str(qdict, "op");
867 const char *arg = qdict_get_try_str(qdict, "arg");
868
869 if (!op) {
870 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
871 } else if (!strcmp(op, "on")) {
872 st_set_trace_file_enabled(true);
873 } else if (!strcmp(op, "off")) {
874 st_set_trace_file_enabled(false);
875 } else if (!strcmp(op, "flush")) {
876 st_flush_trace_buffer();
877 } else if (!strcmp(op, "set")) {
878 if (arg) {
879 st_set_trace_file(arg);
880 }
881 } else {
882 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
883 help_cmd(mon, "trace-file");
884 }
885}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530886#endif
887
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100888static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000889{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300890 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000891}
892
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300893CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000894{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300895 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200896 const mon_cmd_t *cmd;
897
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300898 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200899 info = g_malloc0(sizeof(*info));
900 info->value = g_malloc0(sizeof(*info->value));
901 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200902
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300903 info->next = cmd_list;
904 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200905 }
906
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300907 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000908}
909
Daniel P. Berrange48608532012-05-21 17:59:51 +0100910EventInfoList *qmp_query_events(Error **errp)
911{
912 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200913 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100914
Wenchao Xia75175172014-06-18 08:43:54 +0200915 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
916 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100917 assert(event_name != NULL);
918 info = g_malloc0(sizeof(*info));
919 info->value = g_malloc0(sizeof(*info->value));
920 info->value->name = g_strdup(event_name);
921
922 info->next = ev_list;
923 ev_list = info;
924 }
925
926 return ev_list;
927}
928
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300929/* set the current CPU defined by the user */
930int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000931{
Andreas Färber55e5c282012-12-17 06:18:02 +0100932 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000933
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100934 cpu = qemu_get_cpu(cpu_index);
935 if (cpu == NULL) {
936 return -1;
bellard6a00d602005-11-21 23:25:50 +0000937 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200938 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100939 return 0;
bellard6a00d602005-11-21 23:25:50 +0000940}
941
Andreas Färber9349b4f2012-03-14 01:38:32 +0100942static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000943{
aliguori731b0362009-03-05 23:01:42 +0000944 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300945 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000946 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300947 cpu_synchronize_state(cur_mon->mon_cpu);
Andreas Färbercb446ec2013-05-01 14:24:52 +0200948 return cur_mon->mon_cpu->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000949}
950
Luiz Capitulino99b77962011-10-24 10:53:44 -0200951int monitor_get_cpu_index(void)
952{
Andreas Färber55e5c282012-12-17 06:18:02 +0100953 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
954 return cpu->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200955}
956
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100957static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000958{
Andreas Färber878096e2013-05-27 01:33:50 +0200959 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100960 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000961 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +0200962 cpu = ENV_GET_CPU(env);
963 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000964}
965
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100966static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000967{
aliguori376253e2009-03-05 23:01:23 +0000968 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200969 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000970}
971
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100972static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300973{
974 dump_opcount_info((FILE *)mon, monitor_fprintf);
975}
976
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100977static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000978{
979 int i;
bellard7e2515e2004-08-01 21:52:19 +0000980 const char *str;
ths3b46e622007-09-17 08:09:54 +0000981
aliguoricde76ee2009-03-05 23:01:51 +0000982 if (!mon->rs)
983 return;
bellard7e2515e2004-08-01 21:52:19 +0000984 i = 0;
985 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000986 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000987 if (!str)
988 break;
aliguori376253e2009-03-05 23:01:23 +0000989 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000990 i++;
bellardaa455482004-04-04 13:07:25 +0000991 }
992}
993
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100994static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +0000995{
Andreas Färber878096e2013-05-27 01:33:50 +0200996 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100997 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +0000998
999 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001000 cpu = ENV_GET_CPU(env);
1001 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001002}
j_mayer76a66252007-03-07 08:32:30 +00001003
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001004static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301005{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001006 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1007 TraceEventInfoList *elem;
1008
1009 for (elem = events; elem != NULL; elem = elem->next) {
1010 monitor_printf(mon, "%s : state %u\n",
1011 elem->value->name,
1012 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1013 }
1014 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301015}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301016
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001017void qmp_client_migrate_info(const char *protocol, const char *hostname,
1018 bool has_port, int64_t port,
1019 bool has_tls_port, int64_t tls_port,
1020 bool has_cert_subject, const char *cert_subject,
1021 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001022{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001023 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001024 if (!qemu_using_spice(errp)) {
1025 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001026 }
1027
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001028 if (!has_port && !has_tls_port) {
1029 error_set(errp, QERR_MISSING_PARAMETER, "port/tls-port");
1030 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001031 }
1032
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001033 if (qemu_spice_migrate_info(hostname,
1034 has_port ? port : -1,
1035 has_tls_port ? tls_port : -1,
1036 cert_subject)) {
1037 error_set(errp, QERR_UNDEFINED_ERROR);
1038 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001039 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001040 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001041 }
1042
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001043 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001044}
1045
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001046static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001047{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001048 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001049}
1050
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001051static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001052{
1053 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001054 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001055
bellard9307c4c2004-04-04 12:57:25 +00001056 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001057 mask = 0;
1058 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001059 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001060 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001061 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001062 return;
1063 }
1064 }
Peter Maydell24537a02013-02-11 16:41:23 +00001065 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001066}
1067
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001068static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001069{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001070 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001071 if (!option || !strcmp(option, "on")) {
1072 singlestep = 1;
1073 } else if (!strcmp(option, "off")) {
1074 singlestep = 0;
1075 } else {
1076 monitor_printf(mon, "unexpected option %s\n", option);
1077 }
1078}
1079
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001080static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001081{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001082 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001083 if (!device)
1084 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1085 if (gdbserver_start(device) < 0) {
1086 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1087 device);
1088 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001089 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001090 } else {
aliguori59030a82009-04-05 18:43:41 +00001091 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1092 device);
bellard8a7ddc32004-03-31 19:00:16 +00001093 }
1094}
1095
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001096static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001097{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001098 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001099 if (select_watchdog_action(action) == -1) {
1100 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1101 }
1102}
1103
aliguori376253e2009-03-05 23:01:23 +00001104static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001105{
aliguori376253e2009-03-05 23:01:23 +00001106 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001107 switch(c) {
1108 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001109 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001110 break;
1111 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001112 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001113 break;
1114 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001115 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001116 break;
1117 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001118 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001119 break;
1120 default:
1121 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001122 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001123 } else {
aliguori376253e2009-03-05 23:01:23 +00001124 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001125 }
1126 break;
1127 }
aliguori376253e2009-03-05 23:01:23 +00001128 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001129}
1130
aliguori376253e2009-03-05 23:01:23 +00001131static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001132 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001133{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001134 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001135 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001136 uint8_t buf[16];
1137 uint64_t v;
1138
1139 if (format == 'i') {
1140 int flags;
1141 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001142 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001143#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001144 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001145 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001146 } else if (wsize == 4) {
1147 flags = 0;
1148 } else {
bellard6a15fd12006-04-12 21:07:07 +00001149 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001150 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001151 if (env) {
1152#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001153 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001154 (env->segs[R_CS].flags & DESC_L_MASK))
1155 flags = 2;
1156 else
1157#endif
1158 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1159 flags = 1;
1160 }
bellard4c27ba22004-04-25 18:05:08 +00001161 }
1162#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001163#ifdef TARGET_PPC
1164 flags = msr_le << 16;
1165 flags |= env->bfd_mach;
1166#endif
aliguori376253e2009-03-05 23:01:23 +00001167 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001168 return;
1169 }
1170
1171 len = wsize * count;
1172 if (wsize == 1)
1173 line_size = 8;
1174 else
1175 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001176 max_digits = 0;
1177
1178 switch(format) {
1179 case 'o':
1180 max_digits = (wsize * 8 + 2) / 3;
1181 break;
1182 default:
1183 case 'x':
1184 max_digits = (wsize * 8) / 4;
1185 break;
1186 case 'u':
1187 case 'd':
1188 max_digits = (wsize * 8 * 10 + 32) / 33;
1189 break;
1190 case 'c':
1191 wsize = 1;
1192 break;
1193 }
1194
1195 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001196 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001197 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001198 else
aliguori376253e2009-03-05 23:01:23 +00001199 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001200 l = len;
1201 if (l > line_size)
1202 l = line_size;
1203 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001204 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001205 } else {
bellard6a00d602005-11-21 23:25:50 +00001206 env = mon_get_cpu();
Andreas Färberf17ec442013-06-29 19:40:58 +02001207 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001208 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001209 break;
1210 }
bellard9307c4c2004-04-04 12:57:25 +00001211 }
ths5fafdf22007-09-16 21:08:06 +00001212 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001213 while (i < l) {
1214 switch(wsize) {
1215 default:
1216 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001217 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001218 break;
1219 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001220 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001221 break;
1222 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001223 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001224 break;
1225 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001226 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001227 break;
1228 }
aliguori376253e2009-03-05 23:01:23 +00001229 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001230 switch(format) {
1231 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001232 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001233 break;
1234 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001235 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001236 break;
1237 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001238 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001239 break;
1240 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001241 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001242 break;
1243 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001244 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001245 break;
1246 }
1247 i += wsize;
1248 }
aliguori376253e2009-03-05 23:01:23 +00001249 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001250 addr += l;
1251 len -= l;
1252 }
1253}
1254
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001255static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001256{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001257 int count = qdict_get_int(qdict, "count");
1258 int format = qdict_get_int(qdict, "format");
1259 int size = qdict_get_int(qdict, "size");
1260 target_long addr = qdict_get_int(qdict, "addr");
1261
aliguori376253e2009-03-05 23:01:23 +00001262 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001263}
1264
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001265static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001266{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001267 int count = qdict_get_int(qdict, "count");
1268 int format = qdict_get_int(qdict, "format");
1269 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001270 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001271
aliguori376253e2009-03-05 23:01:23 +00001272 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001273}
1274
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001275static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001276{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001277 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001278 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001279
bellard9307c4c2004-04-04 12:57:25 +00001280 switch(format) {
1281 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001282 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001283 break;
1284 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001285 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001286 break;
1287 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001288 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001289 break;
1290 default:
1291 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001292 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001293 break;
1294 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001295 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001296 break;
1297 }
aliguori376253e2009-03-05 23:01:23 +00001298 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001299}
1300
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001301static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001302{
1303 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001304 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001305 uint32_t start = qdict_get_int(qdict, "start");
1306 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001307
1308 sum = 0;
1309 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001310 uint8_t val = address_space_ldub(&address_space_memory, addr,
1311 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001312 /* BSD sum algorithm ('sum' Unix command) */
1313 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001314 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001315 }
aliguori376253e2009-03-05 23:01:23 +00001316 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001317}
1318
bellard13224a82006-07-14 22:03:35 +00001319static int mouse_button_state;
1320
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001321static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001322{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001323 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001324 const char *dx_str = qdict_get_str(qdict, "dx_str");
1325 const char *dy_str = qdict_get_str(qdict, "dy_str");
1326 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001327
bellard13224a82006-07-14 22:03:35 +00001328 dx = strtol(dx_str, NULL, 0);
1329 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001330 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1331 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1332
1333 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001334 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001335 if (dz != 0) {
1336 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1337 qemu_input_queue_btn(NULL, button, true);
1338 qemu_input_event_sync();
1339 qemu_input_queue_btn(NULL, button, false);
1340 }
1341 }
1342 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001343}
1344
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001345static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001346{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001347 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1348 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1349 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1350 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1351 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001352 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001353
1354 if (mouse_button_state == button_state) {
1355 return;
1356 }
1357 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1358 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001359 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001360}
1361
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001362static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001363{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001364 int size = qdict_get_int(qdict, "size");
1365 int addr = qdict_get_int(qdict, "addr");
1366 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001367 uint32_t val;
1368 int suffix;
1369
1370 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001371 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001372 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001373 addr++;
1374 }
1375 addr &= 0xffff;
1376
1377 switch(size) {
1378 default:
1379 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001380 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001381 suffix = 'b';
1382 break;
1383 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001384 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001385 suffix = 'w';
1386 break;
1387 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001388 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001389 suffix = 'l';
1390 break;
1391 }
aliguori376253e2009-03-05 23:01:23 +00001392 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1393 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001394}
bellarda3a91a32004-06-04 11:06:21 +00001395
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001396static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001397{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001398 int size = qdict_get_int(qdict, "size");
1399 int addr = qdict_get_int(qdict, "addr");
1400 int val = qdict_get_int(qdict, "val");
1401
Jan Kiszkaf1147842009-07-14 10:20:11 +02001402 addr &= IOPORTS_MASK;
1403
1404 switch (size) {
1405 default:
1406 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001407 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001408 break;
1409 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001410 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001411 break;
1412 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001413 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001414 break;
1415 }
1416}
1417
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001418static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001419{
Gongleif1839932014-12-03 18:20:58 +00001420 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001421 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001422
Gongleif1839932014-12-03 18:20:58 +00001423 qemu_boot_set(bootdevice, &local_err);
1424 if (local_err) {
1425 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1426 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001427 } else {
Gongleif1839932014-12-03 18:20:58 +00001428 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001429 }
1430}
1431
bellardb86bda52004-09-18 19:32:46 +00001432#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001433static void print_pte(Monitor *mon, hwaddr addr,
1434 hwaddr pte,
1435 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001436{
Blue Swirld65aaf32010-12-11 18:56:24 +00001437#ifdef TARGET_X86_64
1438 if (addr & (1ULL << 47)) {
1439 addr |= -1LL << 48;
1440 }
1441#endif
1442 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1443 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001444 addr,
1445 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001446 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001447 pte & PG_GLOBAL_MASK ? 'G' : '-',
1448 pte & PG_PSE_MASK ? 'P' : '-',
1449 pte & PG_DIRTY_MASK ? 'D' : '-',
1450 pte & PG_ACCESSED_MASK ? 'A' : '-',
1451 pte & PG_PCD_MASK ? 'C' : '-',
1452 pte & PG_PWT_MASK ? 'T' : '-',
1453 pte & PG_USER_MASK ? 'U' : '-',
1454 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001455}
1456
Andreas Färber9349b4f2012-03-14 01:38:32 +01001457static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001458{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001459 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001460 uint32_t pgd, pde, pte;
1461
bellardb86bda52004-09-18 19:32:46 +00001462 pgd = env->cr[3] & ~0xfff;
1463 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001464 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001465 pde = le32_to_cpu(pde);
1466 if (pde & PG_PRESENT_MASK) {
1467 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001468 /* 4M pages */
1469 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001470 } else {
1471 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001472 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001473 pte = le32_to_cpu(pte);
1474 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001475 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001476 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001477 ~0xfff);
1478 }
1479 }
1480 }
1481 }
1482 }
1483}
1484
Andreas Färber9349b4f2012-03-14 01:38:32 +01001485static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001486{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001487 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001488 uint64_t pdpe, pde, pte;
1489 uint64_t pdp_addr, pd_addr, pt_addr;
1490
1491 pdp_addr = env->cr[3] & ~0x1f;
1492 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001493 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001494 pdpe = le64_to_cpu(pdpe);
1495 if (pdpe & PG_PRESENT_MASK) {
1496 pd_addr = pdpe & 0x3fffffffff000ULL;
1497 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001498 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001499 pde = le64_to_cpu(pde);
1500 if (pde & PG_PRESENT_MASK) {
1501 if (pde & PG_PSE_MASK) {
1502 /* 2M pages with PAE, CR4.PSE is ignored */
1503 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001504 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001505 } else {
1506 pt_addr = pde & 0x3fffffffff000ULL;
1507 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001508 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001509 pte = le64_to_cpu(pte);
1510 if (pte & PG_PRESENT_MASK) {
1511 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1512 + (l3 << 12),
1513 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001514 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001515 }
1516 }
1517 }
1518 }
1519 }
1520 }
1521 }
1522}
1523
1524#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001525static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001526{
1527 uint64_t l1, l2, l3, l4;
1528 uint64_t pml4e, pdpe, pde, pte;
1529 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1530
1531 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1532 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001533 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001534 pml4e = le64_to_cpu(pml4e);
1535 if (pml4e & PG_PRESENT_MASK) {
1536 pdp_addr = pml4e & 0x3fffffffff000ULL;
1537 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001538 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001539 pdpe = le64_to_cpu(pdpe);
1540 if (pdpe & PG_PRESENT_MASK) {
1541 if (pdpe & PG_PSE_MASK) {
1542 /* 1G pages, CR4.PSE is ignored */
1543 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1544 0x3ffffc0000000ULL);
1545 } else {
1546 pd_addr = pdpe & 0x3fffffffff000ULL;
1547 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001548 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001549 pde = le64_to_cpu(pde);
1550 if (pde & PG_PRESENT_MASK) {
1551 if (pde & PG_PSE_MASK) {
1552 /* 2M pages, CR4.PSE is ignored */
1553 print_pte(mon, (l1 << 39) + (l2 << 30) +
1554 (l3 << 21), pde,
1555 0x3ffffffe00000ULL);
1556 } else {
1557 pt_addr = pde & 0x3fffffffff000ULL;
1558 for (l4 = 0; l4 < 512; l4++) {
1559 cpu_physical_memory_read(pt_addr
1560 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001561 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001562 pte = le64_to_cpu(pte);
1563 if (pte & PG_PRESENT_MASK) {
1564 print_pte(mon, (l1 << 39) +
1565 (l2 << 30) +
1566 (l3 << 21) + (l4 << 12),
1567 pte & ~PG_PSE_MASK,
1568 0x3fffffffff000ULL);
1569 }
1570 }
1571 }
1572 }
1573 }
1574 }
1575 }
1576 }
1577 }
1578 }
1579}
1580#endif
1581
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001582static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001583{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001584 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001585
1586 env = mon_get_cpu();
1587
1588 if (!(env->cr[0] & CR0_PG_MASK)) {
1589 monitor_printf(mon, "PG disabled\n");
1590 return;
1591 }
1592 if (env->cr[4] & CR4_PAE_MASK) {
1593#ifdef TARGET_X86_64
1594 if (env->hflags & HF_LMA_MASK) {
1595 tlb_info_64(mon, env);
1596 } else
1597#endif
1598 {
1599 tlb_info_pae32(mon, env);
1600 }
1601 } else {
1602 tlb_info_32(mon, env);
1603 }
1604}
1605
Avi Kivitya8170e52012-10-23 12:30:10 +02001606static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001607 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001608 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001609{
bellard9746b152004-11-11 18:30:24 +00001610 int prot1;
1611 prot1 = *plast_prot;
1612 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001613 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001614 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1615 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001616 *pstart, end, end - *pstart,
1617 prot1 & PG_USER_MASK ? 'u' : '-',
1618 'r',
1619 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001620 }
1621 if (prot != 0)
1622 *pstart = end;
1623 else
1624 *pstart = -1;
1625 *plast_prot = prot;
1626 }
1627}
1628
Andreas Färber9349b4f2012-03-14 01:38:32 +01001629static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001630{
Austin Clementsb49ca722011-08-14 23:19:21 -04001631 unsigned int l1, l2;
1632 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001633 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001634 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001635
bellardb86bda52004-09-18 19:32:46 +00001636 pgd = env->cr[3] & ~0xfff;
1637 last_prot = 0;
1638 start = -1;
1639 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001640 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001641 pde = le32_to_cpu(pde);
1642 end = l1 << 22;
1643 if (pde & PG_PRESENT_MASK) {
1644 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1645 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001646 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001647 } else {
1648 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001649 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001650 pte = le32_to_cpu(pte);
1651 end = (l1 << 22) + (l2 << 12);
1652 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001653 prot = pte & pde &
1654 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001655 } else {
1656 prot = 0;
1657 }
aliguori376253e2009-03-05 23:01:23 +00001658 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001659 }
1660 }
1661 } else {
1662 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001663 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001664 }
1665 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001666 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001667 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001668}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001669
Andreas Färber9349b4f2012-03-14 01:38:32 +01001670static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001671{
Austin Clementsb49ca722011-08-14 23:19:21 -04001672 unsigned int l1, l2, l3;
1673 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001674 uint64_t pdpe, pde, pte;
1675 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001676 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001677
1678 pdp_addr = env->cr[3] & ~0x1f;
1679 last_prot = 0;
1680 start = -1;
1681 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001682 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001683 pdpe = le64_to_cpu(pdpe);
1684 end = l1 << 30;
1685 if (pdpe & PG_PRESENT_MASK) {
1686 pd_addr = pdpe & 0x3fffffffff000ULL;
1687 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001688 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001689 pde = le64_to_cpu(pde);
1690 end = (l1 << 30) + (l2 << 21);
1691 if (pde & PG_PRESENT_MASK) {
1692 if (pde & PG_PSE_MASK) {
1693 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1694 PG_PRESENT_MASK);
1695 mem_print(mon, &start, &last_prot, end, prot);
1696 } else {
1697 pt_addr = pde & 0x3fffffffff000ULL;
1698 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001699 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001700 pte = le64_to_cpu(pte);
1701 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1702 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001703 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1704 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001705 } else {
1706 prot = 0;
1707 }
1708 mem_print(mon, &start, &last_prot, end, prot);
1709 }
1710 }
1711 } else {
1712 prot = 0;
1713 mem_print(mon, &start, &last_prot, end, prot);
1714 }
1715 }
1716 } else {
1717 prot = 0;
1718 mem_print(mon, &start, &last_prot, end, prot);
1719 }
1720 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001721 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001722 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001723}
1724
1725
1726#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001727static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001728{
1729 int prot, last_prot;
1730 uint64_t l1, l2, l3, l4;
1731 uint64_t pml4e, pdpe, pde, pte;
1732 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1733
1734 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1735 last_prot = 0;
1736 start = -1;
1737 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001738 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001739 pml4e = le64_to_cpu(pml4e);
1740 end = l1 << 39;
1741 if (pml4e & PG_PRESENT_MASK) {
1742 pdp_addr = pml4e & 0x3fffffffff000ULL;
1743 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001744 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001745 pdpe = le64_to_cpu(pdpe);
1746 end = (l1 << 39) + (l2 << 30);
1747 if (pdpe & PG_PRESENT_MASK) {
1748 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001749 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1750 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001751 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001752 mem_print(mon, &start, &last_prot, end, prot);
1753 } else {
1754 pd_addr = pdpe & 0x3fffffffff000ULL;
1755 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001756 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001757 pde = le64_to_cpu(pde);
1758 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1759 if (pde & PG_PRESENT_MASK) {
1760 if (pde & PG_PSE_MASK) {
1761 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1762 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001763 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001764 mem_print(mon, &start, &last_prot, end, prot);
1765 } else {
1766 pt_addr = pde & 0x3fffffffff000ULL;
1767 for (l4 = 0; l4 < 512; l4++) {
1768 cpu_physical_memory_read(pt_addr
1769 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001770 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001771 pte = le64_to_cpu(pte);
1772 end = (l1 << 39) + (l2 << 30) +
1773 (l3 << 21) + (l4 << 12);
1774 if (pte & PG_PRESENT_MASK) {
1775 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1776 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001777 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001778 } else {
1779 prot = 0;
1780 }
1781 mem_print(mon, &start, &last_prot, end, prot);
1782 }
1783 }
1784 } else {
1785 prot = 0;
1786 mem_print(mon, &start, &last_prot, end, prot);
1787 }
1788 }
1789 }
1790 } else {
1791 prot = 0;
1792 mem_print(mon, &start, &last_prot, end, prot);
1793 }
1794 }
1795 } else {
1796 prot = 0;
1797 mem_print(mon, &start, &last_prot, end, prot);
1798 }
1799 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001800 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001801 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001802}
1803#endif
1804
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001805static void hmp_info_mem(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001806{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001807 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001808
1809 env = mon_get_cpu();
1810
1811 if (!(env->cr[0] & CR0_PG_MASK)) {
1812 monitor_printf(mon, "PG disabled\n");
1813 return;
1814 }
1815 if (env->cr[4] & CR4_PAE_MASK) {
1816#ifdef TARGET_X86_64
1817 if (env->hflags & HF_LMA_MASK) {
1818 mem_info_64(mon, env);
1819 } else
1820#endif
1821 {
1822 mem_info_pae32(mon, env);
1823 }
1824 } else {
1825 mem_info_32(mon, env);
1826 }
1827}
bellardb86bda52004-09-18 19:32:46 +00001828#endif
1829
aurel327c664e22009-03-03 06:12:22 +00001830#if defined(TARGET_SH4)
1831
aliguori376253e2009-03-05 23:01:23 +00001832static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001833{
aliguori376253e2009-03-05 23:01:23 +00001834 monitor_printf(mon, " tlb%i:\t"
1835 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1836 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1837 "dirty=%hhu writethrough=%hhu\n",
1838 idx,
1839 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1840 tlb->v, tlb->sh, tlb->c, tlb->pr,
1841 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001842}
1843
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001844static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001845{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001846 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00001847 int i;
1848
aliguori376253e2009-03-05 23:01:23 +00001849 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001850 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001851 print_tlb (mon, i, &env->itlb[i]);
1852 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001853 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001854 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001855}
1856
1857#endif
1858
Max Filippov692f7372012-01-07 20:02:40 +04001859#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001860static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001861{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001862 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00001863
1864 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1865}
1866#endif
1867
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001868static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001869{
1870 mtree_info((fprintf_function)monitor_printf, mon);
1871}
1872
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001873static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001874{
aliguorib28b6232009-04-22 20:20:29 +00001875 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001876 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001877 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001878
zhanghailiang5b009e42014-11-04 19:49:30 +08001879 node_mem = g_new0(uint64_t, nb_numa_nodes);
1880 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001881 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1882 for (i = 0; i < nb_numa_nodes; i++) {
1883 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001884 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001885 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001886 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001887 }
1888 }
1889 monitor_printf(mon, "\n");
1890 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001891 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001892 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001893 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001894}
1895
bellard5f1ce942006-02-08 22:40:15 +00001896#ifdef CONFIG_PROFILER
1897
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001898int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001899int64_t dev_time;
1900
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001901static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001902{
aliguori376253e2009-03-05 23:01:23 +00001903 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001904 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001905 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001906 tcg_time, tcg_time / (double)get_ticks_per_sec());
1907 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001908 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001909}
1910#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001911static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001912{
aliguori376253e2009-03-05 23:01:23 +00001913 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001914}
1915#endif
1916
bellardec36b692006-07-16 18:57:03 +00001917/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001918static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001919
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001920static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001921{
1922 int i;
1923 CaptureState *s;
1924
1925 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001926 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001927 s->ops.info (s->opaque);
1928 }
1929}
1930
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001931static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001932{
1933 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001934 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001935 CaptureState *s;
1936
1937 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1938 if (i == n) {
1939 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001940 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001941 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001942 return;
1943 }
1944 }
1945}
1946
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001947static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001948{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001949 const char *path = qdict_get_str(qdict, "path");
1950 int has_freq = qdict_haskey(qdict, "freq");
1951 int freq = qdict_get_try_int(qdict, "freq", -1);
1952 int has_bits = qdict_haskey(qdict, "bits");
1953 int bits = qdict_get_try_int(qdict, "bits", -1);
1954 int has_channels = qdict_haskey(qdict, "nchannels");
1955 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001956 CaptureState *s;
1957
Anthony Liguori7267c092011-08-20 22:09:37 -05001958 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001959
1960 freq = has_freq ? freq : 44100;
1961 bits = has_bits ? bits : 16;
1962 nchannels = has_channels ? nchannels : 2;
1963
1964 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001965 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001966 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001967 return;
bellardec36b692006-07-16 18:57:03 +00001968 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001969 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001970}
bellardec36b692006-07-16 18:57:03 +00001971
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001972static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001973{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001974 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001975
aliguori76655d62009-03-06 20:27:37 +00001976 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001977 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001978 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001979 return acl;
1980}
aliguori76655d62009-03-06 20:27:37 +00001981
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001982static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001983{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001984 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001985 qemu_acl *acl = find_acl(mon, aclname);
1986 qemu_acl_entry *entry;
1987 int i = 0;
1988
1989 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001990 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001991 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001992 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001993 i++;
1994 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001995 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001996 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001997 }
1998}
1999
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002000static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002001{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002002 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002003 qemu_acl *acl = find_acl(mon, aclname);
2004
2005 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002006 qemu_acl_reset(acl);
2007 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002008 }
2009}
aliguori76655d62009-03-06 20:27:37 +00002010
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002011static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002012{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002013 const char *aclname = qdict_get_str(qdict, "aclname");
2014 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002015 qemu_acl *acl = find_acl(mon, aclname);
2016
2017 if (acl) {
2018 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002019 acl->defaultDeny = 0;
2020 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002021 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002022 acl->defaultDeny = 1;
2023 monitor_printf(mon, "acl: policy set to 'deny'\n");
2024 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002025 monitor_printf(mon, "acl: unknown policy '%s', "
2026 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002027 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002028 }
2029}
aliguori76655d62009-03-06 20:27:37 +00002030
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002031static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002032{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002033 const char *aclname = qdict_get_str(qdict, "aclname");
2034 const char *match = qdict_get_str(qdict, "match");
2035 const char *policy = qdict_get_str(qdict, "policy");
2036 int has_index = qdict_haskey(qdict, "index");
2037 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002038 qemu_acl *acl = find_acl(mon, aclname);
2039 int deny, ret;
2040
2041 if (acl) {
2042 if (strcmp(policy, "allow") == 0) {
2043 deny = 0;
2044 } else if (strcmp(policy, "deny") == 0) {
2045 deny = 1;
2046 } else {
2047 monitor_printf(mon, "acl: unknown policy '%s', "
2048 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002049 return;
2050 }
aliguori28a76be2009-03-06 20:27:40 +00002051 if (has_index)
2052 ret = qemu_acl_insert(acl, deny, match, index);
2053 else
2054 ret = qemu_acl_append(acl, deny, match);
2055 if (ret < 0)
2056 monitor_printf(mon, "acl: unable to add acl entry\n");
2057 else
2058 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002059 }
2060}
aliguori76655d62009-03-06 20:27:37 +00002061
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002062static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002063{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002064 const char *aclname = qdict_get_str(qdict, "aclname");
2065 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002066 qemu_acl *acl = find_acl(mon, aclname);
2067 int ret;
aliguori76655d62009-03-06 20:27:37 +00002068
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002069 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002070 ret = qemu_acl_remove(acl, match);
2071 if (ret < 0)
2072 monitor_printf(mon, "acl: no matching acl entry\n");
2073 else
2074 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002075 }
2076}
2077
Huang Ying79c4f6b2009-06-23 10:05:14 +08002078#if defined(TARGET_I386)
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002079static void hmp_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002080{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002081 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002082 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002083 int cpu_index = qdict_get_int(qdict, "cpu_index");
2084 int bank = qdict_get_int(qdict, "bank");
2085 uint64_t status = qdict_get_int(qdict, "status");
2086 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2087 uint64_t addr = qdict_get_int(qdict, "addr");
2088 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002089 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002090
Jan Kiszka747461c2011-03-02 08:56:10 +01002091 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2092 flags |= MCE_INJECT_BROADCAST;
2093 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002094 cs = qemu_get_cpu(cpu_index);
2095 if (cs != NULL) {
2096 cpu = X86_CPU(cs);
2097 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2098 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002099 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002100}
2101#endif
2102
Corey Bryant208c9d12012-06-22 14:36:09 -04002103void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002104{
Anthony Liguoric227f092009-10-01 16:12:16 -05002105 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002106 int fd;
2107
Corey Bryant208c9d12012-06-22 14:36:09 -04002108 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002109 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002110 error_set(errp, QERR_FD_NOT_SUPPLIED);
2111 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002112 }
2113
2114 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002115 close(fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002116 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2117 "a name not starting with a digit");
2118 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002119 }
2120
Corey Bryant208c9d12012-06-22 14:36:09 -04002121 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002122 if (strcmp(monfd->name, fdname) != 0) {
2123 continue;
2124 }
2125
2126 close(monfd->fd);
2127 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002128 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002129 }
2130
Anthony Liguori7267c092011-08-20 22:09:37 -05002131 monfd = g_malloc0(sizeof(mon_fd_t));
2132 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002133 monfd->fd = fd;
2134
Corey Bryant208c9d12012-06-22 14:36:09 -04002135 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002136}
2137
Corey Bryant208c9d12012-06-22 14:36:09 -04002138void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002139{
Anthony Liguoric227f092009-10-01 16:12:16 -05002140 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002141
Corey Bryant208c9d12012-06-22 14:36:09 -04002142 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002143 if (strcmp(monfd->name, fdname) != 0) {
2144 continue;
2145 }
2146
Blue Swirl72cf2d42009-09-12 07:36:22 +00002147 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002148 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002149 g_free(monfd->name);
2150 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002151 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002152 }
2153
Corey Bryant208c9d12012-06-22 14:36:09 -04002154 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002155}
2156
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002157static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002158{
Luiz Capitulino13548692011-07-29 15:36:43 -03002159 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002160 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002161
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002162 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002163
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002164 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002165 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002166 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002167}
2168
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002169int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002170{
Anthony Liguoric227f092009-10-01 16:12:16 -05002171 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002172
Blue Swirl72cf2d42009-09-12 07:36:22 +00002173 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002174 int fd;
2175
2176 if (strcmp(monfd->name, fdname) != 0) {
2177 continue;
2178 }
2179
2180 fd = monfd->fd;
2181
2182 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002183 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002184 g_free(monfd->name);
2185 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002186
2187 return fd;
2188 }
2189
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002190 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002191 return -1;
2192}
2193
Corey Bryantba1c0482012-08-14 16:43:43 -04002194static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2195{
2196 MonFdsetFd *mon_fdset_fd;
2197 MonFdsetFd *mon_fdset_fd_next;
2198
2199 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002200 if ((mon_fdset_fd->removed ||
2201 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2202 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002203 close(mon_fdset_fd->fd);
2204 g_free(mon_fdset_fd->opaque);
2205 QLIST_REMOVE(mon_fdset_fd, next);
2206 g_free(mon_fdset_fd);
2207 }
2208 }
2209
Corey Bryantadb696f2012-08-14 16:43:47 -04002210 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002211 QLIST_REMOVE(mon_fdset, next);
2212 g_free(mon_fdset);
2213 }
2214}
2215
Corey Bryantefb87c12012-08-14 16:43:48 -04002216static void monitor_fdsets_cleanup(void)
2217{
2218 MonFdset *mon_fdset;
2219 MonFdset *mon_fdset_next;
2220
2221 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2222 monitor_fdset_cleanup(mon_fdset);
2223 }
2224}
2225
Corey Bryantba1c0482012-08-14 16:43:43 -04002226AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2227 const char *opaque, Error **errp)
2228{
2229 int fd;
2230 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002231 AddfdInfo *fdinfo;
2232
2233 fd = qemu_chr_fe_get_msgfd(mon->chr);
2234 if (fd == -1) {
2235 error_set(errp, QERR_FD_NOT_SUPPLIED);
2236 goto error;
2237 }
2238
Corey Bryante446f702012-10-18 15:19:32 -04002239 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2240 has_opaque, opaque, errp);
2241 if (fdinfo) {
2242 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002243 }
2244
Corey Bryantba1c0482012-08-14 16:43:43 -04002245error:
2246 if (fd != -1) {
2247 close(fd);
2248 }
2249 return NULL;
2250}
2251
2252void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2253{
2254 MonFdset *mon_fdset;
2255 MonFdsetFd *mon_fdset_fd;
2256 char fd_str[60];
2257
2258 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2259 if (mon_fdset->id != fdset_id) {
2260 continue;
2261 }
2262 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2263 if (has_fd) {
2264 if (mon_fdset_fd->fd != fd) {
2265 continue;
2266 }
2267 mon_fdset_fd->removed = true;
2268 break;
2269 } else {
2270 mon_fdset_fd->removed = true;
2271 }
2272 }
2273 if (has_fd && !mon_fdset_fd) {
2274 goto error;
2275 }
2276 monitor_fdset_cleanup(mon_fdset);
2277 return;
2278 }
2279
2280error:
2281 if (has_fd) {
2282 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2283 fdset_id, fd);
2284 } else {
2285 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2286 }
2287 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2288}
2289
2290FdsetInfoList *qmp_query_fdsets(Error **errp)
2291{
2292 MonFdset *mon_fdset;
2293 MonFdsetFd *mon_fdset_fd;
2294 FdsetInfoList *fdset_list = NULL;
2295
2296 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2297 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2298 FdsetFdInfoList *fdsetfd_list = NULL;
2299
2300 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2301 fdset_info->value->fdset_id = mon_fdset->id;
2302
2303 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2304 FdsetFdInfoList *fdsetfd_info;
2305
2306 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2307 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2308 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2309 if (mon_fdset_fd->opaque) {
2310 fdsetfd_info->value->has_opaque = true;
2311 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2312 } else {
2313 fdsetfd_info->value->has_opaque = false;
2314 }
2315
2316 fdsetfd_info->next = fdsetfd_list;
2317 fdsetfd_list = fdsetfd_info;
2318 }
2319
2320 fdset_info->value->fds = fdsetfd_list;
2321
2322 fdset_info->next = fdset_list;
2323 fdset_list = fdset_info;
2324 }
2325
2326 return fdset_list;
2327}
2328
Corey Bryante446f702012-10-18 15:19:32 -04002329AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2330 bool has_opaque, const char *opaque,
2331 Error **errp)
2332{
2333 MonFdset *mon_fdset = NULL;
2334 MonFdsetFd *mon_fdset_fd;
2335 AddfdInfo *fdinfo;
2336
2337 if (has_fdset_id) {
2338 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2339 /* Break if match found or match impossible due to ordering by ID */
2340 if (fdset_id <= mon_fdset->id) {
2341 if (fdset_id < mon_fdset->id) {
2342 mon_fdset = NULL;
2343 }
2344 break;
2345 }
2346 }
2347 }
2348
2349 if (mon_fdset == NULL) {
2350 int64_t fdset_id_prev = -1;
2351 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2352
2353 if (has_fdset_id) {
2354 if (fdset_id < 0) {
2355 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2356 "a non-negative value");
2357 return NULL;
2358 }
2359 /* Use specified fdset ID */
2360 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2361 mon_fdset_cur = mon_fdset;
2362 if (fdset_id < mon_fdset_cur->id) {
2363 break;
2364 }
2365 }
2366 } else {
2367 /* Use first available fdset ID */
2368 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2369 mon_fdset_cur = mon_fdset;
2370 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2371 fdset_id_prev = mon_fdset_cur->id;
2372 continue;
2373 }
2374 break;
2375 }
2376 }
2377
2378 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2379 if (has_fdset_id) {
2380 mon_fdset->id = fdset_id;
2381 } else {
2382 mon_fdset->id = fdset_id_prev + 1;
2383 }
2384
2385 /* The fdset list is ordered by fdset ID */
2386 if (!mon_fdset_cur) {
2387 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2388 } else if (mon_fdset->id < mon_fdset_cur->id) {
2389 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2390 } else {
2391 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2392 }
2393 }
2394
2395 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2396 mon_fdset_fd->fd = fd;
2397 mon_fdset_fd->removed = false;
2398 if (has_opaque) {
2399 mon_fdset_fd->opaque = g_strdup(opaque);
2400 }
2401 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2402
2403 fdinfo = g_malloc0(sizeof(*fdinfo));
2404 fdinfo->fdset_id = mon_fdset->id;
2405 fdinfo->fd = mon_fdset_fd->fd;
2406
2407 return fdinfo;
2408}
2409
Corey Bryantadb696f2012-08-14 16:43:47 -04002410int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2411{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002412#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002413 MonFdset *mon_fdset;
2414 MonFdsetFd *mon_fdset_fd;
2415 int mon_fd_flags;
2416
Corey Bryantadb696f2012-08-14 16:43:47 -04002417 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2418 if (mon_fdset->id != fdset_id) {
2419 continue;
2420 }
2421 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2422 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2423 if (mon_fd_flags == -1) {
2424 return -1;
2425 }
2426
2427 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2428 return mon_fdset_fd->fd;
2429 }
2430 }
2431 errno = EACCES;
2432 return -1;
2433 }
2434#endif
2435
2436 errno = ENOENT;
2437 return -1;
2438}
2439
2440int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2441{
2442 MonFdset *mon_fdset;
2443 MonFdsetFd *mon_fdset_fd_dup;
2444
2445 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2446 if (mon_fdset->id != fdset_id) {
2447 continue;
2448 }
2449 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2450 if (mon_fdset_fd_dup->fd == dup_fd) {
2451 return -1;
2452 }
2453 }
2454 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2455 mon_fdset_fd_dup->fd = dup_fd;
2456 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2457 return 0;
2458 }
2459 return -1;
2460}
2461
2462static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2463{
2464 MonFdset *mon_fdset;
2465 MonFdsetFd *mon_fdset_fd_dup;
2466
2467 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2468 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2469 if (mon_fdset_fd_dup->fd == dup_fd) {
2470 if (remove) {
2471 QLIST_REMOVE(mon_fdset_fd_dup, next);
2472 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2473 monitor_fdset_cleanup(mon_fdset);
2474 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002475 return -1;
2476 } else {
2477 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002478 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002479 }
2480 }
2481 }
2482 return -1;
2483}
2484
2485int monitor_fdset_dup_fd_find(int dup_fd)
2486{
2487 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2488}
2489
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002490void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002491{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002492 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002493}
2494
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002495int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002496{
2497 int fd;
2498 Error *local_err = NULL;
2499
2500 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002501 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002502 } else {
2503 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002504 if (fd == -1) {
2505 error_setg(&local_err, "Invalid file descriptor number '%s'",
2506 fdname);
2507 }
2508 }
2509 if (local_err) {
2510 error_propagate(errp, local_err);
2511 assert(fd == -1);
2512 } else {
2513 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002514 }
2515
2516 return fd;
2517}
2518
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002519/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002520static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002521 {
2522 .name = "version",
2523 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002524 .params = "",
2525 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002526 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002527 },
2528 {
2529 .name = "network",
2530 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002531 .params = "",
2532 .help = "show the network state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002533 .mhandler.cmd = hmp_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002534 },
2535 {
2536 .name = "chardev",
2537 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002538 .params = "",
2539 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002540 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002541 },
2542 {
2543 .name = "block",
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002544 .args_type = "nodes:-n,verbose:-v,device:B?",
2545 .params = "[-n] [-v] [device]",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002546 .help = "show info of one block device or all block devices "
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002547 "(-n: show named nodes; -v: show details)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002548 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002549 },
2550 {
2551 .name = "blockstats",
2552 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002553 .params = "",
2554 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002555 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002556 },
2557 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002558 .name = "block-jobs",
2559 .args_type = "",
2560 .params = "",
2561 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002562 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002563 },
2564 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002565 .name = "registers",
2566 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002567 .params = "",
2568 .help = "show the cpu registers",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002569 .mhandler.cmd = hmp_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002570 },
2571 {
2572 .name = "cpus",
2573 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002574 .params = "",
2575 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002576 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002577 },
2578 {
2579 .name = "history",
2580 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002581 .params = "",
2582 .help = "show the command line history",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002583 .mhandler.cmd = hmp_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002584 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002585#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2586 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002587 {
2588 .name = "irq",
2589 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002590 .params = "",
2591 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002592#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002593 .mhandler.cmd = sun4m_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002594#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002595 .mhandler.cmd = lm32_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002596#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002597 .mhandler.cmd = hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002598#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002599 },
2600 {
2601 .name = "pic",
2602 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002603 .params = "",
2604 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002605#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002606 .mhandler.cmd = sun4m_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002607#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002608 .mhandler.cmd = lm32_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002609#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002610 .mhandler.cmd = hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002611#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002612 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002613#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002614 {
2615 .name = "pci",
2616 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002617 .params = "",
2618 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002619 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002620 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002621#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002622 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002623 {
2624 .name = "tlb",
2625 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002626 .params = "",
2627 .help = "show virtual to physical memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002628 .mhandler.cmd = hmp_info_tlb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002629 },
aurel327c664e22009-03-03 06:12:22 +00002630#endif
2631#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002632 {
2633 .name = "mem",
2634 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002635 .params = "",
2636 .help = "show the active virtual memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002637 .mhandler.cmd = hmp_info_mem,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002638 },
bellardb86bda52004-09-18 19:32:46 +00002639#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002640 {
Blue Swirl314e2982011-09-11 20:22:05 +00002641 .name = "mtree",
2642 .args_type = "",
2643 .params = "",
2644 .help = "show memory tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002645 .mhandler.cmd = hmp_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002646 },
2647 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002648 .name = "jit",
2649 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002650 .params = "",
2651 .help = "show dynamic compiler info",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002652 .mhandler.cmd = hmp_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002653 },
2654 {
Max Filippov246ae242014-11-02 11:04:18 +03002655 .name = "opcount",
2656 .args_type = "",
2657 .params = "",
2658 .help = "show dynamic compiler opcode counters",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002659 .mhandler.cmd = hmp_info_opcount,
Max Filippov246ae242014-11-02 11:04:18 +03002660 },
2661 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002662 .name = "kvm",
2663 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 .params = "",
2665 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002666 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002667 },
2668 {
2669 .name = "numa",
2670 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 .params = "",
2672 .help = "show NUMA information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002673 .mhandler.cmd = hmp_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002674 },
2675 {
2676 .name = "usb",
2677 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002678 .params = "",
2679 .help = "show guest USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002680 .mhandler.cmd = hmp_info_usb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002681 },
2682 {
2683 .name = "usbhost",
2684 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002685 .params = "",
2686 .help = "show host USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002687 .mhandler.cmd = hmp_info_usbhost,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002688 },
2689 {
2690 .name = "profile",
2691 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002692 .params = "",
2693 .help = "show profiling information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002694 .mhandler.cmd = hmp_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002695 },
2696 {
2697 .name = "capture",
2698 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002699 .params = "",
2700 .help = "show capture information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002701 .mhandler.cmd = hmp_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002702 },
2703 {
2704 .name = "snapshots",
2705 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002706 .params = "",
2707 .help = "show the currently saved VM snapshots",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002708 .mhandler.cmd = hmp_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002709 },
2710 {
2711 .name = "status",
2712 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002713 .params = "",
2714 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002715 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002716 },
2717 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002718 .name = "mice",
2719 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002720 .params = "",
2721 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002722 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002723 },
2724 {
2725 .name = "vnc",
2726 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002727 .params = "",
2728 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002729 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002730 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002731#if defined(CONFIG_SPICE)
2732 {
2733 .name = "spice",
2734 .args_type = "",
2735 .params = "",
2736 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002737 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002738 },
2739#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002740 {
2741 .name = "name",
2742 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002743 .params = "",
2744 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002745 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002746 },
2747 {
2748 .name = "uuid",
2749 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002750 .params = "",
2751 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002752 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002753 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002754 {
2755 .name = "cpustats",
2756 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002757 .params = "",
2758 .help = "show CPU statistics",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002759 .mhandler.cmd = hmp_info_cpustats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 },
blueswir131a60e22007-10-26 18:42:59 +00002761#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002762 {
2763 .name = "usernet",
2764 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002765 .params = "",
2766 .help = "show user network stack connection states",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002767 .mhandler.cmd = hmp_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002768 },
blueswir131a60e22007-10-26 18:42:59 +00002769#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002770 {
2771 .name = "migrate",
2772 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002773 .params = "",
2774 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002775 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002776 },
2777 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002778 .name = "migrate_capabilities",
2779 .args_type = "",
2780 .params = "",
2781 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002782 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002783 },
2784 {
Liang Li50e9a622015-03-23 16:32:29 +08002785 .name = "migrate_parameters",
2786 .args_type = "",
2787 .params = "",
2788 .help = "show current migration parameters",
2789 .mhandler.cmd = hmp_info_migrate_parameters,
2790 },
2791 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002792 .name = "migrate_cache_size",
2793 .args_type = "",
2794 .params = "",
2795 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002796 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002797 },
2798 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002799 .name = "balloon",
2800 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002801 .params = "",
2802 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002803 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002804 },
2805 {
2806 .name = "qtree",
2807 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002808 .params = "",
2809 .help = "show device tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002810 .mhandler.cmd = hmp_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002811 },
2812 {
2813 .name = "qdm",
2814 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002815 .params = "",
2816 .help = "show qdev device model list",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002817 .mhandler.cmd = hmp_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002818 },
2819 {
Andreas Färbera01ff752014-05-07 17:03:18 +02002820 .name = "qom-tree",
2821 .args_type = "path:s?",
2822 .params = "[path]",
2823 .help = "show QOM composition tree",
2824 .mhandler.cmd = hmp_info_qom_tree,
2825 },
2826 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002827 .name = "roms",
2828 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002829 .params = "",
2830 .help = "show roms",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002831 .mhandler.cmd = hmp_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002832 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302833 {
2834 .name = "trace-events",
2835 .args_type = "",
2836 .params = "",
2837 .help = "show available trace-events & their state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002838 .mhandler.cmd = hmp_info_trace_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302839 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002840 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002841 .name = "tpm",
2842 .args_type = "",
2843 .params = "",
2844 .help = "show the TPM device",
2845 .mhandler.cmd = hmp_info_tpm,
2846 },
2847 {
Hu Taoeb1539b2014-05-14 17:43:35 +08002848 .name = "memdev",
2849 .args_type = "",
2850 .params = "",
Igor Mammedov8f4e5ac2014-06-19 16:14:43 +02002851 .help = "show memory backends",
Hu Taoeb1539b2014-05-14 17:43:35 +08002852 .mhandler.cmd = hmp_info_memdev,
2853 },
2854 {
Zhu Guihuaa6318922014-09-23 13:35:19 +08002855 .name = "memory-devices",
2856 .args_type = "",
2857 .params = "",
2858 .help = "show memory devices",
2859 .mhandler.cmd = hmp_info_memory_devices,
2860 },
2861 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002862 .name = NULL,
2863 },
bellard9dc39cb2004-03-14 21:38:27 +00002864};
2865
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002866/* mon_cmds and info_cmds would be sorted at runtime */
2867static mon_cmd_t mon_cmds[] = {
2868#include "hmp-commands.h"
2869 { NULL, NULL, },
2870};
2871
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002872static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002873#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002874 { /* NULL */ },
2875};
2876
bellard9307c4c2004-04-04 12:57:25 +00002877/*******************************************************************/
2878
2879static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002880static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002881
bellard92a31b12005-02-10 22:00:52 +00002882#define MD_TLONG 0
2883#define MD_I32 1
2884
bellard9307c4c2004-04-04 12:57:25 +00002885typedef struct MonitorDef {
2886 const char *name;
2887 int offset;
blueswir18662d652008-10-02 18:32:44 +00002888 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002889 int type;
bellard9307c4c2004-04-04 12:57:25 +00002890} MonitorDef;
2891
bellard57206fd2004-04-25 18:54:52 +00002892#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002893static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002894{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002895 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002896 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002897}
2898#endif
2899
bellarda541f292004-04-12 20:39:29 +00002900#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002901static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002902{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002903 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002904 unsigned int u;
2905 int i;
2906
2907 u = 0;
2908 for (i = 0; i < 8; i++)
Paolo Bonzinid2981182014-08-28 19:14:59 +02002909 u |= env->crf[i] << (32 - (4 * (i + 1)));
bellarda541f292004-04-12 20:39:29 +00002910
2911 return u;
2912}
2913
blueswir18662d652008-10-02 18:32:44 +00002914static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002915{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002916 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002917 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002918}
2919
blueswir18662d652008-10-02 18:32:44 +00002920static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002921{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002922 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002923 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002924}
bellard9fddaa02004-05-21 12:59:32 +00002925
blueswir18662d652008-10-02 18:32:44 +00002926static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002927{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002928 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002929 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002930}
2931
blueswir18662d652008-10-02 18:32:44 +00002932static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002933{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002934 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002935 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002936}
2937
blueswir18662d652008-10-02 18:32:44 +00002938static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002939{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002940 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002941 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002942}
bellarda541f292004-04-12 20:39:29 +00002943#endif
2944
bellarde95c8d52004-09-30 22:22:08 +00002945#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002946#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002947static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002948{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002949 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002950
2951 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002952}
bellard7b936c02005-10-30 17:05:13 +00002953#endif
bellarde95c8d52004-09-30 22:22:08 +00002954
blueswir18662d652008-10-02 18:32:44 +00002955static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002956{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002957 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002958 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002959}
2960#endif
2961
blueswir18662d652008-10-02 18:32:44 +00002962static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002963#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002964
2965#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01002966 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2967 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2968 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002969
Andreas Färbere59d1672012-02-16 00:40:47 +01002970 { "eax", offsetof(CPUX86State, regs[0]) },
2971 { "ecx", offsetof(CPUX86State, regs[1]) },
2972 { "edx", offsetof(CPUX86State, regs[2]) },
2973 { "ebx", offsetof(CPUX86State, regs[3]) },
2974 { "esp|sp", offsetof(CPUX86State, regs[4]) },
2975 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
2976 { "esi", offsetof(CPUX86State, regs[6]) },
2977 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002978#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01002979 { "r8", offsetof(CPUX86State, regs[8]) },
2980 { "r9", offsetof(CPUX86State, regs[9]) },
2981 { "r10", offsetof(CPUX86State, regs[10]) },
2982 { "r11", offsetof(CPUX86State, regs[11]) },
2983 { "r12", offsetof(CPUX86State, regs[12]) },
2984 { "r13", offsetof(CPUX86State, regs[13]) },
2985 { "r14", offsetof(CPUX86State, regs[14]) },
2986 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00002987#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01002988 { "eflags", offsetof(CPUX86State, eflags) },
2989 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00002990 SEG("cs", R_CS)
2991 SEG("ds", R_DS)
2992 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002993 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002994 SEG("fs", R_FS)
2995 SEG("gs", R_GS)
2996 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002997#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002998 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01002999 { "r0", offsetof(CPUPPCState, gpr[0]) },
3000 { "r1", offsetof(CPUPPCState, gpr[1]) },
3001 { "r2", offsetof(CPUPPCState, gpr[2]) },
3002 { "r3", offsetof(CPUPPCState, gpr[3]) },
3003 { "r4", offsetof(CPUPPCState, gpr[4]) },
3004 { "r5", offsetof(CPUPPCState, gpr[5]) },
3005 { "r6", offsetof(CPUPPCState, gpr[6]) },
3006 { "r7", offsetof(CPUPPCState, gpr[7]) },
3007 { "r8", offsetof(CPUPPCState, gpr[8]) },
3008 { "r9", offsetof(CPUPPCState, gpr[9]) },
3009 { "r10", offsetof(CPUPPCState, gpr[10]) },
3010 { "r11", offsetof(CPUPPCState, gpr[11]) },
3011 { "r12", offsetof(CPUPPCState, gpr[12]) },
3012 { "r13", offsetof(CPUPPCState, gpr[13]) },
3013 { "r14", offsetof(CPUPPCState, gpr[14]) },
3014 { "r15", offsetof(CPUPPCState, gpr[15]) },
3015 { "r16", offsetof(CPUPPCState, gpr[16]) },
3016 { "r17", offsetof(CPUPPCState, gpr[17]) },
3017 { "r18", offsetof(CPUPPCState, gpr[18]) },
3018 { "r19", offsetof(CPUPPCState, gpr[19]) },
3019 { "r20", offsetof(CPUPPCState, gpr[20]) },
3020 { "r21", offsetof(CPUPPCState, gpr[21]) },
3021 { "r22", offsetof(CPUPPCState, gpr[22]) },
3022 { "r23", offsetof(CPUPPCState, gpr[23]) },
3023 { "r24", offsetof(CPUPPCState, gpr[24]) },
3024 { "r25", offsetof(CPUPPCState, gpr[25]) },
3025 { "r26", offsetof(CPUPPCState, gpr[26]) },
3026 { "r27", offsetof(CPUPPCState, gpr[27]) },
3027 { "r28", offsetof(CPUPPCState, gpr[28]) },
3028 { "r29", offsetof(CPUPPCState, gpr[29]) },
3029 { "r30", offsetof(CPUPPCState, gpr[30]) },
3030 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003031 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003032 { "f0", offsetof(CPUPPCState, fpr[0]) },
3033 { "f1", offsetof(CPUPPCState, fpr[1]) },
3034 { "f2", offsetof(CPUPPCState, fpr[2]) },
3035 { "f3", offsetof(CPUPPCState, fpr[3]) },
3036 { "f4", offsetof(CPUPPCState, fpr[4]) },
3037 { "f5", offsetof(CPUPPCState, fpr[5]) },
3038 { "f6", offsetof(CPUPPCState, fpr[6]) },
3039 { "f7", offsetof(CPUPPCState, fpr[7]) },
3040 { "f8", offsetof(CPUPPCState, fpr[8]) },
3041 { "f9", offsetof(CPUPPCState, fpr[9]) },
3042 { "f10", offsetof(CPUPPCState, fpr[10]) },
3043 { "f11", offsetof(CPUPPCState, fpr[11]) },
3044 { "f12", offsetof(CPUPPCState, fpr[12]) },
3045 { "f13", offsetof(CPUPPCState, fpr[13]) },
3046 { "f14", offsetof(CPUPPCState, fpr[14]) },
3047 { "f15", offsetof(CPUPPCState, fpr[15]) },
3048 { "f16", offsetof(CPUPPCState, fpr[16]) },
3049 { "f17", offsetof(CPUPPCState, fpr[17]) },
3050 { "f18", offsetof(CPUPPCState, fpr[18]) },
3051 { "f19", offsetof(CPUPPCState, fpr[19]) },
3052 { "f20", offsetof(CPUPPCState, fpr[20]) },
3053 { "f21", offsetof(CPUPPCState, fpr[21]) },
3054 { "f22", offsetof(CPUPPCState, fpr[22]) },
3055 { "f23", offsetof(CPUPPCState, fpr[23]) },
3056 { "f24", offsetof(CPUPPCState, fpr[24]) },
3057 { "f25", offsetof(CPUPPCState, fpr[25]) },
3058 { "f26", offsetof(CPUPPCState, fpr[26]) },
3059 { "f27", offsetof(CPUPPCState, fpr[27]) },
3060 { "f28", offsetof(CPUPPCState, fpr[28]) },
3061 { "f29", offsetof(CPUPPCState, fpr[29]) },
3062 { "f30", offsetof(CPUPPCState, fpr[30]) },
3063 { "f31", offsetof(CPUPPCState, fpr[31]) },
3064 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003065 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003066 { "nip|pc", offsetof(CPUPPCState, nip) },
3067 { "lr", offsetof(CPUPPCState, lr) },
3068 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003069 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003070 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003071 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003072 { "msr", 0, &monitor_get_msr, },
3073 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003074 { "tbu", 0, &monitor_get_tbu, },
3075 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003076 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003077 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3078 { "sr0", offsetof(CPUPPCState, sr[0]) },
3079 { "sr1", offsetof(CPUPPCState, sr[1]) },
3080 { "sr2", offsetof(CPUPPCState, sr[2]) },
3081 { "sr3", offsetof(CPUPPCState, sr[3]) },
3082 { "sr4", offsetof(CPUPPCState, sr[4]) },
3083 { "sr5", offsetof(CPUPPCState, sr[5]) },
3084 { "sr6", offsetof(CPUPPCState, sr[6]) },
3085 { "sr7", offsetof(CPUPPCState, sr[7]) },
3086 { "sr8", offsetof(CPUPPCState, sr[8]) },
3087 { "sr9", offsetof(CPUPPCState, sr[9]) },
3088 { "sr10", offsetof(CPUPPCState, sr[10]) },
3089 { "sr11", offsetof(CPUPPCState, sr[11]) },
3090 { "sr12", offsetof(CPUPPCState, sr[12]) },
3091 { "sr13", offsetof(CPUPPCState, sr[13]) },
3092 { "sr14", offsetof(CPUPPCState, sr[14]) },
3093 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003094 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003095 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003096
Andreas Färbere59d1672012-02-16 00:40:47 +01003097 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3098 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
Tom Musta04f1f782013-09-25 17:41:13 +10003099 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3100 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3101 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
Andreas Färbere59d1672012-02-16 00:40:47 +01003102 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3103 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3104 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3105 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3106 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3107 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3108 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3109 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3110 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3111 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3112 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3113 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3114 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3115 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3116 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3117 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3118 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3119 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3120 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3121 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3122 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3123 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3124 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3125 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3126 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3127 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3128 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3129 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3130 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3131 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3132 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3133 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3134 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3135 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3136 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3137 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3138 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3139 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3140 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3141 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3142 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3143 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3144 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3145 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3146 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3147 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3148 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3149 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3150 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3151 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3152 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3153 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3154 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3155 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3156 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3157 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3158 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3159 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3160 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3161 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3162 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3163 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3164 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3165 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003166
bellarde95c8d52004-09-30 22:22:08 +00003167#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003168 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3169 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3170 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3171 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3172 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3173 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3174 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3175 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003176 { "o0", 0, monitor_get_reg },
3177 { "o1", 1, monitor_get_reg },
3178 { "o2", 2, monitor_get_reg },
3179 { "o3", 3, monitor_get_reg },
3180 { "o4", 4, monitor_get_reg },
3181 { "o5", 5, monitor_get_reg },
3182 { "o6", 6, monitor_get_reg },
3183 { "o7", 7, monitor_get_reg },
3184 { "l0", 8, monitor_get_reg },
3185 { "l1", 9, monitor_get_reg },
3186 { "l2", 10, monitor_get_reg },
3187 { "l3", 11, monitor_get_reg },
3188 { "l4", 12, monitor_get_reg },
3189 { "l5", 13, monitor_get_reg },
3190 { "l6", 14, monitor_get_reg },
3191 { "l7", 15, monitor_get_reg },
3192 { "i0", 16, monitor_get_reg },
3193 { "i1", 17, monitor_get_reg },
3194 { "i2", 18, monitor_get_reg },
3195 { "i3", 19, monitor_get_reg },
3196 { "i4", 20, monitor_get_reg },
3197 { "i5", 21, monitor_get_reg },
3198 { "i6", 22, monitor_get_reg },
3199 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003200 { "pc", offsetof(CPUSPARCState, pc) },
3201 { "npc", offsetof(CPUSPARCState, npc) },
3202 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003203#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003204 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003205 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003206#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003207 { "tbr", offsetof(CPUSPARCState, tbr) },
3208 { "fsr", offsetof(CPUSPARCState, fsr) },
3209 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3210 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3211 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3212 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3213 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3214 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3215 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3216 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3217 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3218 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3219 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3220 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3221 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3222 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3223 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3224 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3225 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3226 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3227 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3228 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3229 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3230 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3231 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3232 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3233 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3234 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3235 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3236 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3237 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3238 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3239 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3240 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003241#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003242 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3243 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3244 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3245 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3246 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3247 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3248 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3249 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3250 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3251 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3252 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3253 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3254 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3255 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3256 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3257 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3258 { "asi", offsetof(CPUSPARCState, asi) },
3259 { "pstate", offsetof(CPUSPARCState, pstate) },
3260 { "cansave", offsetof(CPUSPARCState, cansave) },
3261 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3262 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3263 { "wstate", offsetof(CPUSPARCState, wstate) },
3264 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3265 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003266#endif
bellard9307c4c2004-04-04 12:57:25 +00003267#endif
3268 { NULL },
3269};
3270
Stefan Weil9c3175c2013-08-22 21:30:09 +02003271static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3272expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003273{
Fam Zheng277acfe2013-08-20 10:58:21 +08003274 va_list ap;
3275 va_start(ap, fmt);
3276 monitor_vprintf(mon, fmt, ap);
3277 monitor_printf(mon, "\n");
3278 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003279 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003280}
3281
Markus Armbruster09b94182010-01-20 13:07:30 +01003282/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003283static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003284{
blueswir18662d652008-10-02 18:32:44 +00003285 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003286 void *ptr;
3287
bellard9307c4c2004-04-04 12:57:25 +00003288 for(md = monitor_defs; md->name != NULL; md++) {
3289 if (compare_cmd(name, md->name)) {
3290 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003291 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003292 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003293 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003294 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003295 switch(md->type) {
3296 case MD_I32:
3297 *pval = *(int32_t *)ptr;
3298 break;
3299 case MD_TLONG:
3300 *pval = *(target_long *)ptr;
3301 break;
3302 default:
3303 *pval = 0;
3304 break;
3305 }
bellard9307c4c2004-04-04 12:57:25 +00003306 }
3307 return 0;
3308 }
3309 }
3310 return -1;
3311}
3312
3313static void next(void)
3314{
Blue Swirl660f11b2009-07-31 21:16:51 +00003315 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003316 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003317 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003318 pch++;
3319 }
3320}
3321
aliguori376253e2009-03-05 23:01:23 +00003322static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003323
aliguori376253e2009-03-05 23:01:23 +00003324static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003325{
blueswir1c2efc952007-09-25 17:28:42 +00003326 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003327 char *p;
bellard6a00d602005-11-21 23:25:50 +00003328 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003329
3330 switch(*pch) {
3331 case '+':
3332 next();
aliguori376253e2009-03-05 23:01:23 +00003333 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003334 break;
3335 case '-':
3336 next();
aliguori376253e2009-03-05 23:01:23 +00003337 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003338 break;
3339 case '~':
3340 next();
aliguori376253e2009-03-05 23:01:23 +00003341 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003342 break;
3343 case '(':
3344 next();
aliguori376253e2009-03-05 23:01:23 +00003345 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003346 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003347 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003348 }
3349 next();
3350 break;
bellard81d09122004-07-14 17:21:37 +00003351 case '\'':
3352 pch++;
3353 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003354 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003355 n = *pch;
3356 pch++;
3357 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003358 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003359 next();
3360 break;
bellard9307c4c2004-04-04 12:57:25 +00003361 case '$':
3362 {
3363 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003364 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003365
bellard9307c4c2004-04-04 12:57:25 +00003366 pch++;
3367 q = buf;
3368 while ((*pch >= 'a' && *pch <= 'z') ||
3369 (*pch >= 'A' && *pch <= 'Z') ||
3370 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003371 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003372 if ((q - buf) < sizeof(buf) - 1)
3373 *q++ = *pch;
3374 pch++;
3375 }
blueswir1cd390082008-11-16 13:53:32 +00003376 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003377 pch++;
3378 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003379 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003380 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003381 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003382 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003383 }
3384 break;
3385 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003386 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003387 n = 0;
3388 break;
3389 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003390 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003391 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003392 if (errno == ERANGE) {
3393 expr_error(mon, "number too large");
3394 }
bellard9307c4c2004-04-04 12:57:25 +00003395 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003396 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003397 }
3398 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003399 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003400 pch++;
3401 break;
3402 }
3403 return n;
3404}
3405
3406
aliguori376253e2009-03-05 23:01:23 +00003407static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003408{
blueswir1c2efc952007-09-25 17:28:42 +00003409 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003410 int op;
ths3b46e622007-09-17 08:09:54 +00003411
aliguori376253e2009-03-05 23:01:23 +00003412 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003413 for(;;) {
3414 op = *pch;
3415 if (op != '*' && op != '/' && op != '%')
3416 break;
3417 next();
aliguori376253e2009-03-05 23:01:23 +00003418 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003419 switch(op) {
3420 default:
3421 case '*':
3422 val *= val2;
3423 break;
3424 case '/':
3425 case '%':
ths5fafdf22007-09-16 21:08:06 +00003426 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003427 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003428 if (op == '/')
3429 val /= val2;
3430 else
3431 val %= val2;
3432 break;
3433 }
3434 }
3435 return val;
3436}
3437
aliguori376253e2009-03-05 23:01:23 +00003438static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003439{
blueswir1c2efc952007-09-25 17:28:42 +00003440 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003441 int op;
bellard9307c4c2004-04-04 12:57:25 +00003442
aliguori376253e2009-03-05 23:01:23 +00003443 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003444 for(;;) {
3445 op = *pch;
3446 if (op != '&' && op != '|' && op != '^')
3447 break;
3448 next();
aliguori376253e2009-03-05 23:01:23 +00003449 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003450 switch(op) {
3451 default:
3452 case '&':
3453 val &= val2;
3454 break;
3455 case '|':
3456 val |= val2;
3457 break;
3458 case '^':
3459 val ^= val2;
3460 break;
3461 }
3462 }
3463 return val;
3464}
3465
aliguori376253e2009-03-05 23:01:23 +00003466static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003467{
blueswir1c2efc952007-09-25 17:28:42 +00003468 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003469 int op;
bellard9307c4c2004-04-04 12:57:25 +00003470
aliguori376253e2009-03-05 23:01:23 +00003471 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003472 for(;;) {
3473 op = *pch;
3474 if (op != '+' && op != '-')
3475 break;
3476 next();
aliguori376253e2009-03-05 23:01:23 +00003477 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003478 if (op == '+')
3479 val += val2;
3480 else
3481 val -= val2;
3482 }
3483 return val;
3484}
3485
aliguori376253e2009-03-05 23:01:23 +00003486static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003487{
3488 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003489 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003490 *pp = pch;
3491 return -1;
3492 }
blueswir1cd390082008-11-16 13:53:32 +00003493 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003494 pch++;
aliguori376253e2009-03-05 23:01:23 +00003495 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003496 *pp = pch;
3497 return 0;
3498}
3499
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003500static int get_double(Monitor *mon, double *pval, const char **pp)
3501{
3502 const char *p = *pp;
3503 char *tailp;
3504 double d;
3505
3506 d = strtod(p, &tailp);
3507 if (tailp == p) {
3508 monitor_printf(mon, "Number expected\n");
3509 return -1;
3510 }
3511 if (d != d || d - d != 0) {
3512 /* NaN or infinity */
3513 monitor_printf(mon, "Bad number\n");
3514 return -1;
3515 }
3516 *pval = d;
3517 *pp = tailp;
3518 return 0;
3519}
3520
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003521/*
3522 * Store the command-name in cmdname, and return a pointer to
3523 * the remaining of the command string.
3524 */
3525static const char *get_command_name(const char *cmdline,
3526 char *cmdname, size_t nlen)
3527{
3528 size_t len;
3529 const char *p, *pstart;
3530
3531 p = cmdline;
3532 while (qemu_isspace(*p))
3533 p++;
3534 if (*p == '\0')
3535 return NULL;
3536 pstart = p;
3537 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3538 p++;
3539 len = p - pstart;
3540 if (len > nlen - 1)
3541 len = nlen - 1;
3542 memcpy(cmdname, pstart, len);
3543 cmdname[len] = '\0';
3544 return p;
3545}
3546
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003547/**
3548 * Read key of 'type' into 'key' and return the current
3549 * 'type' pointer.
3550 */
3551static char *key_get_info(const char *type, char **key)
3552{
3553 size_t len;
3554 char *p, *str;
3555
3556 if (*type == ',')
3557 type++;
3558
3559 p = strchr(type, ':');
3560 if (!p) {
3561 *key = NULL;
3562 return NULL;
3563 }
3564 len = p - type;
3565
Anthony Liguori7267c092011-08-20 22:09:37 -05003566 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003567 memcpy(str, type, len);
3568 str[len] = '\0';
3569
3570 *key = str;
3571 return ++p;
3572}
3573
bellard9307c4c2004-04-04 12:57:25 +00003574static int default_fmt_format = 'x';
3575static int default_fmt_size = 4;
3576
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003577static int is_valid_option(const char *c, const char *typestr)
3578{
3579 char option[3];
3580
3581 option[0] = '-';
3582 option[1] = *c;
3583 option[2] = '\0';
3584
3585 typestr = strstr(typestr, option);
3586 return (typestr != NULL);
3587}
3588
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003589static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3590 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003591{
3592 const mon_cmd_t *cmd;
3593
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003594 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003595 if (compare_cmd(cmdname, cmd->name)) {
3596 return cmd;
3597 }
3598 }
3599
3600 return NULL;
3601}
3602
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003603static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3604{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003605 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003606}
3607
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003608/*
3609 * Parse @cmdline according to command table @table.
3610 * If @cmdline is blank, return NULL.
3611 * If it can't be parsed, report to @mon, and return NULL.
3612 * Else, insert command arguments into @qdict, and return the command.
Peter Maydell085d8132013-03-18 17:20:07 +00003613 * If a sub-command table exists, and if @cmdline contains an additional string
3614 * for a sub-command, this function will try to search the sub-command table.
3615 * If no additional string for a sub-command is present, this function will
3616 * return the command found in @table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003617 * Do not assume the returned command points into @table! It doesn't
3618 * when the command is a sub-command.
3619 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003620static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003621 const char *cmdline,
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003622 int start,
3623 mon_cmd_t *table,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003624 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003625{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003626 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003627 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003628 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003629 char cmdname[256];
3630 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003631 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003632
3633#ifdef DEBUG
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003634 monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start);
bellard9dc39cb2004-03-14 21:38:27 +00003635#endif
ths3b46e622007-09-17 08:09:54 +00003636
bellard9307c4c2004-04-04 12:57:25 +00003637 /* extract the command name */
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003638 p = get_command_name(cmdline + start, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003639 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003640 return NULL;
ths3b46e622007-09-17 08:09:54 +00003641
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003642 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003643 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003644 monitor_printf(mon, "unknown command: '%.*s'\n",
3645 (int)(p - cmdline), cmdline);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003646 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003647 }
bellard9307c4c2004-04-04 12:57:25 +00003648
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003649 /* filter out following useless space */
3650 while (qemu_isspace(*p)) {
3651 p++;
3652 }
3653 /* search sub command */
3654 if (cmd->sub_table != NULL) {
3655 /* check if user set additional command */
3656 if (*p == '\0') {
3657 return cmd;
3658 }
3659 return monitor_parse_command(mon, cmdline, p - cmdline,
3660 cmd->sub_table, qdict);
3661 }
3662
bellard9307c4c2004-04-04 12:57:25 +00003663 /* parse the parameters */
3664 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003665 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003666 typestr = key_get_info(typestr, &key);
3667 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003668 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003669 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003670 typestr++;
3671 switch(c) {
3672 case 'F':
bellard81d09122004-07-14 17:21:37 +00003673 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003674 case 's':
3675 {
3676 int ret;
ths3b46e622007-09-17 08:09:54 +00003677
blueswir1cd390082008-11-16 13:53:32 +00003678 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003679 p++;
3680 if (*typestr == '?') {
3681 typestr++;
3682 if (*p == '\0') {
3683 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003684 break;
bellard9307c4c2004-04-04 12:57:25 +00003685 }
3686 }
3687 ret = get_str(buf, sizeof(buf), &p);
3688 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003689 switch(c) {
3690 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003691 monitor_printf(mon, "%s: filename expected\n",
3692 cmdname);
bellard81d09122004-07-14 17:21:37 +00003693 break;
3694 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003695 monitor_printf(mon, "%s: block device name expected\n",
3696 cmdname);
bellard81d09122004-07-14 17:21:37 +00003697 break;
3698 default:
aliguori376253e2009-03-05 23:01:23 +00003699 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003700 break;
3701 }
bellard9307c4c2004-04-04 12:57:25 +00003702 goto fail;
3703 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003704 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003705 }
3706 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003707 case 'O':
3708 {
3709 QemuOptsList *opts_list;
3710 QemuOpts *opts;
3711
3712 opts_list = qemu_find_opts(key);
3713 if (!opts_list || opts_list->desc->name) {
3714 goto bad_type;
3715 }
3716 while (qemu_isspace(*p)) {
3717 p++;
3718 }
3719 if (!*p)
3720 break;
3721 if (get_str(buf, sizeof(buf), &p) < 0) {
3722 goto fail;
3723 }
3724 opts = qemu_opts_parse(opts_list, buf, 1);
3725 if (!opts) {
3726 goto fail;
3727 }
3728 qemu_opts_to_qdict(opts, qdict);
3729 qemu_opts_del(opts);
3730 }
3731 break;
bellard9307c4c2004-04-04 12:57:25 +00003732 case '/':
3733 {
3734 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003735
blueswir1cd390082008-11-16 13:53:32 +00003736 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003737 p++;
3738 if (*p == '/') {
3739 /* format found */
3740 p++;
3741 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003742 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003743 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003744 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003745 count = count * 10 + (*p - '0');
3746 p++;
3747 }
3748 }
3749 size = -1;
3750 format = -1;
3751 for(;;) {
3752 switch(*p) {
3753 case 'o':
3754 case 'd':
3755 case 'u':
3756 case 'x':
3757 case 'i':
3758 case 'c':
3759 format = *p++;
3760 break;
3761 case 'b':
3762 size = 1;
3763 p++;
3764 break;
3765 case 'h':
3766 size = 2;
3767 p++;
3768 break;
3769 case 'w':
3770 size = 4;
3771 p++;
3772 break;
3773 case 'g':
3774 case 'L':
3775 size = 8;
3776 p++;
3777 break;
3778 default:
3779 goto next;
3780 }
3781 }
3782 next:
blueswir1cd390082008-11-16 13:53:32 +00003783 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003784 monitor_printf(mon, "invalid char in format: '%c'\n",
3785 *p);
bellard9307c4c2004-04-04 12:57:25 +00003786 goto fail;
3787 }
bellard9307c4c2004-04-04 12:57:25 +00003788 if (format < 0)
3789 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003790 if (format != 'i') {
3791 /* for 'i', not specifying a size gives -1 as size */
3792 if (size < 0)
3793 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003794 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003795 }
bellard9307c4c2004-04-04 12:57:25 +00003796 default_fmt_format = format;
3797 } else {
3798 count = 1;
3799 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003800 if (format != 'i') {
3801 size = default_fmt_size;
3802 } else {
3803 size = -1;
3804 }
bellard9307c4c2004-04-04 12:57:25 +00003805 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003806 qdict_put(qdict, "count", qint_from_int(count));
3807 qdict_put(qdict, "format", qint_from_int(format));
3808 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003809 }
3810 break;
3811 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003812 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003813 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003814 {
blueswir1c2efc952007-09-25 17:28:42 +00003815 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003816
blueswir1cd390082008-11-16 13:53:32 +00003817 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003818 p++;
bellard34405572004-06-08 00:55:58 +00003819 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003820 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003821 if (*p == '\0') {
3822 typestr++;
3823 break;
3824 }
bellard34405572004-06-08 00:55:58 +00003825 } else {
3826 if (*p == '.') {
3827 p++;
blueswir1cd390082008-11-16 13:53:32 +00003828 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003829 p++;
bellard34405572004-06-08 00:55:58 +00003830 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003831 typestr++;
3832 break;
bellard34405572004-06-08 00:55:58 +00003833 }
3834 }
bellard13224a82006-07-14 22:03:35 +00003835 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003836 }
aliguori376253e2009-03-05 23:01:23 +00003837 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003838 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003839 /* Check if 'i' is greater than 32-bit */
3840 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3841 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3842 monitor_printf(mon, "integer is for 32-bit values\n");
3843 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003844 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003845 if (val < 0) {
3846 monitor_printf(mon, "enter a positive value\n");
3847 goto fail;
3848 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003849 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003850 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003851 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003852 }
3853 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003854 case 'o':
3855 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003856 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003857 char *end;
3858
3859 while (qemu_isspace(*p)) {
3860 p++;
3861 }
3862 if (*typestr == '?') {
3863 typestr++;
3864 if (*p == '\0') {
3865 break;
3866 }
3867 }
3868 val = strtosz(p, &end);
3869 if (val < 0) {
3870 monitor_printf(mon, "invalid size\n");
3871 goto fail;
3872 }
3873 qdict_put(qdict, key, qint_from_int(val));
3874 p = end;
3875 }
3876 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003877 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003878 {
3879 double val;
3880
3881 while (qemu_isspace(*p))
3882 p++;
3883 if (*typestr == '?') {
3884 typestr++;
3885 if (*p == '\0') {
3886 break;
3887 }
3888 }
3889 if (get_double(mon, &val, &p) < 0) {
3890 goto fail;
3891 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003892 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003893 switch (*p) {
3894 case 'm':
3895 val /= 1e3; p += 2; break;
3896 case 'u':
3897 val /= 1e6; p += 2; break;
3898 case 'n':
3899 val /= 1e9; p += 2; break;
3900 }
3901 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003902 if (*p && !qemu_isspace(*p)) {
3903 monitor_printf(mon, "Unknown unit suffix\n");
3904 goto fail;
3905 }
3906 qdict_put(qdict, key, qfloat_from_double(val));
3907 }
3908 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003909 case 'b':
3910 {
3911 const char *beg;
3912 int val;
3913
3914 while (qemu_isspace(*p)) {
3915 p++;
3916 }
3917 beg = p;
3918 while (qemu_isgraph(*p)) {
3919 p++;
3920 }
3921 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3922 val = 1;
3923 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3924 val = 0;
3925 } else {
3926 monitor_printf(mon, "Expected 'on' or 'off'\n");
3927 goto fail;
3928 }
3929 qdict_put(qdict, key, qbool_from_int(val));
3930 }
3931 break;
bellard9307c4c2004-04-04 12:57:25 +00003932 case '-':
3933 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003934 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003935 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003936 /* option */
ths3b46e622007-09-17 08:09:54 +00003937
bellard9307c4c2004-04-04 12:57:25 +00003938 c = *typestr++;
3939 if (c == '\0')
3940 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003941 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003942 p++;
bellard9307c4c2004-04-04 12:57:25 +00003943 if (*p == '-') {
3944 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003945 if(c != *p) {
3946 if(!is_valid_option(p, typestr)) {
3947
3948 monitor_printf(mon, "%s: unsupported option -%c\n",
3949 cmdname, *p);
3950 goto fail;
3951 } else {
3952 skip_key = 1;
3953 }
bellard9307c4c2004-04-04 12:57:25 +00003954 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003955 if(skip_key) {
3956 p = tmp;
3957 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003958 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003959 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003960 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003961 }
bellard9307c4c2004-04-04 12:57:25 +00003962 }
bellard9307c4c2004-04-04 12:57:25 +00003963 }
3964 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003965 case 'S':
3966 {
3967 /* package all remaining string */
3968 int len;
3969
3970 while (qemu_isspace(*p)) {
3971 p++;
3972 }
3973 if (*typestr == '?') {
3974 typestr++;
3975 if (*p == '\0') {
3976 /* no remaining string: NULL argument */
3977 break;
3978 }
3979 }
3980 len = strlen(p);
3981 if (len <= 0) {
3982 monitor_printf(mon, "%s: string expected\n",
3983 cmdname);
3984 break;
3985 }
3986 qdict_put(qdict, key, qstring_from_str(p));
3987 p += len;
3988 }
3989 break;
bellard9307c4c2004-04-04 12:57:25 +00003990 default:
3991 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003992 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003993 goto fail;
3994 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003995 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003996 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003997 }
3998 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003999 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004000 p++;
4001 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004002 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4003 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004004 goto fail;
4005 }
4006
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004007 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004008
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004009fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004010 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004011 return NULL;
4012}
4013
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004014void monitor_set_error(Monitor *mon, QError *qerror)
4015{
4016 /* report only the first error */
4017 if (!mon->error) {
4018 mon->error = qerror;
4019 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004020 QDECREF(qerror);
4021 }
4022}
4023
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004024static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004025{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004026 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004027 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004028
4029 qdict = qdict_new();
4030
Wenchao Xia77172392013-08-27 20:38:20 +08004031 cmd = monitor_parse_command(mon, cmdline, 0, mon->cmd_table, qdict);
Markus Armbruster8a4f5012015-03-05 18:50:05 +01004032 if (cmd) {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004033 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004034 }
4035
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004036 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004037}
4038
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004039static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004040{
4041 const char *p, *pstart;
4042 char cmd[128];
4043 int len;
4044
4045 p = list;
4046 for(;;) {
4047 pstart = p;
4048 p = strchr(p, '|');
4049 if (!p)
4050 p = pstart + strlen(pstart);
4051 len = p - pstart;
4052 if (len > sizeof(cmd) - 2)
4053 len = sizeof(cmd) - 2;
4054 memcpy(cmd, pstart, len);
4055 cmd[len] = '\0';
4056 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004057 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004058 }
4059 if (*p == '\0')
4060 break;
4061 p++;
4062 }
4063}
4064
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004065static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004066{
4067 DIR *ffs;
4068 struct dirent *d;
4069 char path[1024];
4070 char file[1024], file_prefix[1024];
4071 int input_path_len;
4072 const char *p;
4073
ths5fafdf22007-09-16 21:08:06 +00004074 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004075 if (!p) {
4076 input_path_len = 0;
4077 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004078 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004079 } else {
4080 input_path_len = p - input + 1;
4081 memcpy(path, input, input_path_len);
4082 if (input_path_len > sizeof(path) - 1)
4083 input_path_len = sizeof(path) - 1;
4084 path[input_path_len] = '\0';
4085 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4086 }
4087#ifdef DEBUG_COMPLETION
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004088 monitor_printf(mon, "input='%s' path='%s' prefix='%s'\n",
aliguori376253e2009-03-05 23:01:23 +00004089 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004090#endif
4091 ffs = opendir(path);
4092 if (!ffs)
4093 return;
4094 for(;;) {
4095 struct stat sb;
4096 d = readdir(ffs);
4097 if (!d)
4098 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004099
4100 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4101 continue;
4102 }
4103
bellard81d09122004-07-14 17:21:37 +00004104 if (strstart(d->d_name, file_prefix, NULL)) {
4105 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004106 if (input_path_len < sizeof(file))
4107 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4108 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004109 /* stat the file to find out if it's a directory.
4110 * In that case add a slash to speed up typing long paths
4111 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004112 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004113 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004114 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004115 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004116 }
4117 }
4118 closedir(ffs);
4119}
4120
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004121static const char *next_arg_type(const char *typestr)
4122{
4123 const char *p = strchr(typestr, ':');
4124 return (p != NULL ? ++p : typestr);
4125}
4126
Hani Benhabiles40d19392014-05-07 23:41:30 +01004127static void add_completion_option(ReadLineState *rs, const char *str,
4128 const char *option)
4129{
4130 if (!str || !option) {
4131 return;
4132 }
4133 if (!strncmp(option, str, strlen(str))) {
4134 readline_add_completion(rs, option);
4135 }
4136}
4137
Hani Benhabiles13e315d2014-05-07 23:41:29 +01004138void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4139{
4140 size_t len;
4141 ChardevBackendInfoList *list, *start;
4142
4143 if (nb_args != 2) {
4144 return;
4145 }
4146 len = strlen(str);
4147 readline_set_completion_index(rs, len);
4148
4149 start = list = qmp_query_chardev_backends(NULL);
4150 while (list) {
4151 const char *chr_name = list->value->name;
4152
4153 if (!strncmp(chr_name, str, len)) {
4154 readline_add_completion(rs, chr_name);
4155 }
4156 list = list->next;
4157 }
4158 qapi_free_ChardevBackendInfoList(start);
4159}
4160
Hani Benhabilesb162b492014-05-07 23:41:31 +01004161void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4162{
4163 size_t len;
4164 int i;
4165
4166 if (nb_args != 2) {
4167 return;
4168 }
4169 len = strlen(str);
4170 readline_set_completion_index(rs, len);
4171 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
4172 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
4173 }
4174}
4175
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004176void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004177{
4178 GSList *list, *elt;
4179 size_t len;
4180
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004181 if (nb_args != 2) {
4182 return;
4183 }
4184
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004185 len = strlen(str);
4186 readline_set_completion_index(rs, len);
4187 list = elt = object_class_get_list(TYPE_DEVICE, false);
4188 while (elt) {
4189 const char *name;
4190 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
4191 TYPE_DEVICE);
4192 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004193
4194 if (!dc->cannot_instantiate_with_device_add_yet
4195 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004196 readline_add_completion(rs, name);
4197 }
4198 elt = elt->next;
4199 }
4200 g_slist_free(list);
4201}
4202
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004203void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004204{
4205 GSList *list, *elt;
4206 size_t len;
4207
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004208 if (nb_args != 2) {
4209 return;
4210 }
4211
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004212 len = strlen(str);
4213 readline_set_completion_index(rs, len);
4214 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
4215 while (elt) {
4216 const char *name;
4217
4218 name = object_class_get_name(OBJECT_CLASS(elt->data));
4219 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
4220 readline_add_completion(rs, name);
4221 }
4222 elt = elt->next;
4223 }
4224 g_slist_free(list);
4225}
4226
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004227static void peripheral_device_del_completion(ReadLineState *rs,
4228 const char *str, size_t len)
4229{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004230 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
4231 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004232
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004233 list = qdev_build_hotpluggable_device_list(peripheral);
4234 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004235 return;
4236 }
4237
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004238 for (item = list; item; item = g_slist_next(item)) {
4239 DeviceState *dev = item->data;
4240
4241 if (dev->id && !strncmp(str, dev->id, len)) {
4242 readline_add_completion(rs, dev->id);
4243 }
4244 }
4245
4246 g_slist_free(list);
4247}
4248
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01004249void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4250{
4251 size_t len;
4252 ChardevInfoList *list, *start;
4253
4254 if (nb_args != 2) {
4255 return;
4256 }
4257 len = strlen(str);
4258 readline_set_completion_index(rs, len);
4259
4260 start = list = qmp_query_chardev(NULL);
4261 while (list) {
4262 ChardevInfo *chr = list->value;
4263
4264 if (!strncmp(chr->label, str, len)) {
4265 readline_add_completion(rs, chr->label);
4266 }
4267 list = list->next;
4268 }
4269 qapi_free_ChardevInfoList(start);
4270}
4271
Hani Benhabiles8e597772014-05-27 23:39:30 +01004272static void ringbuf_completion(ReadLineState *rs, const char *str)
4273{
4274 size_t len;
4275 ChardevInfoList *list, *start;
4276
4277 len = strlen(str);
4278 readline_set_completion_index(rs, len);
4279
4280 start = list = qmp_query_chardev(NULL);
4281 while (list) {
4282 ChardevInfo *chr_info = list->value;
4283
4284 if (!strncmp(chr_info->label, str, len)) {
4285 CharDriverState *chr = qemu_chr_find(chr_info->label);
4286 if (chr && chr_is_ringbuf(chr)) {
4287 readline_add_completion(rs, chr_info->label);
4288 }
4289 }
4290 list = list->next;
4291 }
4292 qapi_free_ChardevInfoList(start);
4293}
4294
Hani Benhabiles8e597772014-05-27 23:39:30 +01004295void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
4296{
4297 if (nb_args != 2) {
4298 return;
4299 }
4300 ringbuf_completion(rs, str);
4301}
4302
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004303void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
4304{
4305 size_t len;
4306
4307 if (nb_args != 2) {
4308 return;
4309 }
4310
4311 len = strlen(str);
4312 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004313 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004314}
4315
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004316void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004317{
4318 ObjectPropertyInfoList *list, *start;
4319 size_t len;
4320
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004321 if (nb_args != 2) {
4322 return;
4323 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004324 len = strlen(str);
4325 readline_set_completion_index(rs, len);
4326
4327 start = list = qmp_qom_list("/objects", NULL);
4328 while (list) {
4329 ObjectPropertyInfo *info = list->value;
4330
4331 if (!strncmp(info->type, "child<", 5)
4332 && !strncmp(info->name, str, len)) {
4333 readline_add_completion(rs, info->name);
4334 }
4335 list = list->next;
4336 }
4337 qapi_free_ObjectPropertyInfoList(start);
4338}
4339
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004340void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
4341{
4342 int i;
4343 char *sep;
4344 size_t len;
4345
4346 if (nb_args != 2) {
4347 return;
4348 }
4349 sep = strrchr(str, '-');
4350 if (sep) {
4351 str = sep + 1;
4352 }
4353 len = strlen(str);
4354 readline_set_completion_index(rs, len);
4355 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4356 if (!strncmp(str, QKeyCode_lookup[i], len)) {
4357 readline_add_completion(rs, QKeyCode_lookup[i]);
4358 }
4359 }
4360}
4361
Hani Benhabiles40d19392014-05-07 23:41:30 +01004362void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
4363{
4364 size_t len;
4365
4366 len = strlen(str);
4367 readline_set_completion_index(rs, len);
4368 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08004369 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01004370 int count, i;
4371 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004372 NET_CLIENT_OPTIONS_KIND_NONE,
4373 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004374 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01004375 const char *name = ncs[i]->name;
4376 if (!strncmp(str, name, len)) {
4377 readline_add_completion(rs, name);
4378 }
4379 }
4380 } else if (nb_args == 3) {
4381 add_completion_option(rs, str, "on");
4382 add_completion_option(rs, str, "off");
4383 }
4384}
4385
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004386void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
4387{
4388 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08004389 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004390
4391 if (nb_args != 2) {
4392 return;
4393 }
4394
4395 len = strlen(str);
4396 readline_set_completion_index(rs, len);
4397 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08004398 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004399 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004400 QemuOpts *opts;
4401 const char *name = ncs[i]->name;
4402 if (strncmp(str, name, len)) {
4403 continue;
4404 }
4405 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
4406 if (opts) {
4407 readline_add_completion(rs, name);
4408 }
4409 }
4410}
4411
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004412void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
4413{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004414 int i;
4415
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004416 if (nb_args != 2) {
4417 return;
4418 }
4419 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004420 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
4421 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
4422 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004423}
4424
Hani Benhabilesc68a0402014-05-27 23:39:32 +01004425void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
4426 const char *str)
4427{
4428 size_t len;
4429
4430 len = strlen(str);
4431 readline_set_completion_index(rs, len);
4432 if (nb_args == 2) {
4433 int i;
4434 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
4435 const char *name = MigrationCapability_lookup[i];
4436 if (!strncmp(str, name, len)) {
4437 readline_add_completion(rs, name);
4438 }
4439 }
4440 } else if (nb_args == 3) {
4441 add_completion_option(rs, str, "on");
4442 add_completion_option(rs, str, "off");
4443 }
4444}
4445
Liang Li50e9a622015-03-23 16:32:29 +08004446void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
4447 const char *str)
4448{
4449 size_t len;
4450
4451 len = strlen(str);
4452 readline_set_completion_index(rs, len);
4453 if (nb_args == 2) {
4454 int i;
4455 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
4456 const char *name = MigrationParameter_lookup[i];
4457 if (!strncmp(str, name, len)) {
4458 readline_add_completion(rs, name);
4459 }
4460 }
4461 }
4462}
4463
Hani Benhabilese3bb5322014-05-27 23:39:34 +01004464void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
4465{
4466 int i;
4467 size_t len;
4468 if (nb_args != 2) {
4469 return;
4470 }
4471 len = strlen(str);
4472 readline_set_completion_index(rs, len);
4473 for (i = 0; host_net_devices[i]; i++) {
4474 if (!strncmp(host_net_devices[i], str, len)) {
4475 readline_add_completion(rs, host_net_devices[i]);
4476 }
4477 }
4478}
4479
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004480void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4481{
Jason Wangeaed4832015-04-23 14:21:38 +08004482 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004483 int count, i, len;
4484
4485 len = strlen(str);
4486 readline_set_completion_index(rs, len);
4487 if (nb_args == 2) {
4488 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004489 NET_CLIENT_OPTIONS_KIND_NONE,
4490 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004491 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004492 int id;
4493 char name[16];
4494
4495 if (net_hub_id_for_client(ncs[i], &id)) {
4496 continue;
4497 }
4498 snprintf(name, sizeof(name), "%d", id);
4499 if (!strncmp(str, name, len)) {
4500 readline_add_completion(rs, name);
4501 }
4502 }
4503 return;
4504 } else if (nb_args == 3) {
4505 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004506 NET_CLIENT_OPTIONS_KIND_NIC,
4507 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004508 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08004509 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004510 const char *name;
4511
Jason Wang2c4681f2015-02-02 15:06:38 +08004512 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
4513 net_hub_id_for_client(ncs[i], &id)) {
4514 continue;
4515 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004516 name = ncs[i]->name;
4517 if (!strncmp(str, name, len)) {
4518 readline_add_completion(rs, name);
4519 }
4520 }
4521 return;
4522 }
4523}
4524
Hani Benhabilesb21631f2014-05-27 23:39:37 +01004525static void vm_completion(ReadLineState *rs, const char *str)
4526{
4527 size_t len;
4528 BlockDriverState *bs = NULL;
4529
4530 len = strlen(str);
4531 readline_set_completion_index(rs, len);
4532 while ((bs = bdrv_next(bs))) {
4533 SnapshotInfoList *snapshots, *snapshot;
4534
4535 if (!bdrv_can_snapshot(bs)) {
4536 continue;
4537 }
4538 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
4539 continue;
4540 }
4541 snapshot = snapshots;
4542 while (snapshot) {
4543 char *completion = snapshot->value->name;
4544 if (!strncmp(str, completion, len)) {
4545 readline_add_completion(rs, completion);
4546 }
4547 completion = snapshot->value->id;
4548 if (!strncmp(str, completion, len)) {
4549 readline_add_completion(rs, completion);
4550 }
4551 snapshot = snapshot->next;
4552 }
4553 qapi_free_SnapshotInfoList(snapshots);
4554 }
4555
4556}
4557
4558void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
4559{
4560 if (nb_args == 2) {
4561 vm_completion(rs, str);
4562 }
4563}
4564
4565void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
4566{
4567 if (nb_args == 2) {
4568 vm_completion(rs, str);
4569 }
4570}
4571
Wenchao Xiac35b6402013-08-27 20:38:24 +08004572static void monitor_find_completion_by_table(Monitor *mon,
4573 const mon_cmd_t *cmd_table,
4574 char **args,
4575 int nb_args)
bellard81d09122004-07-14 17:21:37 +00004576{
4577 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004578 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004579 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05004580 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004581 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00004582
bellard81d09122004-07-14 17:21:37 +00004583 if (nb_args <= 1) {
4584 /* command completion */
4585 if (nb_args == 0)
4586 cmdname = "";
4587 else
4588 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004589 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004590 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004591 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00004592 }
4593 } else {
4594 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004595 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004596 if (compare_cmd(args[0], cmd->name)) {
4597 break;
4598 }
bellard81d09122004-07-14 17:21:37 +00004599 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004600 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004601 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004602 }
4603
Wenchao Xiad903a772013-08-27 20:38:25 +08004604 if (cmd->sub_table) {
4605 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01004606 monitor_find_completion_by_table(mon, cmd->sub_table,
4607 &args[1], nb_args - 1);
4608 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08004609 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004610 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01004611 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4612 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004613 }
Wenchao Xiad903a772013-08-27 20:38:25 +08004614
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004615 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004616 for(i = 0; i < nb_args - 2; i++) {
4617 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004618 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004619 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004620 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004621 }
4622 }
4623 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01004624 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004625 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004626 }
bellard81d09122004-07-14 17:21:37 +00004627 switch(*ptype) {
4628 case 'F':
4629 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004630 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004631 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004632 break;
4633 case 'B':
4634 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004635 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004636 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
4637 name = bdrv_get_device_name(bs);
4638 if (str[0] == '\0' ||
4639 !strncmp(name, str, strlen(str))) {
4640 readline_add_completion(mon->rs, name);
4641 }
4642 }
bellard81d09122004-07-14 17:21:37 +00004643 break;
bellard7fe48482004-10-09 18:08:01 +00004644 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004645 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004646 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004647 monitor_find_completion_by_table(mon, cmd_table,
4648 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004649 }
4650 break;
bellard81d09122004-07-14 17:21:37 +00004651 default:
4652 break;
4653 }
4654 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004655}
4656
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004657static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004658 const char *cmdline)
4659{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004660 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004661 char *args[MAX_ARGS];
4662 int nb_args, len;
4663
4664 /* 1. parse the cmdline */
4665 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4666 return;
4667 }
4668#ifdef DEBUG_COMPLETION
Gonglei5fb9b5b2014-08-21 21:03:09 +08004669 {
4670 int i;
4671 for (i = 0; i < nb_args; i++) {
4672 monitor_printf(mon, "arg%d = '%s'\n", i, args[i]);
4673 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004674 }
4675#endif
4676
4677 /* if the line ends with a space, it means we want to complete the
4678 next arg */
4679 len = strlen(cmdline);
4680 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4681 if (nb_args >= MAX_ARGS) {
4682 goto cleanup;
4683 }
4684 args[nb_args++] = g_strdup("");
4685 }
4686
4687 /* 2. auto complete according to args */
4688 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004689
4690cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004691 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004692}
4693
aliguori731b0362009-03-05 23:01:42 +00004694static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004695{
aliguori731b0362009-03-05 23:01:42 +00004696 Monitor *mon = opaque;
4697
Jan Kiszkac62313b2009-12-04 14:05:29 +01004698 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004699}
4700
Markus Armbruster40861822015-05-29 10:27:16 +02004701static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
4702 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02004703{
Eric Blake2d5a8342015-04-15 09:19:23 -06004704 bool is_cap = cmd->mhandler.cmd_new == do_qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01004705
4706 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02004707 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4708 "Capabilities negotiation is already complete, command "
4709 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004710 return true;
4711 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01004712 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02004713 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4714 "Expecting capabilities negotiation with "
4715 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004716 return true;
4717 }
4718 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004719}
4720
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004721/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004722 * Argument validation rules:
4723 *
4724 * 1. The argument must exist in cmd_args qdict
4725 * 2. The argument type must be the expected one
4726 *
4727 * Special case: If the argument doesn't exist in cmd_args and
4728 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4729 * checking is skipped for it.
4730 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004731static void check_client_args_type(const QDict *client_args,
4732 const QDict *cmd_args, int flags,
4733 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03004734{
4735 const QDictEntry *ent;
4736
4737 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4738 QObject *obj;
4739 QString *arg_type;
4740 const QObject *client_arg = qdict_entry_value(ent);
4741 const char *client_arg_name = qdict_entry_key(ent);
4742
4743 obj = qdict_get(cmd_args, client_arg_name);
4744 if (!obj) {
4745 if (flags & QMP_ACCEPT_UNKNOWNS) {
4746 /* handler accepts unknowns */
4747 continue;
4748 }
4749 /* client arg doesn't exist */
Markus Armbruster326283a2015-03-02 18:39:09 +01004750 error_set(errp, QERR_INVALID_PARAMETER, client_arg_name);
4751 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004752 }
4753
4754 arg_type = qobject_to_qstring(obj);
4755 assert(arg_type != NULL);
4756
4757 /* check if argument's type is correct */
4758 switch (qstring_get_str(arg_type)[0]) {
4759 case 'F':
4760 case 'B':
4761 case 's':
4762 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004763 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4764 client_arg_name, "string");
4765 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004766 }
4767 break;
4768 case 'i':
4769 case 'l':
4770 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004771 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004772 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004773 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4774 client_arg_name, "int");
4775 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004776 }
4777 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004778 case 'T':
4779 if (qobject_type(client_arg) != QTYPE_QINT &&
4780 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004781 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4782 client_arg_name, "number");
4783 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004784 }
4785 break;
4786 case 'b':
4787 case '-':
4788 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004789 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4790 client_arg_name, "bool");
4791 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004792 }
4793 break;
4794 case 'O':
4795 assert(flags & QMP_ACCEPT_UNKNOWNS);
4796 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004797 case 'q':
4798 /* Any QObject can be passed. */
4799 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004800 case '/':
4801 case '.':
4802 /*
4803 * These types are not supported by QMP and thus are not
4804 * handled here. Fall through.
4805 */
4806 default:
4807 abort();
4808 }
4809 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03004810}
4811
4812/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004813 * - Check if the client has passed all mandatory args
4814 * - Set special flags for argument validation
4815 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004816static void check_mandatory_args(const QDict *cmd_args,
4817 const QDict *client_args, int *flags,
4818 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004819{
4820 const QDictEntry *ent;
4821
4822 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4823 const char *cmd_arg_name = qdict_entry_key(ent);
4824 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4825 assert(type != NULL);
4826
4827 if (qstring_get_str(type)[0] == 'O') {
4828 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4829 *flags |= QMP_ACCEPT_UNKNOWNS;
4830 } else if (qstring_get_str(type)[0] != '-' &&
4831 qstring_get_str(type)[1] != '?' &&
4832 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004833 error_set(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
4834 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004835 }
4836 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004837}
4838
4839static QDict *qdict_from_args_type(const char *args_type)
4840{
4841 int i;
4842 QDict *qdict;
4843 QString *key, *type, *cur_qs;
4844
4845 assert(args_type != NULL);
4846
4847 qdict = qdict_new();
4848
4849 if (args_type == NULL || args_type[0] == '\0') {
4850 /* no args, empty qdict */
4851 goto out;
4852 }
4853
4854 key = qstring_new();
4855 type = qstring_new();
4856
4857 cur_qs = key;
4858
4859 for (i = 0;; i++) {
4860 switch (args_type[i]) {
4861 case ',':
4862 case '\0':
4863 qdict_put(qdict, qstring_get_str(key), type);
4864 QDECREF(key);
4865 if (args_type[i] == '\0') {
4866 goto out;
4867 }
4868 type = qstring_new(); /* qdict has ref */
4869 cur_qs = key = qstring_new();
4870 break;
4871 case ':':
4872 cur_qs = type;
4873 break;
4874 default:
4875 qstring_append_chr(cur_qs, args_type[i]);
4876 break;
4877 }
4878 }
4879
4880out:
4881 return qdict;
4882}
4883
4884/*
4885 * Client argument checking rules:
4886 *
4887 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004888 * 2. Each argument provided by the client must be expected
4889 * 3. Each argument provided by the client must have the type expected
4890 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004891 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004892static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
4893 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004894{
Markus Armbruster326283a2015-03-02 18:39:09 +01004895 Error *err = NULL;
4896 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004897 QDict *cmd_args;
4898
4899 cmd_args = qdict_from_args_type(cmd->args_type);
4900
4901 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01004902 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004903 if (err) {
4904 goto out;
4905 }
4906
Markus Armbruster326283a2015-03-02 18:39:09 +01004907 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004908
4909out:
Markus Armbruster326283a2015-03-02 18:39:09 +01004910 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004911 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004912}
4913
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004914/*
4915 * Input object checking rules
4916 *
4917 * 1. Input object must be a dict
4918 * 2. The "execute" key must exist
4919 * 3. The "execute" key must be a string
4920 * 4. If the "arguments" key exists, it must be a dict
4921 * 5. If the "id" key exists, it can be anything (ie. json-value)
4922 * 6. Any argument not listed above is considered invalid
4923 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004924static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004925{
4926 const QDictEntry *ent;
4927 int has_exec_key = 0;
4928 QDict *input_dict;
4929
4930 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004931 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004932 return NULL;
4933 }
4934
4935 input_dict = qobject_to_qdict(input_obj);
4936
4937 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4938 const char *arg_name = qdict_entry_key(ent);
4939 const QObject *arg_obj = qdict_entry_value(ent);
4940
4941 if (!strcmp(arg_name, "execute")) {
4942 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004943 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4944 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004945 return NULL;
4946 }
4947 has_exec_key = 1;
4948 } else if (!strcmp(arg_name, "arguments")) {
4949 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004950 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4951 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004952 return NULL;
4953 }
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004954 } else {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004955 error_set(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004956 return NULL;
4957 }
4958 }
4959
4960 if (!has_exec_key) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004961 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004962 return NULL;
4963 }
4964
4965 return input_dict;
4966}
4967
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004968static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4969{
Markus Armbruster326283a2015-03-02 18:39:09 +01004970 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01004971 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004972 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004973 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004974 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004975 Monitor *mon = cur_mon;
4976
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004977 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01004978 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004979
4980 obj = json_parser_parse(tokens, NULL);
4981 if (!obj) {
4982 // FIXME: should be triggered in json_parser_parse()
Markus Armbruster710aec92015-03-06 11:28:00 +01004983 error_set(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004984 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004985 }
4986
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004987 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004988 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004989 qobject_decref(obj);
4990 goto err_out;
4991 }
4992
Markus Armbruster74358f22015-03-06 19:35:59 +01004993 mon->qmp.id = qdict_get(input, "id");
4994 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004995
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004996 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004997 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05004998 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004999 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01005000 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
5001 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005002 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005003 }
Markus Armbruster40861822015-05-29 10:27:16 +02005004 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06005005 goto err_out;
5006 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005007
5008 obj = qdict_get(input, "arguments");
5009 if (!obj) {
5010 args = qdict_new();
5011 } else {
5012 args = qobject_to_qdict(obj);
5013 QINCREF(args);
5014 }
5015
Markus Armbruster326283a2015-03-02 18:39:09 +01005016 qmp_check_client_args(cmd, args, &local_err);
5017 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005018 goto err_out;
5019 }
5020
Markus Armbruster84add862015-03-05 16:45:15 +01005021 if (cmd->mhandler.cmd_new(mon, args, &data)) {
5022 /* Command failed... */
Markus Armbruster452e0302015-03-06 19:11:13 +01005023 if (!mon->error) {
Markus Armbruster84add862015-03-05 16:45:15 +01005024 /* ... without setting an error, so make one up */
Markus Armbruster710aec92015-03-06 11:28:00 +01005025 error_set(&local_err, QERR_UNDEFINED_ERROR);
Markus Armbruster84add862015-03-05 16:45:15 +01005026 }
5027 }
Markus Armbruster710aec92015-03-06 11:28:00 +01005028 if (mon->error) {
5029 error_set(&local_err, mon->error->err_class, "%s",
5030 mon->error->err_msg);
5031 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005032
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005033err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01005034 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01005035 qobject_decref(data);
Markus Armbruster70ea0c52015-03-06 10:47:08 +01005036 QDECREF(mon->error);
5037 mon->error = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005038 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005039 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005040}
5041
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005042static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005043{
5044 Monitor *old_mon = cur_mon;
5045
5046 cur_mon = opaque;
5047
Markus Armbruster74358f22015-03-06 19:35:59 +01005048 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005049
5050 cur_mon = old_mon;
5051}
5052
aliguori731b0362009-03-05 23:01:42 +00005053static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005054{
aliguori731b0362009-03-05 23:01:42 +00005055 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005056 int i;
aliguori376253e2009-03-05 23:01:23 +00005057
aliguori731b0362009-03-05 23:01:42 +00005058 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005059
aliguoricde76ee2009-03-05 23:01:51 +00005060 if (cur_mon->rs) {
5061 for (i = 0; i < size; i++)
5062 readline_handle_byte(cur_mon->rs, buf[i]);
5063 } else {
5064 if (size == 0 || buf[size - 1] != 0)
5065 monitor_printf(cur_mon, "corrupted command\n");
5066 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01005067 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005068 }
aliguori731b0362009-03-05 23:01:42 +00005069
5070 cur_mon = old_mon;
5071}
aliguorid8f44602008-10-06 13:52:44 +00005072
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005073static void monitor_command_cb(void *opaque, const char *cmdline,
5074 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005075{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005076 Monitor *mon = opaque;
5077
aliguori731b0362009-03-05 23:01:42 +00005078 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01005079 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005080 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005081}
5082
aliguoricde76ee2009-03-05 23:01:51 +00005083int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005084{
aliguoricde76ee2009-03-05 23:01:51 +00005085 if (!mon->rs)
5086 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005087 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005088 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005089}
5090
aliguori376253e2009-03-05 23:01:23 +00005091void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005092{
aliguoricde76ee2009-03-05 23:01:51 +00005093 if (!mon->rs)
5094 return;
aliguori731b0362009-03-05 23:01:42 +00005095 if (--mon->suspend_cnt == 0)
5096 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005097}
5098
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005099static QObject *get_qmp_greeting(void)
5100{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005101 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005102
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005103 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005104 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5105}
5106
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005107static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005108{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005109 QObject *data;
5110 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005111
Luiz Capitulino47116d12010-02-08 17:01:30 -02005112 switch (event) {
5113 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01005114 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005115 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005116 monitor_json_emitter(mon, data);
5117 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04005118 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02005119 break;
5120 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01005121 json_message_parser_destroy(&mon->qmp.parser);
5122 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04005123 mon_refcount--;
5124 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02005125 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005126 }
5127}
5128
aliguori731b0362009-03-05 23:01:42 +00005129static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005130{
aliguori376253e2009-03-05 23:01:23 +00005131 Monitor *mon = opaque;
5132
aliguori2724b182009-03-05 23:01:47 +00005133 switch (event) {
5134 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005135 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005136 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005137 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005138 if (mon->reset_seen) {
5139 readline_restart(mon->rs);
5140 monitor_resume(mon);
5141 monitor_flush(mon);
5142 } else {
5143 mon->suspend_cnt = 0;
5144 }
aliguori2724b182009-03-05 23:01:47 +00005145 break;
ths86e94de2007-01-05 22:01:59 +00005146
aliguori2724b182009-03-05 23:01:47 +00005147 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005148 if (mon->reset_seen) {
5149 if (mon->suspend_cnt == 0) {
5150 monitor_printf(mon, "\n");
5151 }
5152 monitor_flush(mon);
5153 monitor_suspend(mon);
5154 } else {
5155 mon->suspend_cnt++;
5156 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005157 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005158 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005159 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00005160 break;
5161
Amit Shahb6b8df52009-10-07 18:31:16 +05305162 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005163 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5164 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005165 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03005166 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00005167 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005168 }
5169 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04005170 mon_refcount++;
5171 break;
5172
5173 case CHR_EVENT_CLOSED:
5174 mon_refcount--;
5175 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00005176 break;
5177 }
ths86e94de2007-01-05 22:01:59 +00005178}
5179
Wayne Xia816f8922011-10-12 11:32:41 +08005180static int
5181compare_mon_cmd(const void *a, const void *b)
5182{
5183 return strcmp(((const mon_cmd_t *)a)->name,
5184 ((const mon_cmd_t *)b)->name);
5185}
5186
5187static void sortcmdlist(void)
5188{
5189 int array_num;
5190 int elem_size = sizeof(mon_cmd_t);
5191
5192 array_num = sizeof(mon_cmds)/elem_size-1;
5193 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
5194
5195 array_num = sizeof(info_cmds)/elem_size-1;
5196 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
5197}
5198
aliguori76655d62009-03-06 20:27:37 +00005199
5200/*
5201 * Local variables:
5202 * c-indent-level: 4
5203 * c-basic-offset: 4
5204 * tab-width: 8
5205 * End:
5206 */
5207
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005208/* These functions just adapt the readline interface in a typesafe way. We
5209 * could cast function pointers but that discards compiler checks.
5210 */
Stefan Weild5d15072014-01-25 18:18:23 +01005211static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
5212 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005213{
5214 va_list ap;
5215 va_start(ap, fmt);
5216 monitor_vprintf(opaque, fmt, ap);
5217 va_end(ap);
5218}
5219
5220static void monitor_readline_flush(void *opaque)
5221{
5222 monitor_flush(opaque);
5223}
5224
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005225static void __attribute__((constructor)) monitor_lock_init(void)
5226{
5227 qemu_mutex_init(&monitor_lock);
5228}
5229
aliguori731b0362009-03-05 23:01:42 +00005230void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005231{
aliguori731b0362009-03-05 23:01:42 +00005232 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005233 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005234
5235 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02005236 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08005237 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00005238 is_first_init = 0;
5239 }
aliguori87127162009-03-05 23:01:29 +00005240
Wenchao Xiab01fe892013-08-27 20:38:19 +08005241 mon = g_malloc(sizeof(*mon));
5242 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00005243
aliguori87127162009-03-05 23:01:29 +00005244 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005245 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005246 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005247 mon->rs = readline_init(monitor_readline_printf,
5248 monitor_readline_flush,
5249 mon,
5250 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00005251 monitor_read_command(mon, 0);
5252 }
aliguori87127162009-03-05 23:01:29 +00005253
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005254 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005255 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
5256 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005257 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01005258 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005259 } else {
5260 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5261 monitor_event, mon);
5262 }
aliguori87127162009-03-05 23:01:29 +00005263
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005264 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00005265 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005266 qemu_mutex_unlock(&monitor_lock);
5267
Markus Armbruster8631b602010-02-18 11:41:55 +01005268 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5269 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005270}
5271
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005272static void bdrv_password_cb(void *opaque, const char *password,
5273 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005274{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005275 Monitor *mon = opaque;
5276 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005277 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005278 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00005279
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005280 bdrv_add_key(bs, password, &local_err);
5281 if (local_err) {
5282 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
5283 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00005284 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005285 }
aliguori731b0362009-03-05 23:01:42 +00005286 if (mon->password_completion_cb)
5287 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005288
aliguori731b0362009-03-05 23:01:42 +00005289 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005290}
aliguoric0f4ce72009-03-05 23:01:01 +00005291
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005292int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02005293 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005294 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005295{
aliguoricde76ee2009-03-05 23:01:51 +00005296 int err;
5297
aliguori376253e2009-03-05 23:01:23 +00005298 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5299 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005300
aliguori731b0362009-03-05 23:01:42 +00005301 mon->password_completion_cb = completion_cb;
5302 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005303
aliguoricde76ee2009-03-05 23:01:51 +00005304 err = monitor_read_password(mon, bdrv_password_cb, bs);
5305
5306 if (err && completion_cb)
5307 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005308
5309 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005310}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005311
5312int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02005313 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005314 void *opaque)
5315{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005316 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08005317 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005318
Fam Zheng55606252015-03-02 19:36:46 +08005319 blk = blk_by_name(device);
5320 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005321 monitor_printf(mon, "Device not found %s\n", device);
5322 return -1;
5323 }
5324
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005325 bdrv_add_key(blk_bs(blk), NULL, &err);
5326 if (err) {
5327 error_free(err);
5328 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
5329 }
5330
5331 if (completion_cb) {
5332 completion_cb(opaque, 0);
5333 }
5334 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005335}
Paolo Bonzini4d454572012-11-26 16:03:42 +01005336
5337QemuOptsList qemu_mon_opts = {
5338 .name = "mon",
5339 .implied_opt_name = "chardev",
5340 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5341 .desc = {
5342 {
5343 .name = "mode",
5344 .type = QEMU_OPT_STRING,
5345 },{
5346 .name = "chardev",
5347 .type = QEMU_OPT_STRING,
5348 },{
5349 .name = "default",
5350 .type = QEMU_OPT_BOOL,
5351 },{
5352 .name = "pretty",
5353 .type = QEMU_OPT_BOOL,
5354 },
5355 { /* end of list */ }
5356 },
5357};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03005358
5359#ifndef TARGET_I386
5360void qmp_rtc_reset_reinjection(Error **errp)
5361{
5362 error_set(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
5363}
5364#endif