blob: 683babf24a9f6de673d081aca68bd30065221abc [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",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100509};
510QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names) != QEVENT_MAX)
511
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100512MonitorEventState monitor_event_state[QEVENT_MAX];
513QemuMutex monitor_event_state_lock;
514
515/*
516 * Emits the event to every monitor instance
517 */
518static void
519monitor_protocol_event_emit(MonitorEvent event,
520 QObject *data)
521{
522 Monitor *mon;
523
524 trace_monitor_protocol_event_emit(event, data);
525 QLIST_FOREACH(mon, &mon_list, entry) {
526 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
527 monitor_json_emitter(mon, data);
528 }
529 }
530}
531
532
533/*
534 * Queue a new event for emission to Monitor instances,
535 * applying any rate limiting if required.
536 */
537static void
538monitor_protocol_event_queue(MonitorEvent event,
539 QObject *data)
540{
541 MonitorEventState *evstate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100542 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100543 assert(event < QEVENT_MAX);
544
545 qemu_mutex_lock(&monitor_event_state_lock);
546 evstate = &(monitor_event_state[event]);
547 trace_monitor_protocol_event_queue(event,
548 data,
549 evstate->rate,
550 evstate->last,
551 now);
552
553 /* Rate limit of 0 indicates no throttling */
554 if (!evstate->rate) {
555 monitor_protocol_event_emit(event, data);
556 evstate->last = now;
557 } else {
558 int64_t delta = now - evstate->last;
559 if (evstate->data ||
560 delta < evstate->rate) {
561 /* If there's an existing event pending, replace
562 * it with the new event, otherwise schedule a
563 * timer for delayed emission
564 */
565 if (evstate->data) {
566 qobject_decref(evstate->data);
567 } else {
568 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100569 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100570 }
571 evstate->data = data;
572 qobject_incref(evstate->data);
573 } else {
574 monitor_protocol_event_emit(event, data);
575 evstate->last = now;
576 }
577 }
578 qemu_mutex_unlock(&monitor_event_state_lock);
579}
580
581
582/*
583 * The callback invoked by QemuTimer when a delayed
584 * event is ready to be emitted
585 */
586static void monitor_protocol_event_handler(void *opaque)
587{
588 MonitorEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100589 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100590
591 qemu_mutex_lock(&monitor_event_state_lock);
592
593 trace_monitor_protocol_event_handler(evstate->event,
594 evstate->data,
595 evstate->last,
596 now);
597 if (evstate->data) {
598 monitor_protocol_event_emit(evstate->event, evstate->data);
599 qobject_decref(evstate->data);
600 evstate->data = NULL;
601 }
602 evstate->last = now;
603 qemu_mutex_unlock(&monitor_event_state_lock);
604}
605
606
607/*
608 * @event: the event ID to be limited
609 * @rate: the rate limit in milliseconds
610 *
611 * Sets a rate limit on a particular event, so no
612 * more than 1 event will be emitted within @rate
613 * milliseconds
614 */
615static void
616monitor_protocol_event_throttle(MonitorEvent event,
617 int64_t rate)
618{
619 MonitorEventState *evstate;
620 assert(event < QEVENT_MAX);
621
622 evstate = &(monitor_event_state[event]);
623
624 trace_monitor_protocol_event_throttle(event, rate);
625 evstate->event = event;
626 evstate->rate = rate * SCALE_MS;
Alex Blighbc72ad62013-08-21 16:03:08 +0100627 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100628 SCALE_MS,
629 monitor_protocol_event_handler,
630 evstate);
631 evstate->last = 0;
632 evstate->data = NULL;
633}
634
635
636/* Global, one-time initializer to configure the rate limiting
637 * and initialize state */
638static void monitor_protocol_event_init(void)
639{
640 qemu_mutex_init(&monitor_event_state_lock);
641 /* Limit RTC & BALLOON events to 1 per second */
642 monitor_protocol_event_throttle(QEVENT_RTC_CHANGE, 1000);
643 monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE, 1000);
644 monitor_protocol_event_throttle(QEVENT_WATCHDOG, 1000);
645}
646
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200647/**
648 * monitor_protocol_event(): Generate a Monitor event
649 *
650 * Event-specific data can be emitted through the (optional) 'data' parameter.
651 */
652void monitor_protocol_event(MonitorEvent event, QObject *data)
653{
654 QDict *qmp;
655 const char *event_name;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200656
Blue Swirl242cd002009-12-04 18:05:45 +0000657 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200658
Daniel P. Berrange48608532012-05-21 17:59:51 +0100659 event_name = monitor_event_names[event];
660 assert(event_name != NULL);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200661
662 qmp = qdict_new();
663 timestamp_put(qmp);
664 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200665 if (data) {
666 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200667 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200668 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200669
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100670 trace_monitor_protocol_event(event, event_name, qmp);
671 monitor_protocol_event_queue(event, QOBJECT(qmp));
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200672 QDECREF(qmp);
673}
674
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200675static int do_qmp_capabilities(Monitor *mon, const QDict *params,
676 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200677{
678 /* Will setup QMP capabilities in the future */
679 if (monitor_ctrl_mode(mon)) {
680 mon->mc->command_mode = 1;
681 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200682
683 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200684}
685
Luiz Capitulino0268d972010-10-22 10:08:02 -0200686static void handle_user_command(Monitor *mon, const char *cmdline);
687
Wenchao Xiab01fe892013-08-27 20:38:19 +0800688static void monitor_data_init(Monitor *mon)
689{
690 memset(mon, 0, sizeof(Monitor));
691 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800692 /* Use *mon_cmds by default. */
693 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800694}
695
696static void monitor_data_destroy(Monitor *mon)
697{
698 QDECREF(mon->outbuf);
699}
700
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200701char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
702 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200703{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200704 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200705 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200706
Wenchao Xiab01fe892013-08-27 20:38:19 +0800707 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400708 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200709
710 old_mon = cur_mon;
711 cur_mon = &hmp;
712
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200713 if (has_cpu_index) {
714 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200715 if (ret < 0) {
716 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200717 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
718 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200719 goto out;
720 }
721 }
722
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200723 handle_user_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200724 cur_mon = old_mon;
725
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400726 if (qstring_get_length(hmp.outbuf) > 0) {
727 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200728 } else {
729 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200730 }
731
732out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800733 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200734 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200735}
736
bellard9dc39cb2004-03-14 21:38:27 +0000737static int compare_cmd(const char *name, const char *list)
738{
739 const char *p, *pstart;
740 int len;
741 len = strlen(name);
742 p = list;
743 for(;;) {
744 pstart = p;
745 p = strchr(p, '|');
746 if (!p)
747 p = pstart + strlen(pstart);
748 if ((p - pstart) == len && !memcmp(pstart, name, len))
749 return 1;
750 if (*p == '\0')
751 break;
752 p++;
753 }
754 return 0;
755}
756
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800757static int get_str(char *buf, int buf_size, const char **pp)
758{
759 const char *p;
760 char *q;
761 int c;
762
763 q = buf;
764 p = *pp;
765 while (qemu_isspace(*p)) {
766 p++;
767 }
768 if (*p == '\0') {
769 fail:
770 *q = '\0';
771 *pp = p;
772 return -1;
773 }
774 if (*p == '\"') {
775 p++;
776 while (*p != '\0' && *p != '\"') {
777 if (*p == '\\') {
778 p++;
779 c = *p++;
780 switch (c) {
781 case 'n':
782 c = '\n';
783 break;
784 case 'r':
785 c = '\r';
786 break;
787 case '\\':
788 case '\'':
789 case '\"':
790 break;
791 default:
792 qemu_printf("unsupported escape code: '\\%c'\n", c);
793 goto fail;
794 }
795 if ((q - buf) < buf_size - 1) {
796 *q++ = c;
797 }
798 } else {
799 if ((q - buf) < buf_size - 1) {
800 *q++ = *p;
801 }
802 p++;
803 }
804 }
805 if (*p != '\"') {
806 qemu_printf("unterminated string\n");
807 goto fail;
808 }
809 p++;
810 } else {
811 while (*p != '\0' && !qemu_isspace(*p)) {
812 if ((q - buf) < buf_size - 1) {
813 *q++ = *p;
814 }
815 p++;
816 }
817 }
818 *q = '\0';
819 *pp = p;
820 return 0;
821}
822
823#define MAX_ARGS 16
824
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800825static void free_cmdline_args(char **args, int nb_args)
826{
827 int i;
828
829 assert(nb_args <= MAX_ARGS);
830
831 for (i = 0; i < nb_args; i++) {
832 g_free(args[i]);
833 }
834
835}
836
837/*
838 * Parse the command line to get valid args.
839 * @cmdline: command line to be parsed.
840 * @pnb_args: location to store the number of args, must NOT be NULL.
841 * @args: location to store the args, which should be freed by caller, must
842 * NOT be NULL.
843 *
844 * Returns 0 on success, negative on failure.
845 *
846 * NOTE: this parser is an approximate form of the real command parser. Number
847 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
848 * return with failure.
849 */
850static int parse_cmdline(const char *cmdline,
851 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800852{
853 const char *p;
854 int nb_args, ret;
855 char buf[1024];
856
857 p = cmdline;
858 nb_args = 0;
859 for (;;) {
860 while (qemu_isspace(*p)) {
861 p++;
862 }
863 if (*p == '\0') {
864 break;
865 }
866 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800867 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800868 }
869 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800870 if (ret < 0) {
871 goto fail;
872 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800873 args[nb_args] = g_strdup(buf);
874 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800875 }
876 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800877 return 0;
878
879 fail:
880 free_cmdline_args(args, nb_args);
881 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800882}
883
Wenchao Xia66855492013-08-27 20:38:23 +0800884static void help_cmd_dump_one(Monitor *mon,
885 const mon_cmd_t *cmd,
886 char **prefix_args,
887 int prefix_args_nb)
888{
889 int i;
890
891 for (i = 0; i < prefix_args_nb; i++) {
892 monitor_printf(mon, "%s ", prefix_args[i]);
893 }
894 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
895}
896
897/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500898static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800899 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000900{
Anthony Liguoric227f092009-10-01 16:12:16 -0500901 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000902
Wenchao Xia66855492013-08-27 20:38:23 +0800903 /* No valid arg need to compare with, dump all in *cmds */
904 if (arg_index >= nb_args) {
905 for (cmd = cmds; cmd->name != NULL; cmd++) {
906 help_cmd_dump_one(mon, cmd, args, arg_index);
907 }
908 return;
909 }
910
911 /* Find one entry to dump */
912 for (cmd = cmds; cmd->name != NULL; cmd++) {
913 if (compare_cmd(args[arg_index], cmd->name)) {
914 if (cmd->sub_table) {
915 /* continue with next arg */
916 help_cmd_dump(mon, cmd->sub_table,
917 args, nb_args, arg_index + 1);
918 } else {
919 help_cmd_dump_one(mon, cmd, args, arg_index);
920 }
921 break;
922 }
bellard9dc39cb2004-03-14 21:38:27 +0000923 }
924}
925
aliguori376253e2009-03-05 23:01:23 +0000926static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000927{
Wenchao Xia66855492013-08-27 20:38:23 +0800928 char *args[MAX_ARGS];
929 int nb_args = 0;
930
931 /* 1. parse user input */
932 if (name) {
933 /* special case for log, directly dump and return */
934 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000935 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000936 monitor_printf(mon, "Log items (comma separated):\n");
937 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000938 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000939 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000940 }
Wenchao Xia66855492013-08-27 20:38:23 +0800941 return;
942 }
943
944 if (parse_cmdline(name, &nb_args, args) < 0) {
945 return;
bellardf193c792004-03-21 17:06:25 +0000946 }
bellard9dc39cb2004-03-14 21:38:27 +0000947 }
Wenchao Xia66855492013-08-27 20:38:23 +0800948
949 /* 2. dump the contents according to parsed args */
950 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
951
952 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000953}
954
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300955static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300956{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300957 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300958}
959
Lluísfc764102011-08-31 20:31:18 +0200960static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530961{
962 const char *tp_name = qdict_get_str(qdict, "name");
963 bool new_state = qdict_get_bool(qdict, "option");
Blue Swirlf871d682010-10-13 19:14:29 +0000964
Lluís Vilanovaca285c32013-03-05 14:47:43 +0100965 bool found = false;
966 TraceEvent *ev = NULL;
967 while ((ev = trace_event_pattern(tp_name, ev)) != NULL) {
968 found = true;
969 if (!trace_event_get_state_static(ev)) {
970 monitor_printf(mon, "event \"%s\" is not traceable\n", tp_name);
971 } else {
972 trace_event_set_state_dynamic(ev, new_state);
973 }
974 }
975 if (!trace_event_is_pattern(tp_name) && !found) {
Blue Swirlf871d682010-10-13 19:14:29 +0000976 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
977 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530978}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100979
Michael Rothc45a8162011-10-02 08:44:37 -0500980#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100981static void do_trace_file(Monitor *mon, const QDict *qdict)
982{
983 const char *op = qdict_get_try_str(qdict, "op");
984 const char *arg = qdict_get_try_str(qdict, "arg");
985
986 if (!op) {
987 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
988 } else if (!strcmp(op, "on")) {
989 st_set_trace_file_enabled(true);
990 } else if (!strcmp(op, "off")) {
991 st_set_trace_file_enabled(false);
992 } else if (!strcmp(op, "flush")) {
993 st_flush_trace_buffer();
994 } else if (!strcmp(op, "set")) {
995 if (arg) {
996 st_set_trace_file(arg);
997 }
998 } else {
999 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
1000 help_cmd(mon, "trace-file");
1001 }
1002}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301003#endif
1004
Adam Litke940cc302010-01-25 12:18:44 -06001005static void user_monitor_complete(void *opaque, QObject *ret_data)
1006{
1007 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
1008
1009 if (ret_data) {
1010 data->user_print(data->mon, ret_data);
1011 }
1012 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -05001013 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -06001014}
1015
1016static void qmp_monitor_complete(void *opaque, QObject *ret_data)
1017{
1018 monitor_protocol_emitter(opaque, ret_data);
1019}
1020
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03001021static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
1022 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -06001023{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03001024 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -06001025}
1026
Adam Litke940cc302010-01-25 12:18:44 -06001027static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
1028 const QDict *params)
1029{
1030 int ret;
1031
Anthony Liguori7267c092011-08-20 22:09:37 -05001032 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -06001033 cb_data->mon = mon;
1034 cb_data->user_print = cmd->user_print;
1035 monitor_suspend(mon);
1036 ret = cmd->mhandler.cmd_async(mon, params,
1037 user_monitor_complete, cb_data);
1038 if (ret < 0) {
1039 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -05001040 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -06001041 }
1042}
1043
Wenchao Xia84c44612013-01-14 14:06:29 +08001044static void do_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +00001045{
Luiz Capitulino13c74252009-10-07 13:41:55 -03001046 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +00001047}
1048
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -03001049CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +00001050{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -03001051 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001052 const mon_cmd_t *cmd;
1053
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03001054 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -02001055 info = g_malloc0(sizeof(*info));
1056 info->value = g_malloc0(sizeof(*info->value));
1057 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001058
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -03001059 info->next = cmd_list;
1060 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001061 }
1062
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -03001063 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +00001064}
1065
Daniel P. Berrange48608532012-05-21 17:59:51 +01001066EventInfoList *qmp_query_events(Error **errp)
1067{
1068 EventInfoList *info, *ev_list = NULL;
1069 MonitorEvent e;
1070
1071 for (e = 0 ; e < QEVENT_MAX ; e++) {
1072 const char *event_name = monitor_event_names[e];
1073 assert(event_name != NULL);
1074 info = g_malloc0(sizeof(*info));
1075 info->value = g_malloc0(sizeof(*info->value));
1076 info->value->name = g_strdup(event_name);
1077
1078 info->next = ev_list;
1079 ev_list = info;
1080 }
1081
1082 return ev_list;
1083}
1084
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001085/* set the current CPU defined by the user */
1086int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001087{
Andreas Färber55e5c282012-12-17 06:18:02 +01001088 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001089
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001090 cpu = qemu_get_cpu(cpu_index);
1091 if (cpu == NULL) {
1092 return -1;
bellard6a00d602005-11-21 23:25:50 +00001093 }
Andreas Färbercb446ec2013-05-01 14:24:52 +02001094 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001095 return 0;
bellard6a00d602005-11-21 23:25:50 +00001096}
1097
Andreas Färber9349b4f2012-03-14 01:38:32 +01001098static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001099{
aliguori731b0362009-03-05 23:01:42 +00001100 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001101 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +00001102 }
Avi Kivity4c0960c2009-08-17 23:19:53 +03001103 cpu_synchronize_state(cur_mon->mon_cpu);
Andreas Färbercb446ec2013-05-01 14:24:52 +02001104 return cur_mon->mon_cpu->env_ptr;
bellard6a00d602005-11-21 23:25:50 +00001105}
1106
Luiz Capitulino99b77962011-10-24 10:53:44 -02001107int monitor_get_cpu_index(void)
1108{
Andreas Färber55e5c282012-12-17 06:18:02 +01001109 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
1110 return cpu->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001111}
1112
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001113static void do_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001114{
Andreas Färber878096e2013-05-27 01:33:50 +02001115 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001116 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +00001117 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001118 cpu = ENV_GET_CPU(env);
1119 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +00001120}
1121
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001122static void do_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001123{
aliguori376253e2009-03-05 23:01:23 +00001124 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001125}
1126
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001127static void do_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001128{
1129 int i;
bellard7e2515e2004-08-01 21:52:19 +00001130 const char *str;
ths3b46e622007-09-17 08:09:54 +00001131
aliguoricde76ee2009-03-05 23:01:51 +00001132 if (!mon->rs)
1133 return;
bellard7e2515e2004-08-01 21:52:19 +00001134 i = 0;
1135 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001136 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001137 if (!str)
1138 break;
aliguori376253e2009-03-05 23:01:23 +00001139 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001140 i++;
bellardaa455482004-04-04 13:07:25 +00001141 }
1142}
1143
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001144static void do_info_cpu_stats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001145{
Andreas Färber878096e2013-05-27 01:33:50 +02001146 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001147 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +00001148
1149 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001150 cpu = ENV_GET_CPU(env);
1151 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001152}
j_mayer76a66252007-03-07 08:32:30 +00001153
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001154static void do_trace_print_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301155{
Lluísfc764102011-08-31 20:31:18 +02001156 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301157}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301158
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001159static int client_migrate_info(Monitor *mon, const QDict *qdict,
1160 MonitorCompletion cb, void *opaque)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001161{
1162 const char *protocol = qdict_get_str(qdict, "protocol");
1163 const char *hostname = qdict_get_str(qdict, "hostname");
1164 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1165 int port = qdict_get_try_int(qdict, "port", -1);
1166 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1167 int ret;
1168
1169 if (strcmp(protocol, "spice") == 0) {
1170 if (!using_spice) {
1171 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1172 return -1;
1173 }
1174
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001175 if (port == -1 && tls_port == -1) {
1176 qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
1177 return -1;
1178 }
1179
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001180 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
1181 cb, opaque);
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001182 if (ret != 0) {
1183 qerror_report(QERR_UNDEFINED_ERROR);
1184 return -1;
1185 }
1186 return 0;
1187 }
1188
1189 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1190 return -1;
1191}
1192
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001193static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001194{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001195 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001196}
1197
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001198static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001199{
1200 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001201 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001202
bellard9307c4c2004-04-04 12:57:25 +00001203 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001204 mask = 0;
1205 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001206 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001207 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001208 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001209 return;
1210 }
1211 }
Peter Maydell24537a02013-02-11 16:41:23 +00001212 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001213}
1214
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001215static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001216{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001217 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001218 if (!option || !strcmp(option, "on")) {
1219 singlestep = 1;
1220 } else if (!strcmp(option, "off")) {
1221 singlestep = 0;
1222 } else {
1223 monitor_printf(mon, "unexpected option %s\n", option);
1224 }
1225}
1226
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001227static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001228{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001229 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001230 if (!device)
1231 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1232 if (gdbserver_start(device) < 0) {
1233 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1234 device);
1235 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001236 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001237 } else {
aliguori59030a82009-04-05 18:43:41 +00001238 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1239 device);
bellard8a7ddc32004-03-31 19:00:16 +00001240 }
1241}
1242
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001243static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001244{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001245 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001246 if (select_watchdog_action(action) == -1) {
1247 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1248 }
1249}
1250
aliguori376253e2009-03-05 23:01:23 +00001251static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001252{
aliguori376253e2009-03-05 23:01:23 +00001253 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001254 switch(c) {
1255 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001256 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001257 break;
1258 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001259 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001260 break;
1261 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001262 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001263 break;
1264 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001265 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001266 break;
1267 default:
1268 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001269 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001270 } else {
aliguori376253e2009-03-05 23:01:23 +00001271 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001272 }
1273 break;
1274 }
aliguori376253e2009-03-05 23:01:23 +00001275 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001276}
1277
aliguori376253e2009-03-05 23:01:23 +00001278static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001279 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001280{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001281 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001282 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001283 uint8_t buf[16];
1284 uint64_t v;
1285
1286 if (format == 'i') {
1287 int flags;
1288 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001289 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001290#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001291 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001292 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001293 } else if (wsize == 4) {
1294 flags = 0;
1295 } else {
bellard6a15fd12006-04-12 21:07:07 +00001296 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001297 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001298 if (env) {
1299#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001300 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001301 (env->segs[R_CS].flags & DESC_L_MASK))
1302 flags = 2;
1303 else
1304#endif
1305 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1306 flags = 1;
1307 }
bellard4c27ba22004-04-25 18:05:08 +00001308 }
1309#endif
aliguori376253e2009-03-05 23:01:23 +00001310 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001311 return;
1312 }
1313
1314 len = wsize * count;
1315 if (wsize == 1)
1316 line_size = 8;
1317 else
1318 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001319 max_digits = 0;
1320
1321 switch(format) {
1322 case 'o':
1323 max_digits = (wsize * 8 + 2) / 3;
1324 break;
1325 default:
1326 case 'x':
1327 max_digits = (wsize * 8) / 4;
1328 break;
1329 case 'u':
1330 case 'd':
1331 max_digits = (wsize * 8 * 10 + 32) / 33;
1332 break;
1333 case 'c':
1334 wsize = 1;
1335 break;
1336 }
1337
1338 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001339 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001340 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001341 else
aliguori376253e2009-03-05 23:01:23 +00001342 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001343 l = len;
1344 if (l > line_size)
1345 l = line_size;
1346 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001347 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001348 } else {
bellard6a00d602005-11-21 23:25:50 +00001349 env = mon_get_cpu();
Andreas Färberf17ec442013-06-29 19:40:58 +02001350 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001351 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001352 break;
1353 }
bellard9307c4c2004-04-04 12:57:25 +00001354 }
ths5fafdf22007-09-16 21:08:06 +00001355 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001356 while (i < l) {
1357 switch(wsize) {
1358 default:
1359 case 1:
1360 v = ldub_raw(buf + i);
1361 break;
1362 case 2:
1363 v = lduw_raw(buf + i);
1364 break;
1365 case 4:
bellard92a31b12005-02-10 22:00:52 +00001366 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001367 break;
1368 case 8:
1369 v = ldq_raw(buf + i);
1370 break;
1371 }
aliguori376253e2009-03-05 23:01:23 +00001372 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001373 switch(format) {
1374 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001375 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001376 break;
1377 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001378 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001379 break;
1380 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001381 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001382 break;
1383 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001384 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001385 break;
1386 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001387 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001388 break;
1389 }
1390 i += wsize;
1391 }
aliguori376253e2009-03-05 23:01:23 +00001392 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001393 addr += l;
1394 len -= l;
1395 }
1396}
1397
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001398static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001399{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001400 int count = qdict_get_int(qdict, "count");
1401 int format = qdict_get_int(qdict, "format");
1402 int size = qdict_get_int(qdict, "size");
1403 target_long addr = qdict_get_int(qdict, "addr");
1404
aliguori376253e2009-03-05 23:01:23 +00001405 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001406}
1407
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001408static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001409{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001410 int count = qdict_get_int(qdict, "count");
1411 int format = qdict_get_int(qdict, "format");
1412 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001413 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001414
aliguori376253e2009-03-05 23:01:23 +00001415 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001416}
1417
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001418static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001419{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001420 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001421 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001422
bellard9307c4c2004-04-04 12:57:25 +00001423 switch(format) {
1424 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001425 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001426 break;
1427 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001428 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001429 break;
1430 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001431 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001432 break;
1433 default:
1434 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001435 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001436 break;
1437 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001438 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001439 break;
1440 }
aliguori376253e2009-03-05 23:01:23 +00001441 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001442}
1443
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001444static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001445{
1446 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001447 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001448 uint32_t start = qdict_get_int(qdict, "start");
1449 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001450
1451 sum = 0;
1452 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001453 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001454 /* BSD sum algorithm ('sum' Unix command) */
1455 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001456 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001457 }
aliguori376253e2009-03-05 23:01:23 +00001458 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001459}
1460
bellard13224a82006-07-14 22:03:35 +00001461static int mouse_button_state;
1462
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001463static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001464{
1465 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001466 const char *dx_str = qdict_get_str(qdict, "dx_str");
1467 const char *dy_str = qdict_get_str(qdict, "dy_str");
1468 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001469 dx = strtol(dx_str, NULL, 0);
1470 dy = strtol(dy_str, NULL, 0);
1471 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001472 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001473 dz = strtol(dz_str, NULL, 0);
1474 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1475}
1476
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001477static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001478{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001479 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001480 mouse_button_state = button_state;
1481 kbd_mouse_event(0, 0, 0, mouse_button_state);
1482}
1483
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001484static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001485{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001486 int size = qdict_get_int(qdict, "size");
1487 int addr = qdict_get_int(qdict, "addr");
1488 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001489 uint32_t val;
1490 int suffix;
1491
1492 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001493 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001494 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001495 addr++;
1496 }
1497 addr &= 0xffff;
1498
1499 switch(size) {
1500 default:
1501 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001502 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001503 suffix = 'b';
1504 break;
1505 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001506 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001507 suffix = 'w';
1508 break;
1509 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001510 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001511 suffix = 'l';
1512 break;
1513 }
aliguori376253e2009-03-05 23:01:23 +00001514 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1515 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001516}
bellarda3a91a32004-06-04 11:06:21 +00001517
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001518static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001519{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001520 int size = qdict_get_int(qdict, "size");
1521 int addr = qdict_get_int(qdict, "addr");
1522 int val = qdict_get_int(qdict, "val");
1523
Jan Kiszkaf1147842009-07-14 10:20:11 +02001524 addr &= IOPORTS_MASK;
1525
1526 switch (size) {
1527 default:
1528 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001529 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001530 break;
1531 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001532 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001533 break;
1534 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001535 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001536 break;
1537 }
1538}
1539
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001540static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001541{
1542 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001543 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001544
Jan Kiszka76e30d02009-07-02 00:19:02 +02001545 res = qemu_boot_set(bootdevice);
1546 if (res == 0) {
1547 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1548 } else if (res > 0) {
1549 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001550 } else {
aliguori376253e2009-03-05 23:01:23 +00001551 monitor_printf(mon, "no function defined to set boot device list for "
1552 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001553 }
1554}
1555
bellardb86bda52004-09-18 19:32:46 +00001556#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001557static void print_pte(Monitor *mon, hwaddr addr,
1558 hwaddr pte,
1559 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001560{
Blue Swirld65aaf32010-12-11 18:56:24 +00001561#ifdef TARGET_X86_64
1562 if (addr & (1ULL << 47)) {
1563 addr |= -1LL << 48;
1564 }
1565#endif
1566 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1567 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001568 addr,
1569 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001570 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001571 pte & PG_GLOBAL_MASK ? 'G' : '-',
1572 pte & PG_PSE_MASK ? 'P' : '-',
1573 pte & PG_DIRTY_MASK ? 'D' : '-',
1574 pte & PG_ACCESSED_MASK ? 'A' : '-',
1575 pte & PG_PCD_MASK ? 'C' : '-',
1576 pte & PG_PWT_MASK ? 'T' : '-',
1577 pte & PG_USER_MASK ? 'U' : '-',
1578 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001579}
1580
Andreas Färber9349b4f2012-03-14 01:38:32 +01001581static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001582{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001583 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001584 uint32_t pgd, pde, pte;
1585
bellardb86bda52004-09-18 19:32:46 +00001586 pgd = env->cr[3] & ~0xfff;
1587 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001588 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001589 pde = le32_to_cpu(pde);
1590 if (pde & PG_PRESENT_MASK) {
1591 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001592 /* 4M pages */
1593 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001594 } else {
1595 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001596 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001597 pte = le32_to_cpu(pte);
1598 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001599 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001600 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001601 ~0xfff);
1602 }
1603 }
1604 }
1605 }
1606 }
1607}
1608
Andreas Färber9349b4f2012-03-14 01:38:32 +01001609static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001610{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001611 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001612 uint64_t pdpe, pde, pte;
1613 uint64_t pdp_addr, pd_addr, pt_addr;
1614
1615 pdp_addr = env->cr[3] & ~0x1f;
1616 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001617 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001618 pdpe = le64_to_cpu(pdpe);
1619 if (pdpe & PG_PRESENT_MASK) {
1620 pd_addr = pdpe & 0x3fffffffff000ULL;
1621 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001622 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001623 pde = le64_to_cpu(pde);
1624 if (pde & PG_PRESENT_MASK) {
1625 if (pde & PG_PSE_MASK) {
1626 /* 2M pages with PAE, CR4.PSE is ignored */
1627 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001628 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001629 } else {
1630 pt_addr = pde & 0x3fffffffff000ULL;
1631 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001632 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001633 pte = le64_to_cpu(pte);
1634 if (pte & PG_PRESENT_MASK) {
1635 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1636 + (l3 << 12),
1637 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001638 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001639 }
1640 }
1641 }
1642 }
1643 }
1644 }
1645 }
1646}
1647
1648#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001649static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001650{
1651 uint64_t l1, l2, l3, l4;
1652 uint64_t pml4e, pdpe, pde, pte;
1653 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1654
1655 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1656 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001657 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001658 pml4e = le64_to_cpu(pml4e);
1659 if (pml4e & PG_PRESENT_MASK) {
1660 pdp_addr = pml4e & 0x3fffffffff000ULL;
1661 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001662 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001663 pdpe = le64_to_cpu(pdpe);
1664 if (pdpe & PG_PRESENT_MASK) {
1665 if (pdpe & PG_PSE_MASK) {
1666 /* 1G pages, CR4.PSE is ignored */
1667 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1668 0x3ffffc0000000ULL);
1669 } else {
1670 pd_addr = pdpe & 0x3fffffffff000ULL;
1671 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001672 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001673 pde = le64_to_cpu(pde);
1674 if (pde & PG_PRESENT_MASK) {
1675 if (pde & PG_PSE_MASK) {
1676 /* 2M pages, CR4.PSE is ignored */
1677 print_pte(mon, (l1 << 39) + (l2 << 30) +
1678 (l3 << 21), pde,
1679 0x3ffffffe00000ULL);
1680 } else {
1681 pt_addr = pde & 0x3fffffffff000ULL;
1682 for (l4 = 0; l4 < 512; l4++) {
1683 cpu_physical_memory_read(pt_addr
1684 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001685 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001686 pte = le64_to_cpu(pte);
1687 if (pte & PG_PRESENT_MASK) {
1688 print_pte(mon, (l1 << 39) +
1689 (l2 << 30) +
1690 (l3 << 21) + (l4 << 12),
1691 pte & ~PG_PSE_MASK,
1692 0x3fffffffff000ULL);
1693 }
1694 }
1695 }
1696 }
1697 }
1698 }
1699 }
1700 }
1701 }
1702 }
1703}
1704#endif
1705
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001706static void tlb_info(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001707{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001708 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001709
1710 env = mon_get_cpu();
1711
1712 if (!(env->cr[0] & CR0_PG_MASK)) {
1713 monitor_printf(mon, "PG disabled\n");
1714 return;
1715 }
1716 if (env->cr[4] & CR4_PAE_MASK) {
1717#ifdef TARGET_X86_64
1718 if (env->hflags & HF_LMA_MASK) {
1719 tlb_info_64(mon, env);
1720 } else
1721#endif
1722 {
1723 tlb_info_pae32(mon, env);
1724 }
1725 } else {
1726 tlb_info_32(mon, env);
1727 }
1728}
1729
Avi Kivitya8170e52012-10-23 12:30:10 +02001730static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001731 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001732 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001733{
bellard9746b152004-11-11 18:30:24 +00001734 int prot1;
1735 prot1 = *plast_prot;
1736 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001737 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001738 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1739 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001740 *pstart, end, end - *pstart,
1741 prot1 & PG_USER_MASK ? 'u' : '-',
1742 'r',
1743 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001744 }
1745 if (prot != 0)
1746 *pstart = end;
1747 else
1748 *pstart = -1;
1749 *plast_prot = prot;
1750 }
1751}
1752
Andreas Färber9349b4f2012-03-14 01:38:32 +01001753static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001754{
Austin Clementsb49ca722011-08-14 23:19:21 -04001755 unsigned int l1, l2;
1756 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001757 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001758 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001759
bellardb86bda52004-09-18 19:32:46 +00001760 pgd = env->cr[3] & ~0xfff;
1761 last_prot = 0;
1762 start = -1;
1763 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001764 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001765 pde = le32_to_cpu(pde);
1766 end = l1 << 22;
1767 if (pde & PG_PRESENT_MASK) {
1768 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1769 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001770 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001771 } else {
1772 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001773 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001774 pte = le32_to_cpu(pte);
1775 end = (l1 << 22) + (l2 << 12);
1776 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001777 prot = pte & pde &
1778 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001779 } else {
1780 prot = 0;
1781 }
aliguori376253e2009-03-05 23:01:23 +00001782 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001783 }
1784 }
1785 } else {
1786 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001787 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001788 }
1789 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001790 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001791 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001792}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001793
Andreas Färber9349b4f2012-03-14 01:38:32 +01001794static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001795{
Austin Clementsb49ca722011-08-14 23:19:21 -04001796 unsigned int l1, l2, l3;
1797 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001798 uint64_t pdpe, pde, pte;
1799 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001800 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001801
1802 pdp_addr = env->cr[3] & ~0x1f;
1803 last_prot = 0;
1804 start = -1;
1805 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001806 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001807 pdpe = le64_to_cpu(pdpe);
1808 end = l1 << 30;
1809 if (pdpe & PG_PRESENT_MASK) {
1810 pd_addr = pdpe & 0x3fffffffff000ULL;
1811 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001812 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001813 pde = le64_to_cpu(pde);
1814 end = (l1 << 30) + (l2 << 21);
1815 if (pde & PG_PRESENT_MASK) {
1816 if (pde & PG_PSE_MASK) {
1817 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1818 PG_PRESENT_MASK);
1819 mem_print(mon, &start, &last_prot, end, prot);
1820 } else {
1821 pt_addr = pde & 0x3fffffffff000ULL;
1822 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001823 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001824 pte = le64_to_cpu(pte);
1825 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1826 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001827 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1828 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001829 } else {
1830 prot = 0;
1831 }
1832 mem_print(mon, &start, &last_prot, end, prot);
1833 }
1834 }
1835 } else {
1836 prot = 0;
1837 mem_print(mon, &start, &last_prot, end, prot);
1838 }
1839 }
1840 } else {
1841 prot = 0;
1842 mem_print(mon, &start, &last_prot, end, prot);
1843 }
1844 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001845 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001846 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001847}
1848
1849
1850#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001851static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001852{
1853 int prot, last_prot;
1854 uint64_t l1, l2, l3, l4;
1855 uint64_t pml4e, pdpe, pde, pte;
1856 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1857
1858 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1859 last_prot = 0;
1860 start = -1;
1861 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001862 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001863 pml4e = le64_to_cpu(pml4e);
1864 end = l1 << 39;
1865 if (pml4e & PG_PRESENT_MASK) {
1866 pdp_addr = pml4e & 0x3fffffffff000ULL;
1867 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001868 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001869 pdpe = le64_to_cpu(pdpe);
1870 end = (l1 << 39) + (l2 << 30);
1871 if (pdpe & PG_PRESENT_MASK) {
1872 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001873 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1874 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001875 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001876 mem_print(mon, &start, &last_prot, end, prot);
1877 } else {
1878 pd_addr = pdpe & 0x3fffffffff000ULL;
1879 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001880 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001881 pde = le64_to_cpu(pde);
1882 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1883 if (pde & PG_PRESENT_MASK) {
1884 if (pde & PG_PSE_MASK) {
1885 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1886 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001887 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001888 mem_print(mon, &start, &last_prot, end, prot);
1889 } else {
1890 pt_addr = pde & 0x3fffffffff000ULL;
1891 for (l4 = 0; l4 < 512; l4++) {
1892 cpu_physical_memory_read(pt_addr
1893 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001894 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001895 pte = le64_to_cpu(pte);
1896 end = (l1 << 39) + (l2 << 30) +
1897 (l3 << 21) + (l4 << 12);
1898 if (pte & PG_PRESENT_MASK) {
1899 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1900 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001901 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001902 } else {
1903 prot = 0;
1904 }
1905 mem_print(mon, &start, &last_prot, end, prot);
1906 }
1907 }
1908 } else {
1909 prot = 0;
1910 mem_print(mon, &start, &last_prot, end, prot);
1911 }
1912 }
1913 }
1914 } else {
1915 prot = 0;
1916 mem_print(mon, &start, &last_prot, end, prot);
1917 }
1918 }
1919 } else {
1920 prot = 0;
1921 mem_print(mon, &start, &last_prot, end, prot);
1922 }
1923 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001924 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001925 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001926}
1927#endif
1928
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001929static void mem_info(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001930{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001931 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001932
1933 env = mon_get_cpu();
1934
1935 if (!(env->cr[0] & CR0_PG_MASK)) {
1936 monitor_printf(mon, "PG disabled\n");
1937 return;
1938 }
1939 if (env->cr[4] & CR4_PAE_MASK) {
1940#ifdef TARGET_X86_64
1941 if (env->hflags & HF_LMA_MASK) {
1942 mem_info_64(mon, env);
1943 } else
1944#endif
1945 {
1946 mem_info_pae32(mon, env);
1947 }
1948 } else {
1949 mem_info_32(mon, env);
1950 }
1951}
bellardb86bda52004-09-18 19:32:46 +00001952#endif
1953
aurel327c664e22009-03-03 06:12:22 +00001954#if defined(TARGET_SH4)
1955
aliguori376253e2009-03-05 23:01:23 +00001956static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001957{
aliguori376253e2009-03-05 23:01:23 +00001958 monitor_printf(mon, " tlb%i:\t"
1959 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1960 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1961 "dirty=%hhu writethrough=%hhu\n",
1962 idx,
1963 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1964 tlb->v, tlb->sh, tlb->c, tlb->pr,
1965 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001966}
1967
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001968static void tlb_info(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001969{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001970 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00001971 int i;
1972
aliguori376253e2009-03-05 23:01:23 +00001973 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001974 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001975 print_tlb (mon, i, &env->itlb[i]);
1976 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001977 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001978 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001979}
1980
1981#endif
1982
Max Filippov692f7372012-01-07 20:02:40 +04001983#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001984static void tlb_info(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001985{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001986 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00001987
1988 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1989}
1990#endif
1991
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001992static void do_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001993{
1994 mtree_info((fprintf_function)monitor_printf, mon);
1995}
1996
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001997static void do_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001998{
aliguorib28b6232009-04-22 20:20:29 +00001999 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01002000 CPUState *cpu;
aliguori030ea372009-04-21 22:30:47 +00002001
2002 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2003 for (i = 0; i < nb_numa_nodes; i++) {
2004 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02002005 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01002006 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01002007 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00002008 }
2009 }
2010 monitor_printf(mon, "\n");
2011 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2012 node_mem[i] >> 20);
2013 }
2014}
2015
bellard5f1ce942006-02-08 22:40:15 +00002016#ifdef CONFIG_PROFILER
2017
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002018int64_t qemu_time;
2019int64_t dev_time;
2020
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002021static void do_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002022{
2023 int64_t total;
2024 total = qemu_time;
2025 if (total == 0)
2026 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002027 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002028 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002029 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002030 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002031 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002032 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002033}
2034#else
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002035static void do_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002036{
aliguori376253e2009-03-05 23:01:23 +00002037 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002038}
2039#endif
2040
bellardec36b692006-07-16 18:57:03 +00002041/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002042static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002043
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002044static void do_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002045{
2046 int i;
2047 CaptureState *s;
2048
2049 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002050 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002051 s->ops.info (s->opaque);
2052 }
2053}
2054
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002055static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002056{
2057 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002058 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002059 CaptureState *s;
2060
2061 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2062 if (i == n) {
2063 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002064 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002065 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002066 return;
2067 }
2068 }
2069}
2070
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002071static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002072{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002073 const char *path = qdict_get_str(qdict, "path");
2074 int has_freq = qdict_haskey(qdict, "freq");
2075 int freq = qdict_get_try_int(qdict, "freq", -1);
2076 int has_bits = qdict_haskey(qdict, "bits");
2077 int bits = qdict_get_try_int(qdict, "bits", -1);
2078 int has_channels = qdict_haskey(qdict, "nchannels");
2079 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002080 CaptureState *s;
2081
Anthony Liguori7267c092011-08-20 22:09:37 -05002082 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002083
2084 freq = has_freq ? freq : 44100;
2085 bits = has_bits ? bits : 16;
2086 nchannels = has_channels ? nchannels : 2;
2087
2088 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002089 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002090 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002091 return;
bellardec36b692006-07-16 18:57:03 +00002092 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002093 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002094}
bellardec36b692006-07-16 18:57:03 +00002095
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002096static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002097{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002098 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002099
aliguori76655d62009-03-06 20:27:37 +00002100 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002101 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002102 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002103 return acl;
2104}
aliguori76655d62009-03-06 20:27:37 +00002105
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002106static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002107{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002108 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002109 qemu_acl *acl = find_acl(mon, aclname);
2110 qemu_acl_entry *entry;
2111 int i = 0;
2112
2113 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002114 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002115 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002116 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002117 i++;
2118 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002119 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002120 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002121 }
2122}
2123
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002124static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002125{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002126 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002127 qemu_acl *acl = find_acl(mon, aclname);
2128
2129 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002130 qemu_acl_reset(acl);
2131 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002132 }
2133}
aliguori76655d62009-03-06 20:27:37 +00002134
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002135static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002136{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002137 const char *aclname = qdict_get_str(qdict, "aclname");
2138 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002139 qemu_acl *acl = find_acl(mon, aclname);
2140
2141 if (acl) {
2142 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002143 acl->defaultDeny = 0;
2144 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002145 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002146 acl->defaultDeny = 1;
2147 monitor_printf(mon, "acl: policy set to 'deny'\n");
2148 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002149 monitor_printf(mon, "acl: unknown policy '%s', "
2150 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002151 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002152 }
2153}
aliguori76655d62009-03-06 20:27:37 +00002154
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002155static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002156{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002157 const char *aclname = qdict_get_str(qdict, "aclname");
2158 const char *match = qdict_get_str(qdict, "match");
2159 const char *policy = qdict_get_str(qdict, "policy");
2160 int has_index = qdict_haskey(qdict, "index");
2161 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002162 qemu_acl *acl = find_acl(mon, aclname);
2163 int deny, ret;
2164
2165 if (acl) {
2166 if (strcmp(policy, "allow") == 0) {
2167 deny = 0;
2168 } else if (strcmp(policy, "deny") == 0) {
2169 deny = 1;
2170 } else {
2171 monitor_printf(mon, "acl: unknown policy '%s', "
2172 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002173 return;
2174 }
aliguori28a76be2009-03-06 20:27:40 +00002175 if (has_index)
2176 ret = qemu_acl_insert(acl, deny, match, index);
2177 else
2178 ret = qemu_acl_append(acl, deny, match);
2179 if (ret < 0)
2180 monitor_printf(mon, "acl: unable to add acl entry\n");
2181 else
2182 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002183 }
2184}
aliguori76655d62009-03-06 20:27:37 +00002185
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002186static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002187{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002188 const char *aclname = qdict_get_str(qdict, "aclname");
2189 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002190 qemu_acl *acl = find_acl(mon, aclname);
2191 int ret;
aliguori76655d62009-03-06 20:27:37 +00002192
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002193 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002194 ret = qemu_acl_remove(acl, match);
2195 if (ret < 0)
2196 monitor_printf(mon, "acl: no matching acl entry\n");
2197 else
2198 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002199 }
2200}
2201
Huang Ying79c4f6b2009-06-23 10:05:14 +08002202#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002203static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002204{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002205 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002206 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002207 int cpu_index = qdict_get_int(qdict, "cpu_index");
2208 int bank = qdict_get_int(qdict, "bank");
2209 uint64_t status = qdict_get_int(qdict, "status");
2210 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2211 uint64_t addr = qdict_get_int(qdict, "addr");
2212 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002213 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002214
Jan Kiszka747461c2011-03-02 08:56:10 +01002215 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2216 flags |= MCE_INJECT_BROADCAST;
2217 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002218 cs = qemu_get_cpu(cpu_index);
2219 if (cs != NULL) {
2220 cpu = X86_CPU(cs);
2221 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2222 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002223 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002224}
2225#endif
2226
Corey Bryant208c9d12012-06-22 14:36:09 -04002227void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002228{
Anthony Liguoric227f092009-10-01 16:12:16 -05002229 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002230 int fd;
2231
Corey Bryant208c9d12012-06-22 14:36:09 -04002232 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002233 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002234 error_set(errp, QERR_FD_NOT_SUPPLIED);
2235 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002236 }
2237
2238 if (qemu_isdigit(fdname[0])) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002239 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2240 "a name not starting with a digit");
2241 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002242 }
2243
Corey Bryant208c9d12012-06-22 14:36:09 -04002244 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002245 if (strcmp(monfd->name, fdname) != 0) {
2246 continue;
2247 }
2248
2249 close(monfd->fd);
2250 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002251 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002252 }
2253
Anthony Liguori7267c092011-08-20 22:09:37 -05002254 monfd = g_malloc0(sizeof(mon_fd_t));
2255 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002256 monfd->fd = fd;
2257
Corey Bryant208c9d12012-06-22 14:36:09 -04002258 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002259}
2260
Corey Bryant208c9d12012-06-22 14:36:09 -04002261void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002262{
Anthony Liguoric227f092009-10-01 16:12:16 -05002263 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002264
Corey Bryant208c9d12012-06-22 14:36:09 -04002265 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002266 if (strcmp(monfd->name, fdname) != 0) {
2267 continue;
2268 }
2269
Blue Swirl72cf2d42009-09-12 07:36:22 +00002270 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002271 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002272 g_free(monfd->name);
2273 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002274 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002275 }
2276
Corey Bryant208c9d12012-06-22 14:36:09 -04002277 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002278}
2279
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002280static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002281{
Luiz Capitulino13548692011-07-29 15:36:43 -03002282 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002283 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002284
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002285 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002286
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002287 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002288 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002289 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002290}
2291
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002292int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002293{
Anthony Liguoric227f092009-10-01 16:12:16 -05002294 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002295
Blue Swirl72cf2d42009-09-12 07:36:22 +00002296 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002297 int fd;
2298
2299 if (strcmp(monfd->name, fdname) != 0) {
2300 continue;
2301 }
2302
2303 fd = monfd->fd;
2304
2305 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002306 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002307 g_free(monfd->name);
2308 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002309
2310 return fd;
2311 }
2312
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002313 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002314 return -1;
2315}
2316
Corey Bryantba1c0482012-08-14 16:43:43 -04002317static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2318{
2319 MonFdsetFd *mon_fdset_fd;
2320 MonFdsetFd *mon_fdset_fd_next;
2321
2322 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002323 if ((mon_fdset_fd->removed ||
2324 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2325 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002326 close(mon_fdset_fd->fd);
2327 g_free(mon_fdset_fd->opaque);
2328 QLIST_REMOVE(mon_fdset_fd, next);
2329 g_free(mon_fdset_fd);
2330 }
2331 }
2332
Corey Bryantadb696f2012-08-14 16:43:47 -04002333 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002334 QLIST_REMOVE(mon_fdset, next);
2335 g_free(mon_fdset);
2336 }
2337}
2338
Corey Bryantefb87c12012-08-14 16:43:48 -04002339static void monitor_fdsets_cleanup(void)
2340{
2341 MonFdset *mon_fdset;
2342 MonFdset *mon_fdset_next;
2343
2344 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2345 monitor_fdset_cleanup(mon_fdset);
2346 }
2347}
2348
Corey Bryantba1c0482012-08-14 16:43:43 -04002349AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2350 const char *opaque, Error **errp)
2351{
2352 int fd;
2353 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002354 AddfdInfo *fdinfo;
2355
2356 fd = qemu_chr_fe_get_msgfd(mon->chr);
2357 if (fd == -1) {
2358 error_set(errp, QERR_FD_NOT_SUPPLIED);
2359 goto error;
2360 }
2361
Corey Bryante446f702012-10-18 15:19:32 -04002362 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2363 has_opaque, opaque, errp);
2364 if (fdinfo) {
2365 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002366 }
2367
Corey Bryantba1c0482012-08-14 16:43:43 -04002368error:
2369 if (fd != -1) {
2370 close(fd);
2371 }
2372 return NULL;
2373}
2374
2375void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2376{
2377 MonFdset *mon_fdset;
2378 MonFdsetFd *mon_fdset_fd;
2379 char fd_str[60];
2380
2381 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2382 if (mon_fdset->id != fdset_id) {
2383 continue;
2384 }
2385 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2386 if (has_fd) {
2387 if (mon_fdset_fd->fd != fd) {
2388 continue;
2389 }
2390 mon_fdset_fd->removed = true;
2391 break;
2392 } else {
2393 mon_fdset_fd->removed = true;
2394 }
2395 }
2396 if (has_fd && !mon_fdset_fd) {
2397 goto error;
2398 }
2399 monitor_fdset_cleanup(mon_fdset);
2400 return;
2401 }
2402
2403error:
2404 if (has_fd) {
2405 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2406 fdset_id, fd);
2407 } else {
2408 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2409 }
2410 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2411}
2412
2413FdsetInfoList *qmp_query_fdsets(Error **errp)
2414{
2415 MonFdset *mon_fdset;
2416 MonFdsetFd *mon_fdset_fd;
2417 FdsetInfoList *fdset_list = NULL;
2418
2419 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2420 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2421 FdsetFdInfoList *fdsetfd_list = NULL;
2422
2423 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2424 fdset_info->value->fdset_id = mon_fdset->id;
2425
2426 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2427 FdsetFdInfoList *fdsetfd_info;
2428
2429 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2430 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2431 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2432 if (mon_fdset_fd->opaque) {
2433 fdsetfd_info->value->has_opaque = true;
2434 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2435 } else {
2436 fdsetfd_info->value->has_opaque = false;
2437 }
2438
2439 fdsetfd_info->next = fdsetfd_list;
2440 fdsetfd_list = fdsetfd_info;
2441 }
2442
2443 fdset_info->value->fds = fdsetfd_list;
2444
2445 fdset_info->next = fdset_list;
2446 fdset_list = fdset_info;
2447 }
2448
2449 return fdset_list;
2450}
2451
Corey Bryante446f702012-10-18 15:19:32 -04002452AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2453 bool has_opaque, const char *opaque,
2454 Error **errp)
2455{
2456 MonFdset *mon_fdset = NULL;
2457 MonFdsetFd *mon_fdset_fd;
2458 AddfdInfo *fdinfo;
2459
2460 if (has_fdset_id) {
2461 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2462 /* Break if match found or match impossible due to ordering by ID */
2463 if (fdset_id <= mon_fdset->id) {
2464 if (fdset_id < mon_fdset->id) {
2465 mon_fdset = NULL;
2466 }
2467 break;
2468 }
2469 }
2470 }
2471
2472 if (mon_fdset == NULL) {
2473 int64_t fdset_id_prev = -1;
2474 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2475
2476 if (has_fdset_id) {
2477 if (fdset_id < 0) {
2478 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2479 "a non-negative value");
2480 return NULL;
2481 }
2482 /* Use specified fdset ID */
2483 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2484 mon_fdset_cur = mon_fdset;
2485 if (fdset_id < mon_fdset_cur->id) {
2486 break;
2487 }
2488 }
2489 } else {
2490 /* Use first available fdset ID */
2491 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2492 mon_fdset_cur = mon_fdset;
2493 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2494 fdset_id_prev = mon_fdset_cur->id;
2495 continue;
2496 }
2497 break;
2498 }
2499 }
2500
2501 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2502 if (has_fdset_id) {
2503 mon_fdset->id = fdset_id;
2504 } else {
2505 mon_fdset->id = fdset_id_prev + 1;
2506 }
2507
2508 /* The fdset list is ordered by fdset ID */
2509 if (!mon_fdset_cur) {
2510 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2511 } else if (mon_fdset->id < mon_fdset_cur->id) {
2512 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2513 } else {
2514 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2515 }
2516 }
2517
2518 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2519 mon_fdset_fd->fd = fd;
2520 mon_fdset_fd->removed = false;
2521 if (has_opaque) {
2522 mon_fdset_fd->opaque = g_strdup(opaque);
2523 }
2524 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2525
2526 fdinfo = g_malloc0(sizeof(*fdinfo));
2527 fdinfo->fdset_id = mon_fdset->id;
2528 fdinfo->fd = mon_fdset_fd->fd;
2529
2530 return fdinfo;
2531}
2532
Corey Bryantadb696f2012-08-14 16:43:47 -04002533int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2534{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002535#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002536 MonFdset *mon_fdset;
2537 MonFdsetFd *mon_fdset_fd;
2538 int mon_fd_flags;
2539
Corey Bryantadb696f2012-08-14 16:43:47 -04002540 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2541 if (mon_fdset->id != fdset_id) {
2542 continue;
2543 }
2544 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2545 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2546 if (mon_fd_flags == -1) {
2547 return -1;
2548 }
2549
2550 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2551 return mon_fdset_fd->fd;
2552 }
2553 }
2554 errno = EACCES;
2555 return -1;
2556 }
2557#endif
2558
2559 errno = ENOENT;
2560 return -1;
2561}
2562
2563int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2564{
2565 MonFdset *mon_fdset;
2566 MonFdsetFd *mon_fdset_fd_dup;
2567
2568 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2569 if (mon_fdset->id != fdset_id) {
2570 continue;
2571 }
2572 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2573 if (mon_fdset_fd_dup->fd == dup_fd) {
2574 return -1;
2575 }
2576 }
2577 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2578 mon_fdset_fd_dup->fd = dup_fd;
2579 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2580 return 0;
2581 }
2582 return -1;
2583}
2584
2585static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2586{
2587 MonFdset *mon_fdset;
2588 MonFdsetFd *mon_fdset_fd_dup;
2589
2590 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2591 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2592 if (mon_fdset_fd_dup->fd == dup_fd) {
2593 if (remove) {
2594 QLIST_REMOVE(mon_fdset_fd_dup, next);
2595 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2596 monitor_fdset_cleanup(mon_fdset);
2597 }
2598 }
2599 return mon_fdset->id;
2600 }
2601 }
2602 }
2603 return -1;
2604}
2605
2606int monitor_fdset_dup_fd_find(int dup_fd)
2607{
2608 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2609}
2610
2611int monitor_fdset_dup_fd_remove(int dup_fd)
2612{
2613 return monitor_fdset_dup_fd_find_remove(dup_fd, true);
2614}
2615
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002616int monitor_handle_fd_param(Monitor *mon, const char *fdname)
2617{
2618 int fd;
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002619 Error *local_err = NULL;
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002620
2621 if (!qemu_isdigit(fdname[0]) && mon) {
2622
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002623 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002624 if (fd == -1) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002625 qerror_report_err(local_err);
2626 error_free(local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002627 return -1;
2628 }
2629 } else {
2630 fd = qemu_parse_fd(fdname);
2631 }
2632
2633 return fd;
2634}
2635
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002636/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002637static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002638 {
2639 .name = "version",
2640 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002641 .params = "",
2642 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002643 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002644 },
2645 {
2646 .name = "network",
2647 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002648 .params = "",
2649 .help = "show the network state",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002650 .mhandler.cmd = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002651 },
2652 {
2653 .name = "chardev",
2654 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002655 .params = "",
2656 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002657 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002658 },
2659 {
2660 .name = "block",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002661 .args_type = "verbose:-v,device:B?",
2662 .params = "[-v] [device]",
2663 .help = "show info of one block device or all block devices "
2664 "(and details of images with -v option)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002665 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002666 },
2667 {
2668 .name = "blockstats",
2669 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002670 .params = "",
2671 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002672 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002673 },
2674 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002675 .name = "block-jobs",
2676 .args_type = "",
2677 .params = "",
2678 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002679 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002680 },
2681 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002682 .name = "registers",
2683 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002684 .params = "",
2685 .help = "show the cpu registers",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002686 .mhandler.cmd = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002687 },
2688 {
2689 .name = "cpus",
2690 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002691 .params = "",
2692 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002693 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002694 },
2695 {
2696 .name = "history",
2697 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002698 .params = "",
2699 .help = "show the command line history",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002700 .mhandler.cmd = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002701 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002702#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2703 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002704 {
2705 .name = "irq",
2706 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002707 .params = "",
2708 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002709#ifdef TARGET_SPARC
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002710 .mhandler.cmd = sun4m_irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002711#elif defined(TARGET_LM32)
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002712 .mhandler.cmd = lm32_irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002713#else
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002714 .mhandler.cmd = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002715#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002716 },
2717 {
2718 .name = "pic",
2719 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002720 .params = "",
2721 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002722#ifdef TARGET_SPARC
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002723 .mhandler.cmd = sun4m_pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002724#elif defined(TARGET_LM32)
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002725 .mhandler.cmd = lm32_do_pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002726#else
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002727 .mhandler.cmd = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002728#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002729 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002730#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002731 {
2732 .name = "pci",
2733 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002734 .params = "",
2735 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002736 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002737 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002738#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002739 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002740 {
2741 .name = "tlb",
2742 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002743 .params = "",
2744 .help = "show virtual to physical memory mappings",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002745 .mhandler.cmd = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002746 },
aurel327c664e22009-03-03 06:12:22 +00002747#endif
2748#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002749 {
2750 .name = "mem",
2751 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002752 .params = "",
2753 .help = "show the active virtual memory mappings",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002754 .mhandler.cmd = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002755 },
bellardb86bda52004-09-18 19:32:46 +00002756#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002757 {
Blue Swirl314e2982011-09-11 20:22:05 +00002758 .name = "mtree",
2759 .args_type = "",
2760 .params = "",
2761 .help = "show memory tree",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002762 .mhandler.cmd = do_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002763 },
2764 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002765 .name = "jit",
2766 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 .params = "",
2768 .help = "show dynamic compiler info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002769 .mhandler.cmd = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002770 },
2771 {
2772 .name = "kvm",
2773 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002774 .params = "",
2775 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002776 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002777 },
2778 {
2779 .name = "numa",
2780 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002781 .params = "",
2782 .help = "show NUMA information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002783 .mhandler.cmd = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002784 },
2785 {
2786 .name = "usb",
2787 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002788 .params = "",
2789 .help = "show guest USB devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002790 .mhandler.cmd = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002791 },
2792 {
2793 .name = "usbhost",
2794 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002795 .params = "",
2796 .help = "show host USB devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002797 .mhandler.cmd = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002798 },
2799 {
2800 .name = "profile",
2801 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002802 .params = "",
2803 .help = "show profiling information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002804 .mhandler.cmd = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002805 },
2806 {
2807 .name = "capture",
2808 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002809 .params = "",
2810 .help = "show capture information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002811 .mhandler.cmd = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002812 },
2813 {
2814 .name = "snapshots",
2815 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002816 .params = "",
2817 .help = "show the currently saved VM snapshots",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002818 .mhandler.cmd = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002819 },
2820 {
2821 .name = "status",
2822 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002823 .params = "",
2824 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002825 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002826 },
2827 {
2828 .name = "pcmcia",
2829 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002830 .params = "",
2831 .help = "show guest PCMCIA status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002832 .mhandler.cmd = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002833 },
2834 {
2835 .name = "mice",
2836 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002837 .params = "",
2838 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002839 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002840 },
2841 {
2842 .name = "vnc",
2843 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002844 .params = "",
2845 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002846 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002847 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002848#if defined(CONFIG_SPICE)
2849 {
2850 .name = "spice",
2851 .args_type = "",
2852 .params = "",
2853 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002854 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002855 },
2856#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002857 {
2858 .name = "name",
2859 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002860 .params = "",
2861 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002862 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002863 },
2864 {
2865 .name = "uuid",
2866 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002867 .params = "",
2868 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002869 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002870 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002871 {
2872 .name = "cpustats",
2873 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002874 .params = "",
2875 .help = "show CPU statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002876 .mhandler.cmd = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002877 },
blueswir131a60e22007-10-26 18:42:59 +00002878#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002879 {
2880 .name = "usernet",
2881 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002882 .params = "",
2883 .help = "show user network stack connection states",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002884 .mhandler.cmd = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002885 },
blueswir131a60e22007-10-26 18:42:59 +00002886#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002887 {
2888 .name = "migrate",
2889 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002890 .params = "",
2891 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002892 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002893 },
2894 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002895 .name = "migrate_capabilities",
2896 .args_type = "",
2897 .params = "",
2898 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002899 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002900 },
2901 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002902 .name = "migrate_cache_size",
2903 .args_type = "",
2904 .params = "",
2905 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002906 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002907 },
2908 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002909 .name = "balloon",
2910 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002911 .params = "",
2912 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002913 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002914 },
2915 {
2916 .name = "qtree",
2917 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002918 .params = "",
2919 .help = "show device tree",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002920 .mhandler.cmd = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002921 },
2922 {
2923 .name = "qdm",
2924 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002925 .params = "",
2926 .help = "show qdev device model list",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002927 .mhandler.cmd = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002928 },
2929 {
2930 .name = "roms",
2931 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002932 .params = "",
2933 .help = "show roms",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002934 .mhandler.cmd = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002935 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302936 {
2937 .name = "trace-events",
2938 .args_type = "",
2939 .params = "",
2940 .help = "show available trace-events & their state",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002941 .mhandler.cmd = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302942 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002943 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002944 .name = "tpm",
2945 .args_type = "",
2946 .params = "",
2947 .help = "show the TPM device",
2948 .mhandler.cmd = hmp_info_tpm,
2949 },
2950 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002951 .name = NULL,
2952 },
bellard9dc39cb2004-03-14 21:38:27 +00002953};
2954
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002955/* mon_cmds and info_cmds would be sorted at runtime */
2956static mon_cmd_t mon_cmds[] = {
2957#include "hmp-commands.h"
2958 { NULL, NULL, },
2959};
2960
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002961static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002962#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002963 { /* NULL */ },
2964};
2965
bellard9307c4c2004-04-04 12:57:25 +00002966/*******************************************************************/
2967
2968static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002969static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002970
bellard92a31b12005-02-10 22:00:52 +00002971#define MD_TLONG 0
2972#define MD_I32 1
2973
bellard9307c4c2004-04-04 12:57:25 +00002974typedef struct MonitorDef {
2975 const char *name;
2976 int offset;
blueswir18662d652008-10-02 18:32:44 +00002977 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002978 int type;
bellard9307c4c2004-04-04 12:57:25 +00002979} MonitorDef;
2980
bellard57206fd2004-04-25 18:54:52 +00002981#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002982static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002983{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002984 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002985 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002986}
2987#endif
2988
bellarda541f292004-04-12 20:39:29 +00002989#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002990static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002991{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002992 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002993 unsigned int u;
2994 int i;
2995
2996 u = 0;
2997 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002998 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002999
3000 return u;
3001}
3002
blueswir18662d652008-10-02 18:32:44 +00003003static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003004{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003005 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003006 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003007}
3008
blueswir18662d652008-10-02 18:32:44 +00003009static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003010{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003011 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003012 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003013}
bellard9fddaa02004-05-21 12:59:32 +00003014
blueswir18662d652008-10-02 18:32:44 +00003015static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003016{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003017 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003018 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003019}
3020
blueswir18662d652008-10-02 18:32:44 +00003021static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003022{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003023 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003024 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003025}
3026
blueswir18662d652008-10-02 18:32:44 +00003027static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003028{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003029 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003030 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003031}
bellarda541f292004-04-12 20:39:29 +00003032#endif
3033
bellarde95c8d52004-09-30 22:22:08 +00003034#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003035#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003036static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003037{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003038 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003039
3040 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003041}
bellard7b936c02005-10-30 17:05:13 +00003042#endif
bellarde95c8d52004-09-30 22:22:08 +00003043
blueswir18662d652008-10-02 18:32:44 +00003044static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003045{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003046 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003047 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003048}
3049#endif
3050
blueswir18662d652008-10-02 18:32:44 +00003051static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003052#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003053
3054#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01003055 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
3056 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
3057 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003058
Andreas Färbere59d1672012-02-16 00:40:47 +01003059 { "eax", offsetof(CPUX86State, regs[0]) },
3060 { "ecx", offsetof(CPUX86State, regs[1]) },
3061 { "edx", offsetof(CPUX86State, regs[2]) },
3062 { "ebx", offsetof(CPUX86State, regs[3]) },
3063 { "esp|sp", offsetof(CPUX86State, regs[4]) },
3064 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
3065 { "esi", offsetof(CPUX86State, regs[6]) },
3066 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003067#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01003068 { "r8", offsetof(CPUX86State, regs[8]) },
3069 { "r9", offsetof(CPUX86State, regs[9]) },
3070 { "r10", offsetof(CPUX86State, regs[10]) },
3071 { "r11", offsetof(CPUX86State, regs[11]) },
3072 { "r12", offsetof(CPUX86State, regs[12]) },
3073 { "r13", offsetof(CPUX86State, regs[13]) },
3074 { "r14", offsetof(CPUX86State, regs[14]) },
3075 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00003076#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003077 { "eflags", offsetof(CPUX86State, eflags) },
3078 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00003079 SEG("cs", R_CS)
3080 SEG("ds", R_DS)
3081 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003082 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003083 SEG("fs", R_FS)
3084 SEG("gs", R_GS)
3085 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003086#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003087 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003088 { "r0", offsetof(CPUPPCState, gpr[0]) },
3089 { "r1", offsetof(CPUPPCState, gpr[1]) },
3090 { "r2", offsetof(CPUPPCState, gpr[2]) },
3091 { "r3", offsetof(CPUPPCState, gpr[3]) },
3092 { "r4", offsetof(CPUPPCState, gpr[4]) },
3093 { "r5", offsetof(CPUPPCState, gpr[5]) },
3094 { "r6", offsetof(CPUPPCState, gpr[6]) },
3095 { "r7", offsetof(CPUPPCState, gpr[7]) },
3096 { "r8", offsetof(CPUPPCState, gpr[8]) },
3097 { "r9", offsetof(CPUPPCState, gpr[9]) },
3098 { "r10", offsetof(CPUPPCState, gpr[10]) },
3099 { "r11", offsetof(CPUPPCState, gpr[11]) },
3100 { "r12", offsetof(CPUPPCState, gpr[12]) },
3101 { "r13", offsetof(CPUPPCState, gpr[13]) },
3102 { "r14", offsetof(CPUPPCState, gpr[14]) },
3103 { "r15", offsetof(CPUPPCState, gpr[15]) },
3104 { "r16", offsetof(CPUPPCState, gpr[16]) },
3105 { "r17", offsetof(CPUPPCState, gpr[17]) },
3106 { "r18", offsetof(CPUPPCState, gpr[18]) },
3107 { "r19", offsetof(CPUPPCState, gpr[19]) },
3108 { "r20", offsetof(CPUPPCState, gpr[20]) },
3109 { "r21", offsetof(CPUPPCState, gpr[21]) },
3110 { "r22", offsetof(CPUPPCState, gpr[22]) },
3111 { "r23", offsetof(CPUPPCState, gpr[23]) },
3112 { "r24", offsetof(CPUPPCState, gpr[24]) },
3113 { "r25", offsetof(CPUPPCState, gpr[25]) },
3114 { "r26", offsetof(CPUPPCState, gpr[26]) },
3115 { "r27", offsetof(CPUPPCState, gpr[27]) },
3116 { "r28", offsetof(CPUPPCState, gpr[28]) },
3117 { "r29", offsetof(CPUPPCState, gpr[29]) },
3118 { "r30", offsetof(CPUPPCState, gpr[30]) },
3119 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003120 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003121 { "f0", offsetof(CPUPPCState, fpr[0]) },
3122 { "f1", offsetof(CPUPPCState, fpr[1]) },
3123 { "f2", offsetof(CPUPPCState, fpr[2]) },
3124 { "f3", offsetof(CPUPPCState, fpr[3]) },
3125 { "f4", offsetof(CPUPPCState, fpr[4]) },
3126 { "f5", offsetof(CPUPPCState, fpr[5]) },
3127 { "f6", offsetof(CPUPPCState, fpr[6]) },
3128 { "f7", offsetof(CPUPPCState, fpr[7]) },
3129 { "f8", offsetof(CPUPPCState, fpr[8]) },
3130 { "f9", offsetof(CPUPPCState, fpr[9]) },
3131 { "f10", offsetof(CPUPPCState, fpr[10]) },
3132 { "f11", offsetof(CPUPPCState, fpr[11]) },
3133 { "f12", offsetof(CPUPPCState, fpr[12]) },
3134 { "f13", offsetof(CPUPPCState, fpr[13]) },
3135 { "f14", offsetof(CPUPPCState, fpr[14]) },
3136 { "f15", offsetof(CPUPPCState, fpr[15]) },
3137 { "f16", offsetof(CPUPPCState, fpr[16]) },
3138 { "f17", offsetof(CPUPPCState, fpr[17]) },
3139 { "f18", offsetof(CPUPPCState, fpr[18]) },
3140 { "f19", offsetof(CPUPPCState, fpr[19]) },
3141 { "f20", offsetof(CPUPPCState, fpr[20]) },
3142 { "f21", offsetof(CPUPPCState, fpr[21]) },
3143 { "f22", offsetof(CPUPPCState, fpr[22]) },
3144 { "f23", offsetof(CPUPPCState, fpr[23]) },
3145 { "f24", offsetof(CPUPPCState, fpr[24]) },
3146 { "f25", offsetof(CPUPPCState, fpr[25]) },
3147 { "f26", offsetof(CPUPPCState, fpr[26]) },
3148 { "f27", offsetof(CPUPPCState, fpr[27]) },
3149 { "f28", offsetof(CPUPPCState, fpr[28]) },
3150 { "f29", offsetof(CPUPPCState, fpr[29]) },
3151 { "f30", offsetof(CPUPPCState, fpr[30]) },
3152 { "f31", offsetof(CPUPPCState, fpr[31]) },
3153 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003154 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003155 { "nip|pc", offsetof(CPUPPCState, nip) },
3156 { "lr", offsetof(CPUPPCState, lr) },
3157 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003158 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003159 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003160 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003161 { "msr", 0, &monitor_get_msr, },
3162 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003163 { "tbu", 0, &monitor_get_tbu, },
3164 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003165 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003166 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3167 { "sr0", offsetof(CPUPPCState, sr[0]) },
3168 { "sr1", offsetof(CPUPPCState, sr[1]) },
3169 { "sr2", offsetof(CPUPPCState, sr[2]) },
3170 { "sr3", offsetof(CPUPPCState, sr[3]) },
3171 { "sr4", offsetof(CPUPPCState, sr[4]) },
3172 { "sr5", offsetof(CPUPPCState, sr[5]) },
3173 { "sr6", offsetof(CPUPPCState, sr[6]) },
3174 { "sr7", offsetof(CPUPPCState, sr[7]) },
3175 { "sr8", offsetof(CPUPPCState, sr[8]) },
3176 { "sr9", offsetof(CPUPPCState, sr[9]) },
3177 { "sr10", offsetof(CPUPPCState, sr[10]) },
3178 { "sr11", offsetof(CPUPPCState, sr[11]) },
3179 { "sr12", offsetof(CPUPPCState, sr[12]) },
3180 { "sr13", offsetof(CPUPPCState, sr[13]) },
3181 { "sr14", offsetof(CPUPPCState, sr[14]) },
3182 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003183 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003184 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003185
Andreas Färbere59d1672012-02-16 00:40:47 +01003186 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3187 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
3188 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3189 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3190 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3191 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3192 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3193 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3194 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3195 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3196 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3197 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3198 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3199 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3200 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3201 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3202 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3203 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3204 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3205 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3206 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3207 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3208 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3209 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3210 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3211 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3212 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3213 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3214 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3215 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3216 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3217 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3218 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3219 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3220 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3221 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3222 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3223 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3224 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3225 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3226 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3227 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3228 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3229 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3230 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3231 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3232 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3233 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3234 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3235 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3236 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3237 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3238 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3239 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3240 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3241 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3242 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3243 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3244 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3245 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3246 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3247 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3248 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3249 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3250 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3251 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003252
bellarde95c8d52004-09-30 22:22:08 +00003253#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003254 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3255 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3256 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3257 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3258 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3259 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3260 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3261 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003262 { "o0", 0, monitor_get_reg },
3263 { "o1", 1, monitor_get_reg },
3264 { "o2", 2, monitor_get_reg },
3265 { "o3", 3, monitor_get_reg },
3266 { "o4", 4, monitor_get_reg },
3267 { "o5", 5, monitor_get_reg },
3268 { "o6", 6, monitor_get_reg },
3269 { "o7", 7, monitor_get_reg },
3270 { "l0", 8, monitor_get_reg },
3271 { "l1", 9, monitor_get_reg },
3272 { "l2", 10, monitor_get_reg },
3273 { "l3", 11, monitor_get_reg },
3274 { "l4", 12, monitor_get_reg },
3275 { "l5", 13, monitor_get_reg },
3276 { "l6", 14, monitor_get_reg },
3277 { "l7", 15, monitor_get_reg },
3278 { "i0", 16, monitor_get_reg },
3279 { "i1", 17, monitor_get_reg },
3280 { "i2", 18, monitor_get_reg },
3281 { "i3", 19, monitor_get_reg },
3282 { "i4", 20, monitor_get_reg },
3283 { "i5", 21, monitor_get_reg },
3284 { "i6", 22, monitor_get_reg },
3285 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003286 { "pc", offsetof(CPUSPARCState, pc) },
3287 { "npc", offsetof(CPUSPARCState, npc) },
3288 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003289#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003290 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003291 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003292#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003293 { "tbr", offsetof(CPUSPARCState, tbr) },
3294 { "fsr", offsetof(CPUSPARCState, fsr) },
3295 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3296 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3297 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3298 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3299 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3300 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3301 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3302 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3303 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3304 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3305 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3306 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3307 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3308 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3309 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3310 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3311 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3312 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3313 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3314 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3315 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3316 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3317 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3318 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3319 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3320 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3321 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3322 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3323 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3324 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3325 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3326 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003327#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003328 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3329 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3330 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3331 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3332 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3333 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3334 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3335 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3336 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3337 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3338 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3339 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3340 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3341 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3342 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3343 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3344 { "asi", offsetof(CPUSPARCState, asi) },
3345 { "pstate", offsetof(CPUSPARCState, pstate) },
3346 { "cansave", offsetof(CPUSPARCState, cansave) },
3347 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3348 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3349 { "wstate", offsetof(CPUSPARCState, wstate) },
3350 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3351 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003352#endif
bellard9307c4c2004-04-04 12:57:25 +00003353#endif
3354 { NULL },
3355};
3356
Stefan Weil9c3175c2013-08-22 21:30:09 +02003357static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3358expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003359{
Fam Zheng277acfe2013-08-20 10:58:21 +08003360 va_list ap;
3361 va_start(ap, fmt);
3362 monitor_vprintf(mon, fmt, ap);
3363 monitor_printf(mon, "\n");
3364 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003365 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003366}
3367
Markus Armbruster09b94182010-01-20 13:07:30 +01003368/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003369static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003370{
blueswir18662d652008-10-02 18:32:44 +00003371 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003372 void *ptr;
3373
bellard9307c4c2004-04-04 12:57:25 +00003374 for(md = monitor_defs; md->name != NULL; md++) {
3375 if (compare_cmd(name, md->name)) {
3376 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003377 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003378 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003379 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003380 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003381 switch(md->type) {
3382 case MD_I32:
3383 *pval = *(int32_t *)ptr;
3384 break;
3385 case MD_TLONG:
3386 *pval = *(target_long *)ptr;
3387 break;
3388 default:
3389 *pval = 0;
3390 break;
3391 }
bellard9307c4c2004-04-04 12:57:25 +00003392 }
3393 return 0;
3394 }
3395 }
3396 return -1;
3397}
3398
3399static void next(void)
3400{
Blue Swirl660f11b2009-07-31 21:16:51 +00003401 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003402 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003403 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003404 pch++;
3405 }
3406}
3407
aliguori376253e2009-03-05 23:01:23 +00003408static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003409
aliguori376253e2009-03-05 23:01:23 +00003410static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003411{
blueswir1c2efc952007-09-25 17:28:42 +00003412 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003413 char *p;
bellard6a00d602005-11-21 23:25:50 +00003414 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003415
3416 switch(*pch) {
3417 case '+':
3418 next();
aliguori376253e2009-03-05 23:01:23 +00003419 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003420 break;
3421 case '-':
3422 next();
aliguori376253e2009-03-05 23:01:23 +00003423 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003424 break;
3425 case '~':
3426 next();
aliguori376253e2009-03-05 23:01:23 +00003427 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003428 break;
3429 case '(':
3430 next();
aliguori376253e2009-03-05 23:01:23 +00003431 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003432 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003433 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003434 }
3435 next();
3436 break;
bellard81d09122004-07-14 17:21:37 +00003437 case '\'':
3438 pch++;
3439 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003440 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003441 n = *pch;
3442 pch++;
3443 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003444 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003445 next();
3446 break;
bellard9307c4c2004-04-04 12:57:25 +00003447 case '$':
3448 {
3449 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003450 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003451
bellard9307c4c2004-04-04 12:57:25 +00003452 pch++;
3453 q = buf;
3454 while ((*pch >= 'a' && *pch <= 'z') ||
3455 (*pch >= 'A' && *pch <= 'Z') ||
3456 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003457 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003458 if ((q - buf) < sizeof(buf) - 1)
3459 *q++ = *pch;
3460 pch++;
3461 }
blueswir1cd390082008-11-16 13:53:32 +00003462 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003463 pch++;
3464 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003465 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003466 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003467 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003468 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003469 }
3470 break;
3471 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003472 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003473 n = 0;
3474 break;
3475 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003476 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003477 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003478 if (errno == ERANGE) {
3479 expr_error(mon, "number too large");
3480 }
bellard9307c4c2004-04-04 12:57:25 +00003481 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003482 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003483 }
3484 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003485 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003486 pch++;
3487 break;
3488 }
3489 return n;
3490}
3491
3492
aliguori376253e2009-03-05 23:01:23 +00003493static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003494{
blueswir1c2efc952007-09-25 17:28:42 +00003495 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003496 int op;
ths3b46e622007-09-17 08:09:54 +00003497
aliguori376253e2009-03-05 23:01:23 +00003498 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003499 for(;;) {
3500 op = *pch;
3501 if (op != '*' && op != '/' && op != '%')
3502 break;
3503 next();
aliguori376253e2009-03-05 23:01:23 +00003504 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003505 switch(op) {
3506 default:
3507 case '*':
3508 val *= val2;
3509 break;
3510 case '/':
3511 case '%':
ths5fafdf22007-09-16 21:08:06 +00003512 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003513 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003514 if (op == '/')
3515 val /= val2;
3516 else
3517 val %= val2;
3518 break;
3519 }
3520 }
3521 return val;
3522}
3523
aliguori376253e2009-03-05 23:01:23 +00003524static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003525{
blueswir1c2efc952007-09-25 17:28:42 +00003526 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003527 int op;
bellard9307c4c2004-04-04 12:57:25 +00003528
aliguori376253e2009-03-05 23:01:23 +00003529 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003530 for(;;) {
3531 op = *pch;
3532 if (op != '&' && op != '|' && op != '^')
3533 break;
3534 next();
aliguori376253e2009-03-05 23:01:23 +00003535 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003536 switch(op) {
3537 default:
3538 case '&':
3539 val &= val2;
3540 break;
3541 case '|':
3542 val |= val2;
3543 break;
3544 case '^':
3545 val ^= val2;
3546 break;
3547 }
3548 }
3549 return val;
3550}
3551
aliguori376253e2009-03-05 23:01:23 +00003552static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003553{
blueswir1c2efc952007-09-25 17:28:42 +00003554 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003555 int op;
bellard9307c4c2004-04-04 12:57:25 +00003556
aliguori376253e2009-03-05 23:01:23 +00003557 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003558 for(;;) {
3559 op = *pch;
3560 if (op != '+' && op != '-')
3561 break;
3562 next();
aliguori376253e2009-03-05 23:01:23 +00003563 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003564 if (op == '+')
3565 val += val2;
3566 else
3567 val -= val2;
3568 }
3569 return val;
3570}
3571
aliguori376253e2009-03-05 23:01:23 +00003572static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003573{
3574 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003575 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003576 *pp = pch;
3577 return -1;
3578 }
blueswir1cd390082008-11-16 13:53:32 +00003579 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003580 pch++;
aliguori376253e2009-03-05 23:01:23 +00003581 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003582 *pp = pch;
3583 return 0;
3584}
3585
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003586static int get_double(Monitor *mon, double *pval, const char **pp)
3587{
3588 const char *p = *pp;
3589 char *tailp;
3590 double d;
3591
3592 d = strtod(p, &tailp);
3593 if (tailp == p) {
3594 monitor_printf(mon, "Number expected\n");
3595 return -1;
3596 }
3597 if (d != d || d - d != 0) {
3598 /* NaN or infinity */
3599 monitor_printf(mon, "Bad number\n");
3600 return -1;
3601 }
3602 *pval = d;
3603 *pp = tailp;
3604 return 0;
3605}
3606
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003607/*
3608 * Store the command-name in cmdname, and return a pointer to
3609 * the remaining of the command string.
3610 */
3611static const char *get_command_name(const char *cmdline,
3612 char *cmdname, size_t nlen)
3613{
3614 size_t len;
3615 const char *p, *pstart;
3616
3617 p = cmdline;
3618 while (qemu_isspace(*p))
3619 p++;
3620 if (*p == '\0')
3621 return NULL;
3622 pstart = p;
3623 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3624 p++;
3625 len = p - pstart;
3626 if (len > nlen - 1)
3627 len = nlen - 1;
3628 memcpy(cmdname, pstart, len);
3629 cmdname[len] = '\0';
3630 return p;
3631}
3632
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003633/**
3634 * Read key of 'type' into 'key' and return the current
3635 * 'type' pointer.
3636 */
3637static char *key_get_info(const char *type, char **key)
3638{
3639 size_t len;
3640 char *p, *str;
3641
3642 if (*type == ',')
3643 type++;
3644
3645 p = strchr(type, ':');
3646 if (!p) {
3647 *key = NULL;
3648 return NULL;
3649 }
3650 len = p - type;
3651
Anthony Liguori7267c092011-08-20 22:09:37 -05003652 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003653 memcpy(str, type, len);
3654 str[len] = '\0';
3655
3656 *key = str;
3657 return ++p;
3658}
3659
bellard9307c4c2004-04-04 12:57:25 +00003660static int default_fmt_format = 'x';
3661static int default_fmt_size = 4;
3662
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003663static int is_valid_option(const char *c, const char *typestr)
3664{
3665 char option[3];
3666
3667 option[0] = '-';
3668 option[1] = *c;
3669 option[2] = '\0';
3670
3671 typestr = strstr(typestr, option);
3672 return (typestr != NULL);
3673}
3674
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003675static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3676 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003677{
3678 const mon_cmd_t *cmd;
3679
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003680 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003681 if (compare_cmd(cmdname, cmd->name)) {
3682 return cmd;
3683 }
3684 }
3685
3686 return NULL;
3687}
3688
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003689static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3690{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003691 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003692}
3693
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003694/*
3695 * Parse @cmdline according to command table @table.
3696 * If @cmdline is blank, return NULL.
3697 * If it can't be parsed, report to @mon, and return NULL.
3698 * Else, insert command arguments into @qdict, and return the command.
Peter Maydell085d8132013-03-18 17:20:07 +00003699 * If a sub-command table exists, and if @cmdline contains an additional string
3700 * for a sub-command, this function will try to search the sub-command table.
3701 * If no additional string for a sub-command is present, this function will
3702 * return the command found in @table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003703 * Do not assume the returned command points into @table! It doesn't
3704 * when the command is a sub-command.
3705 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003706static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003707 const char *cmdline,
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003708 int start,
3709 mon_cmd_t *table,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003710 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003711{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003712 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003713 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003714 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003715 char cmdname[256];
3716 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003717 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003718
3719#ifdef DEBUG
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003720 monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start);
bellard9dc39cb2004-03-14 21:38:27 +00003721#endif
ths3b46e622007-09-17 08:09:54 +00003722
bellard9307c4c2004-04-04 12:57:25 +00003723 /* extract the command name */
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003724 p = get_command_name(cmdline + start, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003725 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003726 return NULL;
ths3b46e622007-09-17 08:09:54 +00003727
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003728 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003729 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003730 monitor_printf(mon, "unknown command: '%.*s'\n",
3731 (int)(p - cmdline), cmdline);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003732 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003733 }
bellard9307c4c2004-04-04 12:57:25 +00003734
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003735 /* filter out following useless space */
3736 while (qemu_isspace(*p)) {
3737 p++;
3738 }
3739 /* search sub command */
3740 if (cmd->sub_table != NULL) {
3741 /* check if user set additional command */
3742 if (*p == '\0') {
3743 return cmd;
3744 }
3745 return monitor_parse_command(mon, cmdline, p - cmdline,
3746 cmd->sub_table, qdict);
3747 }
3748
bellard9307c4c2004-04-04 12:57:25 +00003749 /* parse the parameters */
3750 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003751 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003752 typestr = key_get_info(typestr, &key);
3753 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003754 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003755 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003756 typestr++;
3757 switch(c) {
3758 case 'F':
bellard81d09122004-07-14 17:21:37 +00003759 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003760 case 's':
3761 {
3762 int ret;
ths3b46e622007-09-17 08:09:54 +00003763
blueswir1cd390082008-11-16 13:53:32 +00003764 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003765 p++;
3766 if (*typestr == '?') {
3767 typestr++;
3768 if (*p == '\0') {
3769 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003770 break;
bellard9307c4c2004-04-04 12:57:25 +00003771 }
3772 }
3773 ret = get_str(buf, sizeof(buf), &p);
3774 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003775 switch(c) {
3776 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003777 monitor_printf(mon, "%s: filename expected\n",
3778 cmdname);
bellard81d09122004-07-14 17:21:37 +00003779 break;
3780 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003781 monitor_printf(mon, "%s: block device name expected\n",
3782 cmdname);
bellard81d09122004-07-14 17:21:37 +00003783 break;
3784 default:
aliguori376253e2009-03-05 23:01:23 +00003785 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003786 break;
3787 }
bellard9307c4c2004-04-04 12:57:25 +00003788 goto fail;
3789 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003790 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003791 }
3792 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003793 case 'O':
3794 {
3795 QemuOptsList *opts_list;
3796 QemuOpts *opts;
3797
3798 opts_list = qemu_find_opts(key);
3799 if (!opts_list || opts_list->desc->name) {
3800 goto bad_type;
3801 }
3802 while (qemu_isspace(*p)) {
3803 p++;
3804 }
3805 if (!*p)
3806 break;
3807 if (get_str(buf, sizeof(buf), &p) < 0) {
3808 goto fail;
3809 }
3810 opts = qemu_opts_parse(opts_list, buf, 1);
3811 if (!opts) {
3812 goto fail;
3813 }
3814 qemu_opts_to_qdict(opts, qdict);
3815 qemu_opts_del(opts);
3816 }
3817 break;
bellard9307c4c2004-04-04 12:57:25 +00003818 case '/':
3819 {
3820 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003821
blueswir1cd390082008-11-16 13:53:32 +00003822 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003823 p++;
3824 if (*p == '/') {
3825 /* format found */
3826 p++;
3827 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003828 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003829 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003830 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003831 count = count * 10 + (*p - '0');
3832 p++;
3833 }
3834 }
3835 size = -1;
3836 format = -1;
3837 for(;;) {
3838 switch(*p) {
3839 case 'o':
3840 case 'd':
3841 case 'u':
3842 case 'x':
3843 case 'i':
3844 case 'c':
3845 format = *p++;
3846 break;
3847 case 'b':
3848 size = 1;
3849 p++;
3850 break;
3851 case 'h':
3852 size = 2;
3853 p++;
3854 break;
3855 case 'w':
3856 size = 4;
3857 p++;
3858 break;
3859 case 'g':
3860 case 'L':
3861 size = 8;
3862 p++;
3863 break;
3864 default:
3865 goto next;
3866 }
3867 }
3868 next:
blueswir1cd390082008-11-16 13:53:32 +00003869 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003870 monitor_printf(mon, "invalid char in format: '%c'\n",
3871 *p);
bellard9307c4c2004-04-04 12:57:25 +00003872 goto fail;
3873 }
bellard9307c4c2004-04-04 12:57:25 +00003874 if (format < 0)
3875 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003876 if (format != 'i') {
3877 /* for 'i', not specifying a size gives -1 as size */
3878 if (size < 0)
3879 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003880 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003881 }
bellard9307c4c2004-04-04 12:57:25 +00003882 default_fmt_format = format;
3883 } else {
3884 count = 1;
3885 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003886 if (format != 'i') {
3887 size = default_fmt_size;
3888 } else {
3889 size = -1;
3890 }
bellard9307c4c2004-04-04 12:57:25 +00003891 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003892 qdict_put(qdict, "count", qint_from_int(count));
3893 qdict_put(qdict, "format", qint_from_int(format));
3894 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003895 }
3896 break;
3897 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003898 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003899 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003900 {
blueswir1c2efc952007-09-25 17:28:42 +00003901 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003902
blueswir1cd390082008-11-16 13:53:32 +00003903 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003904 p++;
bellard34405572004-06-08 00:55:58 +00003905 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003906 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003907 if (*p == '\0') {
3908 typestr++;
3909 break;
3910 }
bellard34405572004-06-08 00:55:58 +00003911 } else {
3912 if (*p == '.') {
3913 p++;
blueswir1cd390082008-11-16 13:53:32 +00003914 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003915 p++;
bellard34405572004-06-08 00:55:58 +00003916 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003917 typestr++;
3918 break;
bellard34405572004-06-08 00:55:58 +00003919 }
3920 }
bellard13224a82006-07-14 22:03:35 +00003921 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003922 }
aliguori376253e2009-03-05 23:01:23 +00003923 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003924 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003925 /* Check if 'i' is greater than 32-bit */
3926 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3927 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3928 monitor_printf(mon, "integer is for 32-bit values\n");
3929 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003930 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003931 if (val < 0) {
3932 monitor_printf(mon, "enter a positive value\n");
3933 goto fail;
3934 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003935 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003936 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003937 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003938 }
3939 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003940 case 'o':
3941 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003942 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003943 char *end;
3944
3945 while (qemu_isspace(*p)) {
3946 p++;
3947 }
3948 if (*typestr == '?') {
3949 typestr++;
3950 if (*p == '\0') {
3951 break;
3952 }
3953 }
3954 val = strtosz(p, &end);
3955 if (val < 0) {
3956 monitor_printf(mon, "invalid size\n");
3957 goto fail;
3958 }
3959 qdict_put(qdict, key, qint_from_int(val));
3960 p = end;
3961 }
3962 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003963 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003964 {
3965 double val;
3966
3967 while (qemu_isspace(*p))
3968 p++;
3969 if (*typestr == '?') {
3970 typestr++;
3971 if (*p == '\0') {
3972 break;
3973 }
3974 }
3975 if (get_double(mon, &val, &p) < 0) {
3976 goto fail;
3977 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003978 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003979 switch (*p) {
3980 case 'm':
3981 val /= 1e3; p += 2; break;
3982 case 'u':
3983 val /= 1e6; p += 2; break;
3984 case 'n':
3985 val /= 1e9; p += 2; break;
3986 }
3987 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003988 if (*p && !qemu_isspace(*p)) {
3989 monitor_printf(mon, "Unknown unit suffix\n");
3990 goto fail;
3991 }
3992 qdict_put(qdict, key, qfloat_from_double(val));
3993 }
3994 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003995 case 'b':
3996 {
3997 const char *beg;
3998 int val;
3999
4000 while (qemu_isspace(*p)) {
4001 p++;
4002 }
4003 beg = p;
4004 while (qemu_isgraph(*p)) {
4005 p++;
4006 }
4007 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4008 val = 1;
4009 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4010 val = 0;
4011 } else {
4012 monitor_printf(mon, "Expected 'on' or 'off'\n");
4013 goto fail;
4014 }
4015 qdict_put(qdict, key, qbool_from_int(val));
4016 }
4017 break;
bellard9307c4c2004-04-04 12:57:25 +00004018 case '-':
4019 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004020 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004021 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004022 /* option */
ths3b46e622007-09-17 08:09:54 +00004023
bellard9307c4c2004-04-04 12:57:25 +00004024 c = *typestr++;
4025 if (c == '\0')
4026 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004027 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004028 p++;
bellard9307c4c2004-04-04 12:57:25 +00004029 if (*p == '-') {
4030 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004031 if(c != *p) {
4032 if(!is_valid_option(p, typestr)) {
4033
4034 monitor_printf(mon, "%s: unsupported option -%c\n",
4035 cmdname, *p);
4036 goto fail;
4037 } else {
4038 skip_key = 1;
4039 }
bellard9307c4c2004-04-04 12:57:25 +00004040 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004041 if(skip_key) {
4042 p = tmp;
4043 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004044 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004045 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004046 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004047 }
bellard9307c4c2004-04-04 12:57:25 +00004048 }
bellard9307c4c2004-04-04 12:57:25 +00004049 }
4050 break;
Wenchao Xia129be002013-08-27 20:38:26 +08004051 case 'S':
4052 {
4053 /* package all remaining string */
4054 int len;
4055
4056 while (qemu_isspace(*p)) {
4057 p++;
4058 }
4059 if (*typestr == '?') {
4060 typestr++;
4061 if (*p == '\0') {
4062 /* no remaining string: NULL argument */
4063 break;
4064 }
4065 }
4066 len = strlen(p);
4067 if (len <= 0) {
4068 monitor_printf(mon, "%s: string expected\n",
4069 cmdname);
4070 break;
4071 }
4072 qdict_put(qdict, key, qstring_from_str(p));
4073 p += len;
4074 }
4075 break;
bellard9307c4c2004-04-04 12:57:25 +00004076 default:
4077 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004078 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004079 goto fail;
4080 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004081 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004082 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004083 }
4084 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004085 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004086 p++;
4087 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004088 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4089 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004090 goto fail;
4091 }
4092
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004093 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004094
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004095fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004096 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004097 return NULL;
4098}
4099
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004100void monitor_set_error(Monitor *mon, QError *qerror)
4101{
4102 /* report only the first error */
4103 if (!mon->error) {
4104 mon->error = qerror;
4105 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004106 QDECREF(qerror);
4107 }
4108}
4109
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004110static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4111{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004112 if (ret && !monitor_has_error(mon)) {
4113 /*
4114 * If it returns failure, it must have passed on error.
4115 *
4116 * Action: Report an internal error to the client if in QMP.
4117 */
4118 qerror_report(QERR_UNDEFINED_ERROR);
Luiz Capitulino6d441432010-11-22 16:35:09 -02004119 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004120}
4121
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004122static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004123{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004124 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004125 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004126
4127 qdict = qdict_new();
4128
Wenchao Xia77172392013-08-27 20:38:20 +08004129 cmd = monitor_parse_command(mon, cmdline, 0, mon->cmd_table, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004130 if (!cmd)
4131 goto out;
4132
Luiz Capitulino4903de02010-09-16 11:01:32 -03004133 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004134 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004135 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004136 QObject *data = NULL;
4137
4138 /* XXX: ignores the error code */
4139 cmd->mhandler.cmd_new(mon, qdict, &data);
4140 assert(!monitor_has_error(mon));
4141 if (data) {
4142 cmd->user_print(mon, data);
4143 qobject_decref(data);
4144 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004145 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004146 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004147 }
4148
Luiz Capitulino13917be2009-10-07 13:41:54 -03004149out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004150 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004151}
4152
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004153static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004154{
4155 const char *p, *pstart;
4156 char cmd[128];
4157 int len;
4158
4159 p = list;
4160 for(;;) {
4161 pstart = p;
4162 p = strchr(p, '|');
4163 if (!p)
4164 p = pstart + strlen(pstart);
4165 len = p - pstart;
4166 if (len > sizeof(cmd) - 2)
4167 len = sizeof(cmd) - 2;
4168 memcpy(cmd, pstart, len);
4169 cmd[len] = '\0';
4170 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004171 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004172 }
4173 if (*p == '\0')
4174 break;
4175 p++;
4176 }
4177}
4178
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004179static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004180{
4181 DIR *ffs;
4182 struct dirent *d;
4183 char path[1024];
4184 char file[1024], file_prefix[1024];
4185 int input_path_len;
4186 const char *p;
4187
ths5fafdf22007-09-16 21:08:06 +00004188 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004189 if (!p) {
4190 input_path_len = 0;
4191 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004192 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004193 } else {
4194 input_path_len = p - input + 1;
4195 memcpy(path, input, input_path_len);
4196 if (input_path_len > sizeof(path) - 1)
4197 input_path_len = sizeof(path) - 1;
4198 path[input_path_len] = '\0';
4199 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4200 }
4201#ifdef DEBUG_COMPLETION
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004202 monitor_printf(mon, "input='%s' path='%s' prefix='%s'\n",
aliguori376253e2009-03-05 23:01:23 +00004203 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004204#endif
4205 ffs = opendir(path);
4206 if (!ffs)
4207 return;
4208 for(;;) {
4209 struct stat sb;
4210 d = readdir(ffs);
4211 if (!d)
4212 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004213
4214 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4215 continue;
4216 }
4217
bellard81d09122004-07-14 17:21:37 +00004218 if (strstart(d->d_name, file_prefix, NULL)) {
4219 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004220 if (input_path_len < sizeof(file))
4221 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4222 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004223 /* stat the file to find out if it's a directory.
4224 * In that case add a slash to speed up typing long paths
4225 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004226 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004227 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004228 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004229 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004230 }
4231 }
4232 closedir(ffs);
4233}
4234
Wenchao Xia599a9262013-08-27 20:38:15 +08004235typedef struct MonitorBlockComplete {
4236 Monitor *mon;
4237 const char *input;
4238} MonitorBlockComplete;
4239
aliguori51de9762009-03-05 23:00:43 +00004240static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004241{
aliguori51de9762009-03-05 23:00:43 +00004242 const char *name = bdrv_get_device_name(bs);
Wenchao Xia599a9262013-08-27 20:38:15 +08004243 MonitorBlockComplete *mbc = opaque;
4244 Monitor *mon = mbc->mon;
4245 const char *input = mbc->input;
bellard81d09122004-07-14 17:21:37 +00004246
4247 if (input[0] == '\0' ||
4248 !strncmp(name, (char *)input, strlen(input))) {
Wenchao Xia599a9262013-08-27 20:38:15 +08004249 readline_add_completion(mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004250 }
4251}
4252
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004253static const char *next_arg_type(const char *typestr)
4254{
4255 const char *p = strchr(typestr, ':');
4256 return (p != NULL ? ++p : typestr);
4257}
4258
Wenchao Xiac35b6402013-08-27 20:38:24 +08004259static void monitor_find_completion_by_table(Monitor *mon,
4260 const mon_cmd_t *cmd_table,
4261 char **args,
4262 int nb_args)
bellard81d09122004-07-14 17:21:37 +00004263{
4264 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004265 int i;
bellard81d09122004-07-14 17:21:37 +00004266 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004267 const mon_cmd_t *cmd;
Wenchao Xia599a9262013-08-27 20:38:15 +08004268 MonitorBlockComplete mbs;
bellard81d09122004-07-14 17:21:37 +00004269
bellard81d09122004-07-14 17:21:37 +00004270 if (nb_args <= 1) {
4271 /* command completion */
4272 if (nb_args == 0)
4273 cmdname = "";
4274 else
4275 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004276 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004277 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004278 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00004279 }
4280 } else {
4281 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004282 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004283 if (compare_cmd(args[0], cmd->name)) {
4284 break;
4285 }
bellard81d09122004-07-14 17:21:37 +00004286 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004287 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004288 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004289 }
4290
Wenchao Xiad903a772013-08-27 20:38:25 +08004291 if (cmd->sub_table) {
4292 /* do the job again */
4293 return monitor_find_completion_by_table(mon, cmd->sub_table,
4294 &args[1], nb_args - 1);
4295 }
4296
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004297 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004298 for(i = 0; i < nb_args - 2; i++) {
4299 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004300 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004301 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004302 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004303 }
4304 }
4305 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004306 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004307 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004308 }
bellard81d09122004-07-14 17:21:37 +00004309 switch(*ptype) {
4310 case 'F':
4311 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004312 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004313 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004314 break;
4315 case 'B':
4316 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004317 mbs.mon = mon;
4318 mbs.input = str;
4319 readline_set_completion_index(mon->rs, strlen(str));
4320 bdrv_iterate(block_completion_it, &mbs);
bellard81d09122004-07-14 17:21:37 +00004321 break;
bellard7fe48482004-10-09 18:08:01 +00004322 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004323 case 'S':
Wenchao Xiad903a772013-08-27 20:38:25 +08004324 if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004325 char *sep = strrchr(str, '-');
4326 if (sep)
4327 str = sep + 1;
Wenchao Xiad2674b22013-08-27 20:38:16 +08004328 readline_set_completion_index(mon->rs, strlen(str));
Amos Kong1048c882012-08-31 10:56:25 +08004329 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004330 cmd_completion(mon, str, QKeyCode_lookup[i]);
bellard64866c32006-05-07 18:03:31 +00004331 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004332 } else if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004333 monitor_find_completion_by_table(mon, cmd_table,
4334 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004335 }
4336 break;
bellard81d09122004-07-14 17:21:37 +00004337 default:
4338 break;
4339 }
4340 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004341}
4342
4343static void monitor_find_completion(Monitor *mon,
4344 const char *cmdline)
4345{
4346 char *args[MAX_ARGS];
4347 int nb_args, len;
4348
4349 /* 1. parse the cmdline */
4350 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4351 return;
4352 }
4353#ifdef DEBUG_COMPLETION
4354 for (i = 0; i < nb_args; i++) {
4355 monitor_printf(mon, "arg%d = '%s'\n", i, args[i]);
4356 }
4357#endif
4358
4359 /* if the line ends with a space, it means we want to complete the
4360 next arg */
4361 len = strlen(cmdline);
4362 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4363 if (nb_args >= MAX_ARGS) {
4364 goto cleanup;
4365 }
4366 args[nb_args++] = g_strdup("");
4367 }
4368
4369 /* 2. auto complete according to args */
4370 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004371
4372cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004373 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004374}
4375
aliguori731b0362009-03-05 23:01:42 +00004376static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004377{
aliguori731b0362009-03-05 23:01:42 +00004378 Monitor *mon = opaque;
4379
Jan Kiszkac62313b2009-12-04 14:05:29 +01004380 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004381}
4382
Luiz Capitulino09069b12010-02-04 18:10:06 -02004383static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4384{
4385 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4386 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4387}
4388
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004389/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004390 * Argument validation rules:
4391 *
4392 * 1. The argument must exist in cmd_args qdict
4393 * 2. The argument type must be the expected one
4394 *
4395 * Special case: If the argument doesn't exist in cmd_args and
4396 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4397 * checking is skipped for it.
4398 */
4399static int check_client_args_type(const QDict *client_args,
4400 const QDict *cmd_args, int flags)
4401{
4402 const QDictEntry *ent;
4403
4404 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4405 QObject *obj;
4406 QString *arg_type;
4407 const QObject *client_arg = qdict_entry_value(ent);
4408 const char *client_arg_name = qdict_entry_key(ent);
4409
4410 obj = qdict_get(cmd_args, client_arg_name);
4411 if (!obj) {
4412 if (flags & QMP_ACCEPT_UNKNOWNS) {
4413 /* handler accepts unknowns */
4414 continue;
4415 }
4416 /* client arg doesn't exist */
4417 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4418 return -1;
4419 }
4420
4421 arg_type = qobject_to_qstring(obj);
4422 assert(arg_type != NULL);
4423
4424 /* check if argument's type is correct */
4425 switch (qstring_get_str(arg_type)[0]) {
4426 case 'F':
4427 case 'B':
4428 case 's':
4429 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4430 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4431 "string");
4432 return -1;
4433 }
4434 break;
4435 case 'i':
4436 case 'l':
4437 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004438 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004439 if (qobject_type(client_arg) != QTYPE_QINT) {
4440 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4441 "int");
4442 return -1;
4443 }
4444 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004445 case 'T':
4446 if (qobject_type(client_arg) != QTYPE_QINT &&
4447 qobject_type(client_arg) != QTYPE_QFLOAT) {
4448 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4449 "number");
4450 return -1;
4451 }
4452 break;
4453 case 'b':
4454 case '-':
4455 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4456 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4457 "bool");
4458 return -1;
4459 }
4460 break;
4461 case 'O':
4462 assert(flags & QMP_ACCEPT_UNKNOWNS);
4463 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004464 case 'q':
4465 /* Any QObject can be passed. */
4466 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004467 case '/':
4468 case '.':
4469 /*
4470 * These types are not supported by QMP and thus are not
4471 * handled here. Fall through.
4472 */
4473 default:
4474 abort();
4475 }
4476 }
4477
4478 return 0;
4479}
4480
4481/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004482 * - Check if the client has passed all mandatory args
4483 * - Set special flags for argument validation
4484 */
4485static int check_mandatory_args(const QDict *cmd_args,
4486 const QDict *client_args, int *flags)
4487{
4488 const QDictEntry *ent;
4489
4490 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4491 const char *cmd_arg_name = qdict_entry_key(ent);
4492 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4493 assert(type != NULL);
4494
4495 if (qstring_get_str(type)[0] == 'O') {
4496 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4497 *flags |= QMP_ACCEPT_UNKNOWNS;
4498 } else if (qstring_get_str(type)[0] != '-' &&
4499 qstring_get_str(type)[1] != '?' &&
4500 !qdict_haskey(client_args, cmd_arg_name)) {
4501 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4502 return -1;
4503 }
4504 }
4505
4506 return 0;
4507}
4508
4509static QDict *qdict_from_args_type(const char *args_type)
4510{
4511 int i;
4512 QDict *qdict;
4513 QString *key, *type, *cur_qs;
4514
4515 assert(args_type != NULL);
4516
4517 qdict = qdict_new();
4518
4519 if (args_type == NULL || args_type[0] == '\0') {
4520 /* no args, empty qdict */
4521 goto out;
4522 }
4523
4524 key = qstring_new();
4525 type = qstring_new();
4526
4527 cur_qs = key;
4528
4529 for (i = 0;; i++) {
4530 switch (args_type[i]) {
4531 case ',':
4532 case '\0':
4533 qdict_put(qdict, qstring_get_str(key), type);
4534 QDECREF(key);
4535 if (args_type[i] == '\0') {
4536 goto out;
4537 }
4538 type = qstring_new(); /* qdict has ref */
4539 cur_qs = key = qstring_new();
4540 break;
4541 case ':':
4542 cur_qs = type;
4543 break;
4544 default:
4545 qstring_append_chr(cur_qs, args_type[i]);
4546 break;
4547 }
4548 }
4549
4550out:
4551 return qdict;
4552}
4553
4554/*
4555 * Client argument checking rules:
4556 *
4557 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004558 * 2. Each argument provided by the client must be expected
4559 * 3. Each argument provided by the client must have the type expected
4560 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004561 */
4562static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4563{
4564 int flags, err;
4565 QDict *cmd_args;
4566
4567 cmd_args = qdict_from_args_type(cmd->args_type);
4568
4569 flags = 0;
4570 err = check_mandatory_args(cmd_args, client_args, &flags);
4571 if (err) {
4572 goto out;
4573 }
4574
Luiz Capitulino4af91932010-06-22 11:44:05 -03004575 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004576
4577out:
4578 QDECREF(cmd_args);
4579 return err;
4580}
4581
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004582/*
4583 * Input object checking rules
4584 *
4585 * 1. Input object must be a dict
4586 * 2. The "execute" key must exist
4587 * 3. The "execute" key must be a string
4588 * 4. If the "arguments" key exists, it must be a dict
4589 * 5. If the "id" key exists, it can be anything (ie. json-value)
4590 * 6. Any argument not listed above is considered invalid
4591 */
4592static QDict *qmp_check_input_obj(QObject *input_obj)
4593{
4594 const QDictEntry *ent;
4595 int has_exec_key = 0;
4596 QDict *input_dict;
4597
4598 if (qobject_type(input_obj) != QTYPE_QDICT) {
4599 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4600 return NULL;
4601 }
4602
4603 input_dict = qobject_to_qdict(input_obj);
4604
4605 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4606 const char *arg_name = qdict_entry_key(ent);
4607 const QObject *arg_obj = qdict_entry_value(ent);
4608
4609 if (!strcmp(arg_name, "execute")) {
4610 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4611 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4612 "string");
4613 return NULL;
4614 }
4615 has_exec_key = 1;
4616 } else if (!strcmp(arg_name, "arguments")) {
4617 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4618 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4619 "object");
4620 return NULL;
4621 }
4622 } else if (!strcmp(arg_name, "id")) {
4623 /* FIXME: check duplicated IDs for async commands */
4624 } else {
4625 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4626 return NULL;
4627 }
4628 }
4629
4630 if (!has_exec_key) {
4631 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4632 return NULL;
4633 }
4634
4635 return input_dict;
4636}
4637
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004638static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4639 const QDict *params)
4640{
4641 int ret;
4642 QObject *data = NULL;
4643
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004644 ret = cmd->mhandler.cmd_new(mon, params, &data);
4645 handler_audit(mon, cmd, ret);
4646 monitor_protocol_emitter(mon, data);
4647 qobject_decref(data);
4648}
4649
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004650static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4651{
4652 int err;
4653 QObject *obj;
4654 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004655 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004656 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004657 Monitor *mon = cur_mon;
4658
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004659 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004660
4661 obj = json_parser_parse(tokens, NULL);
4662 if (!obj) {
4663 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004664 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004665 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004666 }
4667
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004668 input = qmp_check_input_obj(obj);
4669 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004670 qobject_decref(obj);
4671 goto err_out;
4672 }
4673
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004674 mon->mc->id = qdict_get(input, "id");
4675 qobject_incref(mon->mc->id);
4676
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004677 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004678 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004679 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004680 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004681 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004682 }
4683
Anthony Liguorie3193602011-09-02 12:34:47 -05004684 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004685 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004686 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004687 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004688 }
4689
4690 obj = qdict_get(input, "arguments");
4691 if (!obj) {
4692 args = qdict_new();
4693 } else {
4694 args = qobject_to_qdict(obj);
4695 QINCREF(args);
4696 }
4697
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004698 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004699 if (err < 0) {
4700 goto err_out;
4701 }
4702
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004703 if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004704 err = qmp_async_cmd_handler(mon, cmd, args);
4705 if (err) {
4706 /* emit the error response */
4707 goto err_out;
4708 }
Adam Litke940cc302010-01-25 12:18:44 -06004709 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004710 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004711 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004712
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004713 goto out;
4714
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004715err_out:
4716 monitor_protocol_emitter(mon, NULL);
4717out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004718 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004719 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004720}
4721
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004722/**
4723 * monitor_control_read(): Read and handle QMP input
4724 */
4725static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4726{
4727 Monitor *old_mon = cur_mon;
4728
4729 cur_mon = opaque;
4730
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004731 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004732
4733 cur_mon = old_mon;
4734}
4735
aliguori731b0362009-03-05 23:01:42 +00004736static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004737{
aliguori731b0362009-03-05 23:01:42 +00004738 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004739 int i;
aliguori376253e2009-03-05 23:01:23 +00004740
aliguori731b0362009-03-05 23:01:42 +00004741 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004742
aliguoricde76ee2009-03-05 23:01:51 +00004743 if (cur_mon->rs) {
4744 for (i = 0; i < size; i++)
4745 readline_handle_byte(cur_mon->rs, buf[i]);
4746 } else {
4747 if (size == 0 || buf[size - 1] != 0)
4748 monitor_printf(cur_mon, "corrupted command\n");
4749 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004750 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004751 }
aliguori731b0362009-03-05 23:01:42 +00004752
4753 cur_mon = old_mon;
4754}
aliguorid8f44602008-10-06 13:52:44 +00004755
aliguori376253e2009-03-05 23:01:23 +00004756static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004757{
aliguori731b0362009-03-05 23:01:42 +00004758 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004759 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004760 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004761}
4762
aliguoricde76ee2009-03-05 23:01:51 +00004763int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004764{
aliguoricde76ee2009-03-05 23:01:51 +00004765 if (!mon->rs)
4766 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004767 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004768 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004769}
4770
aliguori376253e2009-03-05 23:01:23 +00004771void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004772{
aliguoricde76ee2009-03-05 23:01:51 +00004773 if (!mon->rs)
4774 return;
aliguori731b0362009-03-05 23:01:42 +00004775 if (--mon->suspend_cnt == 0)
4776 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004777}
4778
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004779static QObject *get_qmp_greeting(void)
4780{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004781 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004782
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004783 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004784 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4785}
4786
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004787/**
4788 * monitor_control_event(): Print QMP gretting
4789 */
4790static void monitor_control_event(void *opaque, int event)
4791{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004792 QObject *data;
4793 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004794
Luiz Capitulino47116d12010-02-08 17:01:30 -02004795 switch (event) {
4796 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004797 mon->mc->command_mode = 0;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004798 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004799 monitor_json_emitter(mon, data);
4800 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004801 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004802 break;
4803 case CHR_EVENT_CLOSED:
4804 json_message_parser_destroy(&mon->mc->parser);
Anthony Liguori58617a72012-08-23 08:03:21 -05004805 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004806 mon_refcount--;
4807 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004808 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004809 }
4810}
4811
aliguori731b0362009-03-05 23:01:42 +00004812static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004813{
aliguori376253e2009-03-05 23:01:23 +00004814 Monitor *mon = opaque;
4815
aliguori2724b182009-03-05 23:01:47 +00004816 switch (event) {
4817 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004818 mon->mux_out = 0;
4819 if (mon->reset_seen) {
4820 readline_restart(mon->rs);
4821 monitor_resume(mon);
4822 monitor_flush(mon);
4823 } else {
4824 mon->suspend_cnt = 0;
4825 }
aliguori2724b182009-03-05 23:01:47 +00004826 break;
ths86e94de2007-01-05 22:01:59 +00004827
aliguori2724b182009-03-05 23:01:47 +00004828 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004829 if (mon->reset_seen) {
4830 if (mon->suspend_cnt == 0) {
4831 monitor_printf(mon, "\n");
4832 }
4833 monitor_flush(mon);
4834 monitor_suspend(mon);
4835 } else {
4836 mon->suspend_cnt++;
4837 }
4838 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004839 break;
4840
Amit Shahb6b8df52009-10-07 18:31:16 +05304841 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004842 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4843 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004844 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004845 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004846 }
4847 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004848 mon_refcount++;
4849 break;
4850
4851 case CHR_EVENT_CLOSED:
4852 mon_refcount--;
4853 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004854 break;
4855 }
ths86e94de2007-01-05 22:01:59 +00004856}
4857
Wayne Xia816f8922011-10-12 11:32:41 +08004858static int
4859compare_mon_cmd(const void *a, const void *b)
4860{
4861 return strcmp(((const mon_cmd_t *)a)->name,
4862 ((const mon_cmd_t *)b)->name);
4863}
4864
4865static void sortcmdlist(void)
4866{
4867 int array_num;
4868 int elem_size = sizeof(mon_cmd_t);
4869
4870 array_num = sizeof(mon_cmds)/elem_size-1;
4871 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4872
4873 array_num = sizeof(info_cmds)/elem_size-1;
4874 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4875}
4876
aliguori76655d62009-03-06 20:27:37 +00004877
4878/*
4879 * Local variables:
4880 * c-indent-level: 4
4881 * c-basic-offset: 4
4882 * tab-width: 8
4883 * End:
4884 */
4885
aliguori731b0362009-03-05 23:01:42 +00004886void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004887{
aliguori731b0362009-03-05 23:01:42 +00004888 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004889 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004890
4891 if (is_first_init) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +01004892 monitor_protocol_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004893 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004894 is_first_init = 0;
4895 }
aliguori87127162009-03-05 23:01:29 +00004896
Wenchao Xiab01fe892013-08-27 20:38:19 +08004897 mon = g_malloc(sizeof(*mon));
4898 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004899
aliguori87127162009-03-05 23:01:29 +00004900 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004901 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004902 if (flags & MONITOR_USE_READLINE) {
4903 mon->rs = readline_init(mon, monitor_find_completion);
4904 monitor_read_command(mon, 0);
4905 }
aliguori87127162009-03-05 23:01:29 +00004906
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004907 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004908 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004909 /* Control mode requires special handlers */
4910 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4911 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004912 qemu_chr_fe_set_echo(chr, true);
Anthony Liguori26efaca2012-08-23 13:49:02 -05004913
4914 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004915 } else {
4916 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4917 monitor_event, mon);
4918 }
aliguori87127162009-03-05 23:01:29 +00004919
Blue Swirl72cf2d42009-09-12 07:36:22 +00004920 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004921 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4922 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004923}
4924
aliguori376253e2009-03-05 23:01:23 +00004925static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004926{
aliguoribb5fc202009-03-05 23:01:15 +00004927 BlockDriverState *bs = opaque;
4928 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004929
aliguoribb5fc202009-03-05 23:01:15 +00004930 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004931 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004932 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004933 }
aliguori731b0362009-03-05 23:01:42 +00004934 if (mon->password_completion_cb)
4935 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004936
aliguori731b0362009-03-05 23:01:42 +00004937 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004938}
aliguoric0f4ce72009-03-05 23:01:01 +00004939
Anthony Liguori7060b472011-09-02 12:34:50 -05004940ReadLineState *monitor_get_rs(Monitor *mon)
4941{
4942 return mon->rs;
4943}
4944
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004945int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4946 BlockDriverCompletionFunc *completion_cb,
4947 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004948{
aliguoricde76ee2009-03-05 23:01:51 +00004949 int err;
4950
aliguoribb5fc202009-03-05 23:01:15 +00004951 if (!bdrv_key_required(bs)) {
4952 if (completion_cb)
4953 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004954 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004955 }
aliguoric0f4ce72009-03-05 23:01:01 +00004956
Luiz Capitulino94171e12009-12-07 21:37:00 +01004957 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino903a8812011-12-13 17:18:30 -02004958 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
4959 bdrv_get_encrypted_filename(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004960 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004961 }
4962
aliguori376253e2009-03-05 23:01:23 +00004963 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4964 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004965
aliguori731b0362009-03-05 23:01:42 +00004966 mon->password_completion_cb = completion_cb;
4967 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004968
aliguoricde76ee2009-03-05 23:01:51 +00004969 err = monitor_read_password(mon, bdrv_password_cb, bs);
4970
4971 if (err && completion_cb)
4972 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004973
4974 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004975}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004976
4977int monitor_read_block_device_key(Monitor *mon, const char *device,
4978 BlockDriverCompletionFunc *completion_cb,
4979 void *opaque)
4980{
4981 BlockDriverState *bs;
4982
4983 bs = bdrv_find(device);
4984 if (!bs) {
4985 monitor_printf(mon, "Device not found %s\n", device);
4986 return -1;
4987 }
4988
4989 return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);
4990}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004991
4992QemuOptsList qemu_mon_opts = {
4993 .name = "mon",
4994 .implied_opt_name = "chardev",
4995 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4996 .desc = {
4997 {
4998 .name = "mode",
4999 .type = QEMU_OPT_STRING,
5000 },{
5001 .name = "chardev",
5002 .type = QEMU_OPT_STRING,
5003 },{
5004 .name = "default",
5005 .type = QEMU_OPT_BOOL,
5006 },{
5007 .name = "pretty",
5008 .type = QEMU_OPT_BOOL,
5009 },
5010 { /* end of list */ }
5011 },
5012};