blob: 016910deef846de67ddfdc030ba9492f2f07a514 [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"
Gerd Hoffmanncae49562009-06-05 15:53:17 +010026#include "hw/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"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010050#include "qapi/qmp/qint.h"
51#include "qapi/qmp/qfloat.h"
52#include "qapi/qmp/qlist.h"
53#include "qapi/qmp/qbool.h"
54#include "qapi/qmp/qstring.h"
55#include "qapi/qmp/qjson.h"
56#include "qapi/qmp/json-streamer.h"
57#include "qapi/qmp/json-parser.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010058#include "qemu/osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000059#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010060#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020061#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020062#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020063#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053064#endif
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +020065#include "ui/qemu-spice.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010066#include "exec/memory.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050067#include "qmp-commands.h"
68#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010069#include "qemu/thread.h"
ths6a5bd302007-12-03 17:05:38 +000070
Jan Kiszka661f1922011-10-16 11:53:13 +020071/* for pic/irq_info */
72#if defined(TARGET_SPARC)
73#include "hw/sun4m.h"
74#endif
75#include "hw/lm32_pic.h"
76
bellard9dc39cb2004-03-14 21:38:27 +000077//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000078//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000079
bellard9307c4c2004-04-04 12:57:25 +000080/*
81 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000082 *
bellard9307c4c2004-04-04 12:57:25 +000083 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000084 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000085 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010086 * 'O' option string of the form NAME=VALUE,...
87 * parsed according to QemuOptsList given by its name
88 * Example: 'device:O' uses qemu_device_opts.
89 * Restriction: only lists with empty desc are supported
90 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000091 * 'i' 32 bit integer
92 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -030093 * 'M' Non-negative target long (32 or 64 bit), in user mode the
94 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +020095 * 'o' octets (aka bytes)
96 * user mode accepts an optional T, t, G, g, M, m, K, k
97 * suffix, which multiplies the value by 2^40 for
98 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
99 * M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +0100100 * 'T' double
101 * user mode accepts an optional ms, us, ns suffix,
102 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000103 * '/' optional gdb-like print format (like "/10x")
104 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300105 * '?' optional type (for all types, except '/')
106 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100107 * 'b' boolean
108 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300109 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000110 *
111 */
112
Adam Litke940cc302010-01-25 12:18:44 -0600113typedef struct MonitorCompletionData MonitorCompletionData;
114struct MonitorCompletionData {
115 Monitor *mon;
116 void (*user_print)(Monitor *mon, const QObject *data);
117};
118
Anthony Liguoric227f092009-10-01 16:12:16 -0500119typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000120 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000121 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000122 const char *params;
123 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300124 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300125 union {
126 void (*info)(Monitor *mon);
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;
Anthony Liguoric227f092009-10-01 16:12:16 -0500133} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000134
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100135/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500136typedef struct mon_fd_t mon_fd_t;
137struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100138 char *name;
139 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500140 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100141};
142
Corey Bryantba1c0482012-08-14 16:43:43 -0400143/* file descriptor associated with a file descriptor set */
144typedef struct MonFdsetFd MonFdsetFd;
145struct MonFdsetFd {
146 int fd;
147 bool removed;
148 char *opaque;
149 QLIST_ENTRY(MonFdsetFd) next;
150};
151
152/* file descriptor set containing fds passed via SCM_RIGHTS */
153typedef struct MonFdset MonFdset;
154struct MonFdset {
155 int64_t id;
156 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400157 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400158 QLIST_ENTRY(MonFdset) next;
159};
160
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200161typedef struct MonitorControl {
162 QObject *id;
163 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200164 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200165} MonitorControl;
166
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100167/*
168 * To prevent flooding clients, events can be throttled. The
169 * throttling is calculated globally, rather than per-Monitor
170 * instance.
171 */
172typedef struct MonitorEventState {
173 MonitorEvent event; /* Event being tracked */
174 int64_t rate; /* Period over which to throttle. 0 to disable */
175 int64_t last; /* Time at which event was last emitted */
176 QEMUTimer *timer; /* Timer for handling delayed events */
177 QObject *data; /* Event pending delayed dispatch */
178} MonitorEventState;
179
aliguori87127162009-03-05 23:01:29 +0000180struct Monitor {
181 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200182 int mux_out;
183 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000184 int flags;
185 int suspend_cnt;
186 uint8_t outbuf[1024];
187 int outbuf_index;
188 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200189 MonitorControl *mc;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100190 CPUArchState *mon_cpu;
aliguori731b0362009-03-05 23:01:42 +0000191 BlockDriverCompletionFunc *password_completion_cb;
192 void *password_opaque;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200193 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500194 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000195 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000196};
197
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300198/* QMP checker flags */
199#define QMP_ACCEPT_UNKNOWNS 1
200
Blue Swirl72cf2d42009-09-12 07:36:22 +0000201static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400202static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400203static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000204
Wayne Xia816f8922011-10-12 11:32:41 +0800205static mon_cmd_t mon_cmds[];
206static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000207
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300208static const mon_cmd_t qmp_cmds[];
209
Markus Armbruster8631b602010-02-18 11:41:55 +0100210Monitor *cur_mon;
211Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000212
aliguori731b0362009-03-05 23:01:42 +0000213static void monitor_command_cb(Monitor *mon, const char *cmdline,
214 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000215
Luiz Capitulino09069b12010-02-04 18:10:06 -0200216static inline int qmp_cmd_mode(const Monitor *mon)
217{
218 return (mon->mc ? mon->mc->command_mode : 0);
219}
220
Luiz Capitulino418173c2009-11-26 22:58:51 -0200221/* Return true if in control mode, false otherwise */
222static inline int monitor_ctrl_mode(const Monitor *mon)
223{
224 return (mon->flags & MONITOR_USE_CONTROL);
225}
226
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100227/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
228int monitor_cur_is_qmp(void)
229{
230 return cur_mon && monitor_ctrl_mode(cur_mon);
231}
232
Anthony Liguori7060b472011-09-02 12:34:50 -0500233void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000234{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200235 if (!mon->rs)
236 return;
237
aliguori731b0362009-03-05 23:01:42 +0000238 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
239 if (show_prompt)
240 readline_show_prompt(mon->rs);
241}
bellard6a00d602005-11-21 23:25:50 +0000242
Anthony Liguori7060b472011-09-02 12:34:50 -0500243int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
244 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000245{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100246 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100247 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100248 return -EINVAL;
249 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000250 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
251 /* prompt is printed on return from the command handler */
252 return 0;
253 } else {
254 monitor_printf(mon, "terminal does not support password prompting\n");
255 return -ENOTTY;
256 }
aliguoribb5fc202009-03-05 23:01:15 +0000257}
258
aliguori376253e2009-03-05 23:01:23 +0000259void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000260{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200261 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500262 qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
aliguori731b0362009-03-05 23:01:42 +0000263 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000264 }
265}
266
267/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000268static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000269{
ths60fe76f2007-12-16 03:02:09 +0000270 char c;
aliguori731b0362009-03-05 23:01:42 +0000271
bellard7e2515e2004-08-01 21:52:19 +0000272 for(;;) {
Markus Armbruster51782342013-01-10 14:10:22 +0100273 assert(mon->outbuf_index < sizeof(mon->outbuf) - 1);
bellard7e2515e2004-08-01 21:52:19 +0000274 c = *str++;
275 if (c == '\0')
276 break;
bellard7ba12602006-07-14 20:26:42 +0000277 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000278 mon->outbuf[mon->outbuf_index++] = '\r';
279 mon->outbuf[mon->outbuf_index++] = c;
280 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
281 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000282 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000283 }
284}
285
aliguori376253e2009-03-05 23:01:23 +0000286void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000287{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200288 char buf[4096];
289
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200290 if (!mon)
291 return;
292
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200293 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200294 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200295 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200296
297 vsnprintf(buf, sizeof(buf), fmt, ap);
298 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000299}
300
aliguori376253e2009-03-05 23:01:23 +0000301void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000302{
303 va_list ap;
304 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000305 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000306 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000307}
308
aliguori376253e2009-03-05 23:01:23 +0000309void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000310{
311 int i;
312
313 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000314 switch (filename[i]) {
315 case ' ':
316 case '"':
317 case '\\':
318 monitor_printf(mon, "\\%c", filename[i]);
319 break;
320 case '\t':
321 monitor_printf(mon, "\\t");
322 break;
323 case '\r':
324 monitor_printf(mon, "\\r");
325 break;
326 case '\n':
327 monitor_printf(mon, "\\n");
328 break;
329 default:
330 monitor_printf(mon, "%c", filename[i]);
331 break;
332 }
thsfef30742006-12-22 14:11:32 +0000333 }
334}
335
Stefan Weil8b7968f2010-09-23 21:28:05 +0200336static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
337 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000338{
339 va_list ap;
340 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000341 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000342 va_end(ap);
343 return 0;
344}
345
Luiz Capitulino13c74252009-10-07 13:41:55 -0300346static void monitor_user_noop(Monitor *mon, const QObject *data) { }
347
Luiz Capitulino9e807212010-09-16 10:58:59 -0300348static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300349{
350 return cmd->user_print != NULL;
351}
352
Luiz Capitulino4903de02010-09-16 11:01:32 -0300353static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600354{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200355 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600356}
357
Luiz Capitulino8204a912009-11-18 23:05:31 -0200358static inline int monitor_has_error(const Monitor *mon)
359{
360 return mon->error != NULL;
361}
362
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200363static void monitor_json_emitter(Monitor *mon, const QObject *data)
364{
365 QString *json;
366
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200367 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
368 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200369 assert(json != NULL);
370
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200371 qstring_append_chr(json, '\n');
372 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200373
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200374 QDECREF(json);
375}
376
Luiz Capitulinode253f12012-07-27 16:18:16 -0300377static QDict *build_qmp_error_dict(const QError *err)
378{
379 QObject *obj;
380
381 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
382 ErrorClass_lookup[err->err_class],
383 qerror_human(err));
384
385 return qobject_to_qdict(obj);
386}
387
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200388static void monitor_protocol_emitter(Monitor *mon, QObject *data)
389{
390 QDict *qmp;
391
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100392 trace_monitor_protocol_emitter(mon);
393
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200394 if (!monitor_has_error(mon)) {
395 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300396 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200397 if (data) {
398 qobject_incref(data);
399 qdict_put_obj(qmp, "return", data);
400 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200401 /* return an empty QDict by default */
402 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200403 }
404 } else {
405 /* error response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300406 qmp = build_qmp_error_dict(mon->error);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200407 QDECREF(mon->error);
408 mon->error = NULL;
409 }
410
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200411 if (mon->mc->id) {
412 qdict_put_obj(qmp, "id", mon->mc->id);
413 mon->mc->id = NULL;
414 }
415
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200416 monitor_json_emitter(mon, QOBJECT(qmp));
417 QDECREF(qmp);
418}
419
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200420static void timestamp_put(QDict *qdict)
421{
422 int err;
423 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000424 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200425
Blue Swirld08d6f02009-12-04 18:06:39 +0000426 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200427 if (err < 0)
428 return;
429
430 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
431 "'microseconds': %" PRId64 " }",
432 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200433 qdict_put_obj(qdict, "timestamp", obj);
434}
435
Daniel P. Berrange48608532012-05-21 17:59:51 +0100436
437static const char *monitor_event_names[] = {
438 [QEVENT_SHUTDOWN] = "SHUTDOWN",
439 [QEVENT_RESET] = "RESET",
440 [QEVENT_POWERDOWN] = "POWERDOWN",
441 [QEVENT_STOP] = "STOP",
442 [QEVENT_RESUME] = "RESUME",
443 [QEVENT_VNC_CONNECTED] = "VNC_CONNECTED",
444 [QEVENT_VNC_INITIALIZED] = "VNC_INITIALIZED",
445 [QEVENT_VNC_DISCONNECTED] = "VNC_DISCONNECTED",
446 [QEVENT_BLOCK_IO_ERROR] = "BLOCK_IO_ERROR",
447 [QEVENT_RTC_CHANGE] = "RTC_CHANGE",
448 [QEVENT_WATCHDOG] = "WATCHDOG",
449 [QEVENT_SPICE_CONNECTED] = "SPICE_CONNECTED",
450 [QEVENT_SPICE_INITIALIZED] = "SPICE_INITIALIZED",
451 [QEVENT_SPICE_DISCONNECTED] = "SPICE_DISCONNECTED",
452 [QEVENT_BLOCK_JOB_COMPLETED] = "BLOCK_JOB_COMPLETED",
453 [QEVENT_BLOCK_JOB_CANCELLED] = "BLOCK_JOB_CANCELLED",
Paolo Bonzini32c81a42012-09-28 17:22:58 +0200454 [QEVENT_BLOCK_JOB_ERROR] = "BLOCK_JOB_ERROR",
Paolo Bonzinia66a2a32012-07-23 15:15:47 +0200455 [QEVENT_BLOCK_JOB_READY] = "BLOCK_JOB_READY",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100456 [QEVENT_DEVICE_TRAY_MOVED] = "DEVICE_TRAY_MOVED",
457 [QEVENT_SUSPEND] = "SUSPEND",
Luiz Capitulino25df49f2012-08-08 17:03:01 -0300458 [QEVENT_SUSPEND_DISK] = "SUSPEND_DISK",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100459 [QEVENT_WAKEUP] = "WAKEUP",
Daniel P. Berrange973603a2012-06-14 18:12:56 +0100460 [QEVENT_BALLOON_CHANGE] = "BALLOON_CHANGE",
Yonit Halperin2fdd16e2012-08-21 11:51:57 +0300461 [QEVENT_SPICE_MIGRATE_COMPLETED] = "SPICE_MIGRATE_COMPLETED",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100462};
463QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names) != QEVENT_MAX)
464
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100465MonitorEventState monitor_event_state[QEVENT_MAX];
466QemuMutex monitor_event_state_lock;
467
468/*
469 * Emits the event to every monitor instance
470 */
471static void
472monitor_protocol_event_emit(MonitorEvent event,
473 QObject *data)
474{
475 Monitor *mon;
476
477 trace_monitor_protocol_event_emit(event, data);
478 QLIST_FOREACH(mon, &mon_list, entry) {
479 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
480 monitor_json_emitter(mon, data);
481 }
482 }
483}
484
485
486/*
487 * Queue a new event for emission to Monitor instances,
488 * applying any rate limiting if required.
489 */
490static void
491monitor_protocol_event_queue(MonitorEvent event,
492 QObject *data)
493{
494 MonitorEventState *evstate;
495 int64_t now = qemu_get_clock_ns(rt_clock);
496 assert(event < QEVENT_MAX);
497
498 qemu_mutex_lock(&monitor_event_state_lock);
499 evstate = &(monitor_event_state[event]);
500 trace_monitor_protocol_event_queue(event,
501 data,
502 evstate->rate,
503 evstate->last,
504 now);
505
506 /* Rate limit of 0 indicates no throttling */
507 if (!evstate->rate) {
508 monitor_protocol_event_emit(event, data);
509 evstate->last = now;
510 } else {
511 int64_t delta = now - evstate->last;
512 if (evstate->data ||
513 delta < evstate->rate) {
514 /* If there's an existing event pending, replace
515 * it with the new event, otherwise schedule a
516 * timer for delayed emission
517 */
518 if (evstate->data) {
519 qobject_decref(evstate->data);
520 } else {
521 int64_t then = evstate->last + evstate->rate;
522 qemu_mod_timer_ns(evstate->timer, then);
523 }
524 evstate->data = data;
525 qobject_incref(evstate->data);
526 } else {
527 monitor_protocol_event_emit(event, data);
528 evstate->last = now;
529 }
530 }
531 qemu_mutex_unlock(&monitor_event_state_lock);
532}
533
534
535/*
536 * The callback invoked by QemuTimer when a delayed
537 * event is ready to be emitted
538 */
539static void monitor_protocol_event_handler(void *opaque)
540{
541 MonitorEventState *evstate = opaque;
542 int64_t now = qemu_get_clock_ns(rt_clock);
543
544 qemu_mutex_lock(&monitor_event_state_lock);
545
546 trace_monitor_protocol_event_handler(evstate->event,
547 evstate->data,
548 evstate->last,
549 now);
550 if (evstate->data) {
551 monitor_protocol_event_emit(evstate->event, evstate->data);
552 qobject_decref(evstate->data);
553 evstate->data = NULL;
554 }
555 evstate->last = now;
556 qemu_mutex_unlock(&monitor_event_state_lock);
557}
558
559
560/*
561 * @event: the event ID to be limited
562 * @rate: the rate limit in milliseconds
563 *
564 * Sets a rate limit on a particular event, so no
565 * more than 1 event will be emitted within @rate
566 * milliseconds
567 */
568static void
569monitor_protocol_event_throttle(MonitorEvent event,
570 int64_t rate)
571{
572 MonitorEventState *evstate;
573 assert(event < QEVENT_MAX);
574
575 evstate = &(monitor_event_state[event]);
576
577 trace_monitor_protocol_event_throttle(event, rate);
578 evstate->event = event;
579 evstate->rate = rate * SCALE_MS;
580 evstate->timer = qemu_new_timer(rt_clock,
581 SCALE_MS,
582 monitor_protocol_event_handler,
583 evstate);
584 evstate->last = 0;
585 evstate->data = NULL;
586}
587
588
589/* Global, one-time initializer to configure the rate limiting
590 * and initialize state */
591static void monitor_protocol_event_init(void)
592{
593 qemu_mutex_init(&monitor_event_state_lock);
594 /* Limit RTC & BALLOON events to 1 per second */
595 monitor_protocol_event_throttle(QEVENT_RTC_CHANGE, 1000);
596 monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE, 1000);
597 monitor_protocol_event_throttle(QEVENT_WATCHDOG, 1000);
598}
599
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200600/**
601 * monitor_protocol_event(): Generate a Monitor event
602 *
603 * Event-specific data can be emitted through the (optional) 'data' parameter.
604 */
605void monitor_protocol_event(MonitorEvent event, QObject *data)
606{
607 QDict *qmp;
608 const char *event_name;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200609
Blue Swirl242cd002009-12-04 18:05:45 +0000610 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200611
Daniel P. Berrange48608532012-05-21 17:59:51 +0100612 event_name = monitor_event_names[event];
613 assert(event_name != NULL);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200614
615 qmp = qdict_new();
616 timestamp_put(qmp);
617 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200618 if (data) {
619 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200620 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200621 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200622
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100623 trace_monitor_protocol_event(event, event_name, qmp);
624 monitor_protocol_event_queue(event, QOBJECT(qmp));
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200625 QDECREF(qmp);
626}
627
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200628static int do_qmp_capabilities(Monitor *mon, const QDict *params,
629 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200630{
631 /* Will setup QMP capabilities in the future */
632 if (monitor_ctrl_mode(mon)) {
633 mon->mc->command_mode = 1;
634 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200635
636 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200637}
638
Luiz Capitulino0268d972010-10-22 10:08:02 -0200639static void handle_user_command(Monitor *mon, const char *cmdline);
640
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200641char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
642 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200643{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200644 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200645 Monitor *old_mon, hmp;
646 CharDriverState mchar;
647
648 memset(&hmp, 0, sizeof(hmp));
649 qemu_chr_init_mem(&mchar);
650 hmp.chr = &mchar;
651
652 old_mon = cur_mon;
653 cur_mon = &hmp;
654
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200655 if (has_cpu_index) {
656 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200657 if (ret < 0) {
658 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200659 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
660 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200661 goto out;
662 }
663 }
664
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200665 handle_user_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200666 cur_mon = old_mon;
667
668 if (qemu_chr_mem_osize(hmp.chr) > 0) {
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200669 QString *str = qemu_chr_mem_to_qs(hmp.chr);
670 output = g_strdup(qstring_get_str(str));
671 QDECREF(str);
672 } else {
673 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200674 }
675
676out:
677 qemu_chr_close_mem(hmp.chr);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200678 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200679}
680
bellard9dc39cb2004-03-14 21:38:27 +0000681static int compare_cmd(const char *name, const char *list)
682{
683 const char *p, *pstart;
684 int len;
685 len = strlen(name);
686 p = list;
687 for(;;) {
688 pstart = p;
689 p = strchr(p, '|');
690 if (!p)
691 p = pstart + strlen(pstart);
692 if ((p - pstart) == len && !memcmp(pstart, name, len))
693 return 1;
694 if (*p == '\0')
695 break;
696 p++;
697 }
698 return 0;
699}
700
Anthony Liguoric227f092009-10-01 16:12:16 -0500701static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000702 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000703{
Anthony Liguoric227f092009-10-01 16:12:16 -0500704 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000705
706 for(cmd = cmds; cmd->name != NULL; cmd++) {
707 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000708 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
709 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000710 }
711}
712
aliguori376253e2009-03-05 23:01:23 +0000713static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000714{
715 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000716 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000717 } else {
aliguori376253e2009-03-05 23:01:23 +0000718 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000719 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000720 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000721 monitor_printf(mon, "Log items (comma separated):\n");
722 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000723 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000724 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000725 }
726 }
bellard9dc39cb2004-03-14 21:38:27 +0000727 }
728}
729
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300730static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300731{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300732 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300733}
734
Lluísfc764102011-08-31 20:31:18 +0200735static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530736{
737 const char *tp_name = qdict_get_str(qdict, "name");
738 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200739 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000740
741 if (!ret) {
742 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
743 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530744}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100745
Michael Rothc45a8162011-10-02 08:44:37 -0500746#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100747static void do_trace_file(Monitor *mon, const QDict *qdict)
748{
749 const char *op = qdict_get_try_str(qdict, "op");
750 const char *arg = qdict_get_try_str(qdict, "arg");
751
752 if (!op) {
753 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
754 } else if (!strcmp(op, "on")) {
755 st_set_trace_file_enabled(true);
756 } else if (!strcmp(op, "off")) {
757 st_set_trace_file_enabled(false);
758 } else if (!strcmp(op, "flush")) {
759 st_flush_trace_buffer();
760 } else if (!strcmp(op, "set")) {
761 if (arg) {
762 st_set_trace_file(arg);
763 }
764 } else {
765 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
766 help_cmd(mon, "trace-file");
767 }
768}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530769#endif
770
Adam Litke940cc302010-01-25 12:18:44 -0600771static void user_monitor_complete(void *opaque, QObject *ret_data)
772{
773 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
774
775 if (ret_data) {
776 data->user_print(data->mon, ret_data);
777 }
778 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500779 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600780}
781
782static void qmp_monitor_complete(void *opaque, QObject *ret_data)
783{
784 monitor_protocol_emitter(opaque, ret_data);
785}
786
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300787static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
788 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600789{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300790 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600791}
792
Adam Litke940cc302010-01-25 12:18:44 -0600793static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
794 const QDict *params)
795{
796 int ret;
797
Anthony Liguori7267c092011-08-20 22:09:37 -0500798 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600799 cb_data->mon = mon;
800 cb_data->user_print = cmd->user_print;
801 monitor_suspend(mon);
802 ret = cmd->mhandler.cmd_async(mon, params,
803 user_monitor_complete, cb_data);
804 if (ret < 0) {
805 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500806 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600807 }
808}
809
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300810static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000811{
Anthony Liguoric227f092009-10-01 16:12:16 -0500812 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300813 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000814
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200815 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000816 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200817 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300818
819 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000820 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300821 break;
bellard9dc39cb2004-03-14 21:38:27 +0000822 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300823
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200824 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300825 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200826 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300827
Luiz Capitulinob5c30582011-10-21 16:24:28 -0200828 cmd->mhandler.info(mon);
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300829 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300830
831help:
832 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000833}
834
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300835CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000836{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300837 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200838 const mon_cmd_t *cmd;
839
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300840 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200841 info = g_malloc0(sizeof(*info));
842 info->value = g_malloc0(sizeof(*info->value));
843 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200844
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300845 info->next = cmd_list;
846 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200847 }
848
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300849 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000850}
851
Daniel P. Berrange48608532012-05-21 17:59:51 +0100852EventInfoList *qmp_query_events(Error **errp)
853{
854 EventInfoList *info, *ev_list = NULL;
855 MonitorEvent e;
856
857 for (e = 0 ; e < QEVENT_MAX ; e++) {
858 const char *event_name = monitor_event_names[e];
859 assert(event_name != NULL);
860 info = g_malloc0(sizeof(*info));
861 info->value = g_malloc0(sizeof(*info->value));
862 info->value->name = g_strdup(event_name);
863
864 info->next = ev_list;
865 ev_list = info;
866 }
867
868 return ev_list;
869}
870
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300871/* set the current CPU defined by the user */
872int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000873{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100874 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000875
876 for(env = first_cpu; env != NULL; env = env->next_cpu) {
877 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000878 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000879 return 0;
880 }
881 }
882 return -1;
883}
884
Andreas Färber9349b4f2012-03-14 01:38:32 +0100885static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000886{
aliguori731b0362009-03-05 23:01:42 +0000887 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300888 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000889 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300890 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000891 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000892}
893
Luiz Capitulino99b77962011-10-24 10:53:44 -0200894int monitor_get_cpu_index(void)
895{
896 return mon_get_cpu()->cpu_index;
897}
898
aliguori376253e2009-03-05 23:01:23 +0000899static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000900{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100901 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000902 env = mon_get_cpu();
Peter Maydell6fd2a022012-10-05 15:04:43 +0100903 cpu_dump_state(env, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000904}
905
aliguori376253e2009-03-05 23:01:23 +0000906static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000907{
aliguori376253e2009-03-05 23:01:23 +0000908 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000909}
910
aliguori376253e2009-03-05 23:01:23 +0000911static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000912{
913 int i;
bellard7e2515e2004-08-01 21:52:19 +0000914 const char *str;
ths3b46e622007-09-17 08:09:54 +0000915
aliguoricde76ee2009-03-05 23:01:51 +0000916 if (!mon->rs)
917 return;
bellard7e2515e2004-08-01 21:52:19 +0000918 i = 0;
919 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000920 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000921 if (!str)
922 break;
aliguori376253e2009-03-05 23:01:23 +0000923 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000924 i++;
bellardaa455482004-04-04 13:07:25 +0000925 }
926}
927
j_mayer76a66252007-03-07 08:32:30 +0000928#if defined(TARGET_PPC)
929/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000930static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000931{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100932 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +0000933
934 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000935 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000936}
937#endif
938
Lluísfc764102011-08-31 20:31:18 +0200939static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530940{
Lluísfc764102011-08-31 20:31:18 +0200941 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530942}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530943
Yonit Halperinedc5cb12011-10-17 10:03:18 +0200944static int client_migrate_info(Monitor *mon, const QDict *qdict,
945 MonitorCompletion cb, void *opaque)
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200946{
947 const char *protocol = qdict_get_str(qdict, "protocol");
948 const char *hostname = qdict_get_str(qdict, "hostname");
949 const char *subject = qdict_get_try_str(qdict, "cert-subject");
950 int port = qdict_get_try_int(qdict, "port", -1);
951 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
952 int ret;
953
954 if (strcmp(protocol, "spice") == 0) {
955 if (!using_spice) {
956 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
957 return -1;
958 }
959
Yonit Halperin6ec5dae2012-03-18 09:42:39 +0200960 if (port == -1 && tls_port == -1) {
961 qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
962 return -1;
963 }
964
Yonit Halperinedc5cb12011-10-17 10:03:18 +0200965 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
966 cb, opaque);
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200967 if (ret != 0) {
968 qerror_report(QERR_UNDEFINED_ERROR);
969 return -1;
970 }
971 return 0;
972 }
973
974 qerror_report(QERR_INVALID_PARAMETER, "protocol");
975 return -1;
976}
977
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300978static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000979{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300980 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000981}
982
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300983static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000984{
985 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300986 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000987
bellard9307c4c2004-04-04 12:57:25 +0000988 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000989 mask = 0;
990 } else {
bellard9307c4c2004-04-04 12:57:25 +0000991 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000992 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000993 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000994 return;
995 }
996 }
997 cpu_set_log(mask);
998}
999
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001000static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001001{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001002 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001003 if (!option || !strcmp(option, "on")) {
1004 singlestep = 1;
1005 } else if (!strcmp(option, "off")) {
1006 singlestep = 0;
1007 } else {
1008 monitor_printf(mon, "unexpected option %s\n", option);
1009 }
1010}
1011
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001012static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001013{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001014 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001015 if (!device)
1016 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1017 if (gdbserver_start(device) < 0) {
1018 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1019 device);
1020 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001021 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001022 } else {
aliguori59030a82009-04-05 18:43:41 +00001023 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1024 device);
bellard8a7ddc32004-03-31 19:00:16 +00001025 }
1026}
1027
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001028static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001029{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001030 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001031 if (select_watchdog_action(action) == -1) {
1032 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1033 }
1034}
1035
aliguori376253e2009-03-05 23:01:23 +00001036static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001037{
aliguori376253e2009-03-05 23:01:23 +00001038 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001039 switch(c) {
1040 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001041 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001042 break;
1043 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001044 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001045 break;
1046 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001047 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001048 break;
1049 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001050 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001051 break;
1052 default:
1053 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001054 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001055 } else {
aliguori376253e2009-03-05 23:01:23 +00001056 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001057 }
1058 break;
1059 }
aliguori376253e2009-03-05 23:01:23 +00001060 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001061}
1062
aliguori376253e2009-03-05 23:01:23 +00001063static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001064 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001065{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001066 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001067 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001068 uint8_t buf[16];
1069 uint64_t v;
1070
1071 if (format == 'i') {
1072 int flags;
1073 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001074 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001075#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001076 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001077 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001078 } else if (wsize == 4) {
1079 flags = 0;
1080 } else {
bellard6a15fd12006-04-12 21:07:07 +00001081 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001082 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001083 if (env) {
1084#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001085 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001086 (env->segs[R_CS].flags & DESC_L_MASK))
1087 flags = 2;
1088 else
1089#endif
1090 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1091 flags = 1;
1092 }
bellard4c27ba22004-04-25 18:05:08 +00001093 }
1094#endif
aliguori376253e2009-03-05 23:01:23 +00001095 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001096 return;
1097 }
1098
1099 len = wsize * count;
1100 if (wsize == 1)
1101 line_size = 8;
1102 else
1103 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001104 max_digits = 0;
1105
1106 switch(format) {
1107 case 'o':
1108 max_digits = (wsize * 8 + 2) / 3;
1109 break;
1110 default:
1111 case 'x':
1112 max_digits = (wsize * 8) / 4;
1113 break;
1114 case 'u':
1115 case 'd':
1116 max_digits = (wsize * 8 * 10 + 32) / 33;
1117 break;
1118 case 'c':
1119 wsize = 1;
1120 break;
1121 }
1122
1123 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001124 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001125 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001126 else
aliguori376253e2009-03-05 23:01:23 +00001127 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001128 l = len;
1129 if (l > line_size)
1130 l = line_size;
1131 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001132 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001133 } else {
bellard6a00d602005-11-21 23:25:50 +00001134 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001135 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001136 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001137 break;
1138 }
bellard9307c4c2004-04-04 12:57:25 +00001139 }
ths5fafdf22007-09-16 21:08:06 +00001140 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001141 while (i < l) {
1142 switch(wsize) {
1143 default:
1144 case 1:
1145 v = ldub_raw(buf + i);
1146 break;
1147 case 2:
1148 v = lduw_raw(buf + i);
1149 break;
1150 case 4:
bellard92a31b12005-02-10 22:00:52 +00001151 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001152 break;
1153 case 8:
1154 v = ldq_raw(buf + i);
1155 break;
1156 }
aliguori376253e2009-03-05 23:01:23 +00001157 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001158 switch(format) {
1159 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001160 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001161 break;
1162 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001163 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001164 break;
1165 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001166 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001167 break;
1168 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001169 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001170 break;
1171 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001172 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001173 break;
1174 }
1175 i += wsize;
1176 }
aliguori376253e2009-03-05 23:01:23 +00001177 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001178 addr += l;
1179 len -= l;
1180 }
1181}
1182
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001183static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001184{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001185 int count = qdict_get_int(qdict, "count");
1186 int format = qdict_get_int(qdict, "format");
1187 int size = qdict_get_int(qdict, "size");
1188 target_long addr = qdict_get_int(qdict, "addr");
1189
aliguori376253e2009-03-05 23:01:23 +00001190 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001191}
1192
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001193static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001194{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001195 int count = qdict_get_int(qdict, "count");
1196 int format = qdict_get_int(qdict, "format");
1197 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001198 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001199
aliguori376253e2009-03-05 23:01:23 +00001200 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001201}
1202
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001203static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001204{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001205 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001206 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001207
bellard9307c4c2004-04-04 12:57:25 +00001208 switch(format) {
1209 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001210 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001211 break;
1212 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001213 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001214 break;
1215 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001216 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001217 break;
1218 default:
1219 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001220 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001221 break;
1222 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001223 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001224 break;
1225 }
aliguori376253e2009-03-05 23:01:23 +00001226 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001227}
1228
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001229static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001230{
1231 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001232 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001233 uint32_t start = qdict_get_int(qdict, "start");
1234 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001235
1236 sum = 0;
1237 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001238 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001239 /* BSD sum algorithm ('sum' Unix command) */
1240 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001241 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001242 }
aliguori376253e2009-03-05 23:01:23 +00001243 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001244}
1245
bellard13224a82006-07-14 22:03:35 +00001246static int mouse_button_state;
1247
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001248static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001249{
1250 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001251 const char *dx_str = qdict_get_str(qdict, "dx_str");
1252 const char *dy_str = qdict_get_str(qdict, "dy_str");
1253 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001254 dx = strtol(dx_str, NULL, 0);
1255 dy = strtol(dy_str, NULL, 0);
1256 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001257 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001258 dz = strtol(dz_str, NULL, 0);
1259 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1260}
1261
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001262static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001263{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001264 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001265 mouse_button_state = button_state;
1266 kbd_mouse_event(0, 0, 0, mouse_button_state);
1267}
1268
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001269static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001270{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001271 int size = qdict_get_int(qdict, "size");
1272 int addr = qdict_get_int(qdict, "addr");
1273 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001274 uint32_t val;
1275 int suffix;
1276
1277 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001278 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001279 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001280 addr++;
1281 }
1282 addr &= 0xffff;
1283
1284 switch(size) {
1285 default:
1286 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001287 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001288 suffix = 'b';
1289 break;
1290 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001291 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001292 suffix = 'w';
1293 break;
1294 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001295 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001296 suffix = 'l';
1297 break;
1298 }
aliguori376253e2009-03-05 23:01:23 +00001299 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1300 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001301}
bellarda3a91a32004-06-04 11:06:21 +00001302
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001303static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001304{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001305 int size = qdict_get_int(qdict, "size");
1306 int addr = qdict_get_int(qdict, "addr");
1307 int val = qdict_get_int(qdict, "val");
1308
Jan Kiszkaf1147842009-07-14 10:20:11 +02001309 addr &= IOPORTS_MASK;
1310
1311 switch (size) {
1312 default:
1313 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001314 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001315 break;
1316 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001317 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001318 break;
1319 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001320 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001321 break;
1322 }
1323}
1324
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001325static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001326{
1327 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001328 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001329
Jan Kiszka76e30d02009-07-02 00:19:02 +02001330 res = qemu_boot_set(bootdevice);
1331 if (res == 0) {
1332 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1333 } else if (res > 0) {
1334 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001335 } else {
aliguori376253e2009-03-05 23:01:23 +00001336 monitor_printf(mon, "no function defined to set boot device list for "
1337 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001338 }
1339}
1340
bellardb86bda52004-09-18 19:32:46 +00001341#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001342static void print_pte(Monitor *mon, hwaddr addr,
1343 hwaddr pte,
1344 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001345{
Blue Swirld65aaf32010-12-11 18:56:24 +00001346#ifdef TARGET_X86_64
1347 if (addr & (1ULL << 47)) {
1348 addr |= -1LL << 48;
1349 }
1350#endif
1351 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1352 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001353 addr,
1354 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001355 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001356 pte & PG_GLOBAL_MASK ? 'G' : '-',
1357 pte & PG_PSE_MASK ? 'P' : '-',
1358 pte & PG_DIRTY_MASK ? 'D' : '-',
1359 pte & PG_ACCESSED_MASK ? 'A' : '-',
1360 pte & PG_PCD_MASK ? 'C' : '-',
1361 pte & PG_PWT_MASK ? 'T' : '-',
1362 pte & PG_USER_MASK ? 'U' : '-',
1363 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001364}
1365
Andreas Färber9349b4f2012-03-14 01:38:32 +01001366static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001367{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001368 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001369 uint32_t pgd, pde, pte;
1370
bellardb86bda52004-09-18 19:32:46 +00001371 pgd = env->cr[3] & ~0xfff;
1372 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001373 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001374 pde = le32_to_cpu(pde);
1375 if (pde & PG_PRESENT_MASK) {
1376 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001377 /* 4M pages */
1378 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001379 } else {
1380 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001381 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001382 pte = le32_to_cpu(pte);
1383 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001384 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001385 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001386 ~0xfff);
1387 }
1388 }
1389 }
1390 }
1391 }
1392}
1393
Andreas Färber9349b4f2012-03-14 01:38:32 +01001394static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001395{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001396 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001397 uint64_t pdpe, pde, pte;
1398 uint64_t pdp_addr, pd_addr, pt_addr;
1399
1400 pdp_addr = env->cr[3] & ~0x1f;
1401 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001402 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001403 pdpe = le64_to_cpu(pdpe);
1404 if (pdpe & PG_PRESENT_MASK) {
1405 pd_addr = pdpe & 0x3fffffffff000ULL;
1406 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001407 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001408 pde = le64_to_cpu(pde);
1409 if (pde & PG_PRESENT_MASK) {
1410 if (pde & PG_PSE_MASK) {
1411 /* 2M pages with PAE, CR4.PSE is ignored */
1412 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001413 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001414 } else {
1415 pt_addr = pde & 0x3fffffffff000ULL;
1416 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001417 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001418 pte = le64_to_cpu(pte);
1419 if (pte & PG_PRESENT_MASK) {
1420 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1421 + (l3 << 12),
1422 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001423 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001424 }
1425 }
1426 }
1427 }
1428 }
1429 }
1430 }
1431}
1432
1433#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001434static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001435{
1436 uint64_t l1, l2, l3, l4;
1437 uint64_t pml4e, pdpe, pde, pte;
1438 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1439
1440 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1441 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001442 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001443 pml4e = le64_to_cpu(pml4e);
1444 if (pml4e & PG_PRESENT_MASK) {
1445 pdp_addr = pml4e & 0x3fffffffff000ULL;
1446 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001447 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001448 pdpe = le64_to_cpu(pdpe);
1449 if (pdpe & PG_PRESENT_MASK) {
1450 if (pdpe & PG_PSE_MASK) {
1451 /* 1G pages, CR4.PSE is ignored */
1452 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1453 0x3ffffc0000000ULL);
1454 } else {
1455 pd_addr = pdpe & 0x3fffffffff000ULL;
1456 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001457 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001458 pde = le64_to_cpu(pde);
1459 if (pde & PG_PRESENT_MASK) {
1460 if (pde & PG_PSE_MASK) {
1461 /* 2M pages, CR4.PSE is ignored */
1462 print_pte(mon, (l1 << 39) + (l2 << 30) +
1463 (l3 << 21), pde,
1464 0x3ffffffe00000ULL);
1465 } else {
1466 pt_addr = pde & 0x3fffffffff000ULL;
1467 for (l4 = 0; l4 < 512; l4++) {
1468 cpu_physical_memory_read(pt_addr
1469 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001470 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001471 pte = le64_to_cpu(pte);
1472 if (pte & PG_PRESENT_MASK) {
1473 print_pte(mon, (l1 << 39) +
1474 (l2 << 30) +
1475 (l3 << 21) + (l4 << 12),
1476 pte & ~PG_PSE_MASK,
1477 0x3fffffffff000ULL);
1478 }
1479 }
1480 }
1481 }
1482 }
1483 }
1484 }
1485 }
1486 }
1487 }
1488}
1489#endif
1490
1491static void tlb_info(Monitor *mon)
1492{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001493 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001494
1495 env = mon_get_cpu();
1496
1497 if (!(env->cr[0] & CR0_PG_MASK)) {
1498 monitor_printf(mon, "PG disabled\n");
1499 return;
1500 }
1501 if (env->cr[4] & CR4_PAE_MASK) {
1502#ifdef TARGET_X86_64
1503 if (env->hflags & HF_LMA_MASK) {
1504 tlb_info_64(mon, env);
1505 } else
1506#endif
1507 {
1508 tlb_info_pae32(mon, env);
1509 }
1510 } else {
1511 tlb_info_32(mon, env);
1512 }
1513}
1514
Avi Kivitya8170e52012-10-23 12:30:10 +02001515static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001516 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001517 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001518{
bellard9746b152004-11-11 18:30:24 +00001519 int prot1;
1520 prot1 = *plast_prot;
1521 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001522 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001523 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1524 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001525 *pstart, end, end - *pstart,
1526 prot1 & PG_USER_MASK ? 'u' : '-',
1527 'r',
1528 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001529 }
1530 if (prot != 0)
1531 *pstart = end;
1532 else
1533 *pstart = -1;
1534 *plast_prot = prot;
1535 }
1536}
1537
Andreas Färber9349b4f2012-03-14 01:38:32 +01001538static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001539{
Austin Clementsb49ca722011-08-14 23:19:21 -04001540 unsigned int l1, l2;
1541 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001542 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001543 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001544
bellardb86bda52004-09-18 19:32:46 +00001545 pgd = env->cr[3] & ~0xfff;
1546 last_prot = 0;
1547 start = -1;
1548 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001549 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001550 pde = le32_to_cpu(pde);
1551 end = l1 << 22;
1552 if (pde & PG_PRESENT_MASK) {
1553 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1554 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001555 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001556 } else {
1557 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001558 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001559 pte = le32_to_cpu(pte);
1560 end = (l1 << 22) + (l2 << 12);
1561 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001562 prot = pte & pde &
1563 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001564 } else {
1565 prot = 0;
1566 }
aliguori376253e2009-03-05 23:01:23 +00001567 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001568 }
1569 }
1570 } else {
1571 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001572 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001573 }
1574 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001575 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001576 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001577}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001578
Andreas Färber9349b4f2012-03-14 01:38:32 +01001579static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001580{
Austin Clementsb49ca722011-08-14 23:19:21 -04001581 unsigned int l1, l2, l3;
1582 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001583 uint64_t pdpe, pde, pte;
1584 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001585 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001586
1587 pdp_addr = env->cr[3] & ~0x1f;
1588 last_prot = 0;
1589 start = -1;
1590 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001591 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001592 pdpe = le64_to_cpu(pdpe);
1593 end = l1 << 30;
1594 if (pdpe & PG_PRESENT_MASK) {
1595 pd_addr = pdpe & 0x3fffffffff000ULL;
1596 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001597 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001598 pde = le64_to_cpu(pde);
1599 end = (l1 << 30) + (l2 << 21);
1600 if (pde & PG_PRESENT_MASK) {
1601 if (pde & PG_PSE_MASK) {
1602 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1603 PG_PRESENT_MASK);
1604 mem_print(mon, &start, &last_prot, end, prot);
1605 } else {
1606 pt_addr = pde & 0x3fffffffff000ULL;
1607 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001608 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001609 pte = le64_to_cpu(pte);
1610 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1611 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001612 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1613 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001614 } else {
1615 prot = 0;
1616 }
1617 mem_print(mon, &start, &last_prot, end, prot);
1618 }
1619 }
1620 } else {
1621 prot = 0;
1622 mem_print(mon, &start, &last_prot, end, prot);
1623 }
1624 }
1625 } else {
1626 prot = 0;
1627 mem_print(mon, &start, &last_prot, end, prot);
1628 }
1629 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001630 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001631 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001632}
1633
1634
1635#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001636static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001637{
1638 int prot, last_prot;
1639 uint64_t l1, l2, l3, l4;
1640 uint64_t pml4e, pdpe, pde, pte;
1641 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1642
1643 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1644 last_prot = 0;
1645 start = -1;
1646 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001647 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001648 pml4e = le64_to_cpu(pml4e);
1649 end = l1 << 39;
1650 if (pml4e & PG_PRESENT_MASK) {
1651 pdp_addr = pml4e & 0x3fffffffff000ULL;
1652 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001653 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001654 pdpe = le64_to_cpu(pdpe);
1655 end = (l1 << 39) + (l2 << 30);
1656 if (pdpe & PG_PRESENT_MASK) {
1657 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001658 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1659 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001660 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001661 mem_print(mon, &start, &last_prot, end, prot);
1662 } else {
1663 pd_addr = pdpe & 0x3fffffffff000ULL;
1664 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001665 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001666 pde = le64_to_cpu(pde);
1667 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1668 if (pde & PG_PRESENT_MASK) {
1669 if (pde & PG_PSE_MASK) {
1670 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1671 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001672 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001673 mem_print(mon, &start, &last_prot, end, prot);
1674 } else {
1675 pt_addr = pde & 0x3fffffffff000ULL;
1676 for (l4 = 0; l4 < 512; l4++) {
1677 cpu_physical_memory_read(pt_addr
1678 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001679 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001680 pte = le64_to_cpu(pte);
1681 end = (l1 << 39) + (l2 << 30) +
1682 (l3 << 21) + (l4 << 12);
1683 if (pte & PG_PRESENT_MASK) {
1684 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1685 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001686 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001687 } else {
1688 prot = 0;
1689 }
1690 mem_print(mon, &start, &last_prot, end, prot);
1691 }
1692 }
1693 } else {
1694 prot = 0;
1695 mem_print(mon, &start, &last_prot, end, prot);
1696 }
1697 }
1698 }
1699 } else {
1700 prot = 0;
1701 mem_print(mon, &start, &last_prot, end, prot);
1702 }
1703 }
1704 } else {
1705 prot = 0;
1706 mem_print(mon, &start, &last_prot, end, prot);
1707 }
1708 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001709 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001710 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001711}
1712#endif
1713
1714static void mem_info(Monitor *mon)
1715{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001716 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001717
1718 env = mon_get_cpu();
1719
1720 if (!(env->cr[0] & CR0_PG_MASK)) {
1721 monitor_printf(mon, "PG disabled\n");
1722 return;
1723 }
1724 if (env->cr[4] & CR4_PAE_MASK) {
1725#ifdef TARGET_X86_64
1726 if (env->hflags & HF_LMA_MASK) {
1727 mem_info_64(mon, env);
1728 } else
1729#endif
1730 {
1731 mem_info_pae32(mon, env);
1732 }
1733 } else {
1734 mem_info_32(mon, env);
1735 }
1736}
bellardb86bda52004-09-18 19:32:46 +00001737#endif
1738
aurel327c664e22009-03-03 06:12:22 +00001739#if defined(TARGET_SH4)
1740
aliguori376253e2009-03-05 23:01:23 +00001741static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001742{
aliguori376253e2009-03-05 23:01:23 +00001743 monitor_printf(mon, " tlb%i:\t"
1744 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1745 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1746 "dirty=%hhu writethrough=%hhu\n",
1747 idx,
1748 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1749 tlb->v, tlb->sh, tlb->c, tlb->pr,
1750 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001751}
1752
aliguori376253e2009-03-05 23:01:23 +00001753static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001754{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001755 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00001756 int i;
1757
aliguori376253e2009-03-05 23:01:23 +00001758 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001759 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001760 print_tlb (mon, i, &env->itlb[i]);
1761 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001762 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001763 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001764}
1765
1766#endif
1767
Max Filippov692f7372012-01-07 20:02:40 +04001768#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Blue Swirld41160a2010-12-19 13:42:56 +00001769static void tlb_info(Monitor *mon)
1770{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001771 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00001772
1773 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1774}
1775#endif
1776
Blue Swirl314e2982011-09-11 20:22:05 +00001777static void do_info_mtree(Monitor *mon)
1778{
1779 mtree_info((fprintf_function)monitor_printf, mon);
1780}
1781
aliguori030ea372009-04-21 22:30:47 +00001782static void do_info_numa(Monitor *mon)
1783{
aliguorib28b6232009-04-22 20:20:29 +00001784 int i;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001785 CPUArchState *env;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001786 CPUState *cpu;
aliguori030ea372009-04-21 22:30:47 +00001787
1788 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1789 for (i = 0; i < nb_numa_nodes; i++) {
1790 monitor_printf(mon, "node %d cpus:", i);
1791 for (env = first_cpu; env != NULL; env = env->next_cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001792 cpu = ENV_GET_CPU(env);
1793 if (cpu->numa_node == i) {
aliguori030ea372009-04-21 22:30:47 +00001794 monitor_printf(mon, " %d", env->cpu_index);
1795 }
1796 }
1797 monitor_printf(mon, "\n");
1798 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1799 node_mem[i] >> 20);
1800 }
1801}
1802
bellard5f1ce942006-02-08 22:40:15 +00001803#ifdef CONFIG_PROFILER
1804
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001805int64_t qemu_time;
1806int64_t dev_time;
1807
aliguori376253e2009-03-05 23:01:23 +00001808static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001809{
1810 int64_t total;
1811 total = qemu_time;
1812 if (total == 0)
1813 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001814 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001815 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001816 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001817 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001818 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001819 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001820}
1821#else
aliguori376253e2009-03-05 23:01:23 +00001822static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001823{
aliguori376253e2009-03-05 23:01:23 +00001824 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001825}
1826#endif
1827
bellardec36b692006-07-16 18:57:03 +00001828/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001829static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001830
aliguori376253e2009-03-05 23:01:23 +00001831static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001832{
1833 int i;
1834 CaptureState *s;
1835
1836 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001837 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001838 s->ops.info (s->opaque);
1839 }
1840}
1841
Blue Swirl23130862009-06-06 08:22:04 +00001842#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001843static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001844{
1845 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001846 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001847 CaptureState *s;
1848
1849 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1850 if (i == n) {
1851 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001852 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001853 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001854 return;
1855 }
1856 }
1857}
1858
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001859static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001860{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001861 const char *path = qdict_get_str(qdict, "path");
1862 int has_freq = qdict_haskey(qdict, "freq");
1863 int freq = qdict_get_try_int(qdict, "freq", -1);
1864 int has_bits = qdict_haskey(qdict, "bits");
1865 int bits = qdict_get_try_int(qdict, "bits", -1);
1866 int has_channels = qdict_haskey(qdict, "nchannels");
1867 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001868 CaptureState *s;
1869
Anthony Liguori7267c092011-08-20 22:09:37 -05001870 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001871
1872 freq = has_freq ? freq : 44100;
1873 bits = has_bits ? bits : 16;
1874 nchannels = has_channels ? nchannels : 2;
1875
1876 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001877 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001878 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001879 return;
bellardec36b692006-07-16 18:57:03 +00001880 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001881 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001882}
1883#endif
1884
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001885static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001886{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001887 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001888
aliguori76655d62009-03-06 20:27:37 +00001889 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001890 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001891 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001892 return acl;
1893}
aliguori76655d62009-03-06 20:27:37 +00001894
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001895static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001896{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001897 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001898 qemu_acl *acl = find_acl(mon, aclname);
1899 qemu_acl_entry *entry;
1900 int i = 0;
1901
1902 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001903 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001904 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001905 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001906 i++;
1907 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001908 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001909 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001910 }
1911}
1912
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001913static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001914{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001915 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001916 qemu_acl *acl = find_acl(mon, aclname);
1917
1918 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001919 qemu_acl_reset(acl);
1920 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001921 }
1922}
aliguori76655d62009-03-06 20:27:37 +00001923
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001924static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001925{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001926 const char *aclname = qdict_get_str(qdict, "aclname");
1927 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001928 qemu_acl *acl = find_acl(mon, aclname);
1929
1930 if (acl) {
1931 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001932 acl->defaultDeny = 0;
1933 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001934 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001935 acl->defaultDeny = 1;
1936 monitor_printf(mon, "acl: policy set to 'deny'\n");
1937 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001938 monitor_printf(mon, "acl: unknown policy '%s', "
1939 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001940 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001941 }
1942}
aliguori76655d62009-03-06 20:27:37 +00001943
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001944static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001945{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001946 const char *aclname = qdict_get_str(qdict, "aclname");
1947 const char *match = qdict_get_str(qdict, "match");
1948 const char *policy = qdict_get_str(qdict, "policy");
1949 int has_index = qdict_haskey(qdict, "index");
1950 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001951 qemu_acl *acl = find_acl(mon, aclname);
1952 int deny, ret;
1953
1954 if (acl) {
1955 if (strcmp(policy, "allow") == 0) {
1956 deny = 0;
1957 } else if (strcmp(policy, "deny") == 0) {
1958 deny = 1;
1959 } else {
1960 monitor_printf(mon, "acl: unknown policy '%s', "
1961 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001962 return;
1963 }
aliguori28a76be2009-03-06 20:27:40 +00001964 if (has_index)
1965 ret = qemu_acl_insert(acl, deny, match, index);
1966 else
1967 ret = qemu_acl_append(acl, deny, match);
1968 if (ret < 0)
1969 monitor_printf(mon, "acl: unable to add acl entry\n");
1970 else
1971 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001972 }
1973}
aliguori76655d62009-03-06 20:27:37 +00001974
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001975static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001976{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001977 const char *aclname = qdict_get_str(qdict, "aclname");
1978 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001979 qemu_acl *acl = find_acl(mon, aclname);
1980 int ret;
aliguori76655d62009-03-06 20:27:37 +00001981
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001982 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001983 ret = qemu_acl_remove(acl, match);
1984 if (ret < 0)
1985 monitor_printf(mon, "acl: no matching acl entry\n");
1986 else
1987 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001988 }
1989}
1990
Huang Ying79c4f6b2009-06-23 10:05:14 +08001991#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001992static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001993{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02001994 X86CPU *cpu;
1995 CPUX86State *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001996 int cpu_index = qdict_get_int(qdict, "cpu_index");
1997 int bank = qdict_get_int(qdict, "bank");
1998 uint64_t status = qdict_get_int(qdict, "status");
1999 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2000 uint64_t addr = qdict_get_int(qdict, "addr");
2001 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002002 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002003
Jan Kiszka747461c2011-03-02 08:56:10 +01002004 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2005 flags |= MCE_INJECT_BROADCAST;
2006 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002007 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002008 cpu = x86_env_get_cpu(cenv);
Jan Kiszka316378e2011-03-02 08:56:09 +01002009 if (cenv->cpu_index == cpu_index) {
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002010 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002011 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002012 break;
2013 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002014 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002015}
2016#endif
2017
Corey Bryant208c9d12012-06-22 14:36:09 -04002018void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002019{
Anthony Liguoric227f092009-10-01 16:12:16 -05002020 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002021 int fd;
2022
Corey Bryant208c9d12012-06-22 14:36:09 -04002023 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002024 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002025 error_set(errp, QERR_FD_NOT_SUPPLIED);
2026 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002027 }
2028
2029 if (qemu_isdigit(fdname[0])) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002030 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2031 "a name not starting with a digit");
2032 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002033 }
2034
Corey Bryant208c9d12012-06-22 14:36:09 -04002035 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002036 if (strcmp(monfd->name, fdname) != 0) {
2037 continue;
2038 }
2039
2040 close(monfd->fd);
2041 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002042 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002043 }
2044
Anthony Liguori7267c092011-08-20 22:09:37 -05002045 monfd = g_malloc0(sizeof(mon_fd_t));
2046 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002047 monfd->fd = fd;
2048
Corey Bryant208c9d12012-06-22 14:36:09 -04002049 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002050}
2051
Corey Bryant208c9d12012-06-22 14:36:09 -04002052void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002053{
Anthony Liguoric227f092009-10-01 16:12:16 -05002054 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002055
Corey Bryant208c9d12012-06-22 14:36:09 -04002056 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002057 if (strcmp(monfd->name, fdname) != 0) {
2058 continue;
2059 }
2060
Blue Swirl72cf2d42009-09-12 07:36:22 +00002061 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002062 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002063 g_free(monfd->name);
2064 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002065 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002066 }
2067
Corey Bryant208c9d12012-06-22 14:36:09 -04002068 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002069}
2070
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002071static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002072{
Luiz Capitulino13548692011-07-29 15:36:43 -03002073 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002074 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002075
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002076 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002077
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002078 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002079 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002080 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002081}
2082
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002083int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002084{
Anthony Liguoric227f092009-10-01 16:12:16 -05002085 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002086
Blue Swirl72cf2d42009-09-12 07:36:22 +00002087 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002088 int fd;
2089
2090 if (strcmp(monfd->name, fdname) != 0) {
2091 continue;
2092 }
2093
2094 fd = monfd->fd;
2095
2096 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002097 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002098 g_free(monfd->name);
2099 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002100
2101 return fd;
2102 }
2103
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002104 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002105 return -1;
2106}
2107
Corey Bryantba1c0482012-08-14 16:43:43 -04002108static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2109{
2110 MonFdsetFd *mon_fdset_fd;
2111 MonFdsetFd *mon_fdset_fd_next;
2112
2113 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002114 if ((mon_fdset_fd->removed ||
2115 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2116 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002117 close(mon_fdset_fd->fd);
2118 g_free(mon_fdset_fd->opaque);
2119 QLIST_REMOVE(mon_fdset_fd, next);
2120 g_free(mon_fdset_fd);
2121 }
2122 }
2123
Corey Bryantadb696f2012-08-14 16:43:47 -04002124 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002125 QLIST_REMOVE(mon_fdset, next);
2126 g_free(mon_fdset);
2127 }
2128}
2129
Corey Bryantefb87c12012-08-14 16:43:48 -04002130static void monitor_fdsets_cleanup(void)
2131{
2132 MonFdset *mon_fdset;
2133 MonFdset *mon_fdset_next;
2134
2135 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2136 monitor_fdset_cleanup(mon_fdset);
2137 }
2138}
2139
Corey Bryantba1c0482012-08-14 16:43:43 -04002140AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2141 const char *opaque, Error **errp)
2142{
2143 int fd;
2144 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002145 AddfdInfo *fdinfo;
2146
2147 fd = qemu_chr_fe_get_msgfd(mon->chr);
2148 if (fd == -1) {
2149 error_set(errp, QERR_FD_NOT_SUPPLIED);
2150 goto error;
2151 }
2152
Corey Bryante446f702012-10-18 15:19:32 -04002153 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2154 has_opaque, opaque, errp);
2155 if (fdinfo) {
2156 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002157 }
2158
Corey Bryantba1c0482012-08-14 16:43:43 -04002159error:
2160 if (fd != -1) {
2161 close(fd);
2162 }
2163 return NULL;
2164}
2165
2166void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2167{
2168 MonFdset *mon_fdset;
2169 MonFdsetFd *mon_fdset_fd;
2170 char fd_str[60];
2171
2172 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2173 if (mon_fdset->id != fdset_id) {
2174 continue;
2175 }
2176 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2177 if (has_fd) {
2178 if (mon_fdset_fd->fd != fd) {
2179 continue;
2180 }
2181 mon_fdset_fd->removed = true;
2182 break;
2183 } else {
2184 mon_fdset_fd->removed = true;
2185 }
2186 }
2187 if (has_fd && !mon_fdset_fd) {
2188 goto error;
2189 }
2190 monitor_fdset_cleanup(mon_fdset);
2191 return;
2192 }
2193
2194error:
2195 if (has_fd) {
2196 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2197 fdset_id, fd);
2198 } else {
2199 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2200 }
2201 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2202}
2203
2204FdsetInfoList *qmp_query_fdsets(Error **errp)
2205{
2206 MonFdset *mon_fdset;
2207 MonFdsetFd *mon_fdset_fd;
2208 FdsetInfoList *fdset_list = NULL;
2209
2210 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2211 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2212 FdsetFdInfoList *fdsetfd_list = NULL;
2213
2214 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2215 fdset_info->value->fdset_id = mon_fdset->id;
2216
2217 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2218 FdsetFdInfoList *fdsetfd_info;
2219
2220 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2221 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2222 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2223 if (mon_fdset_fd->opaque) {
2224 fdsetfd_info->value->has_opaque = true;
2225 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2226 } else {
2227 fdsetfd_info->value->has_opaque = false;
2228 }
2229
2230 fdsetfd_info->next = fdsetfd_list;
2231 fdsetfd_list = fdsetfd_info;
2232 }
2233
2234 fdset_info->value->fds = fdsetfd_list;
2235
2236 fdset_info->next = fdset_list;
2237 fdset_list = fdset_info;
2238 }
2239
2240 return fdset_list;
2241}
2242
Corey Bryante446f702012-10-18 15:19:32 -04002243AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2244 bool has_opaque, const char *opaque,
2245 Error **errp)
2246{
2247 MonFdset *mon_fdset = NULL;
2248 MonFdsetFd *mon_fdset_fd;
2249 AddfdInfo *fdinfo;
2250
2251 if (has_fdset_id) {
2252 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2253 /* Break if match found or match impossible due to ordering by ID */
2254 if (fdset_id <= mon_fdset->id) {
2255 if (fdset_id < mon_fdset->id) {
2256 mon_fdset = NULL;
2257 }
2258 break;
2259 }
2260 }
2261 }
2262
2263 if (mon_fdset == NULL) {
2264 int64_t fdset_id_prev = -1;
2265 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2266
2267 if (has_fdset_id) {
2268 if (fdset_id < 0) {
2269 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2270 "a non-negative value");
2271 return NULL;
2272 }
2273 /* Use specified fdset ID */
2274 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2275 mon_fdset_cur = mon_fdset;
2276 if (fdset_id < mon_fdset_cur->id) {
2277 break;
2278 }
2279 }
2280 } else {
2281 /* Use first available fdset ID */
2282 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2283 mon_fdset_cur = mon_fdset;
2284 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2285 fdset_id_prev = mon_fdset_cur->id;
2286 continue;
2287 }
2288 break;
2289 }
2290 }
2291
2292 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2293 if (has_fdset_id) {
2294 mon_fdset->id = fdset_id;
2295 } else {
2296 mon_fdset->id = fdset_id_prev + 1;
2297 }
2298
2299 /* The fdset list is ordered by fdset ID */
2300 if (!mon_fdset_cur) {
2301 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2302 } else if (mon_fdset->id < mon_fdset_cur->id) {
2303 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2304 } else {
2305 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2306 }
2307 }
2308
2309 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2310 mon_fdset_fd->fd = fd;
2311 mon_fdset_fd->removed = false;
2312 if (has_opaque) {
2313 mon_fdset_fd->opaque = g_strdup(opaque);
2314 }
2315 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2316
2317 fdinfo = g_malloc0(sizeof(*fdinfo));
2318 fdinfo->fdset_id = mon_fdset->id;
2319 fdinfo->fd = mon_fdset_fd->fd;
2320
2321 return fdinfo;
2322}
2323
Corey Bryantadb696f2012-08-14 16:43:47 -04002324int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2325{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002326#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002327 MonFdset *mon_fdset;
2328 MonFdsetFd *mon_fdset_fd;
2329 int mon_fd_flags;
2330
Corey Bryantadb696f2012-08-14 16:43:47 -04002331 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2332 if (mon_fdset->id != fdset_id) {
2333 continue;
2334 }
2335 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2336 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2337 if (mon_fd_flags == -1) {
2338 return -1;
2339 }
2340
2341 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2342 return mon_fdset_fd->fd;
2343 }
2344 }
2345 errno = EACCES;
2346 return -1;
2347 }
2348#endif
2349
2350 errno = ENOENT;
2351 return -1;
2352}
2353
2354int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2355{
2356 MonFdset *mon_fdset;
2357 MonFdsetFd *mon_fdset_fd_dup;
2358
2359 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2360 if (mon_fdset->id != fdset_id) {
2361 continue;
2362 }
2363 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2364 if (mon_fdset_fd_dup->fd == dup_fd) {
2365 return -1;
2366 }
2367 }
2368 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2369 mon_fdset_fd_dup->fd = dup_fd;
2370 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2371 return 0;
2372 }
2373 return -1;
2374}
2375
2376static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2377{
2378 MonFdset *mon_fdset;
2379 MonFdsetFd *mon_fdset_fd_dup;
2380
2381 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2382 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2383 if (mon_fdset_fd_dup->fd == dup_fd) {
2384 if (remove) {
2385 QLIST_REMOVE(mon_fdset_fd_dup, next);
2386 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2387 monitor_fdset_cleanup(mon_fdset);
2388 }
2389 }
2390 return mon_fdset->id;
2391 }
2392 }
2393 }
2394 return -1;
2395}
2396
2397int monitor_fdset_dup_fd_find(int dup_fd)
2398{
2399 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2400}
2401
2402int monitor_fdset_dup_fd_remove(int dup_fd)
2403{
2404 return monitor_fdset_dup_fd_find_remove(dup_fd, true);
2405}
2406
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002407int monitor_handle_fd_param(Monitor *mon, const char *fdname)
2408{
2409 int fd;
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002410 Error *local_err = NULL;
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002411
2412 if (!qemu_isdigit(fdname[0]) && mon) {
2413
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002414 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002415 if (fd == -1) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002416 qerror_report_err(local_err);
2417 error_free(local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002418 return -1;
2419 }
2420 } else {
2421 fd = qemu_parse_fd(fdname);
2422 }
2423
2424 return fd;
2425}
2426
Wayne Xia816f8922011-10-12 11:32:41 +08002427/* mon_cmds and info_cmds would be sorted at runtime */
2428static mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002429#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002430 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002431};
2432
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002433/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002434static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002435 {
2436 .name = "version",
2437 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002438 .params = "",
2439 .help = "show the version of QEMU",
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03002440 .mhandler.info = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002441 },
2442 {
2443 .name = "network",
2444 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002445 .params = "",
2446 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002447 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002448 },
2449 {
2450 .name = "chardev",
2451 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002452 .params = "",
2453 .help = "show the character devices",
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002454 .mhandler.info = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002455 },
2456 {
2457 .name = "block",
2458 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002459 .params = "",
2460 .help = "show the block devices",
Luiz Capitulinob2023812011-09-21 17:16:47 -03002461 .mhandler.info = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002462 },
2463 {
2464 .name = "blockstats",
2465 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002466 .params = "",
2467 .help = "show block device statistics",
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03002468 .mhandler.info = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002469 },
2470 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002471 .name = "block-jobs",
2472 .args_type = "",
2473 .params = "",
2474 .help = "show progress of ongoing block device operations",
2475 .mhandler.info = hmp_info_block_jobs,
2476 },
2477 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002478 .name = "registers",
2479 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002480 .params = "",
2481 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002482 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002483 },
2484 {
2485 .name = "cpus",
2486 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002487 .params = "",
2488 .help = "show infos for each CPU",
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002489 .mhandler.info = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002490 },
2491 {
2492 .name = "history",
2493 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002494 .params = "",
2495 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002496 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002497 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002498#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2499 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002500 {
2501 .name = "irq",
2502 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002503 .params = "",
2504 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002505#ifdef TARGET_SPARC
2506 .mhandler.info = sun4m_irq_info,
2507#elif defined(TARGET_LM32)
2508 .mhandler.info = lm32_irq_info,
2509#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002510 .mhandler.info = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002511#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002512 },
2513 {
2514 .name = "pic",
2515 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002516 .params = "",
2517 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002518#ifdef TARGET_SPARC
2519 .mhandler.info = sun4m_pic_info,
2520#elif defined(TARGET_LM32)
2521 .mhandler.info = lm32_do_pic_info,
2522#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002523 .mhandler.info = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002524#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002525 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002526#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002527 {
2528 .name = "pci",
2529 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002530 .params = "",
2531 .help = "show PCI info",
Luiz Capitulino79627472011-10-21 14:15:33 -02002532 .mhandler.info = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002533 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002534#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002535 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002536 {
2537 .name = "tlb",
2538 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002539 .params = "",
2540 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002541 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002542 },
aurel327c664e22009-03-03 06:12:22 +00002543#endif
2544#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002545 {
2546 .name = "mem",
2547 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002548 .params = "",
2549 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002550 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002551 },
bellardb86bda52004-09-18 19:32:46 +00002552#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002553 {
Blue Swirl314e2982011-09-11 20:22:05 +00002554 .name = "mtree",
2555 .args_type = "",
2556 .params = "",
2557 .help = "show memory tree",
2558 .mhandler.info = do_info_mtree,
2559 },
2560 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002561 .name = "jit",
2562 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002563 .params = "",
2564 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002565 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002566 },
2567 {
2568 .name = "kvm",
2569 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002570 .params = "",
2571 .help = "show KVM information",
Luiz Capitulino292a2602011-09-12 15:10:53 -03002572 .mhandler.info = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002573 },
2574 {
2575 .name = "numa",
2576 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002577 .params = "",
2578 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002579 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 },
2581 {
2582 .name = "usb",
2583 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002584 .params = "",
2585 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002586 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002587 },
2588 {
2589 .name = "usbhost",
2590 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002591 .params = "",
2592 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002593 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002594 },
2595 {
2596 .name = "profile",
2597 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002598 .params = "",
2599 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002600 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002601 },
2602 {
2603 .name = "capture",
2604 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002605 .params = "",
2606 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002607 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002608 },
2609 {
2610 .name = "snapshots",
2611 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002612 .params = "",
2613 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002614 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002615 },
2616 {
2617 .name = "status",
2618 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002619 .params = "",
2620 .help = "show the current VM status (running|paused)",
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -03002621 .mhandler.info = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002622 },
2623 {
2624 .name = "pcmcia",
2625 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002626 .params = "",
2627 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002628 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002629 },
2630 {
2631 .name = "mice",
2632 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002633 .params = "",
2634 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe235cec2011-09-21 15:29:55 -03002635 .mhandler.info = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002636 },
2637 {
2638 .name = "vnc",
2639 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002640 .params = "",
2641 .help = "show the vnc server status",
Luiz Capitulino2b54aa82011-10-17 16:41:22 -02002642 .mhandler.info = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002643 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002644#if defined(CONFIG_SPICE)
2645 {
2646 .name = "spice",
2647 .args_type = "",
2648 .params = "",
2649 .help = "show the spice server status",
Luiz Capitulinod1f29642011-10-20 17:01:33 -02002650 .mhandler.info = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002651 },
2652#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002653 {
2654 .name = "name",
2655 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002656 .params = "",
2657 .help = "show the current VM name",
Anthony Liguori48a32be2011-09-02 12:34:48 -05002658 .mhandler.info = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002659 },
2660 {
2661 .name = "uuid",
2662 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002663 .params = "",
2664 .help = "show the current VM UUID",
Luiz Capitulinoefab7672011-09-13 17:16:25 -03002665 .mhandler.info = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002666 },
j_mayer76a66252007-03-07 08:32:30 +00002667#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002668 {
2669 .name = "cpustats",
2670 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 .params = "",
2672 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002673 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002674 },
j_mayer76a66252007-03-07 08:32:30 +00002675#endif
blueswir131a60e22007-10-26 18:42:59 +00002676#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002677 {
2678 .name = "usernet",
2679 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002680 .params = "",
2681 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002682 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002683 },
blueswir131a60e22007-10-26 18:42:59 +00002684#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002685 {
2686 .name = "migrate",
2687 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002688 .params = "",
2689 .help = "show migration status",
Luiz Capitulino791e7c82011-09-13 17:37:16 -03002690 .mhandler.info = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002691 },
2692 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002693 .name = "migrate_capabilities",
2694 .args_type = "",
2695 .params = "",
2696 .help = "show current migration capabilities",
2697 .mhandler.info = hmp_info_migrate_capabilities,
2698 },
2699 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002700 .name = "migrate_cache_size",
2701 .args_type = "",
2702 .params = "",
2703 .help = "show current migration xbzrle cache size",
2704 .mhandler.info = hmp_info_migrate_cache_size,
2705 },
2706 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002707 .name = "balloon",
2708 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002709 .params = "",
2710 .help = "show balloon information",
Luiz Capitulino96637bc2011-10-21 11:41:37 -02002711 .mhandler.info = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002712 },
2713 {
2714 .name = "qtree",
2715 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002716 .params = "",
2717 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002718 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002719 },
2720 {
2721 .name = "qdm",
2722 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002723 .params = "",
2724 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002725 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002726 },
2727 {
2728 .name = "roms",
2729 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002730 .params = "",
2731 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002732 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002733 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302734 {
2735 .name = "trace-events",
2736 .args_type = "",
2737 .params = "",
2738 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02002739 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302740 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002741 {
2742 .name = NULL,
2743 },
bellard9dc39cb2004-03-14 21:38:27 +00002744};
2745
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002746static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002747#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002748 { /* NULL */ },
2749};
2750
bellard9307c4c2004-04-04 12:57:25 +00002751/*******************************************************************/
2752
2753static const char *pch;
2754static jmp_buf expr_env;
2755
bellard92a31b12005-02-10 22:00:52 +00002756#define MD_TLONG 0
2757#define MD_I32 1
2758
bellard9307c4c2004-04-04 12:57:25 +00002759typedef struct MonitorDef {
2760 const char *name;
2761 int offset;
blueswir18662d652008-10-02 18:32:44 +00002762 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002763 int type;
bellard9307c4c2004-04-04 12:57:25 +00002764} MonitorDef;
2765
bellard57206fd2004-04-25 18:54:52 +00002766#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002767static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002768{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002769 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002770 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002771}
2772#endif
2773
bellarda541f292004-04-12 20:39:29 +00002774#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002775static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002776{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002777 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002778 unsigned int u;
2779 int i;
2780
2781 u = 0;
2782 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002783 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002784
2785 return u;
2786}
2787
blueswir18662d652008-10-02 18:32:44 +00002788static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002789{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002790 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002791 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002792}
2793
blueswir18662d652008-10-02 18:32:44 +00002794static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002795{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002796 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002797 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002798}
bellard9fddaa02004-05-21 12:59:32 +00002799
blueswir18662d652008-10-02 18:32:44 +00002800static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002801{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002802 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002803 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002804}
2805
blueswir18662d652008-10-02 18:32:44 +00002806static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002807{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002808 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002809 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002810}
2811
blueswir18662d652008-10-02 18:32:44 +00002812static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002813{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002814 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002815 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002816}
bellarda541f292004-04-12 20:39:29 +00002817#endif
2818
bellarde95c8d52004-09-30 22:22:08 +00002819#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002820#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002821static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002822{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002823 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002824
2825 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002826}
bellard7b936c02005-10-30 17:05:13 +00002827#endif
bellarde95c8d52004-09-30 22:22:08 +00002828
blueswir18662d652008-10-02 18:32:44 +00002829static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002830{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002831 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002832 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002833}
2834#endif
2835
blueswir18662d652008-10-02 18:32:44 +00002836static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002837#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002838
2839#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01002840 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2841 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2842 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002843
Andreas Färbere59d1672012-02-16 00:40:47 +01002844 { "eax", offsetof(CPUX86State, regs[0]) },
2845 { "ecx", offsetof(CPUX86State, regs[1]) },
2846 { "edx", offsetof(CPUX86State, regs[2]) },
2847 { "ebx", offsetof(CPUX86State, regs[3]) },
2848 { "esp|sp", offsetof(CPUX86State, regs[4]) },
2849 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
2850 { "esi", offsetof(CPUX86State, regs[6]) },
2851 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002852#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01002853 { "r8", offsetof(CPUX86State, regs[8]) },
2854 { "r9", offsetof(CPUX86State, regs[9]) },
2855 { "r10", offsetof(CPUX86State, regs[10]) },
2856 { "r11", offsetof(CPUX86State, regs[11]) },
2857 { "r12", offsetof(CPUX86State, regs[12]) },
2858 { "r13", offsetof(CPUX86State, regs[13]) },
2859 { "r14", offsetof(CPUX86State, regs[14]) },
2860 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00002861#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01002862 { "eflags", offsetof(CPUX86State, eflags) },
2863 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00002864 SEG("cs", R_CS)
2865 SEG("ds", R_DS)
2866 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002867 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002868 SEG("fs", R_FS)
2869 SEG("gs", R_GS)
2870 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002871#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002872 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01002873 { "r0", offsetof(CPUPPCState, gpr[0]) },
2874 { "r1", offsetof(CPUPPCState, gpr[1]) },
2875 { "r2", offsetof(CPUPPCState, gpr[2]) },
2876 { "r3", offsetof(CPUPPCState, gpr[3]) },
2877 { "r4", offsetof(CPUPPCState, gpr[4]) },
2878 { "r5", offsetof(CPUPPCState, gpr[5]) },
2879 { "r6", offsetof(CPUPPCState, gpr[6]) },
2880 { "r7", offsetof(CPUPPCState, gpr[7]) },
2881 { "r8", offsetof(CPUPPCState, gpr[8]) },
2882 { "r9", offsetof(CPUPPCState, gpr[9]) },
2883 { "r10", offsetof(CPUPPCState, gpr[10]) },
2884 { "r11", offsetof(CPUPPCState, gpr[11]) },
2885 { "r12", offsetof(CPUPPCState, gpr[12]) },
2886 { "r13", offsetof(CPUPPCState, gpr[13]) },
2887 { "r14", offsetof(CPUPPCState, gpr[14]) },
2888 { "r15", offsetof(CPUPPCState, gpr[15]) },
2889 { "r16", offsetof(CPUPPCState, gpr[16]) },
2890 { "r17", offsetof(CPUPPCState, gpr[17]) },
2891 { "r18", offsetof(CPUPPCState, gpr[18]) },
2892 { "r19", offsetof(CPUPPCState, gpr[19]) },
2893 { "r20", offsetof(CPUPPCState, gpr[20]) },
2894 { "r21", offsetof(CPUPPCState, gpr[21]) },
2895 { "r22", offsetof(CPUPPCState, gpr[22]) },
2896 { "r23", offsetof(CPUPPCState, gpr[23]) },
2897 { "r24", offsetof(CPUPPCState, gpr[24]) },
2898 { "r25", offsetof(CPUPPCState, gpr[25]) },
2899 { "r26", offsetof(CPUPPCState, gpr[26]) },
2900 { "r27", offsetof(CPUPPCState, gpr[27]) },
2901 { "r28", offsetof(CPUPPCState, gpr[28]) },
2902 { "r29", offsetof(CPUPPCState, gpr[29]) },
2903 { "r30", offsetof(CPUPPCState, gpr[30]) },
2904 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002905 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01002906 { "f0", offsetof(CPUPPCState, fpr[0]) },
2907 { "f1", offsetof(CPUPPCState, fpr[1]) },
2908 { "f2", offsetof(CPUPPCState, fpr[2]) },
2909 { "f3", offsetof(CPUPPCState, fpr[3]) },
2910 { "f4", offsetof(CPUPPCState, fpr[4]) },
2911 { "f5", offsetof(CPUPPCState, fpr[5]) },
2912 { "f6", offsetof(CPUPPCState, fpr[6]) },
2913 { "f7", offsetof(CPUPPCState, fpr[7]) },
2914 { "f8", offsetof(CPUPPCState, fpr[8]) },
2915 { "f9", offsetof(CPUPPCState, fpr[9]) },
2916 { "f10", offsetof(CPUPPCState, fpr[10]) },
2917 { "f11", offsetof(CPUPPCState, fpr[11]) },
2918 { "f12", offsetof(CPUPPCState, fpr[12]) },
2919 { "f13", offsetof(CPUPPCState, fpr[13]) },
2920 { "f14", offsetof(CPUPPCState, fpr[14]) },
2921 { "f15", offsetof(CPUPPCState, fpr[15]) },
2922 { "f16", offsetof(CPUPPCState, fpr[16]) },
2923 { "f17", offsetof(CPUPPCState, fpr[17]) },
2924 { "f18", offsetof(CPUPPCState, fpr[18]) },
2925 { "f19", offsetof(CPUPPCState, fpr[19]) },
2926 { "f20", offsetof(CPUPPCState, fpr[20]) },
2927 { "f21", offsetof(CPUPPCState, fpr[21]) },
2928 { "f22", offsetof(CPUPPCState, fpr[22]) },
2929 { "f23", offsetof(CPUPPCState, fpr[23]) },
2930 { "f24", offsetof(CPUPPCState, fpr[24]) },
2931 { "f25", offsetof(CPUPPCState, fpr[25]) },
2932 { "f26", offsetof(CPUPPCState, fpr[26]) },
2933 { "f27", offsetof(CPUPPCState, fpr[27]) },
2934 { "f28", offsetof(CPUPPCState, fpr[28]) },
2935 { "f29", offsetof(CPUPPCState, fpr[29]) },
2936 { "f30", offsetof(CPUPPCState, fpr[30]) },
2937 { "f31", offsetof(CPUPPCState, fpr[31]) },
2938 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00002939 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01002940 { "nip|pc", offsetof(CPUPPCState, nip) },
2941 { "lr", offsetof(CPUPPCState, lr) },
2942 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002943 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002944 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002945 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002946 { "msr", 0, &monitor_get_msr, },
2947 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002948 { "tbu", 0, &monitor_get_tbu, },
2949 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002950#if defined(TARGET_PPC64)
2951 /* Address space register */
Andreas Färbere59d1672012-02-16 00:40:47 +01002952 { "asr", offsetof(CPUPPCState, asr) },
j_mayerff937db2007-09-19 05:49:13 +00002953#endif
2954 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01002955 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
2956 { "sr0", offsetof(CPUPPCState, sr[0]) },
2957 { "sr1", offsetof(CPUPPCState, sr[1]) },
2958 { "sr2", offsetof(CPUPPCState, sr[2]) },
2959 { "sr3", offsetof(CPUPPCState, sr[3]) },
2960 { "sr4", offsetof(CPUPPCState, sr[4]) },
2961 { "sr5", offsetof(CPUPPCState, sr[5]) },
2962 { "sr6", offsetof(CPUPPCState, sr[6]) },
2963 { "sr7", offsetof(CPUPPCState, sr[7]) },
2964 { "sr8", offsetof(CPUPPCState, sr[8]) },
2965 { "sr9", offsetof(CPUPPCState, sr[9]) },
2966 { "sr10", offsetof(CPUPPCState, sr[10]) },
2967 { "sr11", offsetof(CPUPPCState, sr[11]) },
2968 { "sr12", offsetof(CPUPPCState, sr[12]) },
2969 { "sr13", offsetof(CPUPPCState, sr[13]) },
2970 { "sr14", offsetof(CPUPPCState, sr[14]) },
2971 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05002972 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01002973 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05002974
Andreas Färbere59d1672012-02-16 00:40:47 +01002975 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
2976 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
2977 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
2978 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
2979 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
2980 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
2981 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
2982 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
2983 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
2984 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
2985 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
2986 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
2987 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
2988 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
2989 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
2990 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
2991 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
2992 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
2993 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
2994 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
2995 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
2996 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
2997 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
2998 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
2999 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3000 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3001 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3002 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3003 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3004 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3005 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3006 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3007 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3008 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3009 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3010 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3011 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3012 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3013 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3014 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3015 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3016 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3017 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3018 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3019 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3020 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3021 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3022 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3023 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3024 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3025 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3026 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3027 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3028 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3029 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3030 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3031 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3032 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3033 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3034 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3035 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3036 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3037 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3038 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3039 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3040 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003041
bellarde95c8d52004-09-30 22:22:08 +00003042#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003043 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3044 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3045 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3046 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3047 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3048 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3049 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3050 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003051 { "o0", 0, monitor_get_reg },
3052 { "o1", 1, monitor_get_reg },
3053 { "o2", 2, monitor_get_reg },
3054 { "o3", 3, monitor_get_reg },
3055 { "o4", 4, monitor_get_reg },
3056 { "o5", 5, monitor_get_reg },
3057 { "o6", 6, monitor_get_reg },
3058 { "o7", 7, monitor_get_reg },
3059 { "l0", 8, monitor_get_reg },
3060 { "l1", 9, monitor_get_reg },
3061 { "l2", 10, monitor_get_reg },
3062 { "l3", 11, monitor_get_reg },
3063 { "l4", 12, monitor_get_reg },
3064 { "l5", 13, monitor_get_reg },
3065 { "l6", 14, monitor_get_reg },
3066 { "l7", 15, monitor_get_reg },
3067 { "i0", 16, monitor_get_reg },
3068 { "i1", 17, monitor_get_reg },
3069 { "i2", 18, monitor_get_reg },
3070 { "i3", 19, monitor_get_reg },
3071 { "i4", 20, monitor_get_reg },
3072 { "i5", 21, monitor_get_reg },
3073 { "i6", 22, monitor_get_reg },
3074 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003075 { "pc", offsetof(CPUSPARCState, pc) },
3076 { "npc", offsetof(CPUSPARCState, npc) },
3077 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003078#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003079 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003080 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003081#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003082 { "tbr", offsetof(CPUSPARCState, tbr) },
3083 { "fsr", offsetof(CPUSPARCState, fsr) },
3084 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3085 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3086 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3087 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3088 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3089 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3090 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3091 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3092 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3093 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3094 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3095 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3096 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3097 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3098 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3099 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3100 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3101 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3102 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3103 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3104 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3105 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3106 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3107 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3108 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3109 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3110 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3111 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3112 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3113 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3114 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3115 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003116#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003117 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3118 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3119 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3120 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3121 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3122 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3123 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3124 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3125 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3126 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3127 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3128 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3129 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3130 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3131 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3132 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3133 { "asi", offsetof(CPUSPARCState, asi) },
3134 { "pstate", offsetof(CPUSPARCState, pstate) },
3135 { "cansave", offsetof(CPUSPARCState, cansave) },
3136 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3137 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3138 { "wstate", offsetof(CPUSPARCState, wstate) },
3139 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3140 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003141#endif
bellard9307c4c2004-04-04 12:57:25 +00003142#endif
3143 { NULL },
3144};
3145
aliguori376253e2009-03-05 23:01:23 +00003146static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003147{
aliguori376253e2009-03-05 23:01:23 +00003148 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003149 longjmp(expr_env, 1);
3150}
3151
Markus Armbruster09b94182010-01-20 13:07:30 +01003152/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003153static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003154{
blueswir18662d652008-10-02 18:32:44 +00003155 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003156 void *ptr;
3157
bellard9307c4c2004-04-04 12:57:25 +00003158 for(md = monitor_defs; md->name != NULL; md++) {
3159 if (compare_cmd(name, md->name)) {
3160 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003161 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003162 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003163 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003164 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003165 switch(md->type) {
3166 case MD_I32:
3167 *pval = *(int32_t *)ptr;
3168 break;
3169 case MD_TLONG:
3170 *pval = *(target_long *)ptr;
3171 break;
3172 default:
3173 *pval = 0;
3174 break;
3175 }
bellard9307c4c2004-04-04 12:57:25 +00003176 }
3177 return 0;
3178 }
3179 }
3180 return -1;
3181}
3182
3183static void next(void)
3184{
Blue Swirl660f11b2009-07-31 21:16:51 +00003185 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003186 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003187 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003188 pch++;
3189 }
3190}
3191
aliguori376253e2009-03-05 23:01:23 +00003192static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003193
aliguori376253e2009-03-05 23:01:23 +00003194static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003195{
blueswir1c2efc952007-09-25 17:28:42 +00003196 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003197 char *p;
bellard6a00d602005-11-21 23:25:50 +00003198 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003199
3200 switch(*pch) {
3201 case '+':
3202 next();
aliguori376253e2009-03-05 23:01:23 +00003203 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003204 break;
3205 case '-':
3206 next();
aliguori376253e2009-03-05 23:01:23 +00003207 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003208 break;
3209 case '~':
3210 next();
aliguori376253e2009-03-05 23:01:23 +00003211 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003212 break;
3213 case '(':
3214 next();
aliguori376253e2009-03-05 23:01:23 +00003215 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003216 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003217 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003218 }
3219 next();
3220 break;
bellard81d09122004-07-14 17:21:37 +00003221 case '\'':
3222 pch++;
3223 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003224 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003225 n = *pch;
3226 pch++;
3227 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003228 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003229 next();
3230 break;
bellard9307c4c2004-04-04 12:57:25 +00003231 case '$':
3232 {
3233 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003234 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003235
bellard9307c4c2004-04-04 12:57:25 +00003236 pch++;
3237 q = buf;
3238 while ((*pch >= 'a' && *pch <= 'z') ||
3239 (*pch >= 'A' && *pch <= 'Z') ||
3240 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003241 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003242 if ((q - buf) < sizeof(buf) - 1)
3243 *q++ = *pch;
3244 pch++;
3245 }
blueswir1cd390082008-11-16 13:53:32 +00003246 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003247 pch++;
3248 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003249 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003250 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003251 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003252 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003253 }
3254 break;
3255 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003256 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003257 n = 0;
3258 break;
3259 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003260 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003261 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003262 if (errno == ERANGE) {
3263 expr_error(mon, "number too large");
3264 }
bellard9307c4c2004-04-04 12:57:25 +00003265 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003266 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003267 }
3268 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003269 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003270 pch++;
3271 break;
3272 }
3273 return n;
3274}
3275
3276
aliguori376253e2009-03-05 23:01:23 +00003277static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003278{
blueswir1c2efc952007-09-25 17:28:42 +00003279 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003280 int op;
ths3b46e622007-09-17 08:09:54 +00003281
aliguori376253e2009-03-05 23:01:23 +00003282 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003283 for(;;) {
3284 op = *pch;
3285 if (op != '*' && op != '/' && op != '%')
3286 break;
3287 next();
aliguori376253e2009-03-05 23:01:23 +00003288 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003289 switch(op) {
3290 default:
3291 case '*':
3292 val *= val2;
3293 break;
3294 case '/':
3295 case '%':
ths5fafdf22007-09-16 21:08:06 +00003296 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003297 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003298 if (op == '/')
3299 val /= val2;
3300 else
3301 val %= val2;
3302 break;
3303 }
3304 }
3305 return val;
3306}
3307
aliguori376253e2009-03-05 23:01:23 +00003308static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003309{
blueswir1c2efc952007-09-25 17:28:42 +00003310 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003311 int op;
bellard9307c4c2004-04-04 12:57:25 +00003312
aliguori376253e2009-03-05 23:01:23 +00003313 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003314 for(;;) {
3315 op = *pch;
3316 if (op != '&' && op != '|' && op != '^')
3317 break;
3318 next();
aliguori376253e2009-03-05 23:01:23 +00003319 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003320 switch(op) {
3321 default:
3322 case '&':
3323 val &= val2;
3324 break;
3325 case '|':
3326 val |= val2;
3327 break;
3328 case '^':
3329 val ^= val2;
3330 break;
3331 }
3332 }
3333 return val;
3334}
3335
aliguori376253e2009-03-05 23:01:23 +00003336static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003337{
blueswir1c2efc952007-09-25 17:28:42 +00003338 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003339 int op;
bellard9307c4c2004-04-04 12:57:25 +00003340
aliguori376253e2009-03-05 23:01:23 +00003341 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003342 for(;;) {
3343 op = *pch;
3344 if (op != '+' && op != '-')
3345 break;
3346 next();
aliguori376253e2009-03-05 23:01:23 +00003347 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003348 if (op == '+')
3349 val += val2;
3350 else
3351 val -= val2;
3352 }
3353 return val;
3354}
3355
aliguori376253e2009-03-05 23:01:23 +00003356static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003357{
3358 pch = *pp;
3359 if (setjmp(expr_env)) {
3360 *pp = pch;
3361 return -1;
3362 }
blueswir1cd390082008-11-16 13:53:32 +00003363 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003364 pch++;
aliguori376253e2009-03-05 23:01:23 +00003365 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003366 *pp = pch;
3367 return 0;
3368}
3369
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003370static int get_double(Monitor *mon, double *pval, const char **pp)
3371{
3372 const char *p = *pp;
3373 char *tailp;
3374 double d;
3375
3376 d = strtod(p, &tailp);
3377 if (tailp == p) {
3378 monitor_printf(mon, "Number expected\n");
3379 return -1;
3380 }
3381 if (d != d || d - d != 0) {
3382 /* NaN or infinity */
3383 monitor_printf(mon, "Bad number\n");
3384 return -1;
3385 }
3386 *pval = d;
3387 *pp = tailp;
3388 return 0;
3389}
3390
bellard9307c4c2004-04-04 12:57:25 +00003391static int get_str(char *buf, int buf_size, const char **pp)
3392{
3393 const char *p;
3394 char *q;
3395 int c;
3396
bellard81d09122004-07-14 17:21:37 +00003397 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003398 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003399 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003400 p++;
3401 if (*p == '\0') {
3402 fail:
bellard81d09122004-07-14 17:21:37 +00003403 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003404 *pp = p;
3405 return -1;
3406 }
bellard9307c4c2004-04-04 12:57:25 +00003407 if (*p == '\"') {
3408 p++;
3409 while (*p != '\0' && *p != '\"') {
3410 if (*p == '\\') {
3411 p++;
3412 c = *p++;
3413 switch(c) {
3414 case 'n':
3415 c = '\n';
3416 break;
3417 case 'r':
3418 c = '\r';
3419 break;
3420 case '\\':
3421 case '\'':
3422 case '\"':
3423 break;
3424 default:
3425 qemu_printf("unsupported escape code: '\\%c'\n", c);
3426 goto fail;
3427 }
3428 if ((q - buf) < buf_size - 1) {
3429 *q++ = c;
3430 }
3431 } else {
3432 if ((q - buf) < buf_size - 1) {
3433 *q++ = *p;
3434 }
3435 p++;
3436 }
3437 }
3438 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003439 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003440 goto fail;
3441 }
3442 p++;
3443 } else {
blueswir1cd390082008-11-16 13:53:32 +00003444 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003445 if ((q - buf) < buf_size - 1) {
3446 *q++ = *p;
3447 }
3448 p++;
3449 }
bellard9307c4c2004-04-04 12:57:25 +00003450 }
bellard81d09122004-07-14 17:21:37 +00003451 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003452 *pp = p;
3453 return 0;
3454}
3455
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003456/*
3457 * Store the command-name in cmdname, and return a pointer to
3458 * the remaining of the command string.
3459 */
3460static const char *get_command_name(const char *cmdline,
3461 char *cmdname, size_t nlen)
3462{
3463 size_t len;
3464 const char *p, *pstart;
3465
3466 p = cmdline;
3467 while (qemu_isspace(*p))
3468 p++;
3469 if (*p == '\0')
3470 return NULL;
3471 pstart = p;
3472 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3473 p++;
3474 len = p - pstart;
3475 if (len > nlen - 1)
3476 len = nlen - 1;
3477 memcpy(cmdname, pstart, len);
3478 cmdname[len] = '\0';
3479 return p;
3480}
3481
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003482/**
3483 * Read key of 'type' into 'key' and return the current
3484 * 'type' pointer.
3485 */
3486static char *key_get_info(const char *type, char **key)
3487{
3488 size_t len;
3489 char *p, *str;
3490
3491 if (*type == ',')
3492 type++;
3493
3494 p = strchr(type, ':');
3495 if (!p) {
3496 *key = NULL;
3497 return NULL;
3498 }
3499 len = p - type;
3500
Anthony Liguori7267c092011-08-20 22:09:37 -05003501 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003502 memcpy(str, type, len);
3503 str[len] = '\0';
3504
3505 *key = str;
3506 return ++p;
3507}
3508
bellard9307c4c2004-04-04 12:57:25 +00003509static int default_fmt_format = 'x';
3510static int default_fmt_size = 4;
3511
3512#define MAX_ARGS 16
3513
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003514static int is_valid_option(const char *c, const char *typestr)
3515{
3516 char option[3];
3517
3518 option[0] = '-';
3519 option[1] = *c;
3520 option[2] = '\0';
3521
3522 typestr = strstr(typestr, option);
3523 return (typestr != NULL);
3524}
3525
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003526static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3527 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003528{
3529 const mon_cmd_t *cmd;
3530
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003531 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003532 if (compare_cmd(cmdname, cmd->name)) {
3533 return cmd;
3534 }
3535 }
3536
3537 return NULL;
3538}
3539
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003540static const mon_cmd_t *monitor_find_command(const char *cmdname)
3541{
3542 return search_dispatch_table(mon_cmds, cmdname);
3543}
3544
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003545static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3546{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003547 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003548}
3549
Anthony Liguoric227f092009-10-01 16:12:16 -05003550static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003551 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003552 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003553{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003554 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003555 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003556 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003557 char cmdname[256];
3558 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003559 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003560
3561#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003562 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003563#endif
ths3b46e622007-09-17 08:09:54 +00003564
bellard9307c4c2004-04-04 12:57:25 +00003565 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003566 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3567 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003568 return NULL;
ths3b46e622007-09-17 08:09:54 +00003569
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003570 cmd = monitor_find_command(cmdname);
3571 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003572 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003573 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003574 }
bellard9307c4c2004-04-04 12:57:25 +00003575
bellard9307c4c2004-04-04 12:57:25 +00003576 /* parse the parameters */
3577 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003578 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003579 typestr = key_get_info(typestr, &key);
3580 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003581 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003582 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003583 typestr++;
3584 switch(c) {
3585 case 'F':
bellard81d09122004-07-14 17:21:37 +00003586 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003587 case 's':
3588 {
3589 int ret;
ths3b46e622007-09-17 08:09:54 +00003590
blueswir1cd390082008-11-16 13:53:32 +00003591 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003592 p++;
3593 if (*typestr == '?') {
3594 typestr++;
3595 if (*p == '\0') {
3596 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003597 break;
bellard9307c4c2004-04-04 12:57:25 +00003598 }
3599 }
3600 ret = get_str(buf, sizeof(buf), &p);
3601 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003602 switch(c) {
3603 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003604 monitor_printf(mon, "%s: filename expected\n",
3605 cmdname);
bellard81d09122004-07-14 17:21:37 +00003606 break;
3607 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003608 monitor_printf(mon, "%s: block device name expected\n",
3609 cmdname);
bellard81d09122004-07-14 17:21:37 +00003610 break;
3611 default:
aliguori376253e2009-03-05 23:01:23 +00003612 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003613 break;
3614 }
bellard9307c4c2004-04-04 12:57:25 +00003615 goto fail;
3616 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003617 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003618 }
3619 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003620 case 'O':
3621 {
3622 QemuOptsList *opts_list;
3623 QemuOpts *opts;
3624
3625 opts_list = qemu_find_opts(key);
3626 if (!opts_list || opts_list->desc->name) {
3627 goto bad_type;
3628 }
3629 while (qemu_isspace(*p)) {
3630 p++;
3631 }
3632 if (!*p)
3633 break;
3634 if (get_str(buf, sizeof(buf), &p) < 0) {
3635 goto fail;
3636 }
3637 opts = qemu_opts_parse(opts_list, buf, 1);
3638 if (!opts) {
3639 goto fail;
3640 }
3641 qemu_opts_to_qdict(opts, qdict);
3642 qemu_opts_del(opts);
3643 }
3644 break;
bellard9307c4c2004-04-04 12:57:25 +00003645 case '/':
3646 {
3647 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003648
blueswir1cd390082008-11-16 13:53:32 +00003649 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003650 p++;
3651 if (*p == '/') {
3652 /* format found */
3653 p++;
3654 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003655 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003656 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003657 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003658 count = count * 10 + (*p - '0');
3659 p++;
3660 }
3661 }
3662 size = -1;
3663 format = -1;
3664 for(;;) {
3665 switch(*p) {
3666 case 'o':
3667 case 'd':
3668 case 'u':
3669 case 'x':
3670 case 'i':
3671 case 'c':
3672 format = *p++;
3673 break;
3674 case 'b':
3675 size = 1;
3676 p++;
3677 break;
3678 case 'h':
3679 size = 2;
3680 p++;
3681 break;
3682 case 'w':
3683 size = 4;
3684 p++;
3685 break;
3686 case 'g':
3687 case 'L':
3688 size = 8;
3689 p++;
3690 break;
3691 default:
3692 goto next;
3693 }
3694 }
3695 next:
blueswir1cd390082008-11-16 13:53:32 +00003696 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003697 monitor_printf(mon, "invalid char in format: '%c'\n",
3698 *p);
bellard9307c4c2004-04-04 12:57:25 +00003699 goto fail;
3700 }
bellard9307c4c2004-04-04 12:57:25 +00003701 if (format < 0)
3702 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003703 if (format != 'i') {
3704 /* for 'i', not specifying a size gives -1 as size */
3705 if (size < 0)
3706 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003707 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003708 }
bellard9307c4c2004-04-04 12:57:25 +00003709 default_fmt_format = format;
3710 } else {
3711 count = 1;
3712 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003713 if (format != 'i') {
3714 size = default_fmt_size;
3715 } else {
3716 size = -1;
3717 }
bellard9307c4c2004-04-04 12:57:25 +00003718 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003719 qdict_put(qdict, "count", qint_from_int(count));
3720 qdict_put(qdict, "format", qint_from_int(format));
3721 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003722 }
3723 break;
3724 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003725 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003726 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003727 {
blueswir1c2efc952007-09-25 17:28:42 +00003728 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003729
blueswir1cd390082008-11-16 13:53:32 +00003730 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003731 p++;
bellard34405572004-06-08 00:55:58 +00003732 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003733 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003734 if (*p == '\0') {
3735 typestr++;
3736 break;
3737 }
bellard34405572004-06-08 00:55:58 +00003738 } else {
3739 if (*p == '.') {
3740 p++;
blueswir1cd390082008-11-16 13:53:32 +00003741 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003742 p++;
bellard34405572004-06-08 00:55:58 +00003743 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003744 typestr++;
3745 break;
bellard34405572004-06-08 00:55:58 +00003746 }
3747 }
bellard13224a82006-07-14 22:03:35 +00003748 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003749 }
aliguori376253e2009-03-05 23:01:23 +00003750 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003751 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003752 /* Check if 'i' is greater than 32-bit */
3753 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3754 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3755 monitor_printf(mon, "integer is for 32-bit values\n");
3756 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003757 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003758 if (val < 0) {
3759 monitor_printf(mon, "enter a positive value\n");
3760 goto fail;
3761 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003762 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003763 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003764 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003765 }
3766 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003767 case 'o':
3768 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003769 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003770 char *end;
3771
3772 while (qemu_isspace(*p)) {
3773 p++;
3774 }
3775 if (*typestr == '?') {
3776 typestr++;
3777 if (*p == '\0') {
3778 break;
3779 }
3780 }
3781 val = strtosz(p, &end);
3782 if (val < 0) {
3783 monitor_printf(mon, "invalid size\n");
3784 goto fail;
3785 }
3786 qdict_put(qdict, key, qint_from_int(val));
3787 p = end;
3788 }
3789 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003790 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003791 {
3792 double val;
3793
3794 while (qemu_isspace(*p))
3795 p++;
3796 if (*typestr == '?') {
3797 typestr++;
3798 if (*p == '\0') {
3799 break;
3800 }
3801 }
3802 if (get_double(mon, &val, &p) < 0) {
3803 goto fail;
3804 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003805 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003806 switch (*p) {
3807 case 'm':
3808 val /= 1e3; p += 2; break;
3809 case 'u':
3810 val /= 1e6; p += 2; break;
3811 case 'n':
3812 val /= 1e9; p += 2; break;
3813 }
3814 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003815 if (*p && !qemu_isspace(*p)) {
3816 monitor_printf(mon, "Unknown unit suffix\n");
3817 goto fail;
3818 }
3819 qdict_put(qdict, key, qfloat_from_double(val));
3820 }
3821 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003822 case 'b':
3823 {
3824 const char *beg;
3825 int val;
3826
3827 while (qemu_isspace(*p)) {
3828 p++;
3829 }
3830 beg = p;
3831 while (qemu_isgraph(*p)) {
3832 p++;
3833 }
3834 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3835 val = 1;
3836 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3837 val = 0;
3838 } else {
3839 monitor_printf(mon, "Expected 'on' or 'off'\n");
3840 goto fail;
3841 }
3842 qdict_put(qdict, key, qbool_from_int(val));
3843 }
3844 break;
bellard9307c4c2004-04-04 12:57:25 +00003845 case '-':
3846 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003847 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003848 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003849 /* option */
ths3b46e622007-09-17 08:09:54 +00003850
bellard9307c4c2004-04-04 12:57:25 +00003851 c = *typestr++;
3852 if (c == '\0')
3853 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003854 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003855 p++;
bellard9307c4c2004-04-04 12:57:25 +00003856 if (*p == '-') {
3857 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003858 if(c != *p) {
3859 if(!is_valid_option(p, typestr)) {
3860
3861 monitor_printf(mon, "%s: unsupported option -%c\n",
3862 cmdname, *p);
3863 goto fail;
3864 } else {
3865 skip_key = 1;
3866 }
bellard9307c4c2004-04-04 12:57:25 +00003867 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003868 if(skip_key) {
3869 p = tmp;
3870 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003871 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003872 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003873 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003874 }
bellard9307c4c2004-04-04 12:57:25 +00003875 }
bellard9307c4c2004-04-04 12:57:25 +00003876 }
3877 break;
3878 default:
3879 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003880 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003881 goto fail;
3882 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003883 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003884 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003885 }
3886 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003887 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003888 p++;
3889 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003890 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3891 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003892 goto fail;
3893 }
3894
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003895 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003896
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003897fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05003898 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003899 return NULL;
3900}
3901
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003902void monitor_set_error(Monitor *mon, QError *qerror)
3903{
3904 /* report only the first error */
3905 if (!mon->error) {
3906 mon->error = qerror;
3907 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003908 QDECREF(qerror);
3909 }
3910}
3911
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003912static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3913{
Luiz Capitulino6d441432010-11-22 16:35:09 -02003914 if (ret && !monitor_has_error(mon)) {
3915 /*
3916 * If it returns failure, it must have passed on error.
3917 *
3918 * Action: Report an internal error to the client if in QMP.
3919 */
3920 qerror_report(QERR_UNDEFINED_ERROR);
Luiz Capitulino6d441432010-11-22 16:35:09 -02003921 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003922}
3923
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003924static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003925{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003926 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003927 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003928
3929 qdict = qdict_new();
3930
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003931 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003932 if (!cmd)
3933 goto out;
3934
Luiz Capitulino4903de02010-09-16 11:01:32 -03003935 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06003936 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03003937 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03003938 QObject *data = NULL;
3939
3940 /* XXX: ignores the error code */
3941 cmd->mhandler.cmd_new(mon, qdict, &data);
3942 assert(!monitor_has_error(mon));
3943 if (data) {
3944 cmd->user_print(mon, data);
3945 qobject_decref(data);
3946 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03003947 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003948 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003949 }
3950
Luiz Capitulino13917be2009-10-07 13:41:54 -03003951out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003952 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003953}
3954
bellard81d09122004-07-14 17:21:37 +00003955static void cmd_completion(const char *name, const char *list)
3956{
3957 const char *p, *pstart;
3958 char cmd[128];
3959 int len;
3960
3961 p = list;
3962 for(;;) {
3963 pstart = p;
3964 p = strchr(p, '|');
3965 if (!p)
3966 p = pstart + strlen(pstart);
3967 len = p - pstart;
3968 if (len > sizeof(cmd) - 2)
3969 len = sizeof(cmd) - 2;
3970 memcpy(cmd, pstart, len);
3971 cmd[len] = '\0';
3972 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003973 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003974 }
3975 if (*p == '\0')
3976 break;
3977 p++;
3978 }
3979}
3980
3981static void file_completion(const char *input)
3982{
3983 DIR *ffs;
3984 struct dirent *d;
3985 char path[1024];
3986 char file[1024], file_prefix[1024];
3987 int input_path_len;
3988 const char *p;
3989
ths5fafdf22007-09-16 21:08:06 +00003990 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003991 if (!p) {
3992 input_path_len = 0;
3993 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003994 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003995 } else {
3996 input_path_len = p - input + 1;
3997 memcpy(path, input, input_path_len);
3998 if (input_path_len > sizeof(path) - 1)
3999 input_path_len = sizeof(path) - 1;
4000 path[input_path_len] = '\0';
4001 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4002 }
4003#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004004 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4005 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004006#endif
4007 ffs = opendir(path);
4008 if (!ffs)
4009 return;
4010 for(;;) {
4011 struct stat sb;
4012 d = readdir(ffs);
4013 if (!d)
4014 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004015
4016 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4017 continue;
4018 }
4019
bellard81d09122004-07-14 17:21:37 +00004020 if (strstart(d->d_name, file_prefix, NULL)) {
4021 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004022 if (input_path_len < sizeof(file))
4023 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4024 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004025 /* stat the file to find out if it's a directory.
4026 * In that case add a slash to speed up typing long paths
4027 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004028 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004029 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004030 }
aliguori731b0362009-03-05 23:01:42 +00004031 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004032 }
4033 }
4034 closedir(ffs);
4035}
4036
aliguori51de9762009-03-05 23:00:43 +00004037static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004038{
aliguori51de9762009-03-05 23:00:43 +00004039 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004040 const char *input = opaque;
4041
4042 if (input[0] == '\0' ||
4043 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004044 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004045 }
4046}
4047
4048/* NOTE: this parser is an approximate form of the real command parser */
4049static void parse_cmdline(const char *cmdline,
4050 int *pnb_args, char **args)
4051{
4052 const char *p;
4053 int nb_args, ret;
4054 char buf[1024];
4055
4056 p = cmdline;
4057 nb_args = 0;
4058 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004059 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004060 p++;
4061 if (*p == '\0')
4062 break;
4063 if (nb_args >= MAX_ARGS)
4064 break;
4065 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004066 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004067 nb_args++;
4068 if (ret < 0)
4069 break;
4070 }
4071 *pnb_args = nb_args;
4072}
4073
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004074static const char *next_arg_type(const char *typestr)
4075{
4076 const char *p = strchr(typestr, ':');
4077 return (p != NULL ? ++p : typestr);
4078}
4079
aliguori4c36ba32009-03-05 23:01:37 +00004080static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004081{
4082 const char *cmdname;
4083 char *args[MAX_ARGS];
4084 int nb_args, i, len;
4085 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004086 const mon_cmd_t *cmd;
bellard81d09122004-07-14 17:21:37 +00004087
4088 parse_cmdline(cmdline, &nb_args, args);
4089#ifdef DEBUG_COMPLETION
4090 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004091 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004092 }
4093#endif
4094
4095 /* if the line ends with a space, it means we want to complete the
4096 next arg */
4097 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004098 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004099 if (nb_args >= MAX_ARGS) {
4100 goto cleanup;
4101 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004102 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004103 }
4104 if (nb_args <= 1) {
4105 /* command completion */
4106 if (nb_args == 0)
4107 cmdname = "";
4108 else
4109 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004110 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004111 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004112 cmd_completion(cmdname, cmd->name);
4113 }
4114 } else {
4115 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004116 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4117 if (compare_cmd(args[0], cmd->name)) {
4118 break;
4119 }
bellard81d09122004-07-14 17:21:37 +00004120 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004121 if (!cmd->name) {
4122 goto cleanup;
4123 }
4124
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004125 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004126 for(i = 0; i < nb_args - 2; i++) {
4127 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004128 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004129 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004130 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004131 }
4132 }
4133 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004134 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004135 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004136 }
bellard81d09122004-07-14 17:21:37 +00004137 switch(*ptype) {
4138 case 'F':
4139 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004140 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004141 file_completion(str);
4142 break;
4143 case 'B':
4144 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004145 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004146 bdrv_iterate(block_completion_it, (void *)str);
4147 break;
bellard7fe48482004-10-09 18:08:01 +00004148 case 's':
4149 /* XXX: more generic ? */
4150 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004151 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004152 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4153 cmd_completion(str, cmd->name);
4154 }
bellard64866c32006-05-07 18:03:31 +00004155 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004156 char *sep = strrchr(str, '-');
4157 if (sep)
4158 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004159 readline_set_completion_index(cur_mon->rs, strlen(str));
Amos Kong1048c882012-08-31 10:56:25 +08004160 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4161 cmd_completion(str, QKeyCode_lookup[i]);
bellard64866c32006-05-07 18:03:31 +00004162 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004163 } else if (!strcmp(cmd->name, "help|?")) {
4164 readline_set_completion_index(cur_mon->rs, strlen(str));
4165 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4166 cmd_completion(str, cmd->name);
4167 }
bellard7fe48482004-10-09 18:08:01 +00004168 }
4169 break;
bellard81d09122004-07-14 17:21:37 +00004170 default:
4171 break;
4172 }
4173 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004174
4175cleanup:
4176 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004177 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004178 }
bellard81d09122004-07-14 17:21:37 +00004179}
4180
aliguori731b0362009-03-05 23:01:42 +00004181static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004182{
aliguori731b0362009-03-05 23:01:42 +00004183 Monitor *mon = opaque;
4184
Jan Kiszkac62313b2009-12-04 14:05:29 +01004185 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004186}
4187
Luiz Capitulino09069b12010-02-04 18:10:06 -02004188static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4189{
4190 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4191 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4192}
4193
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004194/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004195 * Argument validation rules:
4196 *
4197 * 1. The argument must exist in cmd_args qdict
4198 * 2. The argument type must be the expected one
4199 *
4200 * Special case: If the argument doesn't exist in cmd_args and
4201 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4202 * checking is skipped for it.
4203 */
4204static int check_client_args_type(const QDict *client_args,
4205 const QDict *cmd_args, int flags)
4206{
4207 const QDictEntry *ent;
4208
4209 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4210 QObject *obj;
4211 QString *arg_type;
4212 const QObject *client_arg = qdict_entry_value(ent);
4213 const char *client_arg_name = qdict_entry_key(ent);
4214
4215 obj = qdict_get(cmd_args, client_arg_name);
4216 if (!obj) {
4217 if (flags & QMP_ACCEPT_UNKNOWNS) {
4218 /* handler accepts unknowns */
4219 continue;
4220 }
4221 /* client arg doesn't exist */
4222 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4223 return -1;
4224 }
4225
4226 arg_type = qobject_to_qstring(obj);
4227 assert(arg_type != NULL);
4228
4229 /* check if argument's type is correct */
4230 switch (qstring_get_str(arg_type)[0]) {
4231 case 'F':
4232 case 'B':
4233 case 's':
4234 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4235 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4236 "string");
4237 return -1;
4238 }
4239 break;
4240 case 'i':
4241 case 'l':
4242 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004243 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004244 if (qobject_type(client_arg) != QTYPE_QINT) {
4245 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4246 "int");
4247 return -1;
4248 }
4249 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004250 case 'T':
4251 if (qobject_type(client_arg) != QTYPE_QINT &&
4252 qobject_type(client_arg) != QTYPE_QFLOAT) {
4253 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4254 "number");
4255 return -1;
4256 }
4257 break;
4258 case 'b':
4259 case '-':
4260 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4261 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4262 "bool");
4263 return -1;
4264 }
4265 break;
4266 case 'O':
4267 assert(flags & QMP_ACCEPT_UNKNOWNS);
4268 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004269 case 'q':
4270 /* Any QObject can be passed. */
4271 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004272 case '/':
4273 case '.':
4274 /*
4275 * These types are not supported by QMP and thus are not
4276 * handled here. Fall through.
4277 */
4278 default:
4279 abort();
4280 }
4281 }
4282
4283 return 0;
4284}
4285
4286/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004287 * - Check if the client has passed all mandatory args
4288 * - Set special flags for argument validation
4289 */
4290static int check_mandatory_args(const QDict *cmd_args,
4291 const QDict *client_args, int *flags)
4292{
4293 const QDictEntry *ent;
4294
4295 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4296 const char *cmd_arg_name = qdict_entry_key(ent);
4297 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4298 assert(type != NULL);
4299
4300 if (qstring_get_str(type)[0] == 'O') {
4301 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4302 *flags |= QMP_ACCEPT_UNKNOWNS;
4303 } else if (qstring_get_str(type)[0] != '-' &&
4304 qstring_get_str(type)[1] != '?' &&
4305 !qdict_haskey(client_args, cmd_arg_name)) {
4306 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4307 return -1;
4308 }
4309 }
4310
4311 return 0;
4312}
4313
4314static QDict *qdict_from_args_type(const char *args_type)
4315{
4316 int i;
4317 QDict *qdict;
4318 QString *key, *type, *cur_qs;
4319
4320 assert(args_type != NULL);
4321
4322 qdict = qdict_new();
4323
4324 if (args_type == NULL || args_type[0] == '\0') {
4325 /* no args, empty qdict */
4326 goto out;
4327 }
4328
4329 key = qstring_new();
4330 type = qstring_new();
4331
4332 cur_qs = key;
4333
4334 for (i = 0;; i++) {
4335 switch (args_type[i]) {
4336 case ',':
4337 case '\0':
4338 qdict_put(qdict, qstring_get_str(key), type);
4339 QDECREF(key);
4340 if (args_type[i] == '\0') {
4341 goto out;
4342 }
4343 type = qstring_new(); /* qdict has ref */
4344 cur_qs = key = qstring_new();
4345 break;
4346 case ':':
4347 cur_qs = type;
4348 break;
4349 default:
4350 qstring_append_chr(cur_qs, args_type[i]);
4351 break;
4352 }
4353 }
4354
4355out:
4356 return qdict;
4357}
4358
4359/*
4360 * Client argument checking rules:
4361 *
4362 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004363 * 2. Each argument provided by the client must be expected
4364 * 3. Each argument provided by the client must have the type expected
4365 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004366 */
4367static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4368{
4369 int flags, err;
4370 QDict *cmd_args;
4371
4372 cmd_args = qdict_from_args_type(cmd->args_type);
4373
4374 flags = 0;
4375 err = check_mandatory_args(cmd_args, client_args, &flags);
4376 if (err) {
4377 goto out;
4378 }
4379
Luiz Capitulino4af91932010-06-22 11:44:05 -03004380 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004381
4382out:
4383 QDECREF(cmd_args);
4384 return err;
4385}
4386
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004387/*
4388 * Input object checking rules
4389 *
4390 * 1. Input object must be a dict
4391 * 2. The "execute" key must exist
4392 * 3. The "execute" key must be a string
4393 * 4. If the "arguments" key exists, it must be a dict
4394 * 5. If the "id" key exists, it can be anything (ie. json-value)
4395 * 6. Any argument not listed above is considered invalid
4396 */
4397static QDict *qmp_check_input_obj(QObject *input_obj)
4398{
4399 const QDictEntry *ent;
4400 int has_exec_key = 0;
4401 QDict *input_dict;
4402
4403 if (qobject_type(input_obj) != QTYPE_QDICT) {
4404 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4405 return NULL;
4406 }
4407
4408 input_dict = qobject_to_qdict(input_obj);
4409
4410 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4411 const char *arg_name = qdict_entry_key(ent);
4412 const QObject *arg_obj = qdict_entry_value(ent);
4413
4414 if (!strcmp(arg_name, "execute")) {
4415 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4416 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4417 "string");
4418 return NULL;
4419 }
4420 has_exec_key = 1;
4421 } else if (!strcmp(arg_name, "arguments")) {
4422 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4423 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4424 "object");
4425 return NULL;
4426 }
4427 } else if (!strcmp(arg_name, "id")) {
4428 /* FIXME: check duplicated IDs for async commands */
4429 } else {
4430 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4431 return NULL;
4432 }
4433 }
4434
4435 if (!has_exec_key) {
4436 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4437 return NULL;
4438 }
4439
4440 return input_dict;
4441}
4442
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004443static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4444 const QDict *params)
4445{
4446 int ret;
4447 QObject *data = NULL;
4448
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004449 ret = cmd->mhandler.cmd_new(mon, params, &data);
4450 handler_audit(mon, cmd, ret);
4451 monitor_protocol_emitter(mon, data);
4452 qobject_decref(data);
4453}
4454
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004455static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4456{
4457 int err;
4458 QObject *obj;
4459 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004460 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004461 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004462 Monitor *mon = cur_mon;
4463
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004464 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004465
4466 obj = json_parser_parse(tokens, NULL);
4467 if (!obj) {
4468 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004469 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004470 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004471 }
4472
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004473 input = qmp_check_input_obj(obj);
4474 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004475 qobject_decref(obj);
4476 goto err_out;
4477 }
4478
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004479 mon->mc->id = qdict_get(input, "id");
4480 qobject_incref(mon->mc->id);
4481
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004482 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004483 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004484 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004485 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004486 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004487 }
4488
Anthony Liguorie3193602011-09-02 12:34:47 -05004489 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004490 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004491 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004492 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004493 }
4494
4495 obj = qdict_get(input, "arguments");
4496 if (!obj) {
4497 args = qdict_new();
4498 } else {
4499 args = qobject_to_qdict(obj);
4500 QINCREF(args);
4501 }
4502
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004503 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004504 if (err < 0) {
4505 goto err_out;
4506 }
4507
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004508 if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004509 err = qmp_async_cmd_handler(mon, cmd, args);
4510 if (err) {
4511 /* emit the error response */
4512 goto err_out;
4513 }
Adam Litke940cc302010-01-25 12:18:44 -06004514 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004515 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004516 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004517
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004518 goto out;
4519
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004520err_out:
4521 monitor_protocol_emitter(mon, NULL);
4522out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004523 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004524 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004525}
4526
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004527/**
4528 * monitor_control_read(): Read and handle QMP input
4529 */
4530static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4531{
4532 Monitor *old_mon = cur_mon;
4533
4534 cur_mon = opaque;
4535
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004536 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004537
4538 cur_mon = old_mon;
4539}
4540
aliguori731b0362009-03-05 23:01:42 +00004541static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004542{
aliguori731b0362009-03-05 23:01:42 +00004543 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004544 int i;
aliguori376253e2009-03-05 23:01:23 +00004545
aliguori731b0362009-03-05 23:01:42 +00004546 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004547
aliguoricde76ee2009-03-05 23:01:51 +00004548 if (cur_mon->rs) {
4549 for (i = 0; i < size; i++)
4550 readline_handle_byte(cur_mon->rs, buf[i]);
4551 } else {
4552 if (size == 0 || buf[size - 1] != 0)
4553 monitor_printf(cur_mon, "corrupted command\n");
4554 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004555 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004556 }
aliguori731b0362009-03-05 23:01:42 +00004557
4558 cur_mon = old_mon;
4559}
aliguorid8f44602008-10-06 13:52:44 +00004560
aliguori376253e2009-03-05 23:01:23 +00004561static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004562{
aliguori731b0362009-03-05 23:01:42 +00004563 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004564 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004565 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004566}
4567
aliguoricde76ee2009-03-05 23:01:51 +00004568int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004569{
aliguoricde76ee2009-03-05 23:01:51 +00004570 if (!mon->rs)
4571 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004572 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004573 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004574}
4575
aliguori376253e2009-03-05 23:01:23 +00004576void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004577{
aliguoricde76ee2009-03-05 23:01:51 +00004578 if (!mon->rs)
4579 return;
aliguori731b0362009-03-05 23:01:42 +00004580 if (--mon->suspend_cnt == 0)
4581 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004582}
4583
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004584static QObject *get_qmp_greeting(void)
4585{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004586 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004587
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004588 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004589 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4590}
4591
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004592/**
4593 * monitor_control_event(): Print QMP gretting
4594 */
4595static void monitor_control_event(void *opaque, int event)
4596{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004597 QObject *data;
4598 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004599
Luiz Capitulino47116d12010-02-08 17:01:30 -02004600 switch (event) {
4601 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004602 mon->mc->command_mode = 0;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004603 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004604 monitor_json_emitter(mon, data);
4605 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004606 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004607 break;
4608 case CHR_EVENT_CLOSED:
4609 json_message_parser_destroy(&mon->mc->parser);
Anthony Liguori58617a72012-08-23 08:03:21 -05004610 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004611 mon_refcount--;
4612 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004613 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004614 }
4615}
4616
aliguori731b0362009-03-05 23:01:42 +00004617static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004618{
aliguori376253e2009-03-05 23:01:23 +00004619 Monitor *mon = opaque;
4620
aliguori2724b182009-03-05 23:01:47 +00004621 switch (event) {
4622 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004623 mon->mux_out = 0;
4624 if (mon->reset_seen) {
4625 readline_restart(mon->rs);
4626 monitor_resume(mon);
4627 monitor_flush(mon);
4628 } else {
4629 mon->suspend_cnt = 0;
4630 }
aliguori2724b182009-03-05 23:01:47 +00004631 break;
ths86e94de2007-01-05 22:01:59 +00004632
aliguori2724b182009-03-05 23:01:47 +00004633 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004634 if (mon->reset_seen) {
4635 if (mon->suspend_cnt == 0) {
4636 monitor_printf(mon, "\n");
4637 }
4638 monitor_flush(mon);
4639 monitor_suspend(mon);
4640 } else {
4641 mon->suspend_cnt++;
4642 }
4643 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004644 break;
4645
Amit Shahb6b8df52009-10-07 18:31:16 +05304646 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004647 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4648 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004649 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004650 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004651 }
4652 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004653 mon_refcount++;
4654 break;
4655
4656 case CHR_EVENT_CLOSED:
4657 mon_refcount--;
4658 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004659 break;
4660 }
ths86e94de2007-01-05 22:01:59 +00004661}
4662
Wayne Xia816f8922011-10-12 11:32:41 +08004663static int
4664compare_mon_cmd(const void *a, const void *b)
4665{
4666 return strcmp(((const mon_cmd_t *)a)->name,
4667 ((const mon_cmd_t *)b)->name);
4668}
4669
4670static void sortcmdlist(void)
4671{
4672 int array_num;
4673 int elem_size = sizeof(mon_cmd_t);
4674
4675 array_num = sizeof(mon_cmds)/elem_size-1;
4676 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4677
4678 array_num = sizeof(info_cmds)/elem_size-1;
4679 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4680}
4681
aliguori76655d62009-03-06 20:27:37 +00004682
4683/*
4684 * Local variables:
4685 * c-indent-level: 4
4686 * c-basic-offset: 4
4687 * tab-width: 8
4688 * End:
4689 */
4690
aliguori731b0362009-03-05 23:01:42 +00004691void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004692{
aliguori731b0362009-03-05 23:01:42 +00004693 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004694 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004695
4696 if (is_first_init) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +01004697 monitor_protocol_event_init();
ths20d8a3e2007-02-18 17:04:49 +00004698 is_first_init = 0;
4699 }
aliguori87127162009-03-05 23:01:29 +00004700
Anthony Liguori7267c092011-08-20 22:09:37 -05004701 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004702
aliguori87127162009-03-05 23:01:29 +00004703 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004704 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004705 if (flags & MONITOR_USE_READLINE) {
4706 mon->rs = readline_init(mon, monitor_find_completion);
4707 monitor_read_command(mon, 0);
4708 }
aliguori87127162009-03-05 23:01:29 +00004709
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004710 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004711 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004712 /* Control mode requires special handlers */
4713 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4714 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004715 qemu_chr_fe_set_echo(chr, true);
Anthony Liguori26efaca2012-08-23 13:49:02 -05004716
4717 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004718 } else {
4719 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4720 monitor_event, mon);
4721 }
aliguori87127162009-03-05 23:01:29 +00004722
Blue Swirl72cf2d42009-09-12 07:36:22 +00004723 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004724 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4725 default_mon = mon;
Wayne Xia816f8922011-10-12 11:32:41 +08004726
4727 sortcmdlist();
bellard7e2515e2004-08-01 21:52:19 +00004728}
4729
aliguori376253e2009-03-05 23:01:23 +00004730static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004731{
aliguoribb5fc202009-03-05 23:01:15 +00004732 BlockDriverState *bs = opaque;
4733 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004734
aliguoribb5fc202009-03-05 23:01:15 +00004735 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004736 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004737 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004738 }
aliguori731b0362009-03-05 23:01:42 +00004739 if (mon->password_completion_cb)
4740 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004741
aliguori731b0362009-03-05 23:01:42 +00004742 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004743}
aliguoric0f4ce72009-03-05 23:01:01 +00004744
Anthony Liguori7060b472011-09-02 12:34:50 -05004745ReadLineState *monitor_get_rs(Monitor *mon)
4746{
4747 return mon->rs;
4748}
4749
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004750int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4751 BlockDriverCompletionFunc *completion_cb,
4752 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004753{
aliguoricde76ee2009-03-05 23:01:51 +00004754 int err;
4755
aliguoribb5fc202009-03-05 23:01:15 +00004756 if (!bdrv_key_required(bs)) {
4757 if (completion_cb)
4758 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004759 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004760 }
aliguoric0f4ce72009-03-05 23:01:01 +00004761
Luiz Capitulino94171e12009-12-07 21:37:00 +01004762 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino903a8812011-12-13 17:18:30 -02004763 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
4764 bdrv_get_encrypted_filename(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004765 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004766 }
4767
aliguori376253e2009-03-05 23:01:23 +00004768 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4769 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004770
aliguori731b0362009-03-05 23:01:42 +00004771 mon->password_completion_cb = completion_cb;
4772 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004773
aliguoricde76ee2009-03-05 23:01:51 +00004774 err = monitor_read_password(mon, bdrv_password_cb, bs);
4775
4776 if (err && completion_cb)
4777 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004778
4779 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004780}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004781
4782int monitor_read_block_device_key(Monitor *mon, const char *device,
4783 BlockDriverCompletionFunc *completion_cb,
4784 void *opaque)
4785{
4786 BlockDriverState *bs;
4787
4788 bs = bdrv_find(device);
4789 if (!bs) {
4790 monitor_printf(mon, "Device not found %s\n", device);
4791 return -1;
4792 }
4793
4794 return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);
4795}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004796
4797QemuOptsList qemu_mon_opts = {
4798 .name = "mon",
4799 .implied_opt_name = "chardev",
4800 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4801 .desc = {
4802 {
4803 .name = "mode",
4804 .type = QEMU_OPT_STRING,
4805 },{
4806 .name = "chardev",
4807 .type = QEMU_OPT_STRING,
4808 },{
4809 .name = "default",
4810 .type = QEMU_OPT_BOOL,
4811 },{
4812 .name = "pretty",
4813 .type = QEMU_OPT_BOOL,
4814 },
4815 { /* end of list */ }
4816 },
4817};