blob: be18ac1e44bf4a6bc85c26c6b32b6156b8a5f213 [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
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100234/**
235 * Is @mon a QMP monitor?
236 */
237static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200238{
239 return (mon->flags & MONITOR_USE_CONTROL);
240}
241
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100242/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
243int monitor_cur_is_qmp(void)
244{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100245 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100246}
247
Anthony Liguori7060b472011-09-02 12:34:50 -0500248void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000249{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200250 if (!mon->rs)
251 return;
252
aliguori731b0362009-03-05 23:01:42 +0000253 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
254 if (show_prompt)
255 readline_show_prompt(mon->rs);
256}
bellard6a00d602005-11-21 23:25:50 +0000257
Anthony Liguori7060b472011-09-02 12:34:50 -0500258int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
259 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000260{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100261 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000262 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
263 /* prompt is printed on return from the command handler */
264 return 0;
265 } else {
266 monitor_printf(mon, "terminal does not support password prompting\n");
267 return -ENOTTY;
268 }
aliguoribb5fc202009-03-05 23:01:15 +0000269}
270
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200271static void monitor_flush_locked(Monitor *mon);
272
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100273static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
274 void *opaque)
275{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200276 Monitor *mon = opaque;
277
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200278 qemu_mutex_lock(&mon->out_lock);
279 mon->out_watch = 0;
280 monitor_flush_locked(mon);
281 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100282 return FALSE;
283}
284
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200285/* Called with mon->out_lock held. */
286static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000287{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100288 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400289 size_t len;
290 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100291
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400292 if (mon->skip_flush) {
293 return;
294 }
295
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400296 buf = qstring_get_str(mon->outbuf);
297 len = qstring_get_length(mon->outbuf);
298
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200299 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400300 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200301 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
302 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400303 QDECREF(mon->outbuf);
304 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100305 return;
306 }
307 if (rc > 0) {
308 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400309 QString *tmp = qstring_from_str(buf + rc);
310 QDECREF(mon->outbuf);
311 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100312 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200313 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200314 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200315 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200316 }
bellard7e2515e2004-08-01 21:52:19 +0000317 }
318}
319
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200320void monitor_flush(Monitor *mon)
321{
322 qemu_mutex_lock(&mon->out_lock);
323 monitor_flush_locked(mon);
324 qemu_mutex_unlock(&mon->out_lock);
325}
326
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400327/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000328static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000329{
ths60fe76f2007-12-16 03:02:09 +0000330 char c;
aliguori731b0362009-03-05 23:01:42 +0000331
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200332 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000333 for(;;) {
334 c = *str++;
335 if (c == '\0')
336 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400337 if (c == '\n') {
338 qstring_append_chr(mon->outbuf, '\r');
339 }
340 qstring_append_chr(mon->outbuf, c);
341 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200342 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400343 }
bellard7e2515e2004-08-01 21:52:19 +0000344 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200345 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000346}
347
aliguori376253e2009-03-05 23:01:23 +0000348void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000349{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400350 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200351
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200352 if (!mon)
353 return;
354
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100355 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200356 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200357 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200358
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400359 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200360 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400361 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000362}
363
aliguori376253e2009-03-05 23:01:23 +0000364void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000365{
366 va_list ap;
367 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000368 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000369 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000370}
371
Stefan Weil8b7968f2010-09-23 21:28:05 +0200372static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
373 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000374{
375 va_list ap;
376 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000377 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000378 va_end(ap);
379 return 0;
380}
381
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200382static void monitor_json_emitter(Monitor *mon, const QObject *data)
383{
384 QString *json;
385
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200386 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
387 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200388 assert(json != NULL);
389
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200390 qstring_append_chr(json, '\n');
391 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200392
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200393 QDECREF(json);
394}
395
Markus Armbruster710aec92015-03-06 11:28:00 +0100396static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300397{
398 QObject *obj;
399
Markus Armbruster710aec92015-03-06 11:28:00 +0100400 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
401 ErrorClass_lookup[error_get_class(err)],
402 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300403
404 return qobject_to_qdict(obj);
405}
406
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100407static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100408 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200409{
410 QDict *qmp;
411
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100412 trace_monitor_protocol_emitter(mon);
413
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100414 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200415 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300416 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200417 if (data) {
418 qobject_incref(data);
419 qdict_put_obj(qmp, "return", data);
420 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200421 /* return an empty QDict by default */
422 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200423 }
424 } else {
425 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100426 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200427 }
428
Markus Armbruster74358f22015-03-06 19:35:59 +0100429 if (mon->qmp.id) {
430 qdict_put_obj(qmp, "id", mon->qmp.id);
431 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200432 }
433
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200434 monitor_json_emitter(mon, QOBJECT(qmp));
435 QDECREF(qmp);
436}
437
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200438
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200439static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100440
441/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200442 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200443 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100444 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200445static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100446{
447 Monitor *mon;
448
449 trace_monitor_protocol_event_emit(event, data);
450 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100451 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100452 monitor_json_emitter(mon, data);
453 }
454 }
455}
456
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100457/*
458 * Queue a new event for emission to Monitor instances,
459 * applying any rate limiting if required.
460 */
461static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200462monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100463{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200464 MonitorQAPIEventState *evstate;
465 assert(event < QAPI_EVENT_MAX);
Alex Blighbc72ad62013-08-21 16:03:08 +0100466 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100467
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200468 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100469 trace_monitor_protocol_event_queue(event,
470 data,
471 evstate->rate,
472 evstate->last,
473 now);
474
475 /* Rate limit of 0 indicates no throttling */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200476 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100477 if (!evstate->rate) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200478 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100479 evstate->last = now;
480 } else {
481 int64_t delta = now - evstate->last;
482 if (evstate->data ||
483 delta < evstate->rate) {
484 /* If there's an existing event pending, replace
485 * it with the new event, otherwise schedule a
486 * timer for delayed emission
487 */
488 if (evstate->data) {
489 qobject_decref(evstate->data);
490 } else {
491 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100492 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100493 }
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200494 evstate->data = QOBJECT(data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100495 qobject_incref(evstate->data);
496 } else {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200497 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100498 evstate->last = now;
499 }
500 }
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200501 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100502}
503
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100504/*
505 * The callback invoked by QemuTimer when a delayed
506 * event is ready to be emitted
507 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200508static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100509{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200510 MonitorQAPIEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100511 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100512
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100513 trace_monitor_protocol_event_handler(evstate->event,
514 evstate->data,
515 evstate->last,
516 now);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200517 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100518 if (evstate->data) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200519 monitor_qapi_event_emit(evstate->event, evstate->data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100520 qobject_decref(evstate->data);
521 evstate->data = NULL;
522 }
523 evstate->last = now;
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200524 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100525}
526
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100527/*
528 * @event: the event ID to be limited
529 * @rate: the rate limit in milliseconds
530 *
531 * Sets a rate limit on a particular event, so no
532 * more than 1 event will be emitted within @rate
533 * milliseconds
534 */
535static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200536monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100537{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200538 MonitorQAPIEventState *evstate;
539 assert(event < QAPI_EVENT_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100540
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200541 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100542
543 trace_monitor_protocol_event_throttle(event, rate);
544 evstate->event = event;
Wenchao Xia2f44a082014-06-24 16:34:00 -0700545 assert(rate * SCALE_MS <= INT64_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100546 evstate->rate = rate * SCALE_MS;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100547 evstate->last = 0;
548 evstate->data = NULL;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200549 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
550 SCALE_MS,
551 monitor_qapi_event_handler,
552 evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100553}
554
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200555static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100556{
Wenchao Xiae010ad82014-06-18 08:43:41 +0200557 /* Limit guest-triggerable events to 1 per second */
558 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
Wenchao Xia99eaf092014-06-18 08:43:42 +0200559 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
Wenchao Xiaaef9d312014-06-18 08:43:51 +0200560 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
Wenchao Xiafe069d92014-06-18 08:43:53 +0200561 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
562 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
Laszlo Erseke2ae6152014-06-26 17:50:02 +0200563 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100564
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200565 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200566}
567
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200568static int do_qmp_capabilities(Monitor *mon, const QDict *params,
569 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200570{
Markus Armbrusterf994b252015-03-06 19:51:51 +0100571 mon->qmp.in_command_mode = true;
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200572 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200573}
574
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100575static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200576
Wenchao Xiab01fe892013-08-27 20:38:19 +0800577static void monitor_data_init(Monitor *mon)
578{
579 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200580 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800581 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800582 /* Use *mon_cmds by default. */
583 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800584}
585
586static void monitor_data_destroy(Monitor *mon)
587{
588 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200589 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800590}
591
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200592char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
593 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200594{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200595 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200596 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200597
Wenchao Xiab01fe892013-08-27 20:38:19 +0800598 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400599 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200600
601 old_mon = cur_mon;
602 cur_mon = &hmp;
603
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200604 if (has_cpu_index) {
605 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200606 if (ret < 0) {
607 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200608 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
609 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200610 goto out;
611 }
612 }
613
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100614 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200615 cur_mon = old_mon;
616
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200617 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400618 if (qstring_get_length(hmp.outbuf) > 0) {
619 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200620 } else {
621 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200622 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200623 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200624
625out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800626 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200627 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200628}
629
bellard9dc39cb2004-03-14 21:38:27 +0000630static int compare_cmd(const char *name, const char *list)
631{
632 const char *p, *pstart;
633 int len;
634 len = strlen(name);
635 p = list;
636 for(;;) {
637 pstart = p;
638 p = strchr(p, '|');
639 if (!p)
640 p = pstart + strlen(pstart);
641 if ((p - pstart) == len && !memcmp(pstart, name, len))
642 return 1;
643 if (*p == '\0')
644 break;
645 p++;
646 }
647 return 0;
648}
649
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800650static int get_str(char *buf, int buf_size, const char **pp)
651{
652 const char *p;
653 char *q;
654 int c;
655
656 q = buf;
657 p = *pp;
658 while (qemu_isspace(*p)) {
659 p++;
660 }
661 if (*p == '\0') {
662 fail:
663 *q = '\0';
664 *pp = p;
665 return -1;
666 }
667 if (*p == '\"') {
668 p++;
669 while (*p != '\0' && *p != '\"') {
670 if (*p == '\\') {
671 p++;
672 c = *p++;
673 switch (c) {
674 case 'n':
675 c = '\n';
676 break;
677 case 'r':
678 c = '\r';
679 break;
680 case '\\':
681 case '\'':
682 case '\"':
683 break;
684 default:
685 qemu_printf("unsupported escape code: '\\%c'\n", c);
686 goto fail;
687 }
688 if ((q - buf) < buf_size - 1) {
689 *q++ = c;
690 }
691 } else {
692 if ((q - buf) < buf_size - 1) {
693 *q++ = *p;
694 }
695 p++;
696 }
697 }
698 if (*p != '\"') {
699 qemu_printf("unterminated string\n");
700 goto fail;
701 }
702 p++;
703 } else {
704 while (*p != '\0' && !qemu_isspace(*p)) {
705 if ((q - buf) < buf_size - 1) {
706 *q++ = *p;
707 }
708 p++;
709 }
710 }
711 *q = '\0';
712 *pp = p;
713 return 0;
714}
715
716#define MAX_ARGS 16
717
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800718static void free_cmdline_args(char **args, int nb_args)
719{
720 int i;
721
722 assert(nb_args <= MAX_ARGS);
723
724 for (i = 0; i < nb_args; i++) {
725 g_free(args[i]);
726 }
727
728}
729
730/*
731 * Parse the command line to get valid args.
732 * @cmdline: command line to be parsed.
733 * @pnb_args: location to store the number of args, must NOT be NULL.
734 * @args: location to store the args, which should be freed by caller, must
735 * NOT be NULL.
736 *
737 * Returns 0 on success, negative on failure.
738 *
739 * NOTE: this parser is an approximate form of the real command parser. Number
740 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
741 * return with failure.
742 */
743static int parse_cmdline(const char *cmdline,
744 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800745{
746 const char *p;
747 int nb_args, ret;
748 char buf[1024];
749
750 p = cmdline;
751 nb_args = 0;
752 for (;;) {
753 while (qemu_isspace(*p)) {
754 p++;
755 }
756 if (*p == '\0') {
757 break;
758 }
759 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800760 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800761 }
762 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800763 if (ret < 0) {
764 goto fail;
765 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800766 args[nb_args] = g_strdup(buf);
767 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800768 }
769 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800770 return 0;
771
772 fail:
773 free_cmdline_args(args, nb_args);
774 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800775}
776
Wenchao Xia66855492013-08-27 20:38:23 +0800777static void help_cmd_dump_one(Monitor *mon,
778 const mon_cmd_t *cmd,
779 char **prefix_args,
780 int prefix_args_nb)
781{
782 int i;
783
784 for (i = 0; i < prefix_args_nb; i++) {
785 monitor_printf(mon, "%s ", prefix_args[i]);
786 }
787 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
788}
789
790/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500791static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800792 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000793{
Anthony Liguoric227f092009-10-01 16:12:16 -0500794 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000795
Wenchao Xia66855492013-08-27 20:38:23 +0800796 /* No valid arg need to compare with, dump all in *cmds */
797 if (arg_index >= nb_args) {
798 for (cmd = cmds; cmd->name != NULL; cmd++) {
799 help_cmd_dump_one(mon, cmd, args, arg_index);
800 }
801 return;
802 }
803
804 /* Find one entry to dump */
805 for (cmd = cmds; cmd->name != NULL; cmd++) {
806 if (compare_cmd(args[arg_index], cmd->name)) {
807 if (cmd->sub_table) {
808 /* continue with next arg */
809 help_cmd_dump(mon, cmd->sub_table,
810 args, nb_args, arg_index + 1);
811 } else {
812 help_cmd_dump_one(mon, cmd, args, arg_index);
813 }
814 break;
815 }
bellard9dc39cb2004-03-14 21:38:27 +0000816 }
817}
818
aliguori376253e2009-03-05 23:01:23 +0000819static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000820{
Wenchao Xia66855492013-08-27 20:38:23 +0800821 char *args[MAX_ARGS];
822 int nb_args = 0;
823
824 /* 1. parse user input */
825 if (name) {
826 /* special case for log, directly dump and return */
827 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000828 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000829 monitor_printf(mon, "Log items (comma separated):\n");
830 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000831 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000832 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000833 }
Wenchao Xia66855492013-08-27 20:38:23 +0800834 return;
835 }
836
837 if (parse_cmdline(name, &nb_args, args) < 0) {
838 return;
bellardf193c792004-03-21 17:06:25 +0000839 }
bellard9dc39cb2004-03-14 21:38:27 +0000840 }
Wenchao Xia66855492013-08-27 20:38:23 +0800841
842 /* 2. dump the contents according to parsed args */
843 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
844
845 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000846}
847
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300848static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300849{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300850 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300851}
852
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100853static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530854{
855 const char *tp_name = qdict_get_str(qdict, "name");
856 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200857 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000858
Lluís Vilanova14101d02014-08-25 13:20:03 +0200859 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
860 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100861 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000862 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530863}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100864
Michael Rothc45a8162011-10-02 08:44:37 -0500865#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100866static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100867{
868 const char *op = qdict_get_try_str(qdict, "op");
869 const char *arg = qdict_get_try_str(qdict, "arg");
870
871 if (!op) {
872 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
873 } else if (!strcmp(op, "on")) {
874 st_set_trace_file_enabled(true);
875 } else if (!strcmp(op, "off")) {
876 st_set_trace_file_enabled(false);
877 } else if (!strcmp(op, "flush")) {
878 st_flush_trace_buffer();
879 } else if (!strcmp(op, "set")) {
880 if (arg) {
881 st_set_trace_file(arg);
882 }
883 } else {
884 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
885 help_cmd(mon, "trace-file");
886 }
887}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530888#endif
889
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100890static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000891{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300892 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000893}
894
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300895CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000896{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300897 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200898 const mon_cmd_t *cmd;
899
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300900 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200901 info = g_malloc0(sizeof(*info));
902 info->value = g_malloc0(sizeof(*info->value));
903 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200904
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300905 info->next = cmd_list;
906 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200907 }
908
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300909 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000910}
911
Daniel P. Berrange48608532012-05-21 17:59:51 +0100912EventInfoList *qmp_query_events(Error **errp)
913{
914 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200915 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100916
Wenchao Xia75175172014-06-18 08:43:54 +0200917 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
918 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100919 assert(event_name != NULL);
920 info = g_malloc0(sizeof(*info));
921 info->value = g_malloc0(sizeof(*info->value));
922 info->value->name = g_strdup(event_name);
923
924 info->next = ev_list;
925 ev_list = info;
926 }
927
928 return ev_list;
929}
930
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300931/* set the current CPU defined by the user */
932int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000933{
Andreas Färber55e5c282012-12-17 06:18:02 +0100934 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000935
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100936 cpu = qemu_get_cpu(cpu_index);
937 if (cpu == NULL) {
938 return -1;
bellard6a00d602005-11-21 23:25:50 +0000939 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200940 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100941 return 0;
bellard6a00d602005-11-21 23:25:50 +0000942}
943
Andreas Färber9349b4f2012-03-14 01:38:32 +0100944static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000945{
aliguori731b0362009-03-05 23:01:42 +0000946 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300947 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000948 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300949 cpu_synchronize_state(cur_mon->mon_cpu);
Andreas Färbercb446ec2013-05-01 14:24:52 +0200950 return cur_mon->mon_cpu->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000951}
952
Luiz Capitulino99b77962011-10-24 10:53:44 -0200953int monitor_get_cpu_index(void)
954{
Andreas Färber55e5c282012-12-17 06:18:02 +0100955 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
956 return cpu->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200957}
958
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100959static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000960{
Andreas Färber878096e2013-05-27 01:33:50 +0200961 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100962 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000963 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +0200964 cpu = ENV_GET_CPU(env);
965 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000966}
967
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100968static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000969{
aliguori376253e2009-03-05 23:01:23 +0000970 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200971 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000972}
973
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100974static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300975{
976 dump_opcount_info((FILE *)mon, monitor_fprintf);
977}
978
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100979static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000980{
981 int i;
bellard7e2515e2004-08-01 21:52:19 +0000982 const char *str;
ths3b46e622007-09-17 08:09:54 +0000983
aliguoricde76ee2009-03-05 23:01:51 +0000984 if (!mon->rs)
985 return;
bellard7e2515e2004-08-01 21:52:19 +0000986 i = 0;
987 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000988 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000989 if (!str)
990 break;
aliguori376253e2009-03-05 23:01:23 +0000991 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000992 i++;
bellardaa455482004-04-04 13:07:25 +0000993 }
994}
995
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100996static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +0000997{
Andreas Färber878096e2013-05-27 01:33:50 +0200998 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100999 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +00001000
1001 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001002 cpu = ENV_GET_CPU(env);
1003 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001004}
j_mayer76a66252007-03-07 08:32:30 +00001005
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001006static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301007{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001008 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1009 TraceEventInfoList *elem;
1010
1011 for (elem = events; elem != NULL; elem = elem->next) {
1012 monitor_printf(mon, "%s : state %u\n",
1013 elem->value->name,
1014 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1015 }
1016 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301017}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301018
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001019void qmp_client_migrate_info(const char *protocol, const char *hostname,
1020 bool has_port, int64_t port,
1021 bool has_tls_port, int64_t tls_port,
1022 bool has_cert_subject, const char *cert_subject,
1023 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001024{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001025 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001026 if (!qemu_using_spice(errp)) {
1027 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001028 }
1029
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001030 if (!has_port && !has_tls_port) {
1031 error_set(errp, QERR_MISSING_PARAMETER, "port/tls-port");
1032 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001033 }
1034
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001035 if (qemu_spice_migrate_info(hostname,
1036 has_port ? port : -1,
1037 has_tls_port ? tls_port : -1,
1038 cert_subject)) {
1039 error_set(errp, QERR_UNDEFINED_ERROR);
1040 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001041 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001042 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001043 }
1044
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001045 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001046}
1047
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001048static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001049{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001050 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001051}
1052
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001053static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001054{
1055 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001056 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001057
bellard9307c4c2004-04-04 12:57:25 +00001058 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001059 mask = 0;
1060 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001061 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001062 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001063 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001064 return;
1065 }
1066 }
Peter Maydell24537a02013-02-11 16:41:23 +00001067 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001068}
1069
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001070static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001071{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001072 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001073 if (!option || !strcmp(option, "on")) {
1074 singlestep = 1;
1075 } else if (!strcmp(option, "off")) {
1076 singlestep = 0;
1077 } else {
1078 monitor_printf(mon, "unexpected option %s\n", option);
1079 }
1080}
1081
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001082static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001083{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001084 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001085 if (!device)
1086 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1087 if (gdbserver_start(device) < 0) {
1088 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1089 device);
1090 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001091 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001092 } else {
aliguori59030a82009-04-05 18:43:41 +00001093 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1094 device);
bellard8a7ddc32004-03-31 19:00:16 +00001095 }
1096}
1097
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001098static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001099{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001100 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001101 if (select_watchdog_action(action) == -1) {
1102 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1103 }
1104}
1105
aliguori376253e2009-03-05 23:01:23 +00001106static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001107{
aliguori376253e2009-03-05 23:01:23 +00001108 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001109 switch(c) {
1110 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001111 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001112 break;
1113 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001114 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001115 break;
1116 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001117 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001118 break;
1119 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001120 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001121 break;
1122 default:
1123 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001124 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001125 } else {
aliguori376253e2009-03-05 23:01:23 +00001126 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001127 }
1128 break;
1129 }
aliguori376253e2009-03-05 23:01:23 +00001130 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001131}
1132
aliguori376253e2009-03-05 23:01:23 +00001133static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001134 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001135{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001136 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001137 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001138 uint8_t buf[16];
1139 uint64_t v;
1140
1141 if (format == 'i') {
1142 int flags;
1143 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001144 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001145#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001146 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001147 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001148 } else if (wsize == 4) {
1149 flags = 0;
1150 } else {
bellard6a15fd12006-04-12 21:07:07 +00001151 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001152 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001153 if (env) {
1154#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001155 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001156 (env->segs[R_CS].flags & DESC_L_MASK))
1157 flags = 2;
1158 else
1159#endif
1160 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1161 flags = 1;
1162 }
bellard4c27ba22004-04-25 18:05:08 +00001163 }
1164#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001165#ifdef TARGET_PPC
1166 flags = msr_le << 16;
1167 flags |= env->bfd_mach;
1168#endif
aliguori376253e2009-03-05 23:01:23 +00001169 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001170 return;
1171 }
1172
1173 len = wsize * count;
1174 if (wsize == 1)
1175 line_size = 8;
1176 else
1177 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001178 max_digits = 0;
1179
1180 switch(format) {
1181 case 'o':
1182 max_digits = (wsize * 8 + 2) / 3;
1183 break;
1184 default:
1185 case 'x':
1186 max_digits = (wsize * 8) / 4;
1187 break;
1188 case 'u':
1189 case 'd':
1190 max_digits = (wsize * 8 * 10 + 32) / 33;
1191 break;
1192 case 'c':
1193 wsize = 1;
1194 break;
1195 }
1196
1197 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001198 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001199 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001200 else
aliguori376253e2009-03-05 23:01:23 +00001201 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001202 l = len;
1203 if (l > line_size)
1204 l = line_size;
1205 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001206 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001207 } else {
bellard6a00d602005-11-21 23:25:50 +00001208 env = mon_get_cpu();
Andreas Färberf17ec442013-06-29 19:40:58 +02001209 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001210 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001211 break;
1212 }
bellard9307c4c2004-04-04 12:57:25 +00001213 }
ths5fafdf22007-09-16 21:08:06 +00001214 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001215 while (i < l) {
1216 switch(wsize) {
1217 default:
1218 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001219 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001220 break;
1221 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001222 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001223 break;
1224 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001225 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001226 break;
1227 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001228 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001229 break;
1230 }
aliguori376253e2009-03-05 23:01:23 +00001231 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001232 switch(format) {
1233 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001234 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001235 break;
1236 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001237 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001238 break;
1239 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001240 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001241 break;
1242 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001243 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001244 break;
1245 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001246 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001247 break;
1248 }
1249 i += wsize;
1250 }
aliguori376253e2009-03-05 23:01:23 +00001251 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001252 addr += l;
1253 len -= l;
1254 }
1255}
1256
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001257static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001258{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001259 int count = qdict_get_int(qdict, "count");
1260 int format = qdict_get_int(qdict, "format");
1261 int size = qdict_get_int(qdict, "size");
1262 target_long addr = qdict_get_int(qdict, "addr");
1263
aliguori376253e2009-03-05 23:01:23 +00001264 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001265}
1266
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001267static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001268{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001269 int count = qdict_get_int(qdict, "count");
1270 int format = qdict_get_int(qdict, "format");
1271 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001272 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001273
aliguori376253e2009-03-05 23:01:23 +00001274 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001275}
1276
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001277static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001278{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001279 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001280 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001281
bellard9307c4c2004-04-04 12:57:25 +00001282 switch(format) {
1283 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001284 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001285 break;
1286 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001287 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001288 break;
1289 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001290 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001291 break;
1292 default:
1293 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001294 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001295 break;
1296 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001297 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001298 break;
1299 }
aliguori376253e2009-03-05 23:01:23 +00001300 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001301}
1302
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001303static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001304{
1305 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001306 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001307 uint32_t start = qdict_get_int(qdict, "start");
1308 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001309
1310 sum = 0;
1311 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001312 uint8_t val = address_space_ldub(&address_space_memory, addr,
1313 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001314 /* BSD sum algorithm ('sum' Unix command) */
1315 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001316 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001317 }
aliguori376253e2009-03-05 23:01:23 +00001318 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001319}
1320
bellard13224a82006-07-14 22:03:35 +00001321static int mouse_button_state;
1322
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001323static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001324{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001325 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001326 const char *dx_str = qdict_get_str(qdict, "dx_str");
1327 const char *dy_str = qdict_get_str(qdict, "dy_str");
1328 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001329
bellard13224a82006-07-14 22:03:35 +00001330 dx = strtol(dx_str, NULL, 0);
1331 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001332 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1333 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1334
1335 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001336 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001337 if (dz != 0) {
1338 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1339 qemu_input_queue_btn(NULL, button, true);
1340 qemu_input_event_sync();
1341 qemu_input_queue_btn(NULL, button, false);
1342 }
1343 }
1344 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001345}
1346
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001347static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001348{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001349 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1350 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1351 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1352 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1353 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001354 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001355
1356 if (mouse_button_state == button_state) {
1357 return;
1358 }
1359 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1360 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001361 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001362}
1363
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001364static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001365{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001366 int size = qdict_get_int(qdict, "size");
1367 int addr = qdict_get_int(qdict, "addr");
1368 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001369 uint32_t val;
1370 int suffix;
1371
1372 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001373 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001374 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001375 addr++;
1376 }
1377 addr &= 0xffff;
1378
1379 switch(size) {
1380 default:
1381 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001382 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001383 suffix = 'b';
1384 break;
1385 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001386 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001387 suffix = 'w';
1388 break;
1389 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001390 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001391 suffix = 'l';
1392 break;
1393 }
aliguori376253e2009-03-05 23:01:23 +00001394 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1395 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001396}
bellarda3a91a32004-06-04 11:06:21 +00001397
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001398static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001399{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001400 int size = qdict_get_int(qdict, "size");
1401 int addr = qdict_get_int(qdict, "addr");
1402 int val = qdict_get_int(qdict, "val");
1403
Jan Kiszkaf1147842009-07-14 10:20:11 +02001404 addr &= IOPORTS_MASK;
1405
1406 switch (size) {
1407 default:
1408 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001409 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001410 break;
1411 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001412 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001413 break;
1414 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001415 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001416 break;
1417 }
1418}
1419
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001420static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001421{
Gongleif1839932014-12-03 18:20:58 +00001422 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001423 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001424
Gongleif1839932014-12-03 18:20:58 +00001425 qemu_boot_set(bootdevice, &local_err);
1426 if (local_err) {
1427 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1428 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001429 } else {
Gongleif1839932014-12-03 18:20:58 +00001430 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001431 }
1432}
1433
bellardb86bda52004-09-18 19:32:46 +00001434#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001435static void print_pte(Monitor *mon, hwaddr addr,
1436 hwaddr pte,
1437 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001438{
Blue Swirld65aaf32010-12-11 18:56:24 +00001439#ifdef TARGET_X86_64
1440 if (addr & (1ULL << 47)) {
1441 addr |= -1LL << 48;
1442 }
1443#endif
1444 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1445 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001446 addr,
1447 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001448 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001449 pte & PG_GLOBAL_MASK ? 'G' : '-',
1450 pte & PG_PSE_MASK ? 'P' : '-',
1451 pte & PG_DIRTY_MASK ? 'D' : '-',
1452 pte & PG_ACCESSED_MASK ? 'A' : '-',
1453 pte & PG_PCD_MASK ? 'C' : '-',
1454 pte & PG_PWT_MASK ? 'T' : '-',
1455 pte & PG_USER_MASK ? 'U' : '-',
1456 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001457}
1458
Andreas Färber9349b4f2012-03-14 01:38:32 +01001459static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001460{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001461 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001462 uint32_t pgd, pde, pte;
1463
bellardb86bda52004-09-18 19:32:46 +00001464 pgd = env->cr[3] & ~0xfff;
1465 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001466 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001467 pde = le32_to_cpu(pde);
1468 if (pde & PG_PRESENT_MASK) {
1469 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001470 /* 4M pages */
1471 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001472 } else {
1473 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001474 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001475 pte = le32_to_cpu(pte);
1476 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001477 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001478 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001479 ~0xfff);
1480 }
1481 }
1482 }
1483 }
1484 }
1485}
1486
Andreas Färber9349b4f2012-03-14 01:38:32 +01001487static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001488{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001489 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001490 uint64_t pdpe, pde, pte;
1491 uint64_t pdp_addr, pd_addr, pt_addr;
1492
1493 pdp_addr = env->cr[3] & ~0x1f;
1494 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001495 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001496 pdpe = le64_to_cpu(pdpe);
1497 if (pdpe & PG_PRESENT_MASK) {
1498 pd_addr = pdpe & 0x3fffffffff000ULL;
1499 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001500 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001501 pde = le64_to_cpu(pde);
1502 if (pde & PG_PRESENT_MASK) {
1503 if (pde & PG_PSE_MASK) {
1504 /* 2M pages with PAE, CR4.PSE is ignored */
1505 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001506 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001507 } else {
1508 pt_addr = pde & 0x3fffffffff000ULL;
1509 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001510 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001511 pte = le64_to_cpu(pte);
1512 if (pte & PG_PRESENT_MASK) {
1513 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1514 + (l3 << 12),
1515 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001516 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001517 }
1518 }
1519 }
1520 }
1521 }
1522 }
1523 }
1524}
1525
1526#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001527static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001528{
1529 uint64_t l1, l2, l3, l4;
1530 uint64_t pml4e, pdpe, pde, pte;
1531 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1532
1533 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1534 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001535 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001536 pml4e = le64_to_cpu(pml4e);
1537 if (pml4e & PG_PRESENT_MASK) {
1538 pdp_addr = pml4e & 0x3fffffffff000ULL;
1539 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001540 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001541 pdpe = le64_to_cpu(pdpe);
1542 if (pdpe & PG_PRESENT_MASK) {
1543 if (pdpe & PG_PSE_MASK) {
1544 /* 1G pages, CR4.PSE is ignored */
1545 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1546 0x3ffffc0000000ULL);
1547 } else {
1548 pd_addr = pdpe & 0x3fffffffff000ULL;
1549 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001550 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001551 pde = le64_to_cpu(pde);
1552 if (pde & PG_PRESENT_MASK) {
1553 if (pde & PG_PSE_MASK) {
1554 /* 2M pages, CR4.PSE is ignored */
1555 print_pte(mon, (l1 << 39) + (l2 << 30) +
1556 (l3 << 21), pde,
1557 0x3ffffffe00000ULL);
1558 } else {
1559 pt_addr = pde & 0x3fffffffff000ULL;
1560 for (l4 = 0; l4 < 512; l4++) {
1561 cpu_physical_memory_read(pt_addr
1562 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001563 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001564 pte = le64_to_cpu(pte);
1565 if (pte & PG_PRESENT_MASK) {
1566 print_pte(mon, (l1 << 39) +
1567 (l2 << 30) +
1568 (l3 << 21) + (l4 << 12),
1569 pte & ~PG_PSE_MASK,
1570 0x3fffffffff000ULL);
1571 }
1572 }
1573 }
1574 }
1575 }
1576 }
1577 }
1578 }
1579 }
1580 }
1581}
1582#endif
1583
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001584static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001585{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001586 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001587
1588 env = mon_get_cpu();
1589
1590 if (!(env->cr[0] & CR0_PG_MASK)) {
1591 monitor_printf(mon, "PG disabled\n");
1592 return;
1593 }
1594 if (env->cr[4] & CR4_PAE_MASK) {
1595#ifdef TARGET_X86_64
1596 if (env->hflags & HF_LMA_MASK) {
1597 tlb_info_64(mon, env);
1598 } else
1599#endif
1600 {
1601 tlb_info_pae32(mon, env);
1602 }
1603 } else {
1604 tlb_info_32(mon, env);
1605 }
1606}
1607
Avi Kivitya8170e52012-10-23 12:30:10 +02001608static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001609 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001610 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001611{
bellard9746b152004-11-11 18:30:24 +00001612 int prot1;
1613 prot1 = *plast_prot;
1614 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001615 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001616 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1617 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001618 *pstart, end, end - *pstart,
1619 prot1 & PG_USER_MASK ? 'u' : '-',
1620 'r',
1621 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001622 }
1623 if (prot != 0)
1624 *pstart = end;
1625 else
1626 *pstart = -1;
1627 *plast_prot = prot;
1628 }
1629}
1630
Andreas Färber9349b4f2012-03-14 01:38:32 +01001631static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001632{
Austin Clementsb49ca722011-08-14 23:19:21 -04001633 unsigned int l1, l2;
1634 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001635 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001636 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001637
bellardb86bda52004-09-18 19:32:46 +00001638 pgd = env->cr[3] & ~0xfff;
1639 last_prot = 0;
1640 start = -1;
1641 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001642 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001643 pde = le32_to_cpu(pde);
1644 end = l1 << 22;
1645 if (pde & PG_PRESENT_MASK) {
1646 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1647 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001648 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001649 } else {
1650 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001651 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001652 pte = le32_to_cpu(pte);
1653 end = (l1 << 22) + (l2 << 12);
1654 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001655 prot = pte & pde &
1656 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001657 } else {
1658 prot = 0;
1659 }
aliguori376253e2009-03-05 23:01:23 +00001660 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001661 }
1662 }
1663 } else {
1664 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001665 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001666 }
1667 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001668 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001669 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001670}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001671
Andreas Färber9349b4f2012-03-14 01:38:32 +01001672static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001673{
Austin Clementsb49ca722011-08-14 23:19:21 -04001674 unsigned int l1, l2, l3;
1675 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001676 uint64_t pdpe, pde, pte;
1677 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001678 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001679
1680 pdp_addr = env->cr[3] & ~0x1f;
1681 last_prot = 0;
1682 start = -1;
1683 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001684 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001685 pdpe = le64_to_cpu(pdpe);
1686 end = l1 << 30;
1687 if (pdpe & PG_PRESENT_MASK) {
1688 pd_addr = pdpe & 0x3fffffffff000ULL;
1689 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001690 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001691 pde = le64_to_cpu(pde);
1692 end = (l1 << 30) + (l2 << 21);
1693 if (pde & PG_PRESENT_MASK) {
1694 if (pde & PG_PSE_MASK) {
1695 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1696 PG_PRESENT_MASK);
1697 mem_print(mon, &start, &last_prot, end, prot);
1698 } else {
1699 pt_addr = pde & 0x3fffffffff000ULL;
1700 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001701 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001702 pte = le64_to_cpu(pte);
1703 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1704 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001705 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1706 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001707 } else {
1708 prot = 0;
1709 }
1710 mem_print(mon, &start, &last_prot, end, prot);
1711 }
1712 }
1713 } else {
1714 prot = 0;
1715 mem_print(mon, &start, &last_prot, end, prot);
1716 }
1717 }
1718 } else {
1719 prot = 0;
1720 mem_print(mon, &start, &last_prot, end, prot);
1721 }
1722 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001723 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001724 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001725}
1726
1727
1728#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001729static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001730{
1731 int prot, last_prot;
1732 uint64_t l1, l2, l3, l4;
1733 uint64_t pml4e, pdpe, pde, pte;
1734 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1735
1736 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1737 last_prot = 0;
1738 start = -1;
1739 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001740 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001741 pml4e = le64_to_cpu(pml4e);
1742 end = l1 << 39;
1743 if (pml4e & PG_PRESENT_MASK) {
1744 pdp_addr = pml4e & 0x3fffffffff000ULL;
1745 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001746 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001747 pdpe = le64_to_cpu(pdpe);
1748 end = (l1 << 39) + (l2 << 30);
1749 if (pdpe & PG_PRESENT_MASK) {
1750 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001751 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1752 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001753 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001754 mem_print(mon, &start, &last_prot, end, prot);
1755 } else {
1756 pd_addr = pdpe & 0x3fffffffff000ULL;
1757 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001758 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001759 pde = le64_to_cpu(pde);
1760 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1761 if (pde & PG_PRESENT_MASK) {
1762 if (pde & PG_PSE_MASK) {
1763 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1764 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001765 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001766 mem_print(mon, &start, &last_prot, end, prot);
1767 } else {
1768 pt_addr = pde & 0x3fffffffff000ULL;
1769 for (l4 = 0; l4 < 512; l4++) {
1770 cpu_physical_memory_read(pt_addr
1771 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001772 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001773 pte = le64_to_cpu(pte);
1774 end = (l1 << 39) + (l2 << 30) +
1775 (l3 << 21) + (l4 << 12);
1776 if (pte & PG_PRESENT_MASK) {
1777 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1778 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001779 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001780 } else {
1781 prot = 0;
1782 }
1783 mem_print(mon, &start, &last_prot, end, prot);
1784 }
1785 }
1786 } else {
1787 prot = 0;
1788 mem_print(mon, &start, &last_prot, end, prot);
1789 }
1790 }
1791 }
1792 } else {
1793 prot = 0;
1794 mem_print(mon, &start, &last_prot, end, prot);
1795 }
1796 }
1797 } else {
1798 prot = 0;
1799 mem_print(mon, &start, &last_prot, end, prot);
1800 }
1801 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001802 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001803 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001804}
1805#endif
1806
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001807static void hmp_info_mem(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001808{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001809 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001810
1811 env = mon_get_cpu();
1812
1813 if (!(env->cr[0] & CR0_PG_MASK)) {
1814 monitor_printf(mon, "PG disabled\n");
1815 return;
1816 }
1817 if (env->cr[4] & CR4_PAE_MASK) {
1818#ifdef TARGET_X86_64
1819 if (env->hflags & HF_LMA_MASK) {
1820 mem_info_64(mon, env);
1821 } else
1822#endif
1823 {
1824 mem_info_pae32(mon, env);
1825 }
1826 } else {
1827 mem_info_32(mon, env);
1828 }
1829}
bellardb86bda52004-09-18 19:32:46 +00001830#endif
1831
aurel327c664e22009-03-03 06:12:22 +00001832#if defined(TARGET_SH4)
1833
aliguori376253e2009-03-05 23:01:23 +00001834static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001835{
aliguori376253e2009-03-05 23:01:23 +00001836 monitor_printf(mon, " tlb%i:\t"
1837 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1838 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1839 "dirty=%hhu writethrough=%hhu\n",
1840 idx,
1841 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1842 tlb->v, tlb->sh, tlb->c, tlb->pr,
1843 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001844}
1845
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001846static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001847{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001848 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00001849 int i;
1850
aliguori376253e2009-03-05 23:01:23 +00001851 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001852 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001853 print_tlb (mon, i, &env->itlb[i]);
1854 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001855 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001856 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001857}
1858
1859#endif
1860
Max Filippov692f7372012-01-07 20:02:40 +04001861#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001862static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001863{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001864 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00001865
1866 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1867}
1868#endif
1869
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001870static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001871{
1872 mtree_info((fprintf_function)monitor_printf, mon);
1873}
1874
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001875static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001876{
aliguorib28b6232009-04-22 20:20:29 +00001877 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001878 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001879 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001880
zhanghailiang5b009e42014-11-04 19:49:30 +08001881 node_mem = g_new0(uint64_t, nb_numa_nodes);
1882 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001883 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1884 for (i = 0; i < nb_numa_nodes; i++) {
1885 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001886 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001887 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001888 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001889 }
1890 }
1891 monitor_printf(mon, "\n");
1892 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001893 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001894 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001895 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001896}
1897
bellard5f1ce942006-02-08 22:40:15 +00001898#ifdef CONFIG_PROFILER
1899
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001900int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001901int64_t dev_time;
1902
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001903static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001904{
aliguori376253e2009-03-05 23:01:23 +00001905 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001906 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001907 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001908 tcg_time, tcg_time / (double)get_ticks_per_sec());
1909 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001910 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001911}
1912#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001913static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001914{
aliguori376253e2009-03-05 23:01:23 +00001915 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001916}
1917#endif
1918
bellardec36b692006-07-16 18:57:03 +00001919/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001920static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001921
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001922static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001923{
1924 int i;
1925 CaptureState *s;
1926
1927 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001928 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001929 s->ops.info (s->opaque);
1930 }
1931}
1932
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001933static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001934{
1935 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001936 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001937 CaptureState *s;
1938
1939 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1940 if (i == n) {
1941 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001942 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001943 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001944 return;
1945 }
1946 }
1947}
1948
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001949static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001950{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001951 const char *path = qdict_get_str(qdict, "path");
1952 int has_freq = qdict_haskey(qdict, "freq");
1953 int freq = qdict_get_try_int(qdict, "freq", -1);
1954 int has_bits = qdict_haskey(qdict, "bits");
1955 int bits = qdict_get_try_int(qdict, "bits", -1);
1956 int has_channels = qdict_haskey(qdict, "nchannels");
1957 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001958 CaptureState *s;
1959
Anthony Liguori7267c092011-08-20 22:09:37 -05001960 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001961
1962 freq = has_freq ? freq : 44100;
1963 bits = has_bits ? bits : 16;
1964 nchannels = has_channels ? nchannels : 2;
1965
1966 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001967 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001968 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001969 return;
bellardec36b692006-07-16 18:57:03 +00001970 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001971 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001972}
bellardec36b692006-07-16 18:57:03 +00001973
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001974static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001975{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001976 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001977
aliguori76655d62009-03-06 20:27:37 +00001978 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001979 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001980 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001981 return acl;
1982}
aliguori76655d62009-03-06 20:27:37 +00001983
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001984static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001985{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001986 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001987 qemu_acl *acl = find_acl(mon, aclname);
1988 qemu_acl_entry *entry;
1989 int i = 0;
1990
1991 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001992 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001993 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001994 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001995 i++;
1996 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001997 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001998 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001999 }
2000}
2001
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002002static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002003{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002004 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002005 qemu_acl *acl = find_acl(mon, aclname);
2006
2007 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002008 qemu_acl_reset(acl);
2009 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002010 }
2011}
aliguori76655d62009-03-06 20:27:37 +00002012
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002013static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002014{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002015 const char *aclname = qdict_get_str(qdict, "aclname");
2016 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002017 qemu_acl *acl = find_acl(mon, aclname);
2018
2019 if (acl) {
2020 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002021 acl->defaultDeny = 0;
2022 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002023 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002024 acl->defaultDeny = 1;
2025 monitor_printf(mon, "acl: policy set to 'deny'\n");
2026 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002027 monitor_printf(mon, "acl: unknown policy '%s', "
2028 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002029 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002030 }
2031}
aliguori76655d62009-03-06 20:27:37 +00002032
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002033static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002034{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002035 const char *aclname = qdict_get_str(qdict, "aclname");
2036 const char *match = qdict_get_str(qdict, "match");
2037 const char *policy = qdict_get_str(qdict, "policy");
2038 int has_index = qdict_haskey(qdict, "index");
2039 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002040 qemu_acl *acl = find_acl(mon, aclname);
2041 int deny, ret;
2042
2043 if (acl) {
2044 if (strcmp(policy, "allow") == 0) {
2045 deny = 0;
2046 } else if (strcmp(policy, "deny") == 0) {
2047 deny = 1;
2048 } else {
2049 monitor_printf(mon, "acl: unknown policy '%s', "
2050 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002051 return;
2052 }
aliguori28a76be2009-03-06 20:27:40 +00002053 if (has_index)
2054 ret = qemu_acl_insert(acl, deny, match, index);
2055 else
2056 ret = qemu_acl_append(acl, deny, match);
2057 if (ret < 0)
2058 monitor_printf(mon, "acl: unable to add acl entry\n");
2059 else
2060 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002061 }
2062}
aliguori76655d62009-03-06 20:27:37 +00002063
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002064static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002065{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002066 const char *aclname = qdict_get_str(qdict, "aclname");
2067 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002068 qemu_acl *acl = find_acl(mon, aclname);
2069 int ret;
aliguori76655d62009-03-06 20:27:37 +00002070
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002071 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002072 ret = qemu_acl_remove(acl, match);
2073 if (ret < 0)
2074 monitor_printf(mon, "acl: no matching acl entry\n");
2075 else
2076 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002077 }
2078}
2079
Huang Ying79c4f6b2009-06-23 10:05:14 +08002080#if defined(TARGET_I386)
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002081static void hmp_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002082{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002083 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002084 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002085 int cpu_index = qdict_get_int(qdict, "cpu_index");
2086 int bank = qdict_get_int(qdict, "bank");
2087 uint64_t status = qdict_get_int(qdict, "status");
2088 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2089 uint64_t addr = qdict_get_int(qdict, "addr");
2090 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002091 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002092
Jan Kiszka747461c2011-03-02 08:56:10 +01002093 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2094 flags |= MCE_INJECT_BROADCAST;
2095 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002096 cs = qemu_get_cpu(cpu_index);
2097 if (cs != NULL) {
2098 cpu = X86_CPU(cs);
2099 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2100 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002101 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002102}
2103#endif
2104
Corey Bryant208c9d12012-06-22 14:36:09 -04002105void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002106{
Anthony Liguoric227f092009-10-01 16:12:16 -05002107 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002108 int fd;
2109
Corey Bryant208c9d12012-06-22 14:36:09 -04002110 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002111 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002112 error_set(errp, QERR_FD_NOT_SUPPLIED);
2113 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002114 }
2115
2116 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002117 close(fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002118 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2119 "a name not starting with a digit");
2120 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002121 }
2122
Corey Bryant208c9d12012-06-22 14:36:09 -04002123 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002124 if (strcmp(monfd->name, fdname) != 0) {
2125 continue;
2126 }
2127
2128 close(monfd->fd);
2129 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002130 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002131 }
2132
Anthony Liguori7267c092011-08-20 22:09:37 -05002133 monfd = g_malloc0(sizeof(mon_fd_t));
2134 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002135 monfd->fd = fd;
2136
Corey Bryant208c9d12012-06-22 14:36:09 -04002137 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002138}
2139
Corey Bryant208c9d12012-06-22 14:36:09 -04002140void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002141{
Anthony Liguoric227f092009-10-01 16:12:16 -05002142 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002143
Corey Bryant208c9d12012-06-22 14:36:09 -04002144 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002145 if (strcmp(monfd->name, fdname) != 0) {
2146 continue;
2147 }
2148
Blue Swirl72cf2d42009-09-12 07:36:22 +00002149 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002150 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002151 g_free(monfd->name);
2152 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002153 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002154 }
2155
Corey Bryant208c9d12012-06-22 14:36:09 -04002156 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002157}
2158
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002159static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002160{
Luiz Capitulino13548692011-07-29 15:36:43 -03002161 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002162 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002163
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002164 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002165
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002166 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002167 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002168 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002169}
2170
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002171int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002172{
Anthony Liguoric227f092009-10-01 16:12:16 -05002173 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002174
Blue Swirl72cf2d42009-09-12 07:36:22 +00002175 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002176 int fd;
2177
2178 if (strcmp(monfd->name, fdname) != 0) {
2179 continue;
2180 }
2181
2182 fd = monfd->fd;
2183
2184 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002185 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002186 g_free(monfd->name);
2187 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002188
2189 return fd;
2190 }
2191
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002192 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002193 return -1;
2194}
2195
Corey Bryantba1c0482012-08-14 16:43:43 -04002196static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2197{
2198 MonFdsetFd *mon_fdset_fd;
2199 MonFdsetFd *mon_fdset_fd_next;
2200
2201 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002202 if ((mon_fdset_fd->removed ||
2203 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2204 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002205 close(mon_fdset_fd->fd);
2206 g_free(mon_fdset_fd->opaque);
2207 QLIST_REMOVE(mon_fdset_fd, next);
2208 g_free(mon_fdset_fd);
2209 }
2210 }
2211
Corey Bryantadb696f2012-08-14 16:43:47 -04002212 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002213 QLIST_REMOVE(mon_fdset, next);
2214 g_free(mon_fdset);
2215 }
2216}
2217
Corey Bryantefb87c12012-08-14 16:43:48 -04002218static void monitor_fdsets_cleanup(void)
2219{
2220 MonFdset *mon_fdset;
2221 MonFdset *mon_fdset_next;
2222
2223 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2224 monitor_fdset_cleanup(mon_fdset);
2225 }
2226}
2227
Corey Bryantba1c0482012-08-14 16:43:43 -04002228AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2229 const char *opaque, Error **errp)
2230{
2231 int fd;
2232 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002233 AddfdInfo *fdinfo;
2234
2235 fd = qemu_chr_fe_get_msgfd(mon->chr);
2236 if (fd == -1) {
2237 error_set(errp, QERR_FD_NOT_SUPPLIED);
2238 goto error;
2239 }
2240
Corey Bryante446f702012-10-18 15:19:32 -04002241 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2242 has_opaque, opaque, errp);
2243 if (fdinfo) {
2244 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002245 }
2246
Corey Bryantba1c0482012-08-14 16:43:43 -04002247error:
2248 if (fd != -1) {
2249 close(fd);
2250 }
2251 return NULL;
2252}
2253
2254void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2255{
2256 MonFdset *mon_fdset;
2257 MonFdsetFd *mon_fdset_fd;
2258 char fd_str[60];
2259
2260 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2261 if (mon_fdset->id != fdset_id) {
2262 continue;
2263 }
2264 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2265 if (has_fd) {
2266 if (mon_fdset_fd->fd != fd) {
2267 continue;
2268 }
2269 mon_fdset_fd->removed = true;
2270 break;
2271 } else {
2272 mon_fdset_fd->removed = true;
2273 }
2274 }
2275 if (has_fd && !mon_fdset_fd) {
2276 goto error;
2277 }
2278 monitor_fdset_cleanup(mon_fdset);
2279 return;
2280 }
2281
2282error:
2283 if (has_fd) {
2284 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2285 fdset_id, fd);
2286 } else {
2287 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2288 }
2289 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2290}
2291
2292FdsetInfoList *qmp_query_fdsets(Error **errp)
2293{
2294 MonFdset *mon_fdset;
2295 MonFdsetFd *mon_fdset_fd;
2296 FdsetInfoList *fdset_list = NULL;
2297
2298 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2299 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2300 FdsetFdInfoList *fdsetfd_list = NULL;
2301
2302 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2303 fdset_info->value->fdset_id = mon_fdset->id;
2304
2305 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2306 FdsetFdInfoList *fdsetfd_info;
2307
2308 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2309 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2310 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2311 if (mon_fdset_fd->opaque) {
2312 fdsetfd_info->value->has_opaque = true;
2313 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2314 } else {
2315 fdsetfd_info->value->has_opaque = false;
2316 }
2317
2318 fdsetfd_info->next = fdsetfd_list;
2319 fdsetfd_list = fdsetfd_info;
2320 }
2321
2322 fdset_info->value->fds = fdsetfd_list;
2323
2324 fdset_info->next = fdset_list;
2325 fdset_list = fdset_info;
2326 }
2327
2328 return fdset_list;
2329}
2330
Corey Bryante446f702012-10-18 15:19:32 -04002331AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2332 bool has_opaque, const char *opaque,
2333 Error **errp)
2334{
2335 MonFdset *mon_fdset = NULL;
2336 MonFdsetFd *mon_fdset_fd;
2337 AddfdInfo *fdinfo;
2338
2339 if (has_fdset_id) {
2340 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2341 /* Break if match found or match impossible due to ordering by ID */
2342 if (fdset_id <= mon_fdset->id) {
2343 if (fdset_id < mon_fdset->id) {
2344 mon_fdset = NULL;
2345 }
2346 break;
2347 }
2348 }
2349 }
2350
2351 if (mon_fdset == NULL) {
2352 int64_t fdset_id_prev = -1;
2353 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2354
2355 if (has_fdset_id) {
2356 if (fdset_id < 0) {
2357 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2358 "a non-negative value");
2359 return NULL;
2360 }
2361 /* Use specified fdset ID */
2362 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2363 mon_fdset_cur = mon_fdset;
2364 if (fdset_id < mon_fdset_cur->id) {
2365 break;
2366 }
2367 }
2368 } else {
2369 /* Use first available fdset ID */
2370 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2371 mon_fdset_cur = mon_fdset;
2372 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2373 fdset_id_prev = mon_fdset_cur->id;
2374 continue;
2375 }
2376 break;
2377 }
2378 }
2379
2380 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2381 if (has_fdset_id) {
2382 mon_fdset->id = fdset_id;
2383 } else {
2384 mon_fdset->id = fdset_id_prev + 1;
2385 }
2386
2387 /* The fdset list is ordered by fdset ID */
2388 if (!mon_fdset_cur) {
2389 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2390 } else if (mon_fdset->id < mon_fdset_cur->id) {
2391 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2392 } else {
2393 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2394 }
2395 }
2396
2397 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2398 mon_fdset_fd->fd = fd;
2399 mon_fdset_fd->removed = false;
2400 if (has_opaque) {
2401 mon_fdset_fd->opaque = g_strdup(opaque);
2402 }
2403 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2404
2405 fdinfo = g_malloc0(sizeof(*fdinfo));
2406 fdinfo->fdset_id = mon_fdset->id;
2407 fdinfo->fd = mon_fdset_fd->fd;
2408
2409 return fdinfo;
2410}
2411
Corey Bryantadb696f2012-08-14 16:43:47 -04002412int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2413{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002414#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002415 MonFdset *mon_fdset;
2416 MonFdsetFd *mon_fdset_fd;
2417 int mon_fd_flags;
2418
Corey Bryantadb696f2012-08-14 16:43:47 -04002419 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2420 if (mon_fdset->id != fdset_id) {
2421 continue;
2422 }
2423 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2424 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2425 if (mon_fd_flags == -1) {
2426 return -1;
2427 }
2428
2429 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2430 return mon_fdset_fd->fd;
2431 }
2432 }
2433 errno = EACCES;
2434 return -1;
2435 }
2436#endif
2437
2438 errno = ENOENT;
2439 return -1;
2440}
2441
2442int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2443{
2444 MonFdset *mon_fdset;
2445 MonFdsetFd *mon_fdset_fd_dup;
2446
2447 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2448 if (mon_fdset->id != fdset_id) {
2449 continue;
2450 }
2451 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2452 if (mon_fdset_fd_dup->fd == dup_fd) {
2453 return -1;
2454 }
2455 }
2456 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2457 mon_fdset_fd_dup->fd = dup_fd;
2458 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2459 return 0;
2460 }
2461 return -1;
2462}
2463
2464static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2465{
2466 MonFdset *mon_fdset;
2467 MonFdsetFd *mon_fdset_fd_dup;
2468
2469 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2470 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2471 if (mon_fdset_fd_dup->fd == dup_fd) {
2472 if (remove) {
2473 QLIST_REMOVE(mon_fdset_fd_dup, next);
2474 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2475 monitor_fdset_cleanup(mon_fdset);
2476 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002477 return -1;
2478 } else {
2479 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002480 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002481 }
2482 }
2483 }
2484 return -1;
2485}
2486
2487int monitor_fdset_dup_fd_find(int dup_fd)
2488{
2489 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2490}
2491
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002492void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002493{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002494 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002495}
2496
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002497int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002498{
2499 int fd;
2500 Error *local_err = NULL;
2501
2502 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002503 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002504 } else {
2505 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002506 if (fd == -1) {
2507 error_setg(&local_err, "Invalid file descriptor number '%s'",
2508 fdname);
2509 }
2510 }
2511 if (local_err) {
2512 error_propagate(errp, local_err);
2513 assert(fd == -1);
2514 } else {
2515 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002516 }
2517
2518 return fd;
2519}
2520
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002521/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002522static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002523 {
2524 .name = "version",
2525 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002526 .params = "",
2527 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002528 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002529 },
2530 {
2531 .name = "network",
2532 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002533 .params = "",
2534 .help = "show the network state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002535 .mhandler.cmd = hmp_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002536 },
2537 {
2538 .name = "chardev",
2539 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002540 .params = "",
2541 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002542 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002543 },
2544 {
2545 .name = "block",
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002546 .args_type = "nodes:-n,verbose:-v,device:B?",
2547 .params = "[-n] [-v] [device]",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002548 .help = "show info of one block device or all block devices "
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002549 "(-n: show named nodes; -v: show details)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002550 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002551 },
2552 {
2553 .name = "blockstats",
2554 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002555 .params = "",
2556 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002557 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002558 },
2559 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002560 .name = "block-jobs",
2561 .args_type = "",
2562 .params = "",
2563 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002564 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002565 },
2566 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002567 .name = "registers",
2568 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002569 .params = "",
2570 .help = "show the cpu registers",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002571 .mhandler.cmd = hmp_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002572 },
2573 {
2574 .name = "cpus",
2575 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002576 .params = "",
2577 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002578 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002579 },
2580 {
2581 .name = "history",
2582 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002583 .params = "",
2584 .help = "show the command line history",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002585 .mhandler.cmd = hmp_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002586 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002587#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2588 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002589 {
2590 .name = "irq",
2591 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002592 .params = "",
2593 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002594#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002595 .mhandler.cmd = sun4m_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002596#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002597 .mhandler.cmd = lm32_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002598#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002599 .mhandler.cmd = hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002600#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002601 },
2602 {
2603 .name = "pic",
2604 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002605 .params = "",
2606 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002607#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002608 .mhandler.cmd = sun4m_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002609#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002610 .mhandler.cmd = lm32_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002611#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002612 .mhandler.cmd = hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002613#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002614 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002615#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002616 {
2617 .name = "pci",
2618 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002619 .params = "",
2620 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002621 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002622 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002623#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002624 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002625 {
2626 .name = "tlb",
2627 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002628 .params = "",
2629 .help = "show virtual to physical memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002630 .mhandler.cmd = hmp_info_tlb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002631 },
aurel327c664e22009-03-03 06:12:22 +00002632#endif
2633#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002634 {
2635 .name = "mem",
2636 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002637 .params = "",
2638 .help = "show the active virtual memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002639 .mhandler.cmd = hmp_info_mem,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002640 },
bellardb86bda52004-09-18 19:32:46 +00002641#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002642 {
Blue Swirl314e2982011-09-11 20:22:05 +00002643 .name = "mtree",
2644 .args_type = "",
2645 .params = "",
2646 .help = "show memory tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002647 .mhandler.cmd = hmp_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002648 },
2649 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002650 .name = "jit",
2651 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002652 .params = "",
2653 .help = "show dynamic compiler info",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002654 .mhandler.cmd = hmp_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002655 },
2656 {
Max Filippov246ae242014-11-02 11:04:18 +03002657 .name = "opcount",
2658 .args_type = "",
2659 .params = "",
2660 .help = "show dynamic compiler opcode counters",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002661 .mhandler.cmd = hmp_info_opcount,
Max Filippov246ae242014-11-02 11:04:18 +03002662 },
2663 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 .name = "kvm",
2665 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002666 .params = "",
2667 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002668 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002669 },
2670 {
2671 .name = "numa",
2672 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002673 .params = "",
2674 .help = "show NUMA information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002675 .mhandler.cmd = hmp_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002676 },
2677 {
2678 .name = "usb",
2679 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002680 .params = "",
2681 .help = "show guest USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002682 .mhandler.cmd = hmp_info_usb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002683 },
2684 {
2685 .name = "usbhost",
2686 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002687 .params = "",
2688 .help = "show host USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002689 .mhandler.cmd = hmp_info_usbhost,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002690 },
2691 {
2692 .name = "profile",
2693 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002694 .params = "",
2695 .help = "show profiling information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002696 .mhandler.cmd = hmp_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002697 },
2698 {
2699 .name = "capture",
2700 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002701 .params = "",
2702 .help = "show capture information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002703 .mhandler.cmd = hmp_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002704 },
2705 {
2706 .name = "snapshots",
2707 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002708 .params = "",
2709 .help = "show the currently saved VM snapshots",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002710 .mhandler.cmd = hmp_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002711 },
2712 {
2713 .name = "status",
2714 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002715 .params = "",
2716 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002717 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002718 },
2719 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002720 .name = "mice",
2721 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002722 .params = "",
2723 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002724 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002725 },
2726 {
2727 .name = "vnc",
2728 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002729 .params = "",
2730 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002731 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002732 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002733#if defined(CONFIG_SPICE)
2734 {
2735 .name = "spice",
2736 .args_type = "",
2737 .params = "",
2738 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002739 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002740 },
2741#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002742 {
2743 .name = "name",
2744 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002745 .params = "",
2746 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002747 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002748 },
2749 {
2750 .name = "uuid",
2751 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002752 .params = "",
2753 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002754 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002755 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002756 {
2757 .name = "cpustats",
2758 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002759 .params = "",
2760 .help = "show CPU statistics",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002761 .mhandler.cmd = hmp_info_cpustats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002762 },
blueswir131a60e22007-10-26 18:42:59 +00002763#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 {
2765 .name = "usernet",
2766 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 .params = "",
2768 .help = "show user network stack connection states",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002769 .mhandler.cmd = hmp_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002770 },
blueswir131a60e22007-10-26 18:42:59 +00002771#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002772 {
2773 .name = "migrate",
2774 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002775 .params = "",
2776 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002777 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002778 },
2779 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002780 .name = "migrate_capabilities",
2781 .args_type = "",
2782 .params = "",
2783 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002784 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002785 },
2786 {
Liang Li50e9a622015-03-23 16:32:29 +08002787 .name = "migrate_parameters",
2788 .args_type = "",
2789 .params = "",
2790 .help = "show current migration parameters",
2791 .mhandler.cmd = hmp_info_migrate_parameters,
2792 },
2793 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002794 .name = "migrate_cache_size",
2795 .args_type = "",
2796 .params = "",
2797 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002798 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002799 },
2800 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002801 .name = "balloon",
2802 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002803 .params = "",
2804 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002805 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002806 },
2807 {
2808 .name = "qtree",
2809 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002810 .params = "",
2811 .help = "show device tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002812 .mhandler.cmd = hmp_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002813 },
2814 {
2815 .name = "qdm",
2816 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002817 .params = "",
2818 .help = "show qdev device model list",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002819 .mhandler.cmd = hmp_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002820 },
2821 {
Andreas Färbera01ff752014-05-07 17:03:18 +02002822 .name = "qom-tree",
2823 .args_type = "path:s?",
2824 .params = "[path]",
2825 .help = "show QOM composition tree",
2826 .mhandler.cmd = hmp_info_qom_tree,
2827 },
2828 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002829 .name = "roms",
2830 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002831 .params = "",
2832 .help = "show roms",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002833 .mhandler.cmd = hmp_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002834 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302835 {
2836 .name = "trace-events",
2837 .args_type = "",
2838 .params = "",
2839 .help = "show available trace-events & their state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002840 .mhandler.cmd = hmp_info_trace_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302841 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002842 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002843 .name = "tpm",
2844 .args_type = "",
2845 .params = "",
2846 .help = "show the TPM device",
2847 .mhandler.cmd = hmp_info_tpm,
2848 },
2849 {
Hu Taoeb1539b2014-05-14 17:43:35 +08002850 .name = "memdev",
2851 .args_type = "",
2852 .params = "",
Igor Mammedov8f4e5ac2014-06-19 16:14:43 +02002853 .help = "show memory backends",
Hu Taoeb1539b2014-05-14 17:43:35 +08002854 .mhandler.cmd = hmp_info_memdev,
2855 },
2856 {
Zhu Guihuaa6318922014-09-23 13:35:19 +08002857 .name = "memory-devices",
2858 .args_type = "",
2859 .params = "",
2860 .help = "show memory devices",
2861 .mhandler.cmd = hmp_info_memory_devices,
2862 },
2863 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002864 .name = NULL,
2865 },
bellard9dc39cb2004-03-14 21:38:27 +00002866};
2867
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002868/* mon_cmds and info_cmds would be sorted at runtime */
2869static mon_cmd_t mon_cmds[] = {
2870#include "hmp-commands.h"
2871 { NULL, NULL, },
2872};
2873
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002874static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002875#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002876 { /* NULL */ },
2877};
2878
bellard9307c4c2004-04-04 12:57:25 +00002879/*******************************************************************/
2880
2881static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002882static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002883
bellard92a31b12005-02-10 22:00:52 +00002884#define MD_TLONG 0
2885#define MD_I32 1
2886
bellard9307c4c2004-04-04 12:57:25 +00002887typedef struct MonitorDef {
2888 const char *name;
2889 int offset;
blueswir18662d652008-10-02 18:32:44 +00002890 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002891 int type;
bellard9307c4c2004-04-04 12:57:25 +00002892} MonitorDef;
2893
bellard57206fd2004-04-25 18:54:52 +00002894#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002895static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002896{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002897 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002898 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002899}
2900#endif
2901
bellarda541f292004-04-12 20:39:29 +00002902#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002903static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002904{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002905 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002906 unsigned int u;
2907 int i;
2908
2909 u = 0;
2910 for (i = 0; i < 8; i++)
Paolo Bonzinid2981182014-08-28 19:14:59 +02002911 u |= env->crf[i] << (32 - (4 * (i + 1)));
bellarda541f292004-04-12 20:39:29 +00002912
2913 return u;
2914}
2915
blueswir18662d652008-10-02 18:32:44 +00002916static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002917{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002918 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002919 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002920}
2921
blueswir18662d652008-10-02 18:32:44 +00002922static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002923{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002924 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002925 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002926}
bellard9fddaa02004-05-21 12:59:32 +00002927
blueswir18662d652008-10-02 18:32:44 +00002928static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002929{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002930 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002931 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002932}
2933
blueswir18662d652008-10-02 18:32:44 +00002934static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002935{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002936 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002937 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002938}
2939
blueswir18662d652008-10-02 18:32:44 +00002940static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002941{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002942 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002943 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002944}
bellarda541f292004-04-12 20:39:29 +00002945#endif
2946
bellarde95c8d52004-09-30 22:22:08 +00002947#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002948#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002949static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002950{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002951 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002952
2953 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002954}
bellard7b936c02005-10-30 17:05:13 +00002955#endif
bellarde95c8d52004-09-30 22:22:08 +00002956
blueswir18662d652008-10-02 18:32:44 +00002957static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002958{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002959 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002960 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002961}
2962#endif
2963
blueswir18662d652008-10-02 18:32:44 +00002964static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002965#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002966
2967#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01002968 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2969 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2970 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002971
Andreas Färbere59d1672012-02-16 00:40:47 +01002972 { "eax", offsetof(CPUX86State, regs[0]) },
2973 { "ecx", offsetof(CPUX86State, regs[1]) },
2974 { "edx", offsetof(CPUX86State, regs[2]) },
2975 { "ebx", offsetof(CPUX86State, regs[3]) },
2976 { "esp|sp", offsetof(CPUX86State, regs[4]) },
2977 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
2978 { "esi", offsetof(CPUX86State, regs[6]) },
2979 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002980#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01002981 { "r8", offsetof(CPUX86State, regs[8]) },
2982 { "r9", offsetof(CPUX86State, regs[9]) },
2983 { "r10", offsetof(CPUX86State, regs[10]) },
2984 { "r11", offsetof(CPUX86State, regs[11]) },
2985 { "r12", offsetof(CPUX86State, regs[12]) },
2986 { "r13", offsetof(CPUX86State, regs[13]) },
2987 { "r14", offsetof(CPUX86State, regs[14]) },
2988 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00002989#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01002990 { "eflags", offsetof(CPUX86State, eflags) },
2991 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00002992 SEG("cs", R_CS)
2993 SEG("ds", R_DS)
2994 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002995 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002996 SEG("fs", R_FS)
2997 SEG("gs", R_GS)
2998 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002999#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003000 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003001 { "r0", offsetof(CPUPPCState, gpr[0]) },
3002 { "r1", offsetof(CPUPPCState, gpr[1]) },
3003 { "r2", offsetof(CPUPPCState, gpr[2]) },
3004 { "r3", offsetof(CPUPPCState, gpr[3]) },
3005 { "r4", offsetof(CPUPPCState, gpr[4]) },
3006 { "r5", offsetof(CPUPPCState, gpr[5]) },
3007 { "r6", offsetof(CPUPPCState, gpr[6]) },
3008 { "r7", offsetof(CPUPPCState, gpr[7]) },
3009 { "r8", offsetof(CPUPPCState, gpr[8]) },
3010 { "r9", offsetof(CPUPPCState, gpr[9]) },
3011 { "r10", offsetof(CPUPPCState, gpr[10]) },
3012 { "r11", offsetof(CPUPPCState, gpr[11]) },
3013 { "r12", offsetof(CPUPPCState, gpr[12]) },
3014 { "r13", offsetof(CPUPPCState, gpr[13]) },
3015 { "r14", offsetof(CPUPPCState, gpr[14]) },
3016 { "r15", offsetof(CPUPPCState, gpr[15]) },
3017 { "r16", offsetof(CPUPPCState, gpr[16]) },
3018 { "r17", offsetof(CPUPPCState, gpr[17]) },
3019 { "r18", offsetof(CPUPPCState, gpr[18]) },
3020 { "r19", offsetof(CPUPPCState, gpr[19]) },
3021 { "r20", offsetof(CPUPPCState, gpr[20]) },
3022 { "r21", offsetof(CPUPPCState, gpr[21]) },
3023 { "r22", offsetof(CPUPPCState, gpr[22]) },
3024 { "r23", offsetof(CPUPPCState, gpr[23]) },
3025 { "r24", offsetof(CPUPPCState, gpr[24]) },
3026 { "r25", offsetof(CPUPPCState, gpr[25]) },
3027 { "r26", offsetof(CPUPPCState, gpr[26]) },
3028 { "r27", offsetof(CPUPPCState, gpr[27]) },
3029 { "r28", offsetof(CPUPPCState, gpr[28]) },
3030 { "r29", offsetof(CPUPPCState, gpr[29]) },
3031 { "r30", offsetof(CPUPPCState, gpr[30]) },
3032 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003033 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003034 { "f0", offsetof(CPUPPCState, fpr[0]) },
3035 { "f1", offsetof(CPUPPCState, fpr[1]) },
3036 { "f2", offsetof(CPUPPCState, fpr[2]) },
3037 { "f3", offsetof(CPUPPCState, fpr[3]) },
3038 { "f4", offsetof(CPUPPCState, fpr[4]) },
3039 { "f5", offsetof(CPUPPCState, fpr[5]) },
3040 { "f6", offsetof(CPUPPCState, fpr[6]) },
3041 { "f7", offsetof(CPUPPCState, fpr[7]) },
3042 { "f8", offsetof(CPUPPCState, fpr[8]) },
3043 { "f9", offsetof(CPUPPCState, fpr[9]) },
3044 { "f10", offsetof(CPUPPCState, fpr[10]) },
3045 { "f11", offsetof(CPUPPCState, fpr[11]) },
3046 { "f12", offsetof(CPUPPCState, fpr[12]) },
3047 { "f13", offsetof(CPUPPCState, fpr[13]) },
3048 { "f14", offsetof(CPUPPCState, fpr[14]) },
3049 { "f15", offsetof(CPUPPCState, fpr[15]) },
3050 { "f16", offsetof(CPUPPCState, fpr[16]) },
3051 { "f17", offsetof(CPUPPCState, fpr[17]) },
3052 { "f18", offsetof(CPUPPCState, fpr[18]) },
3053 { "f19", offsetof(CPUPPCState, fpr[19]) },
3054 { "f20", offsetof(CPUPPCState, fpr[20]) },
3055 { "f21", offsetof(CPUPPCState, fpr[21]) },
3056 { "f22", offsetof(CPUPPCState, fpr[22]) },
3057 { "f23", offsetof(CPUPPCState, fpr[23]) },
3058 { "f24", offsetof(CPUPPCState, fpr[24]) },
3059 { "f25", offsetof(CPUPPCState, fpr[25]) },
3060 { "f26", offsetof(CPUPPCState, fpr[26]) },
3061 { "f27", offsetof(CPUPPCState, fpr[27]) },
3062 { "f28", offsetof(CPUPPCState, fpr[28]) },
3063 { "f29", offsetof(CPUPPCState, fpr[29]) },
3064 { "f30", offsetof(CPUPPCState, fpr[30]) },
3065 { "f31", offsetof(CPUPPCState, fpr[31]) },
3066 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003067 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003068 { "nip|pc", offsetof(CPUPPCState, nip) },
3069 { "lr", offsetof(CPUPPCState, lr) },
3070 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003071 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003072 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003073 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003074 { "msr", 0, &monitor_get_msr, },
3075 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003076 { "tbu", 0, &monitor_get_tbu, },
3077 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003078 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003079 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3080 { "sr0", offsetof(CPUPPCState, sr[0]) },
3081 { "sr1", offsetof(CPUPPCState, sr[1]) },
3082 { "sr2", offsetof(CPUPPCState, sr[2]) },
3083 { "sr3", offsetof(CPUPPCState, sr[3]) },
3084 { "sr4", offsetof(CPUPPCState, sr[4]) },
3085 { "sr5", offsetof(CPUPPCState, sr[5]) },
3086 { "sr6", offsetof(CPUPPCState, sr[6]) },
3087 { "sr7", offsetof(CPUPPCState, sr[7]) },
3088 { "sr8", offsetof(CPUPPCState, sr[8]) },
3089 { "sr9", offsetof(CPUPPCState, sr[9]) },
3090 { "sr10", offsetof(CPUPPCState, sr[10]) },
3091 { "sr11", offsetof(CPUPPCState, sr[11]) },
3092 { "sr12", offsetof(CPUPPCState, sr[12]) },
3093 { "sr13", offsetof(CPUPPCState, sr[13]) },
3094 { "sr14", offsetof(CPUPPCState, sr[14]) },
3095 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003096 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003097 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003098
Andreas Färbere59d1672012-02-16 00:40:47 +01003099 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3100 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
Tom Musta04f1f782013-09-25 17:41:13 +10003101 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3102 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3103 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
Andreas Färbere59d1672012-02-16 00:40:47 +01003104 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3105 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3106 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3107 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3108 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3109 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3110 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3111 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3112 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3113 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3114 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3115 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3116 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3117 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3118 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3119 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3120 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3121 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3122 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3123 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3124 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3125 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3126 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3127 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3128 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3129 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3130 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3131 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3132 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3133 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3134 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3135 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3136 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3137 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3138 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3139 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3140 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3141 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3142 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3143 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3144 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3145 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3146 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3147 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3148 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3149 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3150 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3151 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3152 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3153 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3154 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3155 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3156 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3157 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3158 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3159 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3160 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3161 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3162 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3163 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3164 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3165 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3166 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3167 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003168
bellarde95c8d52004-09-30 22:22:08 +00003169#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003170 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3171 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3172 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3173 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3174 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3175 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3176 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3177 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003178 { "o0", 0, monitor_get_reg },
3179 { "o1", 1, monitor_get_reg },
3180 { "o2", 2, monitor_get_reg },
3181 { "o3", 3, monitor_get_reg },
3182 { "o4", 4, monitor_get_reg },
3183 { "o5", 5, monitor_get_reg },
3184 { "o6", 6, monitor_get_reg },
3185 { "o7", 7, monitor_get_reg },
3186 { "l0", 8, monitor_get_reg },
3187 { "l1", 9, monitor_get_reg },
3188 { "l2", 10, monitor_get_reg },
3189 { "l3", 11, monitor_get_reg },
3190 { "l4", 12, monitor_get_reg },
3191 { "l5", 13, monitor_get_reg },
3192 { "l6", 14, monitor_get_reg },
3193 { "l7", 15, monitor_get_reg },
3194 { "i0", 16, monitor_get_reg },
3195 { "i1", 17, monitor_get_reg },
3196 { "i2", 18, monitor_get_reg },
3197 { "i3", 19, monitor_get_reg },
3198 { "i4", 20, monitor_get_reg },
3199 { "i5", 21, monitor_get_reg },
3200 { "i6", 22, monitor_get_reg },
3201 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003202 { "pc", offsetof(CPUSPARCState, pc) },
3203 { "npc", offsetof(CPUSPARCState, npc) },
3204 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003205#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003206 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003207 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003208#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003209 { "tbr", offsetof(CPUSPARCState, tbr) },
3210 { "fsr", offsetof(CPUSPARCState, fsr) },
3211 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3212 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3213 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3214 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3215 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3216 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3217 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3218 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3219 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3220 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3221 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3222 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3223 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3224 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3225 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3226 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3227 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3228 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3229 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3230 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3231 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3232 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3233 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3234 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3235 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3236 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3237 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3238 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3239 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3240 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3241 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3242 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003243#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003244 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3245 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3246 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3247 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3248 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3249 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3250 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3251 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3252 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3253 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3254 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3255 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3256 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3257 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3258 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3259 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3260 { "asi", offsetof(CPUSPARCState, asi) },
3261 { "pstate", offsetof(CPUSPARCState, pstate) },
3262 { "cansave", offsetof(CPUSPARCState, cansave) },
3263 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3264 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3265 { "wstate", offsetof(CPUSPARCState, wstate) },
3266 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3267 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003268#endif
bellard9307c4c2004-04-04 12:57:25 +00003269#endif
3270 { NULL },
3271};
3272
Stefan Weil9c3175c2013-08-22 21:30:09 +02003273static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3274expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003275{
Fam Zheng277acfe2013-08-20 10:58:21 +08003276 va_list ap;
3277 va_start(ap, fmt);
3278 monitor_vprintf(mon, fmt, ap);
3279 monitor_printf(mon, "\n");
3280 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003281 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003282}
3283
Markus Armbruster09b94182010-01-20 13:07:30 +01003284/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003285static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003286{
blueswir18662d652008-10-02 18:32:44 +00003287 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003288 void *ptr;
3289
bellard9307c4c2004-04-04 12:57:25 +00003290 for(md = monitor_defs; md->name != NULL; md++) {
3291 if (compare_cmd(name, md->name)) {
3292 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003293 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003294 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003295 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003296 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003297 switch(md->type) {
3298 case MD_I32:
3299 *pval = *(int32_t *)ptr;
3300 break;
3301 case MD_TLONG:
3302 *pval = *(target_long *)ptr;
3303 break;
3304 default:
3305 *pval = 0;
3306 break;
3307 }
bellard9307c4c2004-04-04 12:57:25 +00003308 }
3309 return 0;
3310 }
3311 }
3312 return -1;
3313}
3314
3315static void next(void)
3316{
Blue Swirl660f11b2009-07-31 21:16:51 +00003317 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003318 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003319 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003320 pch++;
3321 }
3322}
3323
aliguori376253e2009-03-05 23:01:23 +00003324static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003325
aliguori376253e2009-03-05 23:01:23 +00003326static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003327{
blueswir1c2efc952007-09-25 17:28:42 +00003328 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003329 char *p;
bellard6a00d602005-11-21 23:25:50 +00003330 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003331
3332 switch(*pch) {
3333 case '+':
3334 next();
aliguori376253e2009-03-05 23:01:23 +00003335 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003336 break;
3337 case '-':
3338 next();
aliguori376253e2009-03-05 23:01:23 +00003339 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003340 break;
3341 case '~':
3342 next();
aliguori376253e2009-03-05 23:01:23 +00003343 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003344 break;
3345 case '(':
3346 next();
aliguori376253e2009-03-05 23:01:23 +00003347 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003348 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003349 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003350 }
3351 next();
3352 break;
bellard81d09122004-07-14 17:21:37 +00003353 case '\'':
3354 pch++;
3355 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003356 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003357 n = *pch;
3358 pch++;
3359 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003360 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003361 next();
3362 break;
bellard9307c4c2004-04-04 12:57:25 +00003363 case '$':
3364 {
3365 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003366 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003367
bellard9307c4c2004-04-04 12:57:25 +00003368 pch++;
3369 q = buf;
3370 while ((*pch >= 'a' && *pch <= 'z') ||
3371 (*pch >= 'A' && *pch <= 'Z') ||
3372 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003373 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003374 if ((q - buf) < sizeof(buf) - 1)
3375 *q++ = *pch;
3376 pch++;
3377 }
blueswir1cd390082008-11-16 13:53:32 +00003378 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003379 pch++;
3380 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003381 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003382 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003383 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003384 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003385 }
3386 break;
3387 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003388 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003389 n = 0;
3390 break;
3391 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003392 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003393 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003394 if (errno == ERANGE) {
3395 expr_error(mon, "number too large");
3396 }
bellard9307c4c2004-04-04 12:57:25 +00003397 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003398 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003399 }
3400 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003401 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003402 pch++;
3403 break;
3404 }
3405 return n;
3406}
3407
3408
aliguori376253e2009-03-05 23:01:23 +00003409static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003410{
blueswir1c2efc952007-09-25 17:28:42 +00003411 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003412 int op;
ths3b46e622007-09-17 08:09:54 +00003413
aliguori376253e2009-03-05 23:01:23 +00003414 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003415 for(;;) {
3416 op = *pch;
3417 if (op != '*' && op != '/' && op != '%')
3418 break;
3419 next();
aliguori376253e2009-03-05 23:01:23 +00003420 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003421 switch(op) {
3422 default:
3423 case '*':
3424 val *= val2;
3425 break;
3426 case '/':
3427 case '%':
ths5fafdf22007-09-16 21:08:06 +00003428 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003429 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003430 if (op == '/')
3431 val /= val2;
3432 else
3433 val %= val2;
3434 break;
3435 }
3436 }
3437 return val;
3438}
3439
aliguori376253e2009-03-05 23:01:23 +00003440static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003441{
blueswir1c2efc952007-09-25 17:28:42 +00003442 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003443 int op;
bellard9307c4c2004-04-04 12:57:25 +00003444
aliguori376253e2009-03-05 23:01:23 +00003445 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003446 for(;;) {
3447 op = *pch;
3448 if (op != '&' && op != '|' && op != '^')
3449 break;
3450 next();
aliguori376253e2009-03-05 23:01:23 +00003451 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003452 switch(op) {
3453 default:
3454 case '&':
3455 val &= val2;
3456 break;
3457 case '|':
3458 val |= val2;
3459 break;
3460 case '^':
3461 val ^= val2;
3462 break;
3463 }
3464 }
3465 return val;
3466}
3467
aliguori376253e2009-03-05 23:01:23 +00003468static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003469{
blueswir1c2efc952007-09-25 17:28:42 +00003470 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003471 int op;
bellard9307c4c2004-04-04 12:57:25 +00003472
aliguori376253e2009-03-05 23:01:23 +00003473 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003474 for(;;) {
3475 op = *pch;
3476 if (op != '+' && op != '-')
3477 break;
3478 next();
aliguori376253e2009-03-05 23:01:23 +00003479 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003480 if (op == '+')
3481 val += val2;
3482 else
3483 val -= val2;
3484 }
3485 return val;
3486}
3487
aliguori376253e2009-03-05 23:01:23 +00003488static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003489{
3490 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003491 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003492 *pp = pch;
3493 return -1;
3494 }
blueswir1cd390082008-11-16 13:53:32 +00003495 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003496 pch++;
aliguori376253e2009-03-05 23:01:23 +00003497 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003498 *pp = pch;
3499 return 0;
3500}
3501
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003502static int get_double(Monitor *mon, double *pval, const char **pp)
3503{
3504 const char *p = *pp;
3505 char *tailp;
3506 double d;
3507
3508 d = strtod(p, &tailp);
3509 if (tailp == p) {
3510 monitor_printf(mon, "Number expected\n");
3511 return -1;
3512 }
3513 if (d != d || d - d != 0) {
3514 /* NaN or infinity */
3515 monitor_printf(mon, "Bad number\n");
3516 return -1;
3517 }
3518 *pval = d;
3519 *pp = tailp;
3520 return 0;
3521}
3522
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003523/*
3524 * Store the command-name in cmdname, and return a pointer to
3525 * the remaining of the command string.
3526 */
3527static const char *get_command_name(const char *cmdline,
3528 char *cmdname, size_t nlen)
3529{
3530 size_t len;
3531 const char *p, *pstart;
3532
3533 p = cmdline;
3534 while (qemu_isspace(*p))
3535 p++;
3536 if (*p == '\0')
3537 return NULL;
3538 pstart = p;
3539 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3540 p++;
3541 len = p - pstart;
3542 if (len > nlen - 1)
3543 len = nlen - 1;
3544 memcpy(cmdname, pstart, len);
3545 cmdname[len] = '\0';
3546 return p;
3547}
3548
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003549/**
3550 * Read key of 'type' into 'key' and return the current
3551 * 'type' pointer.
3552 */
3553static char *key_get_info(const char *type, char **key)
3554{
3555 size_t len;
3556 char *p, *str;
3557
3558 if (*type == ',')
3559 type++;
3560
3561 p = strchr(type, ':');
3562 if (!p) {
3563 *key = NULL;
3564 return NULL;
3565 }
3566 len = p - type;
3567
Anthony Liguori7267c092011-08-20 22:09:37 -05003568 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003569 memcpy(str, type, len);
3570 str[len] = '\0';
3571
3572 *key = str;
3573 return ++p;
3574}
3575
bellard9307c4c2004-04-04 12:57:25 +00003576static int default_fmt_format = 'x';
3577static int default_fmt_size = 4;
3578
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003579static int is_valid_option(const char *c, const char *typestr)
3580{
3581 char option[3];
3582
3583 option[0] = '-';
3584 option[1] = *c;
3585 option[2] = '\0';
3586
3587 typestr = strstr(typestr, option);
3588 return (typestr != NULL);
3589}
3590
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003591static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3592 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003593{
3594 const mon_cmd_t *cmd;
3595
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003596 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003597 if (compare_cmd(cmdname, cmd->name)) {
3598 return cmd;
3599 }
3600 }
3601
3602 return NULL;
3603}
3604
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003605static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3606{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003607 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003608}
3609
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003610/*
3611 * Parse @cmdline according to command table @table.
3612 * If @cmdline is blank, return NULL.
3613 * If it can't be parsed, report to @mon, and return NULL.
3614 * Else, insert command arguments into @qdict, and return the command.
Peter Maydell085d8132013-03-18 17:20:07 +00003615 * If a sub-command table exists, and if @cmdline contains an additional string
3616 * for a sub-command, this function will try to search the sub-command table.
3617 * If no additional string for a sub-command is present, this function will
3618 * return the command found in @table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003619 * Do not assume the returned command points into @table! It doesn't
3620 * when the command is a sub-command.
3621 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003622static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003623 const char *cmdline,
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003624 int start,
3625 mon_cmd_t *table,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003626 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003627{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003628 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003629 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003630 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003631 char cmdname[256];
3632 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003633 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003634
3635#ifdef DEBUG
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003636 monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start);
bellard9dc39cb2004-03-14 21:38:27 +00003637#endif
ths3b46e622007-09-17 08:09:54 +00003638
bellard9307c4c2004-04-04 12:57:25 +00003639 /* extract the command name */
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003640 p = get_command_name(cmdline + start, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003641 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003642 return NULL;
ths3b46e622007-09-17 08:09:54 +00003643
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003644 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003645 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003646 monitor_printf(mon, "unknown command: '%.*s'\n",
3647 (int)(p - cmdline), cmdline);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003648 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003649 }
bellard9307c4c2004-04-04 12:57:25 +00003650
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003651 /* filter out following useless space */
3652 while (qemu_isspace(*p)) {
3653 p++;
3654 }
3655 /* search sub command */
3656 if (cmd->sub_table != NULL) {
3657 /* check if user set additional command */
3658 if (*p == '\0') {
3659 return cmd;
3660 }
3661 return monitor_parse_command(mon, cmdline, p - cmdline,
3662 cmd->sub_table, qdict);
3663 }
3664
bellard9307c4c2004-04-04 12:57:25 +00003665 /* parse the parameters */
3666 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003667 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003668 typestr = key_get_info(typestr, &key);
3669 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003670 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003671 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003672 typestr++;
3673 switch(c) {
3674 case 'F':
bellard81d09122004-07-14 17:21:37 +00003675 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003676 case 's':
3677 {
3678 int ret;
ths3b46e622007-09-17 08:09:54 +00003679
blueswir1cd390082008-11-16 13:53:32 +00003680 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003681 p++;
3682 if (*typestr == '?') {
3683 typestr++;
3684 if (*p == '\0') {
3685 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003686 break;
bellard9307c4c2004-04-04 12:57:25 +00003687 }
3688 }
3689 ret = get_str(buf, sizeof(buf), &p);
3690 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003691 switch(c) {
3692 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003693 monitor_printf(mon, "%s: filename expected\n",
3694 cmdname);
bellard81d09122004-07-14 17:21:37 +00003695 break;
3696 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003697 monitor_printf(mon, "%s: block device name expected\n",
3698 cmdname);
bellard81d09122004-07-14 17:21:37 +00003699 break;
3700 default:
aliguori376253e2009-03-05 23:01:23 +00003701 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003702 break;
3703 }
bellard9307c4c2004-04-04 12:57:25 +00003704 goto fail;
3705 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003706 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003707 }
3708 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003709 case 'O':
3710 {
3711 QemuOptsList *opts_list;
3712 QemuOpts *opts;
3713
3714 opts_list = qemu_find_opts(key);
3715 if (!opts_list || opts_list->desc->name) {
3716 goto bad_type;
3717 }
3718 while (qemu_isspace(*p)) {
3719 p++;
3720 }
3721 if (!*p)
3722 break;
3723 if (get_str(buf, sizeof(buf), &p) < 0) {
3724 goto fail;
3725 }
3726 opts = qemu_opts_parse(opts_list, buf, 1);
3727 if (!opts) {
3728 goto fail;
3729 }
3730 qemu_opts_to_qdict(opts, qdict);
3731 qemu_opts_del(opts);
3732 }
3733 break;
bellard9307c4c2004-04-04 12:57:25 +00003734 case '/':
3735 {
3736 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003737
blueswir1cd390082008-11-16 13:53:32 +00003738 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003739 p++;
3740 if (*p == '/') {
3741 /* format found */
3742 p++;
3743 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003744 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003745 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003746 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003747 count = count * 10 + (*p - '0');
3748 p++;
3749 }
3750 }
3751 size = -1;
3752 format = -1;
3753 for(;;) {
3754 switch(*p) {
3755 case 'o':
3756 case 'd':
3757 case 'u':
3758 case 'x':
3759 case 'i':
3760 case 'c':
3761 format = *p++;
3762 break;
3763 case 'b':
3764 size = 1;
3765 p++;
3766 break;
3767 case 'h':
3768 size = 2;
3769 p++;
3770 break;
3771 case 'w':
3772 size = 4;
3773 p++;
3774 break;
3775 case 'g':
3776 case 'L':
3777 size = 8;
3778 p++;
3779 break;
3780 default:
3781 goto next;
3782 }
3783 }
3784 next:
blueswir1cd390082008-11-16 13:53:32 +00003785 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003786 monitor_printf(mon, "invalid char in format: '%c'\n",
3787 *p);
bellard9307c4c2004-04-04 12:57:25 +00003788 goto fail;
3789 }
bellard9307c4c2004-04-04 12:57:25 +00003790 if (format < 0)
3791 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003792 if (format != 'i') {
3793 /* for 'i', not specifying a size gives -1 as size */
3794 if (size < 0)
3795 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003796 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003797 }
bellard9307c4c2004-04-04 12:57:25 +00003798 default_fmt_format = format;
3799 } else {
3800 count = 1;
3801 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003802 if (format != 'i') {
3803 size = default_fmt_size;
3804 } else {
3805 size = -1;
3806 }
bellard9307c4c2004-04-04 12:57:25 +00003807 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003808 qdict_put(qdict, "count", qint_from_int(count));
3809 qdict_put(qdict, "format", qint_from_int(format));
3810 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003811 }
3812 break;
3813 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003814 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003815 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003816 {
blueswir1c2efc952007-09-25 17:28:42 +00003817 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003818
blueswir1cd390082008-11-16 13:53:32 +00003819 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003820 p++;
bellard34405572004-06-08 00:55:58 +00003821 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003822 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003823 if (*p == '\0') {
3824 typestr++;
3825 break;
3826 }
bellard34405572004-06-08 00:55:58 +00003827 } else {
3828 if (*p == '.') {
3829 p++;
blueswir1cd390082008-11-16 13:53:32 +00003830 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003831 p++;
bellard34405572004-06-08 00:55:58 +00003832 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003833 typestr++;
3834 break;
bellard34405572004-06-08 00:55:58 +00003835 }
3836 }
bellard13224a82006-07-14 22:03:35 +00003837 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003838 }
aliguori376253e2009-03-05 23:01:23 +00003839 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003840 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003841 /* Check if 'i' is greater than 32-bit */
3842 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3843 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3844 monitor_printf(mon, "integer is for 32-bit values\n");
3845 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003846 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003847 if (val < 0) {
3848 monitor_printf(mon, "enter a positive value\n");
3849 goto fail;
3850 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003851 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003852 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003853 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003854 }
3855 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003856 case 'o':
3857 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003858 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003859 char *end;
3860
3861 while (qemu_isspace(*p)) {
3862 p++;
3863 }
3864 if (*typestr == '?') {
3865 typestr++;
3866 if (*p == '\0') {
3867 break;
3868 }
3869 }
3870 val = strtosz(p, &end);
3871 if (val < 0) {
3872 monitor_printf(mon, "invalid size\n");
3873 goto fail;
3874 }
3875 qdict_put(qdict, key, qint_from_int(val));
3876 p = end;
3877 }
3878 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003879 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003880 {
3881 double val;
3882
3883 while (qemu_isspace(*p))
3884 p++;
3885 if (*typestr == '?') {
3886 typestr++;
3887 if (*p == '\0') {
3888 break;
3889 }
3890 }
3891 if (get_double(mon, &val, &p) < 0) {
3892 goto fail;
3893 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003894 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003895 switch (*p) {
3896 case 'm':
3897 val /= 1e3; p += 2; break;
3898 case 'u':
3899 val /= 1e6; p += 2; break;
3900 case 'n':
3901 val /= 1e9; p += 2; break;
3902 }
3903 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003904 if (*p && !qemu_isspace(*p)) {
3905 monitor_printf(mon, "Unknown unit suffix\n");
3906 goto fail;
3907 }
3908 qdict_put(qdict, key, qfloat_from_double(val));
3909 }
3910 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003911 case 'b':
3912 {
3913 const char *beg;
3914 int val;
3915
3916 while (qemu_isspace(*p)) {
3917 p++;
3918 }
3919 beg = p;
3920 while (qemu_isgraph(*p)) {
3921 p++;
3922 }
3923 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3924 val = 1;
3925 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3926 val = 0;
3927 } else {
3928 monitor_printf(mon, "Expected 'on' or 'off'\n");
3929 goto fail;
3930 }
3931 qdict_put(qdict, key, qbool_from_int(val));
3932 }
3933 break;
bellard9307c4c2004-04-04 12:57:25 +00003934 case '-':
3935 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003936 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003937 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003938 /* option */
ths3b46e622007-09-17 08:09:54 +00003939
bellard9307c4c2004-04-04 12:57:25 +00003940 c = *typestr++;
3941 if (c == '\0')
3942 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003943 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003944 p++;
bellard9307c4c2004-04-04 12:57:25 +00003945 if (*p == '-') {
3946 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003947 if(c != *p) {
3948 if(!is_valid_option(p, typestr)) {
3949
3950 monitor_printf(mon, "%s: unsupported option -%c\n",
3951 cmdname, *p);
3952 goto fail;
3953 } else {
3954 skip_key = 1;
3955 }
bellard9307c4c2004-04-04 12:57:25 +00003956 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003957 if(skip_key) {
3958 p = tmp;
3959 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003960 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003961 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003962 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003963 }
bellard9307c4c2004-04-04 12:57:25 +00003964 }
bellard9307c4c2004-04-04 12:57:25 +00003965 }
3966 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003967 case 'S':
3968 {
3969 /* package all remaining string */
3970 int len;
3971
3972 while (qemu_isspace(*p)) {
3973 p++;
3974 }
3975 if (*typestr == '?') {
3976 typestr++;
3977 if (*p == '\0') {
3978 /* no remaining string: NULL argument */
3979 break;
3980 }
3981 }
3982 len = strlen(p);
3983 if (len <= 0) {
3984 monitor_printf(mon, "%s: string expected\n",
3985 cmdname);
3986 break;
3987 }
3988 qdict_put(qdict, key, qstring_from_str(p));
3989 p += len;
3990 }
3991 break;
bellard9307c4c2004-04-04 12:57:25 +00003992 default:
3993 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003994 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003995 goto fail;
3996 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003997 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003998 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003999 }
4000 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004001 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004002 p++;
4003 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004004 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4005 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004006 goto fail;
4007 }
4008
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004009 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004010
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004011fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004012 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004013 return NULL;
4014}
4015
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004016void monitor_set_error(Monitor *mon, QError *qerror)
4017{
4018 /* report only the first error */
4019 if (!mon->error) {
4020 mon->error = qerror;
4021 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004022 QDECREF(qerror);
4023 }
4024}
4025
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004026static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004027{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004028 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004029 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004030
4031 qdict = qdict_new();
4032
Wenchao Xia77172392013-08-27 20:38:20 +08004033 cmd = monitor_parse_command(mon, cmdline, 0, mon->cmd_table, qdict);
Markus Armbruster8a4f5012015-03-05 18:50:05 +01004034 if (cmd) {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004035 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004036 }
4037
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004038 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004039}
4040
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004041static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004042{
4043 const char *p, *pstart;
4044 char cmd[128];
4045 int len;
4046
4047 p = list;
4048 for(;;) {
4049 pstart = p;
4050 p = strchr(p, '|');
4051 if (!p)
4052 p = pstart + strlen(pstart);
4053 len = p - pstart;
4054 if (len > sizeof(cmd) - 2)
4055 len = sizeof(cmd) - 2;
4056 memcpy(cmd, pstart, len);
4057 cmd[len] = '\0';
4058 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004059 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004060 }
4061 if (*p == '\0')
4062 break;
4063 p++;
4064 }
4065}
4066
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004067static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004068{
4069 DIR *ffs;
4070 struct dirent *d;
4071 char path[1024];
4072 char file[1024], file_prefix[1024];
4073 int input_path_len;
4074 const char *p;
4075
ths5fafdf22007-09-16 21:08:06 +00004076 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004077 if (!p) {
4078 input_path_len = 0;
4079 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004080 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004081 } else {
4082 input_path_len = p - input + 1;
4083 memcpy(path, input, input_path_len);
4084 if (input_path_len > sizeof(path) - 1)
4085 input_path_len = sizeof(path) - 1;
4086 path[input_path_len] = '\0';
4087 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4088 }
4089#ifdef DEBUG_COMPLETION
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004090 monitor_printf(mon, "input='%s' path='%s' prefix='%s'\n",
aliguori376253e2009-03-05 23:01:23 +00004091 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004092#endif
4093 ffs = opendir(path);
4094 if (!ffs)
4095 return;
4096 for(;;) {
4097 struct stat sb;
4098 d = readdir(ffs);
4099 if (!d)
4100 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004101
4102 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4103 continue;
4104 }
4105
bellard81d09122004-07-14 17:21:37 +00004106 if (strstart(d->d_name, file_prefix, NULL)) {
4107 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004108 if (input_path_len < sizeof(file))
4109 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4110 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004111 /* stat the file to find out if it's a directory.
4112 * In that case add a slash to speed up typing long paths
4113 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004114 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004115 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004116 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004117 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004118 }
4119 }
4120 closedir(ffs);
4121}
4122
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004123static const char *next_arg_type(const char *typestr)
4124{
4125 const char *p = strchr(typestr, ':');
4126 return (p != NULL ? ++p : typestr);
4127}
4128
Hani Benhabiles40d19392014-05-07 23:41:30 +01004129static void add_completion_option(ReadLineState *rs, const char *str,
4130 const char *option)
4131{
4132 if (!str || !option) {
4133 return;
4134 }
4135 if (!strncmp(option, str, strlen(str))) {
4136 readline_add_completion(rs, option);
4137 }
4138}
4139
Hani Benhabiles13e315d2014-05-07 23:41:29 +01004140void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4141{
4142 size_t len;
4143 ChardevBackendInfoList *list, *start;
4144
4145 if (nb_args != 2) {
4146 return;
4147 }
4148 len = strlen(str);
4149 readline_set_completion_index(rs, len);
4150
4151 start = list = qmp_query_chardev_backends(NULL);
4152 while (list) {
4153 const char *chr_name = list->value->name;
4154
4155 if (!strncmp(chr_name, str, len)) {
4156 readline_add_completion(rs, chr_name);
4157 }
4158 list = list->next;
4159 }
4160 qapi_free_ChardevBackendInfoList(start);
4161}
4162
Hani Benhabilesb162b492014-05-07 23:41:31 +01004163void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4164{
4165 size_t len;
4166 int i;
4167
4168 if (nb_args != 2) {
4169 return;
4170 }
4171 len = strlen(str);
4172 readline_set_completion_index(rs, len);
4173 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
4174 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
4175 }
4176}
4177
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004178void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004179{
4180 GSList *list, *elt;
4181 size_t len;
4182
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004183 if (nb_args != 2) {
4184 return;
4185 }
4186
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004187 len = strlen(str);
4188 readline_set_completion_index(rs, len);
4189 list = elt = object_class_get_list(TYPE_DEVICE, false);
4190 while (elt) {
4191 const char *name;
4192 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
4193 TYPE_DEVICE);
4194 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004195
4196 if (!dc->cannot_instantiate_with_device_add_yet
4197 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004198 readline_add_completion(rs, name);
4199 }
4200 elt = elt->next;
4201 }
4202 g_slist_free(list);
4203}
4204
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004205void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004206{
4207 GSList *list, *elt;
4208 size_t len;
4209
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004210 if (nb_args != 2) {
4211 return;
4212 }
4213
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004214 len = strlen(str);
4215 readline_set_completion_index(rs, len);
4216 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
4217 while (elt) {
4218 const char *name;
4219
4220 name = object_class_get_name(OBJECT_CLASS(elt->data));
4221 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
4222 readline_add_completion(rs, name);
4223 }
4224 elt = elt->next;
4225 }
4226 g_slist_free(list);
4227}
4228
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004229static void peripheral_device_del_completion(ReadLineState *rs,
4230 const char *str, size_t len)
4231{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004232 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
4233 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004234
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004235 list = qdev_build_hotpluggable_device_list(peripheral);
4236 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004237 return;
4238 }
4239
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004240 for (item = list; item; item = g_slist_next(item)) {
4241 DeviceState *dev = item->data;
4242
4243 if (dev->id && !strncmp(str, dev->id, len)) {
4244 readline_add_completion(rs, dev->id);
4245 }
4246 }
4247
4248 g_slist_free(list);
4249}
4250
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01004251void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4252{
4253 size_t len;
4254 ChardevInfoList *list, *start;
4255
4256 if (nb_args != 2) {
4257 return;
4258 }
4259 len = strlen(str);
4260 readline_set_completion_index(rs, len);
4261
4262 start = list = qmp_query_chardev(NULL);
4263 while (list) {
4264 ChardevInfo *chr = list->value;
4265
4266 if (!strncmp(chr->label, str, len)) {
4267 readline_add_completion(rs, chr->label);
4268 }
4269 list = list->next;
4270 }
4271 qapi_free_ChardevInfoList(start);
4272}
4273
Hani Benhabiles8e597772014-05-27 23:39:30 +01004274static void ringbuf_completion(ReadLineState *rs, const char *str)
4275{
4276 size_t len;
4277 ChardevInfoList *list, *start;
4278
4279 len = strlen(str);
4280 readline_set_completion_index(rs, len);
4281
4282 start = list = qmp_query_chardev(NULL);
4283 while (list) {
4284 ChardevInfo *chr_info = list->value;
4285
4286 if (!strncmp(chr_info->label, str, len)) {
4287 CharDriverState *chr = qemu_chr_find(chr_info->label);
4288 if (chr && chr_is_ringbuf(chr)) {
4289 readline_add_completion(rs, chr_info->label);
4290 }
4291 }
4292 list = list->next;
4293 }
4294 qapi_free_ChardevInfoList(start);
4295}
4296
Hani Benhabiles8e597772014-05-27 23:39:30 +01004297void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
4298{
4299 if (nb_args != 2) {
4300 return;
4301 }
4302 ringbuf_completion(rs, str);
4303}
4304
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004305void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
4306{
4307 size_t len;
4308
4309 if (nb_args != 2) {
4310 return;
4311 }
4312
4313 len = strlen(str);
4314 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004315 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004316}
4317
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004318void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004319{
4320 ObjectPropertyInfoList *list, *start;
4321 size_t len;
4322
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004323 if (nb_args != 2) {
4324 return;
4325 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004326 len = strlen(str);
4327 readline_set_completion_index(rs, len);
4328
4329 start = list = qmp_qom_list("/objects", NULL);
4330 while (list) {
4331 ObjectPropertyInfo *info = list->value;
4332
4333 if (!strncmp(info->type, "child<", 5)
4334 && !strncmp(info->name, str, len)) {
4335 readline_add_completion(rs, info->name);
4336 }
4337 list = list->next;
4338 }
4339 qapi_free_ObjectPropertyInfoList(start);
4340}
4341
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004342void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
4343{
4344 int i;
4345 char *sep;
4346 size_t len;
4347
4348 if (nb_args != 2) {
4349 return;
4350 }
4351 sep = strrchr(str, '-');
4352 if (sep) {
4353 str = sep + 1;
4354 }
4355 len = strlen(str);
4356 readline_set_completion_index(rs, len);
4357 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4358 if (!strncmp(str, QKeyCode_lookup[i], len)) {
4359 readline_add_completion(rs, QKeyCode_lookup[i]);
4360 }
4361 }
4362}
4363
Hani Benhabiles40d19392014-05-07 23:41:30 +01004364void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
4365{
4366 size_t len;
4367
4368 len = strlen(str);
4369 readline_set_completion_index(rs, len);
4370 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08004371 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01004372 int count, i;
4373 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004374 NET_CLIENT_OPTIONS_KIND_NONE,
4375 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004376 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01004377 const char *name = ncs[i]->name;
4378 if (!strncmp(str, name, len)) {
4379 readline_add_completion(rs, name);
4380 }
4381 }
4382 } else if (nb_args == 3) {
4383 add_completion_option(rs, str, "on");
4384 add_completion_option(rs, str, "off");
4385 }
4386}
4387
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004388void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
4389{
4390 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08004391 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004392
4393 if (nb_args != 2) {
4394 return;
4395 }
4396
4397 len = strlen(str);
4398 readline_set_completion_index(rs, len);
4399 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08004400 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004401 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004402 QemuOpts *opts;
4403 const char *name = ncs[i]->name;
4404 if (strncmp(str, name, len)) {
4405 continue;
4406 }
4407 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
4408 if (opts) {
4409 readline_add_completion(rs, name);
4410 }
4411 }
4412}
4413
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004414void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
4415{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004416 int i;
4417
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004418 if (nb_args != 2) {
4419 return;
4420 }
4421 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004422 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
4423 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
4424 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004425}
4426
Hani Benhabilesc68a0402014-05-27 23:39:32 +01004427void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
4428 const char *str)
4429{
4430 size_t len;
4431
4432 len = strlen(str);
4433 readline_set_completion_index(rs, len);
4434 if (nb_args == 2) {
4435 int i;
4436 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
4437 const char *name = MigrationCapability_lookup[i];
4438 if (!strncmp(str, name, len)) {
4439 readline_add_completion(rs, name);
4440 }
4441 }
4442 } else if (nb_args == 3) {
4443 add_completion_option(rs, str, "on");
4444 add_completion_option(rs, str, "off");
4445 }
4446}
4447
Liang Li50e9a622015-03-23 16:32:29 +08004448void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
4449 const char *str)
4450{
4451 size_t len;
4452
4453 len = strlen(str);
4454 readline_set_completion_index(rs, len);
4455 if (nb_args == 2) {
4456 int i;
4457 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
4458 const char *name = MigrationParameter_lookup[i];
4459 if (!strncmp(str, name, len)) {
4460 readline_add_completion(rs, name);
4461 }
4462 }
4463 }
4464}
4465
Hani Benhabilese3bb5322014-05-27 23:39:34 +01004466void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
4467{
4468 int i;
4469 size_t len;
4470 if (nb_args != 2) {
4471 return;
4472 }
4473 len = strlen(str);
4474 readline_set_completion_index(rs, len);
4475 for (i = 0; host_net_devices[i]; i++) {
4476 if (!strncmp(host_net_devices[i], str, len)) {
4477 readline_add_completion(rs, host_net_devices[i]);
4478 }
4479 }
4480}
4481
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004482void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4483{
Jason Wangeaed4832015-04-23 14:21:38 +08004484 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004485 int count, i, len;
4486
4487 len = strlen(str);
4488 readline_set_completion_index(rs, len);
4489 if (nb_args == 2) {
4490 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004491 NET_CLIENT_OPTIONS_KIND_NONE,
4492 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004493 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004494 int id;
4495 char name[16];
4496
4497 if (net_hub_id_for_client(ncs[i], &id)) {
4498 continue;
4499 }
4500 snprintf(name, sizeof(name), "%d", id);
4501 if (!strncmp(str, name, len)) {
4502 readline_add_completion(rs, name);
4503 }
4504 }
4505 return;
4506 } else if (nb_args == 3) {
4507 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004508 NET_CLIENT_OPTIONS_KIND_NIC,
4509 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004510 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08004511 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004512 const char *name;
4513
Jason Wang2c4681f2015-02-02 15:06:38 +08004514 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
4515 net_hub_id_for_client(ncs[i], &id)) {
4516 continue;
4517 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004518 name = ncs[i]->name;
4519 if (!strncmp(str, name, len)) {
4520 readline_add_completion(rs, name);
4521 }
4522 }
4523 return;
4524 }
4525}
4526
Hani Benhabilesb21631f2014-05-27 23:39:37 +01004527static void vm_completion(ReadLineState *rs, const char *str)
4528{
4529 size_t len;
4530 BlockDriverState *bs = NULL;
4531
4532 len = strlen(str);
4533 readline_set_completion_index(rs, len);
4534 while ((bs = bdrv_next(bs))) {
4535 SnapshotInfoList *snapshots, *snapshot;
4536
4537 if (!bdrv_can_snapshot(bs)) {
4538 continue;
4539 }
4540 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
4541 continue;
4542 }
4543 snapshot = snapshots;
4544 while (snapshot) {
4545 char *completion = snapshot->value->name;
4546 if (!strncmp(str, completion, len)) {
4547 readline_add_completion(rs, completion);
4548 }
4549 completion = snapshot->value->id;
4550 if (!strncmp(str, completion, len)) {
4551 readline_add_completion(rs, completion);
4552 }
4553 snapshot = snapshot->next;
4554 }
4555 qapi_free_SnapshotInfoList(snapshots);
4556 }
4557
4558}
4559
4560void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
4561{
4562 if (nb_args == 2) {
4563 vm_completion(rs, str);
4564 }
4565}
4566
4567void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
4568{
4569 if (nb_args == 2) {
4570 vm_completion(rs, str);
4571 }
4572}
4573
Wenchao Xiac35b6402013-08-27 20:38:24 +08004574static void monitor_find_completion_by_table(Monitor *mon,
4575 const mon_cmd_t *cmd_table,
4576 char **args,
4577 int nb_args)
bellard81d09122004-07-14 17:21:37 +00004578{
4579 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004580 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004581 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05004582 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004583 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00004584
bellard81d09122004-07-14 17:21:37 +00004585 if (nb_args <= 1) {
4586 /* command completion */
4587 if (nb_args == 0)
4588 cmdname = "";
4589 else
4590 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004591 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004592 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004593 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00004594 }
4595 } else {
4596 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004597 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004598 if (compare_cmd(args[0], cmd->name)) {
4599 break;
4600 }
bellard81d09122004-07-14 17:21:37 +00004601 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004602 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004603 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004604 }
4605
Wenchao Xiad903a772013-08-27 20:38:25 +08004606 if (cmd->sub_table) {
4607 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01004608 monitor_find_completion_by_table(mon, cmd->sub_table,
4609 &args[1], nb_args - 1);
4610 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08004611 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004612 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01004613 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4614 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004615 }
Wenchao Xiad903a772013-08-27 20:38:25 +08004616
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004617 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004618 for(i = 0; i < nb_args - 2; i++) {
4619 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004620 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004621 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004622 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004623 }
4624 }
4625 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01004626 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004627 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004628 }
bellard81d09122004-07-14 17:21:37 +00004629 switch(*ptype) {
4630 case 'F':
4631 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004632 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004633 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004634 break;
4635 case 'B':
4636 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004637 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004638 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
4639 name = bdrv_get_device_name(bs);
4640 if (str[0] == '\0' ||
4641 !strncmp(name, str, strlen(str))) {
4642 readline_add_completion(mon->rs, name);
4643 }
4644 }
bellard81d09122004-07-14 17:21:37 +00004645 break;
bellard7fe48482004-10-09 18:08:01 +00004646 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004647 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004648 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004649 monitor_find_completion_by_table(mon, cmd_table,
4650 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004651 }
4652 break;
bellard81d09122004-07-14 17:21:37 +00004653 default:
4654 break;
4655 }
4656 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004657}
4658
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004659static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004660 const char *cmdline)
4661{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004662 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004663 char *args[MAX_ARGS];
4664 int nb_args, len;
4665
4666 /* 1. parse the cmdline */
4667 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4668 return;
4669 }
4670#ifdef DEBUG_COMPLETION
Gonglei5fb9b5b2014-08-21 21:03:09 +08004671 {
4672 int i;
4673 for (i = 0; i < nb_args; i++) {
4674 monitor_printf(mon, "arg%d = '%s'\n", i, args[i]);
4675 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004676 }
4677#endif
4678
4679 /* if the line ends with a space, it means we want to complete the
4680 next arg */
4681 len = strlen(cmdline);
4682 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4683 if (nb_args >= MAX_ARGS) {
4684 goto cleanup;
4685 }
4686 args[nb_args++] = g_strdup("");
4687 }
4688
4689 /* 2. auto complete according to args */
4690 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004691
4692cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004693 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004694}
4695
aliguori731b0362009-03-05 23:01:42 +00004696static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004697{
aliguori731b0362009-03-05 23:01:42 +00004698 Monitor *mon = opaque;
4699
Jan Kiszkac62313b2009-12-04 14:05:29 +01004700 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004701}
4702
Markus Armbruster40861822015-05-29 10:27:16 +02004703static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
4704 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02004705{
Eric Blake2d5a8342015-04-15 09:19:23 -06004706 bool is_cap = cmd->mhandler.cmd_new == do_qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01004707
4708 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02004709 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4710 "Capabilities negotiation is already complete, command "
4711 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004712 return true;
4713 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01004714 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02004715 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4716 "Expecting capabilities negotiation with "
4717 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004718 return true;
4719 }
4720 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004721}
4722
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004723/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004724 * Argument validation rules:
4725 *
4726 * 1. The argument must exist in cmd_args qdict
4727 * 2. The argument type must be the expected one
4728 *
4729 * Special case: If the argument doesn't exist in cmd_args and
4730 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4731 * checking is skipped for it.
4732 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004733static void check_client_args_type(const QDict *client_args,
4734 const QDict *cmd_args, int flags,
4735 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03004736{
4737 const QDictEntry *ent;
4738
4739 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4740 QObject *obj;
4741 QString *arg_type;
4742 const QObject *client_arg = qdict_entry_value(ent);
4743 const char *client_arg_name = qdict_entry_key(ent);
4744
4745 obj = qdict_get(cmd_args, client_arg_name);
4746 if (!obj) {
4747 if (flags & QMP_ACCEPT_UNKNOWNS) {
4748 /* handler accepts unknowns */
4749 continue;
4750 }
4751 /* client arg doesn't exist */
Markus Armbruster326283a2015-03-02 18:39:09 +01004752 error_set(errp, QERR_INVALID_PARAMETER, client_arg_name);
4753 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004754 }
4755
4756 arg_type = qobject_to_qstring(obj);
4757 assert(arg_type != NULL);
4758
4759 /* check if argument's type is correct */
4760 switch (qstring_get_str(arg_type)[0]) {
4761 case 'F':
4762 case 'B':
4763 case 's':
4764 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004765 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4766 client_arg_name, "string");
4767 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004768 }
4769 break;
4770 case 'i':
4771 case 'l':
4772 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004773 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004774 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004775 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4776 client_arg_name, "int");
4777 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004778 }
4779 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004780 case 'T':
4781 if (qobject_type(client_arg) != QTYPE_QINT &&
4782 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004783 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4784 client_arg_name, "number");
4785 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004786 }
4787 break;
4788 case 'b':
4789 case '-':
4790 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004791 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4792 client_arg_name, "bool");
4793 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004794 }
4795 break;
4796 case 'O':
4797 assert(flags & QMP_ACCEPT_UNKNOWNS);
4798 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004799 case 'q':
4800 /* Any QObject can be passed. */
4801 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004802 case '/':
4803 case '.':
4804 /*
4805 * These types are not supported by QMP and thus are not
4806 * handled here. Fall through.
4807 */
4808 default:
4809 abort();
4810 }
4811 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03004812}
4813
4814/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004815 * - Check if the client has passed all mandatory args
4816 * - Set special flags for argument validation
4817 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004818static void check_mandatory_args(const QDict *cmd_args,
4819 const QDict *client_args, int *flags,
4820 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004821{
4822 const QDictEntry *ent;
4823
4824 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4825 const char *cmd_arg_name = qdict_entry_key(ent);
4826 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4827 assert(type != NULL);
4828
4829 if (qstring_get_str(type)[0] == 'O') {
4830 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4831 *flags |= QMP_ACCEPT_UNKNOWNS;
4832 } else if (qstring_get_str(type)[0] != '-' &&
4833 qstring_get_str(type)[1] != '?' &&
4834 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004835 error_set(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
4836 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004837 }
4838 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004839}
4840
4841static QDict *qdict_from_args_type(const char *args_type)
4842{
4843 int i;
4844 QDict *qdict;
4845 QString *key, *type, *cur_qs;
4846
4847 assert(args_type != NULL);
4848
4849 qdict = qdict_new();
4850
4851 if (args_type == NULL || args_type[0] == '\0') {
4852 /* no args, empty qdict */
4853 goto out;
4854 }
4855
4856 key = qstring_new();
4857 type = qstring_new();
4858
4859 cur_qs = key;
4860
4861 for (i = 0;; i++) {
4862 switch (args_type[i]) {
4863 case ',':
4864 case '\0':
4865 qdict_put(qdict, qstring_get_str(key), type);
4866 QDECREF(key);
4867 if (args_type[i] == '\0') {
4868 goto out;
4869 }
4870 type = qstring_new(); /* qdict has ref */
4871 cur_qs = key = qstring_new();
4872 break;
4873 case ':':
4874 cur_qs = type;
4875 break;
4876 default:
4877 qstring_append_chr(cur_qs, args_type[i]);
4878 break;
4879 }
4880 }
4881
4882out:
4883 return qdict;
4884}
4885
4886/*
4887 * Client argument checking rules:
4888 *
4889 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004890 * 2. Each argument provided by the client must be expected
4891 * 3. Each argument provided by the client must have the type expected
4892 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004893 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004894static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
4895 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004896{
Markus Armbruster326283a2015-03-02 18:39:09 +01004897 Error *err = NULL;
4898 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004899 QDict *cmd_args;
4900
4901 cmd_args = qdict_from_args_type(cmd->args_type);
4902
4903 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01004904 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004905 if (err) {
4906 goto out;
4907 }
4908
Markus Armbruster326283a2015-03-02 18:39:09 +01004909 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004910
4911out:
Markus Armbruster326283a2015-03-02 18:39:09 +01004912 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004913 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004914}
4915
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004916/*
4917 * Input object checking rules
4918 *
4919 * 1. Input object must be a dict
4920 * 2. The "execute" key must exist
4921 * 3. The "execute" key must be a string
4922 * 4. If the "arguments" key exists, it must be a dict
4923 * 5. If the "id" key exists, it can be anything (ie. json-value)
4924 * 6. Any argument not listed above is considered invalid
4925 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004926static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004927{
4928 const QDictEntry *ent;
4929 int has_exec_key = 0;
4930 QDict *input_dict;
4931
4932 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004933 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004934 return NULL;
4935 }
4936
4937 input_dict = qobject_to_qdict(input_obj);
4938
4939 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4940 const char *arg_name = qdict_entry_key(ent);
4941 const QObject *arg_obj = qdict_entry_value(ent);
4942
4943 if (!strcmp(arg_name, "execute")) {
4944 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004945 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4946 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004947 return NULL;
4948 }
4949 has_exec_key = 1;
4950 } else if (!strcmp(arg_name, "arguments")) {
4951 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004952 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4953 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004954 return NULL;
4955 }
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004956 } else {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004957 error_set(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004958 return NULL;
4959 }
4960 }
4961
4962 if (!has_exec_key) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004963 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004964 return NULL;
4965 }
4966
4967 return input_dict;
4968}
4969
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004970static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4971{
Markus Armbruster326283a2015-03-02 18:39:09 +01004972 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01004973 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004974 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004975 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004976 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004977 Monitor *mon = cur_mon;
4978
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004979 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01004980 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004981
4982 obj = json_parser_parse(tokens, NULL);
4983 if (!obj) {
4984 // FIXME: should be triggered in json_parser_parse()
Markus Armbruster710aec92015-03-06 11:28:00 +01004985 error_set(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004986 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004987 }
4988
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004989 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004990 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004991 qobject_decref(obj);
4992 goto err_out;
4993 }
4994
Markus Armbruster74358f22015-03-06 19:35:59 +01004995 mon->qmp.id = qdict_get(input, "id");
4996 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004997
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004998 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004999 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05005000 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06005001 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01005002 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
5003 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005004 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005005 }
Markus Armbruster40861822015-05-29 10:27:16 +02005006 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06005007 goto err_out;
5008 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005009
5010 obj = qdict_get(input, "arguments");
5011 if (!obj) {
5012 args = qdict_new();
5013 } else {
5014 args = qobject_to_qdict(obj);
5015 QINCREF(args);
5016 }
5017
Markus Armbruster326283a2015-03-02 18:39:09 +01005018 qmp_check_client_args(cmd, args, &local_err);
5019 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005020 goto err_out;
5021 }
5022
Markus Armbruster84add862015-03-05 16:45:15 +01005023 if (cmd->mhandler.cmd_new(mon, args, &data)) {
5024 /* Command failed... */
Markus Armbruster452e0302015-03-06 19:11:13 +01005025 if (!mon->error) {
Markus Armbruster84add862015-03-05 16:45:15 +01005026 /* ... without setting an error, so make one up */
Markus Armbruster710aec92015-03-06 11:28:00 +01005027 error_set(&local_err, QERR_UNDEFINED_ERROR);
Markus Armbruster84add862015-03-05 16:45:15 +01005028 }
5029 }
Markus Armbruster710aec92015-03-06 11:28:00 +01005030 if (mon->error) {
5031 error_set(&local_err, mon->error->err_class, "%s",
5032 mon->error->err_msg);
5033 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005034
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005035err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01005036 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01005037 qobject_decref(data);
Markus Armbruster70ea0c52015-03-06 10:47:08 +01005038 QDECREF(mon->error);
5039 mon->error = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005040 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005041 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005042}
5043
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005044static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005045{
5046 Monitor *old_mon = cur_mon;
5047
5048 cur_mon = opaque;
5049
Markus Armbruster74358f22015-03-06 19:35:59 +01005050 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005051
5052 cur_mon = old_mon;
5053}
5054
aliguori731b0362009-03-05 23:01:42 +00005055static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005056{
aliguori731b0362009-03-05 23:01:42 +00005057 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005058 int i;
aliguori376253e2009-03-05 23:01:23 +00005059
aliguori731b0362009-03-05 23:01:42 +00005060 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005061
aliguoricde76ee2009-03-05 23:01:51 +00005062 if (cur_mon->rs) {
5063 for (i = 0; i < size; i++)
5064 readline_handle_byte(cur_mon->rs, buf[i]);
5065 } else {
5066 if (size == 0 || buf[size - 1] != 0)
5067 monitor_printf(cur_mon, "corrupted command\n");
5068 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01005069 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005070 }
aliguori731b0362009-03-05 23:01:42 +00005071
5072 cur_mon = old_mon;
5073}
aliguorid8f44602008-10-06 13:52:44 +00005074
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005075static void monitor_command_cb(void *opaque, const char *cmdline,
5076 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005077{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005078 Monitor *mon = opaque;
5079
aliguori731b0362009-03-05 23:01:42 +00005080 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01005081 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005082 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005083}
5084
aliguoricde76ee2009-03-05 23:01:51 +00005085int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005086{
aliguoricde76ee2009-03-05 23:01:51 +00005087 if (!mon->rs)
5088 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005089 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005090 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005091}
5092
aliguori376253e2009-03-05 23:01:23 +00005093void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005094{
aliguoricde76ee2009-03-05 23:01:51 +00005095 if (!mon->rs)
5096 return;
aliguori731b0362009-03-05 23:01:42 +00005097 if (--mon->suspend_cnt == 0)
5098 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005099}
5100
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005101static QObject *get_qmp_greeting(void)
5102{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005103 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005104
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005105 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005106 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5107}
5108
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005109static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005110{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005111 QObject *data;
5112 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005113
Luiz Capitulino47116d12010-02-08 17:01:30 -02005114 switch (event) {
5115 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01005116 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005117 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005118 monitor_json_emitter(mon, data);
5119 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04005120 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02005121 break;
5122 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01005123 json_message_parser_destroy(&mon->qmp.parser);
5124 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04005125 mon_refcount--;
5126 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02005127 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005128 }
5129}
5130
aliguori731b0362009-03-05 23:01:42 +00005131static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005132{
aliguori376253e2009-03-05 23:01:23 +00005133 Monitor *mon = opaque;
5134
aliguori2724b182009-03-05 23:01:47 +00005135 switch (event) {
5136 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005137 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005138 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005139 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005140 if (mon->reset_seen) {
5141 readline_restart(mon->rs);
5142 monitor_resume(mon);
5143 monitor_flush(mon);
5144 } else {
5145 mon->suspend_cnt = 0;
5146 }
aliguori2724b182009-03-05 23:01:47 +00005147 break;
ths86e94de2007-01-05 22:01:59 +00005148
aliguori2724b182009-03-05 23:01:47 +00005149 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005150 if (mon->reset_seen) {
5151 if (mon->suspend_cnt == 0) {
5152 monitor_printf(mon, "\n");
5153 }
5154 monitor_flush(mon);
5155 monitor_suspend(mon);
5156 } else {
5157 mon->suspend_cnt++;
5158 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005159 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005160 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005161 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00005162 break;
5163
Amit Shahb6b8df52009-10-07 18:31:16 +05305164 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005165 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5166 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005167 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03005168 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00005169 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005170 }
5171 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04005172 mon_refcount++;
5173 break;
5174
5175 case CHR_EVENT_CLOSED:
5176 mon_refcount--;
5177 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00005178 break;
5179 }
ths86e94de2007-01-05 22:01:59 +00005180}
5181
Wayne Xia816f8922011-10-12 11:32:41 +08005182static int
5183compare_mon_cmd(const void *a, const void *b)
5184{
5185 return strcmp(((const mon_cmd_t *)a)->name,
5186 ((const mon_cmd_t *)b)->name);
5187}
5188
5189static void sortcmdlist(void)
5190{
5191 int array_num;
5192 int elem_size = sizeof(mon_cmd_t);
5193
5194 array_num = sizeof(mon_cmds)/elem_size-1;
5195 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
5196
5197 array_num = sizeof(info_cmds)/elem_size-1;
5198 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
5199}
5200
aliguori76655d62009-03-06 20:27:37 +00005201
5202/*
5203 * Local variables:
5204 * c-indent-level: 4
5205 * c-basic-offset: 4
5206 * tab-width: 8
5207 * End:
5208 */
5209
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005210/* These functions just adapt the readline interface in a typesafe way. We
5211 * could cast function pointers but that discards compiler checks.
5212 */
Stefan Weild5d15072014-01-25 18:18:23 +01005213static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
5214 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005215{
5216 va_list ap;
5217 va_start(ap, fmt);
5218 monitor_vprintf(opaque, fmt, ap);
5219 va_end(ap);
5220}
5221
5222static void monitor_readline_flush(void *opaque)
5223{
5224 monitor_flush(opaque);
5225}
5226
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005227static void __attribute__((constructor)) monitor_lock_init(void)
5228{
5229 qemu_mutex_init(&monitor_lock);
5230}
5231
aliguori731b0362009-03-05 23:01:42 +00005232void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005233{
aliguori731b0362009-03-05 23:01:42 +00005234 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005235 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005236
5237 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02005238 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08005239 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00005240 is_first_init = 0;
5241 }
aliguori87127162009-03-05 23:01:29 +00005242
Wenchao Xiab01fe892013-08-27 20:38:19 +08005243 mon = g_malloc(sizeof(*mon));
5244 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00005245
aliguori87127162009-03-05 23:01:29 +00005246 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005247 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005248 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005249 mon->rs = readline_init(monitor_readline_printf,
5250 monitor_readline_flush,
5251 mon,
5252 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00005253 monitor_read_command(mon, 0);
5254 }
aliguori87127162009-03-05 23:01:29 +00005255
Markus Armbruster9f3982f2015-03-06 19:56:38 +01005256 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005257 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
5258 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005259 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01005260 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005261 } else {
5262 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5263 monitor_event, mon);
5264 }
aliguori87127162009-03-05 23:01:29 +00005265
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005266 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00005267 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005268 qemu_mutex_unlock(&monitor_lock);
5269
Markus Armbruster8631b602010-02-18 11:41:55 +01005270 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5271 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005272}
5273
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005274static void bdrv_password_cb(void *opaque, const char *password,
5275 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005276{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005277 Monitor *mon = opaque;
5278 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005279 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005280 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00005281
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005282 bdrv_add_key(bs, password, &local_err);
5283 if (local_err) {
5284 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
5285 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00005286 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005287 }
aliguori731b0362009-03-05 23:01:42 +00005288 if (mon->password_completion_cb)
5289 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005290
aliguori731b0362009-03-05 23:01:42 +00005291 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005292}
aliguoric0f4ce72009-03-05 23:01:01 +00005293
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005294int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02005295 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005296 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005297{
aliguoricde76ee2009-03-05 23:01:51 +00005298 int err;
5299
aliguori376253e2009-03-05 23:01:23 +00005300 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5301 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005302
aliguori731b0362009-03-05 23:01:42 +00005303 mon->password_completion_cb = completion_cb;
5304 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005305
aliguoricde76ee2009-03-05 23:01:51 +00005306 err = monitor_read_password(mon, bdrv_password_cb, bs);
5307
5308 if (err && completion_cb)
5309 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005310
5311 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005312}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005313
5314int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02005315 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005316 void *opaque)
5317{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005318 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08005319 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005320
Fam Zheng55606252015-03-02 19:36:46 +08005321 blk = blk_by_name(device);
5322 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005323 monitor_printf(mon, "Device not found %s\n", device);
5324 return -1;
5325 }
5326
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005327 bdrv_add_key(blk_bs(blk), NULL, &err);
5328 if (err) {
5329 error_free(err);
5330 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
5331 }
5332
5333 if (completion_cb) {
5334 completion_cb(opaque, 0);
5335 }
5336 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005337}
Paolo Bonzini4d454572012-11-26 16:03:42 +01005338
5339QemuOptsList qemu_mon_opts = {
5340 .name = "mon",
5341 .implied_opt_name = "chardev",
5342 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5343 .desc = {
5344 {
5345 .name = "mode",
5346 .type = QEMU_OPT_STRING,
5347 },{
5348 .name = "chardev",
5349 .type = QEMU_OPT_STRING,
5350 },{
5351 .name = "default",
5352 .type = QEMU_OPT_BOOL,
5353 },{
5354 .name = "pretty",
5355 .type = QEMU_OPT_BOOL,
5356 },
5357 { /* end of list */ }
5358 },
5359};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03005360
5361#ifndef TARGET_I386
5362void qmp_rtc_reset_reinjection(Error **errp)
5363{
5364 error_set(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
5365}
5366#endif