blob: 5c9eec109fea244283a11c07a82644f9cc76cc0c [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"
28#include "hw/pcmcia.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010029#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020030#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010031#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020032#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010033#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020034#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000035#include "net/slirp.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020036#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020037#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/sysemu.h"
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"
ths6a5bd302007-12-03 17:05:38 +000076
Jan Kiszka661f1922011-10-16 11:53:13 +020077/* for pic/irq_info */
78#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010079#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020080#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010081#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020082
bellard9dc39cb2004-03-14 21:38:27 +000083//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000084//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000085
bellard9307c4c2004-04-04 12:57:25 +000086/*
87 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000088 *
bellard9307c4c2004-04-04 12:57:25 +000089 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000090 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000091 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080092 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010093 * 'O' option string of the form NAME=VALUE,...
94 * parsed according to QemuOptsList given by its name
95 * Example: 'device:O' uses qemu_device_opts.
96 * Restriction: only lists with empty desc are supported
97 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000098 * 'i' 32 bit integer
99 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300100 * 'M' Non-negative target long (32 or 64 bit), in user mode the
101 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200102 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200103 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
104 * K, k suffix, which multiplies the value by 2^60 for suffixes E
105 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
106 * 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 +0100107 * 'T' double
108 * user mode accepts an optional ms, us, ns suffix,
109 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000110 * '/' optional gdb-like print format (like "/10x")
111 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300112 * '?' optional type (for all types, except '/')
113 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100114 * 'b' boolean
115 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300116 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000117 *
118 */
119
Adam Litke940cc302010-01-25 12:18:44 -0600120typedef struct MonitorCompletionData MonitorCompletionData;
121struct MonitorCompletionData {
122 Monitor *mon;
123 void (*user_print)(Monitor *mon, const QObject *data);
124};
125
Anthony Liguoric227f092009-10-01 16:12:16 -0500126typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000127 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000128 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000129 const char *params;
130 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300131 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300132 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300133 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200134 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600135 int (*cmd_async)(Monitor *mon, const QDict *params,
136 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300137 } mhandler;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200138 int flags;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800139 /* @sub_table is a list of 2nd level of commands. If it do not exist,
140 * mhandler should be used. If it exist, sub_table[?].mhandler should be
141 * used, and mhandler of 1st level plays the role of help function.
142 */
143 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100144 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500145} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000146
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100147/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500148typedef struct mon_fd_t mon_fd_t;
149struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100150 char *name;
151 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500152 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100153};
154
Corey Bryantba1c0482012-08-14 16:43:43 -0400155/* file descriptor associated with a file descriptor set */
156typedef struct MonFdsetFd MonFdsetFd;
157struct MonFdsetFd {
158 int fd;
159 bool removed;
160 char *opaque;
161 QLIST_ENTRY(MonFdsetFd) next;
162};
163
164/* file descriptor set containing fds passed via SCM_RIGHTS */
165typedef struct MonFdset MonFdset;
166struct MonFdset {
167 int64_t id;
168 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400169 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400170 QLIST_ENTRY(MonFdset) next;
171};
172
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200173typedef struct MonitorControl {
174 QObject *id;
175 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200176 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200177} MonitorControl;
178
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100179/*
180 * To prevent flooding clients, events can be throttled. The
181 * throttling is calculated globally, rather than per-Monitor
182 * instance.
183 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200184typedef struct MonitorQAPIEventState {
185 QAPIEvent event; /* Event being tracked */
186 int64_t rate; /* Minimum time (in ns) between two events */
187 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
188 QEMUTimer *timer; /* Timer for handling delayed events */
189 QObject *data; /* Event pending delayed dispatch */
190} MonitorQAPIEventState;
191
aliguori87127162009-03-05 23:01:29 +0000192struct Monitor {
193 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200194 int mux_out;
195 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000196 int flags;
197 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400198 bool skip_flush;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400199 QString *outbuf;
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200200 guint watch;
aliguori731b0362009-03-05 23:01:42 +0000201 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200202 MonitorControl *mc;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200203 CPUState *mon_cpu;
aliguori731b0362009-03-05 23:01:42 +0000204 BlockDriverCompletionFunc *password_completion_cb;
205 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800206 mon_cmd_t *cmd_table;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200207 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500208 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000209 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000210};
211
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300212/* QMP checker flags */
213#define QMP_ACCEPT_UNKNOWNS 1
214
Blue Swirl72cf2d42009-09-12 07:36:22 +0000215static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400216static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400217static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000218
Wayne Xia816f8922011-10-12 11:32:41 +0800219static mon_cmd_t mon_cmds[];
220static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000221
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300222static const mon_cmd_t qmp_cmds[];
223
Markus Armbruster8631b602010-02-18 11:41:55 +0100224Monitor *cur_mon;
225Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000226
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100227static void monitor_command_cb(void *opaque, const char *cmdline,
228 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000229
Luiz Capitulino09069b12010-02-04 18:10:06 -0200230static inline int qmp_cmd_mode(const Monitor *mon)
231{
232 return (mon->mc ? mon->mc->command_mode : 0);
233}
234
Luiz Capitulino418173c2009-11-26 22:58:51 -0200235/* Return true if in control mode, false otherwise */
236static inline int monitor_ctrl_mode(const Monitor *mon)
237{
238 return (mon->flags & MONITOR_USE_CONTROL);
239}
240
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100241/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
242int monitor_cur_is_qmp(void)
243{
244 return cur_mon && monitor_ctrl_mode(cur_mon);
245}
246
Anthony Liguori7060b472011-09-02 12:34:50 -0500247void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000248{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200249 if (!mon->rs)
250 return;
251
aliguori731b0362009-03-05 23:01:42 +0000252 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
253 if (show_prompt)
254 readline_show_prompt(mon->rs);
255}
bellard6a00d602005-11-21 23:25:50 +0000256
Anthony Liguori7060b472011-09-02 12:34:50 -0500257int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
258 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000259{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100260 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100261 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100262 return -EINVAL;
263 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000264 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
265 /* prompt is printed on return from the command handler */
266 return 0;
267 } else {
268 monitor_printf(mon, "terminal does not support password prompting\n");
269 return -ENOTTY;
270 }
aliguoribb5fc202009-03-05 23:01:15 +0000271}
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
278 mon->watch = 0;
279 monitor_flush(mon);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100280 return FALSE;
281}
282
aliguori376253e2009-03-05 23:01:23 +0000283void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000284{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100285 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400286 size_t len;
287 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100288
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400289 if (mon->skip_flush) {
290 return;
291 }
292
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400293 buf = qstring_get_str(mon->outbuf);
294 len = qstring_get_length(mon->outbuf);
295
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200296 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400297 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200298 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
299 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400300 QDECREF(mon->outbuf);
301 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100302 return;
303 }
304 if (rc > 0) {
305 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400306 QString *tmp = qstring_from_str(buf + rc);
307 QDECREF(mon->outbuf);
308 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100309 }
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200310 if (mon->watch == 0) {
311 mon->watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT,
312 monitor_unblocked, mon);
313 }
bellard7e2515e2004-08-01 21:52:19 +0000314 }
315}
316
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400317/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000318static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000319{
ths60fe76f2007-12-16 03:02:09 +0000320 char c;
aliguori731b0362009-03-05 23:01:42 +0000321
bellard7e2515e2004-08-01 21:52:19 +0000322 for(;;) {
323 c = *str++;
324 if (c == '\0')
325 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400326 if (c == '\n') {
327 qstring_append_chr(mon->outbuf, '\r');
328 }
329 qstring_append_chr(mon->outbuf, c);
330 if (c == '\n') {
aliguori376253e2009-03-05 23:01:23 +0000331 monitor_flush(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400332 }
bellard7e2515e2004-08-01 21:52:19 +0000333 }
334}
335
aliguori376253e2009-03-05 23:01:23 +0000336void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000337{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400338 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200339
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200340 if (!mon)
341 return;
342
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200343 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200344 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200345 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200346
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400347 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200348 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400349 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000350}
351
aliguori376253e2009-03-05 23:01:23 +0000352void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000353{
354 va_list ap;
355 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000356 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000357 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000358}
359
Stefan Weil8b7968f2010-09-23 21:28:05 +0200360static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
361 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000362{
363 va_list ap;
364 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000365 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000366 va_end(ap);
367 return 0;
368}
369
Luiz Capitulino13c74252009-10-07 13:41:55 -0300370static void monitor_user_noop(Monitor *mon, const QObject *data) { }
371
Luiz Capitulino9e807212010-09-16 10:58:59 -0300372static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300373{
374 return cmd->user_print != NULL;
375}
376
Luiz Capitulino4903de02010-09-16 11:01:32 -0300377static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600378{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200379 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600380}
381
Luiz Capitulino8204a912009-11-18 23:05:31 -0200382static inline int monitor_has_error(const Monitor *mon)
383{
384 return mon->error != NULL;
385}
386
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200387static void monitor_json_emitter(Monitor *mon, const QObject *data)
388{
389 QString *json;
390
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200391 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
392 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200393 assert(json != NULL);
394
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200395 qstring_append_chr(json, '\n');
396 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200397
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200398 QDECREF(json);
399}
400
Luiz Capitulinode253f12012-07-27 16:18:16 -0300401static QDict *build_qmp_error_dict(const QError *err)
402{
403 QObject *obj;
404
405 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
406 ErrorClass_lookup[err->err_class],
407 qerror_human(err));
408
409 return qobject_to_qdict(obj);
410}
411
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200412static void monitor_protocol_emitter(Monitor *mon, QObject *data)
413{
414 QDict *qmp;
415
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100416 trace_monitor_protocol_emitter(mon);
417
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200418 if (!monitor_has_error(mon)) {
419 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300420 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200421 if (data) {
422 qobject_incref(data);
423 qdict_put_obj(qmp, "return", data);
424 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200425 /* return an empty QDict by default */
426 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200427 }
428 } else {
429 /* error response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300430 qmp = build_qmp_error_dict(mon->error);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200431 QDECREF(mon->error);
432 mon->error = NULL;
433 }
434
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200435 if (mon->mc->id) {
436 qdict_put_obj(qmp, "id", mon->mc->id);
437 mon->mc->id = NULL;
438 }
439
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200440 monitor_json_emitter(mon, QOBJECT(qmp));
441 QDECREF(qmp);
442}
443
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200444
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200445static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
446
447/*
448 * Emits the event to every monitor instance, @event is only used for trace
449 */
450static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
451{
452 Monitor *mon;
453
454 trace_monitor_protocol_event_emit(event, data);
455 QLIST_FOREACH(mon, &mon_list, entry) {
456 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
457 monitor_json_emitter(mon, data);
458 }
459 }
460}
461
462/*
463 * Queue a new event for emission to Monitor instances,
464 * applying any rate limiting if required.
465 */
466static void
467monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
468{
469 MonitorQAPIEventState *evstate;
470 assert(event < QAPI_EVENT_MAX);
471 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
472
473 evstate = &(monitor_qapi_event_state[event]);
474 trace_monitor_protocol_event_queue(event,
475 data,
476 evstate->rate,
477 evstate->last,
478 now);
479
480 /* Rate limit of 0 indicates no throttling */
481 if (!evstate->rate) {
482 monitor_qapi_event_emit(event, QOBJECT(data));
483 evstate->last = now;
484 } else {
485 int64_t delta = now - evstate->last;
486 if (evstate->data ||
487 delta < evstate->rate) {
488 /* If there's an existing event pending, replace
489 * it with the new event, otherwise schedule a
490 * timer for delayed emission
491 */
492 if (evstate->data) {
493 qobject_decref(evstate->data);
494 } else {
495 int64_t then = evstate->last + evstate->rate;
496 timer_mod_ns(evstate->timer, then);
497 }
498 evstate->data = QOBJECT(data);
499 qobject_incref(evstate->data);
500 } else {
501 monitor_qapi_event_emit(event, QOBJECT(data));
502 evstate->last = now;
503 }
504 }
505}
506
507/*
508 * The callback invoked by QemuTimer when a delayed
509 * event is ready to be emitted
510 */
511static void monitor_qapi_event_handler(void *opaque)
512{
513 MonitorQAPIEventState *evstate = opaque;
514 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
515
516 trace_monitor_protocol_event_handler(evstate->event,
517 evstate->data,
518 evstate->last,
519 now);
520 if (evstate->data) {
521 monitor_qapi_event_emit(evstate->event, evstate->data);
522 qobject_decref(evstate->data);
523 evstate->data = NULL;
524 }
525 evstate->last = now;
526}
527
528/*
529 * @event: the event ID to be limited
530 * @rate: the rate limit in milliseconds
531 *
532 * Sets a rate limit on a particular event, so no
533 * more than 1 event will be emitted within @rate
534 * milliseconds
535 */
Wenchao Xia75175172014-06-18 08:43:54 +0200536static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200537monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
538{
539 MonitorQAPIEventState *evstate;
540 assert(event < QAPI_EVENT_MAX);
541
542 evstate = &(monitor_qapi_event_state[event]);
543
544 trace_monitor_protocol_event_throttle(event, rate);
545 evstate->event = event;
546 evstate->rate = rate * SCALE_MS;
547 evstate->last = 0;
548 evstate->data = NULL;
549 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
550 SCALE_MS,
551 monitor_qapi_event_handler,
552 evstate);
553}
554
555static void monitor_qapi_event_init(void)
556{
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 /* limit the rate of quorum events to avoid hammering the management */
562 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
563 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
Wenchao Xiae010ad82014-06-18 08:43:41 +0200564
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200565 qmp_event_set_func_emit(monitor_qapi_event_queue);
566}
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{
571 /* Will setup QMP capabilities in the future */
572 if (monitor_ctrl_mode(mon)) {
573 mon->mc->command_mode = 1;
574 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200575
576 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200577}
578
Luiz Capitulino0268d972010-10-22 10:08:02 -0200579static void handle_user_command(Monitor *mon, const char *cmdline);
580
Wenchao Xiab01fe892013-08-27 20:38:19 +0800581static void monitor_data_init(Monitor *mon)
582{
583 memset(mon, 0, sizeof(Monitor));
584 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800585 /* Use *mon_cmds by default. */
586 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800587}
588
589static void monitor_data_destroy(Monitor *mon)
590{
591 QDECREF(mon->outbuf);
592}
593
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200594char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
595 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200596{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200597 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200598 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200599
Wenchao Xiab01fe892013-08-27 20:38:19 +0800600 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400601 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200602
603 old_mon = cur_mon;
604 cur_mon = &hmp;
605
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200606 if (has_cpu_index) {
607 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200608 if (ret < 0) {
609 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200610 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
611 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200612 goto out;
613 }
614 }
615
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200616 handle_user_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200617 cur_mon = old_mon;
618
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400619 if (qstring_get_length(hmp.outbuf) > 0) {
620 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200621 } else {
622 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200623 }
624
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
Lluísfc764102011-08-31 20:31:18 +0200853static void do_trace_event_set_state(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");
Blue Swirlf871d682010-10-13 19:14:29 +0000857
Lluís Vilanovaca285c32013-03-05 14:47:43 +0100858 bool found = false;
859 TraceEvent *ev = NULL;
860 while ((ev = trace_event_pattern(tp_name, ev)) != NULL) {
861 found = true;
862 if (!trace_event_get_state_static(ev)) {
863 monitor_printf(mon, "event \"%s\" is not traceable\n", tp_name);
864 } else {
865 trace_event_set_state_dynamic(ev, new_state);
866 }
867 }
868 if (!trace_event_is_pattern(tp_name) && !found) {
Blue Swirlf871d682010-10-13 19:14:29 +0000869 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
870 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530871}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100872
Michael Rothc45a8162011-10-02 08:44:37 -0500873#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100874static void do_trace_file(Monitor *mon, const QDict *qdict)
875{
876 const char *op = qdict_get_try_str(qdict, "op");
877 const char *arg = qdict_get_try_str(qdict, "arg");
878
879 if (!op) {
880 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
881 } else if (!strcmp(op, "on")) {
882 st_set_trace_file_enabled(true);
883 } else if (!strcmp(op, "off")) {
884 st_set_trace_file_enabled(false);
885 } else if (!strcmp(op, "flush")) {
886 st_flush_trace_buffer();
887 } else if (!strcmp(op, "set")) {
888 if (arg) {
889 st_set_trace_file(arg);
890 }
891 } else {
892 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
893 help_cmd(mon, "trace-file");
894 }
895}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530896#endif
897
Adam Litke940cc302010-01-25 12:18:44 -0600898static void user_monitor_complete(void *opaque, QObject *ret_data)
899{
900 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
901
902 if (ret_data) {
903 data->user_print(data->mon, ret_data);
904 }
905 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500906 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600907}
908
909static void qmp_monitor_complete(void *opaque, QObject *ret_data)
910{
911 monitor_protocol_emitter(opaque, ret_data);
912}
913
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300914static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
915 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600916{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300917 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600918}
919
Adam Litke940cc302010-01-25 12:18:44 -0600920static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
921 const QDict *params)
922{
923 int ret;
924
Anthony Liguori7267c092011-08-20 22:09:37 -0500925 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600926 cb_data->mon = mon;
927 cb_data->user_print = cmd->user_print;
928 monitor_suspend(mon);
929 ret = cmd->mhandler.cmd_async(mon, params,
930 user_monitor_complete, cb_data);
931 if (ret < 0) {
932 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500933 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600934 }
935}
936
Wenchao Xia84c44612013-01-14 14:06:29 +0800937static void do_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000938{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300939 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000940}
941
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300942CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000943{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300944 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200945 const mon_cmd_t *cmd;
946
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300947 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200948 info = g_malloc0(sizeof(*info));
949 info->value = g_malloc0(sizeof(*info->value));
950 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200951
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300952 info->next = cmd_list;
953 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200954 }
955
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300956 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000957}
958
Daniel P. Berrange48608532012-05-21 17:59:51 +0100959EventInfoList *qmp_query_events(Error **errp)
960{
961 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200962 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100963
Wenchao Xia75175172014-06-18 08:43:54 +0200964 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
965 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100966 assert(event_name != NULL);
967 info = g_malloc0(sizeof(*info));
968 info->value = g_malloc0(sizeof(*info->value));
969 info->value->name = g_strdup(event_name);
970
971 info->next = ev_list;
972 ev_list = info;
973 }
974
975 return ev_list;
976}
977
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300978/* set the current CPU defined by the user */
979int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000980{
Andreas Färber55e5c282012-12-17 06:18:02 +0100981 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000982
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100983 cpu = qemu_get_cpu(cpu_index);
984 if (cpu == NULL) {
985 return -1;
bellard6a00d602005-11-21 23:25:50 +0000986 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200987 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100988 return 0;
bellard6a00d602005-11-21 23:25:50 +0000989}
990
Andreas Färber9349b4f2012-03-14 01:38:32 +0100991static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000992{
aliguori731b0362009-03-05 23:01:42 +0000993 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300994 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000995 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300996 cpu_synchronize_state(cur_mon->mon_cpu);
Andreas Färbercb446ec2013-05-01 14:24:52 +0200997 return cur_mon->mon_cpu->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000998}
999
Luiz Capitulino99b77962011-10-24 10:53:44 -02001000int monitor_get_cpu_index(void)
1001{
Andreas Färber55e5c282012-12-17 06:18:02 +01001002 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
1003 return cpu->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001004}
1005
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001006static void do_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001007{
Andreas Färber878096e2013-05-27 01:33:50 +02001008 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001009 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +00001010 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001011 cpu = ENV_GET_CPU(env);
1012 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +00001013}
1014
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001015static void do_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001016{
aliguori376253e2009-03-05 23:01:23 +00001017 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001018}
1019
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001020static void do_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001021{
1022 int i;
bellard7e2515e2004-08-01 21:52:19 +00001023 const char *str;
ths3b46e622007-09-17 08:09:54 +00001024
aliguoricde76ee2009-03-05 23:01:51 +00001025 if (!mon->rs)
1026 return;
bellard7e2515e2004-08-01 21:52:19 +00001027 i = 0;
1028 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001029 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001030 if (!str)
1031 break;
aliguori376253e2009-03-05 23:01:23 +00001032 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001033 i++;
bellardaa455482004-04-04 13:07:25 +00001034 }
1035}
1036
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001037static void do_info_cpu_stats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001038{
Andreas Färber878096e2013-05-27 01:33:50 +02001039 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001040 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +00001041
1042 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001043 cpu = ENV_GET_CPU(env);
1044 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001045}
j_mayer76a66252007-03-07 08:32:30 +00001046
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001047static void do_trace_print_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301048{
Lluísfc764102011-08-31 20:31:18 +02001049 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301050}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301051
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001052static int client_migrate_info(Monitor *mon, const QDict *qdict,
1053 MonitorCompletion cb, void *opaque)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001054{
1055 const char *protocol = qdict_get_str(qdict, "protocol");
1056 const char *hostname = qdict_get_str(qdict, "hostname");
1057 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1058 int port = qdict_get_try_int(qdict, "port", -1);
1059 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1060 int ret;
1061
1062 if (strcmp(protocol, "spice") == 0) {
1063 if (!using_spice) {
1064 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1065 return -1;
1066 }
1067
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001068 if (port == -1 && tls_port == -1) {
1069 qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
1070 return -1;
1071 }
1072
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001073 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
1074 cb, opaque);
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001075 if (ret != 0) {
1076 qerror_report(QERR_UNDEFINED_ERROR);
1077 return -1;
1078 }
1079 return 0;
1080 }
1081
1082 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1083 return -1;
1084}
1085
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001086static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001087{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001088 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001089}
1090
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001091static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001092{
1093 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001094 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001095
bellard9307c4c2004-04-04 12:57:25 +00001096 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001097 mask = 0;
1098 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001099 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001100 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001101 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001102 return;
1103 }
1104 }
Peter Maydell24537a02013-02-11 16:41:23 +00001105 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001106}
1107
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001108static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001109{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001110 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001111 if (!option || !strcmp(option, "on")) {
1112 singlestep = 1;
1113 } else if (!strcmp(option, "off")) {
1114 singlestep = 0;
1115 } else {
1116 monitor_printf(mon, "unexpected option %s\n", option);
1117 }
1118}
1119
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001120static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001121{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001122 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001123 if (!device)
1124 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1125 if (gdbserver_start(device) < 0) {
1126 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1127 device);
1128 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001129 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001130 } else {
aliguori59030a82009-04-05 18:43:41 +00001131 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1132 device);
bellard8a7ddc32004-03-31 19:00:16 +00001133 }
1134}
1135
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001136static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001137{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001138 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001139 if (select_watchdog_action(action) == -1) {
1140 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1141 }
1142}
1143
aliguori376253e2009-03-05 23:01:23 +00001144static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001145{
aliguori376253e2009-03-05 23:01:23 +00001146 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001147 switch(c) {
1148 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001149 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001150 break;
1151 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001152 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001153 break;
1154 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001155 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001156 break;
1157 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001158 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001159 break;
1160 default:
1161 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001162 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001163 } else {
aliguori376253e2009-03-05 23:01:23 +00001164 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001165 }
1166 break;
1167 }
aliguori376253e2009-03-05 23:01:23 +00001168 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001169}
1170
aliguori376253e2009-03-05 23:01:23 +00001171static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001172 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001173{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001174 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001175 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001176 uint8_t buf[16];
1177 uint64_t v;
1178
1179 if (format == 'i') {
1180 int flags;
1181 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001182 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001183#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001184 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001185 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001186 } else if (wsize == 4) {
1187 flags = 0;
1188 } else {
bellard6a15fd12006-04-12 21:07:07 +00001189 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001190 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001191 if (env) {
1192#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001193 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001194 (env->segs[R_CS].flags & DESC_L_MASK))
1195 flags = 2;
1196 else
1197#endif
1198 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1199 flags = 1;
1200 }
bellard4c27ba22004-04-25 18:05:08 +00001201 }
1202#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001203#ifdef TARGET_PPC
1204 flags = msr_le << 16;
1205 flags |= env->bfd_mach;
1206#endif
aliguori376253e2009-03-05 23:01:23 +00001207 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001208 return;
1209 }
1210
1211 len = wsize * count;
1212 if (wsize == 1)
1213 line_size = 8;
1214 else
1215 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001216 max_digits = 0;
1217
1218 switch(format) {
1219 case 'o':
1220 max_digits = (wsize * 8 + 2) / 3;
1221 break;
1222 default:
1223 case 'x':
1224 max_digits = (wsize * 8) / 4;
1225 break;
1226 case 'u':
1227 case 'd':
1228 max_digits = (wsize * 8 * 10 + 32) / 33;
1229 break;
1230 case 'c':
1231 wsize = 1;
1232 break;
1233 }
1234
1235 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001236 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001237 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001238 else
aliguori376253e2009-03-05 23:01:23 +00001239 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001240 l = len;
1241 if (l > line_size)
1242 l = line_size;
1243 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001244 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001245 } else {
bellard6a00d602005-11-21 23:25:50 +00001246 env = mon_get_cpu();
Andreas Färberf17ec442013-06-29 19:40:58 +02001247 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001248 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001249 break;
1250 }
bellard9307c4c2004-04-04 12:57:25 +00001251 }
ths5fafdf22007-09-16 21:08:06 +00001252 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001253 while (i < l) {
1254 switch(wsize) {
1255 default:
1256 case 1:
1257 v = ldub_raw(buf + i);
1258 break;
1259 case 2:
1260 v = lduw_raw(buf + i);
1261 break;
1262 case 4:
bellard92a31b12005-02-10 22:00:52 +00001263 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001264 break;
1265 case 8:
1266 v = ldq_raw(buf + i);
1267 break;
1268 }
aliguori376253e2009-03-05 23:01:23 +00001269 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001270 switch(format) {
1271 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001272 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001273 break;
1274 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001275 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001276 break;
1277 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001278 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001279 break;
1280 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001281 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001282 break;
1283 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001284 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001285 break;
1286 }
1287 i += wsize;
1288 }
aliguori376253e2009-03-05 23:01:23 +00001289 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001290 addr += l;
1291 len -= l;
1292 }
1293}
1294
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001295static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001296{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001297 int count = qdict_get_int(qdict, "count");
1298 int format = qdict_get_int(qdict, "format");
1299 int size = qdict_get_int(qdict, "size");
1300 target_long addr = qdict_get_int(qdict, "addr");
1301
aliguori376253e2009-03-05 23:01:23 +00001302 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001303}
1304
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001305static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001306{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001307 int count = qdict_get_int(qdict, "count");
1308 int format = qdict_get_int(qdict, "format");
1309 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001310 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001311
aliguori376253e2009-03-05 23:01:23 +00001312 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001313}
1314
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001315static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001316{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001317 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001318 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001319
bellard9307c4c2004-04-04 12:57:25 +00001320 switch(format) {
1321 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001322 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001323 break;
1324 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001325 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001326 break;
1327 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001328 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001329 break;
1330 default:
1331 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001332 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001333 break;
1334 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001335 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001336 break;
1337 }
aliguori376253e2009-03-05 23:01:23 +00001338 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001339}
1340
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001341static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001342{
1343 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001344 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001345 uint32_t start = qdict_get_int(qdict, "start");
1346 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001347
1348 sum = 0;
1349 for(addr = start; addr < (start + size); addr++) {
Edgar E. Iglesias2c174492013-12-17 14:05:40 +10001350 uint8_t val = ldub_phys(&address_space_memory, addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001351 /* BSD sum algorithm ('sum' Unix command) */
1352 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001353 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001354 }
aliguori376253e2009-03-05 23:01:23 +00001355 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001356}
1357
bellard13224a82006-07-14 22:03:35 +00001358static int mouse_button_state;
1359
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001360static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001361{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001362 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001363 const char *dx_str = qdict_get_str(qdict, "dx_str");
1364 const char *dy_str = qdict_get_str(qdict, "dy_str");
1365 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001366
bellard13224a82006-07-14 22:03:35 +00001367 dx = strtol(dx_str, NULL, 0);
1368 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001369 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1370 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1371
1372 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001373 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001374 if (dz != 0) {
1375 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1376 qemu_input_queue_btn(NULL, button, true);
1377 qemu_input_event_sync();
1378 qemu_input_queue_btn(NULL, button, false);
1379 }
1380 }
1381 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001382}
1383
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001384static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001385{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001386 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1387 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1388 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1389 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1390 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001391 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001392
1393 if (mouse_button_state == button_state) {
1394 return;
1395 }
1396 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1397 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001398 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001399}
1400
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001401static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001402{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001403 int size = qdict_get_int(qdict, "size");
1404 int addr = qdict_get_int(qdict, "addr");
1405 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001406 uint32_t val;
1407 int suffix;
1408
1409 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001410 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001411 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001412 addr++;
1413 }
1414 addr &= 0xffff;
1415
1416 switch(size) {
1417 default:
1418 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001419 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001420 suffix = 'b';
1421 break;
1422 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001423 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001424 suffix = 'w';
1425 break;
1426 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001427 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001428 suffix = 'l';
1429 break;
1430 }
aliguori376253e2009-03-05 23:01:23 +00001431 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1432 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001433}
bellarda3a91a32004-06-04 11:06:21 +00001434
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001435static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001436{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001437 int size = qdict_get_int(qdict, "size");
1438 int addr = qdict_get_int(qdict, "addr");
1439 int val = qdict_get_int(qdict, "val");
1440
Jan Kiszkaf1147842009-07-14 10:20:11 +02001441 addr &= IOPORTS_MASK;
1442
1443 switch (size) {
1444 default:
1445 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001446 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001447 break;
1448 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001449 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001450 break;
1451 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001452 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001453 break;
1454 }
1455}
1456
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001457static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001458{
1459 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001460 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001461
Jan Kiszka76e30d02009-07-02 00:19:02 +02001462 res = qemu_boot_set(bootdevice);
1463 if (res == 0) {
1464 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1465 } else if (res > 0) {
1466 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001467 } else {
aliguori376253e2009-03-05 23:01:23 +00001468 monitor_printf(mon, "no function defined to set boot device list for "
1469 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001470 }
1471}
1472
bellardb86bda52004-09-18 19:32:46 +00001473#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001474static void print_pte(Monitor *mon, hwaddr addr,
1475 hwaddr pte,
1476 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001477{
Blue Swirld65aaf32010-12-11 18:56:24 +00001478#ifdef TARGET_X86_64
1479 if (addr & (1ULL << 47)) {
1480 addr |= -1LL << 48;
1481 }
1482#endif
1483 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1484 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001485 addr,
1486 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001487 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001488 pte & PG_GLOBAL_MASK ? 'G' : '-',
1489 pte & PG_PSE_MASK ? 'P' : '-',
1490 pte & PG_DIRTY_MASK ? 'D' : '-',
1491 pte & PG_ACCESSED_MASK ? 'A' : '-',
1492 pte & PG_PCD_MASK ? 'C' : '-',
1493 pte & PG_PWT_MASK ? 'T' : '-',
1494 pte & PG_USER_MASK ? 'U' : '-',
1495 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001496}
1497
Andreas Färber9349b4f2012-03-14 01:38:32 +01001498static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001499{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001500 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001501 uint32_t pgd, pde, pte;
1502
bellardb86bda52004-09-18 19:32:46 +00001503 pgd = env->cr[3] & ~0xfff;
1504 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001505 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001506 pde = le32_to_cpu(pde);
1507 if (pde & PG_PRESENT_MASK) {
1508 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001509 /* 4M pages */
1510 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001511 } else {
1512 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001513 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001514 pte = le32_to_cpu(pte);
1515 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001516 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001517 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001518 ~0xfff);
1519 }
1520 }
1521 }
1522 }
1523 }
1524}
1525
Andreas Färber9349b4f2012-03-14 01:38:32 +01001526static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001527{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001528 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001529 uint64_t pdpe, pde, pte;
1530 uint64_t pdp_addr, pd_addr, pt_addr;
1531
1532 pdp_addr = env->cr[3] & ~0x1f;
1533 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001534 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001535 pdpe = le64_to_cpu(pdpe);
1536 if (pdpe & PG_PRESENT_MASK) {
1537 pd_addr = pdpe & 0x3fffffffff000ULL;
1538 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001539 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001540 pde = le64_to_cpu(pde);
1541 if (pde & PG_PRESENT_MASK) {
1542 if (pde & PG_PSE_MASK) {
1543 /* 2M pages with PAE, CR4.PSE is ignored */
1544 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001545 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001546 } else {
1547 pt_addr = pde & 0x3fffffffff000ULL;
1548 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001549 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001550 pte = le64_to_cpu(pte);
1551 if (pte & PG_PRESENT_MASK) {
1552 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1553 + (l3 << 12),
1554 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001555 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001556 }
1557 }
1558 }
1559 }
1560 }
1561 }
1562 }
1563}
1564
1565#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001566static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001567{
1568 uint64_t l1, l2, l3, l4;
1569 uint64_t pml4e, pdpe, pde, pte;
1570 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1571
1572 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1573 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001574 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001575 pml4e = le64_to_cpu(pml4e);
1576 if (pml4e & PG_PRESENT_MASK) {
1577 pdp_addr = pml4e & 0x3fffffffff000ULL;
1578 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001579 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001580 pdpe = le64_to_cpu(pdpe);
1581 if (pdpe & PG_PRESENT_MASK) {
1582 if (pdpe & PG_PSE_MASK) {
1583 /* 1G pages, CR4.PSE is ignored */
1584 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1585 0x3ffffc0000000ULL);
1586 } else {
1587 pd_addr = pdpe & 0x3fffffffff000ULL;
1588 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001589 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001590 pde = le64_to_cpu(pde);
1591 if (pde & PG_PRESENT_MASK) {
1592 if (pde & PG_PSE_MASK) {
1593 /* 2M pages, CR4.PSE is ignored */
1594 print_pte(mon, (l1 << 39) + (l2 << 30) +
1595 (l3 << 21), pde,
1596 0x3ffffffe00000ULL);
1597 } else {
1598 pt_addr = pde & 0x3fffffffff000ULL;
1599 for (l4 = 0; l4 < 512; l4++) {
1600 cpu_physical_memory_read(pt_addr
1601 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001602 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001603 pte = le64_to_cpu(pte);
1604 if (pte & PG_PRESENT_MASK) {
1605 print_pte(mon, (l1 << 39) +
1606 (l2 << 30) +
1607 (l3 << 21) + (l4 << 12),
1608 pte & ~PG_PSE_MASK,
1609 0x3fffffffff000ULL);
1610 }
1611 }
1612 }
1613 }
1614 }
1615 }
1616 }
1617 }
1618 }
1619 }
1620}
1621#endif
1622
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001623static void tlb_info(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001624{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001625 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001626
1627 env = mon_get_cpu();
1628
1629 if (!(env->cr[0] & CR0_PG_MASK)) {
1630 monitor_printf(mon, "PG disabled\n");
1631 return;
1632 }
1633 if (env->cr[4] & CR4_PAE_MASK) {
1634#ifdef TARGET_X86_64
1635 if (env->hflags & HF_LMA_MASK) {
1636 tlb_info_64(mon, env);
1637 } else
1638#endif
1639 {
1640 tlb_info_pae32(mon, env);
1641 }
1642 } else {
1643 tlb_info_32(mon, env);
1644 }
1645}
1646
Avi Kivitya8170e52012-10-23 12:30:10 +02001647static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001648 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001649 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001650{
bellard9746b152004-11-11 18:30:24 +00001651 int prot1;
1652 prot1 = *plast_prot;
1653 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001654 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001655 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1656 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001657 *pstart, end, end - *pstart,
1658 prot1 & PG_USER_MASK ? 'u' : '-',
1659 'r',
1660 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001661 }
1662 if (prot != 0)
1663 *pstart = end;
1664 else
1665 *pstart = -1;
1666 *plast_prot = prot;
1667 }
1668}
1669
Andreas Färber9349b4f2012-03-14 01:38:32 +01001670static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001671{
Austin Clementsb49ca722011-08-14 23:19:21 -04001672 unsigned int l1, l2;
1673 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001674 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001675 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001676
bellardb86bda52004-09-18 19:32:46 +00001677 pgd = env->cr[3] & ~0xfff;
1678 last_prot = 0;
1679 start = -1;
1680 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001681 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001682 pde = le32_to_cpu(pde);
1683 end = l1 << 22;
1684 if (pde & PG_PRESENT_MASK) {
1685 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1686 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001687 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001688 } else {
1689 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001690 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001691 pte = le32_to_cpu(pte);
1692 end = (l1 << 22) + (l2 << 12);
1693 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001694 prot = pte & pde &
1695 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001696 } else {
1697 prot = 0;
1698 }
aliguori376253e2009-03-05 23:01:23 +00001699 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001700 }
1701 }
1702 } else {
1703 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001704 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001705 }
1706 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001707 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001708 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001709}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001710
Andreas Färber9349b4f2012-03-14 01:38:32 +01001711static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001712{
Austin Clementsb49ca722011-08-14 23:19:21 -04001713 unsigned int l1, l2, l3;
1714 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001715 uint64_t pdpe, pde, pte;
1716 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001717 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001718
1719 pdp_addr = env->cr[3] & ~0x1f;
1720 last_prot = 0;
1721 start = -1;
1722 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001723 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001724 pdpe = le64_to_cpu(pdpe);
1725 end = l1 << 30;
1726 if (pdpe & PG_PRESENT_MASK) {
1727 pd_addr = pdpe & 0x3fffffffff000ULL;
1728 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001729 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001730 pde = le64_to_cpu(pde);
1731 end = (l1 << 30) + (l2 << 21);
1732 if (pde & PG_PRESENT_MASK) {
1733 if (pde & PG_PSE_MASK) {
1734 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1735 PG_PRESENT_MASK);
1736 mem_print(mon, &start, &last_prot, end, prot);
1737 } else {
1738 pt_addr = pde & 0x3fffffffff000ULL;
1739 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001740 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001741 pte = le64_to_cpu(pte);
1742 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1743 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001744 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1745 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001746 } else {
1747 prot = 0;
1748 }
1749 mem_print(mon, &start, &last_prot, end, prot);
1750 }
1751 }
1752 } else {
1753 prot = 0;
1754 mem_print(mon, &start, &last_prot, end, prot);
1755 }
1756 }
1757 } else {
1758 prot = 0;
1759 mem_print(mon, &start, &last_prot, end, prot);
1760 }
1761 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001762 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001763 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001764}
1765
1766
1767#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001768static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001769{
1770 int prot, last_prot;
1771 uint64_t l1, l2, l3, l4;
1772 uint64_t pml4e, pdpe, pde, pte;
1773 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1774
1775 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1776 last_prot = 0;
1777 start = -1;
1778 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001779 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001780 pml4e = le64_to_cpu(pml4e);
1781 end = l1 << 39;
1782 if (pml4e & PG_PRESENT_MASK) {
1783 pdp_addr = pml4e & 0x3fffffffff000ULL;
1784 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001785 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001786 pdpe = le64_to_cpu(pdpe);
1787 end = (l1 << 39) + (l2 << 30);
1788 if (pdpe & PG_PRESENT_MASK) {
1789 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001790 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1791 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001792 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001793 mem_print(mon, &start, &last_prot, end, prot);
1794 } else {
1795 pd_addr = pdpe & 0x3fffffffff000ULL;
1796 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001797 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001798 pde = le64_to_cpu(pde);
1799 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1800 if (pde & PG_PRESENT_MASK) {
1801 if (pde & PG_PSE_MASK) {
1802 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1803 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001804 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001805 mem_print(mon, &start, &last_prot, end, prot);
1806 } else {
1807 pt_addr = pde & 0x3fffffffff000ULL;
1808 for (l4 = 0; l4 < 512; l4++) {
1809 cpu_physical_memory_read(pt_addr
1810 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001811 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001812 pte = le64_to_cpu(pte);
1813 end = (l1 << 39) + (l2 << 30) +
1814 (l3 << 21) + (l4 << 12);
1815 if (pte & PG_PRESENT_MASK) {
1816 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1817 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001818 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001819 } else {
1820 prot = 0;
1821 }
1822 mem_print(mon, &start, &last_prot, end, prot);
1823 }
1824 }
1825 } else {
1826 prot = 0;
1827 mem_print(mon, &start, &last_prot, end, prot);
1828 }
1829 }
1830 }
1831 } else {
1832 prot = 0;
1833 mem_print(mon, &start, &last_prot, end, prot);
1834 }
1835 }
1836 } else {
1837 prot = 0;
1838 mem_print(mon, &start, &last_prot, end, prot);
1839 }
1840 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001841 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001842 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001843}
1844#endif
1845
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001846static void mem_info(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001847{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001848 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001849
1850 env = mon_get_cpu();
1851
1852 if (!(env->cr[0] & CR0_PG_MASK)) {
1853 monitor_printf(mon, "PG disabled\n");
1854 return;
1855 }
1856 if (env->cr[4] & CR4_PAE_MASK) {
1857#ifdef TARGET_X86_64
1858 if (env->hflags & HF_LMA_MASK) {
1859 mem_info_64(mon, env);
1860 } else
1861#endif
1862 {
1863 mem_info_pae32(mon, env);
1864 }
1865 } else {
1866 mem_info_32(mon, env);
1867 }
1868}
bellardb86bda52004-09-18 19:32:46 +00001869#endif
1870
aurel327c664e22009-03-03 06:12:22 +00001871#if defined(TARGET_SH4)
1872
aliguori376253e2009-03-05 23:01:23 +00001873static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001874{
aliguori376253e2009-03-05 23:01:23 +00001875 monitor_printf(mon, " tlb%i:\t"
1876 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1877 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1878 "dirty=%hhu writethrough=%hhu\n",
1879 idx,
1880 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1881 tlb->v, tlb->sh, tlb->c, tlb->pr,
1882 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001883}
1884
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001885static void tlb_info(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001886{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001887 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00001888 int i;
1889
aliguori376253e2009-03-05 23:01:23 +00001890 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001891 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001892 print_tlb (mon, i, &env->itlb[i]);
1893 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001894 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001895 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001896}
1897
1898#endif
1899
Max Filippov692f7372012-01-07 20:02:40 +04001900#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001901static void tlb_info(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001902{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001903 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00001904
1905 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1906}
1907#endif
1908
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001909static void do_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001910{
1911 mtree_info((fprintf_function)monitor_printf, mon);
1912}
1913
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001914static void do_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001915{
aliguorib28b6232009-04-22 20:20:29 +00001916 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001917 CPUState *cpu;
aliguori030ea372009-04-21 22:30:47 +00001918
1919 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1920 for (i = 0; i < nb_numa_nodes; i++) {
1921 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001922 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001923 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001924 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001925 }
1926 }
1927 monitor_printf(mon, "\n");
1928 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
Wanlong Gao8c859012014-05-14 17:43:07 +08001929 numa_info[i].node_mem >> 20);
aliguori030ea372009-04-21 22:30:47 +00001930 }
1931}
1932
bellard5f1ce942006-02-08 22:40:15 +00001933#ifdef CONFIG_PROFILER
1934
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001935int64_t qemu_time;
1936int64_t dev_time;
1937
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001938static void do_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001939{
aliguori376253e2009-03-05 23:01:23 +00001940 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001941 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001942 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001943 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001944 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001945 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001946}
1947#else
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001948static void do_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001949{
aliguori376253e2009-03-05 23:01:23 +00001950 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001951}
1952#endif
1953
bellardec36b692006-07-16 18:57:03 +00001954/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001955static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001956
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001957static void do_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001958{
1959 int i;
1960 CaptureState *s;
1961
1962 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001963 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001964 s->ops.info (s->opaque);
1965 }
1966}
1967
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001968static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001969{
1970 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001971 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001972 CaptureState *s;
1973
1974 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1975 if (i == n) {
1976 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001977 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001978 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001979 return;
1980 }
1981 }
1982}
1983
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001984static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001985{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001986 const char *path = qdict_get_str(qdict, "path");
1987 int has_freq = qdict_haskey(qdict, "freq");
1988 int freq = qdict_get_try_int(qdict, "freq", -1);
1989 int has_bits = qdict_haskey(qdict, "bits");
1990 int bits = qdict_get_try_int(qdict, "bits", -1);
1991 int has_channels = qdict_haskey(qdict, "nchannels");
1992 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001993 CaptureState *s;
1994
Anthony Liguori7267c092011-08-20 22:09:37 -05001995 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001996
1997 freq = has_freq ? freq : 44100;
1998 bits = has_bits ? bits : 16;
1999 nchannels = has_channels ? nchannels : 2;
2000
2001 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002002 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002003 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002004 return;
bellardec36b692006-07-16 18:57:03 +00002005 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002006 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002007}
bellardec36b692006-07-16 18:57:03 +00002008
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002009static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002010{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002011 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002012
aliguori76655d62009-03-06 20:27:37 +00002013 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002014 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002015 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002016 return acl;
2017}
aliguori76655d62009-03-06 20:27:37 +00002018
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002019static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002020{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002021 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002022 qemu_acl *acl = find_acl(mon, aclname);
2023 qemu_acl_entry *entry;
2024 int i = 0;
2025
2026 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002027 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002028 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002029 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002030 i++;
2031 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002032 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002033 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002034 }
2035}
2036
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002037static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002038{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002039 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002040 qemu_acl *acl = find_acl(mon, aclname);
2041
2042 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002043 qemu_acl_reset(acl);
2044 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002045 }
2046}
aliguori76655d62009-03-06 20:27:37 +00002047
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002048static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002049{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002050 const char *aclname = qdict_get_str(qdict, "aclname");
2051 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002052 qemu_acl *acl = find_acl(mon, aclname);
2053
2054 if (acl) {
2055 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002056 acl->defaultDeny = 0;
2057 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002058 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002059 acl->defaultDeny = 1;
2060 monitor_printf(mon, "acl: policy set to 'deny'\n");
2061 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002062 monitor_printf(mon, "acl: unknown policy '%s', "
2063 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002064 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002065 }
2066}
aliguori76655d62009-03-06 20:27:37 +00002067
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002068static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002069{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002070 const char *aclname = qdict_get_str(qdict, "aclname");
2071 const char *match = qdict_get_str(qdict, "match");
2072 const char *policy = qdict_get_str(qdict, "policy");
2073 int has_index = qdict_haskey(qdict, "index");
2074 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002075 qemu_acl *acl = find_acl(mon, aclname);
2076 int deny, ret;
2077
2078 if (acl) {
2079 if (strcmp(policy, "allow") == 0) {
2080 deny = 0;
2081 } else if (strcmp(policy, "deny") == 0) {
2082 deny = 1;
2083 } else {
2084 monitor_printf(mon, "acl: unknown policy '%s', "
2085 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002086 return;
2087 }
aliguori28a76be2009-03-06 20:27:40 +00002088 if (has_index)
2089 ret = qemu_acl_insert(acl, deny, match, index);
2090 else
2091 ret = qemu_acl_append(acl, deny, match);
2092 if (ret < 0)
2093 monitor_printf(mon, "acl: unable to add acl entry\n");
2094 else
2095 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002096 }
2097}
aliguori76655d62009-03-06 20:27:37 +00002098
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002099static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002100{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002101 const char *aclname = qdict_get_str(qdict, "aclname");
2102 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002103 qemu_acl *acl = find_acl(mon, aclname);
2104 int ret;
aliguori76655d62009-03-06 20:27:37 +00002105
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002106 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002107 ret = qemu_acl_remove(acl, match);
2108 if (ret < 0)
2109 monitor_printf(mon, "acl: no matching acl entry\n");
2110 else
2111 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002112 }
2113}
2114
Huang Ying79c4f6b2009-06-23 10:05:14 +08002115#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002116static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002117{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002118 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002119 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002120 int cpu_index = qdict_get_int(qdict, "cpu_index");
2121 int bank = qdict_get_int(qdict, "bank");
2122 uint64_t status = qdict_get_int(qdict, "status");
2123 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2124 uint64_t addr = qdict_get_int(qdict, "addr");
2125 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002126 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002127
Jan Kiszka747461c2011-03-02 08:56:10 +01002128 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2129 flags |= MCE_INJECT_BROADCAST;
2130 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002131 cs = qemu_get_cpu(cpu_index);
2132 if (cs != NULL) {
2133 cpu = X86_CPU(cs);
2134 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2135 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002136 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002137}
2138#endif
2139
Corey Bryant208c9d12012-06-22 14:36:09 -04002140void qmp_getfd(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 int fd;
2144
Corey Bryant208c9d12012-06-22 14:36:09 -04002145 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002146 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002147 error_set(errp, QERR_FD_NOT_SUPPLIED);
2148 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002149 }
2150
2151 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002152 close(fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002153 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2154 "a name not starting with a digit");
2155 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002156 }
2157
Corey Bryant208c9d12012-06-22 14:36:09 -04002158 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002159 if (strcmp(monfd->name, fdname) != 0) {
2160 continue;
2161 }
2162
2163 close(monfd->fd);
2164 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002165 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002166 }
2167
Anthony Liguori7267c092011-08-20 22:09:37 -05002168 monfd = g_malloc0(sizeof(mon_fd_t));
2169 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002170 monfd->fd = fd;
2171
Corey Bryant208c9d12012-06-22 14:36:09 -04002172 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002173}
2174
Corey Bryant208c9d12012-06-22 14:36:09 -04002175void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002176{
Anthony Liguoric227f092009-10-01 16:12:16 -05002177 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002178
Corey Bryant208c9d12012-06-22 14:36:09 -04002179 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002180 if (strcmp(monfd->name, fdname) != 0) {
2181 continue;
2182 }
2183
Blue Swirl72cf2d42009-09-12 07:36:22 +00002184 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002185 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002186 g_free(monfd->name);
2187 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002188 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002189 }
2190
Corey Bryant208c9d12012-06-22 14:36:09 -04002191 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002192}
2193
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002194static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002195{
Luiz Capitulino13548692011-07-29 15:36:43 -03002196 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002197 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002198
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002199 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002200
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002201 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002202 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002203 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002204}
2205
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002206int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002207{
Anthony Liguoric227f092009-10-01 16:12:16 -05002208 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002209
Blue Swirl72cf2d42009-09-12 07:36:22 +00002210 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002211 int fd;
2212
2213 if (strcmp(monfd->name, fdname) != 0) {
2214 continue;
2215 }
2216
2217 fd = monfd->fd;
2218
2219 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002220 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002221 g_free(monfd->name);
2222 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002223
2224 return fd;
2225 }
2226
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002227 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002228 return -1;
2229}
2230
Corey Bryantba1c0482012-08-14 16:43:43 -04002231static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2232{
2233 MonFdsetFd *mon_fdset_fd;
2234 MonFdsetFd *mon_fdset_fd_next;
2235
2236 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002237 if ((mon_fdset_fd->removed ||
2238 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2239 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002240 close(mon_fdset_fd->fd);
2241 g_free(mon_fdset_fd->opaque);
2242 QLIST_REMOVE(mon_fdset_fd, next);
2243 g_free(mon_fdset_fd);
2244 }
2245 }
2246
Corey Bryantadb696f2012-08-14 16:43:47 -04002247 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002248 QLIST_REMOVE(mon_fdset, next);
2249 g_free(mon_fdset);
2250 }
2251}
2252
Corey Bryantefb87c12012-08-14 16:43:48 -04002253static void monitor_fdsets_cleanup(void)
2254{
2255 MonFdset *mon_fdset;
2256 MonFdset *mon_fdset_next;
2257
2258 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2259 monitor_fdset_cleanup(mon_fdset);
2260 }
2261}
2262
Corey Bryantba1c0482012-08-14 16:43:43 -04002263AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2264 const char *opaque, Error **errp)
2265{
2266 int fd;
2267 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002268 AddfdInfo *fdinfo;
2269
2270 fd = qemu_chr_fe_get_msgfd(mon->chr);
2271 if (fd == -1) {
2272 error_set(errp, QERR_FD_NOT_SUPPLIED);
2273 goto error;
2274 }
2275
Corey Bryante446f702012-10-18 15:19:32 -04002276 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2277 has_opaque, opaque, errp);
2278 if (fdinfo) {
2279 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002280 }
2281
Corey Bryantba1c0482012-08-14 16:43:43 -04002282error:
2283 if (fd != -1) {
2284 close(fd);
2285 }
2286 return NULL;
2287}
2288
2289void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2290{
2291 MonFdset *mon_fdset;
2292 MonFdsetFd *mon_fdset_fd;
2293 char fd_str[60];
2294
2295 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2296 if (mon_fdset->id != fdset_id) {
2297 continue;
2298 }
2299 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2300 if (has_fd) {
2301 if (mon_fdset_fd->fd != fd) {
2302 continue;
2303 }
2304 mon_fdset_fd->removed = true;
2305 break;
2306 } else {
2307 mon_fdset_fd->removed = true;
2308 }
2309 }
2310 if (has_fd && !mon_fdset_fd) {
2311 goto error;
2312 }
2313 monitor_fdset_cleanup(mon_fdset);
2314 return;
2315 }
2316
2317error:
2318 if (has_fd) {
2319 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2320 fdset_id, fd);
2321 } else {
2322 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2323 }
2324 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2325}
2326
2327FdsetInfoList *qmp_query_fdsets(Error **errp)
2328{
2329 MonFdset *mon_fdset;
2330 MonFdsetFd *mon_fdset_fd;
2331 FdsetInfoList *fdset_list = NULL;
2332
2333 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2334 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2335 FdsetFdInfoList *fdsetfd_list = NULL;
2336
2337 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2338 fdset_info->value->fdset_id = mon_fdset->id;
2339
2340 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2341 FdsetFdInfoList *fdsetfd_info;
2342
2343 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2344 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2345 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2346 if (mon_fdset_fd->opaque) {
2347 fdsetfd_info->value->has_opaque = true;
2348 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2349 } else {
2350 fdsetfd_info->value->has_opaque = false;
2351 }
2352
2353 fdsetfd_info->next = fdsetfd_list;
2354 fdsetfd_list = fdsetfd_info;
2355 }
2356
2357 fdset_info->value->fds = fdsetfd_list;
2358
2359 fdset_info->next = fdset_list;
2360 fdset_list = fdset_info;
2361 }
2362
2363 return fdset_list;
2364}
2365
Corey Bryante446f702012-10-18 15:19:32 -04002366AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2367 bool has_opaque, const char *opaque,
2368 Error **errp)
2369{
2370 MonFdset *mon_fdset = NULL;
2371 MonFdsetFd *mon_fdset_fd;
2372 AddfdInfo *fdinfo;
2373
2374 if (has_fdset_id) {
2375 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2376 /* Break if match found or match impossible due to ordering by ID */
2377 if (fdset_id <= mon_fdset->id) {
2378 if (fdset_id < mon_fdset->id) {
2379 mon_fdset = NULL;
2380 }
2381 break;
2382 }
2383 }
2384 }
2385
2386 if (mon_fdset == NULL) {
2387 int64_t fdset_id_prev = -1;
2388 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2389
2390 if (has_fdset_id) {
2391 if (fdset_id < 0) {
2392 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2393 "a non-negative value");
2394 return NULL;
2395 }
2396 /* Use specified fdset ID */
2397 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2398 mon_fdset_cur = mon_fdset;
2399 if (fdset_id < mon_fdset_cur->id) {
2400 break;
2401 }
2402 }
2403 } else {
2404 /* Use first available fdset ID */
2405 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2406 mon_fdset_cur = mon_fdset;
2407 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2408 fdset_id_prev = mon_fdset_cur->id;
2409 continue;
2410 }
2411 break;
2412 }
2413 }
2414
2415 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2416 if (has_fdset_id) {
2417 mon_fdset->id = fdset_id;
2418 } else {
2419 mon_fdset->id = fdset_id_prev + 1;
2420 }
2421
2422 /* The fdset list is ordered by fdset ID */
2423 if (!mon_fdset_cur) {
2424 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2425 } else if (mon_fdset->id < mon_fdset_cur->id) {
2426 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2427 } else {
2428 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2429 }
2430 }
2431
2432 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2433 mon_fdset_fd->fd = fd;
2434 mon_fdset_fd->removed = false;
2435 if (has_opaque) {
2436 mon_fdset_fd->opaque = g_strdup(opaque);
2437 }
2438 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2439
2440 fdinfo = g_malloc0(sizeof(*fdinfo));
2441 fdinfo->fdset_id = mon_fdset->id;
2442 fdinfo->fd = mon_fdset_fd->fd;
2443
2444 return fdinfo;
2445}
2446
Corey Bryantadb696f2012-08-14 16:43:47 -04002447int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2448{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002449#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002450 MonFdset *mon_fdset;
2451 MonFdsetFd *mon_fdset_fd;
2452 int mon_fd_flags;
2453
Corey Bryantadb696f2012-08-14 16:43:47 -04002454 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2455 if (mon_fdset->id != fdset_id) {
2456 continue;
2457 }
2458 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2459 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2460 if (mon_fd_flags == -1) {
2461 return -1;
2462 }
2463
2464 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2465 return mon_fdset_fd->fd;
2466 }
2467 }
2468 errno = EACCES;
2469 return -1;
2470 }
2471#endif
2472
2473 errno = ENOENT;
2474 return -1;
2475}
2476
2477int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2478{
2479 MonFdset *mon_fdset;
2480 MonFdsetFd *mon_fdset_fd_dup;
2481
2482 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2483 if (mon_fdset->id != fdset_id) {
2484 continue;
2485 }
2486 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2487 if (mon_fdset_fd_dup->fd == dup_fd) {
2488 return -1;
2489 }
2490 }
2491 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2492 mon_fdset_fd_dup->fd = dup_fd;
2493 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2494 return 0;
2495 }
2496 return -1;
2497}
2498
2499static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2500{
2501 MonFdset *mon_fdset;
2502 MonFdsetFd *mon_fdset_fd_dup;
2503
2504 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2505 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2506 if (mon_fdset_fd_dup->fd == dup_fd) {
2507 if (remove) {
2508 QLIST_REMOVE(mon_fdset_fd_dup, next);
2509 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2510 monitor_fdset_cleanup(mon_fdset);
2511 }
2512 }
2513 return mon_fdset->id;
2514 }
2515 }
2516 }
2517 return -1;
2518}
2519
2520int monitor_fdset_dup_fd_find(int dup_fd)
2521{
2522 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2523}
2524
2525int monitor_fdset_dup_fd_remove(int dup_fd)
2526{
2527 return monitor_fdset_dup_fd_find_remove(dup_fd, true);
2528}
2529
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002530int monitor_handle_fd_param(Monitor *mon, const char *fdname)
2531{
2532 int fd;
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002533 Error *local_err = NULL;
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002534
Laszlo Ersek59063662014-04-10 10:24:31 +02002535 fd = monitor_handle_fd_param2(mon, fdname, &local_err);
2536 if (local_err) {
2537 qerror_report_err(local_err);
2538 error_free(local_err);
2539 }
2540 return fd;
2541}
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002542
Laszlo Ersek59063662014-04-10 10:24:31 +02002543int monitor_handle_fd_param2(Monitor *mon, const char *fdname, Error **errp)
2544{
2545 int fd;
2546 Error *local_err = NULL;
2547
2548 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002549 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002550 } else {
2551 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002552 if (fd == -1) {
2553 error_setg(&local_err, "Invalid file descriptor number '%s'",
2554 fdname);
2555 }
2556 }
2557 if (local_err) {
2558 error_propagate(errp, local_err);
2559 assert(fd == -1);
2560 } else {
2561 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002562 }
2563
2564 return fd;
2565}
2566
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002567/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002568static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002569 {
2570 .name = "version",
2571 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002572 .params = "",
2573 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002574 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002575 },
2576 {
2577 .name = "network",
2578 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002579 .params = "",
2580 .help = "show the network state",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002581 .mhandler.cmd = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002582 },
2583 {
2584 .name = "chardev",
2585 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002586 .params = "",
2587 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002588 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002589 },
2590 {
2591 .name = "block",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002592 .args_type = "verbose:-v,device:B?",
2593 .params = "[-v] [device]",
2594 .help = "show info of one block device or all block devices "
2595 "(and details of images with -v option)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002596 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002597 },
2598 {
2599 .name = "blockstats",
2600 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002601 .params = "",
2602 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002603 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002604 },
2605 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002606 .name = "block-jobs",
2607 .args_type = "",
2608 .params = "",
2609 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002610 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002611 },
2612 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002613 .name = "registers",
2614 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002615 .params = "",
2616 .help = "show the cpu registers",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002617 .mhandler.cmd = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002618 },
2619 {
2620 .name = "cpus",
2621 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002622 .params = "",
2623 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002624 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002625 },
2626 {
2627 .name = "history",
2628 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002629 .params = "",
2630 .help = "show the command line history",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002631 .mhandler.cmd = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002632 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002633#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2634 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002635 {
2636 .name = "irq",
2637 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002638 .params = "",
2639 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002640#ifdef TARGET_SPARC
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002641 .mhandler.cmd = sun4m_irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002642#elif defined(TARGET_LM32)
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002643 .mhandler.cmd = lm32_irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002644#else
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002645 .mhandler.cmd = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002646#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002647 },
2648 {
2649 .name = "pic",
2650 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002651 .params = "",
2652 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002653#ifdef TARGET_SPARC
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002654 .mhandler.cmd = sun4m_pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002655#elif defined(TARGET_LM32)
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002656 .mhandler.cmd = lm32_do_pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002657#else
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002658 .mhandler.cmd = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002659#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002660 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002661#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002662 {
2663 .name = "pci",
2664 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002665 .params = "",
2666 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002667 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002668 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002669#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002670 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 {
2672 .name = "tlb",
2673 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002674 .params = "",
2675 .help = "show virtual to physical memory mappings",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002676 .mhandler.cmd = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002677 },
aurel327c664e22009-03-03 06:12:22 +00002678#endif
2679#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002680 {
2681 .name = "mem",
2682 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002683 .params = "",
2684 .help = "show the active virtual memory mappings",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002685 .mhandler.cmd = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002686 },
bellardb86bda52004-09-18 19:32:46 +00002687#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002688 {
Blue Swirl314e2982011-09-11 20:22:05 +00002689 .name = "mtree",
2690 .args_type = "",
2691 .params = "",
2692 .help = "show memory tree",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002693 .mhandler.cmd = do_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002694 },
2695 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002696 .name = "jit",
2697 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002698 .params = "",
2699 .help = "show dynamic compiler info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002700 .mhandler.cmd = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002701 },
2702 {
2703 .name = "kvm",
2704 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002705 .params = "",
2706 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002707 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002708 },
2709 {
2710 .name = "numa",
2711 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002712 .params = "",
2713 .help = "show NUMA information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002714 .mhandler.cmd = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002715 },
2716 {
2717 .name = "usb",
2718 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002719 .params = "",
2720 .help = "show guest USB devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002721 .mhandler.cmd = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002722 },
2723 {
2724 .name = "usbhost",
2725 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002726 .params = "",
2727 .help = "show host USB devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002728 .mhandler.cmd = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002729 },
2730 {
2731 .name = "profile",
2732 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002733 .params = "",
2734 .help = "show profiling information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002735 .mhandler.cmd = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002736 },
2737 {
2738 .name = "capture",
2739 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002740 .params = "",
2741 .help = "show capture information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002742 .mhandler.cmd = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002743 },
2744 {
2745 .name = "snapshots",
2746 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002747 .params = "",
2748 .help = "show the currently saved VM snapshots",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002749 .mhandler.cmd = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002750 },
2751 {
2752 .name = "status",
2753 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002754 .params = "",
2755 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002756 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002757 },
2758 {
2759 .name = "pcmcia",
2760 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002761 .params = "",
2762 .help = "show guest PCMCIA status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002763 .mhandler.cmd = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 },
2765 {
2766 .name = "mice",
2767 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002768 .params = "",
2769 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002770 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002771 },
2772 {
2773 .name = "vnc",
2774 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002775 .params = "",
2776 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002777 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002778 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002779#if defined(CONFIG_SPICE)
2780 {
2781 .name = "spice",
2782 .args_type = "",
2783 .params = "",
2784 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002785 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002786 },
2787#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002788 {
2789 .name = "name",
2790 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002791 .params = "",
2792 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002793 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002794 },
2795 {
2796 .name = "uuid",
2797 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002798 .params = "",
2799 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002800 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002801 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002802 {
2803 .name = "cpustats",
2804 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002805 .params = "",
2806 .help = "show CPU statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002807 .mhandler.cmd = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002808 },
blueswir131a60e22007-10-26 18:42:59 +00002809#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002810 {
2811 .name = "usernet",
2812 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002813 .params = "",
2814 .help = "show user network stack connection states",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002815 .mhandler.cmd = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002816 },
blueswir131a60e22007-10-26 18:42:59 +00002817#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002818 {
2819 .name = "migrate",
2820 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002821 .params = "",
2822 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002823 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002824 },
2825 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002826 .name = "migrate_capabilities",
2827 .args_type = "",
2828 .params = "",
2829 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002830 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002831 },
2832 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002833 .name = "migrate_cache_size",
2834 .args_type = "",
2835 .params = "",
2836 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002837 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002838 },
2839 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002840 .name = "balloon",
2841 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002842 .params = "",
2843 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002844 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002845 },
2846 {
2847 .name = "qtree",
2848 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002849 .params = "",
2850 .help = "show device tree",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002851 .mhandler.cmd = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002852 },
2853 {
2854 .name = "qdm",
2855 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002856 .params = "",
2857 .help = "show qdev device model list",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002858 .mhandler.cmd = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002859 },
2860 {
2861 .name = "roms",
2862 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002863 .params = "",
2864 .help = "show roms",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002865 .mhandler.cmd = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002866 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302867 {
2868 .name = "trace-events",
2869 .args_type = "",
2870 .params = "",
2871 .help = "show available trace-events & their state",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002872 .mhandler.cmd = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302873 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002874 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002875 .name = "tpm",
2876 .args_type = "",
2877 .params = "",
2878 .help = "show the TPM device",
2879 .mhandler.cmd = hmp_info_tpm,
2880 },
2881 {
Hu Taoeb1539b2014-05-14 17:43:35 +08002882 .name = "memdev",
2883 .args_type = "",
2884 .params = "",
2885 .help = "show the memory device",
2886 .mhandler.cmd = hmp_info_memdev,
2887 },
2888 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002889 .name = NULL,
2890 },
bellard9dc39cb2004-03-14 21:38:27 +00002891};
2892
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002893/* mon_cmds and info_cmds would be sorted at runtime */
2894static mon_cmd_t mon_cmds[] = {
2895#include "hmp-commands.h"
2896 { NULL, NULL, },
2897};
2898
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002899static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002900#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002901 { /* NULL */ },
2902};
2903
bellard9307c4c2004-04-04 12:57:25 +00002904/*******************************************************************/
2905
2906static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002907static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002908
bellard92a31b12005-02-10 22:00:52 +00002909#define MD_TLONG 0
2910#define MD_I32 1
2911
bellard9307c4c2004-04-04 12:57:25 +00002912typedef struct MonitorDef {
2913 const char *name;
2914 int offset;
blueswir18662d652008-10-02 18:32:44 +00002915 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002916 int type;
bellard9307c4c2004-04-04 12:57:25 +00002917} MonitorDef;
2918
bellard57206fd2004-04-25 18:54:52 +00002919#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002920static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002921{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002922 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002923 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002924}
2925#endif
2926
bellarda541f292004-04-12 20:39:29 +00002927#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002928static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002929{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002930 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002931 unsigned int u;
2932 int i;
2933
2934 u = 0;
2935 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002936 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002937
2938 return u;
2939}
2940
blueswir18662d652008-10-02 18:32:44 +00002941static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002942{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002943 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002944 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002945}
2946
blueswir18662d652008-10-02 18:32:44 +00002947static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002948{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002949 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002950 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002951}
bellard9fddaa02004-05-21 12:59:32 +00002952
blueswir18662d652008-10-02 18:32:44 +00002953static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002954{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002955 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002956 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002957}
2958
blueswir18662d652008-10-02 18:32:44 +00002959static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002960{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002961 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002962 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002963}
2964
blueswir18662d652008-10-02 18:32:44 +00002965static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002966{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002967 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002968 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002969}
bellarda541f292004-04-12 20:39:29 +00002970#endif
2971
bellarde95c8d52004-09-30 22:22:08 +00002972#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002973#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002974static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002975{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002976 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002977
2978 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002979}
bellard7b936c02005-10-30 17:05:13 +00002980#endif
bellarde95c8d52004-09-30 22:22:08 +00002981
blueswir18662d652008-10-02 18:32:44 +00002982static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002983{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002984 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002985 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002986}
2987#endif
2988
blueswir18662d652008-10-02 18:32:44 +00002989static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002990#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002991
2992#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01002993 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2994 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2995 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002996
Andreas Färbere59d1672012-02-16 00:40:47 +01002997 { "eax", offsetof(CPUX86State, regs[0]) },
2998 { "ecx", offsetof(CPUX86State, regs[1]) },
2999 { "edx", offsetof(CPUX86State, regs[2]) },
3000 { "ebx", offsetof(CPUX86State, regs[3]) },
3001 { "esp|sp", offsetof(CPUX86State, regs[4]) },
3002 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
3003 { "esi", offsetof(CPUX86State, regs[6]) },
3004 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003005#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01003006 { "r8", offsetof(CPUX86State, regs[8]) },
3007 { "r9", offsetof(CPUX86State, regs[9]) },
3008 { "r10", offsetof(CPUX86State, regs[10]) },
3009 { "r11", offsetof(CPUX86State, regs[11]) },
3010 { "r12", offsetof(CPUX86State, regs[12]) },
3011 { "r13", offsetof(CPUX86State, regs[13]) },
3012 { "r14", offsetof(CPUX86State, regs[14]) },
3013 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00003014#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003015 { "eflags", offsetof(CPUX86State, eflags) },
3016 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00003017 SEG("cs", R_CS)
3018 SEG("ds", R_DS)
3019 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003020 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003021 SEG("fs", R_FS)
3022 SEG("gs", R_GS)
3023 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003024#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003025 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003026 { "r0", offsetof(CPUPPCState, gpr[0]) },
3027 { "r1", offsetof(CPUPPCState, gpr[1]) },
3028 { "r2", offsetof(CPUPPCState, gpr[2]) },
3029 { "r3", offsetof(CPUPPCState, gpr[3]) },
3030 { "r4", offsetof(CPUPPCState, gpr[4]) },
3031 { "r5", offsetof(CPUPPCState, gpr[5]) },
3032 { "r6", offsetof(CPUPPCState, gpr[6]) },
3033 { "r7", offsetof(CPUPPCState, gpr[7]) },
3034 { "r8", offsetof(CPUPPCState, gpr[8]) },
3035 { "r9", offsetof(CPUPPCState, gpr[9]) },
3036 { "r10", offsetof(CPUPPCState, gpr[10]) },
3037 { "r11", offsetof(CPUPPCState, gpr[11]) },
3038 { "r12", offsetof(CPUPPCState, gpr[12]) },
3039 { "r13", offsetof(CPUPPCState, gpr[13]) },
3040 { "r14", offsetof(CPUPPCState, gpr[14]) },
3041 { "r15", offsetof(CPUPPCState, gpr[15]) },
3042 { "r16", offsetof(CPUPPCState, gpr[16]) },
3043 { "r17", offsetof(CPUPPCState, gpr[17]) },
3044 { "r18", offsetof(CPUPPCState, gpr[18]) },
3045 { "r19", offsetof(CPUPPCState, gpr[19]) },
3046 { "r20", offsetof(CPUPPCState, gpr[20]) },
3047 { "r21", offsetof(CPUPPCState, gpr[21]) },
3048 { "r22", offsetof(CPUPPCState, gpr[22]) },
3049 { "r23", offsetof(CPUPPCState, gpr[23]) },
3050 { "r24", offsetof(CPUPPCState, gpr[24]) },
3051 { "r25", offsetof(CPUPPCState, gpr[25]) },
3052 { "r26", offsetof(CPUPPCState, gpr[26]) },
3053 { "r27", offsetof(CPUPPCState, gpr[27]) },
3054 { "r28", offsetof(CPUPPCState, gpr[28]) },
3055 { "r29", offsetof(CPUPPCState, gpr[29]) },
3056 { "r30", offsetof(CPUPPCState, gpr[30]) },
3057 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003058 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003059 { "f0", offsetof(CPUPPCState, fpr[0]) },
3060 { "f1", offsetof(CPUPPCState, fpr[1]) },
3061 { "f2", offsetof(CPUPPCState, fpr[2]) },
3062 { "f3", offsetof(CPUPPCState, fpr[3]) },
3063 { "f4", offsetof(CPUPPCState, fpr[4]) },
3064 { "f5", offsetof(CPUPPCState, fpr[5]) },
3065 { "f6", offsetof(CPUPPCState, fpr[6]) },
3066 { "f7", offsetof(CPUPPCState, fpr[7]) },
3067 { "f8", offsetof(CPUPPCState, fpr[8]) },
3068 { "f9", offsetof(CPUPPCState, fpr[9]) },
3069 { "f10", offsetof(CPUPPCState, fpr[10]) },
3070 { "f11", offsetof(CPUPPCState, fpr[11]) },
3071 { "f12", offsetof(CPUPPCState, fpr[12]) },
3072 { "f13", offsetof(CPUPPCState, fpr[13]) },
3073 { "f14", offsetof(CPUPPCState, fpr[14]) },
3074 { "f15", offsetof(CPUPPCState, fpr[15]) },
3075 { "f16", offsetof(CPUPPCState, fpr[16]) },
3076 { "f17", offsetof(CPUPPCState, fpr[17]) },
3077 { "f18", offsetof(CPUPPCState, fpr[18]) },
3078 { "f19", offsetof(CPUPPCState, fpr[19]) },
3079 { "f20", offsetof(CPUPPCState, fpr[20]) },
3080 { "f21", offsetof(CPUPPCState, fpr[21]) },
3081 { "f22", offsetof(CPUPPCState, fpr[22]) },
3082 { "f23", offsetof(CPUPPCState, fpr[23]) },
3083 { "f24", offsetof(CPUPPCState, fpr[24]) },
3084 { "f25", offsetof(CPUPPCState, fpr[25]) },
3085 { "f26", offsetof(CPUPPCState, fpr[26]) },
3086 { "f27", offsetof(CPUPPCState, fpr[27]) },
3087 { "f28", offsetof(CPUPPCState, fpr[28]) },
3088 { "f29", offsetof(CPUPPCState, fpr[29]) },
3089 { "f30", offsetof(CPUPPCState, fpr[30]) },
3090 { "f31", offsetof(CPUPPCState, fpr[31]) },
3091 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003092 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003093 { "nip|pc", offsetof(CPUPPCState, nip) },
3094 { "lr", offsetof(CPUPPCState, lr) },
3095 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003096 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003097 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003098 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003099 { "msr", 0, &monitor_get_msr, },
3100 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003101 { "tbu", 0, &monitor_get_tbu, },
3102 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003103 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003104 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3105 { "sr0", offsetof(CPUPPCState, sr[0]) },
3106 { "sr1", offsetof(CPUPPCState, sr[1]) },
3107 { "sr2", offsetof(CPUPPCState, sr[2]) },
3108 { "sr3", offsetof(CPUPPCState, sr[3]) },
3109 { "sr4", offsetof(CPUPPCState, sr[4]) },
3110 { "sr5", offsetof(CPUPPCState, sr[5]) },
3111 { "sr6", offsetof(CPUPPCState, sr[6]) },
3112 { "sr7", offsetof(CPUPPCState, sr[7]) },
3113 { "sr8", offsetof(CPUPPCState, sr[8]) },
3114 { "sr9", offsetof(CPUPPCState, sr[9]) },
3115 { "sr10", offsetof(CPUPPCState, sr[10]) },
3116 { "sr11", offsetof(CPUPPCState, sr[11]) },
3117 { "sr12", offsetof(CPUPPCState, sr[12]) },
3118 { "sr13", offsetof(CPUPPCState, sr[13]) },
3119 { "sr14", offsetof(CPUPPCState, sr[14]) },
3120 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003121 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003122 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003123
Andreas Färbere59d1672012-02-16 00:40:47 +01003124 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3125 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
Tom Musta04f1f782013-09-25 17:41:13 +10003126 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3127 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3128 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
Andreas Färbere59d1672012-02-16 00:40:47 +01003129 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3130 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3131 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3132 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3133 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3134 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3135 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3136 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3137 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3138 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3139 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3140 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3141 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3142 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3143 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3144 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3145 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3146 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3147 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3148 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3149 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3150 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3151 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3152 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3153 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3154 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3155 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3156 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3157 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3158 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3159 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3160 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3161 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3162 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3163 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3164 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3165 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3166 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3167 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3168 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3169 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3170 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3171 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3172 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3173 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3174 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3175 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3176 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3177 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3178 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3179 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3180 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3181 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3182 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3183 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3184 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3185 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3186 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3187 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3188 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3189 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3190 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3191 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3192 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003193
bellarde95c8d52004-09-30 22:22:08 +00003194#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003195 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3196 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3197 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3198 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3199 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3200 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3201 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3202 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003203 { "o0", 0, monitor_get_reg },
3204 { "o1", 1, monitor_get_reg },
3205 { "o2", 2, monitor_get_reg },
3206 { "o3", 3, monitor_get_reg },
3207 { "o4", 4, monitor_get_reg },
3208 { "o5", 5, monitor_get_reg },
3209 { "o6", 6, monitor_get_reg },
3210 { "o7", 7, monitor_get_reg },
3211 { "l0", 8, monitor_get_reg },
3212 { "l1", 9, monitor_get_reg },
3213 { "l2", 10, monitor_get_reg },
3214 { "l3", 11, monitor_get_reg },
3215 { "l4", 12, monitor_get_reg },
3216 { "l5", 13, monitor_get_reg },
3217 { "l6", 14, monitor_get_reg },
3218 { "l7", 15, monitor_get_reg },
3219 { "i0", 16, monitor_get_reg },
3220 { "i1", 17, monitor_get_reg },
3221 { "i2", 18, monitor_get_reg },
3222 { "i3", 19, monitor_get_reg },
3223 { "i4", 20, monitor_get_reg },
3224 { "i5", 21, monitor_get_reg },
3225 { "i6", 22, monitor_get_reg },
3226 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003227 { "pc", offsetof(CPUSPARCState, pc) },
3228 { "npc", offsetof(CPUSPARCState, npc) },
3229 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003230#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003231 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003232 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003233#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003234 { "tbr", offsetof(CPUSPARCState, tbr) },
3235 { "fsr", offsetof(CPUSPARCState, fsr) },
3236 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3237 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3238 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3239 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3240 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3241 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3242 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3243 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3244 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3245 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3246 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3247 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3248 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3249 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3250 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3251 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3252 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3253 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3254 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3255 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3256 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3257 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3258 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3259 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3260 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3261 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3262 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3263 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3264 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3265 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3266 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3267 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003268#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003269 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3270 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3271 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3272 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3273 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3274 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3275 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3276 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3277 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3278 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3279 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3280 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3281 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3282 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3283 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3284 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3285 { "asi", offsetof(CPUSPARCState, asi) },
3286 { "pstate", offsetof(CPUSPARCState, pstate) },
3287 { "cansave", offsetof(CPUSPARCState, cansave) },
3288 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3289 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3290 { "wstate", offsetof(CPUSPARCState, wstate) },
3291 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3292 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003293#endif
bellard9307c4c2004-04-04 12:57:25 +00003294#endif
3295 { NULL },
3296};
3297
Stefan Weil9c3175c2013-08-22 21:30:09 +02003298static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3299expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003300{
Fam Zheng277acfe2013-08-20 10:58:21 +08003301 va_list ap;
3302 va_start(ap, fmt);
3303 monitor_vprintf(mon, fmt, ap);
3304 monitor_printf(mon, "\n");
3305 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003306 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003307}
3308
Markus Armbruster09b94182010-01-20 13:07:30 +01003309/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003310static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003311{
blueswir18662d652008-10-02 18:32:44 +00003312 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003313 void *ptr;
3314
bellard9307c4c2004-04-04 12:57:25 +00003315 for(md = monitor_defs; md->name != NULL; md++) {
3316 if (compare_cmd(name, md->name)) {
3317 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003318 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003319 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003320 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003321 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003322 switch(md->type) {
3323 case MD_I32:
3324 *pval = *(int32_t *)ptr;
3325 break;
3326 case MD_TLONG:
3327 *pval = *(target_long *)ptr;
3328 break;
3329 default:
3330 *pval = 0;
3331 break;
3332 }
bellard9307c4c2004-04-04 12:57:25 +00003333 }
3334 return 0;
3335 }
3336 }
3337 return -1;
3338}
3339
3340static void next(void)
3341{
Blue Swirl660f11b2009-07-31 21:16:51 +00003342 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003343 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003344 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003345 pch++;
3346 }
3347}
3348
aliguori376253e2009-03-05 23:01:23 +00003349static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003350
aliguori376253e2009-03-05 23:01:23 +00003351static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003352{
blueswir1c2efc952007-09-25 17:28:42 +00003353 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003354 char *p;
bellard6a00d602005-11-21 23:25:50 +00003355 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003356
3357 switch(*pch) {
3358 case '+':
3359 next();
aliguori376253e2009-03-05 23:01:23 +00003360 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003361 break;
3362 case '-':
3363 next();
aliguori376253e2009-03-05 23:01:23 +00003364 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003365 break;
3366 case '~':
3367 next();
aliguori376253e2009-03-05 23:01:23 +00003368 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003369 break;
3370 case '(':
3371 next();
aliguori376253e2009-03-05 23:01:23 +00003372 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003373 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003374 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003375 }
3376 next();
3377 break;
bellard81d09122004-07-14 17:21:37 +00003378 case '\'':
3379 pch++;
3380 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003381 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003382 n = *pch;
3383 pch++;
3384 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003385 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003386 next();
3387 break;
bellard9307c4c2004-04-04 12:57:25 +00003388 case '$':
3389 {
3390 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003391 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003392
bellard9307c4c2004-04-04 12:57:25 +00003393 pch++;
3394 q = buf;
3395 while ((*pch >= 'a' && *pch <= 'z') ||
3396 (*pch >= 'A' && *pch <= 'Z') ||
3397 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003398 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003399 if ((q - buf) < sizeof(buf) - 1)
3400 *q++ = *pch;
3401 pch++;
3402 }
blueswir1cd390082008-11-16 13:53:32 +00003403 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003404 pch++;
3405 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003406 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003407 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003408 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003409 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003410 }
3411 break;
3412 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003413 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003414 n = 0;
3415 break;
3416 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003417 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003418 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003419 if (errno == ERANGE) {
3420 expr_error(mon, "number too large");
3421 }
bellard9307c4c2004-04-04 12:57:25 +00003422 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003423 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003424 }
3425 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003426 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003427 pch++;
3428 break;
3429 }
3430 return n;
3431}
3432
3433
aliguori376253e2009-03-05 23:01:23 +00003434static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003435{
blueswir1c2efc952007-09-25 17:28:42 +00003436 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003437 int op;
ths3b46e622007-09-17 08:09:54 +00003438
aliguori376253e2009-03-05 23:01:23 +00003439 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003440 for(;;) {
3441 op = *pch;
3442 if (op != '*' && op != '/' && op != '%')
3443 break;
3444 next();
aliguori376253e2009-03-05 23:01:23 +00003445 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003446 switch(op) {
3447 default:
3448 case '*':
3449 val *= val2;
3450 break;
3451 case '/':
3452 case '%':
ths5fafdf22007-09-16 21:08:06 +00003453 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003454 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003455 if (op == '/')
3456 val /= val2;
3457 else
3458 val %= val2;
3459 break;
3460 }
3461 }
3462 return val;
3463}
3464
aliguori376253e2009-03-05 23:01:23 +00003465static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003466{
blueswir1c2efc952007-09-25 17:28:42 +00003467 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003468 int op;
bellard9307c4c2004-04-04 12:57:25 +00003469
aliguori376253e2009-03-05 23:01:23 +00003470 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003471 for(;;) {
3472 op = *pch;
3473 if (op != '&' && op != '|' && op != '^')
3474 break;
3475 next();
aliguori376253e2009-03-05 23:01:23 +00003476 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003477 switch(op) {
3478 default:
3479 case '&':
3480 val &= val2;
3481 break;
3482 case '|':
3483 val |= val2;
3484 break;
3485 case '^':
3486 val ^= val2;
3487 break;
3488 }
3489 }
3490 return val;
3491}
3492
aliguori376253e2009-03-05 23:01:23 +00003493static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003494{
blueswir1c2efc952007-09-25 17:28:42 +00003495 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003496 int op;
bellard9307c4c2004-04-04 12:57:25 +00003497
aliguori376253e2009-03-05 23:01:23 +00003498 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003499 for(;;) {
3500 op = *pch;
3501 if (op != '+' && op != '-')
3502 break;
3503 next();
aliguori376253e2009-03-05 23:01:23 +00003504 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003505 if (op == '+')
3506 val += val2;
3507 else
3508 val -= val2;
3509 }
3510 return val;
3511}
3512
aliguori376253e2009-03-05 23:01:23 +00003513static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003514{
3515 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003516 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003517 *pp = pch;
3518 return -1;
3519 }
blueswir1cd390082008-11-16 13:53:32 +00003520 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003521 pch++;
aliguori376253e2009-03-05 23:01:23 +00003522 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003523 *pp = pch;
3524 return 0;
3525}
3526
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003527static int get_double(Monitor *mon, double *pval, const char **pp)
3528{
3529 const char *p = *pp;
3530 char *tailp;
3531 double d;
3532
3533 d = strtod(p, &tailp);
3534 if (tailp == p) {
3535 monitor_printf(mon, "Number expected\n");
3536 return -1;
3537 }
3538 if (d != d || d - d != 0) {
3539 /* NaN or infinity */
3540 monitor_printf(mon, "Bad number\n");
3541 return -1;
3542 }
3543 *pval = d;
3544 *pp = tailp;
3545 return 0;
3546}
3547
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003548/*
3549 * Store the command-name in cmdname, and return a pointer to
3550 * the remaining of the command string.
3551 */
3552static const char *get_command_name(const char *cmdline,
3553 char *cmdname, size_t nlen)
3554{
3555 size_t len;
3556 const char *p, *pstart;
3557
3558 p = cmdline;
3559 while (qemu_isspace(*p))
3560 p++;
3561 if (*p == '\0')
3562 return NULL;
3563 pstart = p;
3564 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3565 p++;
3566 len = p - pstart;
3567 if (len > nlen - 1)
3568 len = nlen - 1;
3569 memcpy(cmdname, pstart, len);
3570 cmdname[len] = '\0';
3571 return p;
3572}
3573
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003574/**
3575 * Read key of 'type' into 'key' and return the current
3576 * 'type' pointer.
3577 */
3578static char *key_get_info(const char *type, char **key)
3579{
3580 size_t len;
3581 char *p, *str;
3582
3583 if (*type == ',')
3584 type++;
3585
3586 p = strchr(type, ':');
3587 if (!p) {
3588 *key = NULL;
3589 return NULL;
3590 }
3591 len = p - type;
3592
Anthony Liguori7267c092011-08-20 22:09:37 -05003593 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003594 memcpy(str, type, len);
3595 str[len] = '\0';
3596
3597 *key = str;
3598 return ++p;
3599}
3600
bellard9307c4c2004-04-04 12:57:25 +00003601static int default_fmt_format = 'x';
3602static int default_fmt_size = 4;
3603
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003604static int is_valid_option(const char *c, const char *typestr)
3605{
3606 char option[3];
3607
3608 option[0] = '-';
3609 option[1] = *c;
3610 option[2] = '\0';
3611
3612 typestr = strstr(typestr, option);
3613 return (typestr != NULL);
3614}
3615
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003616static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3617 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003618{
3619 const mon_cmd_t *cmd;
3620
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003621 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003622 if (compare_cmd(cmdname, cmd->name)) {
3623 return cmd;
3624 }
3625 }
3626
3627 return NULL;
3628}
3629
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003630static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3631{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003632 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003633}
3634
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003635/*
3636 * Parse @cmdline according to command table @table.
3637 * If @cmdline is blank, return NULL.
3638 * If it can't be parsed, report to @mon, and return NULL.
3639 * Else, insert command arguments into @qdict, and return the command.
Peter Maydell085d8132013-03-18 17:20:07 +00003640 * If a sub-command table exists, and if @cmdline contains an additional string
3641 * for a sub-command, this function will try to search the sub-command table.
3642 * If no additional string for a sub-command is present, this function will
3643 * return the command found in @table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003644 * Do not assume the returned command points into @table! It doesn't
3645 * when the command is a sub-command.
3646 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003647static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003648 const char *cmdline,
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003649 int start,
3650 mon_cmd_t *table,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003651 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003652{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003653 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003654 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003655 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003656 char cmdname[256];
3657 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003658 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003659
3660#ifdef DEBUG
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003661 monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start);
bellard9dc39cb2004-03-14 21:38:27 +00003662#endif
ths3b46e622007-09-17 08:09:54 +00003663
bellard9307c4c2004-04-04 12:57:25 +00003664 /* extract the command name */
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003665 p = get_command_name(cmdline + start, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003666 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003667 return NULL;
ths3b46e622007-09-17 08:09:54 +00003668
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003669 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003670 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003671 monitor_printf(mon, "unknown command: '%.*s'\n",
3672 (int)(p - cmdline), cmdline);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003673 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003674 }
bellard9307c4c2004-04-04 12:57:25 +00003675
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003676 /* filter out following useless space */
3677 while (qemu_isspace(*p)) {
3678 p++;
3679 }
3680 /* search sub command */
3681 if (cmd->sub_table != NULL) {
3682 /* check if user set additional command */
3683 if (*p == '\0') {
3684 return cmd;
3685 }
3686 return monitor_parse_command(mon, cmdline, p - cmdline,
3687 cmd->sub_table, qdict);
3688 }
3689
bellard9307c4c2004-04-04 12:57:25 +00003690 /* parse the parameters */
3691 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003692 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003693 typestr = key_get_info(typestr, &key);
3694 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003695 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003696 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003697 typestr++;
3698 switch(c) {
3699 case 'F':
bellard81d09122004-07-14 17:21:37 +00003700 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003701 case 's':
3702 {
3703 int ret;
ths3b46e622007-09-17 08:09:54 +00003704
blueswir1cd390082008-11-16 13:53:32 +00003705 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003706 p++;
3707 if (*typestr == '?') {
3708 typestr++;
3709 if (*p == '\0') {
3710 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003711 break;
bellard9307c4c2004-04-04 12:57:25 +00003712 }
3713 }
3714 ret = get_str(buf, sizeof(buf), &p);
3715 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003716 switch(c) {
3717 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003718 monitor_printf(mon, "%s: filename expected\n",
3719 cmdname);
bellard81d09122004-07-14 17:21:37 +00003720 break;
3721 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003722 monitor_printf(mon, "%s: block device name expected\n",
3723 cmdname);
bellard81d09122004-07-14 17:21:37 +00003724 break;
3725 default:
aliguori376253e2009-03-05 23:01:23 +00003726 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003727 break;
3728 }
bellard9307c4c2004-04-04 12:57:25 +00003729 goto fail;
3730 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003731 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003732 }
3733 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003734 case 'O':
3735 {
3736 QemuOptsList *opts_list;
3737 QemuOpts *opts;
3738
3739 opts_list = qemu_find_opts(key);
3740 if (!opts_list || opts_list->desc->name) {
3741 goto bad_type;
3742 }
3743 while (qemu_isspace(*p)) {
3744 p++;
3745 }
3746 if (!*p)
3747 break;
3748 if (get_str(buf, sizeof(buf), &p) < 0) {
3749 goto fail;
3750 }
3751 opts = qemu_opts_parse(opts_list, buf, 1);
3752 if (!opts) {
3753 goto fail;
3754 }
3755 qemu_opts_to_qdict(opts, qdict);
3756 qemu_opts_del(opts);
3757 }
3758 break;
bellard9307c4c2004-04-04 12:57:25 +00003759 case '/':
3760 {
3761 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003762
blueswir1cd390082008-11-16 13:53:32 +00003763 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003764 p++;
3765 if (*p == '/') {
3766 /* format found */
3767 p++;
3768 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003769 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003770 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003771 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003772 count = count * 10 + (*p - '0');
3773 p++;
3774 }
3775 }
3776 size = -1;
3777 format = -1;
3778 for(;;) {
3779 switch(*p) {
3780 case 'o':
3781 case 'd':
3782 case 'u':
3783 case 'x':
3784 case 'i':
3785 case 'c':
3786 format = *p++;
3787 break;
3788 case 'b':
3789 size = 1;
3790 p++;
3791 break;
3792 case 'h':
3793 size = 2;
3794 p++;
3795 break;
3796 case 'w':
3797 size = 4;
3798 p++;
3799 break;
3800 case 'g':
3801 case 'L':
3802 size = 8;
3803 p++;
3804 break;
3805 default:
3806 goto next;
3807 }
3808 }
3809 next:
blueswir1cd390082008-11-16 13:53:32 +00003810 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003811 monitor_printf(mon, "invalid char in format: '%c'\n",
3812 *p);
bellard9307c4c2004-04-04 12:57:25 +00003813 goto fail;
3814 }
bellard9307c4c2004-04-04 12:57:25 +00003815 if (format < 0)
3816 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003817 if (format != 'i') {
3818 /* for 'i', not specifying a size gives -1 as size */
3819 if (size < 0)
3820 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003821 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003822 }
bellard9307c4c2004-04-04 12:57:25 +00003823 default_fmt_format = format;
3824 } else {
3825 count = 1;
3826 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003827 if (format != 'i') {
3828 size = default_fmt_size;
3829 } else {
3830 size = -1;
3831 }
bellard9307c4c2004-04-04 12:57:25 +00003832 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003833 qdict_put(qdict, "count", qint_from_int(count));
3834 qdict_put(qdict, "format", qint_from_int(format));
3835 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003836 }
3837 break;
3838 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003839 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003840 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003841 {
blueswir1c2efc952007-09-25 17:28:42 +00003842 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003843
blueswir1cd390082008-11-16 13:53:32 +00003844 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003845 p++;
bellard34405572004-06-08 00:55:58 +00003846 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003847 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003848 if (*p == '\0') {
3849 typestr++;
3850 break;
3851 }
bellard34405572004-06-08 00:55:58 +00003852 } else {
3853 if (*p == '.') {
3854 p++;
blueswir1cd390082008-11-16 13:53:32 +00003855 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003856 p++;
bellard34405572004-06-08 00:55:58 +00003857 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003858 typestr++;
3859 break;
bellard34405572004-06-08 00:55:58 +00003860 }
3861 }
bellard13224a82006-07-14 22:03:35 +00003862 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003863 }
aliguori376253e2009-03-05 23:01:23 +00003864 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003865 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003866 /* Check if 'i' is greater than 32-bit */
3867 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3868 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3869 monitor_printf(mon, "integer is for 32-bit values\n");
3870 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003871 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003872 if (val < 0) {
3873 monitor_printf(mon, "enter a positive value\n");
3874 goto fail;
3875 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003876 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003877 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003878 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003879 }
3880 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003881 case 'o':
3882 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003883 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003884 char *end;
3885
3886 while (qemu_isspace(*p)) {
3887 p++;
3888 }
3889 if (*typestr == '?') {
3890 typestr++;
3891 if (*p == '\0') {
3892 break;
3893 }
3894 }
3895 val = strtosz(p, &end);
3896 if (val < 0) {
3897 monitor_printf(mon, "invalid size\n");
3898 goto fail;
3899 }
3900 qdict_put(qdict, key, qint_from_int(val));
3901 p = end;
3902 }
3903 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003904 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003905 {
3906 double val;
3907
3908 while (qemu_isspace(*p))
3909 p++;
3910 if (*typestr == '?') {
3911 typestr++;
3912 if (*p == '\0') {
3913 break;
3914 }
3915 }
3916 if (get_double(mon, &val, &p) < 0) {
3917 goto fail;
3918 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003919 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003920 switch (*p) {
3921 case 'm':
3922 val /= 1e3; p += 2; break;
3923 case 'u':
3924 val /= 1e6; p += 2; break;
3925 case 'n':
3926 val /= 1e9; p += 2; break;
3927 }
3928 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003929 if (*p && !qemu_isspace(*p)) {
3930 monitor_printf(mon, "Unknown unit suffix\n");
3931 goto fail;
3932 }
3933 qdict_put(qdict, key, qfloat_from_double(val));
3934 }
3935 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003936 case 'b':
3937 {
3938 const char *beg;
3939 int val;
3940
3941 while (qemu_isspace(*p)) {
3942 p++;
3943 }
3944 beg = p;
3945 while (qemu_isgraph(*p)) {
3946 p++;
3947 }
3948 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3949 val = 1;
3950 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3951 val = 0;
3952 } else {
3953 monitor_printf(mon, "Expected 'on' or 'off'\n");
3954 goto fail;
3955 }
3956 qdict_put(qdict, key, qbool_from_int(val));
3957 }
3958 break;
bellard9307c4c2004-04-04 12:57:25 +00003959 case '-':
3960 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003961 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003962 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003963 /* option */
ths3b46e622007-09-17 08:09:54 +00003964
bellard9307c4c2004-04-04 12:57:25 +00003965 c = *typestr++;
3966 if (c == '\0')
3967 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003968 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003969 p++;
bellard9307c4c2004-04-04 12:57:25 +00003970 if (*p == '-') {
3971 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003972 if(c != *p) {
3973 if(!is_valid_option(p, typestr)) {
3974
3975 monitor_printf(mon, "%s: unsupported option -%c\n",
3976 cmdname, *p);
3977 goto fail;
3978 } else {
3979 skip_key = 1;
3980 }
bellard9307c4c2004-04-04 12:57:25 +00003981 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003982 if(skip_key) {
3983 p = tmp;
3984 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003985 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003986 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003987 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003988 }
bellard9307c4c2004-04-04 12:57:25 +00003989 }
bellard9307c4c2004-04-04 12:57:25 +00003990 }
3991 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003992 case 'S':
3993 {
3994 /* package all remaining string */
3995 int len;
3996
3997 while (qemu_isspace(*p)) {
3998 p++;
3999 }
4000 if (*typestr == '?') {
4001 typestr++;
4002 if (*p == '\0') {
4003 /* no remaining string: NULL argument */
4004 break;
4005 }
4006 }
4007 len = strlen(p);
4008 if (len <= 0) {
4009 monitor_printf(mon, "%s: string expected\n",
4010 cmdname);
4011 break;
4012 }
4013 qdict_put(qdict, key, qstring_from_str(p));
4014 p += len;
4015 }
4016 break;
bellard9307c4c2004-04-04 12:57:25 +00004017 default:
4018 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004019 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004020 goto fail;
4021 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004022 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004023 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004024 }
4025 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004026 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004027 p++;
4028 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004029 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4030 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004031 goto fail;
4032 }
4033
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004034 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004035
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004036fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004037 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004038 return NULL;
4039}
4040
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004041void monitor_set_error(Monitor *mon, QError *qerror)
4042{
4043 /* report only the first error */
4044 if (!mon->error) {
4045 mon->error = qerror;
4046 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004047 QDECREF(qerror);
4048 }
4049}
4050
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004051static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4052{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004053 if (ret && !monitor_has_error(mon)) {
4054 /*
4055 * If it returns failure, it must have passed on error.
4056 *
4057 * Action: Report an internal error to the client if in QMP.
4058 */
4059 qerror_report(QERR_UNDEFINED_ERROR);
Luiz Capitulino6d441432010-11-22 16:35:09 -02004060 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004061}
4062
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004063static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004064{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004065 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004066 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004067
4068 qdict = qdict_new();
4069
Wenchao Xia77172392013-08-27 20:38:20 +08004070 cmd = monitor_parse_command(mon, cmdline, 0, mon->cmd_table, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004071 if (!cmd)
4072 goto out;
4073
Luiz Capitulino4903de02010-09-16 11:01:32 -03004074 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004075 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004076 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004077 QObject *data = NULL;
4078
4079 /* XXX: ignores the error code */
4080 cmd->mhandler.cmd_new(mon, qdict, &data);
4081 assert(!monitor_has_error(mon));
4082 if (data) {
4083 cmd->user_print(mon, data);
4084 qobject_decref(data);
4085 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004086 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004087 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004088 }
4089
Luiz Capitulino13917be2009-10-07 13:41:54 -03004090out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004091 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004092}
4093
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004094static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004095{
4096 const char *p, *pstart;
4097 char cmd[128];
4098 int len;
4099
4100 p = list;
4101 for(;;) {
4102 pstart = p;
4103 p = strchr(p, '|');
4104 if (!p)
4105 p = pstart + strlen(pstart);
4106 len = p - pstart;
4107 if (len > sizeof(cmd) - 2)
4108 len = sizeof(cmd) - 2;
4109 memcpy(cmd, pstart, len);
4110 cmd[len] = '\0';
4111 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004112 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004113 }
4114 if (*p == '\0')
4115 break;
4116 p++;
4117 }
4118}
4119
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004120static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004121{
4122 DIR *ffs;
4123 struct dirent *d;
4124 char path[1024];
4125 char file[1024], file_prefix[1024];
4126 int input_path_len;
4127 const char *p;
4128
ths5fafdf22007-09-16 21:08:06 +00004129 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004130 if (!p) {
4131 input_path_len = 0;
4132 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004133 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004134 } else {
4135 input_path_len = p - input + 1;
4136 memcpy(path, input, input_path_len);
4137 if (input_path_len > sizeof(path) - 1)
4138 input_path_len = sizeof(path) - 1;
4139 path[input_path_len] = '\0';
4140 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4141 }
4142#ifdef DEBUG_COMPLETION
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004143 monitor_printf(mon, "input='%s' path='%s' prefix='%s'\n",
aliguori376253e2009-03-05 23:01:23 +00004144 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004145#endif
4146 ffs = opendir(path);
4147 if (!ffs)
4148 return;
4149 for(;;) {
4150 struct stat sb;
4151 d = readdir(ffs);
4152 if (!d)
4153 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004154
4155 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4156 continue;
4157 }
4158
bellard81d09122004-07-14 17:21:37 +00004159 if (strstart(d->d_name, file_prefix, NULL)) {
4160 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004161 if (input_path_len < sizeof(file))
4162 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4163 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004164 /* stat the file to find out if it's a directory.
4165 * In that case add a slash to speed up typing long paths
4166 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004167 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004168 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004169 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004170 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004171 }
4172 }
4173 closedir(ffs);
4174}
4175
Wenchao Xia599a9262013-08-27 20:38:15 +08004176typedef struct MonitorBlockComplete {
4177 Monitor *mon;
4178 const char *input;
4179} MonitorBlockComplete;
4180
aliguori51de9762009-03-05 23:00:43 +00004181static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004182{
aliguori51de9762009-03-05 23:00:43 +00004183 const char *name = bdrv_get_device_name(bs);
Wenchao Xia599a9262013-08-27 20:38:15 +08004184 MonitorBlockComplete *mbc = opaque;
4185 Monitor *mon = mbc->mon;
4186 const char *input = mbc->input;
bellard81d09122004-07-14 17:21:37 +00004187
4188 if (input[0] == '\0' ||
4189 !strncmp(name, (char *)input, strlen(input))) {
Wenchao Xia599a9262013-08-27 20:38:15 +08004190 readline_add_completion(mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004191 }
4192}
4193
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004194static const char *next_arg_type(const char *typestr)
4195{
4196 const char *p = strchr(typestr, ':');
4197 return (p != NULL ? ++p : typestr);
4198}
4199
Hani Benhabiles40d19392014-05-07 23:41:30 +01004200static void add_completion_option(ReadLineState *rs, const char *str,
4201 const char *option)
4202{
4203 if (!str || !option) {
4204 return;
4205 }
4206 if (!strncmp(option, str, strlen(str))) {
4207 readline_add_completion(rs, option);
4208 }
4209}
4210
Hani Benhabiles13e315d2014-05-07 23:41:29 +01004211void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4212{
4213 size_t len;
4214 ChardevBackendInfoList *list, *start;
4215
4216 if (nb_args != 2) {
4217 return;
4218 }
4219 len = strlen(str);
4220 readline_set_completion_index(rs, len);
4221
4222 start = list = qmp_query_chardev_backends(NULL);
4223 while (list) {
4224 const char *chr_name = list->value->name;
4225
4226 if (!strncmp(chr_name, str, len)) {
4227 readline_add_completion(rs, chr_name);
4228 }
4229 list = list->next;
4230 }
4231 qapi_free_ChardevBackendInfoList(start);
4232}
4233
Hani Benhabilesb162b492014-05-07 23:41:31 +01004234void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4235{
4236 size_t len;
4237 int i;
4238
4239 if (nb_args != 2) {
4240 return;
4241 }
4242 len = strlen(str);
4243 readline_set_completion_index(rs, len);
4244 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
4245 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
4246 }
4247}
4248
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004249void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004250{
4251 GSList *list, *elt;
4252 size_t len;
4253
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004254 if (nb_args != 2) {
4255 return;
4256 }
4257
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004258 len = strlen(str);
4259 readline_set_completion_index(rs, len);
4260 list = elt = object_class_get_list(TYPE_DEVICE, false);
4261 while (elt) {
4262 const char *name;
4263 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
4264 TYPE_DEVICE);
4265 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004266
4267 if (!dc->cannot_instantiate_with_device_add_yet
4268 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004269 readline_add_completion(rs, name);
4270 }
4271 elt = elt->next;
4272 }
4273 g_slist_free(list);
4274}
4275
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004276void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004277{
4278 GSList *list, *elt;
4279 size_t len;
4280
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004281 if (nb_args != 2) {
4282 return;
4283 }
4284
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004285 len = strlen(str);
4286 readline_set_completion_index(rs, len);
4287 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
4288 while (elt) {
4289 const char *name;
4290
4291 name = object_class_get_name(OBJECT_CLASS(elt->data));
4292 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
4293 readline_add_completion(rs, name);
4294 }
4295 elt = elt->next;
4296 }
4297 g_slist_free(list);
4298}
4299
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004300static void device_del_bus_completion(ReadLineState *rs, BusState *bus,
4301 const char *str, size_t len)
Hani Benhabilesfca72d92014-02-06 23:30:10 +01004302{
4303 BusChild *kid;
4304
4305 QTAILQ_FOREACH(kid, &bus->children, sibling) {
4306 DeviceState *dev = kid->child;
4307 BusState *dev_child;
4308
4309 if (dev->id && !strncmp(str, dev->id, len)) {
4310 readline_add_completion(rs, dev->id);
4311 }
4312
4313 QLIST_FOREACH(dev_child, &dev->child_bus, sibling) {
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004314 device_del_bus_completion(rs, dev_child, str, len);
Hani Benhabilesfca72d92014-02-06 23:30:10 +01004315 }
4316 }
4317}
4318
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01004319void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4320{
4321 size_t len;
4322 ChardevInfoList *list, *start;
4323
4324 if (nb_args != 2) {
4325 return;
4326 }
4327 len = strlen(str);
4328 readline_set_completion_index(rs, len);
4329
4330 start = list = qmp_query_chardev(NULL);
4331 while (list) {
4332 ChardevInfo *chr = list->value;
4333
4334 if (!strncmp(chr->label, str, len)) {
4335 readline_add_completion(rs, chr->label);
4336 }
4337 list = list->next;
4338 }
4339 qapi_free_ChardevInfoList(start);
4340}
4341
Hani Benhabiles8e597772014-05-27 23:39:30 +01004342static void ringbuf_completion(ReadLineState *rs, const char *str)
4343{
4344 size_t len;
4345 ChardevInfoList *list, *start;
4346
4347 len = strlen(str);
4348 readline_set_completion_index(rs, len);
4349
4350 start = list = qmp_query_chardev(NULL);
4351 while (list) {
4352 ChardevInfo *chr_info = list->value;
4353
4354 if (!strncmp(chr_info->label, str, len)) {
4355 CharDriverState *chr = qemu_chr_find(chr_info->label);
4356 if (chr && chr_is_ringbuf(chr)) {
4357 readline_add_completion(rs, chr_info->label);
4358 }
4359 }
4360 list = list->next;
4361 }
4362 qapi_free_ChardevInfoList(start);
4363}
4364
4365void ringbuf_read_completion(ReadLineState *rs, int nb_args, const char *str)
4366{
4367 if (nb_args != 2) {
4368 return;
4369 }
4370 ringbuf_completion(rs, str);
4371}
4372
4373void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
4374{
4375 if (nb_args != 2) {
4376 return;
4377 }
4378 ringbuf_completion(rs, str);
4379}
4380
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004381void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
4382{
4383 size_t len;
4384
4385 if (nb_args != 2) {
4386 return;
4387 }
4388
4389 len = strlen(str);
4390 readline_set_completion_index(rs, len);
4391 device_del_bus_completion(rs, sysbus_get_default(), str, len);
4392}
4393
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004394void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004395{
4396 ObjectPropertyInfoList *list, *start;
4397 size_t len;
4398
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004399 if (nb_args != 2) {
4400 return;
4401 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004402 len = strlen(str);
4403 readline_set_completion_index(rs, len);
4404
4405 start = list = qmp_qom_list("/objects", NULL);
4406 while (list) {
4407 ObjectPropertyInfo *info = list->value;
4408
4409 if (!strncmp(info->type, "child<", 5)
4410 && !strncmp(info->name, str, len)) {
4411 readline_add_completion(rs, info->name);
4412 }
4413 list = list->next;
4414 }
4415 qapi_free_ObjectPropertyInfoList(start);
4416}
4417
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004418void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
4419{
4420 int i;
4421 char *sep;
4422 size_t len;
4423
4424 if (nb_args != 2) {
4425 return;
4426 }
4427 sep = strrchr(str, '-');
4428 if (sep) {
4429 str = sep + 1;
4430 }
4431 len = strlen(str);
4432 readline_set_completion_index(rs, len);
4433 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4434 if (!strncmp(str, QKeyCode_lookup[i], len)) {
4435 readline_add_completion(rs, QKeyCode_lookup[i]);
4436 }
4437 }
4438}
4439
Hani Benhabiles40d19392014-05-07 23:41:30 +01004440void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
4441{
4442 size_t len;
4443
4444 len = strlen(str);
4445 readline_set_completion_index(rs, len);
4446 if (nb_args == 2) {
4447 NetClientState *ncs[255];
4448 int count, i;
4449 count = qemu_find_net_clients_except(NULL, ncs,
4450 NET_CLIENT_OPTIONS_KIND_NONE, 255);
4451 for (i = 0; i < count; i++) {
4452 const char *name = ncs[i]->name;
4453 if (!strncmp(str, name, len)) {
4454 readline_add_completion(rs, name);
4455 }
4456 }
4457 } else if (nb_args == 3) {
4458 add_completion_option(rs, str, "on");
4459 add_completion_option(rs, str, "off");
4460 }
4461}
4462
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004463void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
4464{
4465 int len, count, i;
4466 NetClientState *ncs[255];
4467
4468 if (nb_args != 2) {
4469 return;
4470 }
4471
4472 len = strlen(str);
4473 readline_set_completion_index(rs, len);
4474 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
4475 255);
4476 for (i = 0; i < count; i++) {
4477 QemuOpts *opts;
4478 const char *name = ncs[i]->name;
4479 if (strncmp(str, name, len)) {
4480 continue;
4481 }
4482 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
4483 if (opts) {
4484 readline_add_completion(rs, name);
4485 }
4486 }
4487}
4488
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004489void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
4490{
4491 if (nb_args != 2) {
4492 return;
4493 }
4494 readline_set_completion_index(rs, strlen(str));
4495 add_completion_option(rs, str, "reset");
4496 add_completion_option(rs, str, "shutdown");
4497 add_completion_option(rs, str, "poweroff");
4498 add_completion_option(rs, str, "pause");
4499 add_completion_option(rs, str, "debug");
4500 add_completion_option(rs, str, "none");
4501}
4502
Hani Benhabilesc68a0402014-05-27 23:39:32 +01004503void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
4504 const char *str)
4505{
4506 size_t len;
4507
4508 len = strlen(str);
4509 readline_set_completion_index(rs, len);
4510 if (nb_args == 2) {
4511 int i;
4512 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
4513 const char *name = MigrationCapability_lookup[i];
4514 if (!strncmp(str, name, len)) {
4515 readline_add_completion(rs, name);
4516 }
4517 }
4518 } else if (nb_args == 3) {
4519 add_completion_option(rs, str, "on");
4520 add_completion_option(rs, str, "off");
4521 }
4522}
4523
Hani Benhabilese3bb5322014-05-27 23:39:34 +01004524void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
4525{
4526 int i;
4527 size_t len;
4528 if (nb_args != 2) {
4529 return;
4530 }
4531 len = strlen(str);
4532 readline_set_completion_index(rs, len);
4533 for (i = 0; host_net_devices[i]; i++) {
4534 if (!strncmp(host_net_devices[i], str, len)) {
4535 readline_add_completion(rs, host_net_devices[i]);
4536 }
4537 }
4538}
4539
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004540void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4541{
4542 NetClientState *ncs[255];
4543 int count, i, len;
4544
4545 len = strlen(str);
4546 readline_set_completion_index(rs, len);
4547 if (nb_args == 2) {
4548 count = qemu_find_net_clients_except(NULL, ncs,
4549 NET_CLIENT_OPTIONS_KIND_NONE, 255);
4550 for (i = 0; i < count; i++) {
4551 int id;
4552 char name[16];
4553
4554 if (net_hub_id_for_client(ncs[i], &id)) {
4555 continue;
4556 }
4557 snprintf(name, sizeof(name), "%d", id);
4558 if (!strncmp(str, name, len)) {
4559 readline_add_completion(rs, name);
4560 }
4561 }
4562 return;
4563 } else if (nb_args == 3) {
4564 count = qemu_find_net_clients_except(NULL, ncs,
4565 NET_CLIENT_OPTIONS_KIND_NIC, 255);
4566 for (i = 0; i < count; i++) {
4567 const char *name;
4568
4569 name = ncs[i]->name;
4570 if (!strncmp(str, name, len)) {
4571 readline_add_completion(rs, name);
4572 }
4573 }
4574 return;
4575 }
4576}
4577
Hani Benhabilesb21631f2014-05-27 23:39:37 +01004578static void vm_completion(ReadLineState *rs, const char *str)
4579{
4580 size_t len;
4581 BlockDriverState *bs = NULL;
4582
4583 len = strlen(str);
4584 readline_set_completion_index(rs, len);
4585 while ((bs = bdrv_next(bs))) {
4586 SnapshotInfoList *snapshots, *snapshot;
4587
4588 if (!bdrv_can_snapshot(bs)) {
4589 continue;
4590 }
4591 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
4592 continue;
4593 }
4594 snapshot = snapshots;
4595 while (snapshot) {
4596 char *completion = snapshot->value->name;
4597 if (!strncmp(str, completion, len)) {
4598 readline_add_completion(rs, completion);
4599 }
4600 completion = snapshot->value->id;
4601 if (!strncmp(str, completion, len)) {
4602 readline_add_completion(rs, completion);
4603 }
4604 snapshot = snapshot->next;
4605 }
4606 qapi_free_SnapshotInfoList(snapshots);
4607 }
4608
4609}
4610
4611void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
4612{
4613 if (nb_args == 2) {
4614 vm_completion(rs, str);
4615 }
4616}
4617
4618void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
4619{
4620 if (nb_args == 2) {
4621 vm_completion(rs, str);
4622 }
4623}
4624
Wenchao Xiac35b6402013-08-27 20:38:24 +08004625static void monitor_find_completion_by_table(Monitor *mon,
4626 const mon_cmd_t *cmd_table,
4627 char **args,
4628 int nb_args)
bellard81d09122004-07-14 17:21:37 +00004629{
4630 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004631 int i;
bellard81d09122004-07-14 17:21:37 +00004632 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004633 const mon_cmd_t *cmd;
Wenchao Xia599a9262013-08-27 20:38:15 +08004634 MonitorBlockComplete mbs;
bellard81d09122004-07-14 17:21:37 +00004635
bellard81d09122004-07-14 17:21:37 +00004636 if (nb_args <= 1) {
4637 /* command completion */
4638 if (nb_args == 0)
4639 cmdname = "";
4640 else
4641 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004642 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004643 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004644 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00004645 }
4646 } else {
4647 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004648 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004649 if (compare_cmd(args[0], cmd->name)) {
4650 break;
4651 }
bellard81d09122004-07-14 17:21:37 +00004652 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004653 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004654 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004655 }
4656
Wenchao Xiad903a772013-08-27 20:38:25 +08004657 if (cmd->sub_table) {
4658 /* do the job again */
4659 return monitor_find_completion_by_table(mon, cmd->sub_table,
4660 &args[1], nb_args - 1);
4661 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004662 if (cmd->command_completion) {
4663 return cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4664 }
Wenchao Xiad903a772013-08-27 20:38:25 +08004665
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004666 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004667 for(i = 0; i < nb_args - 2; i++) {
4668 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004669 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004670 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004671 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004672 }
4673 }
4674 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004675 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004676 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004677 }
bellard81d09122004-07-14 17:21:37 +00004678 switch(*ptype) {
4679 case 'F':
4680 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004681 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004682 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004683 break;
4684 case 'B':
4685 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004686 mbs.mon = mon;
4687 mbs.input = str;
4688 readline_set_completion_index(mon->rs, strlen(str));
4689 bdrv_iterate(block_completion_it, &mbs);
bellard81d09122004-07-14 17:21:37 +00004690 break;
bellard7fe48482004-10-09 18:08:01 +00004691 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004692 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004693 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004694 monitor_find_completion_by_table(mon, cmd_table,
4695 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004696 }
4697 break;
bellard81d09122004-07-14 17:21:37 +00004698 default:
4699 break;
4700 }
4701 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004702}
4703
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004704static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004705 const char *cmdline)
4706{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004707 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004708 char *args[MAX_ARGS];
4709 int nb_args, len;
4710
4711 /* 1. parse the cmdline */
4712 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4713 return;
4714 }
4715#ifdef DEBUG_COMPLETION
4716 for (i = 0; i < nb_args; i++) {
4717 monitor_printf(mon, "arg%d = '%s'\n", i, args[i]);
4718 }
4719#endif
4720
4721 /* if the line ends with a space, it means we want to complete the
4722 next arg */
4723 len = strlen(cmdline);
4724 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4725 if (nb_args >= MAX_ARGS) {
4726 goto cleanup;
4727 }
4728 args[nb_args++] = g_strdup("");
4729 }
4730
4731 /* 2. auto complete according to args */
4732 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004733
4734cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004735 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004736}
4737
aliguori731b0362009-03-05 23:01:42 +00004738static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004739{
aliguori731b0362009-03-05 23:01:42 +00004740 Monitor *mon = opaque;
4741
Jan Kiszkac62313b2009-12-04 14:05:29 +01004742 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004743}
4744
Luiz Capitulino09069b12010-02-04 18:10:06 -02004745static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4746{
4747 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4748 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4749}
4750
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004751/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004752 * Argument validation rules:
4753 *
4754 * 1. The argument must exist in cmd_args qdict
4755 * 2. The argument type must be the expected one
4756 *
4757 * Special case: If the argument doesn't exist in cmd_args and
4758 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4759 * checking is skipped for it.
4760 */
4761static int check_client_args_type(const QDict *client_args,
4762 const QDict *cmd_args, int flags)
4763{
4764 const QDictEntry *ent;
4765
4766 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4767 QObject *obj;
4768 QString *arg_type;
4769 const QObject *client_arg = qdict_entry_value(ent);
4770 const char *client_arg_name = qdict_entry_key(ent);
4771
4772 obj = qdict_get(cmd_args, client_arg_name);
4773 if (!obj) {
4774 if (flags & QMP_ACCEPT_UNKNOWNS) {
4775 /* handler accepts unknowns */
4776 continue;
4777 }
4778 /* client arg doesn't exist */
4779 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4780 return -1;
4781 }
4782
4783 arg_type = qobject_to_qstring(obj);
4784 assert(arg_type != NULL);
4785
4786 /* check if argument's type is correct */
4787 switch (qstring_get_str(arg_type)[0]) {
4788 case 'F':
4789 case 'B':
4790 case 's':
4791 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4792 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4793 "string");
4794 return -1;
4795 }
4796 break;
4797 case 'i':
4798 case 'l':
4799 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004800 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004801 if (qobject_type(client_arg) != QTYPE_QINT) {
4802 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4803 "int");
4804 return -1;
4805 }
4806 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004807 case 'T':
4808 if (qobject_type(client_arg) != QTYPE_QINT &&
4809 qobject_type(client_arg) != QTYPE_QFLOAT) {
4810 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4811 "number");
4812 return -1;
4813 }
4814 break;
4815 case 'b':
4816 case '-':
4817 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4818 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4819 "bool");
4820 return -1;
4821 }
4822 break;
4823 case 'O':
4824 assert(flags & QMP_ACCEPT_UNKNOWNS);
4825 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004826 case 'q':
4827 /* Any QObject can be passed. */
4828 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004829 case '/':
4830 case '.':
4831 /*
4832 * These types are not supported by QMP and thus are not
4833 * handled here. Fall through.
4834 */
4835 default:
4836 abort();
4837 }
4838 }
4839
4840 return 0;
4841}
4842
4843/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004844 * - Check if the client has passed all mandatory args
4845 * - Set special flags for argument validation
4846 */
4847static int check_mandatory_args(const QDict *cmd_args,
4848 const QDict *client_args, int *flags)
4849{
4850 const QDictEntry *ent;
4851
4852 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4853 const char *cmd_arg_name = qdict_entry_key(ent);
4854 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4855 assert(type != NULL);
4856
4857 if (qstring_get_str(type)[0] == 'O') {
4858 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4859 *flags |= QMP_ACCEPT_UNKNOWNS;
4860 } else if (qstring_get_str(type)[0] != '-' &&
4861 qstring_get_str(type)[1] != '?' &&
4862 !qdict_haskey(client_args, cmd_arg_name)) {
4863 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4864 return -1;
4865 }
4866 }
4867
4868 return 0;
4869}
4870
4871static QDict *qdict_from_args_type(const char *args_type)
4872{
4873 int i;
4874 QDict *qdict;
4875 QString *key, *type, *cur_qs;
4876
4877 assert(args_type != NULL);
4878
4879 qdict = qdict_new();
4880
4881 if (args_type == NULL || args_type[0] == '\0') {
4882 /* no args, empty qdict */
4883 goto out;
4884 }
4885
4886 key = qstring_new();
4887 type = qstring_new();
4888
4889 cur_qs = key;
4890
4891 for (i = 0;; i++) {
4892 switch (args_type[i]) {
4893 case ',':
4894 case '\0':
4895 qdict_put(qdict, qstring_get_str(key), type);
4896 QDECREF(key);
4897 if (args_type[i] == '\0') {
4898 goto out;
4899 }
4900 type = qstring_new(); /* qdict has ref */
4901 cur_qs = key = qstring_new();
4902 break;
4903 case ':':
4904 cur_qs = type;
4905 break;
4906 default:
4907 qstring_append_chr(cur_qs, args_type[i]);
4908 break;
4909 }
4910 }
4911
4912out:
4913 return qdict;
4914}
4915
4916/*
4917 * Client argument checking rules:
4918 *
4919 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004920 * 2. Each argument provided by the client must be expected
4921 * 3. Each argument provided by the client must have the type expected
4922 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004923 */
4924static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4925{
4926 int flags, err;
4927 QDict *cmd_args;
4928
4929 cmd_args = qdict_from_args_type(cmd->args_type);
4930
4931 flags = 0;
4932 err = check_mandatory_args(cmd_args, client_args, &flags);
4933 if (err) {
4934 goto out;
4935 }
4936
Luiz Capitulino4af91932010-06-22 11:44:05 -03004937 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004938
4939out:
4940 QDECREF(cmd_args);
4941 return err;
4942}
4943
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004944/*
4945 * Input object checking rules
4946 *
4947 * 1. Input object must be a dict
4948 * 2. The "execute" key must exist
4949 * 3. The "execute" key must be a string
4950 * 4. If the "arguments" key exists, it must be a dict
4951 * 5. If the "id" key exists, it can be anything (ie. json-value)
4952 * 6. Any argument not listed above is considered invalid
4953 */
4954static QDict *qmp_check_input_obj(QObject *input_obj)
4955{
4956 const QDictEntry *ent;
4957 int has_exec_key = 0;
4958 QDict *input_dict;
4959
4960 if (qobject_type(input_obj) != QTYPE_QDICT) {
4961 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4962 return NULL;
4963 }
4964
4965 input_dict = qobject_to_qdict(input_obj);
4966
4967 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4968 const char *arg_name = qdict_entry_key(ent);
4969 const QObject *arg_obj = qdict_entry_value(ent);
4970
4971 if (!strcmp(arg_name, "execute")) {
4972 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4973 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4974 "string");
4975 return NULL;
4976 }
4977 has_exec_key = 1;
4978 } else if (!strcmp(arg_name, "arguments")) {
4979 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4980 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4981 "object");
4982 return NULL;
4983 }
4984 } else if (!strcmp(arg_name, "id")) {
4985 /* FIXME: check duplicated IDs for async commands */
4986 } else {
4987 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4988 return NULL;
4989 }
4990 }
4991
4992 if (!has_exec_key) {
4993 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4994 return NULL;
4995 }
4996
4997 return input_dict;
4998}
4999
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005000static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
5001 const QDict *params)
5002{
5003 int ret;
5004 QObject *data = NULL;
5005
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005006 ret = cmd->mhandler.cmd_new(mon, params, &data);
5007 handler_audit(mon, cmd, ret);
5008 monitor_protocol_emitter(mon, data);
5009 qobject_decref(data);
5010}
5011
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005012static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
5013{
5014 int err;
5015 QObject *obj;
5016 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005017 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02005018 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005019 Monitor *mon = cur_mon;
5020
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005021 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005022
5023 obj = json_parser_parse(tokens, NULL);
5024 if (!obj) {
5025 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005026 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005027 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005028 }
5029
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03005030 input = qmp_check_input_obj(obj);
5031 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005032 qobject_decref(obj);
5033 goto err_out;
5034 }
5035
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005036 mon->mc->id = qdict_get(input, "id");
5037 qobject_incref(mon->mc->id);
5038
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005039 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01005040 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02005041 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005042 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005043 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02005044 }
5045
Anthony Liguorie3193602011-09-02 12:34:47 -05005046 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03005047 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005048 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005049 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005050 }
5051
5052 obj = qdict_get(input, "arguments");
5053 if (!obj) {
5054 args = qdict_new();
5055 } else {
5056 args = qobject_to_qdict(obj);
5057 QINCREF(args);
5058 }
5059
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03005060 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005061 if (err < 0) {
5062 goto err_out;
5063 }
5064
Luiz Capitulino40e5a012011-10-21 16:15:31 -02005065 if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03005066 err = qmp_async_cmd_handler(mon, cmd, args);
5067 if (err) {
5068 /* emit the error response */
5069 goto err_out;
5070 }
Adam Litke940cc302010-01-25 12:18:44 -06005071 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005072 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06005073 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005074
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005075 goto out;
5076
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005077err_out:
5078 monitor_protocol_emitter(mon, NULL);
5079out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005080 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005081 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005082}
5083
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005084/**
5085 * monitor_control_read(): Read and handle QMP input
5086 */
5087static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
5088{
5089 Monitor *old_mon = cur_mon;
5090
5091 cur_mon = opaque;
5092
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005093 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005094
5095 cur_mon = old_mon;
5096}
5097
aliguori731b0362009-03-05 23:01:42 +00005098static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005099{
aliguori731b0362009-03-05 23:01:42 +00005100 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005101 int i;
aliguori376253e2009-03-05 23:01:23 +00005102
aliguori731b0362009-03-05 23:01:42 +00005103 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005104
aliguoricde76ee2009-03-05 23:01:51 +00005105 if (cur_mon->rs) {
5106 for (i = 0; i < size; i++)
5107 readline_handle_byte(cur_mon->rs, buf[i]);
5108 } else {
5109 if (size == 0 || buf[size - 1] != 0)
5110 monitor_printf(cur_mon, "corrupted command\n");
5111 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005112 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005113 }
aliguori731b0362009-03-05 23:01:42 +00005114
5115 cur_mon = old_mon;
5116}
aliguorid8f44602008-10-06 13:52:44 +00005117
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005118static void monitor_command_cb(void *opaque, const char *cmdline,
5119 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005120{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005121 Monitor *mon = opaque;
5122
aliguori731b0362009-03-05 23:01:42 +00005123 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005124 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005125 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005126}
5127
aliguoricde76ee2009-03-05 23:01:51 +00005128int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005129{
aliguoricde76ee2009-03-05 23:01:51 +00005130 if (!mon->rs)
5131 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005132 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005133 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005134}
5135
aliguori376253e2009-03-05 23:01:23 +00005136void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005137{
aliguoricde76ee2009-03-05 23:01:51 +00005138 if (!mon->rs)
5139 return;
aliguori731b0362009-03-05 23:01:42 +00005140 if (--mon->suspend_cnt == 0)
5141 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005142}
5143
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005144static QObject *get_qmp_greeting(void)
5145{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005146 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005147
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005148 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005149 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5150}
5151
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005152/**
5153 * monitor_control_event(): Print QMP gretting
5154 */
5155static void monitor_control_event(void *opaque, int event)
5156{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005157 QObject *data;
5158 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005159
Luiz Capitulino47116d12010-02-08 17:01:30 -02005160 switch (event) {
5161 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02005162 mon->mc->command_mode = 0;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005163 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005164 monitor_json_emitter(mon, data);
5165 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04005166 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02005167 break;
5168 case CHR_EVENT_CLOSED:
5169 json_message_parser_destroy(&mon->mc->parser);
Anthony Liguori58617a72012-08-23 08:03:21 -05005170 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04005171 mon_refcount--;
5172 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02005173 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005174 }
5175}
5176
aliguori731b0362009-03-05 23:01:42 +00005177static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005178{
aliguori376253e2009-03-05 23:01:23 +00005179 Monitor *mon = opaque;
5180
aliguori2724b182009-03-05 23:01:47 +00005181 switch (event) {
5182 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005183 mon->mux_out = 0;
5184 if (mon->reset_seen) {
5185 readline_restart(mon->rs);
5186 monitor_resume(mon);
5187 monitor_flush(mon);
5188 } else {
5189 mon->suspend_cnt = 0;
5190 }
aliguori2724b182009-03-05 23:01:47 +00005191 break;
ths86e94de2007-01-05 22:01:59 +00005192
aliguori2724b182009-03-05 23:01:47 +00005193 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005194 if (mon->reset_seen) {
5195 if (mon->suspend_cnt == 0) {
5196 monitor_printf(mon, "\n");
5197 }
5198 monitor_flush(mon);
5199 monitor_suspend(mon);
5200 } else {
5201 mon->suspend_cnt++;
5202 }
5203 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00005204 break;
5205
Amit Shahb6b8df52009-10-07 18:31:16 +05305206 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005207 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5208 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005209 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00005210 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005211 }
5212 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04005213 mon_refcount++;
5214 break;
5215
5216 case CHR_EVENT_CLOSED:
5217 mon_refcount--;
5218 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00005219 break;
5220 }
ths86e94de2007-01-05 22:01:59 +00005221}
5222
Wayne Xia816f8922011-10-12 11:32:41 +08005223static int
5224compare_mon_cmd(const void *a, const void *b)
5225{
5226 return strcmp(((const mon_cmd_t *)a)->name,
5227 ((const mon_cmd_t *)b)->name);
5228}
5229
5230static void sortcmdlist(void)
5231{
5232 int array_num;
5233 int elem_size = sizeof(mon_cmd_t);
5234
5235 array_num = sizeof(mon_cmds)/elem_size-1;
5236 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
5237
5238 array_num = sizeof(info_cmds)/elem_size-1;
5239 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
5240}
5241
aliguori76655d62009-03-06 20:27:37 +00005242
5243/*
5244 * Local variables:
5245 * c-indent-level: 4
5246 * c-basic-offset: 4
5247 * tab-width: 8
5248 * End:
5249 */
5250
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005251/* These functions just adapt the readline interface in a typesafe way. We
5252 * could cast function pointers but that discards compiler checks.
5253 */
Stefan Weild5d15072014-01-25 18:18:23 +01005254static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
5255 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005256{
5257 va_list ap;
5258 va_start(ap, fmt);
5259 monitor_vprintf(opaque, fmt, ap);
5260 va_end(ap);
5261}
5262
5263static void monitor_readline_flush(void *opaque)
5264{
5265 monitor_flush(opaque);
5266}
5267
aliguori731b0362009-03-05 23:01:42 +00005268void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005269{
aliguori731b0362009-03-05 23:01:42 +00005270 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005271 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005272
5273 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02005274 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08005275 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00005276 is_first_init = 0;
5277 }
aliguori87127162009-03-05 23:01:29 +00005278
Wenchao Xiab01fe892013-08-27 20:38:19 +08005279 mon = g_malloc(sizeof(*mon));
5280 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00005281
aliguori87127162009-03-05 23:01:29 +00005282 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005283 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005284 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005285 mon->rs = readline_init(monitor_readline_printf,
5286 monitor_readline_flush,
5287 mon,
5288 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00005289 monitor_read_command(mon, 0);
5290 }
aliguori87127162009-03-05 23:01:29 +00005291
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005292 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005293 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005294 /* Control mode requires special handlers */
5295 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5296 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005297 qemu_chr_fe_set_echo(chr, true);
Anthony Liguori26efaca2012-08-23 13:49:02 -05005298
5299 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005300 } else {
5301 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5302 monitor_event, mon);
5303 }
aliguori87127162009-03-05 23:01:29 +00005304
Blue Swirl72cf2d42009-09-12 07:36:22 +00005305 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01005306 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5307 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005308}
5309
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005310static void bdrv_password_cb(void *opaque, const char *password,
5311 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005312{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005313 Monitor *mon = opaque;
5314 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005315 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00005316
aliguoribb5fc202009-03-05 23:01:15 +00005317 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00005318 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00005319 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005320 }
aliguori731b0362009-03-05 23:01:42 +00005321 if (mon->password_completion_cb)
5322 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005323
aliguori731b0362009-03-05 23:01:42 +00005324 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005325}
aliguoric0f4ce72009-03-05 23:01:01 +00005326
Anthony Liguori7060b472011-09-02 12:34:50 -05005327ReadLineState *monitor_get_rs(Monitor *mon)
5328{
5329 return mon->rs;
5330}
5331
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005332int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5333 BlockDriverCompletionFunc *completion_cb,
5334 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005335{
aliguoricde76ee2009-03-05 23:01:51 +00005336 int err;
5337
aliguoribb5fc202009-03-05 23:01:15 +00005338 if (!bdrv_key_required(bs)) {
5339 if (completion_cb)
5340 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005341 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00005342 }
aliguoric0f4ce72009-03-05 23:01:01 +00005343
Luiz Capitulino94171e12009-12-07 21:37:00 +01005344 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino903a8812011-12-13 17:18:30 -02005345 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
5346 bdrv_get_encrypted_filename(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005347 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01005348 }
5349
aliguori376253e2009-03-05 23:01:23 +00005350 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5351 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005352
aliguori731b0362009-03-05 23:01:42 +00005353 mon->password_completion_cb = completion_cb;
5354 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005355
aliguoricde76ee2009-03-05 23:01:51 +00005356 err = monitor_read_password(mon, bdrv_password_cb, bs);
5357
5358 if (err && completion_cb)
5359 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005360
5361 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005362}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005363
5364int monitor_read_block_device_key(Monitor *mon, const char *device,
5365 BlockDriverCompletionFunc *completion_cb,
5366 void *opaque)
5367{
5368 BlockDriverState *bs;
5369
5370 bs = bdrv_find(device);
5371 if (!bs) {
5372 monitor_printf(mon, "Device not found %s\n", device);
5373 return -1;
5374 }
5375
5376 return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);
5377}
Paolo Bonzini4d454572012-11-26 16:03:42 +01005378
5379QemuOptsList qemu_mon_opts = {
5380 .name = "mon",
5381 .implied_opt_name = "chardev",
5382 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5383 .desc = {
5384 {
5385 .name = "mode",
5386 .type = QEMU_OPT_STRING,
5387 },{
5388 .name = "chardev",
5389 .type = QEMU_OPT_STRING,
5390 },{
5391 .name = "default",
5392 .type = QEMU_OPT_BOOL,
5393 },{
5394 .name = "pretty",
5395 .type = QEMU_OPT_BOOL,
5396 },
5397 { /* end of list */ }
5398 },
5399};