blob: 549e98b47a99a6f77db9111ec9bcd49f1c29de47 [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
aliguori731b0362009-03-05 23:01:42 +0000122static void monitor_read_command(Monitor *mon, int show_prompt)
123{
124 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
125 if (show_prompt)
126 readline_show_prompt(mon->rs);
127}
bellard6a00d602005-11-21 23:25:50 +0000128
aliguoricde76ee2009-03-05 23:01:51 +0000129static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
130 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000131{
aliguoricde76ee2009-03-05 23:01:51 +0000132 if (mon->rs) {
133 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
134 /* prompt is printed on return from the command handler */
135 return 0;
136 } else {
137 monitor_printf(mon, "terminal does not support password prompting\n");
138 return -ENOTTY;
139 }
aliguoribb5fc202009-03-05 23:01:15 +0000140}
141
aliguori376253e2009-03-05 23:01:23 +0000142void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000143{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200144 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000145 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
146 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000147 }
148}
149
150/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000151static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000152{
ths60fe76f2007-12-16 03:02:09 +0000153 char c;
aliguori731b0362009-03-05 23:01:42 +0000154
155 if (!mon)
156 return;
157
bellard7e2515e2004-08-01 21:52:19 +0000158 for(;;) {
159 c = *str++;
160 if (c == '\0')
161 break;
bellard7ba12602006-07-14 20:26:42 +0000162 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000163 mon->outbuf[mon->outbuf_index++] = '\r';
164 mon->outbuf[mon->outbuf_index++] = c;
165 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
166 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000167 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000168 }
169}
170
aliguori376253e2009-03-05 23:01:23 +0000171void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000172{
173 char buf[4096];
174 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000175 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000176}
177
aliguori376253e2009-03-05 23:01:23 +0000178void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000179{
180 va_list ap;
181 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000182 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000183 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000184}
185
aliguori376253e2009-03-05 23:01:23 +0000186void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000187{
188 int i;
189
190 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000191 switch (filename[i]) {
192 case ' ':
193 case '"':
194 case '\\':
195 monitor_printf(mon, "\\%c", filename[i]);
196 break;
197 case '\t':
198 monitor_printf(mon, "\\t");
199 break;
200 case '\r':
201 monitor_printf(mon, "\\r");
202 break;
203 case '\n':
204 monitor_printf(mon, "\\n");
205 break;
206 default:
207 monitor_printf(mon, "%c", filename[i]);
208 break;
209 }
thsfef30742006-12-22 14:11:32 +0000210 }
211}
212
bellard7fe48482004-10-09 18:08:01 +0000213static int monitor_fprintf(FILE *stream, const char *fmt, ...)
214{
215 va_list ap;
216 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000217 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000218 va_end(ap);
219 return 0;
220}
221
Luiz Capitulino13c74252009-10-07 13:41:55 -0300222static void monitor_user_noop(Monitor *mon, const QObject *data) { }
223
Luiz Capitulino13917be2009-10-07 13:41:54 -0300224static inline int monitor_handler_ported(const mon_cmd_t *cmd)
225{
226 return cmd->user_print != NULL;
227}
228
Luiz Capitulino8204a912009-11-18 23:05:31 -0200229static inline int monitor_has_error(const Monitor *mon)
230{
231 return mon->error != NULL;
232}
233
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300234static void monitor_print_qobject(Monitor *mon, const QObject *data)
235{
236 switch (qobject_type(data)) {
237 case QTYPE_QSTRING:
238 monitor_printf(mon, "%s",qstring_get_str(qobject_to_qstring(data)));
239 break;
240 case QTYPE_QINT:
241 monitor_printf(mon, "%" PRId64,qint_get_int(qobject_to_qint(data)));
242 break;
243 default:
244 monitor_printf(mon, "ERROR: unsupported type: %d",
245 qobject_type(data));
246 break;
247 }
248
249 monitor_puts(mon, "\n");
250}
251
bellard9dc39cb2004-03-14 21:38:27 +0000252static int compare_cmd(const char *name, const char *list)
253{
254 const char *p, *pstart;
255 int len;
256 len = strlen(name);
257 p = list;
258 for(;;) {
259 pstart = p;
260 p = strchr(p, '|');
261 if (!p)
262 p = pstart + strlen(pstart);
263 if ((p - pstart) == len && !memcmp(pstart, name, len))
264 return 1;
265 if (*p == '\0')
266 break;
267 p++;
268 }
269 return 0;
270}
271
Anthony Liguoric227f092009-10-01 16:12:16 -0500272static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000273 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000274{
Anthony Liguoric227f092009-10-01 16:12:16 -0500275 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000276
277 for(cmd = cmds; cmd->name != NULL; cmd++) {
278 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000279 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
280 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000281 }
282}
283
aliguori376253e2009-03-05 23:01:23 +0000284static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000285{
286 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000287 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000288 } else {
aliguori376253e2009-03-05 23:01:23 +0000289 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000290 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000291 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000292 monitor_printf(mon, "Log items (comma separated):\n");
293 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000294 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000295 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000296 }
297 }
bellard9dc39cb2004-03-14 21:38:27 +0000298 }
299}
300
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300301static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300302{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300303 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300304}
305
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300306static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000307{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200308 int all_devices;
309 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300310 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000311
bellard7954c732006-08-01 15:52:40 +0000312 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000313 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200314 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300315 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200316 continue;
317 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000318 }
319}
320
Luiz Capitulino13c74252009-10-07 13:41:55 -0300321static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000322{
Anthony Liguoric227f092009-10-01 16:12:16 -0500323 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300324 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000325
bellard9307c4c2004-04-04 12:57:25 +0000326 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000327 goto help;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300328
329 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000330 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300331 break;
bellard9dc39cb2004-03-14 21:38:27 +0000332 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300333
334 if (cmd->name == NULL)
335 goto help;
336
337 if (monitor_handler_ported(cmd)) {
338 cmd->mhandler.info_new(mon, ret_data);
339 if (*ret_data)
340 cmd->user_print(mon, *ret_data);
341 } else {
342 cmd->mhandler.info(mon);
343 }
344
bellard9dc39cb2004-03-14 21:38:27 +0000345 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300346
347help:
348 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000349}
350
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300351/**
352 * do_info_version(): Show QEMU version
353 */
354static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000355{
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300356 *ret_data = QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION));
bellard9bc9d1c2004-10-10 15:15:51 +0000357}
358
aliguori376253e2009-03-05 23:01:23 +0000359static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000360{
361 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000362 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000363}
364
aurel32bf4f74c2008-12-18 22:42:34 +0000365#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000366static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000367{
aliguori376253e2009-03-05 23:01:23 +0000368 monitor_printf(mon, "HPET is %s by QEMU\n",
369 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000370}
aurel32bf4f74c2008-12-18 22:42:34 +0000371#endif
aliguori16b29ae2008-12-17 23:28:44 +0000372
aliguori376253e2009-03-05 23:01:23 +0000373static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000374{
aliguori376253e2009-03-05 23:01:23 +0000375 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
376 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
377 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
378 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
379 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000380}
381
bellard6a00d602005-11-21 23:25:50 +0000382/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000383static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000384{
385 CPUState *env;
386
387 for(env = first_cpu; env != NULL; env = env->next_cpu) {
388 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000389 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000390 return 0;
391 }
392 }
393 return -1;
394}
395
pbrook9596ebb2007-11-18 01:44:38 +0000396static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000397{
aliguori731b0362009-03-05 23:01:42 +0000398 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000399 mon_set_cpu(0);
400 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300401 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000402 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000403}
404
aliguori376253e2009-03-05 23:01:23 +0000405static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000406{
bellard6a00d602005-11-21 23:25:50 +0000407 CPUState *env;
408 env = mon_get_cpu();
409 if (!env)
410 return;
bellard9307c4c2004-04-04 12:57:25 +0000411#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000412 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000413 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000414#else
aliguori376253e2009-03-05 23:01:23 +0000415 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000416 0);
bellard9307c4c2004-04-04 12:57:25 +0000417#endif
418}
419
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300420static void print_cpu_iter(QObject *obj, void *opaque)
421{
422 QDict *cpu;
423 int active = ' ';
424 Monitor *mon = opaque;
425
426 assert(qobject_type(obj) == QTYPE_QDICT);
427 cpu = qobject_to_qdict(obj);
428
429 if (strcmp(qdict_get_str(cpu, "current"), "yes") == 0)
430 active = '*';
431
432 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
433
434#if defined(TARGET_I386)
435 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
436 (target_ulong) qdict_get_int(cpu, "pc"));
437#elif defined(TARGET_PPC)
438 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
439 (target_long) qdict_get_int(cpu, "nip"));
440#elif defined(TARGET_SPARC)
441 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
442 (target_long) qdict_get_int(cpu, "pc"));
443 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
444 (target_long) qdict_get_int(cpu, "npc"));
445#elif defined(TARGET_MIPS)
446 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
447 (target_long) qdict_get_int(cpu, "PC"));
448#endif
449
450 if (strcmp(qdict_get_str(cpu, "halted"), "yes") == 0)
451 monitor_printf(mon, " (halted)");
452
453 monitor_printf(mon, "\n");
454}
455
456static void monitor_print_cpus(Monitor *mon, const QObject *data)
457{
458 QList *cpu_list;
459
460 assert(qobject_type(data) == QTYPE_QLIST);
461 cpu_list = qobject_to_qlist(data);
462 qlist_iter(cpu_list, print_cpu_iter, mon);
463}
464
465/**
466 * do_info_cpus(): Show CPU information
467 *
468 * Return a QList with a QDict for each CPU.
469 *
470 * For example:
471 *
472 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
473 * { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]
474 */
475static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000476{
477 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300478 QList *cpu_list;
479
480 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000481
482 /* just to set the default cpu if not already done */
483 mon_get_cpu();
484
485 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300486 const char *answer;
487 QDict *cpu = qdict_new();
488
Avi Kivity4c0960c2009-08-17 23:19:53 +0300489 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300490
491 qdict_put(cpu, "CPU", qint_from_int(env->cpu_index));
492 answer = (env == mon->mon_cpu) ? "yes" : "no";
493 qdict_put(cpu, "current", qstring_from_str(answer));
494
bellard6a00d602005-11-21 23:25:50 +0000495#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300496 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000497#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300498 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000499#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300500 qdict_put(cpu, "pc", qint_from_int(env->pc));
501 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000502#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300503 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000504#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300505 answer = env->halted ? "yes" : "no";
506 qdict_put(cpu, "halted", qstring_from_str(answer));
507
508 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000509 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300510
511 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000512}
513
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300514static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000515{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300516 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000517 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000518 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000519}
520
aliguori376253e2009-03-05 23:01:23 +0000521static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000522{
aliguori376253e2009-03-05 23:01:23 +0000523 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000524}
525
aliguori376253e2009-03-05 23:01:23 +0000526static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000527{
528 int i;
bellard7e2515e2004-08-01 21:52:19 +0000529 const char *str;
ths3b46e622007-09-17 08:09:54 +0000530
aliguoricde76ee2009-03-05 23:01:51 +0000531 if (!mon->rs)
532 return;
bellard7e2515e2004-08-01 21:52:19 +0000533 i = 0;
534 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000535 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000536 if (!str)
537 break;
aliguori376253e2009-03-05 23:01:23 +0000538 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000539 i++;
bellardaa455482004-04-04 13:07:25 +0000540 }
541}
542
j_mayer76a66252007-03-07 08:32:30 +0000543#if defined(TARGET_PPC)
544/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000545static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000546{
547 CPUState *env;
548
549 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000550 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000551}
552#endif
553
Luiz Capitulinob223f352009-10-07 13:41:56 -0300554/**
555 * do_quit(): Quit QEMU execution
556 */
557static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000558{
559 exit(0);
560}
561
aliguori376253e2009-03-05 23:01:23 +0000562static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000563{
564 if (bdrv_is_inserted(bs)) {
565 if (!force) {
566 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000567 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000568 return -1;
569 }
570 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000571 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000572 return -1;
573 }
574 }
575 bdrv_close(bs);
576 }
577 return 0;
578}
579
Luiz Capitulinoe1c923a2009-10-16 12:23:49 -0300580static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000581{
582 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300583 int force = qdict_get_int(qdict, "force");
584 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000585
bellard9307c4c2004-04-04 12:57:25 +0000586 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000587 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000588 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000589 return;
590 }
aliguori376253e2009-03-05 23:01:23 +0000591 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000592}
593
aliguori376253e2009-03-05 23:01:23 +0000594static void do_change_block(Monitor *mon, const char *device,
595 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000596{
597 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000598 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000599
bellard9307c4c2004-04-04 12:57:25 +0000600 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000601 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000602 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000603 return;
604 }
aurel322ecea9b2008-06-18 22:10:01 +0000605 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100606 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000607 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000608 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000609 return;
610 }
611 }
aliguori376253e2009-03-05 23:01:23 +0000612 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000613 return;
aurel322ecea9b2008-06-18 22:10:01 +0000614 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000615 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000616}
617
aliguori376253e2009-03-05 23:01:23 +0000618static void change_vnc_password_cb(Monitor *mon, const char *password,
619 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000620{
621 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000622 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000623
aliguori731b0362009-03-05 23:01:42 +0000624 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000625}
626
aliguori376253e2009-03-05 23:01:23 +0000627static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000628{
ths70848512007-08-25 01:37:05 +0000629 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000630 strcmp(target, "password") == 0) {
631 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000632 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000633 strncpy(password, arg, sizeof(password));
634 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000635 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000636 } else {
aliguori376253e2009-03-05 23:01:23 +0000637 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000638 }
ths70848512007-08-25 01:37:05 +0000639 } else {
aliguori28a76be2009-03-06 20:27:40 +0000640 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000641 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000642 }
thse25a5822007-08-25 01:36:20 +0000643}
644
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300645static void do_change(Monitor *mon, const QDict *qdict)
thse25a5822007-08-25 01:36:20 +0000646{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300647 const char *device = qdict_get_str(qdict, "device");
648 const char *target = qdict_get_str(qdict, "target");
649 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000650 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000651 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000652 } else {
aliguori28a76be2009-03-06 20:27:40 +0000653 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000654 }
655}
656
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300657static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000658{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300659 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000660}
661
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300662static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000663{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300664 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000665}
666
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300667static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000668{
669 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300670 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000671
bellard9307c4c2004-04-04 12:57:25 +0000672 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000673 mask = 0;
674 } else {
bellard9307c4c2004-04-04 12:57:25 +0000675 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000676 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000677 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000678 return;
679 }
680 }
681 cpu_set_log(mask);
682}
683
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300684static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000685{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300686 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000687 if (!option || !strcmp(option, "on")) {
688 singlestep = 1;
689 } else if (!strcmp(option, "off")) {
690 singlestep = 0;
691 } else {
692 monitor_printf(mon, "unexpected option %s\n", option);
693 }
694}
695
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -0300696/**
697 * do_stop(): Stop VM execution
698 */
699static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +0000700{
701 vm_stop(EXCP_INTERRUPT);
702}
703
aliguoribb5fc202009-03-05 23:01:15 +0000704static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000705
aliguori376253e2009-03-05 23:01:23 +0000706struct bdrv_iterate_context {
707 Monitor *mon;
708 int err;
709};
aliguoric0f4ce72009-03-05 23:01:01 +0000710
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300711/**
712 * do_cont(): Resume emulation.
713 */
714static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +0000715{
716 struct bdrv_iterate_context context = { mon, 0 };
717
718 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000719 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000720 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000721 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000722}
723
aliguoribb5fc202009-03-05 23:01:15 +0000724static void bdrv_key_cb(void *opaque, int err)
725{
aliguori376253e2009-03-05 23:01:23 +0000726 Monitor *mon = opaque;
727
aliguoribb5fc202009-03-05 23:01:15 +0000728 /* another key was set successfully, retry to continue */
729 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300730 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000731}
732
733static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
734{
aliguori376253e2009-03-05 23:01:23 +0000735 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000736
aliguori376253e2009-03-05 23:01:23 +0000737 if (!context->err && bdrv_key_required(bs)) {
738 context->err = -EBUSY;
739 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
740 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000741 }
742}
743
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300744static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000745{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300746 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000747 if (!device)
748 device = "tcp::" DEFAULT_GDBSTUB_PORT;
749 if (gdbserver_start(device) < 0) {
750 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
751 device);
752 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000753 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000754 } else {
aliguori59030a82009-04-05 18:43:41 +0000755 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
756 device);
bellard8a7ddc32004-03-31 19:00:16 +0000757 }
758}
759
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300760static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100761{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300762 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100763 if (select_watchdog_action(action) == -1) {
764 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
765 }
766}
767
aliguori376253e2009-03-05 23:01:23 +0000768static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000769{
aliguori376253e2009-03-05 23:01:23 +0000770 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000771 switch(c) {
772 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000773 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000774 break;
775 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000776 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000777 break;
778 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000779 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000780 break;
781 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000782 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000783 break;
784 default:
785 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000786 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000787 } else {
aliguori376253e2009-03-05 23:01:23 +0000788 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000789 }
790 break;
791 }
aliguori376253e2009-03-05 23:01:23 +0000792 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000793}
794
aliguori376253e2009-03-05 23:01:23 +0000795static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -0500796 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000797{
bellard6a00d602005-11-21 23:25:50 +0000798 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000799 int nb_per_line, l, line_size, i, max_digits, len;
800 uint8_t buf[16];
801 uint64_t v;
802
803 if (format == 'i') {
804 int flags;
805 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000806 env = mon_get_cpu();
807 if (!env && !is_physical)
808 return;
bellard9307c4c2004-04-04 12:57:25 +0000809#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000810 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000811 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000812 } else if (wsize == 4) {
813 flags = 0;
814 } else {
bellard6a15fd12006-04-12 21:07:07 +0000815 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000816 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000817 if (env) {
818#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000819 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000820 (env->segs[R_CS].flags & DESC_L_MASK))
821 flags = 2;
822 else
823#endif
824 if (!(env->segs[R_CS].flags & DESC_B_MASK))
825 flags = 1;
826 }
bellard4c27ba22004-04-25 18:05:08 +0000827 }
828#endif
aliguori376253e2009-03-05 23:01:23 +0000829 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000830 return;
831 }
832
833 len = wsize * count;
834 if (wsize == 1)
835 line_size = 8;
836 else
837 line_size = 16;
838 nb_per_line = line_size / wsize;
839 max_digits = 0;
840
841 switch(format) {
842 case 'o':
843 max_digits = (wsize * 8 + 2) / 3;
844 break;
845 default:
846 case 'x':
847 max_digits = (wsize * 8) / 4;
848 break;
849 case 'u':
850 case 'd':
851 max_digits = (wsize * 8 * 10 + 32) / 33;
852 break;
853 case 'c':
854 wsize = 1;
855 break;
856 }
857
858 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000859 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000860 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000861 else
aliguori376253e2009-03-05 23:01:23 +0000862 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000863 l = len;
864 if (l > line_size)
865 l = line_size;
866 if (is_physical) {
867 cpu_physical_memory_rw(addr, buf, l, 0);
868 } else {
bellard6a00d602005-11-21 23:25:50 +0000869 env = mon_get_cpu();
870 if (!env)
871 break;
aliguoric8f79b62008-08-18 14:00:20 +0000872 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000873 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000874 break;
875 }
bellard9307c4c2004-04-04 12:57:25 +0000876 }
ths5fafdf22007-09-16 21:08:06 +0000877 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000878 while (i < l) {
879 switch(wsize) {
880 default:
881 case 1:
882 v = ldub_raw(buf + i);
883 break;
884 case 2:
885 v = lduw_raw(buf + i);
886 break;
887 case 4:
bellard92a31b12005-02-10 22:00:52 +0000888 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000889 break;
890 case 8:
891 v = ldq_raw(buf + i);
892 break;
893 }
aliguori376253e2009-03-05 23:01:23 +0000894 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000895 switch(format) {
896 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000897 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000898 break;
899 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000900 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000901 break;
902 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000903 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000904 break;
905 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000906 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000907 break;
908 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000909 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000910 break;
911 }
912 i += wsize;
913 }
aliguori376253e2009-03-05 23:01:23 +0000914 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000915 addr += l;
916 len -= l;
917 }
918}
919
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300920static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000921{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300922 int count = qdict_get_int(qdict, "count");
923 int format = qdict_get_int(qdict, "format");
924 int size = qdict_get_int(qdict, "size");
925 target_long addr = qdict_get_int(qdict, "addr");
926
aliguori376253e2009-03-05 23:01:23 +0000927 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000928}
929
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300930static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000931{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300932 int count = qdict_get_int(qdict, "count");
933 int format = qdict_get_int(qdict, "format");
934 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -0500935 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300936
aliguori376253e2009-03-05 23:01:23 +0000937 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000938}
939
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300940static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000941{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300942 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -0500943 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300944
blueswir17743e582007-09-24 18:39:04 +0000945#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000946 switch(format) {
947 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000948 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000949 break;
950 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000951 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000952 break;
953 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000954 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000955 break;
956 default:
957 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000958 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000959 break;
960 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000961 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000962 break;
963 }
bellard92a31b12005-02-10 22:00:52 +0000964#else
965 switch(format) {
966 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000967 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000968 break;
969 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000970 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000971 break;
972 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000973 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000974 break;
975 default:
976 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000977 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000978 break;
979 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000980 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000981 break;
982 }
983#endif
aliguori376253e2009-03-05 23:01:23 +0000984 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000985}
986
Luiz Capitulino57e09452009-10-16 12:23:43 -0300987static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +0000988{
989 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300990 uint32_t size = qdict_get_int(qdict, "size");
991 const char *filename = qdict_get_str(qdict, "filename");
992 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +0000993 uint32_t l;
994 CPUState *env;
995 uint8_t buf[1024];
996
997 env = mon_get_cpu();
998 if (!env)
999 return;
1000
1001 f = fopen(filename, "wb");
1002 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001003 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +00001004 return;
1005 }
1006 while (size != 0) {
1007 l = sizeof(buf);
1008 if (l > size)
1009 l = size;
1010 cpu_memory_rw_debug(env, addr, buf, l, 0);
1011 fwrite(buf, 1, l, f);
1012 addr += l;
1013 size -= l;
1014 }
1015 fclose(f);
1016}
1017
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001018static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1019 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001020{
1021 FILE *f;
1022 uint32_t l;
1023 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001024 uint32_t size = qdict_get_int(qdict, "size");
1025 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001026 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +00001027
1028 f = fopen(filename, "wb");
1029 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001030 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +00001031 return;
1032 }
1033 while (size != 0) {
1034 l = sizeof(buf);
1035 if (l > size)
1036 l = size;
1037 cpu_physical_memory_rw(addr, buf, l, 0);
1038 fwrite(buf, 1, l, f);
1039 fflush(f);
1040 addr += l;
1041 size -= l;
1042 }
1043 fclose(f);
1044}
1045
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001046static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001047{
1048 uint32_t addr;
1049 uint8_t buf[1];
1050 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001051 uint32_t start = qdict_get_int(qdict, "start");
1052 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001053
1054 sum = 0;
1055 for(addr = start; addr < (start + size); addr++) {
1056 cpu_physical_memory_rw(addr, buf, 1, 0);
1057 /* BSD sum algorithm ('sum' Unix command) */
1058 sum = (sum >> 1) | (sum << 15);
1059 sum += buf[0];
1060 }
aliguori376253e2009-03-05 23:01:23 +00001061 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001062}
1063
bellarda3a91a32004-06-04 11:06:21 +00001064typedef struct {
1065 int keycode;
1066 const char *name;
1067} KeyDef;
1068
1069static const KeyDef key_defs[] = {
1070 { 0x2a, "shift" },
1071 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001072
bellarda3a91a32004-06-04 11:06:21 +00001073 { 0x38, "alt" },
1074 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001075 { 0x64, "altgr" },
1076 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001077 { 0x1d, "ctrl" },
1078 { 0x9d, "ctrl_r" },
1079
1080 { 0xdd, "menu" },
1081
1082 { 0x01, "esc" },
1083
1084 { 0x02, "1" },
1085 { 0x03, "2" },
1086 { 0x04, "3" },
1087 { 0x05, "4" },
1088 { 0x06, "5" },
1089 { 0x07, "6" },
1090 { 0x08, "7" },
1091 { 0x09, "8" },
1092 { 0x0a, "9" },
1093 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001094 { 0x0c, "minus" },
1095 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001096 { 0x0e, "backspace" },
1097
1098 { 0x0f, "tab" },
1099 { 0x10, "q" },
1100 { 0x11, "w" },
1101 { 0x12, "e" },
1102 { 0x13, "r" },
1103 { 0x14, "t" },
1104 { 0x15, "y" },
1105 { 0x16, "u" },
1106 { 0x17, "i" },
1107 { 0x18, "o" },
1108 { 0x19, "p" },
1109
1110 { 0x1c, "ret" },
1111
1112 { 0x1e, "a" },
1113 { 0x1f, "s" },
1114 { 0x20, "d" },
1115 { 0x21, "f" },
1116 { 0x22, "g" },
1117 { 0x23, "h" },
1118 { 0x24, "j" },
1119 { 0x25, "k" },
1120 { 0x26, "l" },
1121
1122 { 0x2c, "z" },
1123 { 0x2d, "x" },
1124 { 0x2e, "c" },
1125 { 0x2f, "v" },
1126 { 0x30, "b" },
1127 { 0x31, "n" },
1128 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001129 { 0x33, "comma" },
1130 { 0x34, "dot" },
1131 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001132
balrog4d3b6f62008-02-10 16:33:14 +00001133 { 0x37, "asterisk" },
1134
bellarda3a91a32004-06-04 11:06:21 +00001135 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001136 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001137 { 0x3b, "f1" },
1138 { 0x3c, "f2" },
1139 { 0x3d, "f3" },
1140 { 0x3e, "f4" },
1141 { 0x3f, "f5" },
1142 { 0x40, "f6" },
1143 { 0x41, "f7" },
1144 { 0x42, "f8" },
1145 { 0x43, "f9" },
1146 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001147 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001148 { 0x46, "scroll_lock" },
1149
bellard64866c32006-05-07 18:03:31 +00001150 { 0xb5, "kp_divide" },
1151 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001152 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001153 { 0x4e, "kp_add" },
1154 { 0x9c, "kp_enter" },
1155 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001156 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001157
1158 { 0x52, "kp_0" },
1159 { 0x4f, "kp_1" },
1160 { 0x50, "kp_2" },
1161 { 0x51, "kp_3" },
1162 { 0x4b, "kp_4" },
1163 { 0x4c, "kp_5" },
1164 { 0x4d, "kp_6" },
1165 { 0x47, "kp_7" },
1166 { 0x48, "kp_8" },
1167 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001168
bellarda3a91a32004-06-04 11:06:21 +00001169 { 0x56, "<" },
1170
1171 { 0x57, "f11" },
1172 { 0x58, "f12" },
1173
1174 { 0xb7, "print" },
1175
1176 { 0xc7, "home" },
1177 { 0xc9, "pgup" },
1178 { 0xd1, "pgdn" },
1179 { 0xcf, "end" },
1180
1181 { 0xcb, "left" },
1182 { 0xc8, "up" },
1183 { 0xd0, "down" },
1184 { 0xcd, "right" },
1185
1186 { 0xd2, "insert" },
1187 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001188#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1189 { 0xf0, "stop" },
1190 { 0xf1, "again" },
1191 { 0xf2, "props" },
1192 { 0xf3, "undo" },
1193 { 0xf4, "front" },
1194 { 0xf5, "copy" },
1195 { 0xf6, "open" },
1196 { 0xf7, "paste" },
1197 { 0xf8, "find" },
1198 { 0xf9, "cut" },
1199 { 0xfa, "lf" },
1200 { 0xfb, "help" },
1201 { 0xfc, "meta_l" },
1202 { 0xfd, "meta_r" },
1203 { 0xfe, "compose" },
1204#endif
bellarda3a91a32004-06-04 11:06:21 +00001205 { 0, NULL },
1206};
1207
1208static int get_keycode(const char *key)
1209{
1210 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001211 char *endp;
1212 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001213
1214 for(p = key_defs; p->name != NULL; p++) {
1215 if (!strcmp(key, p->name))
1216 return p->keycode;
1217 }
bellard64866c32006-05-07 18:03:31 +00001218 if (strstart(key, "0x", NULL)) {
1219 ret = strtoul(key, &endp, 0);
1220 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1221 return ret;
1222 }
bellarda3a91a32004-06-04 11:06:21 +00001223 return -1;
1224}
1225
balrogc8256f92008-06-08 22:45:01 +00001226#define MAX_KEYCODES 16
1227static uint8_t keycodes[MAX_KEYCODES];
1228static int nb_pending_keycodes;
1229static QEMUTimer *key_timer;
1230
1231static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001232{
balrogc8256f92008-06-08 22:45:01 +00001233 int keycode;
1234
1235 while (nb_pending_keycodes > 0) {
1236 nb_pending_keycodes--;
1237 keycode = keycodes[nb_pending_keycodes];
1238 if (keycode & 0x80)
1239 kbd_put_keycode(0xe0);
1240 kbd_put_keycode(keycode | 0x80);
1241 }
1242}
1243
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001244static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001245{
balrog3401c0d2008-06-04 10:05:59 +00001246 char keyname_buf[16];
1247 char *separator;
1248 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001249 const char *string = qdict_get_str(qdict, "string");
1250 int has_hold_time = qdict_haskey(qdict, "hold_time");
1251 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001252
balrogc8256f92008-06-08 22:45:01 +00001253 if (nb_pending_keycodes > 0) {
1254 qemu_del_timer(key_timer);
1255 release_keys(NULL);
1256 }
1257 if (!has_hold_time)
1258 hold_time = 100;
1259 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001260 while (1) {
1261 separator = strchr(string, '-');
1262 keyname_len = separator ? separator - string : strlen(string);
1263 if (keyname_len > 0) {
1264 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1265 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001266 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001267 return;
bellarda3a91a32004-06-04 11:06:21 +00001268 }
balrogc8256f92008-06-08 22:45:01 +00001269 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001270 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001271 return;
1272 }
1273 keyname_buf[keyname_len] = 0;
1274 keycode = get_keycode(keyname_buf);
1275 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001276 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001277 return;
1278 }
balrogc8256f92008-06-08 22:45:01 +00001279 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001280 }
balrog3401c0d2008-06-04 10:05:59 +00001281 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001282 break;
balrog3401c0d2008-06-04 10:05:59 +00001283 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001284 }
balrogc8256f92008-06-08 22:45:01 +00001285 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001286 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001287 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001288 keycode = keycodes[i];
1289 if (keycode & 0x80)
1290 kbd_put_keycode(0xe0);
1291 kbd_put_keycode(keycode & 0x7f);
1292 }
balrogc8256f92008-06-08 22:45:01 +00001293 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001294 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001295 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001296}
1297
bellard13224a82006-07-14 22:03:35 +00001298static int mouse_button_state;
1299
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001300static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001301{
1302 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001303 const char *dx_str = qdict_get_str(qdict, "dx_str");
1304 const char *dy_str = qdict_get_str(qdict, "dy_str");
1305 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001306 dx = strtol(dx_str, NULL, 0);
1307 dy = strtol(dy_str, NULL, 0);
1308 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001309 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001310 dz = strtol(dz_str, NULL, 0);
1311 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1312}
1313
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001314static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001315{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001316 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001317 mouse_button_state = button_state;
1318 kbd_mouse_event(0, 0, 0, mouse_button_state);
1319}
1320
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001321static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001322{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001323 int size = qdict_get_int(qdict, "size");
1324 int addr = qdict_get_int(qdict, "addr");
1325 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001326 uint32_t val;
1327 int suffix;
1328
1329 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001330 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001331 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001332 addr++;
1333 }
1334 addr &= 0xffff;
1335
1336 switch(size) {
1337 default:
1338 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001339 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001340 suffix = 'b';
1341 break;
1342 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001343 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001344 suffix = 'w';
1345 break;
1346 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001347 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001348 suffix = 'l';
1349 break;
1350 }
aliguori376253e2009-03-05 23:01:23 +00001351 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1352 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001353}
bellarda3a91a32004-06-04 11:06:21 +00001354
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001355static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001356{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001357 int size = qdict_get_int(qdict, "size");
1358 int addr = qdict_get_int(qdict, "addr");
1359 int val = qdict_get_int(qdict, "val");
1360
Jan Kiszkaf1147842009-07-14 10:20:11 +02001361 addr &= IOPORTS_MASK;
1362
1363 switch (size) {
1364 default:
1365 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001366 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001367 break;
1368 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001369 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001370 break;
1371 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001372 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001373 break;
1374 }
1375}
1376
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001377static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001378{
1379 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001380 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001381
Jan Kiszka76e30d02009-07-02 00:19:02 +02001382 res = qemu_boot_set(bootdevice);
1383 if (res == 0) {
1384 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1385 } else if (res > 0) {
1386 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001387 } else {
aliguori376253e2009-03-05 23:01:23 +00001388 monitor_printf(mon, "no function defined to set boot device list for "
1389 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001390 }
1391}
1392
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001393/**
1394 * do_system_reset(): Issue a machine reset
1395 */
1396static void do_system_reset(Monitor *mon, const QDict *qdict,
1397 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001398{
1399 qemu_system_reset_request();
1400}
1401
Luiz Capitulino43076662009-10-07 13:41:59 -03001402/**
1403 * do_system_powerdown(): Issue a machine powerdown
1404 */
1405static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1406 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001407{
1408 qemu_system_powerdown_request();
1409}
1410
bellardb86bda52004-09-18 19:32:46 +00001411#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001412static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001413{
aliguori376253e2009-03-05 23:01:23 +00001414 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1415 addr,
1416 pte & mask,
1417 pte & PG_GLOBAL_MASK ? 'G' : '-',
1418 pte & PG_PSE_MASK ? 'P' : '-',
1419 pte & PG_DIRTY_MASK ? 'D' : '-',
1420 pte & PG_ACCESSED_MASK ? 'A' : '-',
1421 pte & PG_PCD_MASK ? 'C' : '-',
1422 pte & PG_PWT_MASK ? 'T' : '-',
1423 pte & PG_USER_MASK ? 'U' : '-',
1424 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001425}
1426
aliguori376253e2009-03-05 23:01:23 +00001427static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001428{
bellard6a00d602005-11-21 23:25:50 +00001429 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001430 int l1, l2;
1431 uint32_t pgd, pde, pte;
1432
bellard6a00d602005-11-21 23:25:50 +00001433 env = mon_get_cpu();
1434 if (!env)
1435 return;
1436
bellardb86bda52004-09-18 19:32:46 +00001437 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001438 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001439 return;
1440 }
1441 pgd = env->cr[3] & ~0xfff;
1442 for(l1 = 0; l1 < 1024; l1++) {
1443 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1444 pde = le32_to_cpu(pde);
1445 if (pde & PG_PRESENT_MASK) {
1446 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001447 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001448 } else {
1449 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001450 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001451 (uint8_t *)&pte, 4);
1452 pte = le32_to_cpu(pte);
1453 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001454 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001455 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001456 ~0xfff);
1457 }
1458 }
1459 }
1460 }
1461 }
1462}
1463
aliguori376253e2009-03-05 23:01:23 +00001464static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001465 uint32_t end, int prot)
1466{
bellard9746b152004-11-11 18:30:24 +00001467 int prot1;
1468 prot1 = *plast_prot;
1469 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001470 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001471 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1472 *pstart, end, end - *pstart,
1473 prot1 & PG_USER_MASK ? 'u' : '-',
1474 'r',
1475 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001476 }
1477 if (prot != 0)
1478 *pstart = end;
1479 else
1480 *pstart = -1;
1481 *plast_prot = prot;
1482 }
1483}
1484
aliguori376253e2009-03-05 23:01:23 +00001485static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001486{
bellard6a00d602005-11-21 23:25:50 +00001487 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001488 int l1, l2, prot, last_prot;
1489 uint32_t pgd, pde, pte, start, end;
1490
bellard6a00d602005-11-21 23:25:50 +00001491 env = mon_get_cpu();
1492 if (!env)
1493 return;
1494
bellardb86bda52004-09-18 19:32:46 +00001495 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001496 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001497 return;
1498 }
1499 pgd = env->cr[3] & ~0xfff;
1500 last_prot = 0;
1501 start = -1;
1502 for(l1 = 0; l1 < 1024; l1++) {
1503 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1504 pde = le32_to_cpu(pde);
1505 end = l1 << 22;
1506 if (pde & PG_PRESENT_MASK) {
1507 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1508 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001509 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001510 } else {
1511 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001512 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001513 (uint8_t *)&pte, 4);
1514 pte = le32_to_cpu(pte);
1515 end = (l1 << 22) + (l2 << 12);
1516 if (pte & PG_PRESENT_MASK) {
1517 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1518 } else {
1519 prot = 0;
1520 }
aliguori376253e2009-03-05 23:01:23 +00001521 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001522 }
1523 }
1524 } else {
1525 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001526 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001527 }
1528 }
1529}
1530#endif
1531
aurel327c664e22009-03-03 06:12:22 +00001532#if defined(TARGET_SH4)
1533
aliguori376253e2009-03-05 23:01:23 +00001534static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001535{
aliguori376253e2009-03-05 23:01:23 +00001536 monitor_printf(mon, " tlb%i:\t"
1537 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1538 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1539 "dirty=%hhu writethrough=%hhu\n",
1540 idx,
1541 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1542 tlb->v, tlb->sh, tlb->c, tlb->pr,
1543 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001544}
1545
aliguori376253e2009-03-05 23:01:23 +00001546static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001547{
1548 CPUState *env = mon_get_cpu();
1549 int i;
1550
aliguori376253e2009-03-05 23:01:23 +00001551 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001552 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001553 print_tlb (mon, i, &env->itlb[i]);
1554 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001555 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001556 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001557}
1558
1559#endif
1560
aliguori376253e2009-03-05 23:01:23 +00001561static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001562{
1563#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001564 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001565 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001566 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001567 else
aliguori376253e2009-03-05 23:01:23 +00001568 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001569#else
aliguori376253e2009-03-05 23:01:23 +00001570 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001571#endif
1572}
1573
aliguori030ea372009-04-21 22:30:47 +00001574static void do_info_numa(Monitor *mon)
1575{
aliguorib28b6232009-04-22 20:20:29 +00001576 int i;
aliguori030ea372009-04-21 22:30:47 +00001577 CPUState *env;
1578
1579 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1580 for (i = 0; i < nb_numa_nodes; i++) {
1581 monitor_printf(mon, "node %d cpus:", i);
1582 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1583 if (env->numa_node == i) {
1584 monitor_printf(mon, " %d", env->cpu_index);
1585 }
1586 }
1587 monitor_printf(mon, "\n");
1588 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1589 node_mem[i] >> 20);
1590 }
1591}
1592
bellard5f1ce942006-02-08 22:40:15 +00001593#ifdef CONFIG_PROFILER
1594
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001595int64_t qemu_time;
1596int64_t dev_time;
1597
aliguori376253e2009-03-05 23:01:23 +00001598static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001599{
1600 int64_t total;
1601 total = qemu_time;
1602 if (total == 0)
1603 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001604 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001605 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001606 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001607 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001608 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001609 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001610}
1611#else
aliguori376253e2009-03-05 23:01:23 +00001612static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001613{
aliguori376253e2009-03-05 23:01:23 +00001614 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001615}
1616#endif
1617
bellardec36b692006-07-16 18:57:03 +00001618/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001619static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001620
aliguori376253e2009-03-05 23:01:23 +00001621static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001622{
1623 int i;
1624 CaptureState *s;
1625
1626 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001627 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001628 s->ops.info (s->opaque);
1629 }
1630}
1631
Blue Swirl23130862009-06-06 08:22:04 +00001632#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001633static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001634{
1635 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001636 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001637 CaptureState *s;
1638
1639 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1640 if (i == n) {
1641 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001642 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00001643 qemu_free (s);
1644 return;
1645 }
1646 }
1647}
1648
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001649static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001650{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001651 const char *path = qdict_get_str(qdict, "path");
1652 int has_freq = qdict_haskey(qdict, "freq");
1653 int freq = qdict_get_try_int(qdict, "freq", -1);
1654 int has_bits = qdict_haskey(qdict, "bits");
1655 int bits = qdict_get_try_int(qdict, "bits", -1);
1656 int has_channels = qdict_haskey(qdict, "nchannels");
1657 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001658 CaptureState *s;
1659
1660 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001661
1662 freq = has_freq ? freq : 44100;
1663 bits = has_bits ? bits : 16;
1664 nchannels = has_channels ? nchannels : 2;
1665
1666 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001667 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001668 qemu_free (s);
1669 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001670 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001671}
1672#endif
1673
aurel32dc1c0b72008-04-27 23:52:12 +00001674#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001675static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001676{
1677 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001678 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001679
1680 for (env = first_cpu; env != NULL; env = env->next_cpu)
1681 if (env->cpu_index == cpu_index) {
1682 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1683 break;
1684 }
1685}
1686#endif
1687
aliguori376253e2009-03-05 23:01:23 +00001688static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001689{
aurel321b530a62009-04-05 20:08:59 +00001690 if (vm_running) {
1691 if (singlestep) {
1692 monitor_printf(mon, "VM status: running (single step mode)\n");
1693 } else {
1694 monitor_printf(mon, "VM status: running\n");
1695 }
1696 } else
aliguori376253e2009-03-05 23:01:23 +00001697 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001698}
1699
Luiz Capitulino83fb1de2009-10-07 13:42:01 -03001700/**
1701 * do_balloon(): Request VM to change its memory allocation
1702 */
1703static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001704{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001705 int value = qdict_get_int(qdict, "value");
Anthony Liguoric227f092009-10-01 16:12:16 -05001706 ram_addr_t target = value;
aliguoridf751fa2008-12-04 20:19:35 +00001707 qemu_balloon(target << 20);
1708}
1709
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001710static void monitor_print_balloon(Monitor *mon, const QObject *data)
1711{
1712 monitor_printf(mon, "balloon: actual=%d\n",
1713 (int)qint_get_int(qobject_to_qint(data)));
1714}
1715
1716/**
1717 * do_info_balloon(): Balloon information
1718 */
1719static void do_info_balloon(Monitor *mon, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001720{
Anthony Liguoric227f092009-10-01 16:12:16 -05001721 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00001722
1723 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001724 if (kvm_enabled() && !kvm_has_sync_mmu())
Luiz Capitulino5d6c37f2009-11-18 23:05:36 -02001725 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
aliguoribd322082008-12-04 20:33:06 +00001726 else if (actual == 0)
Luiz Capitulino5d6c37f2009-11-18 23:05:36 -02001727 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
aliguoridf751fa2008-12-04 20:19:35 +00001728 else
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001729 *ret_data = QOBJECT(qint_from_int((int)(actual >> 20)));
aliguoridf751fa2008-12-04 20:19:35 +00001730}
1731
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001732static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001733{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001734 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001735
aliguori76655d62009-03-06 20:27:37 +00001736 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001737 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001738 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001739 return acl;
1740}
aliguori76655d62009-03-06 20:27:37 +00001741
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001742static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001743{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001744 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001745 qemu_acl *acl = find_acl(mon, aclname);
1746 qemu_acl_entry *entry;
1747 int i = 0;
1748
1749 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001750 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001751 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001752 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001753 i++;
1754 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001755 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001756 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001757 }
1758}
1759
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001760static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001761{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001762 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001763 qemu_acl *acl = find_acl(mon, aclname);
1764
1765 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001766 qemu_acl_reset(acl);
1767 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001768 }
1769}
aliguori76655d62009-03-06 20:27:37 +00001770
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001771static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001772{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001773 const char *aclname = qdict_get_str(qdict, "aclname");
1774 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001775 qemu_acl *acl = find_acl(mon, aclname);
1776
1777 if (acl) {
1778 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001779 acl->defaultDeny = 0;
1780 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001781 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001782 acl->defaultDeny = 1;
1783 monitor_printf(mon, "acl: policy set to 'deny'\n");
1784 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001785 monitor_printf(mon, "acl: unknown policy '%s', "
1786 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001787 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001788 }
1789}
aliguori76655d62009-03-06 20:27:37 +00001790
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001791static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001792{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001793 const char *aclname = qdict_get_str(qdict, "aclname");
1794 const char *match = qdict_get_str(qdict, "match");
1795 const char *policy = qdict_get_str(qdict, "policy");
1796 int has_index = qdict_haskey(qdict, "index");
1797 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001798 qemu_acl *acl = find_acl(mon, aclname);
1799 int deny, ret;
1800
1801 if (acl) {
1802 if (strcmp(policy, "allow") == 0) {
1803 deny = 0;
1804 } else if (strcmp(policy, "deny") == 0) {
1805 deny = 1;
1806 } else {
1807 monitor_printf(mon, "acl: unknown policy '%s', "
1808 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001809 return;
1810 }
aliguori28a76be2009-03-06 20:27:40 +00001811 if (has_index)
1812 ret = qemu_acl_insert(acl, deny, match, index);
1813 else
1814 ret = qemu_acl_append(acl, deny, match);
1815 if (ret < 0)
1816 monitor_printf(mon, "acl: unable to add acl entry\n");
1817 else
1818 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001819 }
1820}
aliguori76655d62009-03-06 20:27:37 +00001821
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001822static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001823{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001824 const char *aclname = qdict_get_str(qdict, "aclname");
1825 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001826 qemu_acl *acl = find_acl(mon, aclname);
1827 int ret;
aliguori76655d62009-03-06 20:27:37 +00001828
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001829 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001830 ret = qemu_acl_remove(acl, match);
1831 if (ret < 0)
1832 monitor_printf(mon, "acl: no matching acl entry\n");
1833 else
1834 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001835 }
1836}
1837
Huang Ying79c4f6b2009-06-23 10:05:14 +08001838#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001839static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001840{
1841 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001842 int cpu_index = qdict_get_int(qdict, "cpu_index");
1843 int bank = qdict_get_int(qdict, "bank");
1844 uint64_t status = qdict_get_int(qdict, "status");
1845 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1846 uint64_t addr = qdict_get_int(qdict, "addr");
1847 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08001848
1849 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1850 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1851 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1852 break;
1853 }
1854}
1855#endif
1856
Luiz Capitulinof0d60002009-10-16 12:23:50 -03001857static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001858{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001859 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001860 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001861 int fd;
1862
1863 fd = qemu_chr_get_msgfd(mon->chr);
1864 if (fd == -1) {
1865 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1866 return;
1867 }
1868
1869 if (qemu_isdigit(fdname[0])) {
1870 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1871 return;
1872 }
1873
1874 fd = dup(fd);
1875 if (fd == -1) {
1876 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1877 strerror(errno));
1878 return;
1879 }
1880
Blue Swirl72cf2d42009-09-12 07:36:22 +00001881 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001882 if (strcmp(monfd->name, fdname) != 0) {
1883 continue;
1884 }
1885
1886 close(monfd->fd);
1887 monfd->fd = fd;
1888 return;
1889 }
1890
Anthony Liguoric227f092009-10-01 16:12:16 -05001891 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001892 monfd->name = qemu_strdup(fdname);
1893 monfd->fd = fd;
1894
Blue Swirl72cf2d42009-09-12 07:36:22 +00001895 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001896}
1897
Luiz Capitulino18f3a512009-10-16 12:23:51 -03001898static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001899{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001900 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001901 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001902
Blue Swirl72cf2d42009-09-12 07:36:22 +00001903 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001904 if (strcmp(monfd->name, fdname) != 0) {
1905 continue;
1906 }
1907
Blue Swirl72cf2d42009-09-12 07:36:22 +00001908 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001909 close(monfd->fd);
1910 qemu_free(monfd->name);
1911 qemu_free(monfd);
1912 return;
1913 }
1914
1915 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1916 fdname);
1917}
1918
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001919static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001920{
1921 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001922 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001923
1924 vm_stop(0);
1925
Juan Quintela05f24012009-08-20 19:42:22 +02001926 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001927 vm_start();
1928}
1929
Mark McLoughlin7768e042009-07-22 09:11:41 +01001930int monitor_get_fd(Monitor *mon, const char *fdname)
1931{
Anthony Liguoric227f092009-10-01 16:12:16 -05001932 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001933
Blue Swirl72cf2d42009-09-12 07:36:22 +00001934 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001935 int fd;
1936
1937 if (strcmp(monfd->name, fdname) != 0) {
1938 continue;
1939 }
1940
1941 fd = monfd->fd;
1942
1943 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001944 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001945 qemu_free(monfd->name);
1946 qemu_free(monfd);
1947
1948 return fd;
1949 }
1950
1951 return -1;
1952}
1953
Anthony Liguoric227f092009-10-01 16:12:16 -05001954static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001955#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001956 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001957};
1958
Blue Swirl23130862009-06-06 08:22:04 +00001959/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05001960static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001961 {
1962 .name = "version",
1963 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001964 .params = "",
1965 .help = "show the version of QEMU",
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03001966 .user_print = monitor_print_qobject,
1967 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001968 },
1969 {
1970 .name = "network",
1971 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001972 .params = "",
1973 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03001974 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001975 },
1976 {
1977 .name = "chardev",
1978 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001979 .params = "",
1980 .help = "show the character devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001981 .mhandler.info = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001982 },
1983 {
1984 .name = "block",
1985 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001986 .params = "",
1987 .help = "show the block devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001988 .mhandler.info = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001989 },
1990 {
1991 .name = "blockstats",
1992 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001993 .params = "",
1994 .help = "show block device statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03001995 .mhandler.info = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001996 },
1997 {
1998 .name = "registers",
1999 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002000 .params = "",
2001 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002002 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002003 },
2004 {
2005 .name = "cpus",
2006 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002007 .params = "",
2008 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002009 .user_print = monitor_print_cpus,
2010 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002011 },
2012 {
2013 .name = "history",
2014 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002015 .params = "",
2016 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002017 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002018 },
2019 {
2020 .name = "irq",
2021 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002022 .params = "",
2023 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002024 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002025 },
2026 {
2027 .name = "pic",
2028 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002029 .params = "",
2030 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002031 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002032 },
2033 {
2034 .name = "pci",
2035 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002036 .params = "",
2037 .help = "show PCI info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002038 .mhandler.info = pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002039 },
aurel327c664e22009-03-03 06:12:22 +00002040#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002041 {
2042 .name = "tlb",
2043 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002044 .params = "",
2045 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002046 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002047 },
aurel327c664e22009-03-03 06:12:22 +00002048#endif
2049#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002050 {
2051 .name = "mem",
2052 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002053 .params = "",
2054 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002055 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002056 },
2057 {
2058 .name = "hpet",
2059 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002060 .params = "",
2061 .help = "show state of HPET",
Luiz Capitulino910df892009-10-07 13:41:51 -03002062 .mhandler.info = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002063 },
bellardb86bda52004-09-18 19:32:46 +00002064#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002065 {
2066 .name = "jit",
2067 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002068 .params = "",
2069 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002070 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002071 },
2072 {
2073 .name = "kvm",
2074 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002075 .params = "",
2076 .help = "show KVM information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002077 .mhandler.info = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002078 },
2079 {
2080 .name = "numa",
2081 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002082 .params = "",
2083 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002084 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002085 },
2086 {
2087 .name = "usb",
2088 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002089 .params = "",
2090 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002091 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002092 },
2093 {
2094 .name = "usbhost",
2095 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002096 .params = "",
2097 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002098 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002099 },
2100 {
2101 .name = "profile",
2102 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002103 .params = "",
2104 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002105 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002106 },
2107 {
2108 .name = "capture",
2109 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002110 .params = "",
2111 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002112 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002113 },
2114 {
2115 .name = "snapshots",
2116 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002117 .params = "",
2118 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002119 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002120 },
2121 {
2122 .name = "status",
2123 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002124 .params = "",
2125 .help = "show the current VM status (running|paused)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002126 .mhandler.info = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002127 },
2128 {
2129 .name = "pcmcia",
2130 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002131 .params = "",
2132 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002133 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002134 },
2135 {
2136 .name = "mice",
2137 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002138 .params = "",
2139 .help = "show which guest mouse is receiving events",
Luiz Capitulino910df892009-10-07 13:41:51 -03002140 .mhandler.info = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002141 },
2142 {
2143 .name = "vnc",
2144 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002145 .params = "",
2146 .help = "show the vnc server status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002147 .mhandler.info = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002148 },
2149 {
2150 .name = "name",
2151 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002152 .params = "",
2153 .help = "show the current VM name",
Luiz Capitulino910df892009-10-07 13:41:51 -03002154 .mhandler.info = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002155 },
2156 {
2157 .name = "uuid",
2158 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002159 .params = "",
2160 .help = "show the current VM UUID",
Luiz Capitulino910df892009-10-07 13:41:51 -03002161 .mhandler.info = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002162 },
j_mayer76a66252007-03-07 08:32:30 +00002163#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002164 {
2165 .name = "cpustats",
2166 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002167 .params = "",
2168 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002169 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002170 },
j_mayer76a66252007-03-07 08:32:30 +00002171#endif
blueswir131a60e22007-10-26 18:42:59 +00002172#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002173 {
2174 .name = "usernet",
2175 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002176 .params = "",
2177 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002178 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002179 },
blueswir131a60e22007-10-26 18:42:59 +00002180#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002181 {
2182 .name = "migrate",
2183 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002184 .params = "",
2185 .help = "show migration status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002186 .mhandler.info = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002187 },
2188 {
2189 .name = "balloon",
2190 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002191 .params = "",
2192 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002193 .user_print = monitor_print_balloon,
2194 .mhandler.info_new = do_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002195 },
2196 {
2197 .name = "qtree",
2198 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002199 .params = "",
2200 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002201 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002202 },
2203 {
2204 .name = "qdm",
2205 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002206 .params = "",
2207 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002208 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002209 },
2210 {
2211 .name = "roms",
2212 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002213 .params = "",
2214 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002215 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002216 },
2217 {
2218 .name = NULL,
2219 },
bellard9dc39cb2004-03-14 21:38:27 +00002220};
2221
bellard9307c4c2004-04-04 12:57:25 +00002222/*******************************************************************/
2223
2224static const char *pch;
2225static jmp_buf expr_env;
2226
bellard92a31b12005-02-10 22:00:52 +00002227#define MD_TLONG 0
2228#define MD_I32 1
2229
bellard9307c4c2004-04-04 12:57:25 +00002230typedef struct MonitorDef {
2231 const char *name;
2232 int offset;
blueswir18662d652008-10-02 18:32:44 +00002233 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002234 int type;
bellard9307c4c2004-04-04 12:57:25 +00002235} MonitorDef;
2236
bellard57206fd2004-04-25 18:54:52 +00002237#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002238static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002239{
bellard6a00d602005-11-21 23:25:50 +00002240 CPUState *env = mon_get_cpu();
2241 if (!env)
2242 return 0;
2243 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002244}
2245#endif
2246
bellarda541f292004-04-12 20:39:29 +00002247#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002248static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002249{
bellard6a00d602005-11-21 23:25:50 +00002250 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002251 unsigned int u;
2252 int i;
2253
bellard6a00d602005-11-21 23:25:50 +00002254 if (!env)
2255 return 0;
2256
bellarda541f292004-04-12 20:39:29 +00002257 u = 0;
2258 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002259 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002260
2261 return u;
2262}
2263
blueswir18662d652008-10-02 18:32:44 +00002264static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002265{
bellard6a00d602005-11-21 23:25:50 +00002266 CPUState *env = mon_get_cpu();
2267 if (!env)
2268 return 0;
j_mayer0411a972007-10-25 21:35:50 +00002269 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002270}
2271
blueswir18662d652008-10-02 18:32:44 +00002272static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002273{
bellard6a00d602005-11-21 23:25:50 +00002274 CPUState *env = mon_get_cpu();
2275 if (!env)
2276 return 0;
aurel323d7b4172008-10-21 11:28:46 +00002277 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002278}
bellard9fddaa02004-05-21 12:59:32 +00002279
blueswir18662d652008-10-02 18:32:44 +00002280static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002281{
bellard6a00d602005-11-21 23:25:50 +00002282 CPUState *env = mon_get_cpu();
2283 if (!env)
2284 return 0;
2285 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002286}
2287
blueswir18662d652008-10-02 18:32:44 +00002288static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002289{
bellard6a00d602005-11-21 23:25:50 +00002290 CPUState *env = mon_get_cpu();
2291 if (!env)
2292 return 0;
2293 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002294}
2295
blueswir18662d652008-10-02 18:32:44 +00002296static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002297{
bellard6a00d602005-11-21 23:25:50 +00002298 CPUState *env = mon_get_cpu();
2299 if (!env)
2300 return 0;
2301 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002302}
bellarda541f292004-04-12 20:39:29 +00002303#endif
2304
bellarde95c8d52004-09-30 22:22:08 +00002305#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002306#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002307static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002308{
bellard6a00d602005-11-21 23:25:50 +00002309 CPUState *env = mon_get_cpu();
2310 if (!env)
2311 return 0;
2312 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002313}
bellard7b936c02005-10-30 17:05:13 +00002314#endif
bellarde95c8d52004-09-30 22:22:08 +00002315
blueswir18662d652008-10-02 18:32:44 +00002316static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002317{
bellard6a00d602005-11-21 23:25:50 +00002318 CPUState *env = mon_get_cpu();
2319 if (!env)
2320 return 0;
2321 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002322}
2323#endif
2324
blueswir18662d652008-10-02 18:32:44 +00002325static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002326#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002327
2328#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002329 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002330 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002331 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002332
bellard9307c4c2004-04-04 12:57:25 +00002333 { "eax", offsetof(CPUState, regs[0]) },
2334 { "ecx", offsetof(CPUState, regs[1]) },
2335 { "edx", offsetof(CPUState, regs[2]) },
2336 { "ebx", offsetof(CPUState, regs[3]) },
2337 { "esp|sp", offsetof(CPUState, regs[4]) },
2338 { "ebp|fp", offsetof(CPUState, regs[5]) },
2339 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002340 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002341#ifdef TARGET_X86_64
2342 { "r8", offsetof(CPUState, regs[8]) },
2343 { "r9", offsetof(CPUState, regs[9]) },
2344 { "r10", offsetof(CPUState, regs[10]) },
2345 { "r11", offsetof(CPUState, regs[11]) },
2346 { "r12", offsetof(CPUState, regs[12]) },
2347 { "r13", offsetof(CPUState, regs[13]) },
2348 { "r14", offsetof(CPUState, regs[14]) },
2349 { "r15", offsetof(CPUState, regs[15]) },
2350#endif
bellard9307c4c2004-04-04 12:57:25 +00002351 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002352 { "eip", offsetof(CPUState, eip) },
2353 SEG("cs", R_CS)
2354 SEG("ds", R_DS)
2355 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002356 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002357 SEG("fs", R_FS)
2358 SEG("gs", R_GS)
2359 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002360#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002361 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002362 { "r0", offsetof(CPUState, gpr[0]) },
2363 { "r1", offsetof(CPUState, gpr[1]) },
2364 { "r2", offsetof(CPUState, gpr[2]) },
2365 { "r3", offsetof(CPUState, gpr[3]) },
2366 { "r4", offsetof(CPUState, gpr[4]) },
2367 { "r5", offsetof(CPUState, gpr[5]) },
2368 { "r6", offsetof(CPUState, gpr[6]) },
2369 { "r7", offsetof(CPUState, gpr[7]) },
2370 { "r8", offsetof(CPUState, gpr[8]) },
2371 { "r9", offsetof(CPUState, gpr[9]) },
2372 { "r10", offsetof(CPUState, gpr[10]) },
2373 { "r11", offsetof(CPUState, gpr[11]) },
2374 { "r12", offsetof(CPUState, gpr[12]) },
2375 { "r13", offsetof(CPUState, gpr[13]) },
2376 { "r14", offsetof(CPUState, gpr[14]) },
2377 { "r15", offsetof(CPUState, gpr[15]) },
2378 { "r16", offsetof(CPUState, gpr[16]) },
2379 { "r17", offsetof(CPUState, gpr[17]) },
2380 { "r18", offsetof(CPUState, gpr[18]) },
2381 { "r19", offsetof(CPUState, gpr[19]) },
2382 { "r20", offsetof(CPUState, gpr[20]) },
2383 { "r21", offsetof(CPUState, gpr[21]) },
2384 { "r22", offsetof(CPUState, gpr[22]) },
2385 { "r23", offsetof(CPUState, gpr[23]) },
2386 { "r24", offsetof(CPUState, gpr[24]) },
2387 { "r25", offsetof(CPUState, gpr[25]) },
2388 { "r26", offsetof(CPUState, gpr[26]) },
2389 { "r27", offsetof(CPUState, gpr[27]) },
2390 { "r28", offsetof(CPUState, gpr[28]) },
2391 { "r29", offsetof(CPUState, gpr[29]) },
2392 { "r30", offsetof(CPUState, gpr[30]) },
2393 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002394 /* Floating point registers */
2395 { "f0", offsetof(CPUState, fpr[0]) },
2396 { "f1", offsetof(CPUState, fpr[1]) },
2397 { "f2", offsetof(CPUState, fpr[2]) },
2398 { "f3", offsetof(CPUState, fpr[3]) },
2399 { "f4", offsetof(CPUState, fpr[4]) },
2400 { "f5", offsetof(CPUState, fpr[5]) },
2401 { "f6", offsetof(CPUState, fpr[6]) },
2402 { "f7", offsetof(CPUState, fpr[7]) },
2403 { "f8", offsetof(CPUState, fpr[8]) },
2404 { "f9", offsetof(CPUState, fpr[9]) },
2405 { "f10", offsetof(CPUState, fpr[10]) },
2406 { "f11", offsetof(CPUState, fpr[11]) },
2407 { "f12", offsetof(CPUState, fpr[12]) },
2408 { "f13", offsetof(CPUState, fpr[13]) },
2409 { "f14", offsetof(CPUState, fpr[14]) },
2410 { "f15", offsetof(CPUState, fpr[15]) },
2411 { "f16", offsetof(CPUState, fpr[16]) },
2412 { "f17", offsetof(CPUState, fpr[17]) },
2413 { "f18", offsetof(CPUState, fpr[18]) },
2414 { "f19", offsetof(CPUState, fpr[19]) },
2415 { "f20", offsetof(CPUState, fpr[20]) },
2416 { "f21", offsetof(CPUState, fpr[21]) },
2417 { "f22", offsetof(CPUState, fpr[22]) },
2418 { "f23", offsetof(CPUState, fpr[23]) },
2419 { "f24", offsetof(CPUState, fpr[24]) },
2420 { "f25", offsetof(CPUState, fpr[25]) },
2421 { "f26", offsetof(CPUState, fpr[26]) },
2422 { "f27", offsetof(CPUState, fpr[27]) },
2423 { "f28", offsetof(CPUState, fpr[28]) },
2424 { "f29", offsetof(CPUState, fpr[29]) },
2425 { "f30", offsetof(CPUState, fpr[30]) },
2426 { "f31", offsetof(CPUState, fpr[31]) },
2427 { "fpscr", offsetof(CPUState, fpscr) },
2428 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002429 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002430 { "lr", offsetof(CPUState, lr) },
2431 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002432 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002433 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002434 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002435 { "msr", 0, &monitor_get_msr, },
2436 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002437 { "tbu", 0, &monitor_get_tbu, },
2438 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002439#if defined(TARGET_PPC64)
2440 /* Address space register */
2441 { "asr", offsetof(CPUState, asr) },
2442#endif
2443 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002444 { "sdr1", offsetof(CPUState, sdr1) },
2445 { "sr0", offsetof(CPUState, sr[0]) },
2446 { "sr1", offsetof(CPUState, sr[1]) },
2447 { "sr2", offsetof(CPUState, sr[2]) },
2448 { "sr3", offsetof(CPUState, sr[3]) },
2449 { "sr4", offsetof(CPUState, sr[4]) },
2450 { "sr5", offsetof(CPUState, sr[5]) },
2451 { "sr6", offsetof(CPUState, sr[6]) },
2452 { "sr7", offsetof(CPUState, sr[7]) },
2453 { "sr8", offsetof(CPUState, sr[8]) },
2454 { "sr9", offsetof(CPUState, sr[9]) },
2455 { "sr10", offsetof(CPUState, sr[10]) },
2456 { "sr11", offsetof(CPUState, sr[11]) },
2457 { "sr12", offsetof(CPUState, sr[12]) },
2458 { "sr13", offsetof(CPUState, sr[13]) },
2459 { "sr14", offsetof(CPUState, sr[14]) },
2460 { "sr15", offsetof(CPUState, sr[15]) },
2461 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002462#elif defined(TARGET_SPARC)
2463 { "g0", offsetof(CPUState, gregs[0]) },
2464 { "g1", offsetof(CPUState, gregs[1]) },
2465 { "g2", offsetof(CPUState, gregs[2]) },
2466 { "g3", offsetof(CPUState, gregs[3]) },
2467 { "g4", offsetof(CPUState, gregs[4]) },
2468 { "g5", offsetof(CPUState, gregs[5]) },
2469 { "g6", offsetof(CPUState, gregs[6]) },
2470 { "g7", offsetof(CPUState, gregs[7]) },
2471 { "o0", 0, monitor_get_reg },
2472 { "o1", 1, monitor_get_reg },
2473 { "o2", 2, monitor_get_reg },
2474 { "o3", 3, monitor_get_reg },
2475 { "o4", 4, monitor_get_reg },
2476 { "o5", 5, monitor_get_reg },
2477 { "o6", 6, monitor_get_reg },
2478 { "o7", 7, monitor_get_reg },
2479 { "l0", 8, monitor_get_reg },
2480 { "l1", 9, monitor_get_reg },
2481 { "l2", 10, monitor_get_reg },
2482 { "l3", 11, monitor_get_reg },
2483 { "l4", 12, monitor_get_reg },
2484 { "l5", 13, monitor_get_reg },
2485 { "l6", 14, monitor_get_reg },
2486 { "l7", 15, monitor_get_reg },
2487 { "i0", 16, monitor_get_reg },
2488 { "i1", 17, monitor_get_reg },
2489 { "i2", 18, monitor_get_reg },
2490 { "i3", 19, monitor_get_reg },
2491 { "i4", 20, monitor_get_reg },
2492 { "i5", 21, monitor_get_reg },
2493 { "i6", 22, monitor_get_reg },
2494 { "i7", 23, monitor_get_reg },
2495 { "pc", offsetof(CPUState, pc) },
2496 { "npc", offsetof(CPUState, npc) },
2497 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002498#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002499 { "psr", 0, &monitor_get_psr, },
2500 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002501#endif
bellarde95c8d52004-09-30 22:22:08 +00002502 { "tbr", offsetof(CPUState, tbr) },
2503 { "fsr", offsetof(CPUState, fsr) },
2504 { "f0", offsetof(CPUState, fpr[0]) },
2505 { "f1", offsetof(CPUState, fpr[1]) },
2506 { "f2", offsetof(CPUState, fpr[2]) },
2507 { "f3", offsetof(CPUState, fpr[3]) },
2508 { "f4", offsetof(CPUState, fpr[4]) },
2509 { "f5", offsetof(CPUState, fpr[5]) },
2510 { "f6", offsetof(CPUState, fpr[6]) },
2511 { "f7", offsetof(CPUState, fpr[7]) },
2512 { "f8", offsetof(CPUState, fpr[8]) },
2513 { "f9", offsetof(CPUState, fpr[9]) },
2514 { "f10", offsetof(CPUState, fpr[10]) },
2515 { "f11", offsetof(CPUState, fpr[11]) },
2516 { "f12", offsetof(CPUState, fpr[12]) },
2517 { "f13", offsetof(CPUState, fpr[13]) },
2518 { "f14", offsetof(CPUState, fpr[14]) },
2519 { "f15", offsetof(CPUState, fpr[15]) },
2520 { "f16", offsetof(CPUState, fpr[16]) },
2521 { "f17", offsetof(CPUState, fpr[17]) },
2522 { "f18", offsetof(CPUState, fpr[18]) },
2523 { "f19", offsetof(CPUState, fpr[19]) },
2524 { "f20", offsetof(CPUState, fpr[20]) },
2525 { "f21", offsetof(CPUState, fpr[21]) },
2526 { "f22", offsetof(CPUState, fpr[22]) },
2527 { "f23", offsetof(CPUState, fpr[23]) },
2528 { "f24", offsetof(CPUState, fpr[24]) },
2529 { "f25", offsetof(CPUState, fpr[25]) },
2530 { "f26", offsetof(CPUState, fpr[26]) },
2531 { "f27", offsetof(CPUState, fpr[27]) },
2532 { "f28", offsetof(CPUState, fpr[28]) },
2533 { "f29", offsetof(CPUState, fpr[29]) },
2534 { "f30", offsetof(CPUState, fpr[30]) },
2535 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002536#ifdef TARGET_SPARC64
2537 { "f32", offsetof(CPUState, fpr[32]) },
2538 { "f34", offsetof(CPUState, fpr[34]) },
2539 { "f36", offsetof(CPUState, fpr[36]) },
2540 { "f38", offsetof(CPUState, fpr[38]) },
2541 { "f40", offsetof(CPUState, fpr[40]) },
2542 { "f42", offsetof(CPUState, fpr[42]) },
2543 { "f44", offsetof(CPUState, fpr[44]) },
2544 { "f46", offsetof(CPUState, fpr[46]) },
2545 { "f48", offsetof(CPUState, fpr[48]) },
2546 { "f50", offsetof(CPUState, fpr[50]) },
2547 { "f52", offsetof(CPUState, fpr[52]) },
2548 { "f54", offsetof(CPUState, fpr[54]) },
2549 { "f56", offsetof(CPUState, fpr[56]) },
2550 { "f58", offsetof(CPUState, fpr[58]) },
2551 { "f60", offsetof(CPUState, fpr[60]) },
2552 { "f62", offsetof(CPUState, fpr[62]) },
2553 { "asi", offsetof(CPUState, asi) },
2554 { "pstate", offsetof(CPUState, pstate) },
2555 { "cansave", offsetof(CPUState, cansave) },
2556 { "canrestore", offsetof(CPUState, canrestore) },
2557 { "otherwin", offsetof(CPUState, otherwin) },
2558 { "wstate", offsetof(CPUState, wstate) },
2559 { "cleanwin", offsetof(CPUState, cleanwin) },
2560 { "fprs", offsetof(CPUState, fprs) },
2561#endif
bellard9307c4c2004-04-04 12:57:25 +00002562#endif
2563 { NULL },
2564};
2565
aliguori376253e2009-03-05 23:01:23 +00002566static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002567{
aliguori376253e2009-03-05 23:01:23 +00002568 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002569 longjmp(expr_env, 1);
2570}
2571
bellard6a00d602005-11-21 23:25:50 +00002572/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002573static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002574{
blueswir18662d652008-10-02 18:32:44 +00002575 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002576 void *ptr;
2577
bellard9307c4c2004-04-04 12:57:25 +00002578 for(md = monitor_defs; md->name != NULL; md++) {
2579 if (compare_cmd(name, md->name)) {
2580 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002581 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002582 } else {
bellard6a00d602005-11-21 23:25:50 +00002583 CPUState *env = mon_get_cpu();
2584 if (!env)
2585 return -2;
2586 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002587 switch(md->type) {
2588 case MD_I32:
2589 *pval = *(int32_t *)ptr;
2590 break;
2591 case MD_TLONG:
2592 *pval = *(target_long *)ptr;
2593 break;
2594 default:
2595 *pval = 0;
2596 break;
2597 }
bellard9307c4c2004-04-04 12:57:25 +00002598 }
2599 return 0;
2600 }
2601 }
2602 return -1;
2603}
2604
2605static void next(void)
2606{
Blue Swirl660f11b2009-07-31 21:16:51 +00002607 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002608 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002609 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002610 pch++;
2611 }
2612}
2613
aliguori376253e2009-03-05 23:01:23 +00002614static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002615
aliguori376253e2009-03-05 23:01:23 +00002616static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002617{
blueswir1c2efc952007-09-25 17:28:42 +00002618 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002619 char *p;
bellard6a00d602005-11-21 23:25:50 +00002620 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002621
2622 switch(*pch) {
2623 case '+':
2624 next();
aliguori376253e2009-03-05 23:01:23 +00002625 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002626 break;
2627 case '-':
2628 next();
aliguori376253e2009-03-05 23:01:23 +00002629 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002630 break;
2631 case '~':
2632 next();
aliguori376253e2009-03-05 23:01:23 +00002633 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002634 break;
2635 case '(':
2636 next();
aliguori376253e2009-03-05 23:01:23 +00002637 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002638 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002639 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002640 }
2641 next();
2642 break;
bellard81d09122004-07-14 17:21:37 +00002643 case '\'':
2644 pch++;
2645 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002646 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002647 n = *pch;
2648 pch++;
2649 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002650 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002651 next();
2652 break;
bellard9307c4c2004-04-04 12:57:25 +00002653 case '$':
2654 {
2655 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002656 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002657
bellard9307c4c2004-04-04 12:57:25 +00002658 pch++;
2659 q = buf;
2660 while ((*pch >= 'a' && *pch <= 'z') ||
2661 (*pch >= 'A' && *pch <= 'Z') ||
2662 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002663 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002664 if ((q - buf) < sizeof(buf) - 1)
2665 *q++ = *pch;
2666 pch++;
2667 }
blueswir1cd390082008-11-16 13:53:32 +00002668 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002669 pch++;
2670 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002671 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002672 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002673 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002674 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002675 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002676 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002677 }
2678 break;
2679 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002680 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002681 n = 0;
2682 break;
2683 default:
blueswir17743e582007-09-24 18:39:04 +00002684#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002685 n = strtoull(pch, &p, 0);
2686#else
bellard9307c4c2004-04-04 12:57:25 +00002687 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002688#endif
bellard9307c4c2004-04-04 12:57:25 +00002689 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002690 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002691 }
2692 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002693 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002694 pch++;
2695 break;
2696 }
2697 return n;
2698}
2699
2700
aliguori376253e2009-03-05 23:01:23 +00002701static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002702{
blueswir1c2efc952007-09-25 17:28:42 +00002703 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002704 int op;
ths3b46e622007-09-17 08:09:54 +00002705
aliguori376253e2009-03-05 23:01:23 +00002706 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002707 for(;;) {
2708 op = *pch;
2709 if (op != '*' && op != '/' && op != '%')
2710 break;
2711 next();
aliguori376253e2009-03-05 23:01:23 +00002712 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002713 switch(op) {
2714 default:
2715 case '*':
2716 val *= val2;
2717 break;
2718 case '/':
2719 case '%':
ths5fafdf22007-09-16 21:08:06 +00002720 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002721 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002722 if (op == '/')
2723 val /= val2;
2724 else
2725 val %= val2;
2726 break;
2727 }
2728 }
2729 return val;
2730}
2731
aliguori376253e2009-03-05 23:01:23 +00002732static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002733{
blueswir1c2efc952007-09-25 17:28:42 +00002734 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002735 int op;
bellard9307c4c2004-04-04 12:57:25 +00002736
aliguori376253e2009-03-05 23:01:23 +00002737 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002738 for(;;) {
2739 op = *pch;
2740 if (op != '&' && op != '|' && op != '^')
2741 break;
2742 next();
aliguori376253e2009-03-05 23:01:23 +00002743 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002744 switch(op) {
2745 default:
2746 case '&':
2747 val &= val2;
2748 break;
2749 case '|':
2750 val |= val2;
2751 break;
2752 case '^':
2753 val ^= val2;
2754 break;
2755 }
2756 }
2757 return val;
2758}
2759
aliguori376253e2009-03-05 23:01:23 +00002760static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002761{
blueswir1c2efc952007-09-25 17:28:42 +00002762 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002763 int op;
bellard9307c4c2004-04-04 12:57:25 +00002764
aliguori376253e2009-03-05 23:01:23 +00002765 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002766 for(;;) {
2767 op = *pch;
2768 if (op != '+' && op != '-')
2769 break;
2770 next();
aliguori376253e2009-03-05 23:01:23 +00002771 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002772 if (op == '+')
2773 val += val2;
2774 else
2775 val -= val2;
2776 }
2777 return val;
2778}
2779
aliguori376253e2009-03-05 23:01:23 +00002780static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002781{
2782 pch = *pp;
2783 if (setjmp(expr_env)) {
2784 *pp = pch;
2785 return -1;
2786 }
blueswir1cd390082008-11-16 13:53:32 +00002787 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002788 pch++;
aliguori376253e2009-03-05 23:01:23 +00002789 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002790 *pp = pch;
2791 return 0;
2792}
2793
2794static int get_str(char *buf, int buf_size, const char **pp)
2795{
2796 const char *p;
2797 char *q;
2798 int c;
2799
bellard81d09122004-07-14 17:21:37 +00002800 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002801 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002802 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002803 p++;
2804 if (*p == '\0') {
2805 fail:
bellard81d09122004-07-14 17:21:37 +00002806 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002807 *pp = p;
2808 return -1;
2809 }
bellard9307c4c2004-04-04 12:57:25 +00002810 if (*p == '\"') {
2811 p++;
2812 while (*p != '\0' && *p != '\"') {
2813 if (*p == '\\') {
2814 p++;
2815 c = *p++;
2816 switch(c) {
2817 case 'n':
2818 c = '\n';
2819 break;
2820 case 'r':
2821 c = '\r';
2822 break;
2823 case '\\':
2824 case '\'':
2825 case '\"':
2826 break;
2827 default:
2828 qemu_printf("unsupported escape code: '\\%c'\n", c);
2829 goto fail;
2830 }
2831 if ((q - buf) < buf_size - 1) {
2832 *q++ = c;
2833 }
2834 } else {
2835 if ((q - buf) < buf_size - 1) {
2836 *q++ = *p;
2837 }
2838 p++;
2839 }
2840 }
2841 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002842 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002843 goto fail;
2844 }
2845 p++;
2846 } else {
blueswir1cd390082008-11-16 13:53:32 +00002847 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002848 if ((q - buf) < buf_size - 1) {
2849 *q++ = *p;
2850 }
2851 p++;
2852 }
bellard9307c4c2004-04-04 12:57:25 +00002853 }
bellard81d09122004-07-14 17:21:37 +00002854 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002855 *pp = p;
2856 return 0;
2857}
2858
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002859/*
2860 * Store the command-name in cmdname, and return a pointer to
2861 * the remaining of the command string.
2862 */
2863static const char *get_command_name(const char *cmdline,
2864 char *cmdname, size_t nlen)
2865{
2866 size_t len;
2867 const char *p, *pstart;
2868
2869 p = cmdline;
2870 while (qemu_isspace(*p))
2871 p++;
2872 if (*p == '\0')
2873 return NULL;
2874 pstart = p;
2875 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2876 p++;
2877 len = p - pstart;
2878 if (len > nlen - 1)
2879 len = nlen - 1;
2880 memcpy(cmdname, pstart, len);
2881 cmdname[len] = '\0';
2882 return p;
2883}
2884
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002885/**
2886 * Read key of 'type' into 'key' and return the current
2887 * 'type' pointer.
2888 */
2889static char *key_get_info(const char *type, char **key)
2890{
2891 size_t len;
2892 char *p, *str;
2893
2894 if (*type == ',')
2895 type++;
2896
2897 p = strchr(type, ':');
2898 if (!p) {
2899 *key = NULL;
2900 return NULL;
2901 }
2902 len = p - type;
2903
2904 str = qemu_malloc(len + 1);
2905 memcpy(str, type, len);
2906 str[len] = '\0';
2907
2908 *key = str;
2909 return ++p;
2910}
2911
bellard9307c4c2004-04-04 12:57:25 +00002912static int default_fmt_format = 'x';
2913static int default_fmt_size = 4;
2914
2915#define MAX_ARGS 16
2916
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002917static int is_valid_option(const char *c, const char *typestr)
2918{
2919 char option[3];
2920
2921 option[0] = '-';
2922 option[1] = *c;
2923 option[2] = '\0';
2924
2925 typestr = strstr(typestr, option);
2926 return (typestr != NULL);
2927}
2928
Anthony Liguoric227f092009-10-01 16:12:16 -05002929static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002930 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002931 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00002932{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002933 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03002934 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05002935 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002936 char cmdname[256];
2937 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002938 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00002939
2940#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002941 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002942#endif
ths3b46e622007-09-17 08:09:54 +00002943
bellard9307c4c2004-04-04 12:57:25 +00002944 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002945 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2946 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002947 return NULL;
ths3b46e622007-09-17 08:09:54 +00002948
bellard9307c4c2004-04-04 12:57:25 +00002949 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002950 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002951 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002952 break;
bellard9dc39cb2004-03-14 21:38:27 +00002953 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002954
2955 if (cmd->name == NULL) {
2956 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002957 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002958 }
bellard9307c4c2004-04-04 12:57:25 +00002959
bellard9307c4c2004-04-04 12:57:25 +00002960 /* parse the parameters */
2961 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002962 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002963 typestr = key_get_info(typestr, &key);
2964 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002965 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002966 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002967 typestr++;
2968 switch(c) {
2969 case 'F':
bellard81d09122004-07-14 17:21:37 +00002970 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002971 case 's':
2972 {
2973 int ret;
ths3b46e622007-09-17 08:09:54 +00002974
blueswir1cd390082008-11-16 13:53:32 +00002975 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002976 p++;
2977 if (*typestr == '?') {
2978 typestr++;
2979 if (*p == '\0') {
2980 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002981 break;
bellard9307c4c2004-04-04 12:57:25 +00002982 }
2983 }
2984 ret = get_str(buf, sizeof(buf), &p);
2985 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002986 switch(c) {
2987 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002988 monitor_printf(mon, "%s: filename expected\n",
2989 cmdname);
bellard81d09122004-07-14 17:21:37 +00002990 break;
2991 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002992 monitor_printf(mon, "%s: block device name expected\n",
2993 cmdname);
bellard81d09122004-07-14 17:21:37 +00002994 break;
2995 default:
aliguori376253e2009-03-05 23:01:23 +00002996 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002997 break;
2998 }
bellard9307c4c2004-04-04 12:57:25 +00002999 goto fail;
3000 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003001 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003002 }
3003 break;
3004 case '/':
3005 {
3006 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003007
blueswir1cd390082008-11-16 13:53:32 +00003008 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003009 p++;
3010 if (*p == '/') {
3011 /* format found */
3012 p++;
3013 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003014 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003015 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003016 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003017 count = count * 10 + (*p - '0');
3018 p++;
3019 }
3020 }
3021 size = -1;
3022 format = -1;
3023 for(;;) {
3024 switch(*p) {
3025 case 'o':
3026 case 'd':
3027 case 'u':
3028 case 'x':
3029 case 'i':
3030 case 'c':
3031 format = *p++;
3032 break;
3033 case 'b':
3034 size = 1;
3035 p++;
3036 break;
3037 case 'h':
3038 size = 2;
3039 p++;
3040 break;
3041 case 'w':
3042 size = 4;
3043 p++;
3044 break;
3045 case 'g':
3046 case 'L':
3047 size = 8;
3048 p++;
3049 break;
3050 default:
3051 goto next;
3052 }
3053 }
3054 next:
blueswir1cd390082008-11-16 13:53:32 +00003055 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003056 monitor_printf(mon, "invalid char in format: '%c'\n",
3057 *p);
bellard9307c4c2004-04-04 12:57:25 +00003058 goto fail;
3059 }
bellard9307c4c2004-04-04 12:57:25 +00003060 if (format < 0)
3061 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003062 if (format != 'i') {
3063 /* for 'i', not specifying a size gives -1 as size */
3064 if (size < 0)
3065 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003066 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003067 }
bellard9307c4c2004-04-04 12:57:25 +00003068 default_fmt_format = format;
3069 } else {
3070 count = 1;
3071 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003072 if (format != 'i') {
3073 size = default_fmt_size;
3074 } else {
3075 size = -1;
3076 }
bellard9307c4c2004-04-04 12:57:25 +00003077 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003078 qdict_put(qdict, "count", qint_from_int(count));
3079 qdict_put(qdict, "format", qint_from_int(format));
3080 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003081 }
3082 break;
3083 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003084 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00003085 {
blueswir1c2efc952007-09-25 17:28:42 +00003086 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003087
blueswir1cd390082008-11-16 13:53:32 +00003088 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003089 p++;
bellard34405572004-06-08 00:55:58 +00003090 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003091 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003092 if (*p == '\0') {
3093 typestr++;
3094 break;
3095 }
bellard34405572004-06-08 00:55:58 +00003096 } else {
3097 if (*p == '.') {
3098 p++;
blueswir1cd390082008-11-16 13:53:32 +00003099 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003100 p++;
bellard34405572004-06-08 00:55:58 +00003101 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003102 typestr++;
3103 break;
bellard34405572004-06-08 00:55:58 +00003104 }
3105 }
bellard13224a82006-07-14 22:03:35 +00003106 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003107 }
aliguori376253e2009-03-05 23:01:23 +00003108 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003109 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003110 /* Check if 'i' is greater than 32-bit */
3111 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3112 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3113 monitor_printf(mon, "integer is for 32-bit values\n");
3114 goto fail;
3115 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003116 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003117 }
3118 break;
3119 case '-':
3120 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003121 const char *tmp = p;
3122 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003123 /* option */
ths3b46e622007-09-17 08:09:54 +00003124
bellard9307c4c2004-04-04 12:57:25 +00003125 c = *typestr++;
3126 if (c == '\0')
3127 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003128 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003129 p++;
3130 has_option = 0;
3131 if (*p == '-') {
3132 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003133 if(c != *p) {
3134 if(!is_valid_option(p, typestr)) {
3135
3136 monitor_printf(mon, "%s: unsupported option -%c\n",
3137 cmdname, *p);
3138 goto fail;
3139 } else {
3140 skip_key = 1;
3141 }
bellard9307c4c2004-04-04 12:57:25 +00003142 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003143 if(skip_key) {
3144 p = tmp;
3145 } else {
3146 p++;
3147 has_option = 1;
3148 }
bellard9307c4c2004-04-04 12:57:25 +00003149 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003150 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003151 }
3152 break;
3153 default:
3154 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003155 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003156 goto fail;
3157 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003158 qemu_free(key);
3159 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003160 }
3161 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003162 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003163 p++;
3164 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003165 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3166 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003167 goto fail;
3168 }
3169
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003170 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003171
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003172fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003173 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003174 return NULL;
3175}
3176
Luiz Capitulino8204a912009-11-18 23:05:31 -02003177static void monitor_print_error(Monitor *mon)
3178{
3179 qerror_print(mon->error);
3180 QDECREF(mon->error);
3181 mon->error = NULL;
3182}
3183
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003184static void monitor_handle_command(Monitor *mon, const char *cmdline)
3185{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003186 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003187 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003188
3189 qdict = qdict_new();
3190
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003191 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003192 if (!cmd)
3193 goto out;
3194
3195 qemu_errors_to_mon(mon);
3196
3197 if (monitor_handler_ported(cmd)) {
3198 QObject *data = NULL;
3199
3200 cmd->mhandler.cmd_new(mon, qdict, &data);
3201 if (data)
3202 cmd->user_print(mon, data);
3203
3204 qobject_decref(data);
3205 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003206 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003207 }
3208
Luiz Capitulino8204a912009-11-18 23:05:31 -02003209 if (monitor_has_error(mon))
3210 monitor_print_error(mon);
3211
3212 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003213
3214out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003215 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003216}
3217
bellard81d09122004-07-14 17:21:37 +00003218static void cmd_completion(const char *name, const char *list)
3219{
3220 const char *p, *pstart;
3221 char cmd[128];
3222 int len;
3223
3224 p = list;
3225 for(;;) {
3226 pstart = p;
3227 p = strchr(p, '|');
3228 if (!p)
3229 p = pstart + strlen(pstart);
3230 len = p - pstart;
3231 if (len > sizeof(cmd) - 2)
3232 len = sizeof(cmd) - 2;
3233 memcpy(cmd, pstart, len);
3234 cmd[len] = '\0';
3235 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003236 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003237 }
3238 if (*p == '\0')
3239 break;
3240 p++;
3241 }
3242}
3243
3244static void file_completion(const char *input)
3245{
3246 DIR *ffs;
3247 struct dirent *d;
3248 char path[1024];
3249 char file[1024], file_prefix[1024];
3250 int input_path_len;
3251 const char *p;
3252
ths5fafdf22007-09-16 21:08:06 +00003253 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003254 if (!p) {
3255 input_path_len = 0;
3256 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003257 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003258 } else {
3259 input_path_len = p - input + 1;
3260 memcpy(path, input, input_path_len);
3261 if (input_path_len > sizeof(path) - 1)
3262 input_path_len = sizeof(path) - 1;
3263 path[input_path_len] = '\0';
3264 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3265 }
3266#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003267 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3268 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003269#endif
3270 ffs = opendir(path);
3271 if (!ffs)
3272 return;
3273 for(;;) {
3274 struct stat sb;
3275 d = readdir(ffs);
3276 if (!d)
3277 break;
3278 if (strstart(d->d_name, file_prefix, NULL)) {
3279 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003280 if (input_path_len < sizeof(file))
3281 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3282 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003283 /* stat the file to find out if it's a directory.
3284 * In that case add a slash to speed up typing long paths
3285 */
3286 stat(file, &sb);
3287 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003288 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003289 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003290 }
3291 }
3292 closedir(ffs);
3293}
3294
aliguori51de9762009-03-05 23:00:43 +00003295static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003296{
aliguori51de9762009-03-05 23:00:43 +00003297 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003298 const char *input = opaque;
3299
3300 if (input[0] == '\0' ||
3301 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003302 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003303 }
3304}
3305
3306/* NOTE: this parser is an approximate form of the real command parser */
3307static void parse_cmdline(const char *cmdline,
3308 int *pnb_args, char **args)
3309{
3310 const char *p;
3311 int nb_args, ret;
3312 char buf[1024];
3313
3314 p = cmdline;
3315 nb_args = 0;
3316 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003317 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003318 p++;
3319 if (*p == '\0')
3320 break;
3321 if (nb_args >= MAX_ARGS)
3322 break;
3323 ret = get_str(buf, sizeof(buf), &p);
3324 args[nb_args] = qemu_strdup(buf);
3325 nb_args++;
3326 if (ret < 0)
3327 break;
3328 }
3329 *pnb_args = nb_args;
3330}
3331
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003332static const char *next_arg_type(const char *typestr)
3333{
3334 const char *p = strchr(typestr, ':');
3335 return (p != NULL ? ++p : typestr);
3336}
3337
aliguori4c36ba32009-03-05 23:01:37 +00003338static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003339{
3340 const char *cmdname;
3341 char *args[MAX_ARGS];
3342 int nb_args, i, len;
3343 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003344 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003345 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003346
3347 parse_cmdline(cmdline, &nb_args, args);
3348#ifdef DEBUG_COMPLETION
3349 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003350 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003351 }
3352#endif
3353
3354 /* if the line ends with a space, it means we want to complete the
3355 next arg */
3356 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003357 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003358 if (nb_args >= MAX_ARGS)
3359 return;
3360 args[nb_args++] = qemu_strdup("");
3361 }
3362 if (nb_args <= 1) {
3363 /* command completion */
3364 if (nb_args == 0)
3365 cmdname = "";
3366 else
3367 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003368 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003369 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003370 cmd_completion(cmdname, cmd->name);
3371 }
3372 } else {
3373 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003374 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003375 if (compare_cmd(args[0], cmd->name))
3376 goto found;
3377 }
3378 return;
3379 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003380 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003381 for(i = 0; i < nb_args - 2; i++) {
3382 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003383 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003384 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003385 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003386 }
3387 }
3388 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003389 if (*ptype == '-' && ptype[1] != '\0') {
3390 ptype += 2;
3391 }
bellard81d09122004-07-14 17:21:37 +00003392 switch(*ptype) {
3393 case 'F':
3394 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003395 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003396 file_completion(str);
3397 break;
3398 case 'B':
3399 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003400 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003401 bdrv_iterate(block_completion_it, (void *)str);
3402 break;
bellard7fe48482004-10-09 18:08:01 +00003403 case 's':
3404 /* XXX: more generic ? */
3405 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003406 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003407 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3408 cmd_completion(str, cmd->name);
3409 }
bellard64866c32006-05-07 18:03:31 +00003410 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003411 char *sep = strrchr(str, '-');
3412 if (sep)
3413 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003414 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003415 for(key = key_defs; key->name != NULL; key++) {
3416 cmd_completion(str, key->name);
3417 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003418 } else if (!strcmp(cmd->name, "help|?")) {
3419 readline_set_completion_index(cur_mon->rs, strlen(str));
3420 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3421 cmd_completion(str, cmd->name);
3422 }
bellard7fe48482004-10-09 18:08:01 +00003423 }
3424 break;
bellard81d09122004-07-14 17:21:37 +00003425 default:
3426 break;
3427 }
3428 }
3429 for(i = 0; i < nb_args; i++)
3430 qemu_free(args[i]);
3431}
3432
aliguori731b0362009-03-05 23:01:42 +00003433static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003434{
aliguori731b0362009-03-05 23:01:42 +00003435 Monitor *mon = opaque;
3436
3437 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003438}
3439
aliguori731b0362009-03-05 23:01:42 +00003440static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003441{
aliguori731b0362009-03-05 23:01:42 +00003442 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003443 int i;
aliguori376253e2009-03-05 23:01:23 +00003444
aliguori731b0362009-03-05 23:01:42 +00003445 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003446
aliguoricde76ee2009-03-05 23:01:51 +00003447 if (cur_mon->rs) {
3448 for (i = 0; i < size; i++)
3449 readline_handle_byte(cur_mon->rs, buf[i]);
3450 } else {
3451 if (size == 0 || buf[size - 1] != 0)
3452 monitor_printf(cur_mon, "corrupted command\n");
3453 else
3454 monitor_handle_command(cur_mon, (char *)buf);
3455 }
aliguori731b0362009-03-05 23:01:42 +00003456
3457 cur_mon = old_mon;
3458}
aliguorid8f44602008-10-06 13:52:44 +00003459
aliguori376253e2009-03-05 23:01:23 +00003460static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003461{
aliguori731b0362009-03-05 23:01:42 +00003462 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003463 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003464 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003465}
3466
aliguoricde76ee2009-03-05 23:01:51 +00003467int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003468{
aliguoricde76ee2009-03-05 23:01:51 +00003469 if (!mon->rs)
3470 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003471 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003472 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003473}
3474
aliguori376253e2009-03-05 23:01:23 +00003475void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003476{
aliguoricde76ee2009-03-05 23:01:51 +00003477 if (!mon->rs)
3478 return;
aliguori731b0362009-03-05 23:01:42 +00003479 if (--mon->suspend_cnt == 0)
3480 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003481}
3482
aliguori731b0362009-03-05 23:01:42 +00003483static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003484{
aliguori376253e2009-03-05 23:01:23 +00003485 Monitor *mon = opaque;
3486
aliguori2724b182009-03-05 23:01:47 +00003487 switch (event) {
3488 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003489 mon->mux_out = 0;
3490 if (mon->reset_seen) {
3491 readline_restart(mon->rs);
3492 monitor_resume(mon);
3493 monitor_flush(mon);
3494 } else {
3495 mon->suspend_cnt = 0;
3496 }
aliguori2724b182009-03-05 23:01:47 +00003497 break;
ths86e94de2007-01-05 22:01:59 +00003498
aliguori2724b182009-03-05 23:01:47 +00003499 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003500 if (mon->reset_seen) {
3501 if (mon->suspend_cnt == 0) {
3502 monitor_printf(mon, "\n");
3503 }
3504 monitor_flush(mon);
3505 monitor_suspend(mon);
3506 } else {
3507 mon->suspend_cnt++;
3508 }
3509 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00003510 break;
3511
Amit Shahb6b8df52009-10-07 18:31:16 +05303512 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003513 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3514 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003515 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00003516 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003517 }
3518 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00003519 break;
3520 }
ths86e94de2007-01-05 22:01:59 +00003521}
3522
aliguori76655d62009-03-06 20:27:37 +00003523
3524/*
3525 * Local variables:
3526 * c-indent-level: 4
3527 * c-basic-offset: 4
3528 * tab-width: 8
3529 * End:
3530 */
3531
aliguori731b0362009-03-05 23:01:42 +00003532void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003533{
aliguori731b0362009-03-05 23:01:42 +00003534 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003535 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003536
3537 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003538 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003539 is_first_init = 0;
3540 }
aliguori87127162009-03-05 23:01:29 +00003541
3542 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003543
aliguori87127162009-03-05 23:01:29 +00003544 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003545 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00003546 if (flags & MONITOR_USE_READLINE) {
3547 mon->rs = readline_init(mon, monitor_find_completion);
3548 monitor_read_command(mon, 0);
3549 }
aliguori87127162009-03-05 23:01:29 +00003550
aliguori731b0362009-03-05 23:01:42 +00003551 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3552 mon);
aliguori87127162009-03-05 23:01:29 +00003553
Blue Swirl72cf2d42009-09-12 07:36:22 +00003554 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003555 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003556 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003557}
3558
aliguori376253e2009-03-05 23:01:23 +00003559static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003560{
aliguoribb5fc202009-03-05 23:01:15 +00003561 BlockDriverState *bs = opaque;
3562 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003563
aliguoribb5fc202009-03-05 23:01:15 +00003564 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003565 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003566 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003567 }
aliguori731b0362009-03-05 23:01:42 +00003568 if (mon->password_completion_cb)
3569 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003570
aliguori731b0362009-03-05 23:01:42 +00003571 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003572}
aliguoric0f4ce72009-03-05 23:01:01 +00003573
aliguori376253e2009-03-05 23:01:23 +00003574void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003575 BlockDriverCompletionFunc *completion_cb,
3576 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003577{
aliguoricde76ee2009-03-05 23:01:51 +00003578 int err;
3579
aliguoribb5fc202009-03-05 23:01:15 +00003580 if (!bdrv_key_required(bs)) {
3581 if (completion_cb)
3582 completion_cb(opaque, 0);
3583 return;
3584 }
aliguoric0f4ce72009-03-05 23:01:01 +00003585
aliguori376253e2009-03-05 23:01:23 +00003586 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3587 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003588
aliguori731b0362009-03-05 23:01:42 +00003589 mon->password_completion_cb = completion_cb;
3590 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003591
aliguoricde76ee2009-03-05 23:01:51 +00003592 err = monitor_read_password(mon, bdrv_password_cb, bs);
3593
3594 if (err && completion_cb)
3595 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003596}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003597
3598typedef struct QemuErrorSink QemuErrorSink;
3599struct QemuErrorSink {
3600 enum {
3601 ERR_SINK_FILE,
3602 ERR_SINK_MONITOR,
3603 } dest;
3604 union {
3605 FILE *fp;
3606 Monitor *mon;
3607 };
3608 QemuErrorSink *previous;
3609};
3610
Blue Swirl528e93a2009-08-31 15:14:40 +00003611static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003612
3613void qemu_errors_to_file(FILE *fp)
3614{
3615 QemuErrorSink *sink;
3616
3617 sink = qemu_mallocz(sizeof(*sink));
3618 sink->dest = ERR_SINK_FILE;
3619 sink->fp = fp;
3620 sink->previous = qemu_error_sink;
3621 qemu_error_sink = sink;
3622}
3623
3624void qemu_errors_to_mon(Monitor *mon)
3625{
3626 QemuErrorSink *sink;
3627
3628 sink = qemu_mallocz(sizeof(*sink));
3629 sink->dest = ERR_SINK_MONITOR;
3630 sink->mon = mon;
3631 sink->previous = qemu_error_sink;
3632 qemu_error_sink = sink;
3633}
3634
3635void qemu_errors_to_previous(void)
3636{
3637 QemuErrorSink *sink;
3638
3639 assert(qemu_error_sink != NULL);
3640 sink = qemu_error_sink;
3641 qemu_error_sink = sink->previous;
3642 qemu_free(sink);
3643}
3644
3645void qemu_error(const char *fmt, ...)
3646{
3647 va_list args;
3648
3649 assert(qemu_error_sink != NULL);
3650 switch (qemu_error_sink->dest) {
3651 case ERR_SINK_FILE:
3652 va_start(args, fmt);
3653 vfprintf(qemu_error_sink->fp, fmt, args);
3654 va_end(args);
3655 break;
3656 case ERR_SINK_MONITOR:
3657 va_start(args, fmt);
3658 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3659 va_end(args);
3660 break;
3661 }
3662}
Luiz Capitulino8204a912009-11-18 23:05:31 -02003663
3664void qemu_error_internal(const char *file, int linenr, const char *func,
3665 const char *fmt, ...)
3666{
3667 va_list va;
3668 QError *qerror;
3669
3670 assert(qemu_error_sink != NULL);
3671
3672 va_start(va, fmt);
3673 qerror = qerror_from_info(file, linenr, func, fmt, &va);
3674 va_end(va);
3675
3676 switch (qemu_error_sink->dest) {
3677 case ERR_SINK_FILE:
3678 qerror_print(qerror);
3679 QDECREF(qerror);
3680 break;
3681 case ERR_SINK_MONITOR:
3682 assert(qemu_error_sink->mon->error == NULL);
3683 qemu_error_sink->mon->error = qerror;
3684 break;
3685 }
3686}