blob: 1f645e1a80b48a2bd82186049496605aec42fbc0 [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)
Markus Armbruster361127d2010-02-10 20:24:35 +010086 * 'O' option string of the form NAME=VALUE,...
87 * parsed according to QemuOptsList given by its name
88 * Example: 'device:O' uses qemu_device_opts.
89 * Restriction: only lists with empty desc are supported
90 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000091 * 'i' 32 bit integer
92 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -030093 * 'M' Non-negative target long (32 or 64 bit), in user mode the
94 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +020095 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +020096 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
97 * K, k suffix, which multiplies the value by 2^60 for suffixes E
98 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
99 * 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 +0100100 * 'T' double
101 * user mode accepts an optional ms, us, ns suffix,
102 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000103 * '/' optional gdb-like print format (like "/10x")
104 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300105 * '?' optional type (for all types, except '/')
106 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100107 * 'b' boolean
108 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300109 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000110 *
111 */
112
Adam Litke940cc302010-01-25 12:18:44 -0600113typedef struct MonitorCompletionData MonitorCompletionData;
114struct MonitorCompletionData {
115 Monitor *mon;
116 void (*user_print)(Monitor *mon, const QObject *data);
117};
118
Anthony Liguoric227f092009-10-01 16:12:16 -0500119typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000120 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000121 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000122 const char *params;
123 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300124 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300125 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300126 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200127 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600128 int (*cmd_async)(Monitor *mon, const QDict *params,
129 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300130 } mhandler;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200131 int flags;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800132 /* @sub_table is a list of 2nd level of commands. If it do not exist,
133 * mhandler should be used. If it exist, sub_table[?].mhandler should be
134 * used, and mhandler of 1st level plays the role of help function.
135 */
136 struct mon_cmd_t *sub_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500137} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000138
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100139/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500140typedef struct mon_fd_t mon_fd_t;
141struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100142 char *name;
143 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500144 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100145};
146
Corey Bryantba1c0482012-08-14 16:43:43 -0400147/* file descriptor associated with a file descriptor set */
148typedef struct MonFdsetFd MonFdsetFd;
149struct MonFdsetFd {
150 int fd;
151 bool removed;
152 char *opaque;
153 QLIST_ENTRY(MonFdsetFd) next;
154};
155
156/* file descriptor set containing fds passed via SCM_RIGHTS */
157typedef struct MonFdset MonFdset;
158struct MonFdset {
159 int64_t id;
160 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400161 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400162 QLIST_ENTRY(MonFdset) next;
163};
164
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200165typedef struct MonitorControl {
166 QObject *id;
167 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200168 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200169} MonitorControl;
170
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100171/*
172 * To prevent flooding clients, events can be throttled. The
173 * throttling is calculated globally, rather than per-Monitor
174 * instance.
175 */
176typedef struct MonitorEventState {
177 MonitorEvent event; /* Event being tracked */
178 int64_t rate; /* Period over which to throttle. 0 to disable */
179 int64_t last; /* Time at which event was last emitted */
180 QEMUTimer *timer; /* Timer for handling delayed events */
181 QObject *data; /* Event pending delayed dispatch */
182} MonitorEventState;
183
aliguori87127162009-03-05 23:01:29 +0000184struct Monitor {
185 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200186 int mux_out;
187 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000188 int flags;
189 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400190 bool skip_flush;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400191 QString *outbuf;
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200192 guint watch;
aliguori731b0362009-03-05 23:01:42 +0000193 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200194 MonitorControl *mc;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200195 CPUState *mon_cpu;
aliguori731b0362009-03-05 23:01:42 +0000196 BlockDriverCompletionFunc *password_completion_cb;
197 void *password_opaque;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200198 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500199 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000200 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000201};
202
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300203/* QMP checker flags */
204#define QMP_ACCEPT_UNKNOWNS 1
205
Blue Swirl72cf2d42009-09-12 07:36:22 +0000206static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400207static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400208static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000209
Wayne Xia816f8922011-10-12 11:32:41 +0800210static mon_cmd_t mon_cmds[];
211static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000212
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300213static const mon_cmd_t qmp_cmds[];
214
Markus Armbruster8631b602010-02-18 11:41:55 +0100215Monitor *cur_mon;
216Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000217
aliguori731b0362009-03-05 23:01:42 +0000218static void monitor_command_cb(Monitor *mon, const char *cmdline,
219 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000220
Luiz Capitulino09069b12010-02-04 18:10:06 -0200221static inline int qmp_cmd_mode(const Monitor *mon)
222{
223 return (mon->mc ? mon->mc->command_mode : 0);
224}
225
Luiz Capitulino418173c2009-11-26 22:58:51 -0200226/* Return true if in control mode, false otherwise */
227static inline int monitor_ctrl_mode(const Monitor *mon)
228{
229 return (mon->flags & MONITOR_USE_CONTROL);
230}
231
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100232/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
233int monitor_cur_is_qmp(void)
234{
235 return cur_mon && monitor_ctrl_mode(cur_mon);
236}
237
Anthony Liguori7060b472011-09-02 12:34:50 -0500238void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000239{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200240 if (!mon->rs)
241 return;
242
aliguori731b0362009-03-05 23:01:42 +0000243 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
244 if (show_prompt)
245 readline_show_prompt(mon->rs);
246}
bellard6a00d602005-11-21 23:25:50 +0000247
Anthony Liguori7060b472011-09-02 12:34:50 -0500248int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
249 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000250{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100251 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100252 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100253 return -EINVAL;
254 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000255 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
256 /* prompt is printed on return from the command handler */
257 return 0;
258 } else {
259 monitor_printf(mon, "terminal does not support password prompting\n");
260 return -ENOTTY;
261 }
aliguoribb5fc202009-03-05 23:01:15 +0000262}
263
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100264static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
265 void *opaque)
266{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200267 Monitor *mon = opaque;
268
269 mon->watch = 0;
270 monitor_flush(mon);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100271 return FALSE;
272}
273
aliguori376253e2009-03-05 23:01:23 +0000274void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000275{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100276 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400277 size_t len;
278 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100279
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400280 if (mon->skip_flush) {
281 return;
282 }
283
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400284 buf = qstring_get_str(mon->outbuf);
285 len = qstring_get_length(mon->outbuf);
286
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200287 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400288 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
289 if (rc == len) {
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100290 /* all flushed */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400291 QDECREF(mon->outbuf);
292 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100293 return;
294 }
295 if (rc > 0) {
296 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400297 QString *tmp = qstring_from_str(buf + rc);
298 QDECREF(mon->outbuf);
299 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100300 }
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200301 if (mon->watch == 0) {
302 mon->watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT,
303 monitor_unblocked, mon);
304 }
bellard7e2515e2004-08-01 21:52:19 +0000305 }
306}
307
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400308/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000309static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000310{
ths60fe76f2007-12-16 03:02:09 +0000311 char c;
aliguori731b0362009-03-05 23:01:42 +0000312
bellard7e2515e2004-08-01 21:52:19 +0000313 for(;;) {
314 c = *str++;
315 if (c == '\0')
316 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400317 if (c == '\n') {
318 qstring_append_chr(mon->outbuf, '\r');
319 }
320 qstring_append_chr(mon->outbuf, c);
321 if (c == '\n') {
aliguori376253e2009-03-05 23:01:23 +0000322 monitor_flush(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400323 }
bellard7e2515e2004-08-01 21:52:19 +0000324 }
325}
326
aliguori376253e2009-03-05 23:01:23 +0000327void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000328{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400329 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200330
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200331 if (!mon)
332 return;
333
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200334 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200335 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200336 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200337
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400338 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200339 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400340 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000341}
342
aliguori376253e2009-03-05 23:01:23 +0000343void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000344{
345 va_list ap;
346 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000347 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000348 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000349}
350
aliguori376253e2009-03-05 23:01:23 +0000351void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000352{
353 int i;
354
355 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000356 switch (filename[i]) {
357 case ' ':
358 case '"':
359 case '\\':
360 monitor_printf(mon, "\\%c", filename[i]);
361 break;
362 case '\t':
363 monitor_printf(mon, "\\t");
364 break;
365 case '\r':
366 monitor_printf(mon, "\\r");
367 break;
368 case '\n':
369 monitor_printf(mon, "\\n");
370 break;
371 default:
372 monitor_printf(mon, "%c", filename[i]);
373 break;
374 }
thsfef30742006-12-22 14:11:32 +0000375 }
376}
377
Stefan Weil8b7968f2010-09-23 21:28:05 +0200378static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
379 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000380{
381 va_list ap;
382 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000383 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000384 va_end(ap);
385 return 0;
386}
387
Luiz Capitulino13c74252009-10-07 13:41:55 -0300388static void monitor_user_noop(Monitor *mon, const QObject *data) { }
389
Luiz Capitulino9e807212010-09-16 10:58:59 -0300390static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300391{
392 return cmd->user_print != NULL;
393}
394
Luiz Capitulino4903de02010-09-16 11:01:32 -0300395static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600396{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200397 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600398}
399
Luiz Capitulino8204a912009-11-18 23:05:31 -0200400static inline int monitor_has_error(const Monitor *mon)
401{
402 return mon->error != NULL;
403}
404
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200405static void monitor_json_emitter(Monitor *mon, const QObject *data)
406{
407 QString *json;
408
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200409 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
410 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200411 assert(json != NULL);
412
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200413 qstring_append_chr(json, '\n');
414 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200415
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200416 QDECREF(json);
417}
418
Luiz Capitulinode253f12012-07-27 16:18:16 -0300419static QDict *build_qmp_error_dict(const QError *err)
420{
421 QObject *obj;
422
423 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
424 ErrorClass_lookup[err->err_class],
425 qerror_human(err));
426
427 return qobject_to_qdict(obj);
428}
429
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200430static void monitor_protocol_emitter(Monitor *mon, QObject *data)
431{
432 QDict *qmp;
433
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100434 trace_monitor_protocol_emitter(mon);
435
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200436 if (!monitor_has_error(mon)) {
437 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300438 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200439 if (data) {
440 qobject_incref(data);
441 qdict_put_obj(qmp, "return", data);
442 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200443 /* return an empty QDict by default */
444 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200445 }
446 } else {
447 /* error response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300448 qmp = build_qmp_error_dict(mon->error);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200449 QDECREF(mon->error);
450 mon->error = NULL;
451 }
452
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200453 if (mon->mc->id) {
454 qdict_put_obj(qmp, "id", mon->mc->id);
455 mon->mc->id = NULL;
456 }
457
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200458 monitor_json_emitter(mon, QOBJECT(qmp));
459 QDECREF(qmp);
460}
461
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200462static void timestamp_put(QDict *qdict)
463{
464 int err;
465 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000466 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200467
Blue Swirld08d6f02009-12-04 18:06:39 +0000468 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200469 if (err < 0)
470 return;
471
472 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
473 "'microseconds': %" PRId64 " }",
474 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200475 qdict_put_obj(qdict, "timestamp", obj);
476}
477
Daniel P. Berrange48608532012-05-21 17:59:51 +0100478
479static const char *monitor_event_names[] = {
480 [QEVENT_SHUTDOWN] = "SHUTDOWN",
481 [QEVENT_RESET] = "RESET",
482 [QEVENT_POWERDOWN] = "POWERDOWN",
483 [QEVENT_STOP] = "STOP",
484 [QEVENT_RESUME] = "RESUME",
485 [QEVENT_VNC_CONNECTED] = "VNC_CONNECTED",
486 [QEVENT_VNC_INITIALIZED] = "VNC_INITIALIZED",
487 [QEVENT_VNC_DISCONNECTED] = "VNC_DISCONNECTED",
488 [QEVENT_BLOCK_IO_ERROR] = "BLOCK_IO_ERROR",
489 [QEVENT_RTC_CHANGE] = "RTC_CHANGE",
490 [QEVENT_WATCHDOG] = "WATCHDOG",
491 [QEVENT_SPICE_CONNECTED] = "SPICE_CONNECTED",
492 [QEVENT_SPICE_INITIALIZED] = "SPICE_INITIALIZED",
493 [QEVENT_SPICE_DISCONNECTED] = "SPICE_DISCONNECTED",
494 [QEVENT_BLOCK_JOB_COMPLETED] = "BLOCK_JOB_COMPLETED",
495 [QEVENT_BLOCK_JOB_CANCELLED] = "BLOCK_JOB_CANCELLED",
Paolo Bonzini32c81a42012-09-28 17:22:58 +0200496 [QEVENT_BLOCK_JOB_ERROR] = "BLOCK_JOB_ERROR",
Paolo Bonzinia66a2a32012-07-23 15:15:47 +0200497 [QEVENT_BLOCK_JOB_READY] = "BLOCK_JOB_READY",
Michael S. Tsirkin0402a5d2013-03-06 14:58:59 +0200498 [QEVENT_DEVICE_DELETED] = "DEVICE_DELETED",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100499 [QEVENT_DEVICE_TRAY_MOVED] = "DEVICE_TRAY_MOVED",
Amos Kongb1be4282013-06-14 15:45:52 +0800500 [QEVENT_NIC_RX_FILTER_CHANGED] = "NIC_RX_FILTER_CHANGED",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100501 [QEVENT_SUSPEND] = "SUSPEND",
Luiz Capitulino25df49f2012-08-08 17:03:01 -0300502 [QEVENT_SUSPEND_DISK] = "SUSPEND_DISK",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100503 [QEVENT_WAKEUP] = "WAKEUP",
Daniel P. Berrange973603a2012-06-14 18:12:56 +0100504 [QEVENT_BALLOON_CHANGE] = "BALLOON_CHANGE",
Yonit Halperin2fdd16e2012-08-21 11:51:57 +0300505 [QEVENT_SPICE_MIGRATE_COMPLETED] = "SPICE_MIGRATE_COMPLETED",
Hu Taoc401a8a2013-04-26 11:24:41 +0800506 [QEVENT_GUEST_PANICKED] = "GUEST_PANICKED",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100507};
508QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names) != QEVENT_MAX)
509
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100510MonitorEventState monitor_event_state[QEVENT_MAX];
511QemuMutex monitor_event_state_lock;
512
513/*
514 * Emits the event to every monitor instance
515 */
516static void
517monitor_protocol_event_emit(MonitorEvent event,
518 QObject *data)
519{
520 Monitor *mon;
521
522 trace_monitor_protocol_event_emit(event, data);
523 QLIST_FOREACH(mon, &mon_list, entry) {
524 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
525 monitor_json_emitter(mon, data);
526 }
527 }
528}
529
530
531/*
532 * Queue a new event for emission to Monitor instances,
533 * applying any rate limiting if required.
534 */
535static void
536monitor_protocol_event_queue(MonitorEvent event,
537 QObject *data)
538{
539 MonitorEventState *evstate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100540 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100541 assert(event < QEVENT_MAX);
542
543 qemu_mutex_lock(&monitor_event_state_lock);
544 evstate = &(monitor_event_state[event]);
545 trace_monitor_protocol_event_queue(event,
546 data,
547 evstate->rate,
548 evstate->last,
549 now);
550
551 /* Rate limit of 0 indicates no throttling */
552 if (!evstate->rate) {
553 monitor_protocol_event_emit(event, data);
554 evstate->last = now;
555 } else {
556 int64_t delta = now - evstate->last;
557 if (evstate->data ||
558 delta < evstate->rate) {
559 /* If there's an existing event pending, replace
560 * it with the new event, otherwise schedule a
561 * timer for delayed emission
562 */
563 if (evstate->data) {
564 qobject_decref(evstate->data);
565 } else {
566 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100567 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100568 }
569 evstate->data = data;
570 qobject_incref(evstate->data);
571 } else {
572 monitor_protocol_event_emit(event, data);
573 evstate->last = now;
574 }
575 }
576 qemu_mutex_unlock(&monitor_event_state_lock);
577}
578
579
580/*
581 * The callback invoked by QemuTimer when a delayed
582 * event is ready to be emitted
583 */
584static void monitor_protocol_event_handler(void *opaque)
585{
586 MonitorEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100587 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100588
589 qemu_mutex_lock(&monitor_event_state_lock);
590
591 trace_monitor_protocol_event_handler(evstate->event,
592 evstate->data,
593 evstate->last,
594 now);
595 if (evstate->data) {
596 monitor_protocol_event_emit(evstate->event, evstate->data);
597 qobject_decref(evstate->data);
598 evstate->data = NULL;
599 }
600 evstate->last = now;
601 qemu_mutex_unlock(&monitor_event_state_lock);
602}
603
604
605/*
606 * @event: the event ID to be limited
607 * @rate: the rate limit in milliseconds
608 *
609 * Sets a rate limit on a particular event, so no
610 * more than 1 event will be emitted within @rate
611 * milliseconds
612 */
613static void
614monitor_protocol_event_throttle(MonitorEvent event,
615 int64_t rate)
616{
617 MonitorEventState *evstate;
618 assert(event < QEVENT_MAX);
619
620 evstate = &(monitor_event_state[event]);
621
622 trace_monitor_protocol_event_throttle(event, rate);
623 evstate->event = event;
624 evstate->rate = rate * SCALE_MS;
Alex Blighbc72ad62013-08-21 16:03:08 +0100625 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100626 SCALE_MS,
627 monitor_protocol_event_handler,
628 evstate);
629 evstate->last = 0;
630 evstate->data = NULL;
631}
632
633
634/* Global, one-time initializer to configure the rate limiting
635 * and initialize state */
636static void monitor_protocol_event_init(void)
637{
638 qemu_mutex_init(&monitor_event_state_lock);
639 /* Limit RTC & BALLOON events to 1 per second */
640 monitor_protocol_event_throttle(QEVENT_RTC_CHANGE, 1000);
641 monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE, 1000);
642 monitor_protocol_event_throttle(QEVENT_WATCHDOG, 1000);
643}
644
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200645/**
646 * monitor_protocol_event(): Generate a Monitor event
647 *
648 * Event-specific data can be emitted through the (optional) 'data' parameter.
649 */
650void monitor_protocol_event(MonitorEvent event, QObject *data)
651{
652 QDict *qmp;
653 const char *event_name;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200654
Blue Swirl242cd002009-12-04 18:05:45 +0000655 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200656
Daniel P. Berrange48608532012-05-21 17:59:51 +0100657 event_name = monitor_event_names[event];
658 assert(event_name != NULL);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200659
660 qmp = qdict_new();
661 timestamp_put(qmp);
662 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200663 if (data) {
664 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200665 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200666 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200667
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100668 trace_monitor_protocol_event(event, event_name, qmp);
669 monitor_protocol_event_queue(event, QOBJECT(qmp));
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200670 QDECREF(qmp);
671}
672
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200673static int do_qmp_capabilities(Monitor *mon, const QDict *params,
674 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200675{
676 /* Will setup QMP capabilities in the future */
677 if (monitor_ctrl_mode(mon)) {
678 mon->mc->command_mode = 1;
679 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200680
681 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200682}
683
Luiz Capitulino0268d972010-10-22 10:08:02 -0200684static void handle_user_command(Monitor *mon, const char *cmdline);
685
Wenchao Xiab01fe892013-08-27 20:38:19 +0800686static void monitor_data_init(Monitor *mon)
687{
688 memset(mon, 0, sizeof(Monitor));
689 mon->outbuf = qstring_new();
690}
691
692static void monitor_data_destroy(Monitor *mon)
693{
694 QDECREF(mon->outbuf);
695}
696
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200697char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
698 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200699{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200700 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200701 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200702
Wenchao Xiab01fe892013-08-27 20:38:19 +0800703 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400704 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200705
706 old_mon = cur_mon;
707 cur_mon = &hmp;
708
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200709 if (has_cpu_index) {
710 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200711 if (ret < 0) {
712 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200713 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
714 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200715 goto out;
716 }
717 }
718
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200719 handle_user_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200720 cur_mon = old_mon;
721
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400722 if (qstring_get_length(hmp.outbuf) > 0) {
723 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200724 } else {
725 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200726 }
727
728out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800729 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200730 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200731}
732
bellard9dc39cb2004-03-14 21:38:27 +0000733static int compare_cmd(const char *name, const char *list)
734{
735 const char *p, *pstart;
736 int len;
737 len = strlen(name);
738 p = list;
739 for(;;) {
740 pstart = p;
741 p = strchr(p, '|');
742 if (!p)
743 p = pstart + strlen(pstart);
744 if ((p - pstart) == len && !memcmp(pstart, name, len))
745 return 1;
746 if (*p == '\0')
747 break;
748 p++;
749 }
750 return 0;
751}
752
Anthony Liguoric227f092009-10-01 16:12:16 -0500753static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000754 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000755{
Anthony Liguoric227f092009-10-01 16:12:16 -0500756 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000757
758 for(cmd = cmds; cmd->name != NULL; cmd++) {
759 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000760 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
761 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000762 }
763}
764
aliguori376253e2009-03-05 23:01:23 +0000765static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000766{
767 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000768 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000769 } else {
aliguori376253e2009-03-05 23:01:23 +0000770 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000771 if (name && !strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000772 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000773 monitor_printf(mon, "Log items (comma separated):\n");
774 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000775 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000776 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000777 }
778 }
bellard9dc39cb2004-03-14 21:38:27 +0000779 }
780}
781
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300782static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300783{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300784 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300785}
786
Lluísfc764102011-08-31 20:31:18 +0200787static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530788{
789 const char *tp_name = qdict_get_str(qdict, "name");
790 bool new_state = qdict_get_bool(qdict, "option");
Blue Swirlf871d682010-10-13 19:14:29 +0000791
Lluís Vilanovaca285c32013-03-05 14:47:43 +0100792 bool found = false;
793 TraceEvent *ev = NULL;
794 while ((ev = trace_event_pattern(tp_name, ev)) != NULL) {
795 found = true;
796 if (!trace_event_get_state_static(ev)) {
797 monitor_printf(mon, "event \"%s\" is not traceable\n", tp_name);
798 } else {
799 trace_event_set_state_dynamic(ev, new_state);
800 }
801 }
802 if (!trace_event_is_pattern(tp_name) && !found) {
Blue Swirlf871d682010-10-13 19:14:29 +0000803 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
804 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530805}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100806
Michael Rothc45a8162011-10-02 08:44:37 -0500807#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100808static void do_trace_file(Monitor *mon, const QDict *qdict)
809{
810 const char *op = qdict_get_try_str(qdict, "op");
811 const char *arg = qdict_get_try_str(qdict, "arg");
812
813 if (!op) {
814 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
815 } else if (!strcmp(op, "on")) {
816 st_set_trace_file_enabled(true);
817 } else if (!strcmp(op, "off")) {
818 st_set_trace_file_enabled(false);
819 } else if (!strcmp(op, "flush")) {
820 st_flush_trace_buffer();
821 } else if (!strcmp(op, "set")) {
822 if (arg) {
823 st_set_trace_file(arg);
824 }
825 } else {
826 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
827 help_cmd(mon, "trace-file");
828 }
829}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530830#endif
831
Adam Litke940cc302010-01-25 12:18:44 -0600832static void user_monitor_complete(void *opaque, QObject *ret_data)
833{
834 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
835
836 if (ret_data) {
837 data->user_print(data->mon, ret_data);
838 }
839 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500840 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600841}
842
843static void qmp_monitor_complete(void *opaque, QObject *ret_data)
844{
845 monitor_protocol_emitter(opaque, ret_data);
846}
847
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300848static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
849 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600850{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300851 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600852}
853
Adam Litke940cc302010-01-25 12:18:44 -0600854static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
855 const QDict *params)
856{
857 int ret;
858
Anthony Liguori7267c092011-08-20 22:09:37 -0500859 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600860 cb_data->mon = mon;
861 cb_data->user_print = cmd->user_print;
862 monitor_suspend(mon);
863 ret = cmd->mhandler.cmd_async(mon, params,
864 user_monitor_complete, cb_data);
865 if (ret < 0) {
866 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500867 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600868 }
869}
870
Wenchao Xia84c44612013-01-14 14:06:29 +0800871static void do_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000872{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300873 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000874}
875
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300876CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000877{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300878 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200879 const mon_cmd_t *cmd;
880
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300881 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200882 info = g_malloc0(sizeof(*info));
883 info->value = g_malloc0(sizeof(*info->value));
884 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200885
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300886 info->next = cmd_list;
887 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200888 }
889
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300890 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000891}
892
Daniel P. Berrange48608532012-05-21 17:59:51 +0100893EventInfoList *qmp_query_events(Error **errp)
894{
895 EventInfoList *info, *ev_list = NULL;
896 MonitorEvent e;
897
898 for (e = 0 ; e < QEVENT_MAX ; e++) {
899 const char *event_name = monitor_event_names[e];
900 assert(event_name != NULL);
901 info = g_malloc0(sizeof(*info));
902 info->value = g_malloc0(sizeof(*info->value));
903 info->value->name = g_strdup(event_name);
904
905 info->next = ev_list;
906 ev_list = info;
907 }
908
909 return ev_list;
910}
911
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300912/* set the current CPU defined by the user */
913int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000914{
Andreas Färber55e5c282012-12-17 06:18:02 +0100915 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000916
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100917 cpu = qemu_get_cpu(cpu_index);
918 if (cpu == NULL) {
919 return -1;
bellard6a00d602005-11-21 23:25:50 +0000920 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200921 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100922 return 0;
bellard6a00d602005-11-21 23:25:50 +0000923}
924
Andreas Färber9349b4f2012-03-14 01:38:32 +0100925static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000926{
aliguori731b0362009-03-05 23:01:42 +0000927 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300928 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000929 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300930 cpu_synchronize_state(cur_mon->mon_cpu);
Andreas Färbercb446ec2013-05-01 14:24:52 +0200931 return cur_mon->mon_cpu->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000932}
933
Luiz Capitulino99b77962011-10-24 10:53:44 -0200934int monitor_get_cpu_index(void)
935{
Andreas Färber55e5c282012-12-17 06:18:02 +0100936 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
937 return cpu->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200938}
939
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800940static void do_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000941{
Andreas Färber878096e2013-05-27 01:33:50 +0200942 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100943 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000944 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +0200945 cpu = ENV_GET_CPU(env);
946 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000947}
948
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800949static void do_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000950{
aliguori376253e2009-03-05 23:01:23 +0000951 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000952}
953
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800954static void do_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000955{
956 int i;
bellard7e2515e2004-08-01 21:52:19 +0000957 const char *str;
ths3b46e622007-09-17 08:09:54 +0000958
aliguoricde76ee2009-03-05 23:01:51 +0000959 if (!mon->rs)
960 return;
bellard7e2515e2004-08-01 21:52:19 +0000961 i = 0;
962 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000963 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000964 if (!str)
965 break;
aliguori376253e2009-03-05 23:01:23 +0000966 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000967 i++;
bellardaa455482004-04-04 13:07:25 +0000968 }
969}
970
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800971static void do_info_cpu_stats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +0000972{
Andreas Färber878096e2013-05-27 01:33:50 +0200973 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100974 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +0000975
976 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +0200977 cpu = ENV_GET_CPU(env);
978 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000979}
j_mayer76a66252007-03-07 08:32:30 +0000980
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800981static void do_trace_print_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530982{
Lluísfc764102011-08-31 20:31:18 +0200983 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530984}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530985
Yonit Halperinedc5cb12011-10-17 10:03:18 +0200986static int client_migrate_info(Monitor *mon, const QDict *qdict,
987 MonitorCompletion cb, void *opaque)
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200988{
989 const char *protocol = qdict_get_str(qdict, "protocol");
990 const char *hostname = qdict_get_str(qdict, "hostname");
991 const char *subject = qdict_get_try_str(qdict, "cert-subject");
992 int port = qdict_get_try_int(qdict, "port", -1);
993 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
994 int ret;
995
996 if (strcmp(protocol, "spice") == 0) {
997 if (!using_spice) {
998 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
999 return -1;
1000 }
1001
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001002 if (port == -1 && tls_port == -1) {
1003 qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
1004 return -1;
1005 }
1006
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001007 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
1008 cb, opaque);
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001009 if (ret != 0) {
1010 qerror_report(QERR_UNDEFINED_ERROR);
1011 return -1;
1012 }
1013 return 0;
1014 }
1015
1016 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1017 return -1;
1018}
1019
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001020static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001021{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001022 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001023}
1024
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001025static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001026{
1027 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001028 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001029
bellard9307c4c2004-04-04 12:57:25 +00001030 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001031 mask = 0;
1032 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001033 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001034 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001035 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001036 return;
1037 }
1038 }
Peter Maydell24537a02013-02-11 16:41:23 +00001039 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001040}
1041
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001042static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001043{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001044 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001045 if (!option || !strcmp(option, "on")) {
1046 singlestep = 1;
1047 } else if (!strcmp(option, "off")) {
1048 singlestep = 0;
1049 } else {
1050 monitor_printf(mon, "unexpected option %s\n", option);
1051 }
1052}
1053
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001054static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001055{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001056 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001057 if (!device)
1058 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1059 if (gdbserver_start(device) < 0) {
1060 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1061 device);
1062 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001063 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001064 } else {
aliguori59030a82009-04-05 18:43:41 +00001065 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1066 device);
bellard8a7ddc32004-03-31 19:00:16 +00001067 }
1068}
1069
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001070static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001071{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001072 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001073 if (select_watchdog_action(action) == -1) {
1074 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1075 }
1076}
1077
aliguori376253e2009-03-05 23:01:23 +00001078static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001079{
aliguori376253e2009-03-05 23:01:23 +00001080 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001081 switch(c) {
1082 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001083 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001084 break;
1085 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001086 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001087 break;
1088 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001089 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001090 break;
1091 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001092 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001093 break;
1094 default:
1095 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001096 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001097 } else {
aliguori376253e2009-03-05 23:01:23 +00001098 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001099 }
1100 break;
1101 }
aliguori376253e2009-03-05 23:01:23 +00001102 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001103}
1104
aliguori376253e2009-03-05 23:01:23 +00001105static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001106 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001107{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001108 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001109 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001110 uint8_t buf[16];
1111 uint64_t v;
1112
1113 if (format == 'i') {
1114 int flags;
1115 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001116 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001117#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001118 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001119 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001120 } else if (wsize == 4) {
1121 flags = 0;
1122 } else {
bellard6a15fd12006-04-12 21:07:07 +00001123 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001124 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001125 if (env) {
1126#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001127 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001128 (env->segs[R_CS].flags & DESC_L_MASK))
1129 flags = 2;
1130 else
1131#endif
1132 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1133 flags = 1;
1134 }
bellard4c27ba22004-04-25 18:05:08 +00001135 }
1136#endif
aliguori376253e2009-03-05 23:01:23 +00001137 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001138 return;
1139 }
1140
1141 len = wsize * count;
1142 if (wsize == 1)
1143 line_size = 8;
1144 else
1145 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001146 max_digits = 0;
1147
1148 switch(format) {
1149 case 'o':
1150 max_digits = (wsize * 8 + 2) / 3;
1151 break;
1152 default:
1153 case 'x':
1154 max_digits = (wsize * 8) / 4;
1155 break;
1156 case 'u':
1157 case 'd':
1158 max_digits = (wsize * 8 * 10 + 32) / 33;
1159 break;
1160 case 'c':
1161 wsize = 1;
1162 break;
1163 }
1164
1165 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001166 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001167 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001168 else
aliguori376253e2009-03-05 23:01:23 +00001169 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001170 l = len;
1171 if (l > line_size)
1172 l = line_size;
1173 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001174 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001175 } else {
bellard6a00d602005-11-21 23:25:50 +00001176 env = mon_get_cpu();
Andreas Färberf17ec442013-06-29 19:40:58 +02001177 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001178 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001179 break;
1180 }
bellard9307c4c2004-04-04 12:57:25 +00001181 }
ths5fafdf22007-09-16 21:08:06 +00001182 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001183 while (i < l) {
1184 switch(wsize) {
1185 default:
1186 case 1:
1187 v = ldub_raw(buf + i);
1188 break;
1189 case 2:
1190 v = lduw_raw(buf + i);
1191 break;
1192 case 4:
bellard92a31b12005-02-10 22:00:52 +00001193 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001194 break;
1195 case 8:
1196 v = ldq_raw(buf + i);
1197 break;
1198 }
aliguori376253e2009-03-05 23:01:23 +00001199 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001200 switch(format) {
1201 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001202 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001203 break;
1204 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001205 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001206 break;
1207 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001208 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001209 break;
1210 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001211 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001212 break;
1213 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001214 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001215 break;
1216 }
1217 i += wsize;
1218 }
aliguori376253e2009-03-05 23:01:23 +00001219 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001220 addr += l;
1221 len -= l;
1222 }
1223}
1224
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001225static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001226{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001227 int count = qdict_get_int(qdict, "count");
1228 int format = qdict_get_int(qdict, "format");
1229 int size = qdict_get_int(qdict, "size");
1230 target_long addr = qdict_get_int(qdict, "addr");
1231
aliguori376253e2009-03-05 23:01:23 +00001232 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001233}
1234
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001235static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001236{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001237 int count = qdict_get_int(qdict, "count");
1238 int format = qdict_get_int(qdict, "format");
1239 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001240 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001241
aliguori376253e2009-03-05 23:01:23 +00001242 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001243}
1244
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001245static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001246{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001247 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001248 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001249
bellard9307c4c2004-04-04 12:57:25 +00001250 switch(format) {
1251 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001252 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001253 break;
1254 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001255 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001256 break;
1257 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001258 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001259 break;
1260 default:
1261 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001262 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001263 break;
1264 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001265 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001266 break;
1267 }
aliguori376253e2009-03-05 23:01:23 +00001268 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001269}
1270
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001271static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001272{
1273 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001274 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001275 uint32_t start = qdict_get_int(qdict, "start");
1276 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001277
1278 sum = 0;
1279 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001280 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001281 /* BSD sum algorithm ('sum' Unix command) */
1282 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001283 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001284 }
aliguori376253e2009-03-05 23:01:23 +00001285 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001286}
1287
bellard13224a82006-07-14 22:03:35 +00001288static int mouse_button_state;
1289
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001290static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001291{
1292 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001293 const char *dx_str = qdict_get_str(qdict, "dx_str");
1294 const char *dy_str = qdict_get_str(qdict, "dy_str");
1295 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001296 dx = strtol(dx_str, NULL, 0);
1297 dy = strtol(dy_str, NULL, 0);
1298 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001299 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001300 dz = strtol(dz_str, NULL, 0);
1301 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1302}
1303
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001304static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001305{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001306 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001307 mouse_button_state = button_state;
1308 kbd_mouse_event(0, 0, 0, mouse_button_state);
1309}
1310
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001311static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001312{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001313 int size = qdict_get_int(qdict, "size");
1314 int addr = qdict_get_int(qdict, "addr");
1315 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001316 uint32_t val;
1317 int suffix;
1318
1319 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001320 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001321 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001322 addr++;
1323 }
1324 addr &= 0xffff;
1325
1326 switch(size) {
1327 default:
1328 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001329 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001330 suffix = 'b';
1331 break;
1332 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001333 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001334 suffix = 'w';
1335 break;
1336 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001337 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001338 suffix = 'l';
1339 break;
1340 }
aliguori376253e2009-03-05 23:01:23 +00001341 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1342 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001343}
bellarda3a91a32004-06-04 11:06:21 +00001344
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001345static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001346{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001347 int size = qdict_get_int(qdict, "size");
1348 int addr = qdict_get_int(qdict, "addr");
1349 int val = qdict_get_int(qdict, "val");
1350
Jan Kiszkaf1147842009-07-14 10:20:11 +02001351 addr &= IOPORTS_MASK;
1352
1353 switch (size) {
1354 default:
1355 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001356 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001357 break;
1358 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001359 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001360 break;
1361 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001362 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001363 break;
1364 }
1365}
1366
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001367static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001368{
1369 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001370 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001371
Jan Kiszka76e30d02009-07-02 00:19:02 +02001372 res = qemu_boot_set(bootdevice);
1373 if (res == 0) {
1374 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1375 } else if (res > 0) {
1376 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001377 } else {
aliguori376253e2009-03-05 23:01:23 +00001378 monitor_printf(mon, "no function defined to set boot device list for "
1379 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001380 }
1381}
1382
bellardb86bda52004-09-18 19:32:46 +00001383#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001384static void print_pte(Monitor *mon, hwaddr addr,
1385 hwaddr pte,
1386 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001387{
Blue Swirld65aaf32010-12-11 18:56:24 +00001388#ifdef TARGET_X86_64
1389 if (addr & (1ULL << 47)) {
1390 addr |= -1LL << 48;
1391 }
1392#endif
1393 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1394 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001395 addr,
1396 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001397 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001398 pte & PG_GLOBAL_MASK ? 'G' : '-',
1399 pte & PG_PSE_MASK ? 'P' : '-',
1400 pte & PG_DIRTY_MASK ? 'D' : '-',
1401 pte & PG_ACCESSED_MASK ? 'A' : '-',
1402 pte & PG_PCD_MASK ? 'C' : '-',
1403 pte & PG_PWT_MASK ? 'T' : '-',
1404 pte & PG_USER_MASK ? 'U' : '-',
1405 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001406}
1407
Andreas Färber9349b4f2012-03-14 01:38:32 +01001408static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001409{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001410 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001411 uint32_t pgd, pde, pte;
1412
bellardb86bda52004-09-18 19:32:46 +00001413 pgd = env->cr[3] & ~0xfff;
1414 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001415 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001416 pde = le32_to_cpu(pde);
1417 if (pde & PG_PRESENT_MASK) {
1418 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001419 /* 4M pages */
1420 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001421 } else {
1422 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001423 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001424 pte = le32_to_cpu(pte);
1425 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001426 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001427 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001428 ~0xfff);
1429 }
1430 }
1431 }
1432 }
1433 }
1434}
1435
Andreas Färber9349b4f2012-03-14 01:38:32 +01001436static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001437{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001438 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001439 uint64_t pdpe, pde, pte;
1440 uint64_t pdp_addr, pd_addr, pt_addr;
1441
1442 pdp_addr = env->cr[3] & ~0x1f;
1443 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001444 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001445 pdpe = le64_to_cpu(pdpe);
1446 if (pdpe & PG_PRESENT_MASK) {
1447 pd_addr = pdpe & 0x3fffffffff000ULL;
1448 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001449 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001450 pde = le64_to_cpu(pde);
1451 if (pde & PG_PRESENT_MASK) {
1452 if (pde & PG_PSE_MASK) {
1453 /* 2M pages with PAE, CR4.PSE is ignored */
1454 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001455 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001456 } else {
1457 pt_addr = pde & 0x3fffffffff000ULL;
1458 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001459 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001460 pte = le64_to_cpu(pte);
1461 if (pte & PG_PRESENT_MASK) {
1462 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1463 + (l3 << 12),
1464 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001465 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001466 }
1467 }
1468 }
1469 }
1470 }
1471 }
1472 }
1473}
1474
1475#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001476static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001477{
1478 uint64_t l1, l2, l3, l4;
1479 uint64_t pml4e, pdpe, pde, pte;
1480 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1481
1482 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1483 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001484 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001485 pml4e = le64_to_cpu(pml4e);
1486 if (pml4e & PG_PRESENT_MASK) {
1487 pdp_addr = pml4e & 0x3fffffffff000ULL;
1488 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001489 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001490 pdpe = le64_to_cpu(pdpe);
1491 if (pdpe & PG_PRESENT_MASK) {
1492 if (pdpe & PG_PSE_MASK) {
1493 /* 1G pages, CR4.PSE is ignored */
1494 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1495 0x3ffffc0000000ULL);
1496 } else {
1497 pd_addr = pdpe & 0x3fffffffff000ULL;
1498 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001499 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001500 pde = le64_to_cpu(pde);
1501 if (pde & PG_PRESENT_MASK) {
1502 if (pde & PG_PSE_MASK) {
1503 /* 2M pages, CR4.PSE is ignored */
1504 print_pte(mon, (l1 << 39) + (l2 << 30) +
1505 (l3 << 21), pde,
1506 0x3ffffffe00000ULL);
1507 } else {
1508 pt_addr = pde & 0x3fffffffff000ULL;
1509 for (l4 = 0; l4 < 512; l4++) {
1510 cpu_physical_memory_read(pt_addr
1511 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001512 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001513 pte = le64_to_cpu(pte);
1514 if (pte & PG_PRESENT_MASK) {
1515 print_pte(mon, (l1 << 39) +
1516 (l2 << 30) +
1517 (l3 << 21) + (l4 << 12),
1518 pte & ~PG_PSE_MASK,
1519 0x3fffffffff000ULL);
1520 }
1521 }
1522 }
1523 }
1524 }
1525 }
1526 }
1527 }
1528 }
1529 }
1530}
1531#endif
1532
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001533static void tlb_info(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001534{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001535 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001536
1537 env = mon_get_cpu();
1538
1539 if (!(env->cr[0] & CR0_PG_MASK)) {
1540 monitor_printf(mon, "PG disabled\n");
1541 return;
1542 }
1543 if (env->cr[4] & CR4_PAE_MASK) {
1544#ifdef TARGET_X86_64
1545 if (env->hflags & HF_LMA_MASK) {
1546 tlb_info_64(mon, env);
1547 } else
1548#endif
1549 {
1550 tlb_info_pae32(mon, env);
1551 }
1552 } else {
1553 tlb_info_32(mon, env);
1554 }
1555}
1556
Avi Kivitya8170e52012-10-23 12:30:10 +02001557static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001558 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001559 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001560{
bellard9746b152004-11-11 18:30:24 +00001561 int prot1;
1562 prot1 = *plast_prot;
1563 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001564 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001565 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1566 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001567 *pstart, end, end - *pstart,
1568 prot1 & PG_USER_MASK ? 'u' : '-',
1569 'r',
1570 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001571 }
1572 if (prot != 0)
1573 *pstart = end;
1574 else
1575 *pstart = -1;
1576 *plast_prot = prot;
1577 }
1578}
1579
Andreas Färber9349b4f2012-03-14 01:38:32 +01001580static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001581{
Austin Clementsb49ca722011-08-14 23:19:21 -04001582 unsigned int l1, l2;
1583 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001584 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001585 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001586
bellardb86bda52004-09-18 19:32:46 +00001587 pgd = env->cr[3] & ~0xfff;
1588 last_prot = 0;
1589 start = -1;
1590 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001591 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001592 pde = le32_to_cpu(pde);
1593 end = l1 << 22;
1594 if (pde & PG_PRESENT_MASK) {
1595 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1596 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001597 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001598 } else {
1599 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001600 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001601 pte = le32_to_cpu(pte);
1602 end = (l1 << 22) + (l2 << 12);
1603 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001604 prot = pte & pde &
1605 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001606 } else {
1607 prot = 0;
1608 }
aliguori376253e2009-03-05 23:01:23 +00001609 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001610 }
1611 }
1612 } else {
1613 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001614 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001615 }
1616 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001617 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001618 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001619}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001620
Andreas Färber9349b4f2012-03-14 01:38:32 +01001621static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001622{
Austin Clementsb49ca722011-08-14 23:19:21 -04001623 unsigned int l1, l2, l3;
1624 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001625 uint64_t pdpe, pde, pte;
1626 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001627 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001628
1629 pdp_addr = env->cr[3] & ~0x1f;
1630 last_prot = 0;
1631 start = -1;
1632 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001633 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001634 pdpe = le64_to_cpu(pdpe);
1635 end = l1 << 30;
1636 if (pdpe & PG_PRESENT_MASK) {
1637 pd_addr = pdpe & 0x3fffffffff000ULL;
1638 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001639 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001640 pde = le64_to_cpu(pde);
1641 end = (l1 << 30) + (l2 << 21);
1642 if (pde & PG_PRESENT_MASK) {
1643 if (pde & PG_PSE_MASK) {
1644 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1645 PG_PRESENT_MASK);
1646 mem_print(mon, &start, &last_prot, end, prot);
1647 } else {
1648 pt_addr = pde & 0x3fffffffff000ULL;
1649 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001650 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001651 pte = le64_to_cpu(pte);
1652 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1653 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001654 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1655 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001656 } else {
1657 prot = 0;
1658 }
1659 mem_print(mon, &start, &last_prot, end, prot);
1660 }
1661 }
1662 } else {
1663 prot = 0;
1664 mem_print(mon, &start, &last_prot, end, prot);
1665 }
1666 }
1667 } else {
1668 prot = 0;
1669 mem_print(mon, &start, &last_prot, end, prot);
1670 }
1671 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001672 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001673 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001674}
1675
1676
1677#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001678static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001679{
1680 int prot, last_prot;
1681 uint64_t l1, l2, l3, l4;
1682 uint64_t pml4e, pdpe, pde, pte;
1683 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1684
1685 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1686 last_prot = 0;
1687 start = -1;
1688 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001689 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001690 pml4e = le64_to_cpu(pml4e);
1691 end = l1 << 39;
1692 if (pml4e & PG_PRESENT_MASK) {
1693 pdp_addr = pml4e & 0x3fffffffff000ULL;
1694 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001695 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001696 pdpe = le64_to_cpu(pdpe);
1697 end = (l1 << 39) + (l2 << 30);
1698 if (pdpe & PG_PRESENT_MASK) {
1699 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001700 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1701 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001702 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001703 mem_print(mon, &start, &last_prot, end, prot);
1704 } else {
1705 pd_addr = pdpe & 0x3fffffffff000ULL;
1706 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001707 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001708 pde = le64_to_cpu(pde);
1709 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1710 if (pde & PG_PRESENT_MASK) {
1711 if (pde & PG_PSE_MASK) {
1712 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1713 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001714 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001715 mem_print(mon, &start, &last_prot, end, prot);
1716 } else {
1717 pt_addr = pde & 0x3fffffffff000ULL;
1718 for (l4 = 0; l4 < 512; l4++) {
1719 cpu_physical_memory_read(pt_addr
1720 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001721 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001722 pte = le64_to_cpu(pte);
1723 end = (l1 << 39) + (l2 << 30) +
1724 (l3 << 21) + (l4 << 12);
1725 if (pte & PG_PRESENT_MASK) {
1726 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1727 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001728 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001729 } else {
1730 prot = 0;
1731 }
1732 mem_print(mon, &start, &last_prot, end, prot);
1733 }
1734 }
1735 } else {
1736 prot = 0;
1737 mem_print(mon, &start, &last_prot, end, prot);
1738 }
1739 }
1740 }
1741 } else {
1742 prot = 0;
1743 mem_print(mon, &start, &last_prot, end, prot);
1744 }
1745 }
1746 } else {
1747 prot = 0;
1748 mem_print(mon, &start, &last_prot, end, prot);
1749 }
1750 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001751 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001752 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001753}
1754#endif
1755
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001756static void mem_info(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001757{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001758 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001759
1760 env = mon_get_cpu();
1761
1762 if (!(env->cr[0] & CR0_PG_MASK)) {
1763 monitor_printf(mon, "PG disabled\n");
1764 return;
1765 }
1766 if (env->cr[4] & CR4_PAE_MASK) {
1767#ifdef TARGET_X86_64
1768 if (env->hflags & HF_LMA_MASK) {
1769 mem_info_64(mon, env);
1770 } else
1771#endif
1772 {
1773 mem_info_pae32(mon, env);
1774 }
1775 } else {
1776 mem_info_32(mon, env);
1777 }
1778}
bellardb86bda52004-09-18 19:32:46 +00001779#endif
1780
aurel327c664e22009-03-03 06:12:22 +00001781#if defined(TARGET_SH4)
1782
aliguori376253e2009-03-05 23:01:23 +00001783static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001784{
aliguori376253e2009-03-05 23:01:23 +00001785 monitor_printf(mon, " tlb%i:\t"
1786 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1787 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1788 "dirty=%hhu writethrough=%hhu\n",
1789 idx,
1790 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1791 tlb->v, tlb->sh, tlb->c, tlb->pr,
1792 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001793}
1794
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001795static void tlb_info(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001796{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001797 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00001798 int i;
1799
aliguori376253e2009-03-05 23:01:23 +00001800 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001801 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001802 print_tlb (mon, i, &env->itlb[i]);
1803 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001804 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001805 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001806}
1807
1808#endif
1809
Max Filippov692f7372012-01-07 20:02:40 +04001810#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001811static void tlb_info(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001812{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001813 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00001814
1815 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1816}
1817#endif
1818
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001819static void do_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001820{
1821 mtree_info((fprintf_function)monitor_printf, mon);
1822}
1823
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001824static void do_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001825{
aliguorib28b6232009-04-22 20:20:29 +00001826 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001827 CPUState *cpu;
aliguori030ea372009-04-21 22:30:47 +00001828
1829 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1830 for (i = 0; i < nb_numa_nodes; i++) {
1831 monitor_printf(mon, "node %d cpus:", i);
Andreas Färber182735e2013-05-29 22:29:20 +02001832 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001833 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001834 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001835 }
1836 }
1837 monitor_printf(mon, "\n");
1838 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1839 node_mem[i] >> 20);
1840 }
1841}
1842
bellard5f1ce942006-02-08 22:40:15 +00001843#ifdef CONFIG_PROFILER
1844
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001845int64_t qemu_time;
1846int64_t dev_time;
1847
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001848static void do_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001849{
1850 int64_t total;
1851 total = qemu_time;
1852 if (total == 0)
1853 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001854 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001855 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001856 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001857 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001858 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001859 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001860}
1861#else
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001862static void do_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001863{
aliguori376253e2009-03-05 23:01:23 +00001864 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001865}
1866#endif
1867
bellardec36b692006-07-16 18:57:03 +00001868/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001869static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001870
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001871static void do_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001872{
1873 int i;
1874 CaptureState *s;
1875
1876 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001877 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001878 s->ops.info (s->opaque);
1879 }
1880}
1881
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001882static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001883{
1884 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001885 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001886 CaptureState *s;
1887
1888 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1889 if (i == n) {
1890 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001891 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001892 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001893 return;
1894 }
1895 }
1896}
1897
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001898static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001899{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001900 const char *path = qdict_get_str(qdict, "path");
1901 int has_freq = qdict_haskey(qdict, "freq");
1902 int freq = qdict_get_try_int(qdict, "freq", -1);
1903 int has_bits = qdict_haskey(qdict, "bits");
1904 int bits = qdict_get_try_int(qdict, "bits", -1);
1905 int has_channels = qdict_haskey(qdict, "nchannels");
1906 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001907 CaptureState *s;
1908
Anthony Liguori7267c092011-08-20 22:09:37 -05001909 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001910
1911 freq = has_freq ? freq : 44100;
1912 bits = has_bits ? bits : 16;
1913 nchannels = has_channels ? nchannels : 2;
1914
1915 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001916 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001917 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001918 return;
bellardec36b692006-07-16 18:57:03 +00001919 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001920 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001921}
bellardec36b692006-07-16 18:57:03 +00001922
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001923static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001924{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001925 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001926
aliguori76655d62009-03-06 20:27:37 +00001927 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001928 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001929 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001930 return acl;
1931}
aliguori76655d62009-03-06 20:27:37 +00001932
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001933static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001934{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001935 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001936 qemu_acl *acl = find_acl(mon, aclname);
1937 qemu_acl_entry *entry;
1938 int i = 0;
1939
1940 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001941 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001942 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001943 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001944 i++;
1945 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001946 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001947 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001948 }
1949}
1950
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001951static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001952{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001953 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001954 qemu_acl *acl = find_acl(mon, aclname);
1955
1956 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001957 qemu_acl_reset(acl);
1958 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001959 }
1960}
aliguori76655d62009-03-06 20:27:37 +00001961
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001962static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001963{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001964 const char *aclname = qdict_get_str(qdict, "aclname");
1965 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001966 qemu_acl *acl = find_acl(mon, aclname);
1967
1968 if (acl) {
1969 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001970 acl->defaultDeny = 0;
1971 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001972 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001973 acl->defaultDeny = 1;
1974 monitor_printf(mon, "acl: policy set to 'deny'\n");
1975 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001976 monitor_printf(mon, "acl: unknown policy '%s', "
1977 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001978 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001979 }
1980}
aliguori76655d62009-03-06 20:27:37 +00001981
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001982static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001983{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001984 const char *aclname = qdict_get_str(qdict, "aclname");
1985 const char *match = qdict_get_str(qdict, "match");
1986 const char *policy = qdict_get_str(qdict, "policy");
1987 int has_index = qdict_haskey(qdict, "index");
1988 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001989 qemu_acl *acl = find_acl(mon, aclname);
1990 int deny, ret;
1991
1992 if (acl) {
1993 if (strcmp(policy, "allow") == 0) {
1994 deny = 0;
1995 } else if (strcmp(policy, "deny") == 0) {
1996 deny = 1;
1997 } else {
1998 monitor_printf(mon, "acl: unknown policy '%s', "
1999 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002000 return;
2001 }
aliguori28a76be2009-03-06 20:27:40 +00002002 if (has_index)
2003 ret = qemu_acl_insert(acl, deny, match, index);
2004 else
2005 ret = qemu_acl_append(acl, deny, match);
2006 if (ret < 0)
2007 monitor_printf(mon, "acl: unable to add acl entry\n");
2008 else
2009 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002010 }
2011}
aliguori76655d62009-03-06 20:27:37 +00002012
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002013static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002014{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002015 const char *aclname = qdict_get_str(qdict, "aclname");
2016 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002017 qemu_acl *acl = find_acl(mon, aclname);
2018 int ret;
aliguori76655d62009-03-06 20:27:37 +00002019
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002020 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002021 ret = qemu_acl_remove(acl, match);
2022 if (ret < 0)
2023 monitor_printf(mon, "acl: no matching acl entry\n");
2024 else
2025 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002026 }
2027}
2028
Huang Ying79c4f6b2009-06-23 10:05:14 +08002029#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002030static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002031{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002032 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002033 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002034 int cpu_index = qdict_get_int(qdict, "cpu_index");
2035 int bank = qdict_get_int(qdict, "bank");
2036 uint64_t status = qdict_get_int(qdict, "status");
2037 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2038 uint64_t addr = qdict_get_int(qdict, "addr");
2039 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002040 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002041
Jan Kiszka747461c2011-03-02 08:56:10 +01002042 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2043 flags |= MCE_INJECT_BROADCAST;
2044 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002045 cs = qemu_get_cpu(cpu_index);
2046 if (cs != NULL) {
2047 cpu = X86_CPU(cs);
2048 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2049 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002050 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002051}
2052#endif
2053
Corey Bryant208c9d12012-06-22 14:36:09 -04002054void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002055{
Anthony Liguoric227f092009-10-01 16:12:16 -05002056 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002057 int fd;
2058
Corey Bryant208c9d12012-06-22 14:36:09 -04002059 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002060 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002061 error_set(errp, QERR_FD_NOT_SUPPLIED);
2062 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002063 }
2064
2065 if (qemu_isdigit(fdname[0])) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002066 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2067 "a name not starting with a digit");
2068 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002069 }
2070
Corey Bryant208c9d12012-06-22 14:36:09 -04002071 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002072 if (strcmp(monfd->name, fdname) != 0) {
2073 continue;
2074 }
2075
2076 close(monfd->fd);
2077 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002078 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002079 }
2080
Anthony Liguori7267c092011-08-20 22:09:37 -05002081 monfd = g_malloc0(sizeof(mon_fd_t));
2082 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002083 monfd->fd = fd;
2084
Corey Bryant208c9d12012-06-22 14:36:09 -04002085 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002086}
2087
Corey Bryant208c9d12012-06-22 14:36:09 -04002088void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002089{
Anthony Liguoric227f092009-10-01 16:12:16 -05002090 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002091
Corey Bryant208c9d12012-06-22 14:36:09 -04002092 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002093 if (strcmp(monfd->name, fdname) != 0) {
2094 continue;
2095 }
2096
Blue Swirl72cf2d42009-09-12 07:36:22 +00002097 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002098 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002099 g_free(monfd->name);
2100 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002101 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002102 }
2103
Corey Bryant208c9d12012-06-22 14:36:09 -04002104 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002105}
2106
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002107static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002108{
Luiz Capitulino13548692011-07-29 15:36:43 -03002109 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002110 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002111
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002112 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002113
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002114 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002115 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002116 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002117}
2118
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002119int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002120{
Anthony Liguoric227f092009-10-01 16:12:16 -05002121 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002122
Blue Swirl72cf2d42009-09-12 07:36:22 +00002123 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002124 int fd;
2125
2126 if (strcmp(monfd->name, fdname) != 0) {
2127 continue;
2128 }
2129
2130 fd = monfd->fd;
2131
2132 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002133 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002134 g_free(monfd->name);
2135 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002136
2137 return fd;
2138 }
2139
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002140 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002141 return -1;
2142}
2143
Corey Bryantba1c0482012-08-14 16:43:43 -04002144static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2145{
2146 MonFdsetFd *mon_fdset_fd;
2147 MonFdsetFd *mon_fdset_fd_next;
2148
2149 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002150 if ((mon_fdset_fd->removed ||
2151 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2152 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002153 close(mon_fdset_fd->fd);
2154 g_free(mon_fdset_fd->opaque);
2155 QLIST_REMOVE(mon_fdset_fd, next);
2156 g_free(mon_fdset_fd);
2157 }
2158 }
2159
Corey Bryantadb696f2012-08-14 16:43:47 -04002160 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002161 QLIST_REMOVE(mon_fdset, next);
2162 g_free(mon_fdset);
2163 }
2164}
2165
Corey Bryantefb87c12012-08-14 16:43:48 -04002166static void monitor_fdsets_cleanup(void)
2167{
2168 MonFdset *mon_fdset;
2169 MonFdset *mon_fdset_next;
2170
2171 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2172 monitor_fdset_cleanup(mon_fdset);
2173 }
2174}
2175
Corey Bryantba1c0482012-08-14 16:43:43 -04002176AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2177 const char *opaque, Error **errp)
2178{
2179 int fd;
2180 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002181 AddfdInfo *fdinfo;
2182
2183 fd = qemu_chr_fe_get_msgfd(mon->chr);
2184 if (fd == -1) {
2185 error_set(errp, QERR_FD_NOT_SUPPLIED);
2186 goto error;
2187 }
2188
Corey Bryante446f702012-10-18 15:19:32 -04002189 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2190 has_opaque, opaque, errp);
2191 if (fdinfo) {
2192 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002193 }
2194
Corey Bryantba1c0482012-08-14 16:43:43 -04002195error:
2196 if (fd != -1) {
2197 close(fd);
2198 }
2199 return NULL;
2200}
2201
2202void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2203{
2204 MonFdset *mon_fdset;
2205 MonFdsetFd *mon_fdset_fd;
2206 char fd_str[60];
2207
2208 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2209 if (mon_fdset->id != fdset_id) {
2210 continue;
2211 }
2212 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2213 if (has_fd) {
2214 if (mon_fdset_fd->fd != fd) {
2215 continue;
2216 }
2217 mon_fdset_fd->removed = true;
2218 break;
2219 } else {
2220 mon_fdset_fd->removed = true;
2221 }
2222 }
2223 if (has_fd && !mon_fdset_fd) {
2224 goto error;
2225 }
2226 monitor_fdset_cleanup(mon_fdset);
2227 return;
2228 }
2229
2230error:
2231 if (has_fd) {
2232 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2233 fdset_id, fd);
2234 } else {
2235 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2236 }
2237 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2238}
2239
2240FdsetInfoList *qmp_query_fdsets(Error **errp)
2241{
2242 MonFdset *mon_fdset;
2243 MonFdsetFd *mon_fdset_fd;
2244 FdsetInfoList *fdset_list = NULL;
2245
2246 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2247 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2248 FdsetFdInfoList *fdsetfd_list = NULL;
2249
2250 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2251 fdset_info->value->fdset_id = mon_fdset->id;
2252
2253 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2254 FdsetFdInfoList *fdsetfd_info;
2255
2256 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2257 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2258 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2259 if (mon_fdset_fd->opaque) {
2260 fdsetfd_info->value->has_opaque = true;
2261 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2262 } else {
2263 fdsetfd_info->value->has_opaque = false;
2264 }
2265
2266 fdsetfd_info->next = fdsetfd_list;
2267 fdsetfd_list = fdsetfd_info;
2268 }
2269
2270 fdset_info->value->fds = fdsetfd_list;
2271
2272 fdset_info->next = fdset_list;
2273 fdset_list = fdset_info;
2274 }
2275
2276 return fdset_list;
2277}
2278
Corey Bryante446f702012-10-18 15:19:32 -04002279AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2280 bool has_opaque, const char *opaque,
2281 Error **errp)
2282{
2283 MonFdset *mon_fdset = NULL;
2284 MonFdsetFd *mon_fdset_fd;
2285 AddfdInfo *fdinfo;
2286
2287 if (has_fdset_id) {
2288 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2289 /* Break if match found or match impossible due to ordering by ID */
2290 if (fdset_id <= mon_fdset->id) {
2291 if (fdset_id < mon_fdset->id) {
2292 mon_fdset = NULL;
2293 }
2294 break;
2295 }
2296 }
2297 }
2298
2299 if (mon_fdset == NULL) {
2300 int64_t fdset_id_prev = -1;
2301 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2302
2303 if (has_fdset_id) {
2304 if (fdset_id < 0) {
2305 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2306 "a non-negative value");
2307 return NULL;
2308 }
2309 /* Use specified fdset ID */
2310 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2311 mon_fdset_cur = mon_fdset;
2312 if (fdset_id < mon_fdset_cur->id) {
2313 break;
2314 }
2315 }
2316 } else {
2317 /* Use first available fdset ID */
2318 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2319 mon_fdset_cur = mon_fdset;
2320 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2321 fdset_id_prev = mon_fdset_cur->id;
2322 continue;
2323 }
2324 break;
2325 }
2326 }
2327
2328 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2329 if (has_fdset_id) {
2330 mon_fdset->id = fdset_id;
2331 } else {
2332 mon_fdset->id = fdset_id_prev + 1;
2333 }
2334
2335 /* The fdset list is ordered by fdset ID */
2336 if (!mon_fdset_cur) {
2337 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2338 } else if (mon_fdset->id < mon_fdset_cur->id) {
2339 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2340 } else {
2341 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2342 }
2343 }
2344
2345 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2346 mon_fdset_fd->fd = fd;
2347 mon_fdset_fd->removed = false;
2348 if (has_opaque) {
2349 mon_fdset_fd->opaque = g_strdup(opaque);
2350 }
2351 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2352
2353 fdinfo = g_malloc0(sizeof(*fdinfo));
2354 fdinfo->fdset_id = mon_fdset->id;
2355 fdinfo->fd = mon_fdset_fd->fd;
2356
2357 return fdinfo;
2358}
2359
Corey Bryantadb696f2012-08-14 16:43:47 -04002360int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2361{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002362#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002363 MonFdset *mon_fdset;
2364 MonFdsetFd *mon_fdset_fd;
2365 int mon_fd_flags;
2366
Corey Bryantadb696f2012-08-14 16:43:47 -04002367 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2368 if (mon_fdset->id != fdset_id) {
2369 continue;
2370 }
2371 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2372 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2373 if (mon_fd_flags == -1) {
2374 return -1;
2375 }
2376
2377 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2378 return mon_fdset_fd->fd;
2379 }
2380 }
2381 errno = EACCES;
2382 return -1;
2383 }
2384#endif
2385
2386 errno = ENOENT;
2387 return -1;
2388}
2389
2390int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2391{
2392 MonFdset *mon_fdset;
2393 MonFdsetFd *mon_fdset_fd_dup;
2394
2395 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2396 if (mon_fdset->id != fdset_id) {
2397 continue;
2398 }
2399 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2400 if (mon_fdset_fd_dup->fd == dup_fd) {
2401 return -1;
2402 }
2403 }
2404 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2405 mon_fdset_fd_dup->fd = dup_fd;
2406 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2407 return 0;
2408 }
2409 return -1;
2410}
2411
2412static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2413{
2414 MonFdset *mon_fdset;
2415 MonFdsetFd *mon_fdset_fd_dup;
2416
2417 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2418 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2419 if (mon_fdset_fd_dup->fd == dup_fd) {
2420 if (remove) {
2421 QLIST_REMOVE(mon_fdset_fd_dup, next);
2422 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2423 monitor_fdset_cleanup(mon_fdset);
2424 }
2425 }
2426 return mon_fdset->id;
2427 }
2428 }
2429 }
2430 return -1;
2431}
2432
2433int monitor_fdset_dup_fd_find(int dup_fd)
2434{
2435 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2436}
2437
2438int monitor_fdset_dup_fd_remove(int dup_fd)
2439{
2440 return monitor_fdset_dup_fd_find_remove(dup_fd, true);
2441}
2442
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002443int monitor_handle_fd_param(Monitor *mon, const char *fdname)
2444{
2445 int fd;
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002446 Error *local_err = NULL;
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002447
2448 if (!qemu_isdigit(fdname[0]) && mon) {
2449
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002450 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002451 if (fd == -1) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002452 qerror_report_err(local_err);
2453 error_free(local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002454 return -1;
2455 }
2456 } else {
2457 fd = qemu_parse_fd(fdname);
2458 }
2459
2460 return fd;
2461}
2462
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002463/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002464static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002465 {
2466 .name = "version",
2467 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002468 .params = "",
2469 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002470 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002471 },
2472 {
2473 .name = "network",
2474 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002475 .params = "",
2476 .help = "show the network state",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002477 .mhandler.cmd = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002478 },
2479 {
2480 .name = "chardev",
2481 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002482 .params = "",
2483 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002484 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002485 },
2486 {
2487 .name = "block",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002488 .args_type = "verbose:-v,device:B?",
2489 .params = "[-v] [device]",
2490 .help = "show info of one block device or all block devices "
2491 "(and details of images with -v option)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002492 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002493 },
2494 {
2495 .name = "blockstats",
2496 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002497 .params = "",
2498 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002499 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002500 },
2501 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002502 .name = "block-jobs",
2503 .args_type = "",
2504 .params = "",
2505 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002506 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002507 },
2508 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002509 .name = "registers",
2510 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002511 .params = "",
2512 .help = "show the cpu registers",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002513 .mhandler.cmd = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002514 },
2515 {
2516 .name = "cpus",
2517 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002518 .params = "",
2519 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002520 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002521 },
2522 {
2523 .name = "history",
2524 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002525 .params = "",
2526 .help = "show the command line history",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002527 .mhandler.cmd = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002528 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002529#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2530 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002531 {
2532 .name = "irq",
2533 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002534 .params = "",
2535 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002536#ifdef TARGET_SPARC
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002537 .mhandler.cmd = sun4m_irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002538#elif defined(TARGET_LM32)
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002539 .mhandler.cmd = lm32_irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002540#else
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002541 .mhandler.cmd = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002542#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002543 },
2544 {
2545 .name = "pic",
2546 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002547 .params = "",
2548 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002549#ifdef TARGET_SPARC
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002550 .mhandler.cmd = sun4m_pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002551#elif defined(TARGET_LM32)
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002552 .mhandler.cmd = lm32_do_pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002553#else
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002554 .mhandler.cmd = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002555#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002556 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002557#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002558 {
2559 .name = "pci",
2560 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002561 .params = "",
2562 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002563 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002564 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002565#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002566 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002567 {
2568 .name = "tlb",
2569 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002570 .params = "",
2571 .help = "show virtual to physical memory mappings",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002572 .mhandler.cmd = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002573 },
aurel327c664e22009-03-03 06:12:22 +00002574#endif
2575#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002576 {
2577 .name = "mem",
2578 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002579 .params = "",
2580 .help = "show the active virtual memory mappings",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002581 .mhandler.cmd = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002582 },
bellardb86bda52004-09-18 19:32:46 +00002583#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002584 {
Blue Swirl314e2982011-09-11 20:22:05 +00002585 .name = "mtree",
2586 .args_type = "",
2587 .params = "",
2588 .help = "show memory tree",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002589 .mhandler.cmd = do_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002590 },
2591 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002592 .name = "jit",
2593 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002594 .params = "",
2595 .help = "show dynamic compiler info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002596 .mhandler.cmd = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002597 },
2598 {
2599 .name = "kvm",
2600 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002601 .params = "",
2602 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002603 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002604 },
2605 {
2606 .name = "numa",
2607 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002608 .params = "",
2609 .help = "show NUMA information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002610 .mhandler.cmd = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002611 },
2612 {
2613 .name = "usb",
2614 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002615 .params = "",
2616 .help = "show guest USB devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002617 .mhandler.cmd = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002618 },
2619 {
2620 .name = "usbhost",
2621 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002622 .params = "",
2623 .help = "show host USB devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002624 .mhandler.cmd = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002625 },
2626 {
2627 .name = "profile",
2628 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002629 .params = "",
2630 .help = "show profiling information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002631 .mhandler.cmd = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002632 },
2633 {
2634 .name = "capture",
2635 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002636 .params = "",
2637 .help = "show capture information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002638 .mhandler.cmd = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002639 },
2640 {
2641 .name = "snapshots",
2642 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002643 .params = "",
2644 .help = "show the currently saved VM snapshots",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002645 .mhandler.cmd = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002646 },
2647 {
2648 .name = "status",
2649 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002650 .params = "",
2651 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002652 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002653 },
2654 {
2655 .name = "pcmcia",
2656 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002657 .params = "",
2658 .help = "show guest PCMCIA status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002659 .mhandler.cmd = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002660 },
2661 {
2662 .name = "mice",
2663 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 .params = "",
2665 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002666 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002667 },
2668 {
2669 .name = "vnc",
2670 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 .params = "",
2672 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002673 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002674 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002675#if defined(CONFIG_SPICE)
2676 {
2677 .name = "spice",
2678 .args_type = "",
2679 .params = "",
2680 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002681 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002682 },
2683#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002684 {
2685 .name = "name",
2686 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002687 .params = "",
2688 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002689 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002690 },
2691 {
2692 .name = "uuid",
2693 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002694 .params = "",
2695 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002696 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002697 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002698 {
2699 .name = "cpustats",
2700 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002701 .params = "",
2702 .help = "show CPU statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002703 .mhandler.cmd = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002704 },
blueswir131a60e22007-10-26 18:42:59 +00002705#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002706 {
2707 .name = "usernet",
2708 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002709 .params = "",
2710 .help = "show user network stack connection states",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002711 .mhandler.cmd = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002712 },
blueswir131a60e22007-10-26 18:42:59 +00002713#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002714 {
2715 .name = "migrate",
2716 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002717 .params = "",
2718 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002719 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002720 },
2721 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002722 .name = "migrate_capabilities",
2723 .args_type = "",
2724 .params = "",
2725 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002726 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002727 },
2728 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002729 .name = "migrate_cache_size",
2730 .args_type = "",
2731 .params = "",
2732 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002733 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002734 },
2735 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002736 .name = "balloon",
2737 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002738 .params = "",
2739 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002740 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002741 },
2742 {
2743 .name = "qtree",
2744 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002745 .params = "",
2746 .help = "show device tree",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002747 .mhandler.cmd = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002748 },
2749 {
2750 .name = "qdm",
2751 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002752 .params = "",
2753 .help = "show qdev device model list",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002754 .mhandler.cmd = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002755 },
2756 {
2757 .name = "roms",
2758 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002759 .params = "",
2760 .help = "show roms",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002761 .mhandler.cmd = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002762 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302763 {
2764 .name = "trace-events",
2765 .args_type = "",
2766 .params = "",
2767 .help = "show available trace-events & their state",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002768 .mhandler.cmd = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302769 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002770 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002771 .name = "tpm",
2772 .args_type = "",
2773 .params = "",
2774 .help = "show the TPM device",
2775 .mhandler.cmd = hmp_info_tpm,
2776 },
2777 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002778 .name = NULL,
2779 },
bellard9dc39cb2004-03-14 21:38:27 +00002780};
2781
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002782/* mon_cmds and info_cmds would be sorted at runtime */
2783static mon_cmd_t mon_cmds[] = {
2784#include "hmp-commands.h"
2785 { NULL, NULL, },
2786};
2787
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002788static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002789#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002790 { /* NULL */ },
2791};
2792
bellard9307c4c2004-04-04 12:57:25 +00002793/*******************************************************************/
2794
2795static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002796static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002797
bellard92a31b12005-02-10 22:00:52 +00002798#define MD_TLONG 0
2799#define MD_I32 1
2800
bellard9307c4c2004-04-04 12:57:25 +00002801typedef struct MonitorDef {
2802 const char *name;
2803 int offset;
blueswir18662d652008-10-02 18:32:44 +00002804 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002805 int type;
bellard9307c4c2004-04-04 12:57:25 +00002806} MonitorDef;
2807
bellard57206fd2004-04-25 18:54:52 +00002808#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002809static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002810{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002811 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002812 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002813}
2814#endif
2815
bellarda541f292004-04-12 20:39:29 +00002816#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002817static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002818{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002819 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002820 unsigned int u;
2821 int i;
2822
2823 u = 0;
2824 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002825 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002826
2827 return u;
2828}
2829
blueswir18662d652008-10-02 18:32:44 +00002830static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002831{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002832 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002833 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002834}
2835
blueswir18662d652008-10-02 18:32:44 +00002836static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002837{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002838 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002839 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002840}
bellard9fddaa02004-05-21 12:59:32 +00002841
blueswir18662d652008-10-02 18:32:44 +00002842static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002843{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002844 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002845 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002846}
2847
blueswir18662d652008-10-02 18:32:44 +00002848static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002849{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002850 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002851 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002852}
2853
blueswir18662d652008-10-02 18:32:44 +00002854static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002855{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002856 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002857 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002858}
bellarda541f292004-04-12 20:39:29 +00002859#endif
2860
bellarde95c8d52004-09-30 22:22:08 +00002861#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002862#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002863static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002864{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002865 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002866
2867 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002868}
bellard7b936c02005-10-30 17:05:13 +00002869#endif
bellarde95c8d52004-09-30 22:22:08 +00002870
blueswir18662d652008-10-02 18:32:44 +00002871static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002872{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002873 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002874 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002875}
2876#endif
2877
blueswir18662d652008-10-02 18:32:44 +00002878static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002879#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002880
2881#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01002882 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2883 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2884 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002885
Andreas Färbere59d1672012-02-16 00:40:47 +01002886 { "eax", offsetof(CPUX86State, regs[0]) },
2887 { "ecx", offsetof(CPUX86State, regs[1]) },
2888 { "edx", offsetof(CPUX86State, regs[2]) },
2889 { "ebx", offsetof(CPUX86State, regs[3]) },
2890 { "esp|sp", offsetof(CPUX86State, regs[4]) },
2891 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
2892 { "esi", offsetof(CPUX86State, regs[6]) },
2893 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002894#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01002895 { "r8", offsetof(CPUX86State, regs[8]) },
2896 { "r9", offsetof(CPUX86State, regs[9]) },
2897 { "r10", offsetof(CPUX86State, regs[10]) },
2898 { "r11", offsetof(CPUX86State, regs[11]) },
2899 { "r12", offsetof(CPUX86State, regs[12]) },
2900 { "r13", offsetof(CPUX86State, regs[13]) },
2901 { "r14", offsetof(CPUX86State, regs[14]) },
2902 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00002903#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01002904 { "eflags", offsetof(CPUX86State, eflags) },
2905 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00002906 SEG("cs", R_CS)
2907 SEG("ds", R_DS)
2908 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002909 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002910 SEG("fs", R_FS)
2911 SEG("gs", R_GS)
2912 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002913#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002914 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01002915 { "r0", offsetof(CPUPPCState, gpr[0]) },
2916 { "r1", offsetof(CPUPPCState, gpr[1]) },
2917 { "r2", offsetof(CPUPPCState, gpr[2]) },
2918 { "r3", offsetof(CPUPPCState, gpr[3]) },
2919 { "r4", offsetof(CPUPPCState, gpr[4]) },
2920 { "r5", offsetof(CPUPPCState, gpr[5]) },
2921 { "r6", offsetof(CPUPPCState, gpr[6]) },
2922 { "r7", offsetof(CPUPPCState, gpr[7]) },
2923 { "r8", offsetof(CPUPPCState, gpr[8]) },
2924 { "r9", offsetof(CPUPPCState, gpr[9]) },
2925 { "r10", offsetof(CPUPPCState, gpr[10]) },
2926 { "r11", offsetof(CPUPPCState, gpr[11]) },
2927 { "r12", offsetof(CPUPPCState, gpr[12]) },
2928 { "r13", offsetof(CPUPPCState, gpr[13]) },
2929 { "r14", offsetof(CPUPPCState, gpr[14]) },
2930 { "r15", offsetof(CPUPPCState, gpr[15]) },
2931 { "r16", offsetof(CPUPPCState, gpr[16]) },
2932 { "r17", offsetof(CPUPPCState, gpr[17]) },
2933 { "r18", offsetof(CPUPPCState, gpr[18]) },
2934 { "r19", offsetof(CPUPPCState, gpr[19]) },
2935 { "r20", offsetof(CPUPPCState, gpr[20]) },
2936 { "r21", offsetof(CPUPPCState, gpr[21]) },
2937 { "r22", offsetof(CPUPPCState, gpr[22]) },
2938 { "r23", offsetof(CPUPPCState, gpr[23]) },
2939 { "r24", offsetof(CPUPPCState, gpr[24]) },
2940 { "r25", offsetof(CPUPPCState, gpr[25]) },
2941 { "r26", offsetof(CPUPPCState, gpr[26]) },
2942 { "r27", offsetof(CPUPPCState, gpr[27]) },
2943 { "r28", offsetof(CPUPPCState, gpr[28]) },
2944 { "r29", offsetof(CPUPPCState, gpr[29]) },
2945 { "r30", offsetof(CPUPPCState, gpr[30]) },
2946 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002947 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01002948 { "f0", offsetof(CPUPPCState, fpr[0]) },
2949 { "f1", offsetof(CPUPPCState, fpr[1]) },
2950 { "f2", offsetof(CPUPPCState, fpr[2]) },
2951 { "f3", offsetof(CPUPPCState, fpr[3]) },
2952 { "f4", offsetof(CPUPPCState, fpr[4]) },
2953 { "f5", offsetof(CPUPPCState, fpr[5]) },
2954 { "f6", offsetof(CPUPPCState, fpr[6]) },
2955 { "f7", offsetof(CPUPPCState, fpr[7]) },
2956 { "f8", offsetof(CPUPPCState, fpr[8]) },
2957 { "f9", offsetof(CPUPPCState, fpr[9]) },
2958 { "f10", offsetof(CPUPPCState, fpr[10]) },
2959 { "f11", offsetof(CPUPPCState, fpr[11]) },
2960 { "f12", offsetof(CPUPPCState, fpr[12]) },
2961 { "f13", offsetof(CPUPPCState, fpr[13]) },
2962 { "f14", offsetof(CPUPPCState, fpr[14]) },
2963 { "f15", offsetof(CPUPPCState, fpr[15]) },
2964 { "f16", offsetof(CPUPPCState, fpr[16]) },
2965 { "f17", offsetof(CPUPPCState, fpr[17]) },
2966 { "f18", offsetof(CPUPPCState, fpr[18]) },
2967 { "f19", offsetof(CPUPPCState, fpr[19]) },
2968 { "f20", offsetof(CPUPPCState, fpr[20]) },
2969 { "f21", offsetof(CPUPPCState, fpr[21]) },
2970 { "f22", offsetof(CPUPPCState, fpr[22]) },
2971 { "f23", offsetof(CPUPPCState, fpr[23]) },
2972 { "f24", offsetof(CPUPPCState, fpr[24]) },
2973 { "f25", offsetof(CPUPPCState, fpr[25]) },
2974 { "f26", offsetof(CPUPPCState, fpr[26]) },
2975 { "f27", offsetof(CPUPPCState, fpr[27]) },
2976 { "f28", offsetof(CPUPPCState, fpr[28]) },
2977 { "f29", offsetof(CPUPPCState, fpr[29]) },
2978 { "f30", offsetof(CPUPPCState, fpr[30]) },
2979 { "f31", offsetof(CPUPPCState, fpr[31]) },
2980 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00002981 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01002982 { "nip|pc", offsetof(CPUPPCState, nip) },
2983 { "lr", offsetof(CPUPPCState, lr) },
2984 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002985 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002986 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002987 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002988 { "msr", 0, &monitor_get_msr, },
2989 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002990 { "tbu", 0, &monitor_get_tbu, },
2991 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002992 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01002993 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
2994 { "sr0", offsetof(CPUPPCState, sr[0]) },
2995 { "sr1", offsetof(CPUPPCState, sr[1]) },
2996 { "sr2", offsetof(CPUPPCState, sr[2]) },
2997 { "sr3", offsetof(CPUPPCState, sr[3]) },
2998 { "sr4", offsetof(CPUPPCState, sr[4]) },
2999 { "sr5", offsetof(CPUPPCState, sr[5]) },
3000 { "sr6", offsetof(CPUPPCState, sr[6]) },
3001 { "sr7", offsetof(CPUPPCState, sr[7]) },
3002 { "sr8", offsetof(CPUPPCState, sr[8]) },
3003 { "sr9", offsetof(CPUPPCState, sr[9]) },
3004 { "sr10", offsetof(CPUPPCState, sr[10]) },
3005 { "sr11", offsetof(CPUPPCState, sr[11]) },
3006 { "sr12", offsetof(CPUPPCState, sr[12]) },
3007 { "sr13", offsetof(CPUPPCState, sr[13]) },
3008 { "sr14", offsetof(CPUPPCState, sr[14]) },
3009 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003010 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003011 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003012
Andreas Färbere59d1672012-02-16 00:40:47 +01003013 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3014 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
3015 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3016 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3017 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3018 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3019 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3020 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3021 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3022 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3023 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3024 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3025 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3026 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3027 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3028 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3029 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3030 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3031 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3032 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3033 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3034 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3035 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3036 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3037 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3038 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3039 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3040 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3041 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3042 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3043 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3044 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3045 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3046 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3047 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3048 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3049 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3050 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3051 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3052 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3053 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3054 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3055 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3056 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3057 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3058 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3059 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3060 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3061 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3062 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3063 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3064 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3065 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3066 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3067 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3068 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3069 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3070 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3071 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3072 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3073 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3074 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3075 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3076 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3077 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3078 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003079
bellarde95c8d52004-09-30 22:22:08 +00003080#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003081 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3082 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3083 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3084 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3085 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3086 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3087 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3088 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003089 { "o0", 0, monitor_get_reg },
3090 { "o1", 1, monitor_get_reg },
3091 { "o2", 2, monitor_get_reg },
3092 { "o3", 3, monitor_get_reg },
3093 { "o4", 4, monitor_get_reg },
3094 { "o5", 5, monitor_get_reg },
3095 { "o6", 6, monitor_get_reg },
3096 { "o7", 7, monitor_get_reg },
3097 { "l0", 8, monitor_get_reg },
3098 { "l1", 9, monitor_get_reg },
3099 { "l2", 10, monitor_get_reg },
3100 { "l3", 11, monitor_get_reg },
3101 { "l4", 12, monitor_get_reg },
3102 { "l5", 13, monitor_get_reg },
3103 { "l6", 14, monitor_get_reg },
3104 { "l7", 15, monitor_get_reg },
3105 { "i0", 16, monitor_get_reg },
3106 { "i1", 17, monitor_get_reg },
3107 { "i2", 18, monitor_get_reg },
3108 { "i3", 19, monitor_get_reg },
3109 { "i4", 20, monitor_get_reg },
3110 { "i5", 21, monitor_get_reg },
3111 { "i6", 22, monitor_get_reg },
3112 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003113 { "pc", offsetof(CPUSPARCState, pc) },
3114 { "npc", offsetof(CPUSPARCState, npc) },
3115 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003116#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003117 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003118 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003119#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003120 { "tbr", offsetof(CPUSPARCState, tbr) },
3121 { "fsr", offsetof(CPUSPARCState, fsr) },
3122 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3123 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3124 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3125 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3126 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3127 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3128 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3129 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3130 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3131 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3132 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3133 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3134 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3135 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3136 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3137 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3138 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3139 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3140 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3141 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3142 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3143 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3144 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3145 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3146 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3147 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3148 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3149 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3150 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3151 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3152 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3153 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003154#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003155 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3156 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3157 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3158 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3159 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3160 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3161 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3162 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3163 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3164 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3165 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3166 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3167 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3168 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3169 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3170 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3171 { "asi", offsetof(CPUSPARCState, asi) },
3172 { "pstate", offsetof(CPUSPARCState, pstate) },
3173 { "cansave", offsetof(CPUSPARCState, cansave) },
3174 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3175 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3176 { "wstate", offsetof(CPUSPARCState, wstate) },
3177 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3178 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003179#endif
bellard9307c4c2004-04-04 12:57:25 +00003180#endif
3181 { NULL },
3182};
3183
Stefan Weil9c3175c2013-08-22 21:30:09 +02003184static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3185expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003186{
Fam Zheng277acfe2013-08-20 10:58:21 +08003187 va_list ap;
3188 va_start(ap, fmt);
3189 monitor_vprintf(mon, fmt, ap);
3190 monitor_printf(mon, "\n");
3191 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003192 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003193}
3194
Markus Armbruster09b94182010-01-20 13:07:30 +01003195/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003196static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003197{
blueswir18662d652008-10-02 18:32:44 +00003198 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003199 void *ptr;
3200
bellard9307c4c2004-04-04 12:57:25 +00003201 for(md = monitor_defs; md->name != NULL; md++) {
3202 if (compare_cmd(name, md->name)) {
3203 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003204 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003205 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003206 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003207 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003208 switch(md->type) {
3209 case MD_I32:
3210 *pval = *(int32_t *)ptr;
3211 break;
3212 case MD_TLONG:
3213 *pval = *(target_long *)ptr;
3214 break;
3215 default:
3216 *pval = 0;
3217 break;
3218 }
bellard9307c4c2004-04-04 12:57:25 +00003219 }
3220 return 0;
3221 }
3222 }
3223 return -1;
3224}
3225
3226static void next(void)
3227{
Blue Swirl660f11b2009-07-31 21:16:51 +00003228 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003229 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003230 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003231 pch++;
3232 }
3233}
3234
aliguori376253e2009-03-05 23:01:23 +00003235static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003236
aliguori376253e2009-03-05 23:01:23 +00003237static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003238{
blueswir1c2efc952007-09-25 17:28:42 +00003239 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003240 char *p;
bellard6a00d602005-11-21 23:25:50 +00003241 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003242
3243 switch(*pch) {
3244 case '+':
3245 next();
aliguori376253e2009-03-05 23:01:23 +00003246 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003247 break;
3248 case '-':
3249 next();
aliguori376253e2009-03-05 23:01:23 +00003250 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003251 break;
3252 case '~':
3253 next();
aliguori376253e2009-03-05 23:01:23 +00003254 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003255 break;
3256 case '(':
3257 next();
aliguori376253e2009-03-05 23:01:23 +00003258 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003259 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003260 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003261 }
3262 next();
3263 break;
bellard81d09122004-07-14 17:21:37 +00003264 case '\'':
3265 pch++;
3266 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003267 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003268 n = *pch;
3269 pch++;
3270 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003271 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003272 next();
3273 break;
bellard9307c4c2004-04-04 12:57:25 +00003274 case '$':
3275 {
3276 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003277 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003278
bellard9307c4c2004-04-04 12:57:25 +00003279 pch++;
3280 q = buf;
3281 while ((*pch >= 'a' && *pch <= 'z') ||
3282 (*pch >= 'A' && *pch <= 'Z') ||
3283 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003284 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003285 if ((q - buf) < sizeof(buf) - 1)
3286 *q++ = *pch;
3287 pch++;
3288 }
blueswir1cd390082008-11-16 13:53:32 +00003289 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003290 pch++;
3291 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003292 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003293 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003294 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003295 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003296 }
3297 break;
3298 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003299 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003300 n = 0;
3301 break;
3302 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003303 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003304 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003305 if (errno == ERANGE) {
3306 expr_error(mon, "number too large");
3307 }
bellard9307c4c2004-04-04 12:57:25 +00003308 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003309 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003310 }
3311 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003312 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003313 pch++;
3314 break;
3315 }
3316 return n;
3317}
3318
3319
aliguori376253e2009-03-05 23:01:23 +00003320static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003321{
blueswir1c2efc952007-09-25 17:28:42 +00003322 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003323 int op;
ths3b46e622007-09-17 08:09:54 +00003324
aliguori376253e2009-03-05 23:01:23 +00003325 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003326 for(;;) {
3327 op = *pch;
3328 if (op != '*' && op != '/' && op != '%')
3329 break;
3330 next();
aliguori376253e2009-03-05 23:01:23 +00003331 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003332 switch(op) {
3333 default:
3334 case '*':
3335 val *= val2;
3336 break;
3337 case '/':
3338 case '%':
ths5fafdf22007-09-16 21:08:06 +00003339 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003340 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003341 if (op == '/')
3342 val /= val2;
3343 else
3344 val %= val2;
3345 break;
3346 }
3347 }
3348 return val;
3349}
3350
aliguori376253e2009-03-05 23:01:23 +00003351static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003352{
blueswir1c2efc952007-09-25 17:28:42 +00003353 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003354 int op;
bellard9307c4c2004-04-04 12:57:25 +00003355
aliguori376253e2009-03-05 23:01:23 +00003356 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003357 for(;;) {
3358 op = *pch;
3359 if (op != '&' && op != '|' && op != '^')
3360 break;
3361 next();
aliguori376253e2009-03-05 23:01:23 +00003362 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003363 switch(op) {
3364 default:
3365 case '&':
3366 val &= val2;
3367 break;
3368 case '|':
3369 val |= val2;
3370 break;
3371 case '^':
3372 val ^= val2;
3373 break;
3374 }
3375 }
3376 return val;
3377}
3378
aliguori376253e2009-03-05 23:01:23 +00003379static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003380{
blueswir1c2efc952007-09-25 17:28:42 +00003381 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003382 int op;
bellard9307c4c2004-04-04 12:57:25 +00003383
aliguori376253e2009-03-05 23:01:23 +00003384 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003385 for(;;) {
3386 op = *pch;
3387 if (op != '+' && op != '-')
3388 break;
3389 next();
aliguori376253e2009-03-05 23:01:23 +00003390 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003391 if (op == '+')
3392 val += val2;
3393 else
3394 val -= val2;
3395 }
3396 return val;
3397}
3398
aliguori376253e2009-03-05 23:01:23 +00003399static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003400{
3401 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003402 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003403 *pp = pch;
3404 return -1;
3405 }
blueswir1cd390082008-11-16 13:53:32 +00003406 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003407 pch++;
aliguori376253e2009-03-05 23:01:23 +00003408 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003409 *pp = pch;
3410 return 0;
3411}
3412
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003413static int get_double(Monitor *mon, double *pval, const char **pp)
3414{
3415 const char *p = *pp;
3416 char *tailp;
3417 double d;
3418
3419 d = strtod(p, &tailp);
3420 if (tailp == p) {
3421 monitor_printf(mon, "Number expected\n");
3422 return -1;
3423 }
3424 if (d != d || d - d != 0) {
3425 /* NaN or infinity */
3426 monitor_printf(mon, "Bad number\n");
3427 return -1;
3428 }
3429 *pval = d;
3430 *pp = tailp;
3431 return 0;
3432}
3433
bellard9307c4c2004-04-04 12:57:25 +00003434static int get_str(char *buf, int buf_size, const char **pp)
3435{
3436 const char *p;
3437 char *q;
3438 int c;
3439
bellard81d09122004-07-14 17:21:37 +00003440 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003441 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003442 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003443 p++;
3444 if (*p == '\0') {
3445 fail:
bellard81d09122004-07-14 17:21:37 +00003446 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003447 *pp = p;
3448 return -1;
3449 }
bellard9307c4c2004-04-04 12:57:25 +00003450 if (*p == '\"') {
3451 p++;
3452 while (*p != '\0' && *p != '\"') {
3453 if (*p == '\\') {
3454 p++;
3455 c = *p++;
3456 switch(c) {
3457 case 'n':
3458 c = '\n';
3459 break;
3460 case 'r':
3461 c = '\r';
3462 break;
3463 case '\\':
3464 case '\'':
3465 case '\"':
3466 break;
3467 default:
3468 qemu_printf("unsupported escape code: '\\%c'\n", c);
3469 goto fail;
3470 }
3471 if ((q - buf) < buf_size - 1) {
3472 *q++ = c;
3473 }
3474 } else {
3475 if ((q - buf) < buf_size - 1) {
3476 *q++ = *p;
3477 }
3478 p++;
3479 }
3480 }
3481 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003482 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003483 goto fail;
3484 }
3485 p++;
3486 } else {
blueswir1cd390082008-11-16 13:53:32 +00003487 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003488 if ((q - buf) < buf_size - 1) {
3489 *q++ = *p;
3490 }
3491 p++;
3492 }
bellard9307c4c2004-04-04 12:57:25 +00003493 }
bellard81d09122004-07-14 17:21:37 +00003494 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003495 *pp = p;
3496 return 0;
3497}
3498
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003499/*
3500 * Store the command-name in cmdname, and return a pointer to
3501 * the remaining of the command string.
3502 */
3503static const char *get_command_name(const char *cmdline,
3504 char *cmdname, size_t nlen)
3505{
3506 size_t len;
3507 const char *p, *pstart;
3508
3509 p = cmdline;
3510 while (qemu_isspace(*p))
3511 p++;
3512 if (*p == '\0')
3513 return NULL;
3514 pstart = p;
3515 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3516 p++;
3517 len = p - pstart;
3518 if (len > nlen - 1)
3519 len = nlen - 1;
3520 memcpy(cmdname, pstart, len);
3521 cmdname[len] = '\0';
3522 return p;
3523}
3524
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003525/**
3526 * Read key of 'type' into 'key' and return the current
3527 * 'type' pointer.
3528 */
3529static char *key_get_info(const char *type, char **key)
3530{
3531 size_t len;
3532 char *p, *str;
3533
3534 if (*type == ',')
3535 type++;
3536
3537 p = strchr(type, ':');
3538 if (!p) {
3539 *key = NULL;
3540 return NULL;
3541 }
3542 len = p - type;
3543
Anthony Liguori7267c092011-08-20 22:09:37 -05003544 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003545 memcpy(str, type, len);
3546 str[len] = '\0';
3547
3548 *key = str;
3549 return ++p;
3550}
3551
bellard9307c4c2004-04-04 12:57:25 +00003552static int default_fmt_format = 'x';
3553static int default_fmt_size = 4;
3554
3555#define MAX_ARGS 16
3556
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003557static int is_valid_option(const char *c, const char *typestr)
3558{
3559 char option[3];
3560
3561 option[0] = '-';
3562 option[1] = *c;
3563 option[2] = '\0';
3564
3565 typestr = strstr(typestr, option);
3566 return (typestr != NULL);
3567}
3568
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003569static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3570 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003571{
3572 const mon_cmd_t *cmd;
3573
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003574 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003575 if (compare_cmd(cmdname, cmd->name)) {
3576 return cmd;
3577 }
3578 }
3579
3580 return NULL;
3581}
3582
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003583static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3584{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003585 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003586}
3587
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003588/*
3589 * Parse @cmdline according to command table @table.
3590 * If @cmdline is blank, return NULL.
3591 * If it can't be parsed, report to @mon, and return NULL.
3592 * Else, insert command arguments into @qdict, and return the command.
Peter Maydell085d8132013-03-18 17:20:07 +00003593 * If a sub-command table exists, and if @cmdline contains an additional string
3594 * for a sub-command, this function will try to search the sub-command table.
3595 * If no additional string for a sub-command is present, this function will
3596 * return the command found in @table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003597 * Do not assume the returned command points into @table! It doesn't
3598 * when the command is a sub-command.
3599 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003600static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003601 const char *cmdline,
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003602 int start,
3603 mon_cmd_t *table,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003604 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003605{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003606 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003607 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003608 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003609 char cmdname[256];
3610 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003611 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003612
3613#ifdef DEBUG
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003614 monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start);
bellard9dc39cb2004-03-14 21:38:27 +00003615#endif
ths3b46e622007-09-17 08:09:54 +00003616
bellard9307c4c2004-04-04 12:57:25 +00003617 /* extract the command name */
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003618 p = get_command_name(cmdline + start, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003619 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003620 return NULL;
ths3b46e622007-09-17 08:09:54 +00003621
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003622 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003623 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003624 monitor_printf(mon, "unknown command: '%.*s'\n",
3625 (int)(p - cmdline), cmdline);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003626 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003627 }
bellard9307c4c2004-04-04 12:57:25 +00003628
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003629 /* filter out following useless space */
3630 while (qemu_isspace(*p)) {
3631 p++;
3632 }
3633 /* search sub command */
3634 if (cmd->sub_table != NULL) {
3635 /* check if user set additional command */
3636 if (*p == '\0') {
3637 return cmd;
3638 }
3639 return monitor_parse_command(mon, cmdline, p - cmdline,
3640 cmd->sub_table, qdict);
3641 }
3642
bellard9307c4c2004-04-04 12:57:25 +00003643 /* parse the parameters */
3644 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003645 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003646 typestr = key_get_info(typestr, &key);
3647 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003648 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003649 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003650 typestr++;
3651 switch(c) {
3652 case 'F':
bellard81d09122004-07-14 17:21:37 +00003653 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003654 case 's':
3655 {
3656 int ret;
ths3b46e622007-09-17 08:09:54 +00003657
blueswir1cd390082008-11-16 13:53:32 +00003658 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003659 p++;
3660 if (*typestr == '?') {
3661 typestr++;
3662 if (*p == '\0') {
3663 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003664 break;
bellard9307c4c2004-04-04 12:57:25 +00003665 }
3666 }
3667 ret = get_str(buf, sizeof(buf), &p);
3668 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003669 switch(c) {
3670 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003671 monitor_printf(mon, "%s: filename expected\n",
3672 cmdname);
bellard81d09122004-07-14 17:21:37 +00003673 break;
3674 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003675 monitor_printf(mon, "%s: block device name expected\n",
3676 cmdname);
bellard81d09122004-07-14 17:21:37 +00003677 break;
3678 default:
aliguori376253e2009-03-05 23:01:23 +00003679 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003680 break;
3681 }
bellard9307c4c2004-04-04 12:57:25 +00003682 goto fail;
3683 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003684 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003685 }
3686 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003687 case 'O':
3688 {
3689 QemuOptsList *opts_list;
3690 QemuOpts *opts;
3691
3692 opts_list = qemu_find_opts(key);
3693 if (!opts_list || opts_list->desc->name) {
3694 goto bad_type;
3695 }
3696 while (qemu_isspace(*p)) {
3697 p++;
3698 }
3699 if (!*p)
3700 break;
3701 if (get_str(buf, sizeof(buf), &p) < 0) {
3702 goto fail;
3703 }
3704 opts = qemu_opts_parse(opts_list, buf, 1);
3705 if (!opts) {
3706 goto fail;
3707 }
3708 qemu_opts_to_qdict(opts, qdict);
3709 qemu_opts_del(opts);
3710 }
3711 break;
bellard9307c4c2004-04-04 12:57:25 +00003712 case '/':
3713 {
3714 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003715
blueswir1cd390082008-11-16 13:53:32 +00003716 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003717 p++;
3718 if (*p == '/') {
3719 /* format found */
3720 p++;
3721 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003722 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003723 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003724 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003725 count = count * 10 + (*p - '0');
3726 p++;
3727 }
3728 }
3729 size = -1;
3730 format = -1;
3731 for(;;) {
3732 switch(*p) {
3733 case 'o':
3734 case 'd':
3735 case 'u':
3736 case 'x':
3737 case 'i':
3738 case 'c':
3739 format = *p++;
3740 break;
3741 case 'b':
3742 size = 1;
3743 p++;
3744 break;
3745 case 'h':
3746 size = 2;
3747 p++;
3748 break;
3749 case 'w':
3750 size = 4;
3751 p++;
3752 break;
3753 case 'g':
3754 case 'L':
3755 size = 8;
3756 p++;
3757 break;
3758 default:
3759 goto next;
3760 }
3761 }
3762 next:
blueswir1cd390082008-11-16 13:53:32 +00003763 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003764 monitor_printf(mon, "invalid char in format: '%c'\n",
3765 *p);
bellard9307c4c2004-04-04 12:57:25 +00003766 goto fail;
3767 }
bellard9307c4c2004-04-04 12:57:25 +00003768 if (format < 0)
3769 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003770 if (format != 'i') {
3771 /* for 'i', not specifying a size gives -1 as size */
3772 if (size < 0)
3773 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003774 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003775 }
bellard9307c4c2004-04-04 12:57:25 +00003776 default_fmt_format = format;
3777 } else {
3778 count = 1;
3779 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003780 if (format != 'i') {
3781 size = default_fmt_size;
3782 } else {
3783 size = -1;
3784 }
bellard9307c4c2004-04-04 12:57:25 +00003785 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003786 qdict_put(qdict, "count", qint_from_int(count));
3787 qdict_put(qdict, "format", qint_from_int(format));
3788 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003789 }
3790 break;
3791 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003792 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003793 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003794 {
blueswir1c2efc952007-09-25 17:28:42 +00003795 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003796
blueswir1cd390082008-11-16 13:53:32 +00003797 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003798 p++;
bellard34405572004-06-08 00:55:58 +00003799 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003800 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003801 if (*p == '\0') {
3802 typestr++;
3803 break;
3804 }
bellard34405572004-06-08 00:55:58 +00003805 } else {
3806 if (*p == '.') {
3807 p++;
blueswir1cd390082008-11-16 13:53:32 +00003808 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003809 p++;
bellard34405572004-06-08 00:55:58 +00003810 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003811 typestr++;
3812 break;
bellard34405572004-06-08 00:55:58 +00003813 }
3814 }
bellard13224a82006-07-14 22:03:35 +00003815 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003816 }
aliguori376253e2009-03-05 23:01:23 +00003817 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003818 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003819 /* Check if 'i' is greater than 32-bit */
3820 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3821 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3822 monitor_printf(mon, "integer is for 32-bit values\n");
3823 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003824 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003825 if (val < 0) {
3826 monitor_printf(mon, "enter a positive value\n");
3827 goto fail;
3828 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003829 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003830 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003831 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003832 }
3833 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003834 case 'o':
3835 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003836 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003837 char *end;
3838
3839 while (qemu_isspace(*p)) {
3840 p++;
3841 }
3842 if (*typestr == '?') {
3843 typestr++;
3844 if (*p == '\0') {
3845 break;
3846 }
3847 }
3848 val = strtosz(p, &end);
3849 if (val < 0) {
3850 monitor_printf(mon, "invalid size\n");
3851 goto fail;
3852 }
3853 qdict_put(qdict, key, qint_from_int(val));
3854 p = end;
3855 }
3856 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003857 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003858 {
3859 double val;
3860
3861 while (qemu_isspace(*p))
3862 p++;
3863 if (*typestr == '?') {
3864 typestr++;
3865 if (*p == '\0') {
3866 break;
3867 }
3868 }
3869 if (get_double(mon, &val, &p) < 0) {
3870 goto fail;
3871 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003872 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003873 switch (*p) {
3874 case 'm':
3875 val /= 1e3; p += 2; break;
3876 case 'u':
3877 val /= 1e6; p += 2; break;
3878 case 'n':
3879 val /= 1e9; p += 2; break;
3880 }
3881 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003882 if (*p && !qemu_isspace(*p)) {
3883 monitor_printf(mon, "Unknown unit suffix\n");
3884 goto fail;
3885 }
3886 qdict_put(qdict, key, qfloat_from_double(val));
3887 }
3888 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003889 case 'b':
3890 {
3891 const char *beg;
3892 int val;
3893
3894 while (qemu_isspace(*p)) {
3895 p++;
3896 }
3897 beg = p;
3898 while (qemu_isgraph(*p)) {
3899 p++;
3900 }
3901 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3902 val = 1;
3903 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3904 val = 0;
3905 } else {
3906 monitor_printf(mon, "Expected 'on' or 'off'\n");
3907 goto fail;
3908 }
3909 qdict_put(qdict, key, qbool_from_int(val));
3910 }
3911 break;
bellard9307c4c2004-04-04 12:57:25 +00003912 case '-':
3913 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003914 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003915 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003916 /* option */
ths3b46e622007-09-17 08:09:54 +00003917
bellard9307c4c2004-04-04 12:57:25 +00003918 c = *typestr++;
3919 if (c == '\0')
3920 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003921 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003922 p++;
bellard9307c4c2004-04-04 12:57:25 +00003923 if (*p == '-') {
3924 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003925 if(c != *p) {
3926 if(!is_valid_option(p, typestr)) {
3927
3928 monitor_printf(mon, "%s: unsupported option -%c\n",
3929 cmdname, *p);
3930 goto fail;
3931 } else {
3932 skip_key = 1;
3933 }
bellard9307c4c2004-04-04 12:57:25 +00003934 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003935 if(skip_key) {
3936 p = tmp;
3937 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003938 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003939 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003940 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003941 }
bellard9307c4c2004-04-04 12:57:25 +00003942 }
bellard9307c4c2004-04-04 12:57:25 +00003943 }
3944 break;
3945 default:
3946 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003947 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003948 goto fail;
3949 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003950 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003951 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003952 }
3953 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003954 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003955 p++;
3956 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003957 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3958 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003959 goto fail;
3960 }
3961
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003962 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003963
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003964fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05003965 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003966 return NULL;
3967}
3968
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003969void monitor_set_error(Monitor *mon, QError *qerror)
3970{
3971 /* report only the first error */
3972 if (!mon->error) {
3973 mon->error = qerror;
3974 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003975 QDECREF(qerror);
3976 }
3977}
3978
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003979static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3980{
Luiz Capitulino6d441432010-11-22 16:35:09 -02003981 if (ret && !monitor_has_error(mon)) {
3982 /*
3983 * If it returns failure, it must have passed on error.
3984 *
3985 * Action: Report an internal error to the client if in QMP.
3986 */
3987 qerror_report(QERR_UNDEFINED_ERROR);
Luiz Capitulino6d441432010-11-22 16:35:09 -02003988 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003989}
3990
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003991static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003992{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003993 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003994 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003995
3996 qdict = qdict_new();
3997
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003998 cmd = monitor_parse_command(mon, cmdline, 0, mon_cmds, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003999 if (!cmd)
4000 goto out;
4001
Luiz Capitulino4903de02010-09-16 11:01:32 -03004002 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004003 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004004 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004005 QObject *data = NULL;
4006
4007 /* XXX: ignores the error code */
4008 cmd->mhandler.cmd_new(mon, qdict, &data);
4009 assert(!monitor_has_error(mon));
4010 if (data) {
4011 cmd->user_print(mon, data);
4012 qobject_decref(data);
4013 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004014 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004015 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004016 }
4017
Luiz Capitulino13917be2009-10-07 13:41:54 -03004018out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004019 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004020}
4021
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004022static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004023{
4024 const char *p, *pstart;
4025 char cmd[128];
4026 int len;
4027
4028 p = list;
4029 for(;;) {
4030 pstart = p;
4031 p = strchr(p, '|');
4032 if (!p)
4033 p = pstart + strlen(pstart);
4034 len = p - pstart;
4035 if (len > sizeof(cmd) - 2)
4036 len = sizeof(cmd) - 2;
4037 memcpy(cmd, pstart, len);
4038 cmd[len] = '\0';
4039 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004040 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004041 }
4042 if (*p == '\0')
4043 break;
4044 p++;
4045 }
4046}
4047
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004048static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004049{
4050 DIR *ffs;
4051 struct dirent *d;
4052 char path[1024];
4053 char file[1024], file_prefix[1024];
4054 int input_path_len;
4055 const char *p;
4056
ths5fafdf22007-09-16 21:08:06 +00004057 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004058 if (!p) {
4059 input_path_len = 0;
4060 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004061 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004062 } else {
4063 input_path_len = p - input + 1;
4064 memcpy(path, input, input_path_len);
4065 if (input_path_len > sizeof(path) - 1)
4066 input_path_len = sizeof(path) - 1;
4067 path[input_path_len] = '\0';
4068 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4069 }
4070#ifdef DEBUG_COMPLETION
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004071 monitor_printf(mon, "input='%s' path='%s' prefix='%s'\n",
aliguori376253e2009-03-05 23:01:23 +00004072 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004073#endif
4074 ffs = opendir(path);
4075 if (!ffs)
4076 return;
4077 for(;;) {
4078 struct stat sb;
4079 d = readdir(ffs);
4080 if (!d)
4081 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004082
4083 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4084 continue;
4085 }
4086
bellard81d09122004-07-14 17:21:37 +00004087 if (strstart(d->d_name, file_prefix, NULL)) {
4088 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004089 if (input_path_len < sizeof(file))
4090 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4091 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004092 /* stat the file to find out if it's a directory.
4093 * In that case add a slash to speed up typing long paths
4094 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004095 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004096 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004097 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004098 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004099 }
4100 }
4101 closedir(ffs);
4102}
4103
Wenchao Xia599a9262013-08-27 20:38:15 +08004104typedef struct MonitorBlockComplete {
4105 Monitor *mon;
4106 const char *input;
4107} MonitorBlockComplete;
4108
aliguori51de9762009-03-05 23:00:43 +00004109static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004110{
aliguori51de9762009-03-05 23:00:43 +00004111 const char *name = bdrv_get_device_name(bs);
Wenchao Xia599a9262013-08-27 20:38:15 +08004112 MonitorBlockComplete *mbc = opaque;
4113 Monitor *mon = mbc->mon;
4114 const char *input = mbc->input;
bellard81d09122004-07-14 17:21:37 +00004115
4116 if (input[0] == '\0' ||
4117 !strncmp(name, (char *)input, strlen(input))) {
Wenchao Xia599a9262013-08-27 20:38:15 +08004118 readline_add_completion(mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004119 }
4120}
4121
4122/* NOTE: this parser is an approximate form of the real command parser */
4123static void parse_cmdline(const char *cmdline,
4124 int *pnb_args, char **args)
4125{
4126 const char *p;
4127 int nb_args, ret;
4128 char buf[1024];
4129
4130 p = cmdline;
4131 nb_args = 0;
4132 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004133 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004134 p++;
4135 if (*p == '\0')
4136 break;
4137 if (nb_args >= MAX_ARGS)
4138 break;
4139 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004140 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004141 nb_args++;
4142 if (ret < 0)
4143 break;
4144 }
4145 *pnb_args = nb_args;
4146}
4147
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004148static const char *next_arg_type(const char *typestr)
4149{
4150 const char *p = strchr(typestr, ':');
4151 return (p != NULL ? ++p : typestr);
4152}
4153
Wenchao Xiad2674b22013-08-27 20:38:16 +08004154static void monitor_find_completion(Monitor *mon,
4155 const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004156{
4157 const char *cmdname;
4158 char *args[MAX_ARGS];
4159 int nb_args, i, len;
4160 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004161 const mon_cmd_t *cmd;
Wenchao Xia599a9262013-08-27 20:38:15 +08004162 MonitorBlockComplete mbs;
bellard81d09122004-07-14 17:21:37 +00004163
4164 parse_cmdline(cmdline, &nb_args, args);
4165#ifdef DEBUG_COMPLETION
Wenchao Xiad2674b22013-08-27 20:38:16 +08004166 for (i = 0; i < nb_args; i++) {
4167 monitor_printf(mon, "arg%d = '%s'\n", i, args[i]);
bellard81d09122004-07-14 17:21:37 +00004168 }
4169#endif
4170
4171 /* if the line ends with a space, it means we want to complete the
4172 next arg */
4173 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004174 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004175 if (nb_args >= MAX_ARGS) {
4176 goto cleanup;
4177 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004178 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004179 }
4180 if (nb_args <= 1) {
4181 /* command completion */
4182 if (nb_args == 0)
4183 cmdname = "";
4184 else
4185 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004186 readline_set_completion_index(mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004187 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004188 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00004189 }
4190 } else {
4191 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004192 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4193 if (compare_cmd(args[0], cmd->name)) {
4194 break;
4195 }
bellard81d09122004-07-14 17:21:37 +00004196 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004197 if (!cmd->name) {
4198 goto cleanup;
4199 }
4200
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004201 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004202 for(i = 0; i < nb_args - 2; i++) {
4203 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004204 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004205 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004206 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004207 }
4208 }
4209 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004210 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004211 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004212 }
bellard81d09122004-07-14 17:21:37 +00004213 switch(*ptype) {
4214 case 'F':
4215 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004216 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004217 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004218 break;
4219 case 'B':
4220 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004221 mbs.mon = mon;
4222 mbs.input = str;
4223 readline_set_completion_index(mon->rs, strlen(str));
4224 bdrv_iterate(block_completion_it, &mbs);
bellard81d09122004-07-14 17:21:37 +00004225 break;
bellard7fe48482004-10-09 18:08:01 +00004226 case 's':
4227 /* XXX: more generic ? */
4228 if (!strcmp(cmd->name, "info")) {
Wenchao Xiad2674b22013-08-27 20:38:16 +08004229 readline_set_completion_index(mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004230 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004231 cmd_completion(mon, str, cmd->name);
bellard7fe48482004-10-09 18:08:01 +00004232 }
bellard64866c32006-05-07 18:03:31 +00004233 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004234 char *sep = strrchr(str, '-');
4235 if (sep)
4236 str = sep + 1;
Wenchao Xiad2674b22013-08-27 20:38:16 +08004237 readline_set_completion_index(mon->rs, strlen(str));
Amos Kong1048c882012-08-31 10:56:25 +08004238 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004239 cmd_completion(mon, str, QKeyCode_lookup[i]);
bellard64866c32006-05-07 18:03:31 +00004240 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004241 } else if (!strcmp(cmd->name, "help|?")) {
Wenchao Xiad2674b22013-08-27 20:38:16 +08004242 readline_set_completion_index(mon->rs, strlen(str));
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004243 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004244 cmd_completion(mon, str, cmd->name);
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004245 }
bellard7fe48482004-10-09 18:08:01 +00004246 }
4247 break;
bellard81d09122004-07-14 17:21:37 +00004248 default:
4249 break;
4250 }
4251 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004252
4253cleanup:
4254 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004255 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004256 }
bellard81d09122004-07-14 17:21:37 +00004257}
4258
aliguori731b0362009-03-05 23:01:42 +00004259static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004260{
aliguori731b0362009-03-05 23:01:42 +00004261 Monitor *mon = opaque;
4262
Jan Kiszkac62313b2009-12-04 14:05:29 +01004263 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004264}
4265
Luiz Capitulino09069b12010-02-04 18:10:06 -02004266static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4267{
4268 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4269 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4270}
4271
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004272/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004273 * Argument validation rules:
4274 *
4275 * 1. The argument must exist in cmd_args qdict
4276 * 2. The argument type must be the expected one
4277 *
4278 * Special case: If the argument doesn't exist in cmd_args and
4279 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4280 * checking is skipped for it.
4281 */
4282static int check_client_args_type(const QDict *client_args,
4283 const QDict *cmd_args, int flags)
4284{
4285 const QDictEntry *ent;
4286
4287 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4288 QObject *obj;
4289 QString *arg_type;
4290 const QObject *client_arg = qdict_entry_value(ent);
4291 const char *client_arg_name = qdict_entry_key(ent);
4292
4293 obj = qdict_get(cmd_args, client_arg_name);
4294 if (!obj) {
4295 if (flags & QMP_ACCEPT_UNKNOWNS) {
4296 /* handler accepts unknowns */
4297 continue;
4298 }
4299 /* client arg doesn't exist */
4300 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4301 return -1;
4302 }
4303
4304 arg_type = qobject_to_qstring(obj);
4305 assert(arg_type != NULL);
4306
4307 /* check if argument's type is correct */
4308 switch (qstring_get_str(arg_type)[0]) {
4309 case 'F':
4310 case 'B':
4311 case 's':
4312 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4313 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4314 "string");
4315 return -1;
4316 }
4317 break;
4318 case 'i':
4319 case 'l':
4320 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004321 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004322 if (qobject_type(client_arg) != QTYPE_QINT) {
4323 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4324 "int");
4325 return -1;
4326 }
4327 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004328 case 'T':
4329 if (qobject_type(client_arg) != QTYPE_QINT &&
4330 qobject_type(client_arg) != QTYPE_QFLOAT) {
4331 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4332 "number");
4333 return -1;
4334 }
4335 break;
4336 case 'b':
4337 case '-':
4338 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4339 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4340 "bool");
4341 return -1;
4342 }
4343 break;
4344 case 'O':
4345 assert(flags & QMP_ACCEPT_UNKNOWNS);
4346 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004347 case 'q':
4348 /* Any QObject can be passed. */
4349 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004350 case '/':
4351 case '.':
4352 /*
4353 * These types are not supported by QMP and thus are not
4354 * handled here. Fall through.
4355 */
4356 default:
4357 abort();
4358 }
4359 }
4360
4361 return 0;
4362}
4363
4364/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004365 * - Check if the client has passed all mandatory args
4366 * - Set special flags for argument validation
4367 */
4368static int check_mandatory_args(const QDict *cmd_args,
4369 const QDict *client_args, int *flags)
4370{
4371 const QDictEntry *ent;
4372
4373 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4374 const char *cmd_arg_name = qdict_entry_key(ent);
4375 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4376 assert(type != NULL);
4377
4378 if (qstring_get_str(type)[0] == 'O') {
4379 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4380 *flags |= QMP_ACCEPT_UNKNOWNS;
4381 } else if (qstring_get_str(type)[0] != '-' &&
4382 qstring_get_str(type)[1] != '?' &&
4383 !qdict_haskey(client_args, cmd_arg_name)) {
4384 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4385 return -1;
4386 }
4387 }
4388
4389 return 0;
4390}
4391
4392static QDict *qdict_from_args_type(const char *args_type)
4393{
4394 int i;
4395 QDict *qdict;
4396 QString *key, *type, *cur_qs;
4397
4398 assert(args_type != NULL);
4399
4400 qdict = qdict_new();
4401
4402 if (args_type == NULL || args_type[0] == '\0') {
4403 /* no args, empty qdict */
4404 goto out;
4405 }
4406
4407 key = qstring_new();
4408 type = qstring_new();
4409
4410 cur_qs = key;
4411
4412 for (i = 0;; i++) {
4413 switch (args_type[i]) {
4414 case ',':
4415 case '\0':
4416 qdict_put(qdict, qstring_get_str(key), type);
4417 QDECREF(key);
4418 if (args_type[i] == '\0') {
4419 goto out;
4420 }
4421 type = qstring_new(); /* qdict has ref */
4422 cur_qs = key = qstring_new();
4423 break;
4424 case ':':
4425 cur_qs = type;
4426 break;
4427 default:
4428 qstring_append_chr(cur_qs, args_type[i]);
4429 break;
4430 }
4431 }
4432
4433out:
4434 return qdict;
4435}
4436
4437/*
4438 * Client argument checking rules:
4439 *
4440 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004441 * 2. Each argument provided by the client must be expected
4442 * 3. Each argument provided by the client must have the type expected
4443 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004444 */
4445static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4446{
4447 int flags, err;
4448 QDict *cmd_args;
4449
4450 cmd_args = qdict_from_args_type(cmd->args_type);
4451
4452 flags = 0;
4453 err = check_mandatory_args(cmd_args, client_args, &flags);
4454 if (err) {
4455 goto out;
4456 }
4457
Luiz Capitulino4af91932010-06-22 11:44:05 -03004458 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004459
4460out:
4461 QDECREF(cmd_args);
4462 return err;
4463}
4464
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004465/*
4466 * Input object checking rules
4467 *
4468 * 1. Input object must be a dict
4469 * 2. The "execute" key must exist
4470 * 3. The "execute" key must be a string
4471 * 4. If the "arguments" key exists, it must be a dict
4472 * 5. If the "id" key exists, it can be anything (ie. json-value)
4473 * 6. Any argument not listed above is considered invalid
4474 */
4475static QDict *qmp_check_input_obj(QObject *input_obj)
4476{
4477 const QDictEntry *ent;
4478 int has_exec_key = 0;
4479 QDict *input_dict;
4480
4481 if (qobject_type(input_obj) != QTYPE_QDICT) {
4482 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4483 return NULL;
4484 }
4485
4486 input_dict = qobject_to_qdict(input_obj);
4487
4488 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4489 const char *arg_name = qdict_entry_key(ent);
4490 const QObject *arg_obj = qdict_entry_value(ent);
4491
4492 if (!strcmp(arg_name, "execute")) {
4493 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4494 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4495 "string");
4496 return NULL;
4497 }
4498 has_exec_key = 1;
4499 } else if (!strcmp(arg_name, "arguments")) {
4500 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4501 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4502 "object");
4503 return NULL;
4504 }
4505 } else if (!strcmp(arg_name, "id")) {
4506 /* FIXME: check duplicated IDs for async commands */
4507 } else {
4508 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4509 return NULL;
4510 }
4511 }
4512
4513 if (!has_exec_key) {
4514 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4515 return NULL;
4516 }
4517
4518 return input_dict;
4519}
4520
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004521static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4522 const QDict *params)
4523{
4524 int ret;
4525 QObject *data = NULL;
4526
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004527 ret = cmd->mhandler.cmd_new(mon, params, &data);
4528 handler_audit(mon, cmd, ret);
4529 monitor_protocol_emitter(mon, data);
4530 qobject_decref(data);
4531}
4532
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004533static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4534{
4535 int err;
4536 QObject *obj;
4537 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004538 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004539 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004540 Monitor *mon = cur_mon;
4541
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004542 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004543
4544 obj = json_parser_parse(tokens, NULL);
4545 if (!obj) {
4546 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004547 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004548 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004549 }
4550
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004551 input = qmp_check_input_obj(obj);
4552 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004553 qobject_decref(obj);
4554 goto err_out;
4555 }
4556
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004557 mon->mc->id = qdict_get(input, "id");
4558 qobject_incref(mon->mc->id);
4559
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004560 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004561 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004562 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004563 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004564 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004565 }
4566
Anthony Liguorie3193602011-09-02 12:34:47 -05004567 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004568 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004569 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004570 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004571 }
4572
4573 obj = qdict_get(input, "arguments");
4574 if (!obj) {
4575 args = qdict_new();
4576 } else {
4577 args = qobject_to_qdict(obj);
4578 QINCREF(args);
4579 }
4580
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004581 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004582 if (err < 0) {
4583 goto err_out;
4584 }
4585
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004586 if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004587 err = qmp_async_cmd_handler(mon, cmd, args);
4588 if (err) {
4589 /* emit the error response */
4590 goto err_out;
4591 }
Adam Litke940cc302010-01-25 12:18:44 -06004592 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004593 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004594 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004595
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004596 goto out;
4597
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004598err_out:
4599 monitor_protocol_emitter(mon, NULL);
4600out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004601 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004602 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004603}
4604
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004605/**
4606 * monitor_control_read(): Read and handle QMP input
4607 */
4608static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4609{
4610 Monitor *old_mon = cur_mon;
4611
4612 cur_mon = opaque;
4613
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004614 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004615
4616 cur_mon = old_mon;
4617}
4618
aliguori731b0362009-03-05 23:01:42 +00004619static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004620{
aliguori731b0362009-03-05 23:01:42 +00004621 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004622 int i;
aliguori376253e2009-03-05 23:01:23 +00004623
aliguori731b0362009-03-05 23:01:42 +00004624 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004625
aliguoricde76ee2009-03-05 23:01:51 +00004626 if (cur_mon->rs) {
4627 for (i = 0; i < size; i++)
4628 readline_handle_byte(cur_mon->rs, buf[i]);
4629 } else {
4630 if (size == 0 || buf[size - 1] != 0)
4631 monitor_printf(cur_mon, "corrupted command\n");
4632 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004633 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004634 }
aliguori731b0362009-03-05 23:01:42 +00004635
4636 cur_mon = old_mon;
4637}
aliguorid8f44602008-10-06 13:52:44 +00004638
aliguori376253e2009-03-05 23:01:23 +00004639static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004640{
aliguori731b0362009-03-05 23:01:42 +00004641 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004642 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004643 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004644}
4645
aliguoricde76ee2009-03-05 23:01:51 +00004646int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004647{
aliguoricde76ee2009-03-05 23:01:51 +00004648 if (!mon->rs)
4649 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004650 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004651 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004652}
4653
aliguori376253e2009-03-05 23:01:23 +00004654void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004655{
aliguoricde76ee2009-03-05 23:01:51 +00004656 if (!mon->rs)
4657 return;
aliguori731b0362009-03-05 23:01:42 +00004658 if (--mon->suspend_cnt == 0)
4659 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004660}
4661
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004662static QObject *get_qmp_greeting(void)
4663{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004664 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004665
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004666 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004667 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4668}
4669
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004670/**
4671 * monitor_control_event(): Print QMP gretting
4672 */
4673static void monitor_control_event(void *opaque, int event)
4674{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004675 QObject *data;
4676 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004677
Luiz Capitulino47116d12010-02-08 17:01:30 -02004678 switch (event) {
4679 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004680 mon->mc->command_mode = 0;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004681 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004682 monitor_json_emitter(mon, data);
4683 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004684 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004685 break;
4686 case CHR_EVENT_CLOSED:
4687 json_message_parser_destroy(&mon->mc->parser);
Anthony Liguori58617a72012-08-23 08:03:21 -05004688 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004689 mon_refcount--;
4690 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004691 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004692 }
4693}
4694
aliguori731b0362009-03-05 23:01:42 +00004695static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004696{
aliguori376253e2009-03-05 23:01:23 +00004697 Monitor *mon = opaque;
4698
aliguori2724b182009-03-05 23:01:47 +00004699 switch (event) {
4700 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004701 mon->mux_out = 0;
4702 if (mon->reset_seen) {
4703 readline_restart(mon->rs);
4704 monitor_resume(mon);
4705 monitor_flush(mon);
4706 } else {
4707 mon->suspend_cnt = 0;
4708 }
aliguori2724b182009-03-05 23:01:47 +00004709 break;
ths86e94de2007-01-05 22:01:59 +00004710
aliguori2724b182009-03-05 23:01:47 +00004711 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004712 if (mon->reset_seen) {
4713 if (mon->suspend_cnt == 0) {
4714 monitor_printf(mon, "\n");
4715 }
4716 monitor_flush(mon);
4717 monitor_suspend(mon);
4718 } else {
4719 mon->suspend_cnt++;
4720 }
4721 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004722 break;
4723
Amit Shahb6b8df52009-10-07 18:31:16 +05304724 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004725 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4726 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004727 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004728 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004729 }
4730 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004731 mon_refcount++;
4732 break;
4733
4734 case CHR_EVENT_CLOSED:
4735 mon_refcount--;
4736 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004737 break;
4738 }
ths86e94de2007-01-05 22:01:59 +00004739}
4740
Wayne Xia816f8922011-10-12 11:32:41 +08004741static int
4742compare_mon_cmd(const void *a, const void *b)
4743{
4744 return strcmp(((const mon_cmd_t *)a)->name,
4745 ((const mon_cmd_t *)b)->name);
4746}
4747
4748static void sortcmdlist(void)
4749{
4750 int array_num;
4751 int elem_size = sizeof(mon_cmd_t);
4752
4753 array_num = sizeof(mon_cmds)/elem_size-1;
4754 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4755
4756 array_num = sizeof(info_cmds)/elem_size-1;
4757 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4758}
4759
aliguori76655d62009-03-06 20:27:37 +00004760
4761/*
4762 * Local variables:
4763 * c-indent-level: 4
4764 * c-basic-offset: 4
4765 * tab-width: 8
4766 * End:
4767 */
4768
aliguori731b0362009-03-05 23:01:42 +00004769void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004770{
aliguori731b0362009-03-05 23:01:42 +00004771 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004772 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004773
4774 if (is_first_init) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +01004775 monitor_protocol_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004776 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004777 is_first_init = 0;
4778 }
aliguori87127162009-03-05 23:01:29 +00004779
Wenchao Xiab01fe892013-08-27 20:38:19 +08004780 mon = g_malloc(sizeof(*mon));
4781 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004782
aliguori87127162009-03-05 23:01:29 +00004783 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004784 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004785 if (flags & MONITOR_USE_READLINE) {
4786 mon->rs = readline_init(mon, monitor_find_completion);
4787 monitor_read_command(mon, 0);
4788 }
aliguori87127162009-03-05 23:01:29 +00004789
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004790 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004791 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004792 /* Control mode requires special handlers */
4793 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4794 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004795 qemu_chr_fe_set_echo(chr, true);
Anthony Liguori26efaca2012-08-23 13:49:02 -05004796
4797 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004798 } else {
4799 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4800 monitor_event, mon);
4801 }
aliguori87127162009-03-05 23:01:29 +00004802
Blue Swirl72cf2d42009-09-12 07:36:22 +00004803 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004804 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4805 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004806}
4807
aliguori376253e2009-03-05 23:01:23 +00004808static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004809{
aliguoribb5fc202009-03-05 23:01:15 +00004810 BlockDriverState *bs = opaque;
4811 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004812
aliguoribb5fc202009-03-05 23:01:15 +00004813 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004814 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004815 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004816 }
aliguori731b0362009-03-05 23:01:42 +00004817 if (mon->password_completion_cb)
4818 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004819
aliguori731b0362009-03-05 23:01:42 +00004820 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004821}
aliguoric0f4ce72009-03-05 23:01:01 +00004822
Anthony Liguori7060b472011-09-02 12:34:50 -05004823ReadLineState *monitor_get_rs(Monitor *mon)
4824{
4825 return mon->rs;
4826}
4827
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004828int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4829 BlockDriverCompletionFunc *completion_cb,
4830 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004831{
aliguoricde76ee2009-03-05 23:01:51 +00004832 int err;
4833
aliguoribb5fc202009-03-05 23:01:15 +00004834 if (!bdrv_key_required(bs)) {
4835 if (completion_cb)
4836 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004837 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004838 }
aliguoric0f4ce72009-03-05 23:01:01 +00004839
Luiz Capitulino94171e12009-12-07 21:37:00 +01004840 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino903a8812011-12-13 17:18:30 -02004841 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
4842 bdrv_get_encrypted_filename(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004843 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004844 }
4845
aliguori376253e2009-03-05 23:01:23 +00004846 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4847 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004848
aliguori731b0362009-03-05 23:01:42 +00004849 mon->password_completion_cb = completion_cb;
4850 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004851
aliguoricde76ee2009-03-05 23:01:51 +00004852 err = monitor_read_password(mon, bdrv_password_cb, bs);
4853
4854 if (err && completion_cb)
4855 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004856
4857 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004858}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004859
4860int monitor_read_block_device_key(Monitor *mon, const char *device,
4861 BlockDriverCompletionFunc *completion_cb,
4862 void *opaque)
4863{
4864 BlockDriverState *bs;
4865
4866 bs = bdrv_find(device);
4867 if (!bs) {
4868 monitor_printf(mon, "Device not found %s\n", device);
4869 return -1;
4870 }
4871
4872 return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);
4873}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004874
4875QemuOptsList qemu_mon_opts = {
4876 .name = "mon",
4877 .implied_opt_name = "chardev",
4878 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4879 .desc = {
4880 {
4881 .name = "mode",
4882 .type = QEMU_OPT_STRING,
4883 },{
4884 .name = "chardev",
4885 .type = QEMU_OPT_STRING,
4886 },{
4887 .name = "default",
4888 .type = QEMU_OPT_BOOL,
4889 },{
4890 .name = "pretty",
4891 .type = QEMU_OPT_BOOL,
4892 },
4893 { /* end of list */ }
4894 },
4895};