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