blob: 1b5acf876dc7709cd1e87a144675cabcb522da85 [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"
29#include "hw/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020030#include "hw/pci/pci.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010031#include "hw/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 Bonzini927d4872012-12-17 18:20:05 +010036#include "char/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"
Stefan Bergerd1a0cf72013-02-27 12:47:49 -050050#include "tpm/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
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +020066#include "ui/qemu-spice.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010067#include "exec/memory.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050068#include "qmp-commands.h"
69#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010070#include "qemu/thread.h"
ths6a5bd302007-12-03 17:05:38 +000071
Jan Kiszka661f1922011-10-16 11:53:13 +020072/* for pic/irq_info */
73#if defined(TARGET_SPARC)
74#include "hw/sun4m.h"
75#endif
76#include "hw/lm32_pic.h"
77
bellard9dc39cb2004-03-14 21:38:27 +000078//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000079//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000080
bellard9307c4c2004-04-04 12:57:25 +000081/*
82 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000083 *
bellard9307c4c2004-04-04 12:57:25 +000084 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000085 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000086 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010087 * 'O' option string of the form NAME=VALUE,...
88 * parsed according to QemuOptsList given by its name
89 * Example: 'device:O' uses qemu_device_opts.
90 * Restriction: only lists with empty desc are supported
91 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000092 * 'i' 32 bit integer
93 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -030094 * 'M' Non-negative target long (32 or 64 bit), in user mode the
95 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +020096 * 'o' octets (aka bytes)
97 * user mode accepts an optional T, t, G, g, M, m, K, k
98 * suffix, which multiplies the value by 2^40 for
99 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
100 * M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +0100101 * 'T' double
102 * user mode accepts an optional ms, us, ns suffix,
103 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000104 * '/' optional gdb-like print format (like "/10x")
105 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300106 * '?' optional type (for all types, except '/')
107 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100108 * 'b' boolean
109 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300110 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000111 *
112 */
113
Adam Litke940cc302010-01-25 12:18:44 -0600114typedef struct MonitorCompletionData MonitorCompletionData;
115struct MonitorCompletionData {
116 Monitor *mon;
117 void (*user_print)(Monitor *mon, const QObject *data);
118};
119
Anthony Liguoric227f092009-10-01 16:12:16 -0500120typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000121 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000122 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000123 const char *params;
124 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300125 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300126 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300127 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200128 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600129 int (*cmd_async)(Monitor *mon, const QDict *params,
130 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300131 } mhandler;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200132 int flags;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800133 /* @sub_table is a list of 2nd level of commands. If it do not exist,
134 * mhandler should be used. If it exist, sub_table[?].mhandler should be
135 * used, and mhandler of 1st level plays the role of help function.
136 */
137 struct mon_cmd_t *sub_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500138} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000139
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100140/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500141typedef struct mon_fd_t mon_fd_t;
142struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100143 char *name;
144 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500145 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100146};
147
Corey Bryantba1c0482012-08-14 16:43:43 -0400148/* file descriptor associated with a file descriptor set */
149typedef struct MonFdsetFd MonFdsetFd;
150struct MonFdsetFd {
151 int fd;
152 bool removed;
153 char *opaque;
154 QLIST_ENTRY(MonFdsetFd) next;
155};
156
157/* file descriptor set containing fds passed via SCM_RIGHTS */
158typedef struct MonFdset MonFdset;
159struct MonFdset {
160 int64_t id;
161 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400162 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400163 QLIST_ENTRY(MonFdset) next;
164};
165
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200166typedef struct MonitorControl {
167 QObject *id;
168 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200169 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200170} MonitorControl;
171
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100172/*
173 * To prevent flooding clients, events can be throttled. The
174 * throttling is calculated globally, rather than per-Monitor
175 * instance.
176 */
177typedef struct MonitorEventState {
178 MonitorEvent event; /* Event being tracked */
179 int64_t rate; /* Period over which to throttle. 0 to disable */
180 int64_t last; /* Time at which event was last emitted */
181 QEMUTimer *timer; /* Timer for handling delayed events */
182 QObject *data; /* Event pending delayed dispatch */
183} MonitorEventState;
184
aliguori87127162009-03-05 23:01:29 +0000185struct Monitor {
186 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200187 int mux_out;
188 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000189 int flags;
190 int suspend_cnt;
191 uint8_t outbuf[1024];
192 int outbuf_index;
193 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200194 MonitorControl *mc;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100195 CPUArchState *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
aliguori376253e2009-03-05 23:01:23 +0000264void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000265{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200266 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500267 qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
aliguori731b0362009-03-05 23:01:42 +0000268 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000269 }
270}
271
272/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000273static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000274{
ths60fe76f2007-12-16 03:02:09 +0000275 char c;
aliguori731b0362009-03-05 23:01:42 +0000276
bellard7e2515e2004-08-01 21:52:19 +0000277 for(;;) {
Markus Armbruster51782342013-01-10 14:10:22 +0100278 assert(mon->outbuf_index < sizeof(mon->outbuf) - 1);
bellard7e2515e2004-08-01 21:52:19 +0000279 c = *str++;
280 if (c == '\0')
281 break;
bellard7ba12602006-07-14 20:26:42 +0000282 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000283 mon->outbuf[mon->outbuf_index++] = '\r';
284 mon->outbuf[mon->outbuf_index++] = c;
285 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
286 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000287 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000288 }
289}
290
aliguori376253e2009-03-05 23:01:23 +0000291void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000292{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200293 char buf[4096];
294
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200295 if (!mon)
296 return;
297
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200298 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200299 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200300 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200301
302 vsnprintf(buf, sizeof(buf), fmt, ap);
303 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000304}
305
aliguori376253e2009-03-05 23:01:23 +0000306void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000307{
308 va_list ap;
309 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000310 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000311 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000312}
313
aliguori376253e2009-03-05 23:01:23 +0000314void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000315{
316 int i;
317
318 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000319 switch (filename[i]) {
320 case ' ':
321 case '"':
322 case '\\':
323 monitor_printf(mon, "\\%c", filename[i]);
324 break;
325 case '\t':
326 monitor_printf(mon, "\\t");
327 break;
328 case '\r':
329 monitor_printf(mon, "\\r");
330 break;
331 case '\n':
332 monitor_printf(mon, "\\n");
333 break;
334 default:
335 monitor_printf(mon, "%c", filename[i]);
336 break;
337 }
thsfef30742006-12-22 14:11:32 +0000338 }
339}
340
Stefan Weil8b7968f2010-09-23 21:28:05 +0200341static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
342 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000343{
344 va_list ap;
345 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000346 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000347 va_end(ap);
348 return 0;
349}
350
Luiz Capitulino13c74252009-10-07 13:41:55 -0300351static void monitor_user_noop(Monitor *mon, const QObject *data) { }
352
Luiz Capitulino9e807212010-09-16 10:58:59 -0300353static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300354{
355 return cmd->user_print != NULL;
356}
357
Luiz Capitulino4903de02010-09-16 11:01:32 -0300358static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600359{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200360 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600361}
362
Luiz Capitulino8204a912009-11-18 23:05:31 -0200363static inline int monitor_has_error(const Monitor *mon)
364{
365 return mon->error != NULL;
366}
367
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200368static void monitor_json_emitter(Monitor *mon, const QObject *data)
369{
370 QString *json;
371
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200372 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
373 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200374 assert(json != NULL);
375
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200376 qstring_append_chr(json, '\n');
377 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200378
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200379 QDECREF(json);
380}
381
Luiz Capitulinode253f12012-07-27 16:18:16 -0300382static QDict *build_qmp_error_dict(const QError *err)
383{
384 QObject *obj;
385
386 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
387 ErrorClass_lookup[err->err_class],
388 qerror_human(err));
389
390 return qobject_to_qdict(obj);
391}
392
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200393static void monitor_protocol_emitter(Monitor *mon, QObject *data)
394{
395 QDict *qmp;
396
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100397 trace_monitor_protocol_emitter(mon);
398
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200399 if (!monitor_has_error(mon)) {
400 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300401 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200402 if (data) {
403 qobject_incref(data);
404 qdict_put_obj(qmp, "return", data);
405 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200406 /* return an empty QDict by default */
407 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200408 }
409 } else {
410 /* error response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300411 qmp = build_qmp_error_dict(mon->error);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200412 QDECREF(mon->error);
413 mon->error = NULL;
414 }
415
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200416 if (mon->mc->id) {
417 qdict_put_obj(qmp, "id", mon->mc->id);
418 mon->mc->id = NULL;
419 }
420
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200421 monitor_json_emitter(mon, QOBJECT(qmp));
422 QDECREF(qmp);
423}
424
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200425static void timestamp_put(QDict *qdict)
426{
427 int err;
428 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000429 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200430
Blue Swirld08d6f02009-12-04 18:06:39 +0000431 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200432 if (err < 0)
433 return;
434
435 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
436 "'microseconds': %" PRId64 " }",
437 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200438 qdict_put_obj(qdict, "timestamp", obj);
439}
440
Daniel P. Berrange48608532012-05-21 17:59:51 +0100441
442static const char *monitor_event_names[] = {
443 [QEVENT_SHUTDOWN] = "SHUTDOWN",
444 [QEVENT_RESET] = "RESET",
445 [QEVENT_POWERDOWN] = "POWERDOWN",
446 [QEVENT_STOP] = "STOP",
447 [QEVENT_RESUME] = "RESUME",
448 [QEVENT_VNC_CONNECTED] = "VNC_CONNECTED",
449 [QEVENT_VNC_INITIALIZED] = "VNC_INITIALIZED",
450 [QEVENT_VNC_DISCONNECTED] = "VNC_DISCONNECTED",
451 [QEVENT_BLOCK_IO_ERROR] = "BLOCK_IO_ERROR",
452 [QEVENT_RTC_CHANGE] = "RTC_CHANGE",
453 [QEVENT_WATCHDOG] = "WATCHDOG",
454 [QEVENT_SPICE_CONNECTED] = "SPICE_CONNECTED",
455 [QEVENT_SPICE_INITIALIZED] = "SPICE_INITIALIZED",
456 [QEVENT_SPICE_DISCONNECTED] = "SPICE_DISCONNECTED",
457 [QEVENT_BLOCK_JOB_COMPLETED] = "BLOCK_JOB_COMPLETED",
458 [QEVENT_BLOCK_JOB_CANCELLED] = "BLOCK_JOB_CANCELLED",
Paolo Bonzini32c81a42012-09-28 17:22:58 +0200459 [QEVENT_BLOCK_JOB_ERROR] = "BLOCK_JOB_ERROR",
Paolo Bonzinia66a2a32012-07-23 15:15:47 +0200460 [QEVENT_BLOCK_JOB_READY] = "BLOCK_JOB_READY",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100461 [QEVENT_DEVICE_TRAY_MOVED] = "DEVICE_TRAY_MOVED",
462 [QEVENT_SUSPEND] = "SUSPEND",
Luiz Capitulino25df49f2012-08-08 17:03:01 -0300463 [QEVENT_SUSPEND_DISK] = "SUSPEND_DISK",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100464 [QEVENT_WAKEUP] = "WAKEUP",
Daniel P. Berrange973603a2012-06-14 18:12:56 +0100465 [QEVENT_BALLOON_CHANGE] = "BALLOON_CHANGE",
Yonit Halperin2fdd16e2012-08-21 11:51:57 +0300466 [QEVENT_SPICE_MIGRATE_COMPLETED] = "SPICE_MIGRATE_COMPLETED",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100467};
468QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names) != QEVENT_MAX)
469
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100470MonitorEventState monitor_event_state[QEVENT_MAX];
471QemuMutex monitor_event_state_lock;
472
473/*
474 * Emits the event to every monitor instance
475 */
476static void
477monitor_protocol_event_emit(MonitorEvent event,
478 QObject *data)
479{
480 Monitor *mon;
481
482 trace_monitor_protocol_event_emit(event, data);
483 QLIST_FOREACH(mon, &mon_list, entry) {
484 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
485 monitor_json_emitter(mon, data);
486 }
487 }
488}
489
490
491/*
492 * Queue a new event for emission to Monitor instances,
493 * applying any rate limiting if required.
494 */
495static void
496monitor_protocol_event_queue(MonitorEvent event,
497 QObject *data)
498{
499 MonitorEventState *evstate;
500 int64_t now = qemu_get_clock_ns(rt_clock);
501 assert(event < QEVENT_MAX);
502
503 qemu_mutex_lock(&monitor_event_state_lock);
504 evstate = &(monitor_event_state[event]);
505 trace_monitor_protocol_event_queue(event,
506 data,
507 evstate->rate,
508 evstate->last,
509 now);
510
511 /* Rate limit of 0 indicates no throttling */
512 if (!evstate->rate) {
513 monitor_protocol_event_emit(event, data);
514 evstate->last = now;
515 } else {
516 int64_t delta = now - evstate->last;
517 if (evstate->data ||
518 delta < evstate->rate) {
519 /* If there's an existing event pending, replace
520 * it with the new event, otherwise schedule a
521 * timer for delayed emission
522 */
523 if (evstate->data) {
524 qobject_decref(evstate->data);
525 } else {
526 int64_t then = evstate->last + evstate->rate;
527 qemu_mod_timer_ns(evstate->timer, then);
528 }
529 evstate->data = data;
530 qobject_incref(evstate->data);
531 } else {
532 monitor_protocol_event_emit(event, data);
533 evstate->last = now;
534 }
535 }
536 qemu_mutex_unlock(&monitor_event_state_lock);
537}
538
539
540/*
541 * The callback invoked by QemuTimer when a delayed
542 * event is ready to be emitted
543 */
544static void monitor_protocol_event_handler(void *opaque)
545{
546 MonitorEventState *evstate = opaque;
547 int64_t now = qemu_get_clock_ns(rt_clock);
548
549 qemu_mutex_lock(&monitor_event_state_lock);
550
551 trace_monitor_protocol_event_handler(evstate->event,
552 evstate->data,
553 evstate->last,
554 now);
555 if (evstate->data) {
556 monitor_protocol_event_emit(evstate->event, evstate->data);
557 qobject_decref(evstate->data);
558 evstate->data = NULL;
559 }
560 evstate->last = now;
561 qemu_mutex_unlock(&monitor_event_state_lock);
562}
563
564
565/*
566 * @event: the event ID to be limited
567 * @rate: the rate limit in milliseconds
568 *
569 * Sets a rate limit on a particular event, so no
570 * more than 1 event will be emitted within @rate
571 * milliseconds
572 */
573static void
574monitor_protocol_event_throttle(MonitorEvent event,
575 int64_t rate)
576{
577 MonitorEventState *evstate;
578 assert(event < QEVENT_MAX);
579
580 evstate = &(monitor_event_state[event]);
581
582 trace_monitor_protocol_event_throttle(event, rate);
583 evstate->event = event;
584 evstate->rate = rate * SCALE_MS;
585 evstate->timer = qemu_new_timer(rt_clock,
586 SCALE_MS,
587 monitor_protocol_event_handler,
588 evstate);
589 evstate->last = 0;
590 evstate->data = NULL;
591}
592
593
594/* Global, one-time initializer to configure the rate limiting
595 * and initialize state */
596static void monitor_protocol_event_init(void)
597{
598 qemu_mutex_init(&monitor_event_state_lock);
599 /* Limit RTC & BALLOON events to 1 per second */
600 monitor_protocol_event_throttle(QEVENT_RTC_CHANGE, 1000);
601 monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE, 1000);
602 monitor_protocol_event_throttle(QEVENT_WATCHDOG, 1000);
603}
604
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200605/**
606 * monitor_protocol_event(): Generate a Monitor event
607 *
608 * Event-specific data can be emitted through the (optional) 'data' parameter.
609 */
610void monitor_protocol_event(MonitorEvent event, QObject *data)
611{
612 QDict *qmp;
613 const char *event_name;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200614
Blue Swirl242cd002009-12-04 18:05:45 +0000615 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200616
Daniel P. Berrange48608532012-05-21 17:59:51 +0100617 event_name = monitor_event_names[event];
618 assert(event_name != NULL);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200619
620 qmp = qdict_new();
621 timestamp_put(qmp);
622 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200623 if (data) {
624 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200625 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200626 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200627
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100628 trace_monitor_protocol_event(event, event_name, qmp);
629 monitor_protocol_event_queue(event, QOBJECT(qmp));
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200630 QDECREF(qmp);
631}
632
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200633static int do_qmp_capabilities(Monitor *mon, const QDict *params,
634 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200635{
636 /* Will setup QMP capabilities in the future */
637 if (monitor_ctrl_mode(mon)) {
638 mon->mc->command_mode = 1;
639 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200640
641 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200642}
643
Luiz Capitulino0268d972010-10-22 10:08:02 -0200644static void handle_user_command(Monitor *mon, const char *cmdline);
645
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200646char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
647 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200648{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200649 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200650 Monitor *old_mon, hmp;
651 CharDriverState mchar;
652
653 memset(&hmp, 0, sizeof(hmp));
654 qemu_chr_init_mem(&mchar);
655 hmp.chr = &mchar;
656
657 old_mon = cur_mon;
658 cur_mon = &hmp;
659
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200660 if (has_cpu_index) {
661 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200662 if (ret < 0) {
663 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200664 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
665 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200666 goto out;
667 }
668 }
669
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200670 handle_user_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200671 cur_mon = old_mon;
672
673 if (qemu_chr_mem_osize(hmp.chr) > 0) {
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200674 QString *str = qemu_chr_mem_to_qs(hmp.chr);
675 output = g_strdup(qstring_get_str(str));
676 QDECREF(str);
677 } else {
678 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200679 }
680
681out:
682 qemu_chr_close_mem(hmp.chr);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200683 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200684}
685
bellard9dc39cb2004-03-14 21:38:27 +0000686static int compare_cmd(const char *name, const char *list)
687{
688 const char *p, *pstart;
689 int len;
690 len = strlen(name);
691 p = list;
692 for(;;) {
693 pstart = p;
694 p = strchr(p, '|');
695 if (!p)
696 p = pstart + strlen(pstart);
697 if ((p - pstart) == len && !memcmp(pstart, name, len))
698 return 1;
699 if (*p == '\0')
700 break;
701 p++;
702 }
703 return 0;
704}
705
Anthony Liguoric227f092009-10-01 16:12:16 -0500706static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000707 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000708{
Anthony Liguoric227f092009-10-01 16:12:16 -0500709 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000710
711 for(cmd = cmds; cmd->name != NULL; cmd++) {
712 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000713 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
714 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000715 }
716}
717
aliguori376253e2009-03-05 23:01:23 +0000718static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000719{
720 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000721 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000722 } else {
aliguori376253e2009-03-05 23:01:23 +0000723 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000724 if (name && !strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000725 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000726 monitor_printf(mon, "Log items (comma separated):\n");
727 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000728 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000729 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000730 }
731 }
bellard9dc39cb2004-03-14 21:38:27 +0000732 }
733}
734
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300735static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300736{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300737 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300738}
739
Lluísfc764102011-08-31 20:31:18 +0200740static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530741{
742 const char *tp_name = qdict_get_str(qdict, "name");
743 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200744 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000745
746 if (!ret) {
747 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
748 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530749}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100750
Michael Rothc45a8162011-10-02 08:44:37 -0500751#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100752static void do_trace_file(Monitor *mon, const QDict *qdict)
753{
754 const char *op = qdict_get_try_str(qdict, "op");
755 const char *arg = qdict_get_try_str(qdict, "arg");
756
757 if (!op) {
758 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
759 } else if (!strcmp(op, "on")) {
760 st_set_trace_file_enabled(true);
761 } else if (!strcmp(op, "off")) {
762 st_set_trace_file_enabled(false);
763 } else if (!strcmp(op, "flush")) {
764 st_flush_trace_buffer();
765 } else if (!strcmp(op, "set")) {
766 if (arg) {
767 st_set_trace_file(arg);
768 }
769 } else {
770 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
771 help_cmd(mon, "trace-file");
772 }
773}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530774#endif
775
Adam Litke940cc302010-01-25 12:18:44 -0600776static void user_monitor_complete(void *opaque, QObject *ret_data)
777{
778 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
779
780 if (ret_data) {
781 data->user_print(data->mon, ret_data);
782 }
783 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500784 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600785}
786
787static void qmp_monitor_complete(void *opaque, QObject *ret_data)
788{
789 monitor_protocol_emitter(opaque, ret_data);
790}
791
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300792static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
793 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600794{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300795 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600796}
797
Adam Litke940cc302010-01-25 12:18:44 -0600798static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
799 const QDict *params)
800{
801 int ret;
802
Anthony Liguori7267c092011-08-20 22:09:37 -0500803 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600804 cb_data->mon = mon;
805 cb_data->user_print = cmd->user_print;
806 monitor_suspend(mon);
807 ret = cmd->mhandler.cmd_async(mon, params,
808 user_monitor_complete, cb_data);
809 if (ret < 0) {
810 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500811 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600812 }
813}
814
Wenchao Xia84c44612013-01-14 14:06:29 +0800815static void do_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000816{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300817 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000818}
819
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300820CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000821{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300822 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200823 const mon_cmd_t *cmd;
824
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300825 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200826 info = g_malloc0(sizeof(*info));
827 info->value = g_malloc0(sizeof(*info->value));
828 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200829
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300830 info->next = cmd_list;
831 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200832 }
833
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300834 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000835}
836
Daniel P. Berrange48608532012-05-21 17:59:51 +0100837EventInfoList *qmp_query_events(Error **errp)
838{
839 EventInfoList *info, *ev_list = NULL;
840 MonitorEvent e;
841
842 for (e = 0 ; e < QEVENT_MAX ; e++) {
843 const char *event_name = monitor_event_names[e];
844 assert(event_name != NULL);
845 info = g_malloc0(sizeof(*info));
846 info->value = g_malloc0(sizeof(*info->value));
847 info->value->name = g_strdup(event_name);
848
849 info->next = ev_list;
850 ev_list = info;
851 }
852
853 return ev_list;
854}
855
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300856/* set the current CPU defined by the user */
857int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000858{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100859 CPUArchState *env;
Andreas Färber55e5c282012-12-17 06:18:02 +0100860 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000861
Andreas Färber55e5c282012-12-17 06:18:02 +0100862 for (env = first_cpu; env != NULL; env = env->next_cpu) {
863 cpu = ENV_GET_CPU(env);
864 if (cpu->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000865 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000866 return 0;
867 }
868 }
869 return -1;
870}
871
Andreas Färber9349b4f2012-03-14 01:38:32 +0100872static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000873{
aliguori731b0362009-03-05 23:01:42 +0000874 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300875 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000876 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300877 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000878 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000879}
880
Luiz Capitulino99b77962011-10-24 10:53:44 -0200881int monitor_get_cpu_index(void)
882{
Andreas Färber55e5c282012-12-17 06:18:02 +0100883 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
884 return cpu->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200885}
886
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800887static void do_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000888{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100889 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000890 env = mon_get_cpu();
Peter Maydell6fd2a022012-10-05 15:04:43 +0100891 cpu_dump_state(env, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000892}
893
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800894static void do_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000895{
aliguori376253e2009-03-05 23:01:23 +0000896 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000897}
898
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800899static void do_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000900{
901 int i;
bellard7e2515e2004-08-01 21:52:19 +0000902 const char *str;
ths3b46e622007-09-17 08:09:54 +0000903
aliguoricde76ee2009-03-05 23:01:51 +0000904 if (!mon->rs)
905 return;
bellard7e2515e2004-08-01 21:52:19 +0000906 i = 0;
907 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000908 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000909 if (!str)
910 break;
aliguori376253e2009-03-05 23:01:23 +0000911 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000912 i++;
bellardaa455482004-04-04 13:07:25 +0000913 }
914}
915
j_mayer76a66252007-03-07 08:32:30 +0000916#if defined(TARGET_PPC)
917/* XXX: not implemented in other targets */
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800918static void do_info_cpu_stats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +0000919{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100920 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +0000921
922 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000923 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000924}
925#endif
926
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800927static void do_trace_print_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530928{
Lluísfc764102011-08-31 20:31:18 +0200929 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530930}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530931
Yonit Halperinedc5cb12011-10-17 10:03:18 +0200932static int client_migrate_info(Monitor *mon, const QDict *qdict,
933 MonitorCompletion cb, void *opaque)
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200934{
935 const char *protocol = qdict_get_str(qdict, "protocol");
936 const char *hostname = qdict_get_str(qdict, "hostname");
937 const char *subject = qdict_get_try_str(qdict, "cert-subject");
938 int port = qdict_get_try_int(qdict, "port", -1);
939 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
940 int ret;
941
942 if (strcmp(protocol, "spice") == 0) {
943 if (!using_spice) {
944 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
945 return -1;
946 }
947
Yonit Halperin6ec5dae2012-03-18 09:42:39 +0200948 if (port == -1 && tls_port == -1) {
949 qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
950 return -1;
951 }
952
Yonit Halperinedc5cb12011-10-17 10:03:18 +0200953 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
954 cb, opaque);
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200955 if (ret != 0) {
956 qerror_report(QERR_UNDEFINED_ERROR);
957 return -1;
958 }
959 return 0;
960 }
961
962 qerror_report(QERR_INVALID_PARAMETER, "protocol");
963 return -1;
964}
965
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300966static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000967{
Peter Maydell9a7e5422013-02-11 16:41:20 +0000968 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000969}
970
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300971static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000972{
973 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300974 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000975
bellard9307c4c2004-04-04 12:57:25 +0000976 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000977 mask = 0;
978 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +0000979 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000980 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000981 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000982 return;
983 }
984 }
Peter Maydell24537a02013-02-11 16:41:23 +0000985 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +0000986}
987
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300988static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000989{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300990 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000991 if (!option || !strcmp(option, "on")) {
992 singlestep = 1;
993 } else if (!strcmp(option, "off")) {
994 singlestep = 0;
995 } else {
996 monitor_printf(mon, "unexpected option %s\n", option);
997 }
998}
999
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001000static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001001{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001002 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001003 if (!device)
1004 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1005 if (gdbserver_start(device) < 0) {
1006 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1007 device);
1008 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001009 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001010 } else {
aliguori59030a82009-04-05 18:43:41 +00001011 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1012 device);
bellard8a7ddc32004-03-31 19:00:16 +00001013 }
1014}
1015
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001016static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001017{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001018 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001019 if (select_watchdog_action(action) == -1) {
1020 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1021 }
1022}
1023
aliguori376253e2009-03-05 23:01:23 +00001024static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001025{
aliguori376253e2009-03-05 23:01:23 +00001026 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001027 switch(c) {
1028 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001029 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001030 break;
1031 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001032 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001033 break;
1034 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001035 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001036 break;
1037 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001038 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001039 break;
1040 default:
1041 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001042 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001043 } else {
aliguori376253e2009-03-05 23:01:23 +00001044 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001045 }
1046 break;
1047 }
aliguori376253e2009-03-05 23:01:23 +00001048 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001049}
1050
aliguori376253e2009-03-05 23:01:23 +00001051static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001052 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001053{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001054 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001055 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001056 uint8_t buf[16];
1057 uint64_t v;
1058
1059 if (format == 'i') {
1060 int flags;
1061 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001062 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001063#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001064 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001065 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001066 } else if (wsize == 4) {
1067 flags = 0;
1068 } else {
bellard6a15fd12006-04-12 21:07:07 +00001069 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001070 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001071 if (env) {
1072#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001073 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001074 (env->segs[R_CS].flags & DESC_L_MASK))
1075 flags = 2;
1076 else
1077#endif
1078 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1079 flags = 1;
1080 }
bellard4c27ba22004-04-25 18:05:08 +00001081 }
1082#endif
aliguori376253e2009-03-05 23:01:23 +00001083 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001084 return;
1085 }
1086
1087 len = wsize * count;
1088 if (wsize == 1)
1089 line_size = 8;
1090 else
1091 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001092 max_digits = 0;
1093
1094 switch(format) {
1095 case 'o':
1096 max_digits = (wsize * 8 + 2) / 3;
1097 break;
1098 default:
1099 case 'x':
1100 max_digits = (wsize * 8) / 4;
1101 break;
1102 case 'u':
1103 case 'd':
1104 max_digits = (wsize * 8 * 10 + 32) / 33;
1105 break;
1106 case 'c':
1107 wsize = 1;
1108 break;
1109 }
1110
1111 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001112 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001113 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001114 else
aliguori376253e2009-03-05 23:01:23 +00001115 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001116 l = len;
1117 if (l > line_size)
1118 l = line_size;
1119 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001120 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001121 } else {
bellard6a00d602005-11-21 23:25:50 +00001122 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001123 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001124 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001125 break;
1126 }
bellard9307c4c2004-04-04 12:57:25 +00001127 }
ths5fafdf22007-09-16 21:08:06 +00001128 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001129 while (i < l) {
1130 switch(wsize) {
1131 default:
1132 case 1:
1133 v = ldub_raw(buf + i);
1134 break;
1135 case 2:
1136 v = lduw_raw(buf + i);
1137 break;
1138 case 4:
bellard92a31b12005-02-10 22:00:52 +00001139 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001140 break;
1141 case 8:
1142 v = ldq_raw(buf + i);
1143 break;
1144 }
aliguori376253e2009-03-05 23:01:23 +00001145 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001146 switch(format) {
1147 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001148 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001149 break;
1150 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001151 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001152 break;
1153 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001154 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001155 break;
1156 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001157 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001158 break;
1159 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001160 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001161 break;
1162 }
1163 i += wsize;
1164 }
aliguori376253e2009-03-05 23:01:23 +00001165 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001166 addr += l;
1167 len -= l;
1168 }
1169}
1170
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001171static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001172{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001173 int count = qdict_get_int(qdict, "count");
1174 int format = qdict_get_int(qdict, "format");
1175 int size = qdict_get_int(qdict, "size");
1176 target_long addr = qdict_get_int(qdict, "addr");
1177
aliguori376253e2009-03-05 23:01:23 +00001178 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001179}
1180
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001181static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001182{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001183 int count = qdict_get_int(qdict, "count");
1184 int format = qdict_get_int(qdict, "format");
1185 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001186 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001187
aliguori376253e2009-03-05 23:01:23 +00001188 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001189}
1190
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001191static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001192{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001193 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001194 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001195
bellard9307c4c2004-04-04 12:57:25 +00001196 switch(format) {
1197 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001198 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001199 break;
1200 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001201 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001202 break;
1203 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001204 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001205 break;
1206 default:
1207 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001208 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001209 break;
1210 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001211 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001212 break;
1213 }
aliguori376253e2009-03-05 23:01:23 +00001214 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001215}
1216
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001217static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001218{
1219 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001220 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001221 uint32_t start = qdict_get_int(qdict, "start");
1222 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001223
1224 sum = 0;
1225 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001226 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001227 /* BSD sum algorithm ('sum' Unix command) */
1228 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001229 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001230 }
aliguori376253e2009-03-05 23:01:23 +00001231 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001232}
1233
bellard13224a82006-07-14 22:03:35 +00001234static int mouse_button_state;
1235
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001236static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001237{
1238 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001239 const char *dx_str = qdict_get_str(qdict, "dx_str");
1240 const char *dy_str = qdict_get_str(qdict, "dy_str");
1241 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001242 dx = strtol(dx_str, NULL, 0);
1243 dy = strtol(dy_str, NULL, 0);
1244 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001245 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001246 dz = strtol(dz_str, NULL, 0);
1247 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1248}
1249
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001250static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001251{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001252 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001253 mouse_button_state = button_state;
1254 kbd_mouse_event(0, 0, 0, mouse_button_state);
1255}
1256
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001257static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001258{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001259 int size = qdict_get_int(qdict, "size");
1260 int addr = qdict_get_int(qdict, "addr");
1261 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001262 uint32_t val;
1263 int suffix;
1264
1265 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001266 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001267 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001268 addr++;
1269 }
1270 addr &= 0xffff;
1271
1272 switch(size) {
1273 default:
1274 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001275 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001276 suffix = 'b';
1277 break;
1278 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001279 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001280 suffix = 'w';
1281 break;
1282 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001283 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001284 suffix = 'l';
1285 break;
1286 }
aliguori376253e2009-03-05 23:01:23 +00001287 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1288 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001289}
bellarda3a91a32004-06-04 11:06:21 +00001290
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001291static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001292{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001293 int size = qdict_get_int(qdict, "size");
1294 int addr = qdict_get_int(qdict, "addr");
1295 int val = qdict_get_int(qdict, "val");
1296
Jan Kiszkaf1147842009-07-14 10:20:11 +02001297 addr &= IOPORTS_MASK;
1298
1299 switch (size) {
1300 default:
1301 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001302 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001303 break;
1304 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001305 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001306 break;
1307 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001308 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001309 break;
1310 }
1311}
1312
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001313static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001314{
1315 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001316 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001317
Jan Kiszka76e30d02009-07-02 00:19:02 +02001318 res = qemu_boot_set(bootdevice);
1319 if (res == 0) {
1320 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1321 } else if (res > 0) {
1322 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001323 } else {
aliguori376253e2009-03-05 23:01:23 +00001324 monitor_printf(mon, "no function defined to set boot device list for "
1325 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001326 }
1327}
1328
bellardb86bda52004-09-18 19:32:46 +00001329#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001330static void print_pte(Monitor *mon, hwaddr addr,
1331 hwaddr pte,
1332 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001333{
Blue Swirld65aaf32010-12-11 18:56:24 +00001334#ifdef TARGET_X86_64
1335 if (addr & (1ULL << 47)) {
1336 addr |= -1LL << 48;
1337 }
1338#endif
1339 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1340 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001341 addr,
1342 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001343 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001344 pte & PG_GLOBAL_MASK ? 'G' : '-',
1345 pte & PG_PSE_MASK ? 'P' : '-',
1346 pte & PG_DIRTY_MASK ? 'D' : '-',
1347 pte & PG_ACCESSED_MASK ? 'A' : '-',
1348 pte & PG_PCD_MASK ? 'C' : '-',
1349 pte & PG_PWT_MASK ? 'T' : '-',
1350 pte & PG_USER_MASK ? 'U' : '-',
1351 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001352}
1353
Andreas Färber9349b4f2012-03-14 01:38:32 +01001354static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001355{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001356 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001357 uint32_t pgd, pde, pte;
1358
bellardb86bda52004-09-18 19:32:46 +00001359 pgd = env->cr[3] & ~0xfff;
1360 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001361 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001362 pde = le32_to_cpu(pde);
1363 if (pde & PG_PRESENT_MASK) {
1364 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001365 /* 4M pages */
1366 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001367 } else {
1368 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001369 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001370 pte = le32_to_cpu(pte);
1371 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001372 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001373 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001374 ~0xfff);
1375 }
1376 }
1377 }
1378 }
1379 }
1380}
1381
Andreas Färber9349b4f2012-03-14 01:38:32 +01001382static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001383{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001384 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001385 uint64_t pdpe, pde, pte;
1386 uint64_t pdp_addr, pd_addr, pt_addr;
1387
1388 pdp_addr = env->cr[3] & ~0x1f;
1389 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001390 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001391 pdpe = le64_to_cpu(pdpe);
1392 if (pdpe & PG_PRESENT_MASK) {
1393 pd_addr = pdpe & 0x3fffffffff000ULL;
1394 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001395 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001396 pde = le64_to_cpu(pde);
1397 if (pde & PG_PRESENT_MASK) {
1398 if (pde & PG_PSE_MASK) {
1399 /* 2M pages with PAE, CR4.PSE is ignored */
1400 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001401 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001402 } else {
1403 pt_addr = pde & 0x3fffffffff000ULL;
1404 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001405 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001406 pte = le64_to_cpu(pte);
1407 if (pte & PG_PRESENT_MASK) {
1408 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1409 + (l3 << 12),
1410 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001411 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001412 }
1413 }
1414 }
1415 }
1416 }
1417 }
1418 }
1419}
1420
1421#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001422static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001423{
1424 uint64_t l1, l2, l3, l4;
1425 uint64_t pml4e, pdpe, pde, pte;
1426 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1427
1428 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1429 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001430 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001431 pml4e = le64_to_cpu(pml4e);
1432 if (pml4e & PG_PRESENT_MASK) {
1433 pdp_addr = pml4e & 0x3fffffffff000ULL;
1434 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001435 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001436 pdpe = le64_to_cpu(pdpe);
1437 if (pdpe & PG_PRESENT_MASK) {
1438 if (pdpe & PG_PSE_MASK) {
1439 /* 1G pages, CR4.PSE is ignored */
1440 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1441 0x3ffffc0000000ULL);
1442 } else {
1443 pd_addr = pdpe & 0x3fffffffff000ULL;
1444 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001445 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001446 pde = le64_to_cpu(pde);
1447 if (pde & PG_PRESENT_MASK) {
1448 if (pde & PG_PSE_MASK) {
1449 /* 2M pages, CR4.PSE is ignored */
1450 print_pte(mon, (l1 << 39) + (l2 << 30) +
1451 (l3 << 21), pde,
1452 0x3ffffffe00000ULL);
1453 } else {
1454 pt_addr = pde & 0x3fffffffff000ULL;
1455 for (l4 = 0; l4 < 512; l4++) {
1456 cpu_physical_memory_read(pt_addr
1457 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001458 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001459 pte = le64_to_cpu(pte);
1460 if (pte & PG_PRESENT_MASK) {
1461 print_pte(mon, (l1 << 39) +
1462 (l2 << 30) +
1463 (l3 << 21) + (l4 << 12),
1464 pte & ~PG_PSE_MASK,
1465 0x3fffffffff000ULL);
1466 }
1467 }
1468 }
1469 }
1470 }
1471 }
1472 }
1473 }
1474 }
1475 }
1476}
1477#endif
1478
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001479static void tlb_info(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001480{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001481 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001482
1483 env = mon_get_cpu();
1484
1485 if (!(env->cr[0] & CR0_PG_MASK)) {
1486 monitor_printf(mon, "PG disabled\n");
1487 return;
1488 }
1489 if (env->cr[4] & CR4_PAE_MASK) {
1490#ifdef TARGET_X86_64
1491 if (env->hflags & HF_LMA_MASK) {
1492 tlb_info_64(mon, env);
1493 } else
1494#endif
1495 {
1496 tlb_info_pae32(mon, env);
1497 }
1498 } else {
1499 tlb_info_32(mon, env);
1500 }
1501}
1502
Avi Kivitya8170e52012-10-23 12:30:10 +02001503static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001504 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001505 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001506{
bellard9746b152004-11-11 18:30:24 +00001507 int prot1;
1508 prot1 = *plast_prot;
1509 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001510 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001511 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1512 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001513 *pstart, end, end - *pstart,
1514 prot1 & PG_USER_MASK ? 'u' : '-',
1515 'r',
1516 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001517 }
1518 if (prot != 0)
1519 *pstart = end;
1520 else
1521 *pstart = -1;
1522 *plast_prot = prot;
1523 }
1524}
1525
Andreas Färber9349b4f2012-03-14 01:38:32 +01001526static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001527{
Austin Clementsb49ca722011-08-14 23:19:21 -04001528 unsigned int l1, l2;
1529 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001530 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001531 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001532
bellardb86bda52004-09-18 19:32:46 +00001533 pgd = env->cr[3] & ~0xfff;
1534 last_prot = 0;
1535 start = -1;
1536 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001537 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001538 pde = le32_to_cpu(pde);
1539 end = l1 << 22;
1540 if (pde & PG_PRESENT_MASK) {
1541 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1542 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001543 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001544 } else {
1545 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001546 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001547 pte = le32_to_cpu(pte);
1548 end = (l1 << 22) + (l2 << 12);
1549 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001550 prot = pte & pde &
1551 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001552 } else {
1553 prot = 0;
1554 }
aliguori376253e2009-03-05 23:01:23 +00001555 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001556 }
1557 }
1558 } else {
1559 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001560 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001561 }
1562 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001563 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001564 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001565}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001566
Andreas Färber9349b4f2012-03-14 01:38:32 +01001567static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001568{
Austin Clementsb49ca722011-08-14 23:19:21 -04001569 unsigned int l1, l2, l3;
1570 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001571 uint64_t pdpe, pde, pte;
1572 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001573 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001574
1575 pdp_addr = env->cr[3] & ~0x1f;
1576 last_prot = 0;
1577 start = -1;
1578 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001579 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001580 pdpe = le64_to_cpu(pdpe);
1581 end = l1 << 30;
1582 if (pdpe & PG_PRESENT_MASK) {
1583 pd_addr = pdpe & 0x3fffffffff000ULL;
1584 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001585 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001586 pde = le64_to_cpu(pde);
1587 end = (l1 << 30) + (l2 << 21);
1588 if (pde & PG_PRESENT_MASK) {
1589 if (pde & PG_PSE_MASK) {
1590 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1591 PG_PRESENT_MASK);
1592 mem_print(mon, &start, &last_prot, end, prot);
1593 } else {
1594 pt_addr = pde & 0x3fffffffff000ULL;
1595 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001596 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001597 pte = le64_to_cpu(pte);
1598 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1599 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001600 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1601 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001602 } else {
1603 prot = 0;
1604 }
1605 mem_print(mon, &start, &last_prot, end, prot);
1606 }
1607 }
1608 } else {
1609 prot = 0;
1610 mem_print(mon, &start, &last_prot, end, prot);
1611 }
1612 }
1613 } else {
1614 prot = 0;
1615 mem_print(mon, &start, &last_prot, end, prot);
1616 }
1617 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001618 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001619 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001620}
1621
1622
1623#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001624static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001625{
1626 int prot, last_prot;
1627 uint64_t l1, l2, l3, l4;
1628 uint64_t pml4e, pdpe, pde, pte;
1629 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1630
1631 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1632 last_prot = 0;
1633 start = -1;
1634 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001635 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001636 pml4e = le64_to_cpu(pml4e);
1637 end = l1 << 39;
1638 if (pml4e & PG_PRESENT_MASK) {
1639 pdp_addr = pml4e & 0x3fffffffff000ULL;
1640 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001641 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001642 pdpe = le64_to_cpu(pdpe);
1643 end = (l1 << 39) + (l2 << 30);
1644 if (pdpe & PG_PRESENT_MASK) {
1645 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001646 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1647 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001648 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001649 mem_print(mon, &start, &last_prot, end, prot);
1650 } else {
1651 pd_addr = pdpe & 0x3fffffffff000ULL;
1652 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001653 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001654 pde = le64_to_cpu(pde);
1655 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1656 if (pde & PG_PRESENT_MASK) {
1657 if (pde & PG_PSE_MASK) {
1658 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1659 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001660 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001661 mem_print(mon, &start, &last_prot, end, prot);
1662 } else {
1663 pt_addr = pde & 0x3fffffffff000ULL;
1664 for (l4 = 0; l4 < 512; l4++) {
1665 cpu_physical_memory_read(pt_addr
1666 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001667 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001668 pte = le64_to_cpu(pte);
1669 end = (l1 << 39) + (l2 << 30) +
1670 (l3 << 21) + (l4 << 12);
1671 if (pte & PG_PRESENT_MASK) {
1672 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1673 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001674 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001675 } else {
1676 prot = 0;
1677 }
1678 mem_print(mon, &start, &last_prot, end, prot);
1679 }
1680 }
1681 } else {
1682 prot = 0;
1683 mem_print(mon, &start, &last_prot, end, prot);
1684 }
1685 }
1686 }
1687 } else {
1688 prot = 0;
1689 mem_print(mon, &start, &last_prot, end, prot);
1690 }
1691 }
1692 } else {
1693 prot = 0;
1694 mem_print(mon, &start, &last_prot, end, prot);
1695 }
1696 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001697 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001698 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001699}
1700#endif
1701
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001702static void mem_info(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001703{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001704 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001705
1706 env = mon_get_cpu();
1707
1708 if (!(env->cr[0] & CR0_PG_MASK)) {
1709 monitor_printf(mon, "PG disabled\n");
1710 return;
1711 }
1712 if (env->cr[4] & CR4_PAE_MASK) {
1713#ifdef TARGET_X86_64
1714 if (env->hflags & HF_LMA_MASK) {
1715 mem_info_64(mon, env);
1716 } else
1717#endif
1718 {
1719 mem_info_pae32(mon, env);
1720 }
1721 } else {
1722 mem_info_32(mon, env);
1723 }
1724}
bellardb86bda52004-09-18 19:32:46 +00001725#endif
1726
aurel327c664e22009-03-03 06:12:22 +00001727#if defined(TARGET_SH4)
1728
aliguori376253e2009-03-05 23:01:23 +00001729static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001730{
aliguori376253e2009-03-05 23:01:23 +00001731 monitor_printf(mon, " tlb%i:\t"
1732 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1733 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1734 "dirty=%hhu writethrough=%hhu\n",
1735 idx,
1736 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1737 tlb->v, tlb->sh, tlb->c, tlb->pr,
1738 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001739}
1740
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001741static void tlb_info(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001742{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001743 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00001744 int i;
1745
aliguori376253e2009-03-05 23:01:23 +00001746 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001747 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001748 print_tlb (mon, i, &env->itlb[i]);
1749 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001750 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001751 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001752}
1753
1754#endif
1755
Max Filippov692f7372012-01-07 20:02:40 +04001756#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001757static void tlb_info(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001758{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001759 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00001760
1761 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1762}
1763#endif
1764
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001765static void do_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001766{
1767 mtree_info((fprintf_function)monitor_printf, mon);
1768}
1769
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001770static void do_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001771{
aliguorib28b6232009-04-22 20:20:29 +00001772 int i;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001773 CPUArchState *env;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001774 CPUState *cpu;
aliguori030ea372009-04-21 22:30:47 +00001775
1776 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1777 for (i = 0; i < nb_numa_nodes; i++) {
1778 monitor_printf(mon, "node %d cpus:", i);
1779 for (env = first_cpu; env != NULL; env = env->next_cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001780 cpu = ENV_GET_CPU(env);
1781 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001782 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001783 }
1784 }
1785 monitor_printf(mon, "\n");
1786 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1787 node_mem[i] >> 20);
1788 }
1789}
1790
bellard5f1ce942006-02-08 22:40:15 +00001791#ifdef CONFIG_PROFILER
1792
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001793int64_t qemu_time;
1794int64_t dev_time;
1795
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001796static void do_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001797{
1798 int64_t total;
1799 total = qemu_time;
1800 if (total == 0)
1801 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001802 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001803 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001804 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001805 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001806 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001807 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001808}
1809#else
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001810static void do_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001811{
aliguori376253e2009-03-05 23:01:23 +00001812 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001813}
1814#endif
1815
bellardec36b692006-07-16 18:57:03 +00001816/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001817static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001818
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001819static void do_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001820{
1821 int i;
1822 CaptureState *s;
1823
1824 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001825 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001826 s->ops.info (s->opaque);
1827 }
1828}
1829
Blue Swirl23130862009-06-06 08:22:04 +00001830#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001831static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001832{
1833 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001834 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001835 CaptureState *s;
1836
1837 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1838 if (i == n) {
1839 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001840 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001841 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001842 return;
1843 }
1844 }
1845}
1846
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001847static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001848{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001849 const char *path = qdict_get_str(qdict, "path");
1850 int has_freq = qdict_haskey(qdict, "freq");
1851 int freq = qdict_get_try_int(qdict, "freq", -1);
1852 int has_bits = qdict_haskey(qdict, "bits");
1853 int bits = qdict_get_try_int(qdict, "bits", -1);
1854 int has_channels = qdict_haskey(qdict, "nchannels");
1855 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001856 CaptureState *s;
1857
Anthony Liguori7267c092011-08-20 22:09:37 -05001858 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001859
1860 freq = has_freq ? freq : 44100;
1861 bits = has_bits ? bits : 16;
1862 nchannels = has_channels ? nchannels : 2;
1863
1864 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001865 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001866 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001867 return;
bellardec36b692006-07-16 18:57:03 +00001868 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001869 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001870}
1871#endif
1872
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001873static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001874{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001875 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001876
aliguori76655d62009-03-06 20:27:37 +00001877 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001878 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001879 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001880 return acl;
1881}
aliguori76655d62009-03-06 20:27:37 +00001882
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001883static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001884{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001885 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001886 qemu_acl *acl = find_acl(mon, aclname);
1887 qemu_acl_entry *entry;
1888 int i = 0;
1889
1890 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001891 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001892 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001893 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001894 i++;
1895 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001896 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001897 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001898 }
1899}
1900
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001901static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001902{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001903 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001904 qemu_acl *acl = find_acl(mon, aclname);
1905
1906 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001907 qemu_acl_reset(acl);
1908 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001909 }
1910}
aliguori76655d62009-03-06 20:27:37 +00001911
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001912static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001913{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001914 const char *aclname = qdict_get_str(qdict, "aclname");
1915 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001916 qemu_acl *acl = find_acl(mon, aclname);
1917
1918 if (acl) {
1919 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001920 acl->defaultDeny = 0;
1921 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001922 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001923 acl->defaultDeny = 1;
1924 monitor_printf(mon, "acl: policy set to 'deny'\n");
1925 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001926 monitor_printf(mon, "acl: unknown policy '%s', "
1927 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001928 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001929 }
1930}
aliguori76655d62009-03-06 20:27:37 +00001931
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001932static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001933{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001934 const char *aclname = qdict_get_str(qdict, "aclname");
1935 const char *match = qdict_get_str(qdict, "match");
1936 const char *policy = qdict_get_str(qdict, "policy");
1937 int has_index = qdict_haskey(qdict, "index");
1938 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001939 qemu_acl *acl = find_acl(mon, aclname);
1940 int deny, ret;
1941
1942 if (acl) {
1943 if (strcmp(policy, "allow") == 0) {
1944 deny = 0;
1945 } else if (strcmp(policy, "deny") == 0) {
1946 deny = 1;
1947 } else {
1948 monitor_printf(mon, "acl: unknown policy '%s', "
1949 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001950 return;
1951 }
aliguori28a76be2009-03-06 20:27:40 +00001952 if (has_index)
1953 ret = qemu_acl_insert(acl, deny, match, index);
1954 else
1955 ret = qemu_acl_append(acl, deny, match);
1956 if (ret < 0)
1957 monitor_printf(mon, "acl: unable to add acl entry\n");
1958 else
1959 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001960 }
1961}
aliguori76655d62009-03-06 20:27:37 +00001962
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001963static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001964{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001965 const char *aclname = qdict_get_str(qdict, "aclname");
1966 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001967 qemu_acl *acl = find_acl(mon, aclname);
1968 int ret;
aliguori76655d62009-03-06 20:27:37 +00001969
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001970 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001971 ret = qemu_acl_remove(acl, match);
1972 if (ret < 0)
1973 monitor_printf(mon, "acl: no matching acl entry\n");
1974 else
1975 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001976 }
1977}
1978
Huang Ying79c4f6b2009-06-23 10:05:14 +08001979#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001980static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001981{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02001982 X86CPU *cpu;
1983 CPUX86State *cenv;
Andreas Färber55e5c282012-12-17 06:18:02 +01001984 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001985 int cpu_index = qdict_get_int(qdict, "cpu_index");
1986 int bank = qdict_get_int(qdict, "bank");
1987 uint64_t status = qdict_get_int(qdict, "status");
1988 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1989 uint64_t addr = qdict_get_int(qdict, "addr");
1990 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01001991 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08001992
Jan Kiszka747461c2011-03-02 08:56:10 +01001993 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
1994 flags |= MCE_INJECT_BROADCAST;
1995 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09001996 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02001997 cpu = x86_env_get_cpu(cenv);
Andreas Färber55e5c282012-12-17 06:18:02 +01001998 cs = CPU(cpu);
1999 if (cs->cpu_index == cpu_index) {
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002000 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002001 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002002 break;
2003 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002004 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002005}
2006#endif
2007
Corey Bryant208c9d12012-06-22 14:36:09 -04002008void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002009{
Anthony Liguoric227f092009-10-01 16:12:16 -05002010 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002011 int fd;
2012
Corey Bryant208c9d12012-06-22 14:36:09 -04002013 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002014 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002015 error_set(errp, QERR_FD_NOT_SUPPLIED);
2016 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002017 }
2018
2019 if (qemu_isdigit(fdname[0])) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002020 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2021 "a name not starting with a digit");
2022 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002023 }
2024
Corey Bryant208c9d12012-06-22 14:36:09 -04002025 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002026 if (strcmp(monfd->name, fdname) != 0) {
2027 continue;
2028 }
2029
2030 close(monfd->fd);
2031 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002032 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002033 }
2034
Anthony Liguori7267c092011-08-20 22:09:37 -05002035 monfd = g_malloc0(sizeof(mon_fd_t));
2036 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002037 monfd->fd = fd;
2038
Corey Bryant208c9d12012-06-22 14:36:09 -04002039 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002040}
2041
Corey Bryant208c9d12012-06-22 14:36:09 -04002042void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002043{
Anthony Liguoric227f092009-10-01 16:12:16 -05002044 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002045
Corey Bryant208c9d12012-06-22 14:36:09 -04002046 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002047 if (strcmp(monfd->name, fdname) != 0) {
2048 continue;
2049 }
2050
Blue Swirl72cf2d42009-09-12 07:36:22 +00002051 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002052 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002053 g_free(monfd->name);
2054 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002055 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002056 }
2057
Corey Bryant208c9d12012-06-22 14:36:09 -04002058 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002059}
2060
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002061static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002062{
Luiz Capitulino13548692011-07-29 15:36:43 -03002063 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002064 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002065
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002066 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002067
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002068 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002069 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002070 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002071}
2072
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002073int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002074{
Anthony Liguoric227f092009-10-01 16:12:16 -05002075 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002076
Blue Swirl72cf2d42009-09-12 07:36:22 +00002077 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002078 int fd;
2079
2080 if (strcmp(monfd->name, fdname) != 0) {
2081 continue;
2082 }
2083
2084 fd = monfd->fd;
2085
2086 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002087 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002088 g_free(monfd->name);
2089 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002090
2091 return fd;
2092 }
2093
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002094 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002095 return -1;
2096}
2097
Corey Bryantba1c0482012-08-14 16:43:43 -04002098static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2099{
2100 MonFdsetFd *mon_fdset_fd;
2101 MonFdsetFd *mon_fdset_fd_next;
2102
2103 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002104 if ((mon_fdset_fd->removed ||
2105 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2106 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002107 close(mon_fdset_fd->fd);
2108 g_free(mon_fdset_fd->opaque);
2109 QLIST_REMOVE(mon_fdset_fd, next);
2110 g_free(mon_fdset_fd);
2111 }
2112 }
2113
Corey Bryantadb696f2012-08-14 16:43:47 -04002114 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002115 QLIST_REMOVE(mon_fdset, next);
2116 g_free(mon_fdset);
2117 }
2118}
2119
Corey Bryantefb87c12012-08-14 16:43:48 -04002120static void monitor_fdsets_cleanup(void)
2121{
2122 MonFdset *mon_fdset;
2123 MonFdset *mon_fdset_next;
2124
2125 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2126 monitor_fdset_cleanup(mon_fdset);
2127 }
2128}
2129
Corey Bryantba1c0482012-08-14 16:43:43 -04002130AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2131 const char *opaque, Error **errp)
2132{
2133 int fd;
2134 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002135 AddfdInfo *fdinfo;
2136
2137 fd = qemu_chr_fe_get_msgfd(mon->chr);
2138 if (fd == -1) {
2139 error_set(errp, QERR_FD_NOT_SUPPLIED);
2140 goto error;
2141 }
2142
Corey Bryante446f702012-10-18 15:19:32 -04002143 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2144 has_opaque, opaque, errp);
2145 if (fdinfo) {
2146 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002147 }
2148
Corey Bryantba1c0482012-08-14 16:43:43 -04002149error:
2150 if (fd != -1) {
2151 close(fd);
2152 }
2153 return NULL;
2154}
2155
2156void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2157{
2158 MonFdset *mon_fdset;
2159 MonFdsetFd *mon_fdset_fd;
2160 char fd_str[60];
2161
2162 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2163 if (mon_fdset->id != fdset_id) {
2164 continue;
2165 }
2166 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2167 if (has_fd) {
2168 if (mon_fdset_fd->fd != fd) {
2169 continue;
2170 }
2171 mon_fdset_fd->removed = true;
2172 break;
2173 } else {
2174 mon_fdset_fd->removed = true;
2175 }
2176 }
2177 if (has_fd && !mon_fdset_fd) {
2178 goto error;
2179 }
2180 monitor_fdset_cleanup(mon_fdset);
2181 return;
2182 }
2183
2184error:
2185 if (has_fd) {
2186 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2187 fdset_id, fd);
2188 } else {
2189 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2190 }
2191 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2192}
2193
2194FdsetInfoList *qmp_query_fdsets(Error **errp)
2195{
2196 MonFdset *mon_fdset;
2197 MonFdsetFd *mon_fdset_fd;
2198 FdsetInfoList *fdset_list = NULL;
2199
2200 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2201 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2202 FdsetFdInfoList *fdsetfd_list = NULL;
2203
2204 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2205 fdset_info->value->fdset_id = mon_fdset->id;
2206
2207 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2208 FdsetFdInfoList *fdsetfd_info;
2209
2210 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2211 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2212 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2213 if (mon_fdset_fd->opaque) {
2214 fdsetfd_info->value->has_opaque = true;
2215 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2216 } else {
2217 fdsetfd_info->value->has_opaque = false;
2218 }
2219
2220 fdsetfd_info->next = fdsetfd_list;
2221 fdsetfd_list = fdsetfd_info;
2222 }
2223
2224 fdset_info->value->fds = fdsetfd_list;
2225
2226 fdset_info->next = fdset_list;
2227 fdset_list = fdset_info;
2228 }
2229
2230 return fdset_list;
2231}
2232
Corey Bryante446f702012-10-18 15:19:32 -04002233AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2234 bool has_opaque, const char *opaque,
2235 Error **errp)
2236{
2237 MonFdset *mon_fdset = NULL;
2238 MonFdsetFd *mon_fdset_fd;
2239 AddfdInfo *fdinfo;
2240
2241 if (has_fdset_id) {
2242 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2243 /* Break if match found or match impossible due to ordering by ID */
2244 if (fdset_id <= mon_fdset->id) {
2245 if (fdset_id < mon_fdset->id) {
2246 mon_fdset = NULL;
2247 }
2248 break;
2249 }
2250 }
2251 }
2252
2253 if (mon_fdset == NULL) {
2254 int64_t fdset_id_prev = -1;
2255 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2256
2257 if (has_fdset_id) {
2258 if (fdset_id < 0) {
2259 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2260 "a non-negative value");
2261 return NULL;
2262 }
2263 /* Use specified fdset ID */
2264 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2265 mon_fdset_cur = mon_fdset;
2266 if (fdset_id < mon_fdset_cur->id) {
2267 break;
2268 }
2269 }
2270 } else {
2271 /* Use first available fdset ID */
2272 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2273 mon_fdset_cur = mon_fdset;
2274 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2275 fdset_id_prev = mon_fdset_cur->id;
2276 continue;
2277 }
2278 break;
2279 }
2280 }
2281
2282 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2283 if (has_fdset_id) {
2284 mon_fdset->id = fdset_id;
2285 } else {
2286 mon_fdset->id = fdset_id_prev + 1;
2287 }
2288
2289 /* The fdset list is ordered by fdset ID */
2290 if (!mon_fdset_cur) {
2291 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2292 } else if (mon_fdset->id < mon_fdset_cur->id) {
2293 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2294 } else {
2295 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2296 }
2297 }
2298
2299 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2300 mon_fdset_fd->fd = fd;
2301 mon_fdset_fd->removed = false;
2302 if (has_opaque) {
2303 mon_fdset_fd->opaque = g_strdup(opaque);
2304 }
2305 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2306
2307 fdinfo = g_malloc0(sizeof(*fdinfo));
2308 fdinfo->fdset_id = mon_fdset->id;
2309 fdinfo->fd = mon_fdset_fd->fd;
2310
2311 return fdinfo;
2312}
2313
Corey Bryantadb696f2012-08-14 16:43:47 -04002314int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2315{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002316#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002317 MonFdset *mon_fdset;
2318 MonFdsetFd *mon_fdset_fd;
2319 int mon_fd_flags;
2320
Corey Bryantadb696f2012-08-14 16:43:47 -04002321 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2322 if (mon_fdset->id != fdset_id) {
2323 continue;
2324 }
2325 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2326 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2327 if (mon_fd_flags == -1) {
2328 return -1;
2329 }
2330
2331 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2332 return mon_fdset_fd->fd;
2333 }
2334 }
2335 errno = EACCES;
2336 return -1;
2337 }
2338#endif
2339
2340 errno = ENOENT;
2341 return -1;
2342}
2343
2344int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2345{
2346 MonFdset *mon_fdset;
2347 MonFdsetFd *mon_fdset_fd_dup;
2348
2349 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2350 if (mon_fdset->id != fdset_id) {
2351 continue;
2352 }
2353 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2354 if (mon_fdset_fd_dup->fd == dup_fd) {
2355 return -1;
2356 }
2357 }
2358 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2359 mon_fdset_fd_dup->fd = dup_fd;
2360 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2361 return 0;
2362 }
2363 return -1;
2364}
2365
2366static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2367{
2368 MonFdset *mon_fdset;
2369 MonFdsetFd *mon_fdset_fd_dup;
2370
2371 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2372 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2373 if (mon_fdset_fd_dup->fd == dup_fd) {
2374 if (remove) {
2375 QLIST_REMOVE(mon_fdset_fd_dup, next);
2376 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2377 monitor_fdset_cleanup(mon_fdset);
2378 }
2379 }
2380 return mon_fdset->id;
2381 }
2382 }
2383 }
2384 return -1;
2385}
2386
2387int monitor_fdset_dup_fd_find(int dup_fd)
2388{
2389 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2390}
2391
2392int monitor_fdset_dup_fd_remove(int dup_fd)
2393{
2394 return monitor_fdset_dup_fd_find_remove(dup_fd, true);
2395}
2396
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002397int monitor_handle_fd_param(Monitor *mon, const char *fdname)
2398{
2399 int fd;
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002400 Error *local_err = NULL;
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002401
2402 if (!qemu_isdigit(fdname[0]) && mon) {
2403
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002404 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002405 if (fd == -1) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002406 qerror_report_err(local_err);
2407 error_free(local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002408 return -1;
2409 }
2410 } else {
2411 fd = qemu_parse_fd(fdname);
2412 }
2413
2414 return fd;
2415}
2416
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002417/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002418static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002419 {
2420 .name = "version",
2421 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002422 .params = "",
2423 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002424 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002425 },
2426 {
2427 .name = "network",
2428 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002429 .params = "",
2430 .help = "show the network state",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002431 .mhandler.cmd = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002432 },
2433 {
2434 .name = "chardev",
2435 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002436 .params = "",
2437 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002438 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002439 },
2440 {
2441 .name = "block",
2442 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002443 .params = "",
2444 .help = "show the block devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002445 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002446 },
2447 {
2448 .name = "blockstats",
2449 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002450 .params = "",
2451 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002452 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002453 },
2454 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002455 .name = "block-jobs",
2456 .args_type = "",
2457 .params = "",
2458 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002459 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002460 },
2461 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002462 .name = "registers",
2463 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002464 .params = "",
2465 .help = "show the cpu registers",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002466 .mhandler.cmd = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002467 },
2468 {
2469 .name = "cpus",
2470 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002471 .params = "",
2472 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002473 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002474 },
2475 {
2476 .name = "history",
2477 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002478 .params = "",
2479 .help = "show the command line history",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002480 .mhandler.cmd = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002481 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002482#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2483 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002484 {
2485 .name = "irq",
2486 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002487 .params = "",
2488 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002489#ifdef TARGET_SPARC
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002490 .mhandler.cmd = sun4m_irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002491#elif defined(TARGET_LM32)
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002492 .mhandler.cmd = lm32_irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002493#else
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002494 .mhandler.cmd = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002495#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002496 },
2497 {
2498 .name = "pic",
2499 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002500 .params = "",
2501 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002502#ifdef TARGET_SPARC
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002503 .mhandler.cmd = sun4m_pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002504#elif defined(TARGET_LM32)
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002505 .mhandler.cmd = lm32_do_pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002506#else
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002507 .mhandler.cmd = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002508#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002509 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002510#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002511 {
2512 .name = "pci",
2513 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002514 .params = "",
2515 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002516 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002517 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002518#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002519 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002520 {
2521 .name = "tlb",
2522 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002523 .params = "",
2524 .help = "show virtual to physical memory mappings",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002525 .mhandler.cmd = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002526 },
aurel327c664e22009-03-03 06:12:22 +00002527#endif
2528#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002529 {
2530 .name = "mem",
2531 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002532 .params = "",
2533 .help = "show the active virtual memory mappings",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002534 .mhandler.cmd = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002535 },
bellardb86bda52004-09-18 19:32:46 +00002536#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002537 {
Blue Swirl314e2982011-09-11 20:22:05 +00002538 .name = "mtree",
2539 .args_type = "",
2540 .params = "",
2541 .help = "show memory tree",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002542 .mhandler.cmd = do_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002543 },
2544 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002545 .name = "jit",
2546 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002547 .params = "",
2548 .help = "show dynamic compiler info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002549 .mhandler.cmd = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002550 },
2551 {
2552 .name = "kvm",
2553 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002554 .params = "",
2555 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002556 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002557 },
2558 {
2559 .name = "numa",
2560 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002561 .params = "",
2562 .help = "show NUMA information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002563 .mhandler.cmd = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002564 },
2565 {
2566 .name = "usb",
2567 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002568 .params = "",
2569 .help = "show guest USB devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002570 .mhandler.cmd = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002571 },
2572 {
2573 .name = "usbhost",
2574 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002575 .params = "",
2576 .help = "show host USB devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002577 .mhandler.cmd = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002578 },
2579 {
2580 .name = "profile",
2581 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002582 .params = "",
2583 .help = "show profiling information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002584 .mhandler.cmd = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002585 },
2586 {
2587 .name = "capture",
2588 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002589 .params = "",
2590 .help = "show capture information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002591 .mhandler.cmd = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002592 },
2593 {
2594 .name = "snapshots",
2595 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002596 .params = "",
2597 .help = "show the currently saved VM snapshots",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002598 .mhandler.cmd = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002599 },
2600 {
2601 .name = "status",
2602 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002603 .params = "",
2604 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002605 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002606 },
2607 {
2608 .name = "pcmcia",
2609 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002610 .params = "",
2611 .help = "show guest PCMCIA status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002612 .mhandler.cmd = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002613 },
2614 {
2615 .name = "mice",
2616 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002617 .params = "",
2618 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002619 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002620 },
2621 {
2622 .name = "vnc",
2623 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002624 .params = "",
2625 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002626 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002627 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002628#if defined(CONFIG_SPICE)
2629 {
2630 .name = "spice",
2631 .args_type = "",
2632 .params = "",
2633 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002634 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002635 },
2636#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002637 {
2638 .name = "name",
2639 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002640 .params = "",
2641 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002642 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002643 },
2644 {
2645 .name = "uuid",
2646 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002647 .params = "",
2648 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002649 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002650 },
j_mayer76a66252007-03-07 08:32:30 +00002651#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002652 {
2653 .name = "cpustats",
2654 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002655 .params = "",
2656 .help = "show CPU statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002657 .mhandler.cmd = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002658 },
j_mayer76a66252007-03-07 08:32:30 +00002659#endif
blueswir131a60e22007-10-26 18:42:59 +00002660#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002661 {
2662 .name = "usernet",
2663 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 .params = "",
2665 .help = "show user network stack connection states",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002666 .mhandler.cmd = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002667 },
blueswir131a60e22007-10-26 18:42:59 +00002668#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002669 {
2670 .name = "migrate",
2671 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002672 .params = "",
2673 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002674 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002675 },
2676 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002677 .name = "migrate_capabilities",
2678 .args_type = "",
2679 .params = "",
2680 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002681 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002682 },
2683 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002684 .name = "migrate_cache_size",
2685 .args_type = "",
2686 .params = "",
2687 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002688 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002689 },
2690 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002691 .name = "balloon",
2692 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002693 .params = "",
2694 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002695 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002696 },
2697 {
2698 .name = "qtree",
2699 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002700 .params = "",
2701 .help = "show device tree",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002702 .mhandler.cmd = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002703 },
2704 {
2705 .name = "qdm",
2706 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002707 .params = "",
2708 .help = "show qdev device model list",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002709 .mhandler.cmd = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002710 },
2711 {
2712 .name = "roms",
2713 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002714 .params = "",
2715 .help = "show roms",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002716 .mhandler.cmd = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002717 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302718 {
2719 .name = "trace-events",
2720 .args_type = "",
2721 .params = "",
2722 .help = "show available trace-events & their state",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002723 .mhandler.cmd = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302724 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002725 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002726 .name = "tpm",
2727 .args_type = "",
2728 .params = "",
2729 .help = "show the TPM device",
2730 .mhandler.cmd = hmp_info_tpm,
2731 },
2732 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002733 .name = NULL,
2734 },
bellard9dc39cb2004-03-14 21:38:27 +00002735};
2736
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002737/* mon_cmds and info_cmds would be sorted at runtime */
2738static mon_cmd_t mon_cmds[] = {
2739#include "hmp-commands.h"
2740 { NULL, NULL, },
2741};
2742
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002743static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002744#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002745 { /* NULL */ },
2746};
2747
bellard9307c4c2004-04-04 12:57:25 +00002748/*******************************************************************/
2749
2750static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002751static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002752
bellard92a31b12005-02-10 22:00:52 +00002753#define MD_TLONG 0
2754#define MD_I32 1
2755
bellard9307c4c2004-04-04 12:57:25 +00002756typedef struct MonitorDef {
2757 const char *name;
2758 int offset;
blueswir18662d652008-10-02 18:32:44 +00002759 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002760 int type;
bellard9307c4c2004-04-04 12:57:25 +00002761} MonitorDef;
2762
bellard57206fd2004-04-25 18:54:52 +00002763#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002764static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002765{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002766 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002767 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002768}
2769#endif
2770
bellarda541f292004-04-12 20:39:29 +00002771#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002772static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002773{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002774 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002775 unsigned int u;
2776 int i;
2777
2778 u = 0;
2779 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002780 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002781
2782 return u;
2783}
2784
blueswir18662d652008-10-02 18:32:44 +00002785static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002786{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002787 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002788 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002789}
2790
blueswir18662d652008-10-02 18:32:44 +00002791static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002792{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002793 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002794 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002795}
bellard9fddaa02004-05-21 12:59:32 +00002796
blueswir18662d652008-10-02 18:32:44 +00002797static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002798{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002799 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002800 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002801}
2802
blueswir18662d652008-10-02 18:32:44 +00002803static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002804{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002805 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002806 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002807}
2808
blueswir18662d652008-10-02 18:32:44 +00002809static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002810{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002811 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002812 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002813}
bellarda541f292004-04-12 20:39:29 +00002814#endif
2815
bellarde95c8d52004-09-30 22:22:08 +00002816#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002817#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002818static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002819{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002820 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002821
2822 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002823}
bellard7b936c02005-10-30 17:05:13 +00002824#endif
bellarde95c8d52004-09-30 22:22:08 +00002825
blueswir18662d652008-10-02 18:32:44 +00002826static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002827{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002828 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002829 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002830}
2831#endif
2832
blueswir18662d652008-10-02 18:32:44 +00002833static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002834#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002835
2836#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01002837 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2838 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2839 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002840
Andreas Färbere59d1672012-02-16 00:40:47 +01002841 { "eax", offsetof(CPUX86State, regs[0]) },
2842 { "ecx", offsetof(CPUX86State, regs[1]) },
2843 { "edx", offsetof(CPUX86State, regs[2]) },
2844 { "ebx", offsetof(CPUX86State, regs[3]) },
2845 { "esp|sp", offsetof(CPUX86State, regs[4]) },
2846 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
2847 { "esi", offsetof(CPUX86State, regs[6]) },
2848 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002849#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01002850 { "r8", offsetof(CPUX86State, regs[8]) },
2851 { "r9", offsetof(CPUX86State, regs[9]) },
2852 { "r10", offsetof(CPUX86State, regs[10]) },
2853 { "r11", offsetof(CPUX86State, regs[11]) },
2854 { "r12", offsetof(CPUX86State, regs[12]) },
2855 { "r13", offsetof(CPUX86State, regs[13]) },
2856 { "r14", offsetof(CPUX86State, regs[14]) },
2857 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00002858#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01002859 { "eflags", offsetof(CPUX86State, eflags) },
2860 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00002861 SEG("cs", R_CS)
2862 SEG("ds", R_DS)
2863 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002864 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002865 SEG("fs", R_FS)
2866 SEG("gs", R_GS)
2867 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002868#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002869 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01002870 { "r0", offsetof(CPUPPCState, gpr[0]) },
2871 { "r1", offsetof(CPUPPCState, gpr[1]) },
2872 { "r2", offsetof(CPUPPCState, gpr[2]) },
2873 { "r3", offsetof(CPUPPCState, gpr[3]) },
2874 { "r4", offsetof(CPUPPCState, gpr[4]) },
2875 { "r5", offsetof(CPUPPCState, gpr[5]) },
2876 { "r6", offsetof(CPUPPCState, gpr[6]) },
2877 { "r7", offsetof(CPUPPCState, gpr[7]) },
2878 { "r8", offsetof(CPUPPCState, gpr[8]) },
2879 { "r9", offsetof(CPUPPCState, gpr[9]) },
2880 { "r10", offsetof(CPUPPCState, gpr[10]) },
2881 { "r11", offsetof(CPUPPCState, gpr[11]) },
2882 { "r12", offsetof(CPUPPCState, gpr[12]) },
2883 { "r13", offsetof(CPUPPCState, gpr[13]) },
2884 { "r14", offsetof(CPUPPCState, gpr[14]) },
2885 { "r15", offsetof(CPUPPCState, gpr[15]) },
2886 { "r16", offsetof(CPUPPCState, gpr[16]) },
2887 { "r17", offsetof(CPUPPCState, gpr[17]) },
2888 { "r18", offsetof(CPUPPCState, gpr[18]) },
2889 { "r19", offsetof(CPUPPCState, gpr[19]) },
2890 { "r20", offsetof(CPUPPCState, gpr[20]) },
2891 { "r21", offsetof(CPUPPCState, gpr[21]) },
2892 { "r22", offsetof(CPUPPCState, gpr[22]) },
2893 { "r23", offsetof(CPUPPCState, gpr[23]) },
2894 { "r24", offsetof(CPUPPCState, gpr[24]) },
2895 { "r25", offsetof(CPUPPCState, gpr[25]) },
2896 { "r26", offsetof(CPUPPCState, gpr[26]) },
2897 { "r27", offsetof(CPUPPCState, gpr[27]) },
2898 { "r28", offsetof(CPUPPCState, gpr[28]) },
2899 { "r29", offsetof(CPUPPCState, gpr[29]) },
2900 { "r30", offsetof(CPUPPCState, gpr[30]) },
2901 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002902 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01002903 { "f0", offsetof(CPUPPCState, fpr[0]) },
2904 { "f1", offsetof(CPUPPCState, fpr[1]) },
2905 { "f2", offsetof(CPUPPCState, fpr[2]) },
2906 { "f3", offsetof(CPUPPCState, fpr[3]) },
2907 { "f4", offsetof(CPUPPCState, fpr[4]) },
2908 { "f5", offsetof(CPUPPCState, fpr[5]) },
2909 { "f6", offsetof(CPUPPCState, fpr[6]) },
2910 { "f7", offsetof(CPUPPCState, fpr[7]) },
2911 { "f8", offsetof(CPUPPCState, fpr[8]) },
2912 { "f9", offsetof(CPUPPCState, fpr[9]) },
2913 { "f10", offsetof(CPUPPCState, fpr[10]) },
2914 { "f11", offsetof(CPUPPCState, fpr[11]) },
2915 { "f12", offsetof(CPUPPCState, fpr[12]) },
2916 { "f13", offsetof(CPUPPCState, fpr[13]) },
2917 { "f14", offsetof(CPUPPCState, fpr[14]) },
2918 { "f15", offsetof(CPUPPCState, fpr[15]) },
2919 { "f16", offsetof(CPUPPCState, fpr[16]) },
2920 { "f17", offsetof(CPUPPCState, fpr[17]) },
2921 { "f18", offsetof(CPUPPCState, fpr[18]) },
2922 { "f19", offsetof(CPUPPCState, fpr[19]) },
2923 { "f20", offsetof(CPUPPCState, fpr[20]) },
2924 { "f21", offsetof(CPUPPCState, fpr[21]) },
2925 { "f22", offsetof(CPUPPCState, fpr[22]) },
2926 { "f23", offsetof(CPUPPCState, fpr[23]) },
2927 { "f24", offsetof(CPUPPCState, fpr[24]) },
2928 { "f25", offsetof(CPUPPCState, fpr[25]) },
2929 { "f26", offsetof(CPUPPCState, fpr[26]) },
2930 { "f27", offsetof(CPUPPCState, fpr[27]) },
2931 { "f28", offsetof(CPUPPCState, fpr[28]) },
2932 { "f29", offsetof(CPUPPCState, fpr[29]) },
2933 { "f30", offsetof(CPUPPCState, fpr[30]) },
2934 { "f31", offsetof(CPUPPCState, fpr[31]) },
2935 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00002936 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01002937 { "nip|pc", offsetof(CPUPPCState, nip) },
2938 { "lr", offsetof(CPUPPCState, lr) },
2939 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002940 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002941 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002942 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002943 { "msr", 0, &monitor_get_msr, },
2944 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002945 { "tbu", 0, &monitor_get_tbu, },
2946 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002947#if defined(TARGET_PPC64)
2948 /* Address space register */
Andreas Färbere59d1672012-02-16 00:40:47 +01002949 { "asr", offsetof(CPUPPCState, asr) },
j_mayerff937db2007-09-19 05:49:13 +00002950#endif
2951 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01002952 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
2953 { "sr0", offsetof(CPUPPCState, sr[0]) },
2954 { "sr1", offsetof(CPUPPCState, sr[1]) },
2955 { "sr2", offsetof(CPUPPCState, sr[2]) },
2956 { "sr3", offsetof(CPUPPCState, sr[3]) },
2957 { "sr4", offsetof(CPUPPCState, sr[4]) },
2958 { "sr5", offsetof(CPUPPCState, sr[5]) },
2959 { "sr6", offsetof(CPUPPCState, sr[6]) },
2960 { "sr7", offsetof(CPUPPCState, sr[7]) },
2961 { "sr8", offsetof(CPUPPCState, sr[8]) },
2962 { "sr9", offsetof(CPUPPCState, sr[9]) },
2963 { "sr10", offsetof(CPUPPCState, sr[10]) },
2964 { "sr11", offsetof(CPUPPCState, sr[11]) },
2965 { "sr12", offsetof(CPUPPCState, sr[12]) },
2966 { "sr13", offsetof(CPUPPCState, sr[13]) },
2967 { "sr14", offsetof(CPUPPCState, sr[14]) },
2968 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05002969 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01002970 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05002971
Andreas Färbere59d1672012-02-16 00:40:47 +01002972 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
2973 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
2974 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
2975 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
2976 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
2977 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
2978 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
2979 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
2980 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
2981 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
2982 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
2983 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
2984 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
2985 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
2986 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
2987 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
2988 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
2989 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
2990 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
2991 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
2992 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
2993 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
2994 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
2995 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
2996 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
2997 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
2998 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
2999 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3000 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3001 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3002 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3003 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3004 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3005 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3006 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3007 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3008 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3009 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3010 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3011 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3012 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3013 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3014 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3015 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3016 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3017 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3018 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3019 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3020 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3021 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3022 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3023 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3024 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3025 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3026 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3027 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3028 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3029 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3030 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3031 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3032 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3033 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3034 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3035 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3036 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3037 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003038
bellarde95c8d52004-09-30 22:22:08 +00003039#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003040 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3041 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3042 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3043 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3044 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3045 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3046 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3047 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003048 { "o0", 0, monitor_get_reg },
3049 { "o1", 1, monitor_get_reg },
3050 { "o2", 2, monitor_get_reg },
3051 { "o3", 3, monitor_get_reg },
3052 { "o4", 4, monitor_get_reg },
3053 { "o5", 5, monitor_get_reg },
3054 { "o6", 6, monitor_get_reg },
3055 { "o7", 7, monitor_get_reg },
3056 { "l0", 8, monitor_get_reg },
3057 { "l1", 9, monitor_get_reg },
3058 { "l2", 10, monitor_get_reg },
3059 { "l3", 11, monitor_get_reg },
3060 { "l4", 12, monitor_get_reg },
3061 { "l5", 13, monitor_get_reg },
3062 { "l6", 14, monitor_get_reg },
3063 { "l7", 15, monitor_get_reg },
3064 { "i0", 16, monitor_get_reg },
3065 { "i1", 17, monitor_get_reg },
3066 { "i2", 18, monitor_get_reg },
3067 { "i3", 19, monitor_get_reg },
3068 { "i4", 20, monitor_get_reg },
3069 { "i5", 21, monitor_get_reg },
3070 { "i6", 22, monitor_get_reg },
3071 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003072 { "pc", offsetof(CPUSPARCState, pc) },
3073 { "npc", offsetof(CPUSPARCState, npc) },
3074 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003075#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003076 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003077 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003078#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003079 { "tbr", offsetof(CPUSPARCState, tbr) },
3080 { "fsr", offsetof(CPUSPARCState, fsr) },
3081 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3082 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3083 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3084 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3085 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3086 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3087 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3088 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3089 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3090 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3091 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3092 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3093 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3094 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3095 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3096 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3097 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3098 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3099 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3100 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3101 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3102 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3103 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3104 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3105 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3106 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3107 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3108 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3109 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3110 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3111 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3112 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003113#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003114 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3115 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3116 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3117 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3118 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3119 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3120 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3121 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3122 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3123 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3124 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3125 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3126 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3127 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3128 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3129 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3130 { "asi", offsetof(CPUSPARCState, asi) },
3131 { "pstate", offsetof(CPUSPARCState, pstate) },
3132 { "cansave", offsetof(CPUSPARCState, cansave) },
3133 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3134 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3135 { "wstate", offsetof(CPUSPARCState, wstate) },
3136 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3137 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003138#endif
bellard9307c4c2004-04-04 12:57:25 +00003139#endif
3140 { NULL },
3141};
3142
aliguori376253e2009-03-05 23:01:23 +00003143static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003144{
aliguori376253e2009-03-05 23:01:23 +00003145 monitor_printf(mon, "%s\n", msg);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003146 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003147}
3148
Markus Armbruster09b94182010-01-20 13:07:30 +01003149/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003150static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003151{
blueswir18662d652008-10-02 18:32:44 +00003152 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003153 void *ptr;
3154
bellard9307c4c2004-04-04 12:57:25 +00003155 for(md = monitor_defs; md->name != NULL; md++) {
3156 if (compare_cmd(name, md->name)) {
3157 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003158 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003159 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003160 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003161 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003162 switch(md->type) {
3163 case MD_I32:
3164 *pval = *(int32_t *)ptr;
3165 break;
3166 case MD_TLONG:
3167 *pval = *(target_long *)ptr;
3168 break;
3169 default:
3170 *pval = 0;
3171 break;
3172 }
bellard9307c4c2004-04-04 12:57:25 +00003173 }
3174 return 0;
3175 }
3176 }
3177 return -1;
3178}
3179
3180static void next(void)
3181{
Blue Swirl660f11b2009-07-31 21:16:51 +00003182 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003183 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003184 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003185 pch++;
3186 }
3187}
3188
aliguori376253e2009-03-05 23:01:23 +00003189static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003190
aliguori376253e2009-03-05 23:01:23 +00003191static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003192{
blueswir1c2efc952007-09-25 17:28:42 +00003193 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003194 char *p;
bellard6a00d602005-11-21 23:25:50 +00003195 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003196
3197 switch(*pch) {
3198 case '+':
3199 next();
aliguori376253e2009-03-05 23:01:23 +00003200 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003201 break;
3202 case '-':
3203 next();
aliguori376253e2009-03-05 23:01:23 +00003204 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003205 break;
3206 case '~':
3207 next();
aliguori376253e2009-03-05 23:01:23 +00003208 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003209 break;
3210 case '(':
3211 next();
aliguori376253e2009-03-05 23:01:23 +00003212 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003213 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003214 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003215 }
3216 next();
3217 break;
bellard81d09122004-07-14 17:21:37 +00003218 case '\'':
3219 pch++;
3220 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003221 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003222 n = *pch;
3223 pch++;
3224 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003225 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003226 next();
3227 break;
bellard9307c4c2004-04-04 12:57:25 +00003228 case '$':
3229 {
3230 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003231 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003232
bellard9307c4c2004-04-04 12:57:25 +00003233 pch++;
3234 q = buf;
3235 while ((*pch >= 'a' && *pch <= 'z') ||
3236 (*pch >= 'A' && *pch <= 'Z') ||
3237 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003238 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003239 if ((q - buf) < sizeof(buf) - 1)
3240 *q++ = *pch;
3241 pch++;
3242 }
blueswir1cd390082008-11-16 13:53:32 +00003243 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003244 pch++;
3245 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003246 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003247 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003248 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003249 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003250 }
3251 break;
3252 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003253 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003254 n = 0;
3255 break;
3256 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003257 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003258 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003259 if (errno == ERANGE) {
3260 expr_error(mon, "number too large");
3261 }
bellard9307c4c2004-04-04 12:57:25 +00003262 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003263 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003264 }
3265 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003266 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003267 pch++;
3268 break;
3269 }
3270 return n;
3271}
3272
3273
aliguori376253e2009-03-05 23:01:23 +00003274static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003275{
blueswir1c2efc952007-09-25 17:28:42 +00003276 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003277 int op;
ths3b46e622007-09-17 08:09:54 +00003278
aliguori376253e2009-03-05 23:01:23 +00003279 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003280 for(;;) {
3281 op = *pch;
3282 if (op != '*' && op != '/' && op != '%')
3283 break;
3284 next();
aliguori376253e2009-03-05 23:01:23 +00003285 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003286 switch(op) {
3287 default:
3288 case '*':
3289 val *= val2;
3290 break;
3291 case '/':
3292 case '%':
ths5fafdf22007-09-16 21:08:06 +00003293 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003294 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003295 if (op == '/')
3296 val /= val2;
3297 else
3298 val %= val2;
3299 break;
3300 }
3301 }
3302 return val;
3303}
3304
aliguori376253e2009-03-05 23:01:23 +00003305static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003306{
blueswir1c2efc952007-09-25 17:28:42 +00003307 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003308 int op;
bellard9307c4c2004-04-04 12:57:25 +00003309
aliguori376253e2009-03-05 23:01:23 +00003310 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003311 for(;;) {
3312 op = *pch;
3313 if (op != '&' && op != '|' && op != '^')
3314 break;
3315 next();
aliguori376253e2009-03-05 23:01:23 +00003316 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003317 switch(op) {
3318 default:
3319 case '&':
3320 val &= val2;
3321 break;
3322 case '|':
3323 val |= val2;
3324 break;
3325 case '^':
3326 val ^= val2;
3327 break;
3328 }
3329 }
3330 return val;
3331}
3332
aliguori376253e2009-03-05 23:01:23 +00003333static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003334{
blueswir1c2efc952007-09-25 17:28:42 +00003335 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003336 int op;
bellard9307c4c2004-04-04 12:57:25 +00003337
aliguori376253e2009-03-05 23:01:23 +00003338 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003339 for(;;) {
3340 op = *pch;
3341 if (op != '+' && op != '-')
3342 break;
3343 next();
aliguori376253e2009-03-05 23:01:23 +00003344 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003345 if (op == '+')
3346 val += val2;
3347 else
3348 val -= val2;
3349 }
3350 return val;
3351}
3352
aliguori376253e2009-03-05 23:01:23 +00003353static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003354{
3355 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003356 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003357 *pp = pch;
3358 return -1;
3359 }
blueswir1cd390082008-11-16 13:53:32 +00003360 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003361 pch++;
aliguori376253e2009-03-05 23:01:23 +00003362 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003363 *pp = pch;
3364 return 0;
3365}
3366
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003367static int get_double(Monitor *mon, double *pval, const char **pp)
3368{
3369 const char *p = *pp;
3370 char *tailp;
3371 double d;
3372
3373 d = strtod(p, &tailp);
3374 if (tailp == p) {
3375 monitor_printf(mon, "Number expected\n");
3376 return -1;
3377 }
3378 if (d != d || d - d != 0) {
3379 /* NaN or infinity */
3380 monitor_printf(mon, "Bad number\n");
3381 return -1;
3382 }
3383 *pval = d;
3384 *pp = tailp;
3385 return 0;
3386}
3387
bellard9307c4c2004-04-04 12:57:25 +00003388static int get_str(char *buf, int buf_size, const char **pp)
3389{
3390 const char *p;
3391 char *q;
3392 int c;
3393
bellard81d09122004-07-14 17:21:37 +00003394 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003395 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003396 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003397 p++;
3398 if (*p == '\0') {
3399 fail:
bellard81d09122004-07-14 17:21:37 +00003400 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003401 *pp = p;
3402 return -1;
3403 }
bellard9307c4c2004-04-04 12:57:25 +00003404 if (*p == '\"') {
3405 p++;
3406 while (*p != '\0' && *p != '\"') {
3407 if (*p == '\\') {
3408 p++;
3409 c = *p++;
3410 switch(c) {
3411 case 'n':
3412 c = '\n';
3413 break;
3414 case 'r':
3415 c = '\r';
3416 break;
3417 case '\\':
3418 case '\'':
3419 case '\"':
3420 break;
3421 default:
3422 qemu_printf("unsupported escape code: '\\%c'\n", c);
3423 goto fail;
3424 }
3425 if ((q - buf) < buf_size - 1) {
3426 *q++ = c;
3427 }
3428 } else {
3429 if ((q - buf) < buf_size - 1) {
3430 *q++ = *p;
3431 }
3432 p++;
3433 }
3434 }
3435 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003436 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003437 goto fail;
3438 }
3439 p++;
3440 } else {
blueswir1cd390082008-11-16 13:53:32 +00003441 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003442 if ((q - buf) < buf_size - 1) {
3443 *q++ = *p;
3444 }
3445 p++;
3446 }
bellard9307c4c2004-04-04 12:57:25 +00003447 }
bellard81d09122004-07-14 17:21:37 +00003448 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003449 *pp = p;
3450 return 0;
3451}
3452
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003453/*
3454 * Store the command-name in cmdname, and return a pointer to
3455 * the remaining of the command string.
3456 */
3457static const char *get_command_name(const char *cmdline,
3458 char *cmdname, size_t nlen)
3459{
3460 size_t len;
3461 const char *p, *pstart;
3462
3463 p = cmdline;
3464 while (qemu_isspace(*p))
3465 p++;
3466 if (*p == '\0')
3467 return NULL;
3468 pstart = p;
3469 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3470 p++;
3471 len = p - pstart;
3472 if (len > nlen - 1)
3473 len = nlen - 1;
3474 memcpy(cmdname, pstart, len);
3475 cmdname[len] = '\0';
3476 return p;
3477}
3478
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003479/**
3480 * Read key of 'type' into 'key' and return the current
3481 * 'type' pointer.
3482 */
3483static char *key_get_info(const char *type, char **key)
3484{
3485 size_t len;
3486 char *p, *str;
3487
3488 if (*type == ',')
3489 type++;
3490
3491 p = strchr(type, ':');
3492 if (!p) {
3493 *key = NULL;
3494 return NULL;
3495 }
3496 len = p - type;
3497
Anthony Liguori7267c092011-08-20 22:09:37 -05003498 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003499 memcpy(str, type, len);
3500 str[len] = '\0';
3501
3502 *key = str;
3503 return ++p;
3504}
3505
bellard9307c4c2004-04-04 12:57:25 +00003506static int default_fmt_format = 'x';
3507static int default_fmt_size = 4;
3508
3509#define MAX_ARGS 16
3510
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003511static int is_valid_option(const char *c, const char *typestr)
3512{
3513 char option[3];
3514
3515 option[0] = '-';
3516 option[1] = *c;
3517 option[2] = '\0';
3518
3519 typestr = strstr(typestr, option);
3520 return (typestr != NULL);
3521}
3522
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003523static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3524 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003525{
3526 const mon_cmd_t *cmd;
3527
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003528 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003529 if (compare_cmd(cmdname, cmd->name)) {
3530 return cmd;
3531 }
3532 }
3533
3534 return NULL;
3535}
3536
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003537static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3538{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003539 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003540}
3541
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003542/*
3543 * Parse @cmdline according to command table @table.
3544 * If @cmdline is blank, return NULL.
3545 * If it can't be parsed, report to @mon, and return NULL.
3546 * Else, insert command arguments into @qdict, and return the command.
3547 * If sub-command table exist, and if @cmdline contains addtional string for
3548 * sub-command, this function will try search sub-command table. if no
3549 * addtional string for sub-command exist, this function will return the found
3550 * one in @table.
3551 * Do not assume the returned command points into @table! It doesn't
3552 * when the command is a sub-command.
3553 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003554static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003555 const char *cmdline,
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003556 int start,
3557 mon_cmd_t *table,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003558 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003559{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003560 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003561 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003562 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003563 char cmdname[256];
3564 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003565 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003566
3567#ifdef DEBUG
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003568 monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start);
bellard9dc39cb2004-03-14 21:38:27 +00003569#endif
ths3b46e622007-09-17 08:09:54 +00003570
bellard9307c4c2004-04-04 12:57:25 +00003571 /* extract the command name */
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003572 p = get_command_name(cmdline + start, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003573 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003574 return NULL;
ths3b46e622007-09-17 08:09:54 +00003575
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003576 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003577 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003578 monitor_printf(mon, "unknown command: '%.*s'\n",
3579 (int)(p - cmdline), cmdline);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003580 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003581 }
bellard9307c4c2004-04-04 12:57:25 +00003582
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003583 /* filter out following useless space */
3584 while (qemu_isspace(*p)) {
3585 p++;
3586 }
3587 /* search sub command */
3588 if (cmd->sub_table != NULL) {
3589 /* check if user set additional command */
3590 if (*p == '\0') {
3591 return cmd;
3592 }
3593 return monitor_parse_command(mon, cmdline, p - cmdline,
3594 cmd->sub_table, qdict);
3595 }
3596
bellard9307c4c2004-04-04 12:57:25 +00003597 /* parse the parameters */
3598 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003599 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003600 typestr = key_get_info(typestr, &key);
3601 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003602 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003603 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003604 typestr++;
3605 switch(c) {
3606 case 'F':
bellard81d09122004-07-14 17:21:37 +00003607 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003608 case 's':
3609 {
3610 int ret;
ths3b46e622007-09-17 08:09:54 +00003611
blueswir1cd390082008-11-16 13:53:32 +00003612 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003613 p++;
3614 if (*typestr == '?') {
3615 typestr++;
3616 if (*p == '\0') {
3617 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003618 break;
bellard9307c4c2004-04-04 12:57:25 +00003619 }
3620 }
3621 ret = get_str(buf, sizeof(buf), &p);
3622 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003623 switch(c) {
3624 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003625 monitor_printf(mon, "%s: filename expected\n",
3626 cmdname);
bellard81d09122004-07-14 17:21:37 +00003627 break;
3628 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003629 monitor_printf(mon, "%s: block device name expected\n",
3630 cmdname);
bellard81d09122004-07-14 17:21:37 +00003631 break;
3632 default:
aliguori376253e2009-03-05 23:01:23 +00003633 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003634 break;
3635 }
bellard9307c4c2004-04-04 12:57:25 +00003636 goto fail;
3637 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003638 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003639 }
3640 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003641 case 'O':
3642 {
3643 QemuOptsList *opts_list;
3644 QemuOpts *opts;
3645
3646 opts_list = qemu_find_opts(key);
3647 if (!opts_list || opts_list->desc->name) {
3648 goto bad_type;
3649 }
3650 while (qemu_isspace(*p)) {
3651 p++;
3652 }
3653 if (!*p)
3654 break;
3655 if (get_str(buf, sizeof(buf), &p) < 0) {
3656 goto fail;
3657 }
3658 opts = qemu_opts_parse(opts_list, buf, 1);
3659 if (!opts) {
3660 goto fail;
3661 }
3662 qemu_opts_to_qdict(opts, qdict);
3663 qemu_opts_del(opts);
3664 }
3665 break;
bellard9307c4c2004-04-04 12:57:25 +00003666 case '/':
3667 {
3668 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003669
blueswir1cd390082008-11-16 13:53:32 +00003670 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003671 p++;
3672 if (*p == '/') {
3673 /* format found */
3674 p++;
3675 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003676 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003677 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003678 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003679 count = count * 10 + (*p - '0');
3680 p++;
3681 }
3682 }
3683 size = -1;
3684 format = -1;
3685 for(;;) {
3686 switch(*p) {
3687 case 'o':
3688 case 'd':
3689 case 'u':
3690 case 'x':
3691 case 'i':
3692 case 'c':
3693 format = *p++;
3694 break;
3695 case 'b':
3696 size = 1;
3697 p++;
3698 break;
3699 case 'h':
3700 size = 2;
3701 p++;
3702 break;
3703 case 'w':
3704 size = 4;
3705 p++;
3706 break;
3707 case 'g':
3708 case 'L':
3709 size = 8;
3710 p++;
3711 break;
3712 default:
3713 goto next;
3714 }
3715 }
3716 next:
blueswir1cd390082008-11-16 13:53:32 +00003717 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003718 monitor_printf(mon, "invalid char in format: '%c'\n",
3719 *p);
bellard9307c4c2004-04-04 12:57:25 +00003720 goto fail;
3721 }
bellard9307c4c2004-04-04 12:57:25 +00003722 if (format < 0)
3723 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003724 if (format != 'i') {
3725 /* for 'i', not specifying a size gives -1 as size */
3726 if (size < 0)
3727 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003728 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003729 }
bellard9307c4c2004-04-04 12:57:25 +00003730 default_fmt_format = format;
3731 } else {
3732 count = 1;
3733 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003734 if (format != 'i') {
3735 size = default_fmt_size;
3736 } else {
3737 size = -1;
3738 }
bellard9307c4c2004-04-04 12:57:25 +00003739 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003740 qdict_put(qdict, "count", qint_from_int(count));
3741 qdict_put(qdict, "format", qint_from_int(format));
3742 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003743 }
3744 break;
3745 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003746 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003747 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003748 {
blueswir1c2efc952007-09-25 17:28:42 +00003749 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003750
blueswir1cd390082008-11-16 13:53:32 +00003751 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003752 p++;
bellard34405572004-06-08 00:55:58 +00003753 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003754 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003755 if (*p == '\0') {
3756 typestr++;
3757 break;
3758 }
bellard34405572004-06-08 00:55:58 +00003759 } else {
3760 if (*p == '.') {
3761 p++;
blueswir1cd390082008-11-16 13:53:32 +00003762 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003763 p++;
bellard34405572004-06-08 00:55:58 +00003764 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003765 typestr++;
3766 break;
bellard34405572004-06-08 00:55:58 +00003767 }
3768 }
bellard13224a82006-07-14 22:03:35 +00003769 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003770 }
aliguori376253e2009-03-05 23:01:23 +00003771 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003772 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003773 /* Check if 'i' is greater than 32-bit */
3774 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3775 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3776 monitor_printf(mon, "integer is for 32-bit values\n");
3777 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003778 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003779 if (val < 0) {
3780 monitor_printf(mon, "enter a positive value\n");
3781 goto fail;
3782 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003783 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003784 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003785 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003786 }
3787 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003788 case 'o':
3789 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003790 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003791 char *end;
3792
3793 while (qemu_isspace(*p)) {
3794 p++;
3795 }
3796 if (*typestr == '?') {
3797 typestr++;
3798 if (*p == '\0') {
3799 break;
3800 }
3801 }
3802 val = strtosz(p, &end);
3803 if (val < 0) {
3804 monitor_printf(mon, "invalid size\n");
3805 goto fail;
3806 }
3807 qdict_put(qdict, key, qint_from_int(val));
3808 p = end;
3809 }
3810 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003811 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003812 {
3813 double val;
3814
3815 while (qemu_isspace(*p))
3816 p++;
3817 if (*typestr == '?') {
3818 typestr++;
3819 if (*p == '\0') {
3820 break;
3821 }
3822 }
3823 if (get_double(mon, &val, &p) < 0) {
3824 goto fail;
3825 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003826 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003827 switch (*p) {
3828 case 'm':
3829 val /= 1e3; p += 2; break;
3830 case 'u':
3831 val /= 1e6; p += 2; break;
3832 case 'n':
3833 val /= 1e9; p += 2; break;
3834 }
3835 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003836 if (*p && !qemu_isspace(*p)) {
3837 monitor_printf(mon, "Unknown unit suffix\n");
3838 goto fail;
3839 }
3840 qdict_put(qdict, key, qfloat_from_double(val));
3841 }
3842 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003843 case 'b':
3844 {
3845 const char *beg;
3846 int val;
3847
3848 while (qemu_isspace(*p)) {
3849 p++;
3850 }
3851 beg = p;
3852 while (qemu_isgraph(*p)) {
3853 p++;
3854 }
3855 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3856 val = 1;
3857 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3858 val = 0;
3859 } else {
3860 monitor_printf(mon, "Expected 'on' or 'off'\n");
3861 goto fail;
3862 }
3863 qdict_put(qdict, key, qbool_from_int(val));
3864 }
3865 break;
bellard9307c4c2004-04-04 12:57:25 +00003866 case '-':
3867 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003868 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003869 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003870 /* option */
ths3b46e622007-09-17 08:09:54 +00003871
bellard9307c4c2004-04-04 12:57:25 +00003872 c = *typestr++;
3873 if (c == '\0')
3874 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003875 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003876 p++;
bellard9307c4c2004-04-04 12:57:25 +00003877 if (*p == '-') {
3878 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003879 if(c != *p) {
3880 if(!is_valid_option(p, typestr)) {
3881
3882 monitor_printf(mon, "%s: unsupported option -%c\n",
3883 cmdname, *p);
3884 goto fail;
3885 } else {
3886 skip_key = 1;
3887 }
bellard9307c4c2004-04-04 12:57:25 +00003888 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003889 if(skip_key) {
3890 p = tmp;
3891 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003892 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003893 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003894 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003895 }
bellard9307c4c2004-04-04 12:57:25 +00003896 }
bellard9307c4c2004-04-04 12:57:25 +00003897 }
3898 break;
3899 default:
3900 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003901 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003902 goto fail;
3903 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003904 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003905 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003906 }
3907 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003908 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003909 p++;
3910 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003911 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3912 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003913 goto fail;
3914 }
3915
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003916 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003917
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003918fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05003919 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003920 return NULL;
3921}
3922
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003923void monitor_set_error(Monitor *mon, QError *qerror)
3924{
3925 /* report only the first error */
3926 if (!mon->error) {
3927 mon->error = qerror;
3928 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003929 QDECREF(qerror);
3930 }
3931}
3932
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003933static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3934{
Luiz Capitulino6d441432010-11-22 16:35:09 -02003935 if (ret && !monitor_has_error(mon)) {
3936 /*
3937 * If it returns failure, it must have passed on error.
3938 *
3939 * Action: Report an internal error to the client if in QMP.
3940 */
3941 qerror_report(QERR_UNDEFINED_ERROR);
Luiz Capitulino6d441432010-11-22 16:35:09 -02003942 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003943}
3944
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003945static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003946{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003947 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003948 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003949
3950 qdict = qdict_new();
3951
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003952 cmd = monitor_parse_command(mon, cmdline, 0, mon_cmds, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003953 if (!cmd)
3954 goto out;
3955
Luiz Capitulino4903de02010-09-16 11:01:32 -03003956 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06003957 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03003958 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03003959 QObject *data = NULL;
3960
3961 /* XXX: ignores the error code */
3962 cmd->mhandler.cmd_new(mon, qdict, &data);
3963 assert(!monitor_has_error(mon));
3964 if (data) {
3965 cmd->user_print(mon, data);
3966 qobject_decref(data);
3967 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03003968 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003969 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003970 }
3971
Luiz Capitulino13917be2009-10-07 13:41:54 -03003972out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003973 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003974}
3975
bellard81d09122004-07-14 17:21:37 +00003976static void cmd_completion(const char *name, const char *list)
3977{
3978 const char *p, *pstart;
3979 char cmd[128];
3980 int len;
3981
3982 p = list;
3983 for(;;) {
3984 pstart = p;
3985 p = strchr(p, '|');
3986 if (!p)
3987 p = pstart + strlen(pstart);
3988 len = p - pstart;
3989 if (len > sizeof(cmd) - 2)
3990 len = sizeof(cmd) - 2;
3991 memcpy(cmd, pstart, len);
3992 cmd[len] = '\0';
3993 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003994 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003995 }
3996 if (*p == '\0')
3997 break;
3998 p++;
3999 }
4000}
4001
4002static void file_completion(const char *input)
4003{
4004 DIR *ffs;
4005 struct dirent *d;
4006 char path[1024];
4007 char file[1024], file_prefix[1024];
4008 int input_path_len;
4009 const char *p;
4010
ths5fafdf22007-09-16 21:08:06 +00004011 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004012 if (!p) {
4013 input_path_len = 0;
4014 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004015 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004016 } else {
4017 input_path_len = p - input + 1;
4018 memcpy(path, input, input_path_len);
4019 if (input_path_len > sizeof(path) - 1)
4020 input_path_len = sizeof(path) - 1;
4021 path[input_path_len] = '\0';
4022 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4023 }
4024#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004025 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4026 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004027#endif
4028 ffs = opendir(path);
4029 if (!ffs)
4030 return;
4031 for(;;) {
4032 struct stat sb;
4033 d = readdir(ffs);
4034 if (!d)
4035 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004036
4037 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4038 continue;
4039 }
4040
bellard81d09122004-07-14 17:21:37 +00004041 if (strstart(d->d_name, file_prefix, NULL)) {
4042 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004043 if (input_path_len < sizeof(file))
4044 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4045 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004046 /* stat the file to find out if it's a directory.
4047 * In that case add a slash to speed up typing long paths
4048 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004049 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004050 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004051 }
aliguori731b0362009-03-05 23:01:42 +00004052 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004053 }
4054 }
4055 closedir(ffs);
4056}
4057
aliguori51de9762009-03-05 23:00:43 +00004058static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004059{
aliguori51de9762009-03-05 23:00:43 +00004060 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004061 const char *input = opaque;
4062
4063 if (input[0] == '\0' ||
4064 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004065 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004066 }
4067}
4068
4069/* NOTE: this parser is an approximate form of the real command parser */
4070static void parse_cmdline(const char *cmdline,
4071 int *pnb_args, char **args)
4072{
4073 const char *p;
4074 int nb_args, ret;
4075 char buf[1024];
4076
4077 p = cmdline;
4078 nb_args = 0;
4079 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004080 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004081 p++;
4082 if (*p == '\0')
4083 break;
4084 if (nb_args >= MAX_ARGS)
4085 break;
4086 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004087 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004088 nb_args++;
4089 if (ret < 0)
4090 break;
4091 }
4092 *pnb_args = nb_args;
4093}
4094
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004095static const char *next_arg_type(const char *typestr)
4096{
4097 const char *p = strchr(typestr, ':');
4098 return (p != NULL ? ++p : typestr);
4099}
4100
aliguori4c36ba32009-03-05 23:01:37 +00004101static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004102{
4103 const char *cmdname;
4104 char *args[MAX_ARGS];
4105 int nb_args, i, len;
4106 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004107 const mon_cmd_t *cmd;
bellard81d09122004-07-14 17:21:37 +00004108
4109 parse_cmdline(cmdline, &nb_args, args);
4110#ifdef DEBUG_COMPLETION
4111 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004112 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004113 }
4114#endif
4115
4116 /* if the line ends with a space, it means we want to complete the
4117 next arg */
4118 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004119 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004120 if (nb_args >= MAX_ARGS) {
4121 goto cleanup;
4122 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004123 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004124 }
4125 if (nb_args <= 1) {
4126 /* command completion */
4127 if (nb_args == 0)
4128 cmdname = "";
4129 else
4130 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004131 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004132 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004133 cmd_completion(cmdname, cmd->name);
4134 }
4135 } else {
4136 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004137 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4138 if (compare_cmd(args[0], cmd->name)) {
4139 break;
4140 }
bellard81d09122004-07-14 17:21:37 +00004141 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004142 if (!cmd->name) {
4143 goto cleanup;
4144 }
4145
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004146 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004147 for(i = 0; i < nb_args - 2; i++) {
4148 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004149 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004150 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004151 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004152 }
4153 }
4154 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004155 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004156 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004157 }
bellard81d09122004-07-14 17:21:37 +00004158 switch(*ptype) {
4159 case 'F':
4160 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004161 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004162 file_completion(str);
4163 break;
4164 case 'B':
4165 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004166 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004167 bdrv_iterate(block_completion_it, (void *)str);
4168 break;
bellard7fe48482004-10-09 18:08:01 +00004169 case 's':
4170 /* XXX: more generic ? */
4171 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004172 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004173 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4174 cmd_completion(str, cmd->name);
4175 }
bellard64866c32006-05-07 18:03:31 +00004176 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004177 char *sep = strrchr(str, '-');
4178 if (sep)
4179 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004180 readline_set_completion_index(cur_mon->rs, strlen(str));
Amos Kong1048c882012-08-31 10:56:25 +08004181 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4182 cmd_completion(str, QKeyCode_lookup[i]);
bellard64866c32006-05-07 18:03:31 +00004183 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004184 } else if (!strcmp(cmd->name, "help|?")) {
4185 readline_set_completion_index(cur_mon->rs, strlen(str));
4186 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4187 cmd_completion(str, cmd->name);
4188 }
bellard7fe48482004-10-09 18:08:01 +00004189 }
4190 break;
bellard81d09122004-07-14 17:21:37 +00004191 default:
4192 break;
4193 }
4194 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004195
4196cleanup:
4197 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004198 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004199 }
bellard81d09122004-07-14 17:21:37 +00004200}
4201
aliguori731b0362009-03-05 23:01:42 +00004202static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004203{
aliguori731b0362009-03-05 23:01:42 +00004204 Monitor *mon = opaque;
4205
Jan Kiszkac62313b2009-12-04 14:05:29 +01004206 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004207}
4208
Luiz Capitulino09069b12010-02-04 18:10:06 -02004209static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4210{
4211 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4212 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4213}
4214
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004215/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004216 * Argument validation rules:
4217 *
4218 * 1. The argument must exist in cmd_args qdict
4219 * 2. The argument type must be the expected one
4220 *
4221 * Special case: If the argument doesn't exist in cmd_args and
4222 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4223 * checking is skipped for it.
4224 */
4225static int check_client_args_type(const QDict *client_args,
4226 const QDict *cmd_args, int flags)
4227{
4228 const QDictEntry *ent;
4229
4230 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4231 QObject *obj;
4232 QString *arg_type;
4233 const QObject *client_arg = qdict_entry_value(ent);
4234 const char *client_arg_name = qdict_entry_key(ent);
4235
4236 obj = qdict_get(cmd_args, client_arg_name);
4237 if (!obj) {
4238 if (flags & QMP_ACCEPT_UNKNOWNS) {
4239 /* handler accepts unknowns */
4240 continue;
4241 }
4242 /* client arg doesn't exist */
4243 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4244 return -1;
4245 }
4246
4247 arg_type = qobject_to_qstring(obj);
4248 assert(arg_type != NULL);
4249
4250 /* check if argument's type is correct */
4251 switch (qstring_get_str(arg_type)[0]) {
4252 case 'F':
4253 case 'B':
4254 case 's':
4255 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4256 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4257 "string");
4258 return -1;
4259 }
4260 break;
4261 case 'i':
4262 case 'l':
4263 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004264 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004265 if (qobject_type(client_arg) != QTYPE_QINT) {
4266 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4267 "int");
4268 return -1;
4269 }
4270 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004271 case 'T':
4272 if (qobject_type(client_arg) != QTYPE_QINT &&
4273 qobject_type(client_arg) != QTYPE_QFLOAT) {
4274 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4275 "number");
4276 return -1;
4277 }
4278 break;
4279 case 'b':
4280 case '-':
4281 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4282 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4283 "bool");
4284 return -1;
4285 }
4286 break;
4287 case 'O':
4288 assert(flags & QMP_ACCEPT_UNKNOWNS);
4289 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004290 case 'q':
4291 /* Any QObject can be passed. */
4292 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004293 case '/':
4294 case '.':
4295 /*
4296 * These types are not supported by QMP and thus are not
4297 * handled here. Fall through.
4298 */
4299 default:
4300 abort();
4301 }
4302 }
4303
4304 return 0;
4305}
4306
4307/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004308 * - Check if the client has passed all mandatory args
4309 * - Set special flags for argument validation
4310 */
4311static int check_mandatory_args(const QDict *cmd_args,
4312 const QDict *client_args, int *flags)
4313{
4314 const QDictEntry *ent;
4315
4316 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4317 const char *cmd_arg_name = qdict_entry_key(ent);
4318 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4319 assert(type != NULL);
4320
4321 if (qstring_get_str(type)[0] == 'O') {
4322 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4323 *flags |= QMP_ACCEPT_UNKNOWNS;
4324 } else if (qstring_get_str(type)[0] != '-' &&
4325 qstring_get_str(type)[1] != '?' &&
4326 !qdict_haskey(client_args, cmd_arg_name)) {
4327 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4328 return -1;
4329 }
4330 }
4331
4332 return 0;
4333}
4334
4335static QDict *qdict_from_args_type(const char *args_type)
4336{
4337 int i;
4338 QDict *qdict;
4339 QString *key, *type, *cur_qs;
4340
4341 assert(args_type != NULL);
4342
4343 qdict = qdict_new();
4344
4345 if (args_type == NULL || args_type[0] == '\0') {
4346 /* no args, empty qdict */
4347 goto out;
4348 }
4349
4350 key = qstring_new();
4351 type = qstring_new();
4352
4353 cur_qs = key;
4354
4355 for (i = 0;; i++) {
4356 switch (args_type[i]) {
4357 case ',':
4358 case '\0':
4359 qdict_put(qdict, qstring_get_str(key), type);
4360 QDECREF(key);
4361 if (args_type[i] == '\0') {
4362 goto out;
4363 }
4364 type = qstring_new(); /* qdict has ref */
4365 cur_qs = key = qstring_new();
4366 break;
4367 case ':':
4368 cur_qs = type;
4369 break;
4370 default:
4371 qstring_append_chr(cur_qs, args_type[i]);
4372 break;
4373 }
4374 }
4375
4376out:
4377 return qdict;
4378}
4379
4380/*
4381 * Client argument checking rules:
4382 *
4383 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004384 * 2. Each argument provided by the client must be expected
4385 * 3. Each argument provided by the client must have the type expected
4386 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004387 */
4388static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4389{
4390 int flags, err;
4391 QDict *cmd_args;
4392
4393 cmd_args = qdict_from_args_type(cmd->args_type);
4394
4395 flags = 0;
4396 err = check_mandatory_args(cmd_args, client_args, &flags);
4397 if (err) {
4398 goto out;
4399 }
4400
Luiz Capitulino4af91932010-06-22 11:44:05 -03004401 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004402
4403out:
4404 QDECREF(cmd_args);
4405 return err;
4406}
4407
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004408/*
4409 * Input object checking rules
4410 *
4411 * 1. Input object must be a dict
4412 * 2. The "execute" key must exist
4413 * 3. The "execute" key must be a string
4414 * 4. If the "arguments" key exists, it must be a dict
4415 * 5. If the "id" key exists, it can be anything (ie. json-value)
4416 * 6. Any argument not listed above is considered invalid
4417 */
4418static QDict *qmp_check_input_obj(QObject *input_obj)
4419{
4420 const QDictEntry *ent;
4421 int has_exec_key = 0;
4422 QDict *input_dict;
4423
4424 if (qobject_type(input_obj) != QTYPE_QDICT) {
4425 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4426 return NULL;
4427 }
4428
4429 input_dict = qobject_to_qdict(input_obj);
4430
4431 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4432 const char *arg_name = qdict_entry_key(ent);
4433 const QObject *arg_obj = qdict_entry_value(ent);
4434
4435 if (!strcmp(arg_name, "execute")) {
4436 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4437 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4438 "string");
4439 return NULL;
4440 }
4441 has_exec_key = 1;
4442 } else if (!strcmp(arg_name, "arguments")) {
4443 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4444 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4445 "object");
4446 return NULL;
4447 }
4448 } else if (!strcmp(arg_name, "id")) {
4449 /* FIXME: check duplicated IDs for async commands */
4450 } else {
4451 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4452 return NULL;
4453 }
4454 }
4455
4456 if (!has_exec_key) {
4457 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4458 return NULL;
4459 }
4460
4461 return input_dict;
4462}
4463
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004464static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4465 const QDict *params)
4466{
4467 int ret;
4468 QObject *data = NULL;
4469
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004470 ret = cmd->mhandler.cmd_new(mon, params, &data);
4471 handler_audit(mon, cmd, ret);
4472 monitor_protocol_emitter(mon, data);
4473 qobject_decref(data);
4474}
4475
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004476static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4477{
4478 int err;
4479 QObject *obj;
4480 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004481 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004482 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004483 Monitor *mon = cur_mon;
4484
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004485 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004486
4487 obj = json_parser_parse(tokens, NULL);
4488 if (!obj) {
4489 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004490 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004491 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004492 }
4493
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004494 input = qmp_check_input_obj(obj);
4495 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004496 qobject_decref(obj);
4497 goto err_out;
4498 }
4499
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004500 mon->mc->id = qdict_get(input, "id");
4501 qobject_incref(mon->mc->id);
4502
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004503 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004504 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004505 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004506 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004507 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004508 }
4509
Anthony Liguorie3193602011-09-02 12:34:47 -05004510 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004511 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004512 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004513 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004514 }
4515
4516 obj = qdict_get(input, "arguments");
4517 if (!obj) {
4518 args = qdict_new();
4519 } else {
4520 args = qobject_to_qdict(obj);
4521 QINCREF(args);
4522 }
4523
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004524 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004525 if (err < 0) {
4526 goto err_out;
4527 }
4528
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004529 if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004530 err = qmp_async_cmd_handler(mon, cmd, args);
4531 if (err) {
4532 /* emit the error response */
4533 goto err_out;
4534 }
Adam Litke940cc302010-01-25 12:18:44 -06004535 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004536 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004537 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004538
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004539 goto out;
4540
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004541err_out:
4542 monitor_protocol_emitter(mon, NULL);
4543out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004544 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004545 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004546}
4547
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004548/**
4549 * monitor_control_read(): Read and handle QMP input
4550 */
4551static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4552{
4553 Monitor *old_mon = cur_mon;
4554
4555 cur_mon = opaque;
4556
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004557 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004558
4559 cur_mon = old_mon;
4560}
4561
aliguori731b0362009-03-05 23:01:42 +00004562static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004563{
aliguori731b0362009-03-05 23:01:42 +00004564 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004565 int i;
aliguori376253e2009-03-05 23:01:23 +00004566
aliguori731b0362009-03-05 23:01:42 +00004567 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004568
aliguoricde76ee2009-03-05 23:01:51 +00004569 if (cur_mon->rs) {
4570 for (i = 0; i < size; i++)
4571 readline_handle_byte(cur_mon->rs, buf[i]);
4572 } else {
4573 if (size == 0 || buf[size - 1] != 0)
4574 monitor_printf(cur_mon, "corrupted command\n");
4575 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004576 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004577 }
aliguori731b0362009-03-05 23:01:42 +00004578
4579 cur_mon = old_mon;
4580}
aliguorid8f44602008-10-06 13:52:44 +00004581
aliguori376253e2009-03-05 23:01:23 +00004582static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004583{
aliguori731b0362009-03-05 23:01:42 +00004584 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004585 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004586 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004587}
4588
aliguoricde76ee2009-03-05 23:01:51 +00004589int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004590{
aliguoricde76ee2009-03-05 23:01:51 +00004591 if (!mon->rs)
4592 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004593 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004594 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004595}
4596
aliguori376253e2009-03-05 23:01:23 +00004597void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004598{
aliguoricde76ee2009-03-05 23:01:51 +00004599 if (!mon->rs)
4600 return;
aliguori731b0362009-03-05 23:01:42 +00004601 if (--mon->suspend_cnt == 0)
4602 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004603}
4604
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004605static QObject *get_qmp_greeting(void)
4606{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004607 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004608
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004609 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004610 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4611}
4612
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004613/**
4614 * monitor_control_event(): Print QMP gretting
4615 */
4616static void monitor_control_event(void *opaque, int event)
4617{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004618 QObject *data;
4619 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004620
Luiz Capitulino47116d12010-02-08 17:01:30 -02004621 switch (event) {
4622 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004623 mon->mc->command_mode = 0;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004624 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004625 monitor_json_emitter(mon, data);
4626 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004627 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004628 break;
4629 case CHR_EVENT_CLOSED:
4630 json_message_parser_destroy(&mon->mc->parser);
Anthony Liguori58617a72012-08-23 08:03:21 -05004631 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004632 mon_refcount--;
4633 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004634 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004635 }
4636}
4637
aliguori731b0362009-03-05 23:01:42 +00004638static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004639{
aliguori376253e2009-03-05 23:01:23 +00004640 Monitor *mon = opaque;
4641
aliguori2724b182009-03-05 23:01:47 +00004642 switch (event) {
4643 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004644 mon->mux_out = 0;
4645 if (mon->reset_seen) {
4646 readline_restart(mon->rs);
4647 monitor_resume(mon);
4648 monitor_flush(mon);
4649 } else {
4650 mon->suspend_cnt = 0;
4651 }
aliguori2724b182009-03-05 23:01:47 +00004652 break;
ths86e94de2007-01-05 22:01:59 +00004653
aliguori2724b182009-03-05 23:01:47 +00004654 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004655 if (mon->reset_seen) {
4656 if (mon->suspend_cnt == 0) {
4657 monitor_printf(mon, "\n");
4658 }
4659 monitor_flush(mon);
4660 monitor_suspend(mon);
4661 } else {
4662 mon->suspend_cnt++;
4663 }
4664 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004665 break;
4666
Amit Shahb6b8df52009-10-07 18:31:16 +05304667 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004668 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4669 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004670 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004671 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004672 }
4673 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004674 mon_refcount++;
4675 break;
4676
4677 case CHR_EVENT_CLOSED:
4678 mon_refcount--;
4679 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004680 break;
4681 }
ths86e94de2007-01-05 22:01:59 +00004682}
4683
Wayne Xia816f8922011-10-12 11:32:41 +08004684static int
4685compare_mon_cmd(const void *a, const void *b)
4686{
4687 return strcmp(((const mon_cmd_t *)a)->name,
4688 ((const mon_cmd_t *)b)->name);
4689}
4690
4691static void sortcmdlist(void)
4692{
4693 int array_num;
4694 int elem_size = sizeof(mon_cmd_t);
4695
4696 array_num = sizeof(mon_cmds)/elem_size-1;
4697 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4698
4699 array_num = sizeof(info_cmds)/elem_size-1;
4700 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4701}
4702
aliguori76655d62009-03-06 20:27:37 +00004703
4704/*
4705 * Local variables:
4706 * c-indent-level: 4
4707 * c-basic-offset: 4
4708 * tab-width: 8
4709 * End:
4710 */
4711
aliguori731b0362009-03-05 23:01:42 +00004712void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004713{
aliguori731b0362009-03-05 23:01:42 +00004714 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004715 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004716
4717 if (is_first_init) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +01004718 monitor_protocol_event_init();
ths20d8a3e2007-02-18 17:04:49 +00004719 is_first_init = 0;
4720 }
aliguori87127162009-03-05 23:01:29 +00004721
Anthony Liguori7267c092011-08-20 22:09:37 -05004722 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004723
aliguori87127162009-03-05 23:01:29 +00004724 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004725 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004726 if (flags & MONITOR_USE_READLINE) {
4727 mon->rs = readline_init(mon, monitor_find_completion);
4728 monitor_read_command(mon, 0);
4729 }
aliguori87127162009-03-05 23:01:29 +00004730
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004731 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004732 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004733 /* Control mode requires special handlers */
4734 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4735 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004736 qemu_chr_fe_set_echo(chr, true);
Anthony Liguori26efaca2012-08-23 13:49:02 -05004737
4738 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004739 } else {
4740 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4741 monitor_event, mon);
4742 }
aliguori87127162009-03-05 23:01:29 +00004743
Blue Swirl72cf2d42009-09-12 07:36:22 +00004744 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004745 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4746 default_mon = mon;
Wayne Xia816f8922011-10-12 11:32:41 +08004747
4748 sortcmdlist();
bellard7e2515e2004-08-01 21:52:19 +00004749}
4750
aliguori376253e2009-03-05 23:01:23 +00004751static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004752{
aliguoribb5fc202009-03-05 23:01:15 +00004753 BlockDriverState *bs = opaque;
4754 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004755
aliguoribb5fc202009-03-05 23:01:15 +00004756 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004757 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004758 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004759 }
aliguori731b0362009-03-05 23:01:42 +00004760 if (mon->password_completion_cb)
4761 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004762
aliguori731b0362009-03-05 23:01:42 +00004763 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004764}
aliguoric0f4ce72009-03-05 23:01:01 +00004765
Anthony Liguori7060b472011-09-02 12:34:50 -05004766ReadLineState *monitor_get_rs(Monitor *mon)
4767{
4768 return mon->rs;
4769}
4770
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004771int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4772 BlockDriverCompletionFunc *completion_cb,
4773 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004774{
aliguoricde76ee2009-03-05 23:01:51 +00004775 int err;
4776
aliguoribb5fc202009-03-05 23:01:15 +00004777 if (!bdrv_key_required(bs)) {
4778 if (completion_cb)
4779 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004780 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004781 }
aliguoric0f4ce72009-03-05 23:01:01 +00004782
Luiz Capitulino94171e12009-12-07 21:37:00 +01004783 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino903a8812011-12-13 17:18:30 -02004784 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
4785 bdrv_get_encrypted_filename(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004786 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004787 }
4788
aliguori376253e2009-03-05 23:01:23 +00004789 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4790 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004791
aliguori731b0362009-03-05 23:01:42 +00004792 mon->password_completion_cb = completion_cb;
4793 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004794
aliguoricde76ee2009-03-05 23:01:51 +00004795 err = monitor_read_password(mon, bdrv_password_cb, bs);
4796
4797 if (err && completion_cb)
4798 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004799
4800 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004801}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004802
4803int monitor_read_block_device_key(Monitor *mon, const char *device,
4804 BlockDriverCompletionFunc *completion_cb,
4805 void *opaque)
4806{
4807 BlockDriverState *bs;
4808
4809 bs = bdrv_find(device);
4810 if (!bs) {
4811 monitor_printf(mon, "Device not found %s\n", device);
4812 return -1;
4813 }
4814
4815 return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);
4816}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004817
4818QemuOptsList qemu_mon_opts = {
4819 .name = "mon",
4820 .implied_opt_name = "chardev",
4821 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4822 .desc = {
4823 {
4824 .name = "mode",
4825 .type = QEMU_OPT_STRING,
4826 },{
4827 .name = "chardev",
4828 .type = QEMU_OPT_STRING,
4829 },{
4830 .name = "default",
4831 .type = QEMU_OPT_BOOL,
4832 },{
4833 .name = "pretty",
4834 .type = QEMU_OPT_BOOL,
4835 },
4836 { /* end of list */ }
4837 },
4838};