blob: 6733022a160e8ead71eecd97847cf731408e98bb [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"
30#include "hw/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"
pbrook87ecb682007-11-17 17:14:51 +000033#include "gdbstub.h"
34#include "net.h"
35#include "qemu-char.h"
36#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000037#include "monitor.h"
38#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000039#include "console.h"
40#include "block.h"
41#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000042#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000043#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000044#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000045#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000046#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000047#include "acl.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030048#include "qint.h"
Luiz Capitulino8f3cec02009-10-07 13:42:04 -030049#include "qlist.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030050#include "qdict.h"
51#include "qstring.h"
Luiz Capitulino8204a912009-11-18 23:05:31 -020052#include "qerror.h"
ths6a5bd302007-12-03 17:05:38 +000053
bellard9dc39cb2004-03-14 21:38:27 +000054//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000055//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000056
bellard9307c4c2004-04-04 12:57:25 +000057/*
58 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000059 *
bellard9307c4c2004-04-04 12:57:25 +000060 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000061 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000062 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000063 * 'i' 32 bit integer
64 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000065 * '/' optional gdb-like print format (like "/10x")
66 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030067 * '?' optional type (for all types, except '/')
68 * '.' other form of optional type (for 'i' and 'l')
69 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000070 *
71 */
72
Anthony Liguoric227f092009-10-01 16:12:16 -050073typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000074 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000075 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +000076 const char *params;
77 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -030078 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -030079 union {
80 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -030081 void (*info_new)(Monitor *mon, QObject **ret_data);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -030082 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -030083 void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Luiz Capitulino910df892009-10-07 13:41:51 -030084 } mhandler;
Anthony Liguoric227f092009-10-01 16:12:16 -050085} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000086
Mark McLoughlinf07918f2009-07-22 09:11:40 +010087/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -050088typedef struct mon_fd_t mon_fd_t;
89struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +010090 char *name;
91 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -050092 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010093};
94
aliguori87127162009-03-05 23:01:29 +000095struct Monitor {
96 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +020097 int mux_out;
98 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +000099 int flags;
100 int suspend_cnt;
101 uint8_t outbuf[1024];
102 int outbuf_index;
103 ReadLineState *rs;
104 CPUState *mon_cpu;
105 BlockDriverCompletionFunc *password_completion_cb;
106 void *password_opaque;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200107 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500108 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000109 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000110};
111
Blue Swirl72cf2d42009-09-12 07:36:22 +0000112static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000113
Anthony Liguoric227f092009-10-01 16:12:16 -0500114static const mon_cmd_t mon_cmds[];
115static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000116
aliguori87127162009-03-05 23:01:29 +0000117Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000118
aliguori731b0362009-03-05 23:01:42 +0000119static void monitor_command_cb(Monitor *mon, const char *cmdline,
120 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000121
Luiz Capitulino418173c2009-11-26 22:58:51 -0200122/* Return true if in control mode, false otherwise */
123static inline int monitor_ctrl_mode(const Monitor *mon)
124{
125 return (mon->flags & MONITOR_USE_CONTROL);
126}
127
aliguori731b0362009-03-05 23:01:42 +0000128static void monitor_read_command(Monitor *mon, int show_prompt)
129{
130 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
131 if (show_prompt)
132 readline_show_prompt(mon->rs);
133}
bellard6a00d602005-11-21 23:25:50 +0000134
aliguoricde76ee2009-03-05 23:01:51 +0000135static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
136 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000137{
aliguoricde76ee2009-03-05 23:01:51 +0000138 if (mon->rs) {
139 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
140 /* prompt is printed on return from the command handler */
141 return 0;
142 } else {
143 monitor_printf(mon, "terminal does not support password prompting\n");
144 return -ENOTTY;
145 }
aliguoribb5fc202009-03-05 23:01:15 +0000146}
147
aliguori376253e2009-03-05 23:01:23 +0000148void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000149{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200150 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000151 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
152 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000153 }
154}
155
156/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000157static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000158{
ths60fe76f2007-12-16 03:02:09 +0000159 char c;
aliguori731b0362009-03-05 23:01:42 +0000160
161 if (!mon)
162 return;
163
bellard7e2515e2004-08-01 21:52:19 +0000164 for(;;) {
165 c = *str++;
166 if (c == '\0')
167 break;
bellard7ba12602006-07-14 20:26:42 +0000168 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000169 mon->outbuf[mon->outbuf_index++] = '\r';
170 mon->outbuf[mon->outbuf_index++] = c;
171 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
172 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000173 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000174 }
175}
176
aliguori376253e2009-03-05 23:01:23 +0000177void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000178{
179 char buf[4096];
180 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000181 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000182}
183
aliguori376253e2009-03-05 23:01:23 +0000184void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000185{
186 va_list ap;
187 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000188 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000189 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000190}
191
aliguori376253e2009-03-05 23:01:23 +0000192void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000193{
194 int i;
195
196 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000197 switch (filename[i]) {
198 case ' ':
199 case '"':
200 case '\\':
201 monitor_printf(mon, "\\%c", filename[i]);
202 break;
203 case '\t':
204 monitor_printf(mon, "\\t");
205 break;
206 case '\r':
207 monitor_printf(mon, "\\r");
208 break;
209 case '\n':
210 monitor_printf(mon, "\\n");
211 break;
212 default:
213 monitor_printf(mon, "%c", filename[i]);
214 break;
215 }
thsfef30742006-12-22 14:11:32 +0000216 }
217}
218
bellard7fe48482004-10-09 18:08:01 +0000219static int monitor_fprintf(FILE *stream, const char *fmt, ...)
220{
221 va_list ap;
222 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000223 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000224 va_end(ap);
225 return 0;
226}
227
Luiz Capitulino13c74252009-10-07 13:41:55 -0300228static void monitor_user_noop(Monitor *mon, const QObject *data) { }
229
Luiz Capitulino13917be2009-10-07 13:41:54 -0300230static inline int monitor_handler_ported(const mon_cmd_t *cmd)
231{
232 return cmd->user_print != NULL;
233}
234
Luiz Capitulino8204a912009-11-18 23:05:31 -0200235static inline int monitor_has_error(const Monitor *mon)
236{
237 return mon->error != NULL;
238}
239
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300240static void monitor_print_qobject(Monitor *mon, const QObject *data)
241{
242 switch (qobject_type(data)) {
243 case QTYPE_QSTRING:
244 monitor_printf(mon, "%s",qstring_get_str(qobject_to_qstring(data)));
245 break;
246 case QTYPE_QINT:
247 monitor_printf(mon, "%" PRId64,qint_get_int(qobject_to_qint(data)));
248 break;
249 default:
250 monitor_printf(mon, "ERROR: unsupported type: %d",
251 qobject_type(data));
252 break;
253 }
254
255 monitor_puts(mon, "\n");
256}
257
bellard9dc39cb2004-03-14 21:38:27 +0000258static int compare_cmd(const char *name, const char *list)
259{
260 const char *p, *pstart;
261 int len;
262 len = strlen(name);
263 p = list;
264 for(;;) {
265 pstart = p;
266 p = strchr(p, '|');
267 if (!p)
268 p = pstart + strlen(pstart);
269 if ((p - pstart) == len && !memcmp(pstart, name, len))
270 return 1;
271 if (*p == '\0')
272 break;
273 p++;
274 }
275 return 0;
276}
277
Anthony Liguoric227f092009-10-01 16:12:16 -0500278static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000279 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000280{
Anthony Liguoric227f092009-10-01 16:12:16 -0500281 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000282
283 for(cmd = cmds; cmd->name != NULL; cmd++) {
284 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000285 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
286 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000287 }
288}
289
aliguori376253e2009-03-05 23:01:23 +0000290static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000291{
292 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000293 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000294 } else {
aliguori376253e2009-03-05 23:01:23 +0000295 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000296 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000297 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000298 monitor_printf(mon, "Log items (comma separated):\n");
299 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000300 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000301 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000302 }
303 }
bellard9dc39cb2004-03-14 21:38:27 +0000304 }
305}
306
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300307static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300308{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300309 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300310}
311
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300312static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000313{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200314 int all_devices;
315 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300316 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000317
bellard7954c732006-08-01 15:52:40 +0000318 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000319 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200320 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300321 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200322 continue;
323 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000324 }
325}
326
Luiz Capitulino13c74252009-10-07 13:41:55 -0300327static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000328{
Anthony Liguoric227f092009-10-01 16:12:16 -0500329 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300330 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000331
bellard9307c4c2004-04-04 12:57:25 +0000332 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000333 goto help;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300334
335 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000336 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300337 break;
bellard9dc39cb2004-03-14 21:38:27 +0000338 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300339
340 if (cmd->name == NULL)
341 goto help;
342
343 if (monitor_handler_ported(cmd)) {
344 cmd->mhandler.info_new(mon, ret_data);
345 if (*ret_data)
346 cmd->user_print(mon, *ret_data);
347 } else {
348 cmd->mhandler.info(mon);
349 }
350
bellard9dc39cb2004-03-14 21:38:27 +0000351 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300352
353help:
354 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000355}
356
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300357/**
358 * do_info_version(): Show QEMU version
359 */
360static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000361{
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300362 *ret_data = QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION));
bellard9bc9d1c2004-10-10 15:15:51 +0000363}
364
aliguori376253e2009-03-05 23:01:23 +0000365static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000366{
367 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000368 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000369}
370
aurel32bf4f74c2008-12-18 22:42:34 +0000371#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000372static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000373{
aliguori376253e2009-03-05 23:01:23 +0000374 monitor_printf(mon, "HPET is %s by QEMU\n",
375 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000376}
aurel32bf4f74c2008-12-18 22:42:34 +0000377#endif
aliguori16b29ae2008-12-17 23:28:44 +0000378
aliguori376253e2009-03-05 23:01:23 +0000379static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000380{
aliguori376253e2009-03-05 23:01:23 +0000381 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
382 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
383 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
384 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
385 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000386}
387
bellard6a00d602005-11-21 23:25:50 +0000388/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000389static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000390{
391 CPUState *env;
392
393 for(env = first_cpu; env != NULL; env = env->next_cpu) {
394 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000395 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000396 return 0;
397 }
398 }
399 return -1;
400}
401
pbrook9596ebb2007-11-18 01:44:38 +0000402static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000403{
aliguori731b0362009-03-05 23:01:42 +0000404 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000405 mon_set_cpu(0);
406 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300407 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000408 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000409}
410
aliguori376253e2009-03-05 23:01:23 +0000411static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000412{
bellard6a00d602005-11-21 23:25:50 +0000413 CPUState *env;
414 env = mon_get_cpu();
415 if (!env)
416 return;
bellard9307c4c2004-04-04 12:57:25 +0000417#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000418 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000419 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000420#else
aliguori376253e2009-03-05 23:01:23 +0000421 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000422 0);
bellard9307c4c2004-04-04 12:57:25 +0000423#endif
424}
425
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300426static void print_cpu_iter(QObject *obj, void *opaque)
427{
428 QDict *cpu;
429 int active = ' ';
430 Monitor *mon = opaque;
431
432 assert(qobject_type(obj) == QTYPE_QDICT);
433 cpu = qobject_to_qdict(obj);
434
435 if (strcmp(qdict_get_str(cpu, "current"), "yes") == 0)
436 active = '*';
437
438 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
439
440#if defined(TARGET_I386)
441 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
442 (target_ulong) qdict_get_int(cpu, "pc"));
443#elif defined(TARGET_PPC)
444 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
445 (target_long) qdict_get_int(cpu, "nip"));
446#elif defined(TARGET_SPARC)
447 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
448 (target_long) qdict_get_int(cpu, "pc"));
449 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
450 (target_long) qdict_get_int(cpu, "npc"));
451#elif defined(TARGET_MIPS)
452 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
453 (target_long) qdict_get_int(cpu, "PC"));
454#endif
455
456 if (strcmp(qdict_get_str(cpu, "halted"), "yes") == 0)
457 monitor_printf(mon, " (halted)");
458
459 monitor_printf(mon, "\n");
460}
461
462static void monitor_print_cpus(Monitor *mon, const QObject *data)
463{
464 QList *cpu_list;
465
466 assert(qobject_type(data) == QTYPE_QLIST);
467 cpu_list = qobject_to_qlist(data);
468 qlist_iter(cpu_list, print_cpu_iter, mon);
469}
470
471/**
472 * do_info_cpus(): Show CPU information
473 *
474 * Return a QList with a QDict for each CPU.
475 *
476 * For example:
477 *
478 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
479 * { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]
480 */
481static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000482{
483 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300484 QList *cpu_list;
485
486 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000487
488 /* just to set the default cpu if not already done */
489 mon_get_cpu();
490
491 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300492 const char *answer;
493 QDict *cpu = qdict_new();
494
Avi Kivity4c0960c2009-08-17 23:19:53 +0300495 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300496
497 qdict_put(cpu, "CPU", qint_from_int(env->cpu_index));
498 answer = (env == mon->mon_cpu) ? "yes" : "no";
499 qdict_put(cpu, "current", qstring_from_str(answer));
500
bellard6a00d602005-11-21 23:25:50 +0000501#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300502 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000503#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300504 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000505#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300506 qdict_put(cpu, "pc", qint_from_int(env->pc));
507 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000508#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300509 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000510#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300511 answer = env->halted ? "yes" : "no";
512 qdict_put(cpu, "halted", qstring_from_str(answer));
513
514 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000515 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300516
517 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000518}
519
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300520static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000521{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300522 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000523 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000524 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000525}
526
aliguori376253e2009-03-05 23:01:23 +0000527static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000528{
aliguori376253e2009-03-05 23:01:23 +0000529 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000530}
531
aliguori376253e2009-03-05 23:01:23 +0000532static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000533{
534 int i;
bellard7e2515e2004-08-01 21:52:19 +0000535 const char *str;
ths3b46e622007-09-17 08:09:54 +0000536
aliguoricde76ee2009-03-05 23:01:51 +0000537 if (!mon->rs)
538 return;
bellard7e2515e2004-08-01 21:52:19 +0000539 i = 0;
540 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000541 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000542 if (!str)
543 break;
aliguori376253e2009-03-05 23:01:23 +0000544 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000545 i++;
bellardaa455482004-04-04 13:07:25 +0000546 }
547}
548
j_mayer76a66252007-03-07 08:32:30 +0000549#if defined(TARGET_PPC)
550/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000551static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000552{
553 CPUState *env;
554
555 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000556 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000557}
558#endif
559
Luiz Capitulinob223f352009-10-07 13:41:56 -0300560/**
561 * do_quit(): Quit QEMU execution
562 */
563static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000564{
565 exit(0);
566}
567
aliguori376253e2009-03-05 23:01:23 +0000568static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000569{
570 if (bdrv_is_inserted(bs)) {
571 if (!force) {
572 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000573 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000574 return -1;
575 }
576 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000577 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000578 return -1;
579 }
580 }
581 bdrv_close(bs);
582 }
583 return 0;
584}
585
Luiz Capitulinoe1c923a2009-10-16 12:23:49 -0300586static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000587{
588 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300589 int force = qdict_get_int(qdict, "force");
590 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000591
bellard9307c4c2004-04-04 12:57:25 +0000592 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000593 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000594 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000595 return;
596 }
aliguori376253e2009-03-05 23:01:23 +0000597 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000598}
599
aliguori376253e2009-03-05 23:01:23 +0000600static void do_change_block(Monitor *mon, const char *device,
601 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000602{
603 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000604 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000605
bellard9307c4c2004-04-04 12:57:25 +0000606 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000607 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000608 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000609 return;
610 }
aurel322ecea9b2008-06-18 22:10:01 +0000611 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100612 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000613 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000614 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000615 return;
616 }
617 }
aliguori376253e2009-03-05 23:01:23 +0000618 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000619 return;
aurel322ecea9b2008-06-18 22:10:01 +0000620 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000621 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000622}
623
aliguori376253e2009-03-05 23:01:23 +0000624static void change_vnc_password_cb(Monitor *mon, const char *password,
625 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000626{
627 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000628 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000629
aliguori731b0362009-03-05 23:01:42 +0000630 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000631}
632
aliguori376253e2009-03-05 23:01:23 +0000633static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000634{
ths70848512007-08-25 01:37:05 +0000635 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000636 strcmp(target, "password") == 0) {
637 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000638 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000639 strncpy(password, arg, sizeof(password));
640 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000641 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000642 } else {
aliguori376253e2009-03-05 23:01:23 +0000643 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000644 }
ths70848512007-08-25 01:37:05 +0000645 } else {
aliguori28a76be2009-03-06 20:27:40 +0000646 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000647 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000648 }
thse25a5822007-08-25 01:36:20 +0000649}
650
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300651static void do_change(Monitor *mon, const QDict *qdict)
thse25a5822007-08-25 01:36:20 +0000652{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300653 const char *device = qdict_get_str(qdict, "device");
654 const char *target = qdict_get_str(qdict, "target");
655 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000656 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000657 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000658 } else {
aliguori28a76be2009-03-06 20:27:40 +0000659 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000660 }
661}
662
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300663static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000664{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300665 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000666}
667
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300668static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000669{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300670 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000671}
672
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300673static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000674{
675 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300676 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000677
bellard9307c4c2004-04-04 12:57:25 +0000678 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000679 mask = 0;
680 } else {
bellard9307c4c2004-04-04 12:57:25 +0000681 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000682 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000683 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000684 return;
685 }
686 }
687 cpu_set_log(mask);
688}
689
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300690static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000691{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300692 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000693 if (!option || !strcmp(option, "on")) {
694 singlestep = 1;
695 } else if (!strcmp(option, "off")) {
696 singlestep = 0;
697 } else {
698 monitor_printf(mon, "unexpected option %s\n", option);
699 }
700}
701
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -0300702/**
703 * do_stop(): Stop VM execution
704 */
705static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +0000706{
707 vm_stop(EXCP_INTERRUPT);
708}
709
aliguoribb5fc202009-03-05 23:01:15 +0000710static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000711
aliguori376253e2009-03-05 23:01:23 +0000712struct bdrv_iterate_context {
713 Monitor *mon;
714 int err;
715};
aliguoric0f4ce72009-03-05 23:01:01 +0000716
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300717/**
718 * do_cont(): Resume emulation.
719 */
720static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +0000721{
722 struct bdrv_iterate_context context = { mon, 0 };
723
724 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000725 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000726 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000727 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000728}
729
aliguoribb5fc202009-03-05 23:01:15 +0000730static void bdrv_key_cb(void *opaque, int err)
731{
aliguori376253e2009-03-05 23:01:23 +0000732 Monitor *mon = opaque;
733
aliguoribb5fc202009-03-05 23:01:15 +0000734 /* another key was set successfully, retry to continue */
735 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300736 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000737}
738
739static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
740{
aliguori376253e2009-03-05 23:01:23 +0000741 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000742
aliguori376253e2009-03-05 23:01:23 +0000743 if (!context->err && bdrv_key_required(bs)) {
744 context->err = -EBUSY;
745 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
746 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000747 }
748}
749
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300750static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000751{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300752 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000753 if (!device)
754 device = "tcp::" DEFAULT_GDBSTUB_PORT;
755 if (gdbserver_start(device) < 0) {
756 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
757 device);
758 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000759 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000760 } else {
aliguori59030a82009-04-05 18:43:41 +0000761 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
762 device);
bellard8a7ddc32004-03-31 19:00:16 +0000763 }
764}
765
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300766static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100767{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300768 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100769 if (select_watchdog_action(action) == -1) {
770 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
771 }
772}
773
aliguori376253e2009-03-05 23:01:23 +0000774static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000775{
aliguori376253e2009-03-05 23:01:23 +0000776 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000777 switch(c) {
778 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000779 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000780 break;
781 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000782 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000783 break;
784 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000785 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000786 break;
787 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000788 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000789 break;
790 default:
791 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000792 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000793 } else {
aliguori376253e2009-03-05 23:01:23 +0000794 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000795 }
796 break;
797 }
aliguori376253e2009-03-05 23:01:23 +0000798 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000799}
800
aliguori376253e2009-03-05 23:01:23 +0000801static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -0500802 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000803{
bellard6a00d602005-11-21 23:25:50 +0000804 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000805 int nb_per_line, l, line_size, i, max_digits, len;
806 uint8_t buf[16];
807 uint64_t v;
808
809 if (format == 'i') {
810 int flags;
811 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000812 env = mon_get_cpu();
813 if (!env && !is_physical)
814 return;
bellard9307c4c2004-04-04 12:57:25 +0000815#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000816 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000817 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000818 } else if (wsize == 4) {
819 flags = 0;
820 } else {
bellard6a15fd12006-04-12 21:07:07 +0000821 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000822 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000823 if (env) {
824#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000825 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000826 (env->segs[R_CS].flags & DESC_L_MASK))
827 flags = 2;
828 else
829#endif
830 if (!(env->segs[R_CS].flags & DESC_B_MASK))
831 flags = 1;
832 }
bellard4c27ba22004-04-25 18:05:08 +0000833 }
834#endif
aliguori376253e2009-03-05 23:01:23 +0000835 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000836 return;
837 }
838
839 len = wsize * count;
840 if (wsize == 1)
841 line_size = 8;
842 else
843 line_size = 16;
844 nb_per_line = line_size / wsize;
845 max_digits = 0;
846
847 switch(format) {
848 case 'o':
849 max_digits = (wsize * 8 + 2) / 3;
850 break;
851 default:
852 case 'x':
853 max_digits = (wsize * 8) / 4;
854 break;
855 case 'u':
856 case 'd':
857 max_digits = (wsize * 8 * 10 + 32) / 33;
858 break;
859 case 'c':
860 wsize = 1;
861 break;
862 }
863
864 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000865 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000866 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000867 else
aliguori376253e2009-03-05 23:01:23 +0000868 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000869 l = len;
870 if (l > line_size)
871 l = line_size;
872 if (is_physical) {
873 cpu_physical_memory_rw(addr, buf, l, 0);
874 } else {
bellard6a00d602005-11-21 23:25:50 +0000875 env = mon_get_cpu();
876 if (!env)
877 break;
aliguoric8f79b62008-08-18 14:00:20 +0000878 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000879 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000880 break;
881 }
bellard9307c4c2004-04-04 12:57:25 +0000882 }
ths5fafdf22007-09-16 21:08:06 +0000883 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000884 while (i < l) {
885 switch(wsize) {
886 default:
887 case 1:
888 v = ldub_raw(buf + i);
889 break;
890 case 2:
891 v = lduw_raw(buf + i);
892 break;
893 case 4:
bellard92a31b12005-02-10 22:00:52 +0000894 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000895 break;
896 case 8:
897 v = ldq_raw(buf + i);
898 break;
899 }
aliguori376253e2009-03-05 23:01:23 +0000900 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000901 switch(format) {
902 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000903 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000904 break;
905 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000906 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000907 break;
908 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000909 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000910 break;
911 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000912 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000913 break;
914 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000915 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000916 break;
917 }
918 i += wsize;
919 }
aliguori376253e2009-03-05 23:01:23 +0000920 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000921 addr += l;
922 len -= l;
923 }
924}
925
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300926static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000927{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300928 int count = qdict_get_int(qdict, "count");
929 int format = qdict_get_int(qdict, "format");
930 int size = qdict_get_int(qdict, "size");
931 target_long addr = qdict_get_int(qdict, "addr");
932
aliguori376253e2009-03-05 23:01:23 +0000933 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000934}
935
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300936static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000937{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300938 int count = qdict_get_int(qdict, "count");
939 int format = qdict_get_int(qdict, "format");
940 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -0500941 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300942
aliguori376253e2009-03-05 23:01:23 +0000943 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000944}
945
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300946static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000947{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300948 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -0500949 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300950
blueswir17743e582007-09-24 18:39:04 +0000951#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000952 switch(format) {
953 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000954 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000955 break;
956 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000957 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000958 break;
959 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000960 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000961 break;
962 default:
963 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000964 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000965 break;
966 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000967 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000968 break;
969 }
bellard92a31b12005-02-10 22:00:52 +0000970#else
971 switch(format) {
972 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000973 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000974 break;
975 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000976 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000977 break;
978 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000979 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000980 break;
981 default:
982 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000983 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000984 break;
985 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000986 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000987 break;
988 }
989#endif
aliguori376253e2009-03-05 23:01:23 +0000990 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000991}
992
Luiz Capitulino57e09452009-10-16 12:23:43 -0300993static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +0000994{
995 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300996 uint32_t size = qdict_get_int(qdict, "size");
997 const char *filename = qdict_get_str(qdict, "filename");
998 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +0000999 uint32_t l;
1000 CPUState *env;
1001 uint8_t buf[1024];
1002
1003 env = mon_get_cpu();
1004 if (!env)
1005 return;
1006
1007 f = fopen(filename, "wb");
1008 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001009 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +00001010 return;
1011 }
1012 while (size != 0) {
1013 l = sizeof(buf);
1014 if (l > size)
1015 l = size;
1016 cpu_memory_rw_debug(env, addr, buf, l, 0);
1017 fwrite(buf, 1, l, f);
1018 addr += l;
1019 size -= l;
1020 }
1021 fclose(f);
1022}
1023
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001024static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1025 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001026{
1027 FILE *f;
1028 uint32_t l;
1029 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001030 uint32_t size = qdict_get_int(qdict, "size");
1031 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001032 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +00001033
1034 f = fopen(filename, "wb");
1035 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001036 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +00001037 return;
1038 }
1039 while (size != 0) {
1040 l = sizeof(buf);
1041 if (l > size)
1042 l = size;
1043 cpu_physical_memory_rw(addr, buf, l, 0);
1044 fwrite(buf, 1, l, f);
1045 fflush(f);
1046 addr += l;
1047 size -= l;
1048 }
1049 fclose(f);
1050}
1051
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001052static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001053{
1054 uint32_t addr;
1055 uint8_t buf[1];
1056 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001057 uint32_t start = qdict_get_int(qdict, "start");
1058 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001059
1060 sum = 0;
1061 for(addr = start; addr < (start + size); addr++) {
1062 cpu_physical_memory_rw(addr, buf, 1, 0);
1063 /* BSD sum algorithm ('sum' Unix command) */
1064 sum = (sum >> 1) | (sum << 15);
1065 sum += buf[0];
1066 }
aliguori376253e2009-03-05 23:01:23 +00001067 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001068}
1069
bellarda3a91a32004-06-04 11:06:21 +00001070typedef struct {
1071 int keycode;
1072 const char *name;
1073} KeyDef;
1074
1075static const KeyDef key_defs[] = {
1076 { 0x2a, "shift" },
1077 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001078
bellarda3a91a32004-06-04 11:06:21 +00001079 { 0x38, "alt" },
1080 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001081 { 0x64, "altgr" },
1082 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001083 { 0x1d, "ctrl" },
1084 { 0x9d, "ctrl_r" },
1085
1086 { 0xdd, "menu" },
1087
1088 { 0x01, "esc" },
1089
1090 { 0x02, "1" },
1091 { 0x03, "2" },
1092 { 0x04, "3" },
1093 { 0x05, "4" },
1094 { 0x06, "5" },
1095 { 0x07, "6" },
1096 { 0x08, "7" },
1097 { 0x09, "8" },
1098 { 0x0a, "9" },
1099 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001100 { 0x0c, "minus" },
1101 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001102 { 0x0e, "backspace" },
1103
1104 { 0x0f, "tab" },
1105 { 0x10, "q" },
1106 { 0x11, "w" },
1107 { 0x12, "e" },
1108 { 0x13, "r" },
1109 { 0x14, "t" },
1110 { 0x15, "y" },
1111 { 0x16, "u" },
1112 { 0x17, "i" },
1113 { 0x18, "o" },
1114 { 0x19, "p" },
1115
1116 { 0x1c, "ret" },
1117
1118 { 0x1e, "a" },
1119 { 0x1f, "s" },
1120 { 0x20, "d" },
1121 { 0x21, "f" },
1122 { 0x22, "g" },
1123 { 0x23, "h" },
1124 { 0x24, "j" },
1125 { 0x25, "k" },
1126 { 0x26, "l" },
1127
1128 { 0x2c, "z" },
1129 { 0x2d, "x" },
1130 { 0x2e, "c" },
1131 { 0x2f, "v" },
1132 { 0x30, "b" },
1133 { 0x31, "n" },
1134 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001135 { 0x33, "comma" },
1136 { 0x34, "dot" },
1137 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001138
balrog4d3b6f62008-02-10 16:33:14 +00001139 { 0x37, "asterisk" },
1140
bellarda3a91a32004-06-04 11:06:21 +00001141 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001142 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001143 { 0x3b, "f1" },
1144 { 0x3c, "f2" },
1145 { 0x3d, "f3" },
1146 { 0x3e, "f4" },
1147 { 0x3f, "f5" },
1148 { 0x40, "f6" },
1149 { 0x41, "f7" },
1150 { 0x42, "f8" },
1151 { 0x43, "f9" },
1152 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001153 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001154 { 0x46, "scroll_lock" },
1155
bellard64866c32006-05-07 18:03:31 +00001156 { 0xb5, "kp_divide" },
1157 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001158 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001159 { 0x4e, "kp_add" },
1160 { 0x9c, "kp_enter" },
1161 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001162 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001163
1164 { 0x52, "kp_0" },
1165 { 0x4f, "kp_1" },
1166 { 0x50, "kp_2" },
1167 { 0x51, "kp_3" },
1168 { 0x4b, "kp_4" },
1169 { 0x4c, "kp_5" },
1170 { 0x4d, "kp_6" },
1171 { 0x47, "kp_7" },
1172 { 0x48, "kp_8" },
1173 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001174
bellarda3a91a32004-06-04 11:06:21 +00001175 { 0x56, "<" },
1176
1177 { 0x57, "f11" },
1178 { 0x58, "f12" },
1179
1180 { 0xb7, "print" },
1181
1182 { 0xc7, "home" },
1183 { 0xc9, "pgup" },
1184 { 0xd1, "pgdn" },
1185 { 0xcf, "end" },
1186
1187 { 0xcb, "left" },
1188 { 0xc8, "up" },
1189 { 0xd0, "down" },
1190 { 0xcd, "right" },
1191
1192 { 0xd2, "insert" },
1193 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001194#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1195 { 0xf0, "stop" },
1196 { 0xf1, "again" },
1197 { 0xf2, "props" },
1198 { 0xf3, "undo" },
1199 { 0xf4, "front" },
1200 { 0xf5, "copy" },
1201 { 0xf6, "open" },
1202 { 0xf7, "paste" },
1203 { 0xf8, "find" },
1204 { 0xf9, "cut" },
1205 { 0xfa, "lf" },
1206 { 0xfb, "help" },
1207 { 0xfc, "meta_l" },
1208 { 0xfd, "meta_r" },
1209 { 0xfe, "compose" },
1210#endif
bellarda3a91a32004-06-04 11:06:21 +00001211 { 0, NULL },
1212};
1213
1214static int get_keycode(const char *key)
1215{
1216 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001217 char *endp;
1218 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001219
1220 for(p = key_defs; p->name != NULL; p++) {
1221 if (!strcmp(key, p->name))
1222 return p->keycode;
1223 }
bellard64866c32006-05-07 18:03:31 +00001224 if (strstart(key, "0x", NULL)) {
1225 ret = strtoul(key, &endp, 0);
1226 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1227 return ret;
1228 }
bellarda3a91a32004-06-04 11:06:21 +00001229 return -1;
1230}
1231
balrogc8256f92008-06-08 22:45:01 +00001232#define MAX_KEYCODES 16
1233static uint8_t keycodes[MAX_KEYCODES];
1234static int nb_pending_keycodes;
1235static QEMUTimer *key_timer;
1236
1237static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001238{
balrogc8256f92008-06-08 22:45:01 +00001239 int keycode;
1240
1241 while (nb_pending_keycodes > 0) {
1242 nb_pending_keycodes--;
1243 keycode = keycodes[nb_pending_keycodes];
1244 if (keycode & 0x80)
1245 kbd_put_keycode(0xe0);
1246 kbd_put_keycode(keycode | 0x80);
1247 }
1248}
1249
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001250static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001251{
balrog3401c0d2008-06-04 10:05:59 +00001252 char keyname_buf[16];
1253 char *separator;
1254 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001255 const char *string = qdict_get_str(qdict, "string");
1256 int has_hold_time = qdict_haskey(qdict, "hold_time");
1257 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001258
balrogc8256f92008-06-08 22:45:01 +00001259 if (nb_pending_keycodes > 0) {
1260 qemu_del_timer(key_timer);
1261 release_keys(NULL);
1262 }
1263 if (!has_hold_time)
1264 hold_time = 100;
1265 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001266 while (1) {
1267 separator = strchr(string, '-');
1268 keyname_len = separator ? separator - string : strlen(string);
1269 if (keyname_len > 0) {
1270 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1271 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001272 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001273 return;
bellarda3a91a32004-06-04 11:06:21 +00001274 }
balrogc8256f92008-06-08 22:45:01 +00001275 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001276 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001277 return;
1278 }
1279 keyname_buf[keyname_len] = 0;
1280 keycode = get_keycode(keyname_buf);
1281 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001282 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001283 return;
1284 }
balrogc8256f92008-06-08 22:45:01 +00001285 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001286 }
balrog3401c0d2008-06-04 10:05:59 +00001287 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001288 break;
balrog3401c0d2008-06-04 10:05:59 +00001289 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001290 }
balrogc8256f92008-06-08 22:45:01 +00001291 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001292 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001293 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001294 keycode = keycodes[i];
1295 if (keycode & 0x80)
1296 kbd_put_keycode(0xe0);
1297 kbd_put_keycode(keycode & 0x7f);
1298 }
balrogc8256f92008-06-08 22:45:01 +00001299 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001300 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001301 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001302}
1303
bellard13224a82006-07-14 22:03:35 +00001304static int mouse_button_state;
1305
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001306static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001307{
1308 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001309 const char *dx_str = qdict_get_str(qdict, "dx_str");
1310 const char *dy_str = qdict_get_str(qdict, "dy_str");
1311 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001312 dx = strtol(dx_str, NULL, 0);
1313 dy = strtol(dy_str, NULL, 0);
1314 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001315 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001316 dz = strtol(dz_str, NULL, 0);
1317 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1318}
1319
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001320static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001321{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001322 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001323 mouse_button_state = button_state;
1324 kbd_mouse_event(0, 0, 0, mouse_button_state);
1325}
1326
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001327static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001328{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001329 int size = qdict_get_int(qdict, "size");
1330 int addr = qdict_get_int(qdict, "addr");
1331 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001332 uint32_t val;
1333 int suffix;
1334
1335 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001336 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001337 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001338 addr++;
1339 }
1340 addr &= 0xffff;
1341
1342 switch(size) {
1343 default:
1344 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001345 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001346 suffix = 'b';
1347 break;
1348 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001349 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001350 suffix = 'w';
1351 break;
1352 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001353 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001354 suffix = 'l';
1355 break;
1356 }
aliguori376253e2009-03-05 23:01:23 +00001357 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1358 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001359}
bellarda3a91a32004-06-04 11:06:21 +00001360
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001361static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001362{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001363 int size = qdict_get_int(qdict, "size");
1364 int addr = qdict_get_int(qdict, "addr");
1365 int val = qdict_get_int(qdict, "val");
1366
Jan Kiszkaf1147842009-07-14 10:20:11 +02001367 addr &= IOPORTS_MASK;
1368
1369 switch (size) {
1370 default:
1371 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001372 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001373 break;
1374 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001375 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001376 break;
1377 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001378 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001379 break;
1380 }
1381}
1382
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001383static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001384{
1385 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001386 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001387
Jan Kiszka76e30d02009-07-02 00:19:02 +02001388 res = qemu_boot_set(bootdevice);
1389 if (res == 0) {
1390 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1391 } else if (res > 0) {
1392 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001393 } else {
aliguori376253e2009-03-05 23:01:23 +00001394 monitor_printf(mon, "no function defined to set boot device list for "
1395 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001396 }
1397}
1398
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001399/**
1400 * do_system_reset(): Issue a machine reset
1401 */
1402static void do_system_reset(Monitor *mon, const QDict *qdict,
1403 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001404{
1405 qemu_system_reset_request();
1406}
1407
Luiz Capitulino43076662009-10-07 13:41:59 -03001408/**
1409 * do_system_powerdown(): Issue a machine powerdown
1410 */
1411static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1412 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001413{
1414 qemu_system_powerdown_request();
1415}
1416
bellardb86bda52004-09-18 19:32:46 +00001417#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001418static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001419{
aliguori376253e2009-03-05 23:01:23 +00001420 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1421 addr,
1422 pte & mask,
1423 pte & PG_GLOBAL_MASK ? 'G' : '-',
1424 pte & PG_PSE_MASK ? 'P' : '-',
1425 pte & PG_DIRTY_MASK ? 'D' : '-',
1426 pte & PG_ACCESSED_MASK ? 'A' : '-',
1427 pte & PG_PCD_MASK ? 'C' : '-',
1428 pte & PG_PWT_MASK ? 'T' : '-',
1429 pte & PG_USER_MASK ? 'U' : '-',
1430 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001431}
1432
aliguori376253e2009-03-05 23:01:23 +00001433static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001434{
bellard6a00d602005-11-21 23:25:50 +00001435 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001436 int l1, l2;
1437 uint32_t pgd, pde, pte;
1438
bellard6a00d602005-11-21 23:25:50 +00001439 env = mon_get_cpu();
1440 if (!env)
1441 return;
1442
bellardb86bda52004-09-18 19:32:46 +00001443 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001444 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001445 return;
1446 }
1447 pgd = env->cr[3] & ~0xfff;
1448 for(l1 = 0; l1 < 1024; l1++) {
1449 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1450 pde = le32_to_cpu(pde);
1451 if (pde & PG_PRESENT_MASK) {
1452 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001453 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001454 } else {
1455 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001456 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001457 (uint8_t *)&pte, 4);
1458 pte = le32_to_cpu(pte);
1459 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001460 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001461 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001462 ~0xfff);
1463 }
1464 }
1465 }
1466 }
1467 }
1468}
1469
aliguori376253e2009-03-05 23:01:23 +00001470static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001471 uint32_t end, int prot)
1472{
bellard9746b152004-11-11 18:30:24 +00001473 int prot1;
1474 prot1 = *plast_prot;
1475 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001476 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001477 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1478 *pstart, end, end - *pstart,
1479 prot1 & PG_USER_MASK ? 'u' : '-',
1480 'r',
1481 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001482 }
1483 if (prot != 0)
1484 *pstart = end;
1485 else
1486 *pstart = -1;
1487 *plast_prot = prot;
1488 }
1489}
1490
aliguori376253e2009-03-05 23:01:23 +00001491static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001492{
bellard6a00d602005-11-21 23:25:50 +00001493 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001494 int l1, l2, prot, last_prot;
1495 uint32_t pgd, pde, pte, start, end;
1496
bellard6a00d602005-11-21 23:25:50 +00001497 env = mon_get_cpu();
1498 if (!env)
1499 return;
1500
bellardb86bda52004-09-18 19:32:46 +00001501 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001502 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001503 return;
1504 }
1505 pgd = env->cr[3] & ~0xfff;
1506 last_prot = 0;
1507 start = -1;
1508 for(l1 = 0; l1 < 1024; l1++) {
1509 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1510 pde = le32_to_cpu(pde);
1511 end = l1 << 22;
1512 if (pde & PG_PRESENT_MASK) {
1513 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1514 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001515 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001516 } else {
1517 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001518 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001519 (uint8_t *)&pte, 4);
1520 pte = le32_to_cpu(pte);
1521 end = (l1 << 22) + (l2 << 12);
1522 if (pte & PG_PRESENT_MASK) {
1523 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1524 } else {
1525 prot = 0;
1526 }
aliguori376253e2009-03-05 23:01:23 +00001527 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001528 }
1529 }
1530 } else {
1531 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001532 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001533 }
1534 }
1535}
1536#endif
1537
aurel327c664e22009-03-03 06:12:22 +00001538#if defined(TARGET_SH4)
1539
aliguori376253e2009-03-05 23:01:23 +00001540static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001541{
aliguori376253e2009-03-05 23:01:23 +00001542 monitor_printf(mon, " tlb%i:\t"
1543 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1544 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1545 "dirty=%hhu writethrough=%hhu\n",
1546 idx,
1547 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1548 tlb->v, tlb->sh, tlb->c, tlb->pr,
1549 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001550}
1551
aliguori376253e2009-03-05 23:01:23 +00001552static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001553{
1554 CPUState *env = mon_get_cpu();
1555 int i;
1556
aliguori376253e2009-03-05 23:01:23 +00001557 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001558 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001559 print_tlb (mon, i, &env->itlb[i]);
1560 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001561 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001562 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001563}
1564
1565#endif
1566
aliguori376253e2009-03-05 23:01:23 +00001567static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001568{
1569#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001570 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001571 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001572 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001573 else
aliguori376253e2009-03-05 23:01:23 +00001574 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001575#else
aliguori376253e2009-03-05 23:01:23 +00001576 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001577#endif
1578}
1579
aliguori030ea372009-04-21 22:30:47 +00001580static void do_info_numa(Monitor *mon)
1581{
aliguorib28b6232009-04-22 20:20:29 +00001582 int i;
aliguori030ea372009-04-21 22:30:47 +00001583 CPUState *env;
1584
1585 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1586 for (i = 0; i < nb_numa_nodes; i++) {
1587 monitor_printf(mon, "node %d cpus:", i);
1588 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1589 if (env->numa_node == i) {
1590 monitor_printf(mon, " %d", env->cpu_index);
1591 }
1592 }
1593 monitor_printf(mon, "\n");
1594 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1595 node_mem[i] >> 20);
1596 }
1597}
1598
bellard5f1ce942006-02-08 22:40:15 +00001599#ifdef CONFIG_PROFILER
1600
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001601int64_t qemu_time;
1602int64_t dev_time;
1603
aliguori376253e2009-03-05 23:01:23 +00001604static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001605{
1606 int64_t total;
1607 total = qemu_time;
1608 if (total == 0)
1609 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001610 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001611 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001612 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001613 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001614 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001615 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001616}
1617#else
aliguori376253e2009-03-05 23:01:23 +00001618static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001619{
aliguori376253e2009-03-05 23:01:23 +00001620 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001621}
1622#endif
1623
bellardec36b692006-07-16 18:57:03 +00001624/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001625static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001626
aliguori376253e2009-03-05 23:01:23 +00001627static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001628{
1629 int i;
1630 CaptureState *s;
1631
1632 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001633 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001634 s->ops.info (s->opaque);
1635 }
1636}
1637
Blue Swirl23130862009-06-06 08:22:04 +00001638#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001639static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001640{
1641 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001642 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001643 CaptureState *s;
1644
1645 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1646 if (i == n) {
1647 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001648 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00001649 qemu_free (s);
1650 return;
1651 }
1652 }
1653}
1654
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001655static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001656{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001657 const char *path = qdict_get_str(qdict, "path");
1658 int has_freq = qdict_haskey(qdict, "freq");
1659 int freq = qdict_get_try_int(qdict, "freq", -1);
1660 int has_bits = qdict_haskey(qdict, "bits");
1661 int bits = qdict_get_try_int(qdict, "bits", -1);
1662 int has_channels = qdict_haskey(qdict, "nchannels");
1663 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001664 CaptureState *s;
1665
1666 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001667
1668 freq = has_freq ? freq : 44100;
1669 bits = has_bits ? bits : 16;
1670 nchannels = has_channels ? nchannels : 2;
1671
1672 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001673 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001674 qemu_free (s);
1675 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001676 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001677}
1678#endif
1679
aurel32dc1c0b72008-04-27 23:52:12 +00001680#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001681static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001682{
1683 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001684 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001685
1686 for (env = first_cpu; env != NULL; env = env->next_cpu)
1687 if (env->cpu_index == cpu_index) {
1688 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1689 break;
1690 }
1691}
1692#endif
1693
aliguori376253e2009-03-05 23:01:23 +00001694static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001695{
aurel321b530a62009-04-05 20:08:59 +00001696 if (vm_running) {
1697 if (singlestep) {
1698 monitor_printf(mon, "VM status: running (single step mode)\n");
1699 } else {
1700 monitor_printf(mon, "VM status: running\n");
1701 }
1702 } else
aliguori376253e2009-03-05 23:01:23 +00001703 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001704}
1705
Luiz Capitulino83fb1de2009-10-07 13:42:01 -03001706/**
1707 * do_balloon(): Request VM to change its memory allocation
1708 */
1709static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001710{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001711 int value = qdict_get_int(qdict, "value");
Anthony Liguoric227f092009-10-01 16:12:16 -05001712 ram_addr_t target = value;
aliguoridf751fa2008-12-04 20:19:35 +00001713 qemu_balloon(target << 20);
1714}
1715
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001716static void monitor_print_balloon(Monitor *mon, const QObject *data)
1717{
1718 monitor_printf(mon, "balloon: actual=%d\n",
1719 (int)qint_get_int(qobject_to_qint(data)));
1720}
1721
1722/**
1723 * do_info_balloon(): Balloon information
1724 */
1725static void do_info_balloon(Monitor *mon, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001726{
Anthony Liguoric227f092009-10-01 16:12:16 -05001727 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00001728
1729 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001730 if (kvm_enabled() && !kvm_has_sync_mmu())
Luiz Capitulino5d6c37f2009-11-18 23:05:36 -02001731 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
aliguoribd322082008-12-04 20:33:06 +00001732 else if (actual == 0)
Luiz Capitulino5d6c37f2009-11-18 23:05:36 -02001733 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
aliguoridf751fa2008-12-04 20:19:35 +00001734 else
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001735 *ret_data = QOBJECT(qint_from_int((int)(actual >> 20)));
aliguoridf751fa2008-12-04 20:19:35 +00001736}
1737
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001738static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001739{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001740 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001741
aliguori76655d62009-03-06 20:27:37 +00001742 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001743 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001744 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001745 return acl;
1746}
aliguori76655d62009-03-06 20:27:37 +00001747
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001748static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001749{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001750 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001751 qemu_acl *acl = find_acl(mon, aclname);
1752 qemu_acl_entry *entry;
1753 int i = 0;
1754
1755 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001756 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001757 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001758 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001759 i++;
1760 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001761 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001762 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001763 }
1764}
1765
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001766static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001767{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001768 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001769 qemu_acl *acl = find_acl(mon, aclname);
1770
1771 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001772 qemu_acl_reset(acl);
1773 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001774 }
1775}
aliguori76655d62009-03-06 20:27:37 +00001776
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001777static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001778{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001779 const char *aclname = qdict_get_str(qdict, "aclname");
1780 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001781 qemu_acl *acl = find_acl(mon, aclname);
1782
1783 if (acl) {
1784 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001785 acl->defaultDeny = 0;
1786 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001787 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001788 acl->defaultDeny = 1;
1789 monitor_printf(mon, "acl: policy set to 'deny'\n");
1790 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001791 monitor_printf(mon, "acl: unknown policy '%s', "
1792 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001793 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001794 }
1795}
aliguori76655d62009-03-06 20:27:37 +00001796
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001797static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001798{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001799 const char *aclname = qdict_get_str(qdict, "aclname");
1800 const char *match = qdict_get_str(qdict, "match");
1801 const char *policy = qdict_get_str(qdict, "policy");
1802 int has_index = qdict_haskey(qdict, "index");
1803 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001804 qemu_acl *acl = find_acl(mon, aclname);
1805 int deny, ret;
1806
1807 if (acl) {
1808 if (strcmp(policy, "allow") == 0) {
1809 deny = 0;
1810 } else if (strcmp(policy, "deny") == 0) {
1811 deny = 1;
1812 } else {
1813 monitor_printf(mon, "acl: unknown policy '%s', "
1814 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001815 return;
1816 }
aliguori28a76be2009-03-06 20:27:40 +00001817 if (has_index)
1818 ret = qemu_acl_insert(acl, deny, match, index);
1819 else
1820 ret = qemu_acl_append(acl, deny, match);
1821 if (ret < 0)
1822 monitor_printf(mon, "acl: unable to add acl entry\n");
1823 else
1824 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001825 }
1826}
aliguori76655d62009-03-06 20:27:37 +00001827
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001828static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001829{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001830 const char *aclname = qdict_get_str(qdict, "aclname");
1831 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001832 qemu_acl *acl = find_acl(mon, aclname);
1833 int ret;
aliguori76655d62009-03-06 20:27:37 +00001834
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001835 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001836 ret = qemu_acl_remove(acl, match);
1837 if (ret < 0)
1838 monitor_printf(mon, "acl: no matching acl entry\n");
1839 else
1840 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001841 }
1842}
1843
Huang Ying79c4f6b2009-06-23 10:05:14 +08001844#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001845static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001846{
1847 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001848 int cpu_index = qdict_get_int(qdict, "cpu_index");
1849 int bank = qdict_get_int(qdict, "bank");
1850 uint64_t status = qdict_get_int(qdict, "status");
1851 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1852 uint64_t addr = qdict_get_int(qdict, "addr");
1853 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08001854
1855 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1856 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1857 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1858 break;
1859 }
1860}
1861#endif
1862
Luiz Capitulinof0d60002009-10-16 12:23:50 -03001863static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001864{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001865 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001866 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001867 int fd;
1868
1869 fd = qemu_chr_get_msgfd(mon->chr);
1870 if (fd == -1) {
1871 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1872 return;
1873 }
1874
1875 if (qemu_isdigit(fdname[0])) {
1876 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1877 return;
1878 }
1879
1880 fd = dup(fd);
1881 if (fd == -1) {
1882 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1883 strerror(errno));
1884 return;
1885 }
1886
Blue Swirl72cf2d42009-09-12 07:36:22 +00001887 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001888 if (strcmp(monfd->name, fdname) != 0) {
1889 continue;
1890 }
1891
1892 close(monfd->fd);
1893 monfd->fd = fd;
1894 return;
1895 }
1896
Anthony Liguoric227f092009-10-01 16:12:16 -05001897 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001898 monfd->name = qemu_strdup(fdname);
1899 monfd->fd = fd;
1900
Blue Swirl72cf2d42009-09-12 07:36:22 +00001901 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001902}
1903
Luiz Capitulino18f3a512009-10-16 12:23:51 -03001904static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001905{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001906 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001907 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001908
Blue Swirl72cf2d42009-09-12 07:36:22 +00001909 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001910 if (strcmp(monfd->name, fdname) != 0) {
1911 continue;
1912 }
1913
Blue Swirl72cf2d42009-09-12 07:36:22 +00001914 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001915 close(monfd->fd);
1916 qemu_free(monfd->name);
1917 qemu_free(monfd);
1918 return;
1919 }
1920
1921 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1922 fdname);
1923}
1924
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001925static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001926{
1927 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001928 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001929
1930 vm_stop(0);
1931
Juan Quintela05f24012009-08-20 19:42:22 +02001932 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001933 vm_start();
1934}
1935
Mark McLoughlin7768e042009-07-22 09:11:41 +01001936int monitor_get_fd(Monitor *mon, const char *fdname)
1937{
Anthony Liguoric227f092009-10-01 16:12:16 -05001938 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001939
Blue Swirl72cf2d42009-09-12 07:36:22 +00001940 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001941 int fd;
1942
1943 if (strcmp(monfd->name, fdname) != 0) {
1944 continue;
1945 }
1946
1947 fd = monfd->fd;
1948
1949 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001950 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001951 qemu_free(monfd->name);
1952 qemu_free(monfd);
1953
1954 return fd;
1955 }
1956
1957 return -1;
1958}
1959
Anthony Liguoric227f092009-10-01 16:12:16 -05001960static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001961#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001962 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001963};
1964
Blue Swirl23130862009-06-06 08:22:04 +00001965/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05001966static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001967 {
1968 .name = "version",
1969 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001970 .params = "",
1971 .help = "show the version of QEMU",
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03001972 .user_print = monitor_print_qobject,
1973 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001974 },
1975 {
1976 .name = "network",
1977 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001978 .params = "",
1979 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03001980 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001981 },
1982 {
1983 .name = "chardev",
1984 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001985 .params = "",
1986 .help = "show the character devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001987 .mhandler.info = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001988 },
1989 {
1990 .name = "block",
1991 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001992 .params = "",
1993 .help = "show the block devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001994 .mhandler.info = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001995 },
1996 {
1997 .name = "blockstats",
1998 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001999 .params = "",
2000 .help = "show block device statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002001 .mhandler.info = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002002 },
2003 {
2004 .name = "registers",
2005 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002006 .params = "",
2007 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002008 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002009 },
2010 {
2011 .name = "cpus",
2012 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002013 .params = "",
2014 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002015 .user_print = monitor_print_cpus,
2016 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002017 },
2018 {
2019 .name = "history",
2020 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002021 .params = "",
2022 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002023 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002024 },
2025 {
2026 .name = "irq",
2027 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002028 .params = "",
2029 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002030 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002031 },
2032 {
2033 .name = "pic",
2034 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002035 .params = "",
2036 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002037 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002038 },
2039 {
2040 .name = "pci",
2041 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002042 .params = "",
2043 .help = "show PCI info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002044 .mhandler.info = pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002045 },
aurel327c664e22009-03-03 06:12:22 +00002046#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002047 {
2048 .name = "tlb",
2049 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002050 .params = "",
2051 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002052 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002053 },
aurel327c664e22009-03-03 06:12:22 +00002054#endif
2055#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002056 {
2057 .name = "mem",
2058 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002059 .params = "",
2060 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002061 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002062 },
2063 {
2064 .name = "hpet",
2065 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002066 .params = "",
2067 .help = "show state of HPET",
Luiz Capitulino910df892009-10-07 13:41:51 -03002068 .mhandler.info = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002069 },
bellardb86bda52004-09-18 19:32:46 +00002070#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002071 {
2072 .name = "jit",
2073 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002074 .params = "",
2075 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002076 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002077 },
2078 {
2079 .name = "kvm",
2080 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002081 .params = "",
2082 .help = "show KVM information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002083 .mhandler.info = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002084 },
2085 {
2086 .name = "numa",
2087 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002088 .params = "",
2089 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002090 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002091 },
2092 {
2093 .name = "usb",
2094 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002095 .params = "",
2096 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002097 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002098 },
2099 {
2100 .name = "usbhost",
2101 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002102 .params = "",
2103 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002104 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002105 },
2106 {
2107 .name = "profile",
2108 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002109 .params = "",
2110 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002111 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002112 },
2113 {
2114 .name = "capture",
2115 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002116 .params = "",
2117 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002118 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002119 },
2120 {
2121 .name = "snapshots",
2122 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002123 .params = "",
2124 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002125 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002126 },
2127 {
2128 .name = "status",
2129 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002130 .params = "",
2131 .help = "show the current VM status (running|paused)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002132 .mhandler.info = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002133 },
2134 {
2135 .name = "pcmcia",
2136 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002137 .params = "",
2138 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002139 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002140 },
2141 {
2142 .name = "mice",
2143 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002144 .params = "",
2145 .help = "show which guest mouse is receiving events",
Luiz Capitulino910df892009-10-07 13:41:51 -03002146 .mhandler.info = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002147 },
2148 {
2149 .name = "vnc",
2150 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002151 .params = "",
2152 .help = "show the vnc server status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002153 .mhandler.info = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002154 },
2155 {
2156 .name = "name",
2157 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002158 .params = "",
2159 .help = "show the current VM name",
Luiz Capitulino910df892009-10-07 13:41:51 -03002160 .mhandler.info = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002161 },
2162 {
2163 .name = "uuid",
2164 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002165 .params = "",
2166 .help = "show the current VM UUID",
Luiz Capitulino910df892009-10-07 13:41:51 -03002167 .mhandler.info = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002168 },
j_mayer76a66252007-03-07 08:32:30 +00002169#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002170 {
2171 .name = "cpustats",
2172 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002173 .params = "",
2174 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002175 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002176 },
j_mayer76a66252007-03-07 08:32:30 +00002177#endif
blueswir131a60e22007-10-26 18:42:59 +00002178#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002179 {
2180 .name = "usernet",
2181 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002182 .params = "",
2183 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002184 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002185 },
blueswir131a60e22007-10-26 18:42:59 +00002186#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002187 {
2188 .name = "migrate",
2189 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002190 .params = "",
2191 .help = "show migration status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002192 .mhandler.info = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002193 },
2194 {
2195 .name = "balloon",
2196 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002197 .params = "",
2198 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002199 .user_print = monitor_print_balloon,
2200 .mhandler.info_new = do_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002201 },
2202 {
2203 .name = "qtree",
2204 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002205 .params = "",
2206 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002207 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002208 },
2209 {
2210 .name = "qdm",
2211 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002212 .params = "",
2213 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002214 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002215 },
2216 {
2217 .name = "roms",
2218 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002219 .params = "",
2220 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002221 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002222 },
2223 {
2224 .name = NULL,
2225 },
bellard9dc39cb2004-03-14 21:38:27 +00002226};
2227
bellard9307c4c2004-04-04 12:57:25 +00002228/*******************************************************************/
2229
2230static const char *pch;
2231static jmp_buf expr_env;
2232
bellard92a31b12005-02-10 22:00:52 +00002233#define MD_TLONG 0
2234#define MD_I32 1
2235
bellard9307c4c2004-04-04 12:57:25 +00002236typedef struct MonitorDef {
2237 const char *name;
2238 int offset;
blueswir18662d652008-10-02 18:32:44 +00002239 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002240 int type;
bellard9307c4c2004-04-04 12:57:25 +00002241} MonitorDef;
2242
bellard57206fd2004-04-25 18:54:52 +00002243#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002244static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002245{
bellard6a00d602005-11-21 23:25:50 +00002246 CPUState *env = mon_get_cpu();
2247 if (!env)
2248 return 0;
2249 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002250}
2251#endif
2252
bellarda541f292004-04-12 20:39:29 +00002253#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002254static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002255{
bellard6a00d602005-11-21 23:25:50 +00002256 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002257 unsigned int u;
2258 int i;
2259
bellard6a00d602005-11-21 23:25:50 +00002260 if (!env)
2261 return 0;
2262
bellarda541f292004-04-12 20:39:29 +00002263 u = 0;
2264 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002265 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002266
2267 return u;
2268}
2269
blueswir18662d652008-10-02 18:32:44 +00002270static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002271{
bellard6a00d602005-11-21 23:25:50 +00002272 CPUState *env = mon_get_cpu();
2273 if (!env)
2274 return 0;
j_mayer0411a972007-10-25 21:35:50 +00002275 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002276}
2277
blueswir18662d652008-10-02 18:32:44 +00002278static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002279{
bellard6a00d602005-11-21 23:25:50 +00002280 CPUState *env = mon_get_cpu();
2281 if (!env)
2282 return 0;
aurel323d7b4172008-10-21 11:28:46 +00002283 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002284}
bellard9fddaa02004-05-21 12:59:32 +00002285
blueswir18662d652008-10-02 18:32:44 +00002286static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002287{
bellard6a00d602005-11-21 23:25:50 +00002288 CPUState *env = mon_get_cpu();
2289 if (!env)
2290 return 0;
2291 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002292}
2293
blueswir18662d652008-10-02 18:32:44 +00002294static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002295{
bellard6a00d602005-11-21 23:25:50 +00002296 CPUState *env = mon_get_cpu();
2297 if (!env)
2298 return 0;
2299 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002300}
2301
blueswir18662d652008-10-02 18:32:44 +00002302static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002303{
bellard6a00d602005-11-21 23:25:50 +00002304 CPUState *env = mon_get_cpu();
2305 if (!env)
2306 return 0;
2307 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002308}
bellarda541f292004-04-12 20:39:29 +00002309#endif
2310
bellarde95c8d52004-09-30 22:22:08 +00002311#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002312#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002313static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002314{
bellard6a00d602005-11-21 23:25:50 +00002315 CPUState *env = mon_get_cpu();
2316 if (!env)
2317 return 0;
2318 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002319}
bellard7b936c02005-10-30 17:05:13 +00002320#endif
bellarde95c8d52004-09-30 22:22:08 +00002321
blueswir18662d652008-10-02 18:32:44 +00002322static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002323{
bellard6a00d602005-11-21 23:25:50 +00002324 CPUState *env = mon_get_cpu();
2325 if (!env)
2326 return 0;
2327 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002328}
2329#endif
2330
blueswir18662d652008-10-02 18:32:44 +00002331static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002332#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002333
2334#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002335 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002336 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002337 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002338
bellard9307c4c2004-04-04 12:57:25 +00002339 { "eax", offsetof(CPUState, regs[0]) },
2340 { "ecx", offsetof(CPUState, regs[1]) },
2341 { "edx", offsetof(CPUState, regs[2]) },
2342 { "ebx", offsetof(CPUState, regs[3]) },
2343 { "esp|sp", offsetof(CPUState, regs[4]) },
2344 { "ebp|fp", offsetof(CPUState, regs[5]) },
2345 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002346 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002347#ifdef TARGET_X86_64
2348 { "r8", offsetof(CPUState, regs[8]) },
2349 { "r9", offsetof(CPUState, regs[9]) },
2350 { "r10", offsetof(CPUState, regs[10]) },
2351 { "r11", offsetof(CPUState, regs[11]) },
2352 { "r12", offsetof(CPUState, regs[12]) },
2353 { "r13", offsetof(CPUState, regs[13]) },
2354 { "r14", offsetof(CPUState, regs[14]) },
2355 { "r15", offsetof(CPUState, regs[15]) },
2356#endif
bellard9307c4c2004-04-04 12:57:25 +00002357 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002358 { "eip", offsetof(CPUState, eip) },
2359 SEG("cs", R_CS)
2360 SEG("ds", R_DS)
2361 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002362 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002363 SEG("fs", R_FS)
2364 SEG("gs", R_GS)
2365 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002366#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002367 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002368 { "r0", offsetof(CPUState, gpr[0]) },
2369 { "r1", offsetof(CPUState, gpr[1]) },
2370 { "r2", offsetof(CPUState, gpr[2]) },
2371 { "r3", offsetof(CPUState, gpr[3]) },
2372 { "r4", offsetof(CPUState, gpr[4]) },
2373 { "r5", offsetof(CPUState, gpr[5]) },
2374 { "r6", offsetof(CPUState, gpr[6]) },
2375 { "r7", offsetof(CPUState, gpr[7]) },
2376 { "r8", offsetof(CPUState, gpr[8]) },
2377 { "r9", offsetof(CPUState, gpr[9]) },
2378 { "r10", offsetof(CPUState, gpr[10]) },
2379 { "r11", offsetof(CPUState, gpr[11]) },
2380 { "r12", offsetof(CPUState, gpr[12]) },
2381 { "r13", offsetof(CPUState, gpr[13]) },
2382 { "r14", offsetof(CPUState, gpr[14]) },
2383 { "r15", offsetof(CPUState, gpr[15]) },
2384 { "r16", offsetof(CPUState, gpr[16]) },
2385 { "r17", offsetof(CPUState, gpr[17]) },
2386 { "r18", offsetof(CPUState, gpr[18]) },
2387 { "r19", offsetof(CPUState, gpr[19]) },
2388 { "r20", offsetof(CPUState, gpr[20]) },
2389 { "r21", offsetof(CPUState, gpr[21]) },
2390 { "r22", offsetof(CPUState, gpr[22]) },
2391 { "r23", offsetof(CPUState, gpr[23]) },
2392 { "r24", offsetof(CPUState, gpr[24]) },
2393 { "r25", offsetof(CPUState, gpr[25]) },
2394 { "r26", offsetof(CPUState, gpr[26]) },
2395 { "r27", offsetof(CPUState, gpr[27]) },
2396 { "r28", offsetof(CPUState, gpr[28]) },
2397 { "r29", offsetof(CPUState, gpr[29]) },
2398 { "r30", offsetof(CPUState, gpr[30]) },
2399 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002400 /* Floating point registers */
2401 { "f0", offsetof(CPUState, fpr[0]) },
2402 { "f1", offsetof(CPUState, fpr[1]) },
2403 { "f2", offsetof(CPUState, fpr[2]) },
2404 { "f3", offsetof(CPUState, fpr[3]) },
2405 { "f4", offsetof(CPUState, fpr[4]) },
2406 { "f5", offsetof(CPUState, fpr[5]) },
2407 { "f6", offsetof(CPUState, fpr[6]) },
2408 { "f7", offsetof(CPUState, fpr[7]) },
2409 { "f8", offsetof(CPUState, fpr[8]) },
2410 { "f9", offsetof(CPUState, fpr[9]) },
2411 { "f10", offsetof(CPUState, fpr[10]) },
2412 { "f11", offsetof(CPUState, fpr[11]) },
2413 { "f12", offsetof(CPUState, fpr[12]) },
2414 { "f13", offsetof(CPUState, fpr[13]) },
2415 { "f14", offsetof(CPUState, fpr[14]) },
2416 { "f15", offsetof(CPUState, fpr[15]) },
2417 { "f16", offsetof(CPUState, fpr[16]) },
2418 { "f17", offsetof(CPUState, fpr[17]) },
2419 { "f18", offsetof(CPUState, fpr[18]) },
2420 { "f19", offsetof(CPUState, fpr[19]) },
2421 { "f20", offsetof(CPUState, fpr[20]) },
2422 { "f21", offsetof(CPUState, fpr[21]) },
2423 { "f22", offsetof(CPUState, fpr[22]) },
2424 { "f23", offsetof(CPUState, fpr[23]) },
2425 { "f24", offsetof(CPUState, fpr[24]) },
2426 { "f25", offsetof(CPUState, fpr[25]) },
2427 { "f26", offsetof(CPUState, fpr[26]) },
2428 { "f27", offsetof(CPUState, fpr[27]) },
2429 { "f28", offsetof(CPUState, fpr[28]) },
2430 { "f29", offsetof(CPUState, fpr[29]) },
2431 { "f30", offsetof(CPUState, fpr[30]) },
2432 { "f31", offsetof(CPUState, fpr[31]) },
2433 { "fpscr", offsetof(CPUState, fpscr) },
2434 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002435 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002436 { "lr", offsetof(CPUState, lr) },
2437 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002438 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002439 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002440 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002441 { "msr", 0, &monitor_get_msr, },
2442 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002443 { "tbu", 0, &monitor_get_tbu, },
2444 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002445#if defined(TARGET_PPC64)
2446 /* Address space register */
2447 { "asr", offsetof(CPUState, asr) },
2448#endif
2449 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002450 { "sdr1", offsetof(CPUState, sdr1) },
2451 { "sr0", offsetof(CPUState, sr[0]) },
2452 { "sr1", offsetof(CPUState, sr[1]) },
2453 { "sr2", offsetof(CPUState, sr[2]) },
2454 { "sr3", offsetof(CPUState, sr[3]) },
2455 { "sr4", offsetof(CPUState, sr[4]) },
2456 { "sr5", offsetof(CPUState, sr[5]) },
2457 { "sr6", offsetof(CPUState, sr[6]) },
2458 { "sr7", offsetof(CPUState, sr[7]) },
2459 { "sr8", offsetof(CPUState, sr[8]) },
2460 { "sr9", offsetof(CPUState, sr[9]) },
2461 { "sr10", offsetof(CPUState, sr[10]) },
2462 { "sr11", offsetof(CPUState, sr[11]) },
2463 { "sr12", offsetof(CPUState, sr[12]) },
2464 { "sr13", offsetof(CPUState, sr[13]) },
2465 { "sr14", offsetof(CPUState, sr[14]) },
2466 { "sr15", offsetof(CPUState, sr[15]) },
2467 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002468#elif defined(TARGET_SPARC)
2469 { "g0", offsetof(CPUState, gregs[0]) },
2470 { "g1", offsetof(CPUState, gregs[1]) },
2471 { "g2", offsetof(CPUState, gregs[2]) },
2472 { "g3", offsetof(CPUState, gregs[3]) },
2473 { "g4", offsetof(CPUState, gregs[4]) },
2474 { "g5", offsetof(CPUState, gregs[5]) },
2475 { "g6", offsetof(CPUState, gregs[6]) },
2476 { "g7", offsetof(CPUState, gregs[7]) },
2477 { "o0", 0, monitor_get_reg },
2478 { "o1", 1, monitor_get_reg },
2479 { "o2", 2, monitor_get_reg },
2480 { "o3", 3, monitor_get_reg },
2481 { "o4", 4, monitor_get_reg },
2482 { "o5", 5, monitor_get_reg },
2483 { "o6", 6, monitor_get_reg },
2484 { "o7", 7, monitor_get_reg },
2485 { "l0", 8, monitor_get_reg },
2486 { "l1", 9, monitor_get_reg },
2487 { "l2", 10, monitor_get_reg },
2488 { "l3", 11, monitor_get_reg },
2489 { "l4", 12, monitor_get_reg },
2490 { "l5", 13, monitor_get_reg },
2491 { "l6", 14, monitor_get_reg },
2492 { "l7", 15, monitor_get_reg },
2493 { "i0", 16, monitor_get_reg },
2494 { "i1", 17, monitor_get_reg },
2495 { "i2", 18, monitor_get_reg },
2496 { "i3", 19, monitor_get_reg },
2497 { "i4", 20, monitor_get_reg },
2498 { "i5", 21, monitor_get_reg },
2499 { "i6", 22, monitor_get_reg },
2500 { "i7", 23, monitor_get_reg },
2501 { "pc", offsetof(CPUState, pc) },
2502 { "npc", offsetof(CPUState, npc) },
2503 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002504#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002505 { "psr", 0, &monitor_get_psr, },
2506 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002507#endif
bellarde95c8d52004-09-30 22:22:08 +00002508 { "tbr", offsetof(CPUState, tbr) },
2509 { "fsr", offsetof(CPUState, fsr) },
2510 { "f0", offsetof(CPUState, fpr[0]) },
2511 { "f1", offsetof(CPUState, fpr[1]) },
2512 { "f2", offsetof(CPUState, fpr[2]) },
2513 { "f3", offsetof(CPUState, fpr[3]) },
2514 { "f4", offsetof(CPUState, fpr[4]) },
2515 { "f5", offsetof(CPUState, fpr[5]) },
2516 { "f6", offsetof(CPUState, fpr[6]) },
2517 { "f7", offsetof(CPUState, fpr[7]) },
2518 { "f8", offsetof(CPUState, fpr[8]) },
2519 { "f9", offsetof(CPUState, fpr[9]) },
2520 { "f10", offsetof(CPUState, fpr[10]) },
2521 { "f11", offsetof(CPUState, fpr[11]) },
2522 { "f12", offsetof(CPUState, fpr[12]) },
2523 { "f13", offsetof(CPUState, fpr[13]) },
2524 { "f14", offsetof(CPUState, fpr[14]) },
2525 { "f15", offsetof(CPUState, fpr[15]) },
2526 { "f16", offsetof(CPUState, fpr[16]) },
2527 { "f17", offsetof(CPUState, fpr[17]) },
2528 { "f18", offsetof(CPUState, fpr[18]) },
2529 { "f19", offsetof(CPUState, fpr[19]) },
2530 { "f20", offsetof(CPUState, fpr[20]) },
2531 { "f21", offsetof(CPUState, fpr[21]) },
2532 { "f22", offsetof(CPUState, fpr[22]) },
2533 { "f23", offsetof(CPUState, fpr[23]) },
2534 { "f24", offsetof(CPUState, fpr[24]) },
2535 { "f25", offsetof(CPUState, fpr[25]) },
2536 { "f26", offsetof(CPUState, fpr[26]) },
2537 { "f27", offsetof(CPUState, fpr[27]) },
2538 { "f28", offsetof(CPUState, fpr[28]) },
2539 { "f29", offsetof(CPUState, fpr[29]) },
2540 { "f30", offsetof(CPUState, fpr[30]) },
2541 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002542#ifdef TARGET_SPARC64
2543 { "f32", offsetof(CPUState, fpr[32]) },
2544 { "f34", offsetof(CPUState, fpr[34]) },
2545 { "f36", offsetof(CPUState, fpr[36]) },
2546 { "f38", offsetof(CPUState, fpr[38]) },
2547 { "f40", offsetof(CPUState, fpr[40]) },
2548 { "f42", offsetof(CPUState, fpr[42]) },
2549 { "f44", offsetof(CPUState, fpr[44]) },
2550 { "f46", offsetof(CPUState, fpr[46]) },
2551 { "f48", offsetof(CPUState, fpr[48]) },
2552 { "f50", offsetof(CPUState, fpr[50]) },
2553 { "f52", offsetof(CPUState, fpr[52]) },
2554 { "f54", offsetof(CPUState, fpr[54]) },
2555 { "f56", offsetof(CPUState, fpr[56]) },
2556 { "f58", offsetof(CPUState, fpr[58]) },
2557 { "f60", offsetof(CPUState, fpr[60]) },
2558 { "f62", offsetof(CPUState, fpr[62]) },
2559 { "asi", offsetof(CPUState, asi) },
2560 { "pstate", offsetof(CPUState, pstate) },
2561 { "cansave", offsetof(CPUState, cansave) },
2562 { "canrestore", offsetof(CPUState, canrestore) },
2563 { "otherwin", offsetof(CPUState, otherwin) },
2564 { "wstate", offsetof(CPUState, wstate) },
2565 { "cleanwin", offsetof(CPUState, cleanwin) },
2566 { "fprs", offsetof(CPUState, fprs) },
2567#endif
bellard9307c4c2004-04-04 12:57:25 +00002568#endif
2569 { NULL },
2570};
2571
aliguori376253e2009-03-05 23:01:23 +00002572static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002573{
aliguori376253e2009-03-05 23:01:23 +00002574 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002575 longjmp(expr_env, 1);
2576}
2577
bellard6a00d602005-11-21 23:25:50 +00002578/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002579static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002580{
blueswir18662d652008-10-02 18:32:44 +00002581 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002582 void *ptr;
2583
bellard9307c4c2004-04-04 12:57:25 +00002584 for(md = monitor_defs; md->name != NULL; md++) {
2585 if (compare_cmd(name, md->name)) {
2586 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002587 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002588 } else {
bellard6a00d602005-11-21 23:25:50 +00002589 CPUState *env = mon_get_cpu();
2590 if (!env)
2591 return -2;
2592 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002593 switch(md->type) {
2594 case MD_I32:
2595 *pval = *(int32_t *)ptr;
2596 break;
2597 case MD_TLONG:
2598 *pval = *(target_long *)ptr;
2599 break;
2600 default:
2601 *pval = 0;
2602 break;
2603 }
bellard9307c4c2004-04-04 12:57:25 +00002604 }
2605 return 0;
2606 }
2607 }
2608 return -1;
2609}
2610
2611static void next(void)
2612{
Blue Swirl660f11b2009-07-31 21:16:51 +00002613 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002614 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002615 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002616 pch++;
2617 }
2618}
2619
aliguori376253e2009-03-05 23:01:23 +00002620static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002621
aliguori376253e2009-03-05 23:01:23 +00002622static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002623{
blueswir1c2efc952007-09-25 17:28:42 +00002624 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002625 char *p;
bellard6a00d602005-11-21 23:25:50 +00002626 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002627
2628 switch(*pch) {
2629 case '+':
2630 next();
aliguori376253e2009-03-05 23:01:23 +00002631 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002632 break;
2633 case '-':
2634 next();
aliguori376253e2009-03-05 23:01:23 +00002635 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002636 break;
2637 case '~':
2638 next();
aliguori376253e2009-03-05 23:01:23 +00002639 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002640 break;
2641 case '(':
2642 next();
aliguori376253e2009-03-05 23:01:23 +00002643 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002644 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002645 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002646 }
2647 next();
2648 break;
bellard81d09122004-07-14 17:21:37 +00002649 case '\'':
2650 pch++;
2651 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002652 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002653 n = *pch;
2654 pch++;
2655 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002656 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002657 next();
2658 break;
bellard9307c4c2004-04-04 12:57:25 +00002659 case '$':
2660 {
2661 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002662 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002663
bellard9307c4c2004-04-04 12:57:25 +00002664 pch++;
2665 q = buf;
2666 while ((*pch >= 'a' && *pch <= 'z') ||
2667 (*pch >= 'A' && *pch <= 'Z') ||
2668 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002669 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002670 if ((q - buf) < sizeof(buf) - 1)
2671 *q++ = *pch;
2672 pch++;
2673 }
blueswir1cd390082008-11-16 13:53:32 +00002674 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002675 pch++;
2676 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002677 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002678 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002679 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002680 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002681 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002682 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002683 }
2684 break;
2685 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002686 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002687 n = 0;
2688 break;
2689 default:
blueswir17743e582007-09-24 18:39:04 +00002690#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002691 n = strtoull(pch, &p, 0);
2692#else
bellard9307c4c2004-04-04 12:57:25 +00002693 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002694#endif
bellard9307c4c2004-04-04 12:57:25 +00002695 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002696 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002697 }
2698 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002699 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002700 pch++;
2701 break;
2702 }
2703 return n;
2704}
2705
2706
aliguori376253e2009-03-05 23:01:23 +00002707static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002708{
blueswir1c2efc952007-09-25 17:28:42 +00002709 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002710 int op;
ths3b46e622007-09-17 08:09:54 +00002711
aliguori376253e2009-03-05 23:01:23 +00002712 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002713 for(;;) {
2714 op = *pch;
2715 if (op != '*' && op != '/' && op != '%')
2716 break;
2717 next();
aliguori376253e2009-03-05 23:01:23 +00002718 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002719 switch(op) {
2720 default:
2721 case '*':
2722 val *= val2;
2723 break;
2724 case '/':
2725 case '%':
ths5fafdf22007-09-16 21:08:06 +00002726 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002727 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002728 if (op == '/')
2729 val /= val2;
2730 else
2731 val %= val2;
2732 break;
2733 }
2734 }
2735 return val;
2736}
2737
aliguori376253e2009-03-05 23:01:23 +00002738static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002739{
blueswir1c2efc952007-09-25 17:28:42 +00002740 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002741 int op;
bellard9307c4c2004-04-04 12:57:25 +00002742
aliguori376253e2009-03-05 23:01:23 +00002743 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002744 for(;;) {
2745 op = *pch;
2746 if (op != '&' && op != '|' && op != '^')
2747 break;
2748 next();
aliguori376253e2009-03-05 23:01:23 +00002749 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002750 switch(op) {
2751 default:
2752 case '&':
2753 val &= val2;
2754 break;
2755 case '|':
2756 val |= val2;
2757 break;
2758 case '^':
2759 val ^= val2;
2760 break;
2761 }
2762 }
2763 return val;
2764}
2765
aliguori376253e2009-03-05 23:01:23 +00002766static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002767{
blueswir1c2efc952007-09-25 17:28:42 +00002768 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002769 int op;
bellard9307c4c2004-04-04 12:57:25 +00002770
aliguori376253e2009-03-05 23:01:23 +00002771 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002772 for(;;) {
2773 op = *pch;
2774 if (op != '+' && op != '-')
2775 break;
2776 next();
aliguori376253e2009-03-05 23:01:23 +00002777 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002778 if (op == '+')
2779 val += val2;
2780 else
2781 val -= val2;
2782 }
2783 return val;
2784}
2785
aliguori376253e2009-03-05 23:01:23 +00002786static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002787{
2788 pch = *pp;
2789 if (setjmp(expr_env)) {
2790 *pp = pch;
2791 return -1;
2792 }
blueswir1cd390082008-11-16 13:53:32 +00002793 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002794 pch++;
aliguori376253e2009-03-05 23:01:23 +00002795 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002796 *pp = pch;
2797 return 0;
2798}
2799
2800static int get_str(char *buf, int buf_size, const char **pp)
2801{
2802 const char *p;
2803 char *q;
2804 int c;
2805
bellard81d09122004-07-14 17:21:37 +00002806 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002807 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002808 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002809 p++;
2810 if (*p == '\0') {
2811 fail:
bellard81d09122004-07-14 17:21:37 +00002812 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002813 *pp = p;
2814 return -1;
2815 }
bellard9307c4c2004-04-04 12:57:25 +00002816 if (*p == '\"') {
2817 p++;
2818 while (*p != '\0' && *p != '\"') {
2819 if (*p == '\\') {
2820 p++;
2821 c = *p++;
2822 switch(c) {
2823 case 'n':
2824 c = '\n';
2825 break;
2826 case 'r':
2827 c = '\r';
2828 break;
2829 case '\\':
2830 case '\'':
2831 case '\"':
2832 break;
2833 default:
2834 qemu_printf("unsupported escape code: '\\%c'\n", c);
2835 goto fail;
2836 }
2837 if ((q - buf) < buf_size - 1) {
2838 *q++ = c;
2839 }
2840 } else {
2841 if ((q - buf) < buf_size - 1) {
2842 *q++ = *p;
2843 }
2844 p++;
2845 }
2846 }
2847 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002848 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002849 goto fail;
2850 }
2851 p++;
2852 } else {
blueswir1cd390082008-11-16 13:53:32 +00002853 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002854 if ((q - buf) < buf_size - 1) {
2855 *q++ = *p;
2856 }
2857 p++;
2858 }
bellard9307c4c2004-04-04 12:57:25 +00002859 }
bellard81d09122004-07-14 17:21:37 +00002860 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002861 *pp = p;
2862 return 0;
2863}
2864
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002865/*
2866 * Store the command-name in cmdname, and return a pointer to
2867 * the remaining of the command string.
2868 */
2869static const char *get_command_name(const char *cmdline,
2870 char *cmdname, size_t nlen)
2871{
2872 size_t len;
2873 const char *p, *pstart;
2874
2875 p = cmdline;
2876 while (qemu_isspace(*p))
2877 p++;
2878 if (*p == '\0')
2879 return NULL;
2880 pstart = p;
2881 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2882 p++;
2883 len = p - pstart;
2884 if (len > nlen - 1)
2885 len = nlen - 1;
2886 memcpy(cmdname, pstart, len);
2887 cmdname[len] = '\0';
2888 return p;
2889}
2890
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002891/**
2892 * Read key of 'type' into 'key' and return the current
2893 * 'type' pointer.
2894 */
2895static char *key_get_info(const char *type, char **key)
2896{
2897 size_t len;
2898 char *p, *str;
2899
2900 if (*type == ',')
2901 type++;
2902
2903 p = strchr(type, ':');
2904 if (!p) {
2905 *key = NULL;
2906 return NULL;
2907 }
2908 len = p - type;
2909
2910 str = qemu_malloc(len + 1);
2911 memcpy(str, type, len);
2912 str[len] = '\0';
2913
2914 *key = str;
2915 return ++p;
2916}
2917
bellard9307c4c2004-04-04 12:57:25 +00002918static int default_fmt_format = 'x';
2919static int default_fmt_size = 4;
2920
2921#define MAX_ARGS 16
2922
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002923static int is_valid_option(const char *c, const char *typestr)
2924{
2925 char option[3];
2926
2927 option[0] = '-';
2928 option[1] = *c;
2929 option[2] = '\0';
2930
2931 typestr = strstr(typestr, option);
2932 return (typestr != NULL);
2933}
2934
Anthony Liguoric227f092009-10-01 16:12:16 -05002935static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002936 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002937 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00002938{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002939 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03002940 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05002941 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002942 char cmdname[256];
2943 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002944 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00002945
2946#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002947 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002948#endif
ths3b46e622007-09-17 08:09:54 +00002949
bellard9307c4c2004-04-04 12:57:25 +00002950 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002951 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2952 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002953 return NULL;
ths3b46e622007-09-17 08:09:54 +00002954
bellard9307c4c2004-04-04 12:57:25 +00002955 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002956 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002957 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002958 break;
bellard9dc39cb2004-03-14 21:38:27 +00002959 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002960
2961 if (cmd->name == NULL) {
2962 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002963 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002964 }
bellard9307c4c2004-04-04 12:57:25 +00002965
bellard9307c4c2004-04-04 12:57:25 +00002966 /* parse the parameters */
2967 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002968 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002969 typestr = key_get_info(typestr, &key);
2970 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002971 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002972 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002973 typestr++;
2974 switch(c) {
2975 case 'F':
bellard81d09122004-07-14 17:21:37 +00002976 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002977 case 's':
2978 {
2979 int ret;
ths3b46e622007-09-17 08:09:54 +00002980
blueswir1cd390082008-11-16 13:53:32 +00002981 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002982 p++;
2983 if (*typestr == '?') {
2984 typestr++;
2985 if (*p == '\0') {
2986 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002987 break;
bellard9307c4c2004-04-04 12:57:25 +00002988 }
2989 }
2990 ret = get_str(buf, sizeof(buf), &p);
2991 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002992 switch(c) {
2993 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002994 monitor_printf(mon, "%s: filename expected\n",
2995 cmdname);
bellard81d09122004-07-14 17:21:37 +00002996 break;
2997 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002998 monitor_printf(mon, "%s: block device name expected\n",
2999 cmdname);
bellard81d09122004-07-14 17:21:37 +00003000 break;
3001 default:
aliguori376253e2009-03-05 23:01:23 +00003002 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003003 break;
3004 }
bellard9307c4c2004-04-04 12:57:25 +00003005 goto fail;
3006 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003007 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003008 }
3009 break;
3010 case '/':
3011 {
3012 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003013
blueswir1cd390082008-11-16 13:53:32 +00003014 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003015 p++;
3016 if (*p == '/') {
3017 /* format found */
3018 p++;
3019 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003020 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003021 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003022 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003023 count = count * 10 + (*p - '0');
3024 p++;
3025 }
3026 }
3027 size = -1;
3028 format = -1;
3029 for(;;) {
3030 switch(*p) {
3031 case 'o':
3032 case 'd':
3033 case 'u':
3034 case 'x':
3035 case 'i':
3036 case 'c':
3037 format = *p++;
3038 break;
3039 case 'b':
3040 size = 1;
3041 p++;
3042 break;
3043 case 'h':
3044 size = 2;
3045 p++;
3046 break;
3047 case 'w':
3048 size = 4;
3049 p++;
3050 break;
3051 case 'g':
3052 case 'L':
3053 size = 8;
3054 p++;
3055 break;
3056 default:
3057 goto next;
3058 }
3059 }
3060 next:
blueswir1cd390082008-11-16 13:53:32 +00003061 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003062 monitor_printf(mon, "invalid char in format: '%c'\n",
3063 *p);
bellard9307c4c2004-04-04 12:57:25 +00003064 goto fail;
3065 }
bellard9307c4c2004-04-04 12:57:25 +00003066 if (format < 0)
3067 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003068 if (format != 'i') {
3069 /* for 'i', not specifying a size gives -1 as size */
3070 if (size < 0)
3071 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003072 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003073 }
bellard9307c4c2004-04-04 12:57:25 +00003074 default_fmt_format = format;
3075 } else {
3076 count = 1;
3077 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003078 if (format != 'i') {
3079 size = default_fmt_size;
3080 } else {
3081 size = -1;
3082 }
bellard9307c4c2004-04-04 12:57:25 +00003083 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003084 qdict_put(qdict, "count", qint_from_int(count));
3085 qdict_put(qdict, "format", qint_from_int(format));
3086 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003087 }
3088 break;
3089 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003090 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00003091 {
blueswir1c2efc952007-09-25 17:28:42 +00003092 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003093
blueswir1cd390082008-11-16 13:53:32 +00003094 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003095 p++;
bellard34405572004-06-08 00:55:58 +00003096 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003097 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003098 if (*p == '\0') {
3099 typestr++;
3100 break;
3101 }
bellard34405572004-06-08 00:55:58 +00003102 } else {
3103 if (*p == '.') {
3104 p++;
blueswir1cd390082008-11-16 13:53:32 +00003105 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003106 p++;
bellard34405572004-06-08 00:55:58 +00003107 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003108 typestr++;
3109 break;
bellard34405572004-06-08 00:55:58 +00003110 }
3111 }
bellard13224a82006-07-14 22:03:35 +00003112 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003113 }
aliguori376253e2009-03-05 23:01:23 +00003114 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003115 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003116 /* Check if 'i' is greater than 32-bit */
3117 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3118 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3119 monitor_printf(mon, "integer is for 32-bit values\n");
3120 goto fail;
3121 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003122 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003123 }
3124 break;
3125 case '-':
3126 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003127 const char *tmp = p;
3128 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003129 /* option */
ths3b46e622007-09-17 08:09:54 +00003130
bellard9307c4c2004-04-04 12:57:25 +00003131 c = *typestr++;
3132 if (c == '\0')
3133 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003134 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003135 p++;
3136 has_option = 0;
3137 if (*p == '-') {
3138 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003139 if(c != *p) {
3140 if(!is_valid_option(p, typestr)) {
3141
3142 monitor_printf(mon, "%s: unsupported option -%c\n",
3143 cmdname, *p);
3144 goto fail;
3145 } else {
3146 skip_key = 1;
3147 }
bellard9307c4c2004-04-04 12:57:25 +00003148 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003149 if(skip_key) {
3150 p = tmp;
3151 } else {
3152 p++;
3153 has_option = 1;
3154 }
bellard9307c4c2004-04-04 12:57:25 +00003155 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003156 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003157 }
3158 break;
3159 default:
3160 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003161 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003162 goto fail;
3163 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003164 qemu_free(key);
3165 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003166 }
3167 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003168 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003169 p++;
3170 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003171 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3172 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003173 goto fail;
3174 }
3175
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003176 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003177
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003178fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003179 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003180 return NULL;
3181}
3182
Luiz Capitulino8204a912009-11-18 23:05:31 -02003183static void monitor_print_error(Monitor *mon)
3184{
3185 qerror_print(mon->error);
3186 QDECREF(mon->error);
3187 mon->error = NULL;
3188}
3189
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003190static void monitor_handle_command(Monitor *mon, const char *cmdline)
3191{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003192 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003193 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003194
3195 qdict = qdict_new();
3196
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003197 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003198 if (!cmd)
3199 goto out;
3200
3201 qemu_errors_to_mon(mon);
3202
3203 if (monitor_handler_ported(cmd)) {
3204 QObject *data = NULL;
3205
3206 cmd->mhandler.cmd_new(mon, qdict, &data);
3207 if (data)
3208 cmd->user_print(mon, data);
3209
3210 qobject_decref(data);
3211 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003212 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003213 }
3214
Luiz Capitulino8204a912009-11-18 23:05:31 -02003215 if (monitor_has_error(mon))
3216 monitor_print_error(mon);
3217
3218 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003219
3220out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003221 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003222}
3223
bellard81d09122004-07-14 17:21:37 +00003224static void cmd_completion(const char *name, const char *list)
3225{
3226 const char *p, *pstart;
3227 char cmd[128];
3228 int len;
3229
3230 p = list;
3231 for(;;) {
3232 pstart = p;
3233 p = strchr(p, '|');
3234 if (!p)
3235 p = pstart + strlen(pstart);
3236 len = p - pstart;
3237 if (len > sizeof(cmd) - 2)
3238 len = sizeof(cmd) - 2;
3239 memcpy(cmd, pstart, len);
3240 cmd[len] = '\0';
3241 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003242 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003243 }
3244 if (*p == '\0')
3245 break;
3246 p++;
3247 }
3248}
3249
3250static void file_completion(const char *input)
3251{
3252 DIR *ffs;
3253 struct dirent *d;
3254 char path[1024];
3255 char file[1024], file_prefix[1024];
3256 int input_path_len;
3257 const char *p;
3258
ths5fafdf22007-09-16 21:08:06 +00003259 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003260 if (!p) {
3261 input_path_len = 0;
3262 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003263 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003264 } else {
3265 input_path_len = p - input + 1;
3266 memcpy(path, input, input_path_len);
3267 if (input_path_len > sizeof(path) - 1)
3268 input_path_len = sizeof(path) - 1;
3269 path[input_path_len] = '\0';
3270 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3271 }
3272#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003273 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3274 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003275#endif
3276 ffs = opendir(path);
3277 if (!ffs)
3278 return;
3279 for(;;) {
3280 struct stat sb;
3281 d = readdir(ffs);
3282 if (!d)
3283 break;
3284 if (strstart(d->d_name, file_prefix, NULL)) {
3285 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003286 if (input_path_len < sizeof(file))
3287 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3288 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003289 /* stat the file to find out if it's a directory.
3290 * In that case add a slash to speed up typing long paths
3291 */
3292 stat(file, &sb);
3293 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003294 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003295 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003296 }
3297 }
3298 closedir(ffs);
3299}
3300
aliguori51de9762009-03-05 23:00:43 +00003301static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003302{
aliguori51de9762009-03-05 23:00:43 +00003303 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003304 const char *input = opaque;
3305
3306 if (input[0] == '\0' ||
3307 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003308 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003309 }
3310}
3311
3312/* NOTE: this parser is an approximate form of the real command parser */
3313static void parse_cmdline(const char *cmdline,
3314 int *pnb_args, char **args)
3315{
3316 const char *p;
3317 int nb_args, ret;
3318 char buf[1024];
3319
3320 p = cmdline;
3321 nb_args = 0;
3322 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003323 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003324 p++;
3325 if (*p == '\0')
3326 break;
3327 if (nb_args >= MAX_ARGS)
3328 break;
3329 ret = get_str(buf, sizeof(buf), &p);
3330 args[nb_args] = qemu_strdup(buf);
3331 nb_args++;
3332 if (ret < 0)
3333 break;
3334 }
3335 *pnb_args = nb_args;
3336}
3337
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003338static const char *next_arg_type(const char *typestr)
3339{
3340 const char *p = strchr(typestr, ':');
3341 return (p != NULL ? ++p : typestr);
3342}
3343
aliguori4c36ba32009-03-05 23:01:37 +00003344static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003345{
3346 const char *cmdname;
3347 char *args[MAX_ARGS];
3348 int nb_args, i, len;
3349 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003350 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003351 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003352
3353 parse_cmdline(cmdline, &nb_args, args);
3354#ifdef DEBUG_COMPLETION
3355 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003356 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003357 }
3358#endif
3359
3360 /* if the line ends with a space, it means we want to complete the
3361 next arg */
3362 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003363 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003364 if (nb_args >= MAX_ARGS)
3365 return;
3366 args[nb_args++] = qemu_strdup("");
3367 }
3368 if (nb_args <= 1) {
3369 /* command completion */
3370 if (nb_args == 0)
3371 cmdname = "";
3372 else
3373 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003374 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003375 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003376 cmd_completion(cmdname, cmd->name);
3377 }
3378 } else {
3379 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003380 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003381 if (compare_cmd(args[0], cmd->name))
3382 goto found;
3383 }
3384 return;
3385 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003386 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003387 for(i = 0; i < nb_args - 2; i++) {
3388 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003389 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003390 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003391 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003392 }
3393 }
3394 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003395 if (*ptype == '-' && ptype[1] != '\0') {
3396 ptype += 2;
3397 }
bellard81d09122004-07-14 17:21:37 +00003398 switch(*ptype) {
3399 case 'F':
3400 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003401 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003402 file_completion(str);
3403 break;
3404 case 'B':
3405 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003406 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003407 bdrv_iterate(block_completion_it, (void *)str);
3408 break;
bellard7fe48482004-10-09 18:08:01 +00003409 case 's':
3410 /* XXX: more generic ? */
3411 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003412 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003413 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3414 cmd_completion(str, cmd->name);
3415 }
bellard64866c32006-05-07 18:03:31 +00003416 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003417 char *sep = strrchr(str, '-');
3418 if (sep)
3419 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003420 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003421 for(key = key_defs; key->name != NULL; key++) {
3422 cmd_completion(str, key->name);
3423 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003424 } else if (!strcmp(cmd->name, "help|?")) {
3425 readline_set_completion_index(cur_mon->rs, strlen(str));
3426 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3427 cmd_completion(str, cmd->name);
3428 }
bellard7fe48482004-10-09 18:08:01 +00003429 }
3430 break;
bellard81d09122004-07-14 17:21:37 +00003431 default:
3432 break;
3433 }
3434 }
3435 for(i = 0; i < nb_args; i++)
3436 qemu_free(args[i]);
3437}
3438
aliguori731b0362009-03-05 23:01:42 +00003439static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003440{
aliguori731b0362009-03-05 23:01:42 +00003441 Monitor *mon = opaque;
3442
3443 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003444}
3445
aliguori731b0362009-03-05 23:01:42 +00003446static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003447{
aliguori731b0362009-03-05 23:01:42 +00003448 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003449 int i;
aliguori376253e2009-03-05 23:01:23 +00003450
aliguori731b0362009-03-05 23:01:42 +00003451 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003452
aliguoricde76ee2009-03-05 23:01:51 +00003453 if (cur_mon->rs) {
3454 for (i = 0; i < size; i++)
3455 readline_handle_byte(cur_mon->rs, buf[i]);
3456 } else {
3457 if (size == 0 || buf[size - 1] != 0)
3458 monitor_printf(cur_mon, "corrupted command\n");
3459 else
3460 monitor_handle_command(cur_mon, (char *)buf);
3461 }
aliguori731b0362009-03-05 23:01:42 +00003462
3463 cur_mon = old_mon;
3464}
aliguorid8f44602008-10-06 13:52:44 +00003465
aliguori376253e2009-03-05 23:01:23 +00003466static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003467{
aliguori731b0362009-03-05 23:01:42 +00003468 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003469 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003470 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003471}
3472
aliguoricde76ee2009-03-05 23:01:51 +00003473int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003474{
aliguoricde76ee2009-03-05 23:01:51 +00003475 if (!mon->rs)
3476 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003477 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003478 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003479}
3480
aliguori376253e2009-03-05 23:01:23 +00003481void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003482{
aliguoricde76ee2009-03-05 23:01:51 +00003483 if (!mon->rs)
3484 return;
aliguori731b0362009-03-05 23:01:42 +00003485 if (--mon->suspend_cnt == 0)
3486 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003487}
3488
aliguori731b0362009-03-05 23:01:42 +00003489static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003490{
aliguori376253e2009-03-05 23:01:23 +00003491 Monitor *mon = opaque;
3492
aliguori2724b182009-03-05 23:01:47 +00003493 switch (event) {
3494 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003495 mon->mux_out = 0;
3496 if (mon->reset_seen) {
3497 readline_restart(mon->rs);
3498 monitor_resume(mon);
3499 monitor_flush(mon);
3500 } else {
3501 mon->suspend_cnt = 0;
3502 }
aliguori2724b182009-03-05 23:01:47 +00003503 break;
ths86e94de2007-01-05 22:01:59 +00003504
aliguori2724b182009-03-05 23:01:47 +00003505 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003506 if (mon->reset_seen) {
3507 if (mon->suspend_cnt == 0) {
3508 monitor_printf(mon, "\n");
3509 }
3510 monitor_flush(mon);
3511 monitor_suspend(mon);
3512 } else {
3513 mon->suspend_cnt++;
3514 }
3515 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00003516 break;
3517
Amit Shahb6b8df52009-10-07 18:31:16 +05303518 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003519 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3520 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003521 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00003522 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003523 }
3524 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00003525 break;
3526 }
ths86e94de2007-01-05 22:01:59 +00003527}
3528
aliguori76655d62009-03-06 20:27:37 +00003529
3530/*
3531 * Local variables:
3532 * c-indent-level: 4
3533 * c-basic-offset: 4
3534 * tab-width: 8
3535 * End:
3536 */
3537
aliguori731b0362009-03-05 23:01:42 +00003538void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003539{
aliguori731b0362009-03-05 23:01:42 +00003540 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003541 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003542
3543 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003544 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003545 is_first_init = 0;
3546 }
aliguori87127162009-03-05 23:01:29 +00003547
3548 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003549
aliguori87127162009-03-05 23:01:29 +00003550 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003551 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00003552 if (flags & MONITOR_USE_READLINE) {
3553 mon->rs = readline_init(mon, monitor_find_completion);
3554 monitor_read_command(mon, 0);
3555 }
aliguori87127162009-03-05 23:01:29 +00003556
aliguori731b0362009-03-05 23:01:42 +00003557 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3558 mon);
aliguori87127162009-03-05 23:01:29 +00003559
Blue Swirl72cf2d42009-09-12 07:36:22 +00003560 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003561 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003562 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003563}
3564
aliguori376253e2009-03-05 23:01:23 +00003565static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003566{
aliguoribb5fc202009-03-05 23:01:15 +00003567 BlockDriverState *bs = opaque;
3568 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003569
aliguoribb5fc202009-03-05 23:01:15 +00003570 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003571 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003572 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003573 }
aliguori731b0362009-03-05 23:01:42 +00003574 if (mon->password_completion_cb)
3575 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003576
aliguori731b0362009-03-05 23:01:42 +00003577 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003578}
aliguoric0f4ce72009-03-05 23:01:01 +00003579
aliguori376253e2009-03-05 23:01:23 +00003580void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003581 BlockDriverCompletionFunc *completion_cb,
3582 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003583{
aliguoricde76ee2009-03-05 23:01:51 +00003584 int err;
3585
aliguoribb5fc202009-03-05 23:01:15 +00003586 if (!bdrv_key_required(bs)) {
3587 if (completion_cb)
3588 completion_cb(opaque, 0);
3589 return;
3590 }
aliguoric0f4ce72009-03-05 23:01:01 +00003591
aliguori376253e2009-03-05 23:01:23 +00003592 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3593 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003594
aliguori731b0362009-03-05 23:01:42 +00003595 mon->password_completion_cb = completion_cb;
3596 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003597
aliguoricde76ee2009-03-05 23:01:51 +00003598 err = monitor_read_password(mon, bdrv_password_cb, bs);
3599
3600 if (err && completion_cb)
3601 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003602}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003603
3604typedef struct QemuErrorSink QemuErrorSink;
3605struct QemuErrorSink {
3606 enum {
3607 ERR_SINK_FILE,
3608 ERR_SINK_MONITOR,
3609 } dest;
3610 union {
3611 FILE *fp;
3612 Monitor *mon;
3613 };
3614 QemuErrorSink *previous;
3615};
3616
Blue Swirl528e93a2009-08-31 15:14:40 +00003617static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003618
3619void qemu_errors_to_file(FILE *fp)
3620{
3621 QemuErrorSink *sink;
3622
3623 sink = qemu_mallocz(sizeof(*sink));
3624 sink->dest = ERR_SINK_FILE;
3625 sink->fp = fp;
3626 sink->previous = qemu_error_sink;
3627 qemu_error_sink = sink;
3628}
3629
3630void qemu_errors_to_mon(Monitor *mon)
3631{
3632 QemuErrorSink *sink;
3633
3634 sink = qemu_mallocz(sizeof(*sink));
3635 sink->dest = ERR_SINK_MONITOR;
3636 sink->mon = mon;
3637 sink->previous = qemu_error_sink;
3638 qemu_error_sink = sink;
3639}
3640
3641void qemu_errors_to_previous(void)
3642{
3643 QemuErrorSink *sink;
3644
3645 assert(qemu_error_sink != NULL);
3646 sink = qemu_error_sink;
3647 qemu_error_sink = sink->previous;
3648 qemu_free(sink);
3649}
3650
3651void qemu_error(const char *fmt, ...)
3652{
3653 va_list args;
3654
3655 assert(qemu_error_sink != NULL);
3656 switch (qemu_error_sink->dest) {
3657 case ERR_SINK_FILE:
3658 va_start(args, fmt);
3659 vfprintf(qemu_error_sink->fp, fmt, args);
3660 va_end(args);
3661 break;
3662 case ERR_SINK_MONITOR:
3663 va_start(args, fmt);
3664 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3665 va_end(args);
3666 break;
3667 }
3668}
Luiz Capitulino8204a912009-11-18 23:05:31 -02003669
3670void qemu_error_internal(const char *file, int linenr, const char *func,
3671 const char *fmt, ...)
3672{
3673 va_list va;
3674 QError *qerror;
3675
3676 assert(qemu_error_sink != NULL);
3677
3678 va_start(va, fmt);
3679 qerror = qerror_from_info(file, linenr, func, fmt, &va);
3680 va_end(va);
3681
3682 switch (qemu_error_sink->dest) {
3683 case ERR_SINK_FILE:
3684 qerror_print(qerror);
3685 QDECREF(qerror);
3686 break;
3687 case ERR_SINK_MONITOR:
3688 assert(qemu_error_sink->mon->error == NULL);
3689 qemu_error_sink->mon->error = qerror;
3690 break;
3691 }
3692}