blob: 74f3f1ba603c60390f767ab0720389d1d2eed10a [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"
40#include "monitor/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010041#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010042#include "sysemu/blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000043#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020044#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010045#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010046#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010047#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010048#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010049#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020050#include "sysemu/tpm.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010051#include "qapi/qmp/qint.h"
52#include "qapi/qmp/qfloat.h"
53#include "qapi/qmp/qlist.h"
54#include "qapi/qmp/qbool.h"
55#include "qapi/qmp/qstring.h"
56#include "qapi/qmp/qjson.h"
57#include "qapi/qmp/json-streamer.h"
58#include "qapi/qmp/json-parser.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010059#include "qemu/osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000060#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010061#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020062#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020063#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020064#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053065#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010066#include "exec/memory.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050067#include "qmp-commands.h"
68#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010069#include "qemu/thread.h"
ths6a5bd302007-12-03 17:05:38 +000070
Jan Kiszka661f1922011-10-16 11:53:13 +020071/* for pic/irq_info */
72#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010073#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020074#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010075#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020076
bellard9dc39cb2004-03-14 21:38:27 +000077//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000078//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000079
bellard9307c4c2004-04-04 12:57:25 +000080/*
81 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000082 *
bellard9307c4c2004-04-04 12:57:25 +000083 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000084 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000085 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080086 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010087 * 'O' option string of the form NAME=VALUE,...
88 * parsed according to QemuOptsList given by its name
89 * Example: 'device:O' uses qemu_device_opts.
90 * Restriction: only lists with empty desc are supported
91 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000092 * 'i' 32 bit integer
93 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -030094 * 'M' Non-negative target long (32 or 64 bit), in user mode the
95 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +020096 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +020097 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
98 * K, k suffix, which multiplies the value by 2^60 for suffixes E
99 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
100 * 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 +0100101 * 'T' double
102 * user mode accepts an optional ms, us, ns suffix,
103 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000104 * '/' optional gdb-like print format (like "/10x")
105 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300106 * '?' optional type (for all types, except '/')
107 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100108 * 'b' boolean
109 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300110 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000111 *
112 */
113
Adam Litke940cc302010-01-25 12:18:44 -0600114typedef struct MonitorCompletionData MonitorCompletionData;
115struct MonitorCompletionData {
116 Monitor *mon;
117 void (*user_print)(Monitor *mon, const QObject *data);
118};
119
Anthony Liguoric227f092009-10-01 16:12:16 -0500120typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000121 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000122 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000123 const char *params;
124 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300125 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300126 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300127 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200128 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600129 int (*cmd_async)(Monitor *mon, const QDict *params,
130 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300131 } mhandler;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200132 int flags;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800133 /* @sub_table is a list of 2nd level of commands. If it do not exist,
134 * mhandler should be used. If it exist, sub_table[?].mhandler should be
135 * used, and mhandler of 1st level plays the role of help function.
136 */
137 struct mon_cmd_t *sub_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500138} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000139
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100140/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500141typedef struct mon_fd_t mon_fd_t;
142struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100143 char *name;
144 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500145 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100146};
147
Corey Bryantba1c0482012-08-14 16:43:43 -0400148/* file descriptor associated with a file descriptor set */
149typedef struct MonFdsetFd MonFdsetFd;
150struct MonFdsetFd {
151 int fd;
152 bool removed;
153 char *opaque;
154 QLIST_ENTRY(MonFdsetFd) next;
155};
156
157/* file descriptor set containing fds passed via SCM_RIGHTS */
158typedef struct MonFdset MonFdset;
159struct MonFdset {
160 int64_t id;
161 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400162 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400163 QLIST_ENTRY(MonFdset) next;
164};
165
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200166typedef struct MonitorControl {
167 QObject *id;
168 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200169 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200170} MonitorControl;
171
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100172/*
173 * To prevent flooding clients, events can be throttled. The
174 * throttling is calculated globally, rather than per-Monitor
175 * instance.
176 */
177typedef struct MonitorEventState {
178 MonitorEvent event; /* Event being tracked */
179 int64_t rate; /* Period over which to throttle. 0 to disable */
180 int64_t last; /* Time at which event was last emitted */
181 QEMUTimer *timer; /* Timer for handling delayed events */
182 QObject *data; /* Event pending delayed dispatch */
183} MonitorEventState;
184
aliguori87127162009-03-05 23:01:29 +0000185struct Monitor {
186 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200187 int mux_out;
188 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000189 int flags;
190 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400191 bool skip_flush;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400192 QString *outbuf;
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200193 guint watch;
aliguori731b0362009-03-05 23:01:42 +0000194 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200195 MonitorControl *mc;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200196 CPUState *mon_cpu;
aliguori731b0362009-03-05 23:01:42 +0000197 BlockDriverCompletionFunc *password_completion_cb;
198 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800199 mon_cmd_t *cmd_table;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200200 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500201 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000202 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000203};
204
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300205/* QMP checker flags */
206#define QMP_ACCEPT_UNKNOWNS 1
207
Blue Swirl72cf2d42009-09-12 07:36:22 +0000208static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400209static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400210static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000211
Wayne Xia816f8922011-10-12 11:32:41 +0800212static mon_cmd_t mon_cmds[];
213static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000214
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300215static const mon_cmd_t qmp_cmds[];
216
Markus Armbruster8631b602010-02-18 11:41:55 +0100217Monitor *cur_mon;
218Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000219
aliguori731b0362009-03-05 23:01:42 +0000220static void monitor_command_cb(Monitor *mon, const char *cmdline,
221 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000222
Luiz Capitulino09069b12010-02-04 18:10:06 -0200223static inline int qmp_cmd_mode(const Monitor *mon)
224{
225 return (mon->mc ? mon->mc->command_mode : 0);
226}
227
Luiz Capitulino418173c2009-11-26 22:58:51 -0200228/* Return true if in control mode, false otherwise */
229static inline int monitor_ctrl_mode(const Monitor *mon)
230{
231 return (mon->flags & MONITOR_USE_CONTROL);
232}
233
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100234/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
235int monitor_cur_is_qmp(void)
236{
237 return cur_mon && monitor_ctrl_mode(cur_mon);
238}
239
Anthony Liguori7060b472011-09-02 12:34:50 -0500240void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000241{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200242 if (!mon->rs)
243 return;
244
aliguori731b0362009-03-05 23:01:42 +0000245 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
246 if (show_prompt)
247 readline_show_prompt(mon->rs);
248}
bellard6a00d602005-11-21 23:25:50 +0000249
Anthony Liguori7060b472011-09-02 12:34:50 -0500250int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
251 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000252{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100253 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100254 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100255 return -EINVAL;
256 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000257 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
258 /* prompt is printed on return from the command handler */
259 return 0;
260 } else {
261 monitor_printf(mon, "terminal does not support password prompting\n");
262 return -ENOTTY;
263 }
aliguoribb5fc202009-03-05 23:01:15 +0000264}
265
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100266static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
267 void *opaque)
268{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200269 Monitor *mon = opaque;
270
271 mon->watch = 0;
272 monitor_flush(mon);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100273 return FALSE;
274}
275
aliguori376253e2009-03-05 23:01:23 +0000276void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000277{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100278 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400279 size_t len;
280 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100281
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400282 if (mon->skip_flush) {
283 return;
284 }
285
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400286 buf = qstring_get_str(mon->outbuf);
287 len = qstring_get_length(mon->outbuf);
288
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200289 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400290 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
291 if (rc == len) {
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100292 /* all flushed */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400293 QDECREF(mon->outbuf);
294 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100295 return;
296 }
297 if (rc > 0) {
298 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400299 QString *tmp = qstring_from_str(buf + rc);
300 QDECREF(mon->outbuf);
301 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100302 }
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200303 if (mon->watch == 0) {
304 mon->watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT,
305 monitor_unblocked, mon);
306 }
bellard7e2515e2004-08-01 21:52:19 +0000307 }
308}
309
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400310/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000311static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000312{
ths60fe76f2007-12-16 03:02:09 +0000313 char c;
aliguori731b0362009-03-05 23:01:42 +0000314
bellard7e2515e2004-08-01 21:52:19 +0000315 for(;;) {
316 c = *str++;
317 if (c == '\0')
318 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400319 if (c == '\n') {
320 qstring_append_chr(mon->outbuf, '\r');
321 }
322 qstring_append_chr(mon->outbuf, c);
323 if (c == '\n') {
aliguori376253e2009-03-05 23:01:23 +0000324 monitor_flush(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400325 }
bellard7e2515e2004-08-01 21:52:19 +0000326 }
327}
328
aliguori376253e2009-03-05 23:01:23 +0000329void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000330{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400331 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200332
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200333 if (!mon)
334 return;
335
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200336 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200337 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200338 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200339
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400340 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200341 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400342 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000343}
344
aliguori376253e2009-03-05 23:01:23 +0000345void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000346{
347 va_list ap;
348 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000349 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000350 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000351}
352
aliguori376253e2009-03-05 23:01:23 +0000353void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000354{
355 int i;
356
357 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000358 switch (filename[i]) {
359 case ' ':
360 case '"':
361 case '\\':
362 monitor_printf(mon, "\\%c", filename[i]);
363 break;
364 case '\t':
365 monitor_printf(mon, "\\t");
366 break;
367 case '\r':
368 monitor_printf(mon, "\\r");
369 break;
370 case '\n':
371 monitor_printf(mon, "\\n");
372 break;
373 default:
374 monitor_printf(mon, "%c", filename[i]);
375 break;
376 }
thsfef30742006-12-22 14:11:32 +0000377 }
378}
379
Stefan Weil8b7968f2010-09-23 21:28:05 +0200380static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
381 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000382{
383 va_list ap;
384 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000385 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000386 va_end(ap);
387 return 0;
388}
389
Luiz Capitulino13c74252009-10-07 13:41:55 -0300390static void monitor_user_noop(Monitor *mon, const QObject *data) { }
391
Luiz Capitulino9e807212010-09-16 10:58:59 -0300392static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300393{
394 return cmd->user_print != NULL;
395}
396
Luiz Capitulino4903de02010-09-16 11:01:32 -0300397static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600398{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200399 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600400}
401
Luiz Capitulino8204a912009-11-18 23:05:31 -0200402static inline int monitor_has_error(const Monitor *mon)
403{
404 return mon->error != NULL;
405}
406
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200407static void monitor_json_emitter(Monitor *mon, const QObject *data)
408{
409 QString *json;
410
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200411 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
412 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200413 assert(json != NULL);
414
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200415 qstring_append_chr(json, '\n');
416 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200417
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200418 QDECREF(json);
419}
420
Luiz Capitulinode253f12012-07-27 16:18:16 -0300421static QDict *build_qmp_error_dict(const QError *err)
422{
423 QObject *obj;
424
425 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
426 ErrorClass_lookup[err->err_class],
427 qerror_human(err));
428
429 return qobject_to_qdict(obj);
430}
431
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200432static void monitor_protocol_emitter(Monitor *mon, QObject *data)
433{
434 QDict *qmp;
435
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100436 trace_monitor_protocol_emitter(mon);
437
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200438 if (!monitor_has_error(mon)) {
439 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300440 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200441 if (data) {
442 qobject_incref(data);
443 qdict_put_obj(qmp, "return", data);
444 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200445 /* return an empty QDict by default */
446 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200447 }
448 } else {
449 /* error response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300450 qmp = build_qmp_error_dict(mon->error);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200451 QDECREF(mon->error);
452 mon->error = NULL;
453 }
454
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200455 if (mon->mc->id) {
456 qdict_put_obj(qmp, "id", mon->mc->id);
457 mon->mc->id = NULL;
458 }
459
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200460 monitor_json_emitter(mon, QOBJECT(qmp));
461 QDECREF(qmp);
462}
463
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200464static void timestamp_put(QDict *qdict)
465{
466 int err;
467 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000468 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200469
Blue Swirld08d6f02009-12-04 18:06:39 +0000470 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200471 if (err < 0)
472 return;
473
474 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
475 "'microseconds': %" PRId64 " }",
476 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200477 qdict_put_obj(qdict, "timestamp", obj);
478}
479
Daniel P. Berrange48608532012-05-21 17:59:51 +0100480
481static const char *monitor_event_names[] = {
482 [QEVENT_SHUTDOWN] = "SHUTDOWN",
483 [QEVENT_RESET] = "RESET",
484 [QEVENT_POWERDOWN] = "POWERDOWN",
485 [QEVENT_STOP] = "STOP",
486 [QEVENT_RESUME] = "RESUME",
487 [QEVENT_VNC_CONNECTED] = "VNC_CONNECTED",
488 [QEVENT_VNC_INITIALIZED] = "VNC_INITIALIZED",
489 [QEVENT_VNC_DISCONNECTED] = "VNC_DISCONNECTED",
490 [QEVENT_BLOCK_IO_ERROR] = "BLOCK_IO_ERROR",
491 [QEVENT_RTC_CHANGE] = "RTC_CHANGE",
492 [QEVENT_WATCHDOG] = "WATCHDOG",
493 [QEVENT_SPICE_CONNECTED] = "SPICE_CONNECTED",
494 [QEVENT_SPICE_INITIALIZED] = "SPICE_INITIALIZED",
495 [QEVENT_SPICE_DISCONNECTED] = "SPICE_DISCONNECTED",
496 [QEVENT_BLOCK_JOB_COMPLETED] = "BLOCK_JOB_COMPLETED",
497 [QEVENT_BLOCK_JOB_CANCELLED] = "BLOCK_JOB_CANCELLED",
Paolo Bonzini32c81a42012-09-28 17:22:58 +0200498 [QEVENT_BLOCK_JOB_ERROR] = "BLOCK_JOB_ERROR",
Paolo Bonzinia66a2a32012-07-23 15:15:47 +0200499 [QEVENT_BLOCK_JOB_READY] = "BLOCK_JOB_READY",
Michael S. Tsirkin0402a5d2013-03-06 14:58:59 +0200500 [QEVENT_DEVICE_DELETED] = "DEVICE_DELETED",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100501 [QEVENT_DEVICE_TRAY_MOVED] = "DEVICE_TRAY_MOVED",
Amos Kongb1be4282013-06-14 15:45:52 +0800502 [QEVENT_NIC_RX_FILTER_CHANGED] = "NIC_RX_FILTER_CHANGED",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100503 [QEVENT_SUSPEND] = "SUSPEND",
Luiz Capitulino25df49f2012-08-08 17:03:01 -0300504 [QEVENT_SUSPEND_DISK] = "SUSPEND_DISK",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100505 [QEVENT_WAKEUP] = "WAKEUP",
Daniel P. Berrange973603a2012-06-14 18:12:56 +0100506 [QEVENT_BALLOON_CHANGE] = "BALLOON_CHANGE",
Yonit Halperin2fdd16e2012-08-21 11:51:57 +0300507 [QEVENT_SPICE_MIGRATE_COMPLETED] = "SPICE_MIGRATE_COMPLETED",
Hu Taoc401a8a2013-04-26 11:24:41 +0800508 [QEVENT_GUEST_PANICKED] = "GUEST_PANICKED",
Max Reitza40f1c22013-08-30 14:34:25 +0200509 [QEVENT_BLOCK_IMAGE_CORRUPTED] = "BLOCK_IMAGE_CORRUPTED",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100510};
511QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names) != QEVENT_MAX)
512
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100513MonitorEventState monitor_event_state[QEVENT_MAX];
514QemuMutex monitor_event_state_lock;
515
516/*
517 * Emits the event to every monitor instance
518 */
519static void
520monitor_protocol_event_emit(MonitorEvent event,
521 QObject *data)
522{
523 Monitor *mon;
524
525 trace_monitor_protocol_event_emit(event, data);
526 QLIST_FOREACH(mon, &mon_list, entry) {
527 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
528 monitor_json_emitter(mon, data);
529 }
530 }
531}
532
533
534/*
535 * Queue a new event for emission to Monitor instances,
536 * applying any rate limiting if required.
537 */
538static void
539monitor_protocol_event_queue(MonitorEvent event,
540 QObject *data)
541{
542 MonitorEventState *evstate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100543 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100544 assert(event < QEVENT_MAX);
545
546 qemu_mutex_lock(&monitor_event_state_lock);
547 evstate = &(monitor_event_state[event]);
548 trace_monitor_protocol_event_queue(event,
549 data,
550 evstate->rate,
551 evstate->last,
552 now);
553
554 /* Rate limit of 0 indicates no throttling */
555 if (!evstate->rate) {
556 monitor_protocol_event_emit(event, data);
557 evstate->last = now;
558 } else {
559 int64_t delta = now - evstate->last;
560 if (evstate->data ||
561 delta < evstate->rate) {
562 /* If there's an existing event pending, replace
563 * it with the new event, otherwise schedule a
564 * timer for delayed emission
565 */
566 if (evstate->data) {
567 qobject_decref(evstate->data);
568 } else {
569 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100570 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100571 }
572 evstate->data = data;
573 qobject_incref(evstate->data);
574 } else {
575 monitor_protocol_event_emit(event, data);
576 evstate->last = now;
577 }
578 }
579 qemu_mutex_unlock(&monitor_event_state_lock);
580}
581
582
583/*
584 * The callback invoked by QemuTimer when a delayed
585 * event is ready to be emitted
586 */
587static void monitor_protocol_event_handler(void *opaque)
588{
589 MonitorEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100590 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100591
592 qemu_mutex_lock(&monitor_event_state_lock);
593
594 trace_monitor_protocol_event_handler(evstate->event,
595 evstate->data,
596 evstate->last,
597 now);
598 if (evstate->data) {
599 monitor_protocol_event_emit(evstate->event, evstate->data);
600 qobject_decref(evstate->data);
601 evstate->data = NULL;
602 }
603 evstate->last = now;
604 qemu_mutex_unlock(&monitor_event_state_lock);
605}
606
607
608/*
609 * @event: the event ID to be limited
610 * @rate: the rate limit in milliseconds
611 *
612 * Sets a rate limit on a particular event, so no
613 * more than 1 event will be emitted within @rate
614 * milliseconds
615 */
616static void
617monitor_protocol_event_throttle(MonitorEvent event,
618 int64_t rate)
619{
620 MonitorEventState *evstate;
621 assert(event < QEVENT_MAX);
622
623 evstate = &(monitor_event_state[event]);
624
625 trace_monitor_protocol_event_throttle(event, rate);
626 evstate->event = event;
627 evstate->rate = rate * SCALE_MS;
Alex Blighbc72ad62013-08-21 16:03:08 +0100628 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100629 SCALE_MS,
630 monitor_protocol_event_handler,
631 evstate);
632 evstate->last = 0;
633 evstate->data = NULL;
634}
635
636
637/* Global, one-time initializer to configure the rate limiting
638 * and initialize state */
639static void monitor_protocol_event_init(void)
640{
641 qemu_mutex_init(&monitor_event_state_lock);
642 /* Limit RTC & BALLOON events to 1 per second */
643 monitor_protocol_event_throttle(QEVENT_RTC_CHANGE, 1000);
644 monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE, 1000);
645 monitor_protocol_event_throttle(QEVENT_WATCHDOG, 1000);
646}
647
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200648/**
649 * monitor_protocol_event(): Generate a Monitor event
650 *
651 * Event-specific data can be emitted through the (optional) 'data' parameter.
652 */
653void monitor_protocol_event(MonitorEvent event, QObject *data)
654{
655 QDict *qmp;
656 const char *event_name;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200657
Blue Swirl242cd002009-12-04 18:05:45 +0000658 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200659
Daniel P. Berrange48608532012-05-21 17:59:51 +0100660 event_name = monitor_event_names[event];
661 assert(event_name != NULL);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200662
663 qmp = qdict_new();
664 timestamp_put(qmp);
665 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200666 if (data) {
667 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200668 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200669 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200670
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100671 trace_monitor_protocol_event(event, event_name, qmp);
672 monitor_protocol_event_queue(event, QOBJECT(qmp));
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200673 QDECREF(qmp);
674}
675
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200676static int do_qmp_capabilities(Monitor *mon, const QDict *params,
677 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200678{
679 /* Will setup QMP capabilities in the future */
680 if (monitor_ctrl_mode(mon)) {
681 mon->mc->command_mode = 1;
682 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200683
684 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200685}
686
Luiz Capitulino0268d972010-10-22 10:08:02 -0200687static void handle_user_command(Monitor *mon, const char *cmdline);
688
Wenchao Xiab01fe892013-08-27 20:38:19 +0800689static void monitor_data_init(Monitor *mon)
690{
691 memset(mon, 0, sizeof(Monitor));
692 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800693 /* Use *mon_cmds by default. */
694 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800695}
696
697static void monitor_data_destroy(Monitor *mon)
698{
699 QDECREF(mon->outbuf);
700}
701
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200702char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
703 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200704{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200705 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200706 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200707
Wenchao Xiab01fe892013-08-27 20:38:19 +0800708 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400709 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200710
711 old_mon = cur_mon;
712 cur_mon = &hmp;
713
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200714 if (has_cpu_index) {
715 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200716 if (ret < 0) {
717 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200718 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
719 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200720 goto out;
721 }
722 }
723
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200724 handle_user_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200725 cur_mon = old_mon;
726
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400727 if (qstring_get_length(hmp.outbuf) > 0) {
728 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200729 } else {
730 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200731 }
732
733out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800734 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200735 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200736}
737
bellard9dc39cb2004-03-14 21:38:27 +0000738static int compare_cmd(const char *name, const char *list)
739{
740 const char *p, *pstart;
741 int len;
742 len = strlen(name);
743 p = list;
744 for(;;) {
745 pstart = p;
746 p = strchr(p, '|');
747 if (!p)
748 p = pstart + strlen(pstart);
749 if ((p - pstart) == len && !memcmp(pstart, name, len))
750 return 1;
751 if (*p == '\0')
752 break;
753 p++;
754 }
755 return 0;
756}
757
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800758static int get_str(char *buf, int buf_size, const char **pp)
759{
760 const char *p;
761 char *q;
762 int c;
763
764 q = buf;
765 p = *pp;
766 while (qemu_isspace(*p)) {
767 p++;
768 }
769 if (*p == '\0') {
770 fail:
771 *q = '\0';
772 *pp = p;
773 return -1;
774 }
775 if (*p == '\"') {
776 p++;
777 while (*p != '\0' && *p != '\"') {
778 if (*p == '\\') {
779 p++;
780 c = *p++;
781 switch (c) {
782 case 'n':
783 c = '\n';
784 break;
785 case 'r':
786 c = '\r';
787 break;
788 case '\\':
789 case '\'':
790 case '\"':
791 break;
792 default:
793 qemu_printf("unsupported escape code: '\\%c'\n", c);
794 goto fail;
795 }
796 if ((q - buf) < buf_size - 1) {
797 *q++ = c;
798 }
799 } else {
800 if ((q - buf) < buf_size - 1) {
801 *q++ = *p;
802 }
803 p++;
804 }
805 }
806 if (*p != '\"') {
807 qemu_printf("unterminated string\n");
808 goto fail;
809 }
810 p++;
811 } else {
812 while (*p != '\0' && !qemu_isspace(*p)) {
813 if ((q - buf) < buf_size - 1) {
814 *q++ = *p;
815 }
816 p++;
817 }
818 }
819 *q = '\0';
820 *pp = p;
821 return 0;
822}
823
824#define MAX_ARGS 16
825
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800826static void free_cmdline_args(char **args, int nb_args)
827{
828 int i;
829
830 assert(nb_args <= MAX_ARGS);
831
832 for (i = 0; i < nb_args; i++) {
833 g_free(args[i]);
834 }
835
836}
837
838/*
839 * Parse the command line to get valid args.
840 * @cmdline: command line to be parsed.
841 * @pnb_args: location to store the number of args, must NOT be NULL.
842 * @args: location to store the args, which should be freed by caller, must
843 * NOT be NULL.
844 *
845 * Returns 0 on success, negative on failure.
846 *
847 * NOTE: this parser is an approximate form of the real command parser. Number
848 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
849 * return with failure.
850 */
851static int parse_cmdline(const char *cmdline,
852 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800853{
854 const char *p;
855 int nb_args, ret;
856 char buf[1024];
857
858 p = cmdline;
859 nb_args = 0;
860 for (;;) {
861 while (qemu_isspace(*p)) {
862 p++;
863 }
864 if (*p == '\0') {
865 break;
866 }
867 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800868 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800869 }
870 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800871 if (ret < 0) {
872 goto fail;
873 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800874 args[nb_args] = g_strdup(buf);
875 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800876 }
877 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800878 return 0;
879
880 fail:
881 free_cmdline_args(args, nb_args);
882 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800883}
884
Wenchao Xia66855492013-08-27 20:38:23 +0800885static void help_cmd_dump_one(Monitor *mon,
886 const mon_cmd_t *cmd,
887 char **prefix_args,
888 int prefix_args_nb)
889{
890 int i;
891
892 for (i = 0; i < prefix_args_nb; i++) {
893 monitor_printf(mon, "%s ", prefix_args[i]);
894 }
895 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
896}
897
898/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500899static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800900 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000901{
Anthony Liguoric227f092009-10-01 16:12:16 -0500902 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000903
Wenchao Xia66855492013-08-27 20:38:23 +0800904 /* No valid arg need to compare with, dump all in *cmds */
905 if (arg_index >= nb_args) {
906 for (cmd = cmds; cmd->name != NULL; cmd++) {
907 help_cmd_dump_one(mon, cmd, args, arg_index);
908 }
909 return;
910 }
911
912 /* Find one entry to dump */
913 for (cmd = cmds; cmd->name != NULL; cmd++) {
914 if (compare_cmd(args[arg_index], cmd->name)) {
915 if (cmd->sub_table) {
916 /* continue with next arg */
917 help_cmd_dump(mon, cmd->sub_table,
918 args, nb_args, arg_index + 1);
919 } else {
920 help_cmd_dump_one(mon, cmd, args, arg_index);
921 }
922 break;
923 }
bellard9dc39cb2004-03-14 21:38:27 +0000924 }
925}
926
aliguori376253e2009-03-05 23:01:23 +0000927static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000928{
Wenchao Xia66855492013-08-27 20:38:23 +0800929 char *args[MAX_ARGS];
930 int nb_args = 0;
931
932 /* 1. parse user input */
933 if (name) {
934 /* special case for log, directly dump and return */
935 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000936 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000937 monitor_printf(mon, "Log items (comma separated):\n");
938 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000939 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000940 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000941 }
Wenchao Xia66855492013-08-27 20:38:23 +0800942 return;
943 }
944
945 if (parse_cmdline(name, &nb_args, args) < 0) {
946 return;
bellardf193c792004-03-21 17:06:25 +0000947 }
bellard9dc39cb2004-03-14 21:38:27 +0000948 }
Wenchao Xia66855492013-08-27 20:38:23 +0800949
950 /* 2. dump the contents according to parsed args */
951 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
952
953 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000954}
955
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300956static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300957{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300958 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300959}
960
Lluísfc764102011-08-31 20:31:18 +0200961static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530962{
963 const char *tp_name = qdict_get_str(qdict, "name");
964 bool new_state = qdict_get_bool(qdict, "option");
Blue Swirlf871d682010-10-13 19:14:29 +0000965
Lluís Vilanovaca285c32013-03-05 14:47:43 +0100966 bool found = false;
967 TraceEvent *ev = NULL;
968 while ((ev = trace_event_pattern(tp_name, ev)) != NULL) {
969 found = true;
970 if (!trace_event_get_state_static(ev)) {
971 monitor_printf(mon, "event \"%s\" is not traceable\n", tp_name);
972 } else {
973 trace_event_set_state_dynamic(ev, new_state);
974 }
975 }
976 if (!trace_event_is_pattern(tp_name) && !found) {
Blue Swirlf871d682010-10-13 19:14:29 +0000977 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
978 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530979}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100980
Michael Rothc45a8162011-10-02 08:44:37 -0500981#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100982static void do_trace_file(Monitor *mon, const QDict *qdict)
983{
984 const char *op = qdict_get_try_str(qdict, "op");
985 const char *arg = qdict_get_try_str(qdict, "arg");
986
987 if (!op) {
988 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
989 } else if (!strcmp(op, "on")) {
990 st_set_trace_file_enabled(true);
991 } else if (!strcmp(op, "off")) {
992 st_set_trace_file_enabled(false);
993 } else if (!strcmp(op, "flush")) {
994 st_flush_trace_buffer();
995 } else if (!strcmp(op, "set")) {
996 if (arg) {
997 st_set_trace_file(arg);
998 }
999 } else {
1000 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
1001 help_cmd(mon, "trace-file");
1002 }
1003}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301004#endif
1005
Adam Litke940cc302010-01-25 12:18:44 -06001006static void user_monitor_complete(void *opaque, QObject *ret_data)
1007{
1008 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
1009
1010 if (ret_data) {
1011 data->user_print(data->mon, ret_data);
1012 }
1013 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -05001014 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -06001015}
1016
1017static void qmp_monitor_complete(void *opaque, QObject *ret_data)
1018{
1019 monitor_protocol_emitter(opaque, ret_data);
1020}
1021
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03001022static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
1023 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -06001024{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03001025 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -06001026}
1027
Adam Litke940cc302010-01-25 12:18:44 -06001028static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
1029 const QDict *params)
1030{
1031 int ret;
1032
Anthony Liguori7267c092011-08-20 22:09:37 -05001033 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -06001034 cb_data->mon = mon;
1035 cb_data->user_print = cmd->user_print;
1036 monitor_suspend(mon);
1037 ret = cmd->mhandler.cmd_async(mon, params,
1038 user_monitor_complete, cb_data);
1039 if (ret < 0) {
1040 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -05001041 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -06001042 }
1043}
1044
Wenchao Xia84c44612013-01-14 14:06:29 +08001045static void do_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +00001046{
Luiz Capitulino13c74252009-10-07 13:41:55 -03001047 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +00001048}
1049
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -03001050CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +00001051{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -03001052 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001053 const mon_cmd_t *cmd;
1054
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03001055 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -02001056 info = g_malloc0(sizeof(*info));
1057 info->value = g_malloc0(sizeof(*info->value));
1058 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001059
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -03001060 info->next = cmd_list;
1061 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001062 }
1063
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -03001064 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +00001065}
1066
Daniel P. Berrange48608532012-05-21 17:59:51 +01001067EventInfoList *qmp_query_events(Error **errp)
1068{
1069 EventInfoList *info, *ev_list = NULL;
1070 MonitorEvent e;
1071
1072 for (e = 0 ; e < QEVENT_MAX ; e++) {
1073 const char *event_name = monitor_event_names[e];
1074 assert(event_name != NULL);
1075 info = g_malloc0(sizeof(*info));
1076 info->value = g_malloc0(sizeof(*info->value));
1077 info->value->name = g_strdup(event_name);
1078
1079 info->next = ev_list;
1080 ev_list = info;
1081 }
1082
1083 return ev_list;
1084}
1085
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001086/* set the current CPU defined by the user */
1087int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001088{
Andreas Färber55e5c282012-12-17 06:18:02 +01001089 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001090
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001091 cpu = qemu_get_cpu(cpu_index);
1092 if (cpu == NULL) {
1093 return -1;
bellard6a00d602005-11-21 23:25:50 +00001094 }
Andreas Färbercb446ec2013-05-01 14:24:52 +02001095 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001096 return 0;
bellard6a00d602005-11-21 23:25:50 +00001097}
1098
Andreas Färber9349b4f2012-03-14 01:38:32 +01001099static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001100{
aliguori731b0362009-03-05 23:01:42 +00001101 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001102 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +00001103 }
Avi Kivity4c0960c2009-08-17 23:19:53 +03001104 cpu_synchronize_state(cur_mon->mon_cpu);
Andreas Färbercb446ec2013-05-01 14:24:52 +02001105 return cur_mon->mon_cpu->env_ptr;
bellard6a00d602005-11-21 23:25:50 +00001106}
1107
Luiz Capitulino99b77962011-10-24 10:53:44 -02001108int monitor_get_cpu_index(void)
1109{
Andreas Färber55e5c282012-12-17 06:18:02 +01001110 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
1111 return cpu->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001112}
1113
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001114static void do_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001115{
Andreas Färber878096e2013-05-27 01:33:50 +02001116 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001117 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +00001118 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001119 cpu = ENV_GET_CPU(env);
1120 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +00001121}
1122
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001123static void do_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001124{
aliguori376253e2009-03-05 23:01:23 +00001125 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001126}
1127
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001128static void do_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001129{
1130 int i;
bellard7e2515e2004-08-01 21:52:19 +00001131 const char *str;
ths3b46e622007-09-17 08:09:54 +00001132
aliguoricde76ee2009-03-05 23:01:51 +00001133 if (!mon->rs)
1134 return;
bellard7e2515e2004-08-01 21:52:19 +00001135 i = 0;
1136 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001137 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001138 if (!str)
1139 break;
aliguori376253e2009-03-05 23:01:23 +00001140 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001141 i++;
bellardaa455482004-04-04 13:07:25 +00001142 }
1143}
1144
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001145static void do_info_cpu_stats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001146{
Andreas Färber878096e2013-05-27 01:33:50 +02001147 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001148 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +00001149
1150 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001151 cpu = ENV_GET_CPU(env);
1152 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001153}
j_mayer76a66252007-03-07 08:32:30 +00001154
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001155static void do_trace_print_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301156{
Lluísfc764102011-08-31 20:31:18 +02001157 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301158}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301159
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001160static int client_migrate_info(Monitor *mon, const QDict *qdict,
1161 MonitorCompletion cb, void *opaque)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001162{
1163 const char *protocol = qdict_get_str(qdict, "protocol");
1164 const char *hostname = qdict_get_str(qdict, "hostname");
1165 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1166 int port = qdict_get_try_int(qdict, "port", -1);
1167 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1168 int ret;
1169
1170 if (strcmp(protocol, "spice") == 0) {
1171 if (!using_spice) {
1172 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1173 return -1;
1174 }
1175
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001176 if (port == -1 && tls_port == -1) {
1177 qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
1178 return -1;
1179 }
1180
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001181 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
1182 cb, opaque);
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001183 if (ret != 0) {
1184 qerror_report(QERR_UNDEFINED_ERROR);
1185 return -1;
1186 }
1187 return 0;
1188 }
1189
1190 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1191 return -1;
1192}
1193
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001194static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001195{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001196 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001197}
1198
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001199static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001200{
1201 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001202 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001203
bellard9307c4c2004-04-04 12:57:25 +00001204 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001205 mask = 0;
1206 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001207 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001208 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001209 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001210 return;
1211 }
1212 }
Peter Maydell24537a02013-02-11 16:41:23 +00001213 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001214}
1215
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001216static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001217{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001218 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001219 if (!option || !strcmp(option, "on")) {
1220 singlestep = 1;
1221 } else if (!strcmp(option, "off")) {
1222 singlestep = 0;
1223 } else {
1224 monitor_printf(mon, "unexpected option %s\n", option);
1225 }
1226}
1227
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001228static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001229{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001230 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001231 if (!device)
1232 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1233 if (gdbserver_start(device) < 0) {
1234 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1235 device);
1236 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001237 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001238 } else {
aliguori59030a82009-04-05 18:43:41 +00001239 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1240 device);
bellard8a7ddc32004-03-31 19:00:16 +00001241 }
1242}
1243
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001244static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001245{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001246 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001247 if (select_watchdog_action(action) == -1) {
1248 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1249 }
1250}
1251
aliguori376253e2009-03-05 23:01:23 +00001252static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001253{
aliguori376253e2009-03-05 23:01:23 +00001254 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001255 switch(c) {
1256 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001257 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001258 break;
1259 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001260 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001261 break;
1262 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001263 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001264 break;
1265 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001266 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001267 break;
1268 default:
1269 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001270 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001271 } else {
aliguori376253e2009-03-05 23:01:23 +00001272 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001273 }
1274 break;
1275 }
aliguori376253e2009-03-05 23:01:23 +00001276 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001277}
1278
aliguori376253e2009-03-05 23:01:23 +00001279static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001280 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001281{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001282 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001283 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001284 uint8_t buf[16];
1285 uint64_t v;
1286
1287 if (format == 'i') {
1288 int flags;
1289 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001290 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001291#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001292 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001293 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001294 } else if (wsize == 4) {
1295 flags = 0;
1296 } else {
bellard6a15fd12006-04-12 21:07:07 +00001297 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001298 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001299 if (env) {
1300#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001301 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001302 (env->segs[R_CS].flags & DESC_L_MASK))
1303 flags = 2;
1304 else
1305#endif
1306 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1307 flags = 1;
1308 }
bellard4c27ba22004-04-25 18:05:08 +00001309 }
1310#endif
aliguori376253e2009-03-05 23:01:23 +00001311 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001312 return;
1313 }
1314
1315 len = wsize * count;
1316 if (wsize == 1)
1317 line_size = 8;
1318 else
1319 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001320 max_digits = 0;
1321
1322 switch(format) {
1323 case 'o':
1324 max_digits = (wsize * 8 + 2) / 3;
1325 break;
1326 default:
1327 case 'x':
1328 max_digits = (wsize * 8) / 4;
1329 break;
1330 case 'u':
1331 case 'd':
1332 max_digits = (wsize * 8 * 10 + 32) / 33;
1333 break;
1334 case 'c':
1335 wsize = 1;
1336 break;
1337 }
1338
1339 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001340 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001341 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001342 else
aliguori376253e2009-03-05 23:01:23 +00001343 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001344 l = len;
1345 if (l > line_size)
1346 l = line_size;
1347 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001348 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001349 } else {
bellard6a00d602005-11-21 23:25:50 +00001350 env = mon_get_cpu();
Andreas Färberf17ec442013-06-29 19:40:58 +02001351 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001352 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001353 break;
1354 }
bellard9307c4c2004-04-04 12:57:25 +00001355 }
ths5fafdf22007-09-16 21:08:06 +00001356 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001357 while (i < l) {
1358 switch(wsize) {
1359 default:
1360 case 1:
1361 v = ldub_raw(buf + i);
1362 break;
1363 case 2:
1364 v = lduw_raw(buf + i);
1365 break;
1366 case 4:
bellard92a31b12005-02-10 22:00:52 +00001367 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001368 break;
1369 case 8:
1370 v = ldq_raw(buf + i);
1371 break;
1372 }
aliguori376253e2009-03-05 23:01:23 +00001373 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001374 switch(format) {
1375 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001376 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001377 break;
1378 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001379 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001380 break;
1381 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001382 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001383 break;
1384 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001385 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001386 break;
1387 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001388 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001389 break;
1390 }
1391 i += wsize;
1392 }
aliguori376253e2009-03-05 23:01:23 +00001393 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001394 addr += l;
1395 len -= l;
1396 }
1397}
1398
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001399static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001400{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001401 int count = qdict_get_int(qdict, "count");
1402 int format = qdict_get_int(qdict, "format");
1403 int size = qdict_get_int(qdict, "size");
1404 target_long addr = qdict_get_int(qdict, "addr");
1405
aliguori376253e2009-03-05 23:01:23 +00001406 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001407}
1408
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001409static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001410{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001411 int count = qdict_get_int(qdict, "count");
1412 int format = qdict_get_int(qdict, "format");
1413 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001414 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001415
aliguori376253e2009-03-05 23:01:23 +00001416 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001417}
1418
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001419static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001420{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001421 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001422 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001423
bellard9307c4c2004-04-04 12:57:25 +00001424 switch(format) {
1425 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001426 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001427 break;
1428 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001429 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001430 break;
1431 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001432 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001433 break;
1434 default:
1435 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001436 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001437 break;
1438 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001439 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001440 break;
1441 }
aliguori376253e2009-03-05 23:01:23 +00001442 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001443}
1444
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001445static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001446{
1447 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001448 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001449 uint32_t start = qdict_get_int(qdict, "start");
1450 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001451
1452 sum = 0;
1453 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001454 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001455 /* BSD sum algorithm ('sum' Unix command) */
1456 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001457 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001458 }
aliguori376253e2009-03-05 23:01:23 +00001459 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001460}
1461
bellard13224a82006-07-14 22:03:35 +00001462static int mouse_button_state;
1463
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001464static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001465{
1466 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001467 const char *dx_str = qdict_get_str(qdict, "dx_str");
1468 const char *dy_str = qdict_get_str(qdict, "dy_str");
1469 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001470 dx = strtol(dx_str, NULL, 0);
1471 dy = strtol(dy_str, NULL, 0);
1472 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001473 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001474 dz = strtol(dz_str, NULL, 0);
1475 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1476}
1477
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001478static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001479{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001480 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001481 mouse_button_state = button_state;
1482 kbd_mouse_event(0, 0, 0, mouse_button_state);
1483}
1484
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001485static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001486{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001487 int size = qdict_get_int(qdict, "size");
1488 int addr = qdict_get_int(qdict, "addr");
1489 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001490 uint32_t val;
1491 int suffix;
1492
1493 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001494 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001495 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001496 addr++;
1497 }
1498 addr &= 0xffff;
1499
1500 switch(size) {
1501 default:
1502 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001503 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001504 suffix = 'b';
1505 break;
1506 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001507 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001508 suffix = 'w';
1509 break;
1510 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001511 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001512 suffix = 'l';
1513 break;
1514 }
aliguori376253e2009-03-05 23:01:23 +00001515 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1516 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001517}
bellarda3a91a32004-06-04 11:06:21 +00001518
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001519static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001520{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001521 int size = qdict_get_int(qdict, "size");
1522 int addr = qdict_get_int(qdict, "addr");
1523 int val = qdict_get_int(qdict, "val");
1524
Jan Kiszkaf1147842009-07-14 10:20:11 +02001525 addr &= IOPORTS_MASK;
1526
1527 switch (size) {
1528 default:
1529 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001530 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001531 break;
1532 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001533 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001534 break;
1535 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001536 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001537 break;
1538 }
1539}
1540
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001541static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001542{
1543 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001544 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001545
Jan Kiszka76e30d02009-07-02 00:19:02 +02001546 res = qemu_boot_set(bootdevice);
1547 if (res == 0) {
1548 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1549 } else if (res > 0) {
1550 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001551 } else {
aliguori376253e2009-03-05 23:01:23 +00001552 monitor_printf(mon, "no function defined to set boot device list for "
1553 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001554 }
1555}
1556
bellardb86bda52004-09-18 19:32:46 +00001557#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001558static void print_pte(Monitor *mon, hwaddr addr,
1559 hwaddr pte,
1560 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001561{
Blue Swirld65aaf32010-12-11 18:56:24 +00001562#ifdef TARGET_X86_64
1563 if (addr & (1ULL << 47)) {
1564 addr |= -1LL << 48;
1565 }
1566#endif
1567 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1568 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001569 addr,
1570 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001571 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001572 pte & PG_GLOBAL_MASK ? 'G' : '-',
1573 pte & PG_PSE_MASK ? 'P' : '-',
1574 pte & PG_DIRTY_MASK ? 'D' : '-',
1575 pte & PG_ACCESSED_MASK ? 'A' : '-',
1576 pte & PG_PCD_MASK ? 'C' : '-',
1577 pte & PG_PWT_MASK ? 'T' : '-',
1578 pte & PG_USER_MASK ? 'U' : '-',
1579 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001580}
1581
Andreas Färber9349b4f2012-03-14 01:38:32 +01001582static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001583{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001584 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001585 uint32_t pgd, pde, pte;
1586
bellardb86bda52004-09-18 19:32:46 +00001587 pgd = env->cr[3] & ~0xfff;
1588 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001589 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001590 pde = le32_to_cpu(pde);
1591 if (pde & PG_PRESENT_MASK) {
1592 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001593 /* 4M pages */
1594 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001595 } else {
1596 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001597 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001598 pte = le32_to_cpu(pte);
1599 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001600 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001601 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001602 ~0xfff);
1603 }
1604 }
1605 }
1606 }
1607 }
1608}
1609
Andreas Färber9349b4f2012-03-14 01:38:32 +01001610static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001611{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001612 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001613 uint64_t pdpe, pde, pte;
1614 uint64_t pdp_addr, pd_addr, pt_addr;
1615
1616 pdp_addr = env->cr[3] & ~0x1f;
1617 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001618 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001619 pdpe = le64_to_cpu(pdpe);
1620 if (pdpe & PG_PRESENT_MASK) {
1621 pd_addr = pdpe & 0x3fffffffff000ULL;
1622 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001623 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001624 pde = le64_to_cpu(pde);
1625 if (pde & PG_PRESENT_MASK) {
1626 if (pde & PG_PSE_MASK) {
1627 /* 2M pages with PAE, CR4.PSE is ignored */
1628 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001629 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001630 } else {
1631 pt_addr = pde & 0x3fffffffff000ULL;
1632 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001633 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001634 pte = le64_to_cpu(pte);
1635 if (pte & PG_PRESENT_MASK) {
1636 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1637 + (l3 << 12),
1638 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001639 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001640 }
1641 }
1642 }
1643 }
1644 }
1645 }
1646 }
1647}
1648
1649#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001650static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001651{
1652 uint64_t l1, l2, l3, l4;
1653 uint64_t pml4e, pdpe, pde, pte;
1654 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1655
1656 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1657 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001658 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001659 pml4e = le64_to_cpu(pml4e);
1660 if (pml4e & PG_PRESENT_MASK) {
1661 pdp_addr = pml4e & 0x3fffffffff000ULL;
1662 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001663 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001664 pdpe = le64_to_cpu(pdpe);
1665 if (pdpe & PG_PRESENT_MASK) {
1666 if (pdpe & PG_PSE_MASK) {
1667 /* 1G pages, CR4.PSE is ignored */
1668 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1669 0x3ffffc0000000ULL);
1670 } else {
1671 pd_addr = pdpe & 0x3fffffffff000ULL;
1672 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001673 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001674 pde = le64_to_cpu(pde);
1675 if (pde & PG_PRESENT_MASK) {
1676 if (pde & PG_PSE_MASK) {
1677 /* 2M pages, CR4.PSE is ignored */
1678 print_pte(mon, (l1 << 39) + (l2 << 30) +
1679 (l3 << 21), pde,
1680 0x3ffffffe00000ULL);
1681 } else {
1682 pt_addr = pde & 0x3fffffffff000ULL;
1683 for (l4 = 0; l4 < 512; l4++) {
1684 cpu_physical_memory_read(pt_addr
1685 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001686 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001687 pte = le64_to_cpu(pte);
1688 if (pte & PG_PRESENT_MASK) {
1689 print_pte(mon, (l1 << 39) +
1690 (l2 << 30) +
1691 (l3 << 21) + (l4 << 12),
1692 pte & ~PG_PSE_MASK,
1693 0x3fffffffff000ULL);
1694 }
1695 }
1696 }
1697 }
1698 }
1699 }
1700 }
1701 }
1702 }
1703 }
1704}
1705#endif
1706
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001707static void tlb_info(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001708{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001709 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001710
1711 env = mon_get_cpu();
1712
1713 if (!(env->cr[0] & CR0_PG_MASK)) {
1714 monitor_printf(mon, "PG disabled\n");
1715 return;
1716 }
1717 if (env->cr[4] & CR4_PAE_MASK) {
1718#ifdef TARGET_X86_64
1719 if (env->hflags & HF_LMA_MASK) {
1720 tlb_info_64(mon, env);
1721 } else
1722#endif
1723 {
1724 tlb_info_pae32(mon, env);
1725 }
1726 } else {
1727 tlb_info_32(mon, env);
1728 }
1729}
1730
Avi Kivitya8170e52012-10-23 12:30:10 +02001731static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001732 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001733 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001734{
bellard9746b152004-11-11 18:30:24 +00001735 int prot1;
1736 prot1 = *plast_prot;
1737 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001738 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001739 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1740 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001741 *pstart, end, end - *pstart,
1742 prot1 & PG_USER_MASK ? 'u' : '-',
1743 'r',
1744 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001745 }
1746 if (prot != 0)
1747 *pstart = end;
1748 else
1749 *pstart = -1;
1750 *plast_prot = prot;
1751 }
1752}
1753
Andreas Färber9349b4f2012-03-14 01:38:32 +01001754static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001755{
Austin Clementsb49ca722011-08-14 23:19:21 -04001756 unsigned int l1, l2;
1757 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001758 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001759 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001760
bellardb86bda52004-09-18 19:32:46 +00001761 pgd = env->cr[3] & ~0xfff;
1762 last_prot = 0;
1763 start = -1;
1764 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001765 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001766 pde = le32_to_cpu(pde);
1767 end = l1 << 22;
1768 if (pde & PG_PRESENT_MASK) {
1769 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1770 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001771 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001772 } else {
1773 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001774 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001775 pte = le32_to_cpu(pte);
1776 end = (l1 << 22) + (l2 << 12);
1777 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001778 prot = pte & pde &
1779 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001780 } else {
1781 prot = 0;
1782 }
aliguori376253e2009-03-05 23:01:23 +00001783 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001784 }
1785 }
1786 } else {
1787 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001788 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001789 }
1790 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001791 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001792 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001793}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001794
Andreas Färber9349b4f2012-03-14 01:38:32 +01001795static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001796{
Austin Clementsb49ca722011-08-14 23:19:21 -04001797 unsigned int l1, l2, l3;
1798 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001799 uint64_t pdpe, pde, pte;
1800 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001801 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001802
1803 pdp_addr = env->cr[3] & ~0x1f;
1804 last_prot = 0;
1805 start = -1;
1806 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001807 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001808 pdpe = le64_to_cpu(pdpe);
1809 end = l1 << 30;
1810 if (pdpe & PG_PRESENT_MASK) {
1811 pd_addr = pdpe & 0x3fffffffff000ULL;
1812 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001813 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001814 pde = le64_to_cpu(pde);
1815 end = (l1 << 30) + (l2 << 21);
1816 if (pde & PG_PRESENT_MASK) {
1817 if (pde & PG_PSE_MASK) {
1818 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1819 PG_PRESENT_MASK);
1820 mem_print(mon, &start, &last_prot, end, prot);
1821 } else {
1822 pt_addr = pde & 0x3fffffffff000ULL;
1823 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001824 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001825 pte = le64_to_cpu(pte);
1826 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1827 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001828 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1829 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001830 } else {
1831 prot = 0;
1832 }
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 }
1841 } else {
1842 prot = 0;
1843 mem_print(mon, &start, &last_prot, end, prot);
1844 }
1845 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001846 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001847 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001848}
1849
1850
1851#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001852static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001853{
1854 int prot, last_prot;
1855 uint64_t l1, l2, l3, l4;
1856 uint64_t pml4e, pdpe, pde, pte;
1857 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1858
1859 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1860 last_prot = 0;
1861 start = -1;
1862 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001863 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001864 pml4e = le64_to_cpu(pml4e);
1865 end = l1 << 39;
1866 if (pml4e & PG_PRESENT_MASK) {
1867 pdp_addr = pml4e & 0x3fffffffff000ULL;
1868 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001869 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001870 pdpe = le64_to_cpu(pdpe);
1871 end = (l1 << 39) + (l2 << 30);
1872 if (pdpe & PG_PRESENT_MASK) {
1873 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001874 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1875 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001876 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001877 mem_print(mon, &start, &last_prot, end, prot);
1878 } else {
1879 pd_addr = pdpe & 0x3fffffffff000ULL;
1880 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001881 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001882 pde = le64_to_cpu(pde);
1883 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1884 if (pde & PG_PRESENT_MASK) {
1885 if (pde & PG_PSE_MASK) {
1886 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1887 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001888 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001889 mem_print(mon, &start, &last_prot, end, prot);
1890 } else {
1891 pt_addr = pde & 0x3fffffffff000ULL;
1892 for (l4 = 0; l4 < 512; l4++) {
1893 cpu_physical_memory_read(pt_addr
1894 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001895 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001896 pte = le64_to_cpu(pte);
1897 end = (l1 << 39) + (l2 << 30) +
1898 (l3 << 21) + (l4 << 12);
1899 if (pte & PG_PRESENT_MASK) {
1900 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1901 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001902 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001903 } else {
1904 prot = 0;
1905 }
1906 mem_print(mon, &start, &last_prot, end, prot);
1907 }
1908 }
1909 } else {
1910 prot = 0;
1911 mem_print(mon, &start, &last_prot, end, prot);
1912 }
1913 }
1914 }
1915 } else {
1916 prot = 0;
1917 mem_print(mon, &start, &last_prot, end, prot);
1918 }
1919 }
1920 } else {
1921 prot = 0;
1922 mem_print(mon, &start, &last_prot, end, prot);
1923 }
1924 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001925 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001926 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001927}
1928#endif
1929
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001930static void mem_info(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001931{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001932 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001933
1934 env = mon_get_cpu();
1935
1936 if (!(env->cr[0] & CR0_PG_MASK)) {
1937 monitor_printf(mon, "PG disabled\n");
1938 return;
1939 }
1940 if (env->cr[4] & CR4_PAE_MASK) {
1941#ifdef TARGET_X86_64
1942 if (env->hflags & HF_LMA_MASK) {
1943 mem_info_64(mon, env);
1944 } else
1945#endif
1946 {
1947 mem_info_pae32(mon, env);
1948 }
1949 } else {
1950 mem_info_32(mon, env);
1951 }
1952}
bellardb86bda52004-09-18 19:32:46 +00001953#endif
1954
aurel327c664e22009-03-03 06:12:22 +00001955#if defined(TARGET_SH4)
1956
aliguori376253e2009-03-05 23:01:23 +00001957static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001958{
aliguori376253e2009-03-05 23:01:23 +00001959 monitor_printf(mon, " tlb%i:\t"
1960 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1961 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1962 "dirty=%hhu writethrough=%hhu\n",
1963 idx,
1964 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1965 tlb->v, tlb->sh, tlb->c, tlb->pr,
1966 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001967}
1968
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001969static void tlb_info(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001970{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001971 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00001972 int i;
1973
aliguori376253e2009-03-05 23:01:23 +00001974 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001975 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001976 print_tlb (mon, i, &env->itlb[i]);
1977 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001978 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001979 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001980}
1981
1982#endif
1983
Max Filippov692f7372012-01-07 20:02:40 +04001984#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001985static void tlb_info(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001986{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001987 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00001988
1989 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1990}
1991#endif
1992
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001993static void do_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001994{
1995 mtree_info((fprintf_function)monitor_printf, mon);
1996}
1997
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001998static void do_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001999{
aliguorib28b6232009-04-22 20:20:29 +00002000 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01002001 CPUState *cpu;
aliguori030ea372009-04-21 22:30:47 +00002002
2003 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2004 for (i = 0; i < nb_numa_nodes; i++) {
2005 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02002006 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01002007 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01002008 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00002009 }
2010 }
2011 monitor_printf(mon, "\n");
2012 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2013 node_mem[i] >> 20);
2014 }
2015}
2016
bellard5f1ce942006-02-08 22:40:15 +00002017#ifdef CONFIG_PROFILER
2018
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002019int64_t qemu_time;
2020int64_t dev_time;
2021
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002022static void do_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002023{
2024 int64_t total;
2025 total = qemu_time;
2026 if (total == 0)
2027 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002028 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002029 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002030 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002031 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002032 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002033 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002034}
2035#else
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002036static void do_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002037{
aliguori376253e2009-03-05 23:01:23 +00002038 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002039}
2040#endif
2041
bellardec36b692006-07-16 18:57:03 +00002042/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002043static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002044
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002045static void do_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002046{
2047 int i;
2048 CaptureState *s;
2049
2050 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002051 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002052 s->ops.info (s->opaque);
2053 }
2054}
2055
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002056static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002057{
2058 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002059 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002060 CaptureState *s;
2061
2062 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2063 if (i == n) {
2064 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002065 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002066 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002067 return;
2068 }
2069 }
2070}
2071
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002072static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002073{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002074 const char *path = qdict_get_str(qdict, "path");
2075 int has_freq = qdict_haskey(qdict, "freq");
2076 int freq = qdict_get_try_int(qdict, "freq", -1);
2077 int has_bits = qdict_haskey(qdict, "bits");
2078 int bits = qdict_get_try_int(qdict, "bits", -1);
2079 int has_channels = qdict_haskey(qdict, "nchannels");
2080 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002081 CaptureState *s;
2082
Anthony Liguori7267c092011-08-20 22:09:37 -05002083 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002084
2085 freq = has_freq ? freq : 44100;
2086 bits = has_bits ? bits : 16;
2087 nchannels = has_channels ? nchannels : 2;
2088
2089 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002090 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002091 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002092 return;
bellardec36b692006-07-16 18:57:03 +00002093 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002094 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002095}
bellardec36b692006-07-16 18:57:03 +00002096
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002097static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002098{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002099 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002100
aliguori76655d62009-03-06 20:27:37 +00002101 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002102 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002103 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002104 return acl;
2105}
aliguori76655d62009-03-06 20:27:37 +00002106
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002107static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002108{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002109 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002110 qemu_acl *acl = find_acl(mon, aclname);
2111 qemu_acl_entry *entry;
2112 int i = 0;
2113
2114 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002115 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002116 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002117 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002118 i++;
2119 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002120 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002121 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002122 }
2123}
2124
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002125static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002126{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002127 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002128 qemu_acl *acl = find_acl(mon, aclname);
2129
2130 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002131 qemu_acl_reset(acl);
2132 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002133 }
2134}
aliguori76655d62009-03-06 20:27:37 +00002135
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002136static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002137{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002138 const char *aclname = qdict_get_str(qdict, "aclname");
2139 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002140 qemu_acl *acl = find_acl(mon, aclname);
2141
2142 if (acl) {
2143 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002144 acl->defaultDeny = 0;
2145 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002146 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002147 acl->defaultDeny = 1;
2148 monitor_printf(mon, "acl: policy set to 'deny'\n");
2149 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002150 monitor_printf(mon, "acl: unknown policy '%s', "
2151 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002152 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002153 }
2154}
aliguori76655d62009-03-06 20:27:37 +00002155
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002156static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002157{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002158 const char *aclname = qdict_get_str(qdict, "aclname");
2159 const char *match = qdict_get_str(qdict, "match");
2160 const char *policy = qdict_get_str(qdict, "policy");
2161 int has_index = qdict_haskey(qdict, "index");
2162 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002163 qemu_acl *acl = find_acl(mon, aclname);
2164 int deny, ret;
2165
2166 if (acl) {
2167 if (strcmp(policy, "allow") == 0) {
2168 deny = 0;
2169 } else if (strcmp(policy, "deny") == 0) {
2170 deny = 1;
2171 } else {
2172 monitor_printf(mon, "acl: unknown policy '%s', "
2173 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002174 return;
2175 }
aliguori28a76be2009-03-06 20:27:40 +00002176 if (has_index)
2177 ret = qemu_acl_insert(acl, deny, match, index);
2178 else
2179 ret = qemu_acl_append(acl, deny, match);
2180 if (ret < 0)
2181 monitor_printf(mon, "acl: unable to add acl entry\n");
2182 else
2183 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002184 }
2185}
aliguori76655d62009-03-06 20:27:37 +00002186
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002187static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002188{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002189 const char *aclname = qdict_get_str(qdict, "aclname");
2190 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002191 qemu_acl *acl = find_acl(mon, aclname);
2192 int ret;
aliguori76655d62009-03-06 20:27:37 +00002193
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002194 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002195 ret = qemu_acl_remove(acl, match);
2196 if (ret < 0)
2197 monitor_printf(mon, "acl: no matching acl entry\n");
2198 else
2199 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002200 }
2201}
2202
Huang Ying79c4f6b2009-06-23 10:05:14 +08002203#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002204static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002205{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002206 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002207 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002208 int cpu_index = qdict_get_int(qdict, "cpu_index");
2209 int bank = qdict_get_int(qdict, "bank");
2210 uint64_t status = qdict_get_int(qdict, "status");
2211 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2212 uint64_t addr = qdict_get_int(qdict, "addr");
2213 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002214 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002215
Jan Kiszka747461c2011-03-02 08:56:10 +01002216 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2217 flags |= MCE_INJECT_BROADCAST;
2218 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002219 cs = qemu_get_cpu(cpu_index);
2220 if (cs != NULL) {
2221 cpu = X86_CPU(cs);
2222 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2223 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002224 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002225}
2226#endif
2227
Corey Bryant208c9d12012-06-22 14:36:09 -04002228void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002229{
Anthony Liguoric227f092009-10-01 16:12:16 -05002230 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002231 int fd;
2232
Corey Bryant208c9d12012-06-22 14:36:09 -04002233 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002234 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002235 error_set(errp, QERR_FD_NOT_SUPPLIED);
2236 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002237 }
2238
2239 if (qemu_isdigit(fdname[0])) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002240 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2241 "a name not starting with a digit");
2242 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002243 }
2244
Corey Bryant208c9d12012-06-22 14:36:09 -04002245 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002246 if (strcmp(monfd->name, fdname) != 0) {
2247 continue;
2248 }
2249
2250 close(monfd->fd);
2251 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002252 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002253 }
2254
Anthony Liguori7267c092011-08-20 22:09:37 -05002255 monfd = g_malloc0(sizeof(mon_fd_t));
2256 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002257 monfd->fd = fd;
2258
Corey Bryant208c9d12012-06-22 14:36:09 -04002259 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002260}
2261
Corey Bryant208c9d12012-06-22 14:36:09 -04002262void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002263{
Anthony Liguoric227f092009-10-01 16:12:16 -05002264 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002265
Corey Bryant208c9d12012-06-22 14:36:09 -04002266 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002267 if (strcmp(monfd->name, fdname) != 0) {
2268 continue;
2269 }
2270
Blue Swirl72cf2d42009-09-12 07:36:22 +00002271 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002272 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002273 g_free(monfd->name);
2274 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002275 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002276 }
2277
Corey Bryant208c9d12012-06-22 14:36:09 -04002278 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002279}
2280
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002281static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002282{
Luiz Capitulino13548692011-07-29 15:36:43 -03002283 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002284 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002285
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002286 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002287
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002288 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002289 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002290 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002291}
2292
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002293int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002294{
Anthony Liguoric227f092009-10-01 16:12:16 -05002295 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002296
Blue Swirl72cf2d42009-09-12 07:36:22 +00002297 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002298 int fd;
2299
2300 if (strcmp(monfd->name, fdname) != 0) {
2301 continue;
2302 }
2303
2304 fd = monfd->fd;
2305
2306 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002307 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002308 g_free(monfd->name);
2309 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002310
2311 return fd;
2312 }
2313
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002314 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002315 return -1;
2316}
2317
Corey Bryantba1c0482012-08-14 16:43:43 -04002318static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2319{
2320 MonFdsetFd *mon_fdset_fd;
2321 MonFdsetFd *mon_fdset_fd_next;
2322
2323 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002324 if ((mon_fdset_fd->removed ||
2325 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2326 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002327 close(mon_fdset_fd->fd);
2328 g_free(mon_fdset_fd->opaque);
2329 QLIST_REMOVE(mon_fdset_fd, next);
2330 g_free(mon_fdset_fd);
2331 }
2332 }
2333
Corey Bryantadb696f2012-08-14 16:43:47 -04002334 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002335 QLIST_REMOVE(mon_fdset, next);
2336 g_free(mon_fdset);
2337 }
2338}
2339
Corey Bryantefb87c12012-08-14 16:43:48 -04002340static void monitor_fdsets_cleanup(void)
2341{
2342 MonFdset *mon_fdset;
2343 MonFdset *mon_fdset_next;
2344
2345 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2346 monitor_fdset_cleanup(mon_fdset);
2347 }
2348}
2349
Corey Bryantba1c0482012-08-14 16:43:43 -04002350AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2351 const char *opaque, Error **errp)
2352{
2353 int fd;
2354 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002355 AddfdInfo *fdinfo;
2356
2357 fd = qemu_chr_fe_get_msgfd(mon->chr);
2358 if (fd == -1) {
2359 error_set(errp, QERR_FD_NOT_SUPPLIED);
2360 goto error;
2361 }
2362
Corey Bryante446f702012-10-18 15:19:32 -04002363 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2364 has_opaque, opaque, errp);
2365 if (fdinfo) {
2366 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002367 }
2368
Corey Bryantba1c0482012-08-14 16:43:43 -04002369error:
2370 if (fd != -1) {
2371 close(fd);
2372 }
2373 return NULL;
2374}
2375
2376void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2377{
2378 MonFdset *mon_fdset;
2379 MonFdsetFd *mon_fdset_fd;
2380 char fd_str[60];
2381
2382 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2383 if (mon_fdset->id != fdset_id) {
2384 continue;
2385 }
2386 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2387 if (has_fd) {
2388 if (mon_fdset_fd->fd != fd) {
2389 continue;
2390 }
2391 mon_fdset_fd->removed = true;
2392 break;
2393 } else {
2394 mon_fdset_fd->removed = true;
2395 }
2396 }
2397 if (has_fd && !mon_fdset_fd) {
2398 goto error;
2399 }
2400 monitor_fdset_cleanup(mon_fdset);
2401 return;
2402 }
2403
2404error:
2405 if (has_fd) {
2406 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2407 fdset_id, fd);
2408 } else {
2409 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2410 }
2411 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2412}
2413
2414FdsetInfoList *qmp_query_fdsets(Error **errp)
2415{
2416 MonFdset *mon_fdset;
2417 MonFdsetFd *mon_fdset_fd;
2418 FdsetInfoList *fdset_list = NULL;
2419
2420 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2421 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2422 FdsetFdInfoList *fdsetfd_list = NULL;
2423
2424 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2425 fdset_info->value->fdset_id = mon_fdset->id;
2426
2427 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2428 FdsetFdInfoList *fdsetfd_info;
2429
2430 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2431 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2432 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2433 if (mon_fdset_fd->opaque) {
2434 fdsetfd_info->value->has_opaque = true;
2435 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2436 } else {
2437 fdsetfd_info->value->has_opaque = false;
2438 }
2439
2440 fdsetfd_info->next = fdsetfd_list;
2441 fdsetfd_list = fdsetfd_info;
2442 }
2443
2444 fdset_info->value->fds = fdsetfd_list;
2445
2446 fdset_info->next = fdset_list;
2447 fdset_list = fdset_info;
2448 }
2449
2450 return fdset_list;
2451}
2452
Corey Bryante446f702012-10-18 15:19:32 -04002453AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2454 bool has_opaque, const char *opaque,
2455 Error **errp)
2456{
2457 MonFdset *mon_fdset = NULL;
2458 MonFdsetFd *mon_fdset_fd;
2459 AddfdInfo *fdinfo;
2460
2461 if (has_fdset_id) {
2462 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2463 /* Break if match found or match impossible due to ordering by ID */
2464 if (fdset_id <= mon_fdset->id) {
2465 if (fdset_id < mon_fdset->id) {
2466 mon_fdset = NULL;
2467 }
2468 break;
2469 }
2470 }
2471 }
2472
2473 if (mon_fdset == NULL) {
2474 int64_t fdset_id_prev = -1;
2475 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2476
2477 if (has_fdset_id) {
2478 if (fdset_id < 0) {
2479 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2480 "a non-negative value");
2481 return NULL;
2482 }
2483 /* Use specified fdset ID */
2484 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2485 mon_fdset_cur = mon_fdset;
2486 if (fdset_id < mon_fdset_cur->id) {
2487 break;
2488 }
2489 }
2490 } else {
2491 /* Use first available fdset ID */
2492 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2493 mon_fdset_cur = mon_fdset;
2494 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2495 fdset_id_prev = mon_fdset_cur->id;
2496 continue;
2497 }
2498 break;
2499 }
2500 }
2501
2502 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2503 if (has_fdset_id) {
2504 mon_fdset->id = fdset_id;
2505 } else {
2506 mon_fdset->id = fdset_id_prev + 1;
2507 }
2508
2509 /* The fdset list is ordered by fdset ID */
2510 if (!mon_fdset_cur) {
2511 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2512 } else if (mon_fdset->id < mon_fdset_cur->id) {
2513 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2514 } else {
2515 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2516 }
2517 }
2518
2519 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2520 mon_fdset_fd->fd = fd;
2521 mon_fdset_fd->removed = false;
2522 if (has_opaque) {
2523 mon_fdset_fd->opaque = g_strdup(opaque);
2524 }
2525 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2526
2527 fdinfo = g_malloc0(sizeof(*fdinfo));
2528 fdinfo->fdset_id = mon_fdset->id;
2529 fdinfo->fd = mon_fdset_fd->fd;
2530
2531 return fdinfo;
2532}
2533
Corey Bryantadb696f2012-08-14 16:43:47 -04002534int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2535{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002536#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002537 MonFdset *mon_fdset;
2538 MonFdsetFd *mon_fdset_fd;
2539 int mon_fd_flags;
2540
Corey Bryantadb696f2012-08-14 16:43:47 -04002541 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2542 if (mon_fdset->id != fdset_id) {
2543 continue;
2544 }
2545 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2546 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2547 if (mon_fd_flags == -1) {
2548 return -1;
2549 }
2550
2551 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2552 return mon_fdset_fd->fd;
2553 }
2554 }
2555 errno = EACCES;
2556 return -1;
2557 }
2558#endif
2559
2560 errno = ENOENT;
2561 return -1;
2562}
2563
2564int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2565{
2566 MonFdset *mon_fdset;
2567 MonFdsetFd *mon_fdset_fd_dup;
2568
2569 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2570 if (mon_fdset->id != fdset_id) {
2571 continue;
2572 }
2573 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2574 if (mon_fdset_fd_dup->fd == dup_fd) {
2575 return -1;
2576 }
2577 }
2578 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2579 mon_fdset_fd_dup->fd = dup_fd;
2580 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2581 return 0;
2582 }
2583 return -1;
2584}
2585
2586static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2587{
2588 MonFdset *mon_fdset;
2589 MonFdsetFd *mon_fdset_fd_dup;
2590
2591 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2592 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2593 if (mon_fdset_fd_dup->fd == dup_fd) {
2594 if (remove) {
2595 QLIST_REMOVE(mon_fdset_fd_dup, next);
2596 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2597 monitor_fdset_cleanup(mon_fdset);
2598 }
2599 }
2600 return mon_fdset->id;
2601 }
2602 }
2603 }
2604 return -1;
2605}
2606
2607int monitor_fdset_dup_fd_find(int dup_fd)
2608{
2609 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2610}
2611
2612int monitor_fdset_dup_fd_remove(int dup_fd)
2613{
2614 return monitor_fdset_dup_fd_find_remove(dup_fd, true);
2615}
2616
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002617int monitor_handle_fd_param(Monitor *mon, const char *fdname)
2618{
2619 int fd;
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002620 Error *local_err = NULL;
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002621
2622 if (!qemu_isdigit(fdname[0]) && mon) {
2623
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002624 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002625 if (fd == -1) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002626 qerror_report_err(local_err);
2627 error_free(local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002628 return -1;
2629 }
2630 } else {
2631 fd = qemu_parse_fd(fdname);
2632 }
2633
2634 return fd;
2635}
2636
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002637/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002638static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002639 {
2640 .name = "version",
2641 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002642 .params = "",
2643 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002644 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002645 },
2646 {
2647 .name = "network",
2648 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002649 .params = "",
2650 .help = "show the network state",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002651 .mhandler.cmd = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002652 },
2653 {
2654 .name = "chardev",
2655 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002656 .params = "",
2657 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002658 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002659 },
2660 {
2661 .name = "block",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002662 .args_type = "verbose:-v,device:B?",
2663 .params = "[-v] [device]",
2664 .help = "show info of one block device or all block devices "
2665 "(and details of images with -v option)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002666 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002667 },
2668 {
2669 .name = "blockstats",
2670 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 .params = "",
2672 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002673 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002674 },
2675 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002676 .name = "block-jobs",
2677 .args_type = "",
2678 .params = "",
2679 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002680 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002681 },
2682 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002683 .name = "registers",
2684 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002685 .params = "",
2686 .help = "show the cpu registers",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002687 .mhandler.cmd = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002688 },
2689 {
2690 .name = "cpus",
2691 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002692 .params = "",
2693 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002694 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002695 },
2696 {
2697 .name = "history",
2698 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002699 .params = "",
2700 .help = "show the command line history",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002701 .mhandler.cmd = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002702 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002703#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2704 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002705 {
2706 .name = "irq",
2707 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002708 .params = "",
2709 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002710#ifdef TARGET_SPARC
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002711 .mhandler.cmd = sun4m_irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002712#elif defined(TARGET_LM32)
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002713 .mhandler.cmd = lm32_irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002714#else
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002715 .mhandler.cmd = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002716#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002717 },
2718 {
2719 .name = "pic",
2720 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002721 .params = "",
2722 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002723#ifdef TARGET_SPARC
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002724 .mhandler.cmd = sun4m_pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002725#elif defined(TARGET_LM32)
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002726 .mhandler.cmd = lm32_do_pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002727#else
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002728 .mhandler.cmd = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002729#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002730 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002731#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002732 {
2733 .name = "pci",
2734 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002735 .params = "",
2736 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002737 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002738 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002739#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002740 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002741 {
2742 .name = "tlb",
2743 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002744 .params = "",
2745 .help = "show virtual to physical memory mappings",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002746 .mhandler.cmd = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002747 },
aurel327c664e22009-03-03 06:12:22 +00002748#endif
2749#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002750 {
2751 .name = "mem",
2752 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002753 .params = "",
2754 .help = "show the active virtual memory mappings",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002755 .mhandler.cmd = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002756 },
bellardb86bda52004-09-18 19:32:46 +00002757#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002758 {
Blue Swirl314e2982011-09-11 20:22:05 +00002759 .name = "mtree",
2760 .args_type = "",
2761 .params = "",
2762 .help = "show memory tree",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002763 .mhandler.cmd = do_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002764 },
2765 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002766 .name = "jit",
2767 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002768 .params = "",
2769 .help = "show dynamic compiler info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002770 .mhandler.cmd = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002771 },
2772 {
2773 .name = "kvm",
2774 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002775 .params = "",
2776 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002777 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002778 },
2779 {
2780 .name = "numa",
2781 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002782 .params = "",
2783 .help = "show NUMA information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002784 .mhandler.cmd = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002785 },
2786 {
2787 .name = "usb",
2788 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002789 .params = "",
2790 .help = "show guest USB devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002791 .mhandler.cmd = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002792 },
2793 {
2794 .name = "usbhost",
2795 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002796 .params = "",
2797 .help = "show host USB devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002798 .mhandler.cmd = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002799 },
2800 {
2801 .name = "profile",
2802 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002803 .params = "",
2804 .help = "show profiling information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002805 .mhandler.cmd = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002806 },
2807 {
2808 .name = "capture",
2809 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002810 .params = "",
2811 .help = "show capture information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002812 .mhandler.cmd = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002813 },
2814 {
2815 .name = "snapshots",
2816 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002817 .params = "",
2818 .help = "show the currently saved VM snapshots",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002819 .mhandler.cmd = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002820 },
2821 {
2822 .name = "status",
2823 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002824 .params = "",
2825 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002826 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002827 },
2828 {
2829 .name = "pcmcia",
2830 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002831 .params = "",
2832 .help = "show guest PCMCIA status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002833 .mhandler.cmd = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002834 },
2835 {
2836 .name = "mice",
2837 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002838 .params = "",
2839 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002840 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002841 },
2842 {
2843 .name = "vnc",
2844 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002845 .params = "",
2846 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002847 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002848 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002849#if defined(CONFIG_SPICE)
2850 {
2851 .name = "spice",
2852 .args_type = "",
2853 .params = "",
2854 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002855 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002856 },
2857#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002858 {
2859 .name = "name",
2860 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002861 .params = "",
2862 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002863 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002864 },
2865 {
2866 .name = "uuid",
2867 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002868 .params = "",
2869 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002870 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002871 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002872 {
2873 .name = "cpustats",
2874 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002875 .params = "",
2876 .help = "show CPU statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002877 .mhandler.cmd = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002878 },
blueswir131a60e22007-10-26 18:42:59 +00002879#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002880 {
2881 .name = "usernet",
2882 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002883 .params = "",
2884 .help = "show user network stack connection states",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002885 .mhandler.cmd = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002886 },
blueswir131a60e22007-10-26 18:42:59 +00002887#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002888 {
2889 .name = "migrate",
2890 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002891 .params = "",
2892 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002893 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002894 },
2895 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002896 .name = "migrate_capabilities",
2897 .args_type = "",
2898 .params = "",
2899 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002900 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002901 },
2902 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002903 .name = "migrate_cache_size",
2904 .args_type = "",
2905 .params = "",
2906 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002907 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002908 },
2909 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002910 .name = "balloon",
2911 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002912 .params = "",
2913 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002914 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002915 },
2916 {
2917 .name = "qtree",
2918 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002919 .params = "",
2920 .help = "show device tree",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002921 .mhandler.cmd = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002922 },
2923 {
2924 .name = "qdm",
2925 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002926 .params = "",
2927 .help = "show qdev device model list",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002928 .mhandler.cmd = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002929 },
2930 {
2931 .name = "roms",
2932 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002933 .params = "",
2934 .help = "show roms",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002935 .mhandler.cmd = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002936 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302937 {
2938 .name = "trace-events",
2939 .args_type = "",
2940 .params = "",
2941 .help = "show available trace-events & their state",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002942 .mhandler.cmd = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302943 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002944 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002945 .name = "tpm",
2946 .args_type = "",
2947 .params = "",
2948 .help = "show the TPM device",
2949 .mhandler.cmd = hmp_info_tpm,
2950 },
2951 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002952 .name = NULL,
2953 },
bellard9dc39cb2004-03-14 21:38:27 +00002954};
2955
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002956/* mon_cmds and info_cmds would be sorted at runtime */
2957static mon_cmd_t mon_cmds[] = {
2958#include "hmp-commands.h"
2959 { NULL, NULL, },
2960};
2961
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002962static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002963#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002964 { /* NULL */ },
2965};
2966
bellard9307c4c2004-04-04 12:57:25 +00002967/*******************************************************************/
2968
2969static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002970static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002971
bellard92a31b12005-02-10 22:00:52 +00002972#define MD_TLONG 0
2973#define MD_I32 1
2974
bellard9307c4c2004-04-04 12:57:25 +00002975typedef struct MonitorDef {
2976 const char *name;
2977 int offset;
blueswir18662d652008-10-02 18:32:44 +00002978 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002979 int type;
bellard9307c4c2004-04-04 12:57:25 +00002980} MonitorDef;
2981
bellard57206fd2004-04-25 18:54:52 +00002982#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002983static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002984{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002985 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002986 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002987}
2988#endif
2989
bellarda541f292004-04-12 20:39:29 +00002990#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002991static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002992{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002993 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002994 unsigned int u;
2995 int i;
2996
2997 u = 0;
2998 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002999 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003000
3001 return u;
3002}
3003
blueswir18662d652008-10-02 18:32:44 +00003004static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003005{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003006 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003007 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003008}
3009
blueswir18662d652008-10-02 18:32:44 +00003010static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003011{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003012 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003013 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003014}
bellard9fddaa02004-05-21 12:59:32 +00003015
blueswir18662d652008-10-02 18:32:44 +00003016static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003017{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003018 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003019 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003020}
3021
blueswir18662d652008-10-02 18:32:44 +00003022static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003023{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003024 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003025 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003026}
3027
blueswir18662d652008-10-02 18:32:44 +00003028static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003029{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003030 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003031 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003032}
bellarda541f292004-04-12 20:39:29 +00003033#endif
3034
bellarde95c8d52004-09-30 22:22:08 +00003035#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003036#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003037static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003038{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003039 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003040
3041 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003042}
bellard7b936c02005-10-30 17:05:13 +00003043#endif
bellarde95c8d52004-09-30 22:22:08 +00003044
blueswir18662d652008-10-02 18:32:44 +00003045static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003046{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003047 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003048 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003049}
3050#endif
3051
blueswir18662d652008-10-02 18:32:44 +00003052static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003053#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003054
3055#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01003056 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
3057 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
3058 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003059
Andreas Färbere59d1672012-02-16 00:40:47 +01003060 { "eax", offsetof(CPUX86State, regs[0]) },
3061 { "ecx", offsetof(CPUX86State, regs[1]) },
3062 { "edx", offsetof(CPUX86State, regs[2]) },
3063 { "ebx", offsetof(CPUX86State, regs[3]) },
3064 { "esp|sp", offsetof(CPUX86State, regs[4]) },
3065 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
3066 { "esi", offsetof(CPUX86State, regs[6]) },
3067 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003068#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01003069 { "r8", offsetof(CPUX86State, regs[8]) },
3070 { "r9", offsetof(CPUX86State, regs[9]) },
3071 { "r10", offsetof(CPUX86State, regs[10]) },
3072 { "r11", offsetof(CPUX86State, regs[11]) },
3073 { "r12", offsetof(CPUX86State, regs[12]) },
3074 { "r13", offsetof(CPUX86State, regs[13]) },
3075 { "r14", offsetof(CPUX86State, regs[14]) },
3076 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00003077#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003078 { "eflags", offsetof(CPUX86State, eflags) },
3079 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00003080 SEG("cs", R_CS)
3081 SEG("ds", R_DS)
3082 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003083 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003084 SEG("fs", R_FS)
3085 SEG("gs", R_GS)
3086 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003087#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003088 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003089 { "r0", offsetof(CPUPPCState, gpr[0]) },
3090 { "r1", offsetof(CPUPPCState, gpr[1]) },
3091 { "r2", offsetof(CPUPPCState, gpr[2]) },
3092 { "r3", offsetof(CPUPPCState, gpr[3]) },
3093 { "r4", offsetof(CPUPPCState, gpr[4]) },
3094 { "r5", offsetof(CPUPPCState, gpr[5]) },
3095 { "r6", offsetof(CPUPPCState, gpr[6]) },
3096 { "r7", offsetof(CPUPPCState, gpr[7]) },
3097 { "r8", offsetof(CPUPPCState, gpr[8]) },
3098 { "r9", offsetof(CPUPPCState, gpr[9]) },
3099 { "r10", offsetof(CPUPPCState, gpr[10]) },
3100 { "r11", offsetof(CPUPPCState, gpr[11]) },
3101 { "r12", offsetof(CPUPPCState, gpr[12]) },
3102 { "r13", offsetof(CPUPPCState, gpr[13]) },
3103 { "r14", offsetof(CPUPPCState, gpr[14]) },
3104 { "r15", offsetof(CPUPPCState, gpr[15]) },
3105 { "r16", offsetof(CPUPPCState, gpr[16]) },
3106 { "r17", offsetof(CPUPPCState, gpr[17]) },
3107 { "r18", offsetof(CPUPPCState, gpr[18]) },
3108 { "r19", offsetof(CPUPPCState, gpr[19]) },
3109 { "r20", offsetof(CPUPPCState, gpr[20]) },
3110 { "r21", offsetof(CPUPPCState, gpr[21]) },
3111 { "r22", offsetof(CPUPPCState, gpr[22]) },
3112 { "r23", offsetof(CPUPPCState, gpr[23]) },
3113 { "r24", offsetof(CPUPPCState, gpr[24]) },
3114 { "r25", offsetof(CPUPPCState, gpr[25]) },
3115 { "r26", offsetof(CPUPPCState, gpr[26]) },
3116 { "r27", offsetof(CPUPPCState, gpr[27]) },
3117 { "r28", offsetof(CPUPPCState, gpr[28]) },
3118 { "r29", offsetof(CPUPPCState, gpr[29]) },
3119 { "r30", offsetof(CPUPPCState, gpr[30]) },
3120 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003121 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003122 { "f0", offsetof(CPUPPCState, fpr[0]) },
3123 { "f1", offsetof(CPUPPCState, fpr[1]) },
3124 { "f2", offsetof(CPUPPCState, fpr[2]) },
3125 { "f3", offsetof(CPUPPCState, fpr[3]) },
3126 { "f4", offsetof(CPUPPCState, fpr[4]) },
3127 { "f5", offsetof(CPUPPCState, fpr[5]) },
3128 { "f6", offsetof(CPUPPCState, fpr[6]) },
3129 { "f7", offsetof(CPUPPCState, fpr[7]) },
3130 { "f8", offsetof(CPUPPCState, fpr[8]) },
3131 { "f9", offsetof(CPUPPCState, fpr[9]) },
3132 { "f10", offsetof(CPUPPCState, fpr[10]) },
3133 { "f11", offsetof(CPUPPCState, fpr[11]) },
3134 { "f12", offsetof(CPUPPCState, fpr[12]) },
3135 { "f13", offsetof(CPUPPCState, fpr[13]) },
3136 { "f14", offsetof(CPUPPCState, fpr[14]) },
3137 { "f15", offsetof(CPUPPCState, fpr[15]) },
3138 { "f16", offsetof(CPUPPCState, fpr[16]) },
3139 { "f17", offsetof(CPUPPCState, fpr[17]) },
3140 { "f18", offsetof(CPUPPCState, fpr[18]) },
3141 { "f19", offsetof(CPUPPCState, fpr[19]) },
3142 { "f20", offsetof(CPUPPCState, fpr[20]) },
3143 { "f21", offsetof(CPUPPCState, fpr[21]) },
3144 { "f22", offsetof(CPUPPCState, fpr[22]) },
3145 { "f23", offsetof(CPUPPCState, fpr[23]) },
3146 { "f24", offsetof(CPUPPCState, fpr[24]) },
3147 { "f25", offsetof(CPUPPCState, fpr[25]) },
3148 { "f26", offsetof(CPUPPCState, fpr[26]) },
3149 { "f27", offsetof(CPUPPCState, fpr[27]) },
3150 { "f28", offsetof(CPUPPCState, fpr[28]) },
3151 { "f29", offsetof(CPUPPCState, fpr[29]) },
3152 { "f30", offsetof(CPUPPCState, fpr[30]) },
3153 { "f31", offsetof(CPUPPCState, fpr[31]) },
3154 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003155 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003156 { "nip|pc", offsetof(CPUPPCState, nip) },
3157 { "lr", offsetof(CPUPPCState, lr) },
3158 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003159 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003160 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003161 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003162 { "msr", 0, &monitor_get_msr, },
3163 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003164 { "tbu", 0, &monitor_get_tbu, },
3165 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003166 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003167 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3168 { "sr0", offsetof(CPUPPCState, sr[0]) },
3169 { "sr1", offsetof(CPUPPCState, sr[1]) },
3170 { "sr2", offsetof(CPUPPCState, sr[2]) },
3171 { "sr3", offsetof(CPUPPCState, sr[3]) },
3172 { "sr4", offsetof(CPUPPCState, sr[4]) },
3173 { "sr5", offsetof(CPUPPCState, sr[5]) },
3174 { "sr6", offsetof(CPUPPCState, sr[6]) },
3175 { "sr7", offsetof(CPUPPCState, sr[7]) },
3176 { "sr8", offsetof(CPUPPCState, sr[8]) },
3177 { "sr9", offsetof(CPUPPCState, sr[9]) },
3178 { "sr10", offsetof(CPUPPCState, sr[10]) },
3179 { "sr11", offsetof(CPUPPCState, sr[11]) },
3180 { "sr12", offsetof(CPUPPCState, sr[12]) },
3181 { "sr13", offsetof(CPUPPCState, sr[13]) },
3182 { "sr14", offsetof(CPUPPCState, sr[14]) },
3183 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003184 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003185 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003186
Andreas Färbere59d1672012-02-16 00:40:47 +01003187 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3188 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
3189 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3190 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3191 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3192 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3193 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3194 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3195 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3196 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3197 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3198 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3199 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3200 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3201 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3202 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3203 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3204 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3205 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3206 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3207 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3208 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3209 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3210 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3211 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3212 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3213 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3214 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3215 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3216 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3217 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3218 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3219 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3220 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3221 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3222 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3223 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3224 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3225 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3226 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3227 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3228 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3229 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3230 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3231 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3232 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3233 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3234 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3235 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3236 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3237 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3238 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3239 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3240 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3241 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3242 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3243 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3244 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3245 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3246 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3247 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3248 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3249 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3250 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3251 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3252 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003253
bellarde95c8d52004-09-30 22:22:08 +00003254#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003255 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3256 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3257 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3258 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3259 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3260 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3261 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3262 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003263 { "o0", 0, monitor_get_reg },
3264 { "o1", 1, monitor_get_reg },
3265 { "o2", 2, monitor_get_reg },
3266 { "o3", 3, monitor_get_reg },
3267 { "o4", 4, monitor_get_reg },
3268 { "o5", 5, monitor_get_reg },
3269 { "o6", 6, monitor_get_reg },
3270 { "o7", 7, monitor_get_reg },
3271 { "l0", 8, monitor_get_reg },
3272 { "l1", 9, monitor_get_reg },
3273 { "l2", 10, monitor_get_reg },
3274 { "l3", 11, monitor_get_reg },
3275 { "l4", 12, monitor_get_reg },
3276 { "l5", 13, monitor_get_reg },
3277 { "l6", 14, monitor_get_reg },
3278 { "l7", 15, monitor_get_reg },
3279 { "i0", 16, monitor_get_reg },
3280 { "i1", 17, monitor_get_reg },
3281 { "i2", 18, monitor_get_reg },
3282 { "i3", 19, monitor_get_reg },
3283 { "i4", 20, monitor_get_reg },
3284 { "i5", 21, monitor_get_reg },
3285 { "i6", 22, monitor_get_reg },
3286 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003287 { "pc", offsetof(CPUSPARCState, pc) },
3288 { "npc", offsetof(CPUSPARCState, npc) },
3289 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003290#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003291 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003292 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003293#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003294 { "tbr", offsetof(CPUSPARCState, tbr) },
3295 { "fsr", offsetof(CPUSPARCState, fsr) },
3296 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3297 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3298 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3299 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3300 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3301 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3302 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3303 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3304 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3305 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3306 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3307 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3308 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3309 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3310 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3311 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3312 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3313 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3314 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3315 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3316 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3317 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3318 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3319 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3320 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3321 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3322 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3323 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3324 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3325 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3326 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3327 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003328#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003329 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3330 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3331 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3332 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3333 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3334 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3335 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3336 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3337 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3338 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3339 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3340 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3341 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3342 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3343 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3344 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3345 { "asi", offsetof(CPUSPARCState, asi) },
3346 { "pstate", offsetof(CPUSPARCState, pstate) },
3347 { "cansave", offsetof(CPUSPARCState, cansave) },
3348 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3349 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3350 { "wstate", offsetof(CPUSPARCState, wstate) },
3351 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3352 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003353#endif
bellard9307c4c2004-04-04 12:57:25 +00003354#endif
3355 { NULL },
3356};
3357
Stefan Weil9c3175c2013-08-22 21:30:09 +02003358static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3359expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003360{
Fam Zheng277acfe2013-08-20 10:58:21 +08003361 va_list ap;
3362 va_start(ap, fmt);
3363 monitor_vprintf(mon, fmt, ap);
3364 monitor_printf(mon, "\n");
3365 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003366 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003367}
3368
Markus Armbruster09b94182010-01-20 13:07:30 +01003369/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003370static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003371{
blueswir18662d652008-10-02 18:32:44 +00003372 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003373 void *ptr;
3374
bellard9307c4c2004-04-04 12:57:25 +00003375 for(md = monitor_defs; md->name != NULL; md++) {
3376 if (compare_cmd(name, md->name)) {
3377 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003378 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003379 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003380 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003381 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003382 switch(md->type) {
3383 case MD_I32:
3384 *pval = *(int32_t *)ptr;
3385 break;
3386 case MD_TLONG:
3387 *pval = *(target_long *)ptr;
3388 break;
3389 default:
3390 *pval = 0;
3391 break;
3392 }
bellard9307c4c2004-04-04 12:57:25 +00003393 }
3394 return 0;
3395 }
3396 }
3397 return -1;
3398}
3399
3400static void next(void)
3401{
Blue Swirl660f11b2009-07-31 21:16:51 +00003402 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003403 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003404 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003405 pch++;
3406 }
3407}
3408
aliguori376253e2009-03-05 23:01:23 +00003409static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003410
aliguori376253e2009-03-05 23:01:23 +00003411static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003412{
blueswir1c2efc952007-09-25 17:28:42 +00003413 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003414 char *p;
bellard6a00d602005-11-21 23:25:50 +00003415 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003416
3417 switch(*pch) {
3418 case '+':
3419 next();
aliguori376253e2009-03-05 23:01:23 +00003420 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003421 break;
3422 case '-':
3423 next();
aliguori376253e2009-03-05 23:01:23 +00003424 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003425 break;
3426 case '~':
3427 next();
aliguori376253e2009-03-05 23:01:23 +00003428 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003429 break;
3430 case '(':
3431 next();
aliguori376253e2009-03-05 23:01:23 +00003432 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003433 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003434 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003435 }
3436 next();
3437 break;
bellard81d09122004-07-14 17:21:37 +00003438 case '\'':
3439 pch++;
3440 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003441 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003442 n = *pch;
3443 pch++;
3444 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003445 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003446 next();
3447 break;
bellard9307c4c2004-04-04 12:57:25 +00003448 case '$':
3449 {
3450 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003451 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003452
bellard9307c4c2004-04-04 12:57:25 +00003453 pch++;
3454 q = buf;
3455 while ((*pch >= 'a' && *pch <= 'z') ||
3456 (*pch >= 'A' && *pch <= 'Z') ||
3457 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003458 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003459 if ((q - buf) < sizeof(buf) - 1)
3460 *q++ = *pch;
3461 pch++;
3462 }
blueswir1cd390082008-11-16 13:53:32 +00003463 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003464 pch++;
3465 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003466 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003467 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003468 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003469 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003470 }
3471 break;
3472 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003473 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003474 n = 0;
3475 break;
3476 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003477 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003478 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003479 if (errno == ERANGE) {
3480 expr_error(mon, "number too large");
3481 }
bellard9307c4c2004-04-04 12:57:25 +00003482 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003483 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003484 }
3485 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003486 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003487 pch++;
3488 break;
3489 }
3490 return n;
3491}
3492
3493
aliguori376253e2009-03-05 23:01:23 +00003494static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003495{
blueswir1c2efc952007-09-25 17:28:42 +00003496 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003497 int op;
ths3b46e622007-09-17 08:09:54 +00003498
aliguori376253e2009-03-05 23:01:23 +00003499 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003500 for(;;) {
3501 op = *pch;
3502 if (op != '*' && op != '/' && op != '%')
3503 break;
3504 next();
aliguori376253e2009-03-05 23:01:23 +00003505 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003506 switch(op) {
3507 default:
3508 case '*':
3509 val *= val2;
3510 break;
3511 case '/':
3512 case '%':
ths5fafdf22007-09-16 21:08:06 +00003513 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003514 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003515 if (op == '/')
3516 val /= val2;
3517 else
3518 val %= val2;
3519 break;
3520 }
3521 }
3522 return val;
3523}
3524
aliguori376253e2009-03-05 23:01:23 +00003525static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003526{
blueswir1c2efc952007-09-25 17:28:42 +00003527 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003528 int op;
bellard9307c4c2004-04-04 12:57:25 +00003529
aliguori376253e2009-03-05 23:01:23 +00003530 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003531 for(;;) {
3532 op = *pch;
3533 if (op != '&' && op != '|' && op != '^')
3534 break;
3535 next();
aliguori376253e2009-03-05 23:01:23 +00003536 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003537 switch(op) {
3538 default:
3539 case '&':
3540 val &= val2;
3541 break;
3542 case '|':
3543 val |= val2;
3544 break;
3545 case '^':
3546 val ^= val2;
3547 break;
3548 }
3549 }
3550 return val;
3551}
3552
aliguori376253e2009-03-05 23:01:23 +00003553static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003554{
blueswir1c2efc952007-09-25 17:28:42 +00003555 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003556 int op;
bellard9307c4c2004-04-04 12:57:25 +00003557
aliguori376253e2009-03-05 23:01:23 +00003558 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003559 for(;;) {
3560 op = *pch;
3561 if (op != '+' && op != '-')
3562 break;
3563 next();
aliguori376253e2009-03-05 23:01:23 +00003564 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003565 if (op == '+')
3566 val += val2;
3567 else
3568 val -= val2;
3569 }
3570 return val;
3571}
3572
aliguori376253e2009-03-05 23:01:23 +00003573static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003574{
3575 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003576 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003577 *pp = pch;
3578 return -1;
3579 }
blueswir1cd390082008-11-16 13:53:32 +00003580 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003581 pch++;
aliguori376253e2009-03-05 23:01:23 +00003582 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003583 *pp = pch;
3584 return 0;
3585}
3586
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003587static int get_double(Monitor *mon, double *pval, const char **pp)
3588{
3589 const char *p = *pp;
3590 char *tailp;
3591 double d;
3592
3593 d = strtod(p, &tailp);
3594 if (tailp == p) {
3595 monitor_printf(mon, "Number expected\n");
3596 return -1;
3597 }
3598 if (d != d || d - d != 0) {
3599 /* NaN or infinity */
3600 monitor_printf(mon, "Bad number\n");
3601 return -1;
3602 }
3603 *pval = d;
3604 *pp = tailp;
3605 return 0;
3606}
3607
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003608/*
3609 * Store the command-name in cmdname, and return a pointer to
3610 * the remaining of the command string.
3611 */
3612static const char *get_command_name(const char *cmdline,
3613 char *cmdname, size_t nlen)
3614{
3615 size_t len;
3616 const char *p, *pstart;
3617
3618 p = cmdline;
3619 while (qemu_isspace(*p))
3620 p++;
3621 if (*p == '\0')
3622 return NULL;
3623 pstart = p;
3624 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3625 p++;
3626 len = p - pstart;
3627 if (len > nlen - 1)
3628 len = nlen - 1;
3629 memcpy(cmdname, pstart, len);
3630 cmdname[len] = '\0';
3631 return p;
3632}
3633
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003634/**
3635 * Read key of 'type' into 'key' and return the current
3636 * 'type' pointer.
3637 */
3638static char *key_get_info(const char *type, char **key)
3639{
3640 size_t len;
3641 char *p, *str;
3642
3643 if (*type == ',')
3644 type++;
3645
3646 p = strchr(type, ':');
3647 if (!p) {
3648 *key = NULL;
3649 return NULL;
3650 }
3651 len = p - type;
3652
Anthony Liguori7267c092011-08-20 22:09:37 -05003653 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003654 memcpy(str, type, len);
3655 str[len] = '\0';
3656
3657 *key = str;
3658 return ++p;
3659}
3660
bellard9307c4c2004-04-04 12:57:25 +00003661static int default_fmt_format = 'x';
3662static int default_fmt_size = 4;
3663
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003664static int is_valid_option(const char *c, const char *typestr)
3665{
3666 char option[3];
3667
3668 option[0] = '-';
3669 option[1] = *c;
3670 option[2] = '\0';
3671
3672 typestr = strstr(typestr, option);
3673 return (typestr != NULL);
3674}
3675
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003676static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3677 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003678{
3679 const mon_cmd_t *cmd;
3680
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003681 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003682 if (compare_cmd(cmdname, cmd->name)) {
3683 return cmd;
3684 }
3685 }
3686
3687 return NULL;
3688}
3689
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003690static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3691{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003692 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003693}
3694
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003695/*
3696 * Parse @cmdline according to command table @table.
3697 * If @cmdline is blank, return NULL.
3698 * If it can't be parsed, report to @mon, and return NULL.
3699 * Else, insert command arguments into @qdict, and return the command.
Peter Maydell085d8132013-03-18 17:20:07 +00003700 * If a sub-command table exists, and if @cmdline contains an additional string
3701 * for a sub-command, this function will try to search the sub-command table.
3702 * If no additional string for a sub-command is present, this function will
3703 * return the command found in @table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003704 * Do not assume the returned command points into @table! It doesn't
3705 * when the command is a sub-command.
3706 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003707static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003708 const char *cmdline,
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003709 int start,
3710 mon_cmd_t *table,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003711 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003712{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003713 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003714 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003715 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003716 char cmdname[256];
3717 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003718 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003719
3720#ifdef DEBUG
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003721 monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start);
bellard9dc39cb2004-03-14 21:38:27 +00003722#endif
ths3b46e622007-09-17 08:09:54 +00003723
bellard9307c4c2004-04-04 12:57:25 +00003724 /* extract the command name */
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003725 p = get_command_name(cmdline + start, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003726 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003727 return NULL;
ths3b46e622007-09-17 08:09:54 +00003728
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003729 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003730 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003731 monitor_printf(mon, "unknown command: '%.*s'\n",
3732 (int)(p - cmdline), cmdline);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003733 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003734 }
bellard9307c4c2004-04-04 12:57:25 +00003735
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003736 /* filter out following useless space */
3737 while (qemu_isspace(*p)) {
3738 p++;
3739 }
3740 /* search sub command */
3741 if (cmd->sub_table != NULL) {
3742 /* check if user set additional command */
3743 if (*p == '\0') {
3744 return cmd;
3745 }
3746 return monitor_parse_command(mon, cmdline, p - cmdline,
3747 cmd->sub_table, qdict);
3748 }
3749
bellard9307c4c2004-04-04 12:57:25 +00003750 /* parse the parameters */
3751 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003752 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003753 typestr = key_get_info(typestr, &key);
3754 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003755 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003756 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003757 typestr++;
3758 switch(c) {
3759 case 'F':
bellard81d09122004-07-14 17:21:37 +00003760 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003761 case 's':
3762 {
3763 int ret;
ths3b46e622007-09-17 08:09:54 +00003764
blueswir1cd390082008-11-16 13:53:32 +00003765 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003766 p++;
3767 if (*typestr == '?') {
3768 typestr++;
3769 if (*p == '\0') {
3770 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003771 break;
bellard9307c4c2004-04-04 12:57:25 +00003772 }
3773 }
3774 ret = get_str(buf, sizeof(buf), &p);
3775 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003776 switch(c) {
3777 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003778 monitor_printf(mon, "%s: filename expected\n",
3779 cmdname);
bellard81d09122004-07-14 17:21:37 +00003780 break;
3781 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003782 monitor_printf(mon, "%s: block device name expected\n",
3783 cmdname);
bellard81d09122004-07-14 17:21:37 +00003784 break;
3785 default:
aliguori376253e2009-03-05 23:01:23 +00003786 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003787 break;
3788 }
bellard9307c4c2004-04-04 12:57:25 +00003789 goto fail;
3790 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003791 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003792 }
3793 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003794 case 'O':
3795 {
3796 QemuOptsList *opts_list;
3797 QemuOpts *opts;
3798
3799 opts_list = qemu_find_opts(key);
3800 if (!opts_list || opts_list->desc->name) {
3801 goto bad_type;
3802 }
3803 while (qemu_isspace(*p)) {
3804 p++;
3805 }
3806 if (!*p)
3807 break;
3808 if (get_str(buf, sizeof(buf), &p) < 0) {
3809 goto fail;
3810 }
3811 opts = qemu_opts_parse(opts_list, buf, 1);
3812 if (!opts) {
3813 goto fail;
3814 }
3815 qemu_opts_to_qdict(opts, qdict);
3816 qemu_opts_del(opts);
3817 }
3818 break;
bellard9307c4c2004-04-04 12:57:25 +00003819 case '/':
3820 {
3821 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003822
blueswir1cd390082008-11-16 13:53:32 +00003823 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003824 p++;
3825 if (*p == '/') {
3826 /* format found */
3827 p++;
3828 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003829 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003830 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003831 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003832 count = count * 10 + (*p - '0');
3833 p++;
3834 }
3835 }
3836 size = -1;
3837 format = -1;
3838 for(;;) {
3839 switch(*p) {
3840 case 'o':
3841 case 'd':
3842 case 'u':
3843 case 'x':
3844 case 'i':
3845 case 'c':
3846 format = *p++;
3847 break;
3848 case 'b':
3849 size = 1;
3850 p++;
3851 break;
3852 case 'h':
3853 size = 2;
3854 p++;
3855 break;
3856 case 'w':
3857 size = 4;
3858 p++;
3859 break;
3860 case 'g':
3861 case 'L':
3862 size = 8;
3863 p++;
3864 break;
3865 default:
3866 goto next;
3867 }
3868 }
3869 next:
blueswir1cd390082008-11-16 13:53:32 +00003870 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003871 monitor_printf(mon, "invalid char in format: '%c'\n",
3872 *p);
bellard9307c4c2004-04-04 12:57:25 +00003873 goto fail;
3874 }
bellard9307c4c2004-04-04 12:57:25 +00003875 if (format < 0)
3876 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003877 if (format != 'i') {
3878 /* for 'i', not specifying a size gives -1 as size */
3879 if (size < 0)
3880 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003881 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003882 }
bellard9307c4c2004-04-04 12:57:25 +00003883 default_fmt_format = format;
3884 } else {
3885 count = 1;
3886 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003887 if (format != 'i') {
3888 size = default_fmt_size;
3889 } else {
3890 size = -1;
3891 }
bellard9307c4c2004-04-04 12:57:25 +00003892 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003893 qdict_put(qdict, "count", qint_from_int(count));
3894 qdict_put(qdict, "format", qint_from_int(format));
3895 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003896 }
3897 break;
3898 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003899 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003900 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003901 {
blueswir1c2efc952007-09-25 17:28:42 +00003902 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003903
blueswir1cd390082008-11-16 13:53:32 +00003904 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003905 p++;
bellard34405572004-06-08 00:55:58 +00003906 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003907 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003908 if (*p == '\0') {
3909 typestr++;
3910 break;
3911 }
bellard34405572004-06-08 00:55:58 +00003912 } else {
3913 if (*p == '.') {
3914 p++;
blueswir1cd390082008-11-16 13:53:32 +00003915 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003916 p++;
bellard34405572004-06-08 00:55:58 +00003917 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003918 typestr++;
3919 break;
bellard34405572004-06-08 00:55:58 +00003920 }
3921 }
bellard13224a82006-07-14 22:03:35 +00003922 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003923 }
aliguori376253e2009-03-05 23:01:23 +00003924 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003925 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003926 /* Check if 'i' is greater than 32-bit */
3927 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3928 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3929 monitor_printf(mon, "integer is for 32-bit values\n");
3930 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003931 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003932 if (val < 0) {
3933 monitor_printf(mon, "enter a positive value\n");
3934 goto fail;
3935 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003936 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003937 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003938 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003939 }
3940 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003941 case 'o':
3942 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003943 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003944 char *end;
3945
3946 while (qemu_isspace(*p)) {
3947 p++;
3948 }
3949 if (*typestr == '?') {
3950 typestr++;
3951 if (*p == '\0') {
3952 break;
3953 }
3954 }
3955 val = strtosz(p, &end);
3956 if (val < 0) {
3957 monitor_printf(mon, "invalid size\n");
3958 goto fail;
3959 }
3960 qdict_put(qdict, key, qint_from_int(val));
3961 p = end;
3962 }
3963 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003964 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003965 {
3966 double val;
3967
3968 while (qemu_isspace(*p))
3969 p++;
3970 if (*typestr == '?') {
3971 typestr++;
3972 if (*p == '\0') {
3973 break;
3974 }
3975 }
3976 if (get_double(mon, &val, &p) < 0) {
3977 goto fail;
3978 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003979 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003980 switch (*p) {
3981 case 'm':
3982 val /= 1e3; p += 2; break;
3983 case 'u':
3984 val /= 1e6; p += 2; break;
3985 case 'n':
3986 val /= 1e9; p += 2; break;
3987 }
3988 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003989 if (*p && !qemu_isspace(*p)) {
3990 monitor_printf(mon, "Unknown unit suffix\n");
3991 goto fail;
3992 }
3993 qdict_put(qdict, key, qfloat_from_double(val));
3994 }
3995 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003996 case 'b':
3997 {
3998 const char *beg;
3999 int val;
4000
4001 while (qemu_isspace(*p)) {
4002 p++;
4003 }
4004 beg = p;
4005 while (qemu_isgraph(*p)) {
4006 p++;
4007 }
4008 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4009 val = 1;
4010 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4011 val = 0;
4012 } else {
4013 monitor_printf(mon, "Expected 'on' or 'off'\n");
4014 goto fail;
4015 }
4016 qdict_put(qdict, key, qbool_from_int(val));
4017 }
4018 break;
bellard9307c4c2004-04-04 12:57:25 +00004019 case '-':
4020 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004021 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004022 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004023 /* option */
ths3b46e622007-09-17 08:09:54 +00004024
bellard9307c4c2004-04-04 12:57:25 +00004025 c = *typestr++;
4026 if (c == '\0')
4027 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004028 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004029 p++;
bellard9307c4c2004-04-04 12:57:25 +00004030 if (*p == '-') {
4031 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004032 if(c != *p) {
4033 if(!is_valid_option(p, typestr)) {
4034
4035 monitor_printf(mon, "%s: unsupported option -%c\n",
4036 cmdname, *p);
4037 goto fail;
4038 } else {
4039 skip_key = 1;
4040 }
bellard9307c4c2004-04-04 12:57:25 +00004041 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004042 if(skip_key) {
4043 p = tmp;
4044 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004045 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004046 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004047 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004048 }
bellard9307c4c2004-04-04 12:57:25 +00004049 }
bellard9307c4c2004-04-04 12:57:25 +00004050 }
4051 break;
Wenchao Xia129be002013-08-27 20:38:26 +08004052 case 'S':
4053 {
4054 /* package all remaining string */
4055 int len;
4056
4057 while (qemu_isspace(*p)) {
4058 p++;
4059 }
4060 if (*typestr == '?') {
4061 typestr++;
4062 if (*p == '\0') {
4063 /* no remaining string: NULL argument */
4064 break;
4065 }
4066 }
4067 len = strlen(p);
4068 if (len <= 0) {
4069 monitor_printf(mon, "%s: string expected\n",
4070 cmdname);
4071 break;
4072 }
4073 qdict_put(qdict, key, qstring_from_str(p));
4074 p += len;
4075 }
4076 break;
bellard9307c4c2004-04-04 12:57:25 +00004077 default:
4078 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004079 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004080 goto fail;
4081 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004082 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004083 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004084 }
4085 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004086 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004087 p++;
4088 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004089 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4090 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004091 goto fail;
4092 }
4093
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004094 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004095
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004096fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004097 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004098 return NULL;
4099}
4100
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004101void monitor_set_error(Monitor *mon, QError *qerror)
4102{
4103 /* report only the first error */
4104 if (!mon->error) {
4105 mon->error = qerror;
4106 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004107 QDECREF(qerror);
4108 }
4109}
4110
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004111static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4112{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004113 if (ret && !monitor_has_error(mon)) {
4114 /*
4115 * If it returns failure, it must have passed on error.
4116 *
4117 * Action: Report an internal error to the client if in QMP.
4118 */
4119 qerror_report(QERR_UNDEFINED_ERROR);
Luiz Capitulino6d441432010-11-22 16:35:09 -02004120 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004121}
4122
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004123static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004124{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004125 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004126 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004127
4128 qdict = qdict_new();
4129
Wenchao Xia77172392013-08-27 20:38:20 +08004130 cmd = monitor_parse_command(mon, cmdline, 0, mon->cmd_table, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004131 if (!cmd)
4132 goto out;
4133
Luiz Capitulino4903de02010-09-16 11:01:32 -03004134 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004135 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004136 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004137 QObject *data = NULL;
4138
4139 /* XXX: ignores the error code */
4140 cmd->mhandler.cmd_new(mon, qdict, &data);
4141 assert(!monitor_has_error(mon));
4142 if (data) {
4143 cmd->user_print(mon, data);
4144 qobject_decref(data);
4145 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004146 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004147 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004148 }
4149
Luiz Capitulino13917be2009-10-07 13:41:54 -03004150out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004151 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004152}
4153
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004154static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004155{
4156 const char *p, *pstart;
4157 char cmd[128];
4158 int len;
4159
4160 p = list;
4161 for(;;) {
4162 pstart = p;
4163 p = strchr(p, '|');
4164 if (!p)
4165 p = pstart + strlen(pstart);
4166 len = p - pstart;
4167 if (len > sizeof(cmd) - 2)
4168 len = sizeof(cmd) - 2;
4169 memcpy(cmd, pstart, len);
4170 cmd[len] = '\0';
4171 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004172 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004173 }
4174 if (*p == '\0')
4175 break;
4176 p++;
4177 }
4178}
4179
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004180static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004181{
4182 DIR *ffs;
4183 struct dirent *d;
4184 char path[1024];
4185 char file[1024], file_prefix[1024];
4186 int input_path_len;
4187 const char *p;
4188
ths5fafdf22007-09-16 21:08:06 +00004189 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004190 if (!p) {
4191 input_path_len = 0;
4192 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004193 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004194 } else {
4195 input_path_len = p - input + 1;
4196 memcpy(path, input, input_path_len);
4197 if (input_path_len > sizeof(path) - 1)
4198 input_path_len = sizeof(path) - 1;
4199 path[input_path_len] = '\0';
4200 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4201 }
4202#ifdef DEBUG_COMPLETION
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004203 monitor_printf(mon, "input='%s' path='%s' prefix='%s'\n",
aliguori376253e2009-03-05 23:01:23 +00004204 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004205#endif
4206 ffs = opendir(path);
4207 if (!ffs)
4208 return;
4209 for(;;) {
4210 struct stat sb;
4211 d = readdir(ffs);
4212 if (!d)
4213 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004214
4215 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4216 continue;
4217 }
4218
bellard81d09122004-07-14 17:21:37 +00004219 if (strstart(d->d_name, file_prefix, NULL)) {
4220 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004221 if (input_path_len < sizeof(file))
4222 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4223 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004224 /* stat the file to find out if it's a directory.
4225 * In that case add a slash to speed up typing long paths
4226 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004227 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004228 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004229 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004230 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004231 }
4232 }
4233 closedir(ffs);
4234}
4235
Wenchao Xia599a9262013-08-27 20:38:15 +08004236typedef struct MonitorBlockComplete {
4237 Monitor *mon;
4238 const char *input;
4239} MonitorBlockComplete;
4240
aliguori51de9762009-03-05 23:00:43 +00004241static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004242{
aliguori51de9762009-03-05 23:00:43 +00004243 const char *name = bdrv_get_device_name(bs);
Wenchao Xia599a9262013-08-27 20:38:15 +08004244 MonitorBlockComplete *mbc = opaque;
4245 Monitor *mon = mbc->mon;
4246 const char *input = mbc->input;
bellard81d09122004-07-14 17:21:37 +00004247
4248 if (input[0] == '\0' ||
4249 !strncmp(name, (char *)input, strlen(input))) {
Wenchao Xia599a9262013-08-27 20:38:15 +08004250 readline_add_completion(mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004251 }
4252}
4253
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004254static const char *next_arg_type(const char *typestr)
4255{
4256 const char *p = strchr(typestr, ':');
4257 return (p != NULL ? ++p : typestr);
4258}
4259
Wenchao Xiac35b6402013-08-27 20:38:24 +08004260static void monitor_find_completion_by_table(Monitor *mon,
4261 const mon_cmd_t *cmd_table,
4262 char **args,
4263 int nb_args)
bellard81d09122004-07-14 17:21:37 +00004264{
4265 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004266 int i;
bellard81d09122004-07-14 17:21:37 +00004267 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004268 const mon_cmd_t *cmd;
Wenchao Xia599a9262013-08-27 20:38:15 +08004269 MonitorBlockComplete mbs;
bellard81d09122004-07-14 17:21:37 +00004270
bellard81d09122004-07-14 17:21:37 +00004271 if (nb_args <= 1) {
4272 /* command completion */
4273 if (nb_args == 0)
4274 cmdname = "";
4275 else
4276 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004277 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004278 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004279 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00004280 }
4281 } else {
4282 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004283 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004284 if (compare_cmd(args[0], cmd->name)) {
4285 break;
4286 }
bellard81d09122004-07-14 17:21:37 +00004287 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004288 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004289 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004290 }
4291
Wenchao Xiad903a772013-08-27 20:38:25 +08004292 if (cmd->sub_table) {
4293 /* do the job again */
4294 return monitor_find_completion_by_table(mon, cmd->sub_table,
4295 &args[1], nb_args - 1);
4296 }
4297
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004298 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004299 for(i = 0; i < nb_args - 2; i++) {
4300 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004301 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004302 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004303 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004304 }
4305 }
4306 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004307 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004308 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004309 }
bellard81d09122004-07-14 17:21:37 +00004310 switch(*ptype) {
4311 case 'F':
4312 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004313 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004314 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004315 break;
4316 case 'B':
4317 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004318 mbs.mon = mon;
4319 mbs.input = str;
4320 readline_set_completion_index(mon->rs, strlen(str));
4321 bdrv_iterate(block_completion_it, &mbs);
bellard81d09122004-07-14 17:21:37 +00004322 break;
bellard7fe48482004-10-09 18:08:01 +00004323 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004324 case 'S':
Wenchao Xiad903a772013-08-27 20:38:25 +08004325 if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004326 char *sep = strrchr(str, '-');
4327 if (sep)
4328 str = sep + 1;
Wenchao Xiad2674b22013-08-27 20:38:16 +08004329 readline_set_completion_index(mon->rs, strlen(str));
Amos Kong1048c882012-08-31 10:56:25 +08004330 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004331 cmd_completion(mon, str, QKeyCode_lookup[i]);
bellard64866c32006-05-07 18:03:31 +00004332 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004333 } else if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004334 monitor_find_completion_by_table(mon, cmd_table,
4335 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004336 }
4337 break;
bellard81d09122004-07-14 17:21:37 +00004338 default:
4339 break;
4340 }
4341 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004342}
4343
4344static void monitor_find_completion(Monitor *mon,
4345 const char *cmdline)
4346{
4347 char *args[MAX_ARGS];
4348 int nb_args, len;
4349
4350 /* 1. parse the cmdline */
4351 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4352 return;
4353 }
4354#ifdef DEBUG_COMPLETION
4355 for (i = 0; i < nb_args; i++) {
4356 monitor_printf(mon, "arg%d = '%s'\n", i, args[i]);
4357 }
4358#endif
4359
4360 /* if the line ends with a space, it means we want to complete the
4361 next arg */
4362 len = strlen(cmdline);
4363 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4364 if (nb_args >= MAX_ARGS) {
4365 goto cleanup;
4366 }
4367 args[nb_args++] = g_strdup("");
4368 }
4369
4370 /* 2. auto complete according to args */
4371 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004372
4373cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004374 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004375}
4376
aliguori731b0362009-03-05 23:01:42 +00004377static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004378{
aliguori731b0362009-03-05 23:01:42 +00004379 Monitor *mon = opaque;
4380
Jan Kiszkac62313b2009-12-04 14:05:29 +01004381 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004382}
4383
Luiz Capitulino09069b12010-02-04 18:10:06 -02004384static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4385{
4386 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4387 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4388}
4389
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004390/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004391 * Argument validation rules:
4392 *
4393 * 1. The argument must exist in cmd_args qdict
4394 * 2. The argument type must be the expected one
4395 *
4396 * Special case: If the argument doesn't exist in cmd_args and
4397 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4398 * checking is skipped for it.
4399 */
4400static int check_client_args_type(const QDict *client_args,
4401 const QDict *cmd_args, int flags)
4402{
4403 const QDictEntry *ent;
4404
4405 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4406 QObject *obj;
4407 QString *arg_type;
4408 const QObject *client_arg = qdict_entry_value(ent);
4409 const char *client_arg_name = qdict_entry_key(ent);
4410
4411 obj = qdict_get(cmd_args, client_arg_name);
4412 if (!obj) {
4413 if (flags & QMP_ACCEPT_UNKNOWNS) {
4414 /* handler accepts unknowns */
4415 continue;
4416 }
4417 /* client arg doesn't exist */
4418 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4419 return -1;
4420 }
4421
4422 arg_type = qobject_to_qstring(obj);
4423 assert(arg_type != NULL);
4424
4425 /* check if argument's type is correct */
4426 switch (qstring_get_str(arg_type)[0]) {
4427 case 'F':
4428 case 'B':
4429 case 's':
4430 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4431 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4432 "string");
4433 return -1;
4434 }
4435 break;
4436 case 'i':
4437 case 'l':
4438 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004439 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004440 if (qobject_type(client_arg) != QTYPE_QINT) {
4441 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4442 "int");
4443 return -1;
4444 }
4445 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004446 case 'T':
4447 if (qobject_type(client_arg) != QTYPE_QINT &&
4448 qobject_type(client_arg) != QTYPE_QFLOAT) {
4449 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4450 "number");
4451 return -1;
4452 }
4453 break;
4454 case 'b':
4455 case '-':
4456 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4457 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4458 "bool");
4459 return -1;
4460 }
4461 break;
4462 case 'O':
4463 assert(flags & QMP_ACCEPT_UNKNOWNS);
4464 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004465 case 'q':
4466 /* Any QObject can be passed. */
4467 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004468 case '/':
4469 case '.':
4470 /*
4471 * These types are not supported by QMP and thus are not
4472 * handled here. Fall through.
4473 */
4474 default:
4475 abort();
4476 }
4477 }
4478
4479 return 0;
4480}
4481
4482/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004483 * - Check if the client has passed all mandatory args
4484 * - Set special flags for argument validation
4485 */
4486static int check_mandatory_args(const QDict *cmd_args,
4487 const QDict *client_args, int *flags)
4488{
4489 const QDictEntry *ent;
4490
4491 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4492 const char *cmd_arg_name = qdict_entry_key(ent);
4493 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4494 assert(type != NULL);
4495
4496 if (qstring_get_str(type)[0] == 'O') {
4497 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4498 *flags |= QMP_ACCEPT_UNKNOWNS;
4499 } else if (qstring_get_str(type)[0] != '-' &&
4500 qstring_get_str(type)[1] != '?' &&
4501 !qdict_haskey(client_args, cmd_arg_name)) {
4502 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4503 return -1;
4504 }
4505 }
4506
4507 return 0;
4508}
4509
4510static QDict *qdict_from_args_type(const char *args_type)
4511{
4512 int i;
4513 QDict *qdict;
4514 QString *key, *type, *cur_qs;
4515
4516 assert(args_type != NULL);
4517
4518 qdict = qdict_new();
4519
4520 if (args_type == NULL || args_type[0] == '\0') {
4521 /* no args, empty qdict */
4522 goto out;
4523 }
4524
4525 key = qstring_new();
4526 type = qstring_new();
4527
4528 cur_qs = key;
4529
4530 for (i = 0;; i++) {
4531 switch (args_type[i]) {
4532 case ',':
4533 case '\0':
4534 qdict_put(qdict, qstring_get_str(key), type);
4535 QDECREF(key);
4536 if (args_type[i] == '\0') {
4537 goto out;
4538 }
4539 type = qstring_new(); /* qdict has ref */
4540 cur_qs = key = qstring_new();
4541 break;
4542 case ':':
4543 cur_qs = type;
4544 break;
4545 default:
4546 qstring_append_chr(cur_qs, args_type[i]);
4547 break;
4548 }
4549 }
4550
4551out:
4552 return qdict;
4553}
4554
4555/*
4556 * Client argument checking rules:
4557 *
4558 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004559 * 2. Each argument provided by the client must be expected
4560 * 3. Each argument provided by the client must have the type expected
4561 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004562 */
4563static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4564{
4565 int flags, err;
4566 QDict *cmd_args;
4567
4568 cmd_args = qdict_from_args_type(cmd->args_type);
4569
4570 flags = 0;
4571 err = check_mandatory_args(cmd_args, client_args, &flags);
4572 if (err) {
4573 goto out;
4574 }
4575
Luiz Capitulino4af91932010-06-22 11:44:05 -03004576 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004577
4578out:
4579 QDECREF(cmd_args);
4580 return err;
4581}
4582
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004583/*
4584 * Input object checking rules
4585 *
4586 * 1. Input object must be a dict
4587 * 2. The "execute" key must exist
4588 * 3. The "execute" key must be a string
4589 * 4. If the "arguments" key exists, it must be a dict
4590 * 5. If the "id" key exists, it can be anything (ie. json-value)
4591 * 6. Any argument not listed above is considered invalid
4592 */
4593static QDict *qmp_check_input_obj(QObject *input_obj)
4594{
4595 const QDictEntry *ent;
4596 int has_exec_key = 0;
4597 QDict *input_dict;
4598
4599 if (qobject_type(input_obj) != QTYPE_QDICT) {
4600 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4601 return NULL;
4602 }
4603
4604 input_dict = qobject_to_qdict(input_obj);
4605
4606 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4607 const char *arg_name = qdict_entry_key(ent);
4608 const QObject *arg_obj = qdict_entry_value(ent);
4609
4610 if (!strcmp(arg_name, "execute")) {
4611 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4612 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4613 "string");
4614 return NULL;
4615 }
4616 has_exec_key = 1;
4617 } else if (!strcmp(arg_name, "arguments")) {
4618 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4619 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4620 "object");
4621 return NULL;
4622 }
4623 } else if (!strcmp(arg_name, "id")) {
4624 /* FIXME: check duplicated IDs for async commands */
4625 } else {
4626 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4627 return NULL;
4628 }
4629 }
4630
4631 if (!has_exec_key) {
4632 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4633 return NULL;
4634 }
4635
4636 return input_dict;
4637}
4638
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004639static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4640 const QDict *params)
4641{
4642 int ret;
4643 QObject *data = NULL;
4644
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004645 ret = cmd->mhandler.cmd_new(mon, params, &data);
4646 handler_audit(mon, cmd, ret);
4647 monitor_protocol_emitter(mon, data);
4648 qobject_decref(data);
4649}
4650
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004651static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4652{
4653 int err;
4654 QObject *obj;
4655 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004656 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004657 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004658 Monitor *mon = cur_mon;
4659
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004660 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004661
4662 obj = json_parser_parse(tokens, NULL);
4663 if (!obj) {
4664 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004665 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004666 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004667 }
4668
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004669 input = qmp_check_input_obj(obj);
4670 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004671 qobject_decref(obj);
4672 goto err_out;
4673 }
4674
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004675 mon->mc->id = qdict_get(input, "id");
4676 qobject_incref(mon->mc->id);
4677
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004678 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004679 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004680 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004681 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004682 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004683 }
4684
Anthony Liguorie3193602011-09-02 12:34:47 -05004685 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004686 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004687 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004688 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004689 }
4690
4691 obj = qdict_get(input, "arguments");
4692 if (!obj) {
4693 args = qdict_new();
4694 } else {
4695 args = qobject_to_qdict(obj);
4696 QINCREF(args);
4697 }
4698
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004699 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004700 if (err < 0) {
4701 goto err_out;
4702 }
4703
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004704 if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004705 err = qmp_async_cmd_handler(mon, cmd, args);
4706 if (err) {
4707 /* emit the error response */
4708 goto err_out;
4709 }
Adam Litke940cc302010-01-25 12:18:44 -06004710 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004711 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004712 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004713
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004714 goto out;
4715
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004716err_out:
4717 monitor_protocol_emitter(mon, NULL);
4718out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004719 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004720 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004721}
4722
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004723/**
4724 * monitor_control_read(): Read and handle QMP input
4725 */
4726static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4727{
4728 Monitor *old_mon = cur_mon;
4729
4730 cur_mon = opaque;
4731
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004732 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004733
4734 cur_mon = old_mon;
4735}
4736
aliguori731b0362009-03-05 23:01:42 +00004737static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004738{
aliguori731b0362009-03-05 23:01:42 +00004739 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004740 int i;
aliguori376253e2009-03-05 23:01:23 +00004741
aliguori731b0362009-03-05 23:01:42 +00004742 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004743
aliguoricde76ee2009-03-05 23:01:51 +00004744 if (cur_mon->rs) {
4745 for (i = 0; i < size; i++)
4746 readline_handle_byte(cur_mon->rs, buf[i]);
4747 } else {
4748 if (size == 0 || buf[size - 1] != 0)
4749 monitor_printf(cur_mon, "corrupted command\n");
4750 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004751 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004752 }
aliguori731b0362009-03-05 23:01:42 +00004753
4754 cur_mon = old_mon;
4755}
aliguorid8f44602008-10-06 13:52:44 +00004756
aliguori376253e2009-03-05 23:01:23 +00004757static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004758{
aliguori731b0362009-03-05 23:01:42 +00004759 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004760 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004761 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004762}
4763
aliguoricde76ee2009-03-05 23:01:51 +00004764int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004765{
aliguoricde76ee2009-03-05 23:01:51 +00004766 if (!mon->rs)
4767 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004768 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004769 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004770}
4771
aliguori376253e2009-03-05 23:01:23 +00004772void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004773{
aliguoricde76ee2009-03-05 23:01:51 +00004774 if (!mon->rs)
4775 return;
aliguori731b0362009-03-05 23:01:42 +00004776 if (--mon->suspend_cnt == 0)
4777 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004778}
4779
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004780static QObject *get_qmp_greeting(void)
4781{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004782 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004783
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004784 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004785 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4786}
4787
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004788/**
4789 * monitor_control_event(): Print QMP gretting
4790 */
4791static void monitor_control_event(void *opaque, int event)
4792{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004793 QObject *data;
4794 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004795
Luiz Capitulino47116d12010-02-08 17:01:30 -02004796 switch (event) {
4797 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004798 mon->mc->command_mode = 0;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004799 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004800 monitor_json_emitter(mon, data);
4801 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004802 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004803 break;
4804 case CHR_EVENT_CLOSED:
4805 json_message_parser_destroy(&mon->mc->parser);
Anthony Liguori58617a72012-08-23 08:03:21 -05004806 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004807 mon_refcount--;
4808 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004809 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004810 }
4811}
4812
aliguori731b0362009-03-05 23:01:42 +00004813static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004814{
aliguori376253e2009-03-05 23:01:23 +00004815 Monitor *mon = opaque;
4816
aliguori2724b182009-03-05 23:01:47 +00004817 switch (event) {
4818 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004819 mon->mux_out = 0;
4820 if (mon->reset_seen) {
4821 readline_restart(mon->rs);
4822 monitor_resume(mon);
4823 monitor_flush(mon);
4824 } else {
4825 mon->suspend_cnt = 0;
4826 }
aliguori2724b182009-03-05 23:01:47 +00004827 break;
ths86e94de2007-01-05 22:01:59 +00004828
aliguori2724b182009-03-05 23:01:47 +00004829 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004830 if (mon->reset_seen) {
4831 if (mon->suspend_cnt == 0) {
4832 monitor_printf(mon, "\n");
4833 }
4834 monitor_flush(mon);
4835 monitor_suspend(mon);
4836 } else {
4837 mon->suspend_cnt++;
4838 }
4839 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004840 break;
4841
Amit Shahb6b8df52009-10-07 18:31:16 +05304842 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004843 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4844 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004845 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004846 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004847 }
4848 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004849 mon_refcount++;
4850 break;
4851
4852 case CHR_EVENT_CLOSED:
4853 mon_refcount--;
4854 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004855 break;
4856 }
ths86e94de2007-01-05 22:01:59 +00004857}
4858
Wayne Xia816f8922011-10-12 11:32:41 +08004859static int
4860compare_mon_cmd(const void *a, const void *b)
4861{
4862 return strcmp(((const mon_cmd_t *)a)->name,
4863 ((const mon_cmd_t *)b)->name);
4864}
4865
4866static void sortcmdlist(void)
4867{
4868 int array_num;
4869 int elem_size = sizeof(mon_cmd_t);
4870
4871 array_num = sizeof(mon_cmds)/elem_size-1;
4872 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4873
4874 array_num = sizeof(info_cmds)/elem_size-1;
4875 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4876}
4877
aliguori76655d62009-03-06 20:27:37 +00004878
4879/*
4880 * Local variables:
4881 * c-indent-level: 4
4882 * c-basic-offset: 4
4883 * tab-width: 8
4884 * End:
4885 */
4886
aliguori731b0362009-03-05 23:01:42 +00004887void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004888{
aliguori731b0362009-03-05 23:01:42 +00004889 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004890 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004891
4892 if (is_first_init) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +01004893 monitor_protocol_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004894 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004895 is_first_init = 0;
4896 }
aliguori87127162009-03-05 23:01:29 +00004897
Wenchao Xiab01fe892013-08-27 20:38:19 +08004898 mon = g_malloc(sizeof(*mon));
4899 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004900
aliguori87127162009-03-05 23:01:29 +00004901 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004902 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004903 if (flags & MONITOR_USE_READLINE) {
4904 mon->rs = readline_init(mon, monitor_find_completion);
4905 monitor_read_command(mon, 0);
4906 }
aliguori87127162009-03-05 23:01:29 +00004907
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004908 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004909 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004910 /* Control mode requires special handlers */
4911 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4912 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004913 qemu_chr_fe_set_echo(chr, true);
Anthony Liguori26efaca2012-08-23 13:49:02 -05004914
4915 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004916 } else {
4917 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4918 monitor_event, mon);
4919 }
aliguori87127162009-03-05 23:01:29 +00004920
Blue Swirl72cf2d42009-09-12 07:36:22 +00004921 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004922 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4923 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004924}
4925
aliguori376253e2009-03-05 23:01:23 +00004926static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004927{
aliguoribb5fc202009-03-05 23:01:15 +00004928 BlockDriverState *bs = opaque;
4929 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004930
aliguoribb5fc202009-03-05 23:01:15 +00004931 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004932 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004933 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004934 }
aliguori731b0362009-03-05 23:01:42 +00004935 if (mon->password_completion_cb)
4936 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004937
aliguori731b0362009-03-05 23:01:42 +00004938 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004939}
aliguoric0f4ce72009-03-05 23:01:01 +00004940
Anthony Liguori7060b472011-09-02 12:34:50 -05004941ReadLineState *monitor_get_rs(Monitor *mon)
4942{
4943 return mon->rs;
4944}
4945
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004946int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4947 BlockDriverCompletionFunc *completion_cb,
4948 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004949{
aliguoricde76ee2009-03-05 23:01:51 +00004950 int err;
4951
aliguoribb5fc202009-03-05 23:01:15 +00004952 if (!bdrv_key_required(bs)) {
4953 if (completion_cb)
4954 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004955 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004956 }
aliguoric0f4ce72009-03-05 23:01:01 +00004957
Luiz Capitulino94171e12009-12-07 21:37:00 +01004958 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino903a8812011-12-13 17:18:30 -02004959 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
4960 bdrv_get_encrypted_filename(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004961 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004962 }
4963
aliguori376253e2009-03-05 23:01:23 +00004964 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4965 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004966
aliguori731b0362009-03-05 23:01:42 +00004967 mon->password_completion_cb = completion_cb;
4968 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004969
aliguoricde76ee2009-03-05 23:01:51 +00004970 err = monitor_read_password(mon, bdrv_password_cb, bs);
4971
4972 if (err && completion_cb)
4973 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004974
4975 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004976}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004977
4978int monitor_read_block_device_key(Monitor *mon, const char *device,
4979 BlockDriverCompletionFunc *completion_cb,
4980 void *opaque)
4981{
4982 BlockDriverState *bs;
4983
4984 bs = bdrv_find(device);
4985 if (!bs) {
4986 monitor_printf(mon, "Device not found %s\n", device);
4987 return -1;
4988 }
4989
4990 return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);
4991}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004992
4993QemuOptsList qemu_mon_opts = {
4994 .name = "mon",
4995 .implied_opt_name = "chardev",
4996 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4997 .desc = {
4998 {
4999 .name = "mode",
5000 .type = QEMU_OPT_STRING,
5001 },{
5002 .name = "chardev",
5003 .type = QEMU_OPT_STRING,
5004 },{
5005 .name = "default",
5006 .type = QEMU_OPT_BOOL,
5007 },{
5008 .name = "pretty",
5009 .type = QEMU_OPT_BOOL,
5010 },
5011 { /* end of list */ }
5012 },
5013};