blob: 57ce68b6d13e9243b93a3c6c8ea4ef3201b235e3 [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"
pbrook87ecb682007-11-17 17:14:51 +000032#include "gdbstub.h"
33#include "net.h"
34#include "qemu-char.h"
35#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000036#include "monitor.h"
37#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000038#include "console.h"
39#include "block.h"
40#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000041#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000042#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000043#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000044#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000045#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000046#include "acl.h"
ths6a5bd302007-12-03 17:05:38 +000047
bellard9dc39cb2004-03-14 21:38:27 +000048//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000049//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000050
bellard9307c4c2004-04-04 12:57:25 +000051/*
52 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000053 *
bellard9307c4c2004-04-04 12:57:25 +000054 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000055 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000056 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000057 * 'i' 32 bit integer
58 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000059 * '/' optional gdb-like print format (like "/10x")
60 *
61 * '?' optional type (for 'F', 's' and 'i')
62 *
63 */
64
aliguori376253e2009-03-05 23:01:23 +000065typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000066 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000067 const char *args_type;
blueswir1a5f1b962008-08-17 20:21:51 +000068 void *handler;
bellard9dc39cb2004-03-14 21:38:27 +000069 const char *params;
70 const char *help;
aliguori376253e2009-03-05 23:01:23 +000071} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000072
Mark McLoughlinf07918f2009-07-22 09:11:40 +010073/* file descriptors passed via SCM_RIGHTS */
74typedef struct mon_fd_t mon_fd_t;
75struct mon_fd_t {
76 char *name;
77 int fd;
78 LIST_ENTRY(mon_fd_t) next;
79};
80
aliguori87127162009-03-05 23:01:29 +000081struct Monitor {
82 CharDriverState *chr;
aliguori731b0362009-03-05 23:01:42 +000083 int flags;
84 int suspend_cnt;
85 uint8_t outbuf[1024];
86 int outbuf_index;
87 ReadLineState *rs;
88 CPUState *mon_cpu;
89 BlockDriverCompletionFunc *password_completion_cb;
90 void *password_opaque;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010091 LIST_HEAD(,mon_fd_t) fds;
aliguori87127162009-03-05 23:01:29 +000092 LIST_ENTRY(Monitor) entry;
93};
94
95static LIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +000096
aliguori376253e2009-03-05 23:01:23 +000097static const mon_cmd_t mon_cmds[];
98static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +000099
aliguori87127162009-03-05 23:01:29 +0000100Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000101
aliguori731b0362009-03-05 23:01:42 +0000102static void monitor_command_cb(Monitor *mon, const char *cmdline,
103 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000104
aliguori731b0362009-03-05 23:01:42 +0000105static void monitor_read_command(Monitor *mon, int show_prompt)
106{
107 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
108 if (show_prompt)
109 readline_show_prompt(mon->rs);
110}
bellard6a00d602005-11-21 23:25:50 +0000111
aliguoricde76ee2009-03-05 23:01:51 +0000112static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
113 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000114{
aliguoricde76ee2009-03-05 23:01:51 +0000115 if (mon->rs) {
116 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
117 /* prompt is printed on return from the command handler */
118 return 0;
119 } else {
120 monitor_printf(mon, "terminal does not support password prompting\n");
121 return -ENOTTY;
122 }
aliguoribb5fc202009-03-05 23:01:15 +0000123}
124
aliguori376253e2009-03-05 23:01:23 +0000125void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000126{
aliguori731b0362009-03-05 23:01:42 +0000127 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
128 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
129 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000130 }
131}
132
133/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000134static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000135{
ths60fe76f2007-12-16 03:02:09 +0000136 char c;
aliguori731b0362009-03-05 23:01:42 +0000137
138 if (!mon)
139 return;
140
bellard7e2515e2004-08-01 21:52:19 +0000141 for(;;) {
142 c = *str++;
143 if (c == '\0')
144 break;
bellard7ba12602006-07-14 20:26:42 +0000145 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000146 mon->outbuf[mon->outbuf_index++] = '\r';
147 mon->outbuf[mon->outbuf_index++] = c;
148 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
149 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000150 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000151 }
152}
153
aliguori376253e2009-03-05 23:01:23 +0000154void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000155{
156 char buf[4096];
157 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000158 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000159}
160
aliguori376253e2009-03-05 23:01:23 +0000161void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000162{
163 va_list ap;
164 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000165 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000166 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000167}
168
aliguori376253e2009-03-05 23:01:23 +0000169void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000170{
171 int i;
172
173 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000174 switch (filename[i]) {
175 case ' ':
176 case '"':
177 case '\\':
178 monitor_printf(mon, "\\%c", filename[i]);
179 break;
180 case '\t':
181 monitor_printf(mon, "\\t");
182 break;
183 case '\r':
184 monitor_printf(mon, "\\r");
185 break;
186 case '\n':
187 monitor_printf(mon, "\\n");
188 break;
189 default:
190 monitor_printf(mon, "%c", filename[i]);
191 break;
192 }
thsfef30742006-12-22 14:11:32 +0000193 }
194}
195
bellard7fe48482004-10-09 18:08:01 +0000196static int monitor_fprintf(FILE *stream, const char *fmt, ...)
197{
198 va_list ap;
199 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000200 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000201 va_end(ap);
202 return 0;
203}
204
bellard9dc39cb2004-03-14 21:38:27 +0000205static int compare_cmd(const char *name, const char *list)
206{
207 const char *p, *pstart;
208 int len;
209 len = strlen(name);
210 p = list;
211 for(;;) {
212 pstart = p;
213 p = strchr(p, '|');
214 if (!p)
215 p = pstart + strlen(pstart);
216 if ((p - pstart) == len && !memcmp(pstart, name, len))
217 return 1;
218 if (*p == '\0')
219 break;
220 p++;
221 }
222 return 0;
223}
224
aliguori376253e2009-03-05 23:01:23 +0000225static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
226 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000227{
aliguori376253e2009-03-05 23:01:23 +0000228 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000229
230 for(cmd = cmds; cmd->name != NULL; cmd++) {
231 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000232 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
233 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000234 }
235}
236
aliguori376253e2009-03-05 23:01:23 +0000237static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000238{
239 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000240 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000241 } else {
aliguori376253e2009-03-05 23:01:23 +0000242 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000243 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000244 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000245 monitor_printf(mon, "Log items (comma separated):\n");
246 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000247 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000248 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000249 }
250 }
bellard9dc39cb2004-03-14 21:38:27 +0000251 }
252}
253
aliguori376253e2009-03-05 23:01:23 +0000254static void do_commit(Monitor *mon, const char *device)
bellard9dc39cb2004-03-14 21:38:27 +0000255{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200256 int all_devices;
257 DriveInfo *dinfo;
balrog2dc7b602007-05-24 18:53:22 +0000258
bellard7954c732006-08-01 15:52:40 +0000259 all_devices = !strcmp(device, "all");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200260 TAILQ_FOREACH(dinfo, &drives, next) {
261 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300262 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200263 continue;
264 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000265 }
266}
267
aliguori376253e2009-03-05 23:01:23 +0000268static void do_info(Monitor *mon, const char *item)
bellard9dc39cb2004-03-14 21:38:27 +0000269{
aliguori376253e2009-03-05 23:01:23 +0000270 const mon_cmd_t *cmd;
271 void (*handler)(Monitor *);
bellard9dc39cb2004-03-14 21:38:27 +0000272
bellard9307c4c2004-04-04 12:57:25 +0000273 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000274 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000275 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000276 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000277 goto found;
278 }
279 help:
aliguori376253e2009-03-05 23:01:23 +0000280 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000281 return;
282 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000283 handler = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +0000284 handler(mon);
bellard9dc39cb2004-03-14 21:38:27 +0000285}
286
aliguori376253e2009-03-05 23:01:23 +0000287static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000288{
pbrook4a19f1e2009-04-07 23:17:49 +0000289 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000290}
291
aliguori376253e2009-03-05 23:01:23 +0000292static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000293{
294 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000295 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000296}
297
aurel32bf4f74c2008-12-18 22:42:34 +0000298#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000299static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000300{
aliguori376253e2009-03-05 23:01:23 +0000301 monitor_printf(mon, "HPET is %s by QEMU\n",
302 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000303}
aurel32bf4f74c2008-12-18 22:42:34 +0000304#endif
aliguori16b29ae2008-12-17 23:28:44 +0000305
aliguori376253e2009-03-05 23:01:23 +0000306static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000307{
aliguori376253e2009-03-05 23:01:23 +0000308 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
309 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
310 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
311 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
312 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000313}
314
bellard6a00d602005-11-21 23:25:50 +0000315/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000316static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000317{
318 CPUState *env;
319
320 for(env = first_cpu; env != NULL; env = env->next_cpu) {
321 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000322 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000323 return 0;
324 }
325 }
326 return -1;
327}
328
pbrook9596ebb2007-11-18 01:44:38 +0000329static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000330{
aliguori731b0362009-03-05 23:01:42 +0000331 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000332 mon_set_cpu(0);
333 }
aliguorid1546152009-03-12 20:12:57 +0000334 cpu_synchronize_state(cur_mon->mon_cpu, 0);
aliguori731b0362009-03-05 23:01:42 +0000335 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000336}
337
aliguori376253e2009-03-05 23:01:23 +0000338static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000339{
bellard6a00d602005-11-21 23:25:50 +0000340 CPUState *env;
341 env = mon_get_cpu();
342 if (!env)
343 return;
bellard9307c4c2004-04-04 12:57:25 +0000344#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000345 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000346 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000347#else
aliguori376253e2009-03-05 23:01:23 +0000348 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000349 0);
bellard9307c4c2004-04-04 12:57:25 +0000350#endif
351}
352
aliguori376253e2009-03-05 23:01:23 +0000353static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000354{
355 CPUState *env;
356
357 /* just to set the default cpu if not already done */
358 mon_get_cpu();
359
360 for(env = first_cpu; env != NULL; env = env->next_cpu) {
aliguorid1546152009-03-12 20:12:57 +0000361 cpu_synchronize_state(env, 0);
aliguori376253e2009-03-05 23:01:23 +0000362 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000363 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000364 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000365#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000366 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
367 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000368#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000369 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000370#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000371 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
372 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000373#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000374 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000375#endif
thsead93602007-09-06 00:18:15 +0000376 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000377 monitor_printf(mon, " (halted)");
378 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000379 }
380}
381
aliguori376253e2009-03-05 23:01:23 +0000382static void do_cpu_set(Monitor *mon, int index)
bellard6a00d602005-11-21 23:25:50 +0000383{
384 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000385 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000386}
387
aliguori376253e2009-03-05 23:01:23 +0000388static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000389{
aliguori376253e2009-03-05 23:01:23 +0000390 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000391}
392
aliguori376253e2009-03-05 23:01:23 +0000393static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000394{
395 int i;
bellard7e2515e2004-08-01 21:52:19 +0000396 const char *str;
ths3b46e622007-09-17 08:09:54 +0000397
aliguoricde76ee2009-03-05 23:01:51 +0000398 if (!mon->rs)
399 return;
bellard7e2515e2004-08-01 21:52:19 +0000400 i = 0;
401 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000402 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000403 if (!str)
404 break;
aliguori376253e2009-03-05 23:01:23 +0000405 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000406 i++;
bellardaa455482004-04-04 13:07:25 +0000407 }
408}
409
j_mayer76a66252007-03-07 08:32:30 +0000410#if defined(TARGET_PPC)
411/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000412static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000413{
414 CPUState *env;
415
416 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000417 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000418}
419#endif
420
aliguori376253e2009-03-05 23:01:23 +0000421static void do_quit(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000422{
423 exit(0);
424}
425
aliguori376253e2009-03-05 23:01:23 +0000426static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000427{
428 if (bdrv_is_inserted(bs)) {
429 if (!force) {
430 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000431 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000432 return -1;
433 }
434 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000435 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000436 return -1;
437 }
438 }
439 bdrv_close(bs);
440 }
441 return 0;
442}
443
aliguori376253e2009-03-05 23:01:23 +0000444static void do_eject(Monitor *mon, int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000445{
446 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000447
bellard9307c4c2004-04-04 12:57:25 +0000448 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000449 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000450 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000451 return;
452 }
aliguori376253e2009-03-05 23:01:23 +0000453 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000454}
455
aliguori376253e2009-03-05 23:01:23 +0000456static void do_change_block(Monitor *mon, const char *device,
457 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000458{
459 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000460 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000461
bellard9307c4c2004-04-04 12:57:25 +0000462 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000463 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000464 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000465 return;
466 }
aurel322ecea9b2008-06-18 22:10:01 +0000467 if (fmt) {
468 drv = bdrv_find_format(fmt);
469 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000470 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000471 return;
472 }
473 }
aliguori376253e2009-03-05 23:01:23 +0000474 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000475 return;
aurel322ecea9b2008-06-18 22:10:01 +0000476 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000477 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000478}
479
aliguori376253e2009-03-05 23:01:23 +0000480static void change_vnc_password_cb(Monitor *mon, const char *password,
481 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000482{
483 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000484 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000485
aliguori731b0362009-03-05 23:01:42 +0000486 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000487}
488
aliguori376253e2009-03-05 23:01:23 +0000489static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000490{
ths70848512007-08-25 01:37:05 +0000491 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000492 strcmp(target, "password") == 0) {
493 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000494 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000495 strncpy(password, arg, sizeof(password));
496 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000497 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000498 } else {
aliguori376253e2009-03-05 23:01:23 +0000499 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000500 }
ths70848512007-08-25 01:37:05 +0000501 } else {
aliguori28a76be2009-03-06 20:27:40 +0000502 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000503 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000504 }
thse25a5822007-08-25 01:36:20 +0000505}
506
aliguori376253e2009-03-05 23:01:23 +0000507static void do_change(Monitor *mon, const char *device, const char *target,
508 const char *arg)
thse25a5822007-08-25 01:36:20 +0000509{
510 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000511 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000512 } else {
aliguori28a76be2009-03-06 20:27:40 +0000513 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000514 }
515}
516
aliguori376253e2009-03-05 23:01:23 +0000517static void do_screen_dump(Monitor *mon, const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000518{
pbrook95219892006-04-09 01:06:34 +0000519 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000520}
521
aliguori376253e2009-03-05 23:01:23 +0000522static void do_logfile(Monitor *mon, const char *filename)
pbrooke735b912007-06-30 13:53:24 +0000523{
524 cpu_set_log_filename(filename);
525}
526
aliguori376253e2009-03-05 23:01:23 +0000527static void do_log(Monitor *mon, const char *items)
bellardf193c792004-03-21 17:06:25 +0000528{
529 int mask;
ths3b46e622007-09-17 08:09:54 +0000530
bellard9307c4c2004-04-04 12:57:25 +0000531 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000532 mask = 0;
533 } else {
bellard9307c4c2004-04-04 12:57:25 +0000534 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000535 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000536 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000537 return;
538 }
539 }
540 cpu_set_log(mask);
541}
542
aurel321b530a62009-04-05 20:08:59 +0000543static void do_singlestep(Monitor *mon, const char *option)
544{
545 if (!option || !strcmp(option, "on")) {
546 singlestep = 1;
547 } else if (!strcmp(option, "off")) {
548 singlestep = 0;
549 } else {
550 monitor_printf(mon, "unexpected option %s\n", option);
551 }
552}
553
aliguori376253e2009-03-05 23:01:23 +0000554static void do_stop(Monitor *mon)
bellard8a7ddc32004-03-31 19:00:16 +0000555{
556 vm_stop(EXCP_INTERRUPT);
557}
558
aliguoribb5fc202009-03-05 23:01:15 +0000559static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000560
aliguori376253e2009-03-05 23:01:23 +0000561struct bdrv_iterate_context {
562 Monitor *mon;
563 int err;
564};
aliguoric0f4ce72009-03-05 23:01:01 +0000565
aliguori376253e2009-03-05 23:01:23 +0000566static void do_cont(Monitor *mon)
567{
568 struct bdrv_iterate_context context = { mon, 0 };
569
570 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000571 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000572 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000573 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000574}
575
aliguoribb5fc202009-03-05 23:01:15 +0000576static void bdrv_key_cb(void *opaque, int err)
577{
aliguori376253e2009-03-05 23:01:23 +0000578 Monitor *mon = opaque;
579
aliguoribb5fc202009-03-05 23:01:15 +0000580 /* another key was set successfully, retry to continue */
581 if (!err)
aliguori376253e2009-03-05 23:01:23 +0000582 do_cont(mon);
aliguoribb5fc202009-03-05 23:01:15 +0000583}
584
585static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
586{
aliguori376253e2009-03-05 23:01:23 +0000587 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000588
aliguori376253e2009-03-05 23:01:23 +0000589 if (!context->err && bdrv_key_required(bs)) {
590 context->err = -EBUSY;
591 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
592 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000593 }
594}
595
aliguori59030a82009-04-05 18:43:41 +0000596static void do_gdbserver(Monitor *mon, const char *device)
bellard8a7ddc32004-03-31 19:00:16 +0000597{
aliguori59030a82009-04-05 18:43:41 +0000598 if (!device)
599 device = "tcp::" DEFAULT_GDBSTUB_PORT;
600 if (gdbserver_start(device) < 0) {
601 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
602 device);
603 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000604 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000605 } else {
aliguori59030a82009-04-05 18:43:41 +0000606 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
607 device);
bellard8a7ddc32004-03-31 19:00:16 +0000608 }
609}
610
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100611static void do_watchdog_action(Monitor *mon, const char *action)
612{
613 if (select_watchdog_action(action) == -1) {
614 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
615 }
616}
617
aliguori376253e2009-03-05 23:01:23 +0000618static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000619{
aliguori376253e2009-03-05 23:01:23 +0000620 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000621 switch(c) {
622 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000623 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000624 break;
625 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000626 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000627 break;
628 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000629 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000630 break;
631 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000632 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000633 break;
634 default:
635 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000636 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000637 } else {
aliguori376253e2009-03-05 23:01:23 +0000638 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000639 }
640 break;
641 }
aliguori376253e2009-03-05 23:01:23 +0000642 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000643}
644
aliguori376253e2009-03-05 23:01:23 +0000645static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000646 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000647{
bellard6a00d602005-11-21 23:25:50 +0000648 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000649 int nb_per_line, l, line_size, i, max_digits, len;
650 uint8_t buf[16];
651 uint64_t v;
652
653 if (format == 'i') {
654 int flags;
655 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000656 env = mon_get_cpu();
657 if (!env && !is_physical)
658 return;
bellard9307c4c2004-04-04 12:57:25 +0000659#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000660 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000661 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000662 } else if (wsize == 4) {
663 flags = 0;
664 } else {
bellard6a15fd12006-04-12 21:07:07 +0000665 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000666 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000667 if (env) {
668#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000669 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000670 (env->segs[R_CS].flags & DESC_L_MASK))
671 flags = 2;
672 else
673#endif
674 if (!(env->segs[R_CS].flags & DESC_B_MASK))
675 flags = 1;
676 }
bellard4c27ba22004-04-25 18:05:08 +0000677 }
678#endif
aliguori376253e2009-03-05 23:01:23 +0000679 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000680 return;
681 }
682
683 len = wsize * count;
684 if (wsize == 1)
685 line_size = 8;
686 else
687 line_size = 16;
688 nb_per_line = line_size / wsize;
689 max_digits = 0;
690
691 switch(format) {
692 case 'o':
693 max_digits = (wsize * 8 + 2) / 3;
694 break;
695 default:
696 case 'x':
697 max_digits = (wsize * 8) / 4;
698 break;
699 case 'u':
700 case 'd':
701 max_digits = (wsize * 8 * 10 + 32) / 33;
702 break;
703 case 'c':
704 wsize = 1;
705 break;
706 }
707
708 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000709 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000710 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000711 else
aliguori376253e2009-03-05 23:01:23 +0000712 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000713 l = len;
714 if (l > line_size)
715 l = line_size;
716 if (is_physical) {
717 cpu_physical_memory_rw(addr, buf, l, 0);
718 } else {
bellard6a00d602005-11-21 23:25:50 +0000719 env = mon_get_cpu();
720 if (!env)
721 break;
aliguoric8f79b62008-08-18 14:00:20 +0000722 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000723 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000724 break;
725 }
bellard9307c4c2004-04-04 12:57:25 +0000726 }
ths5fafdf22007-09-16 21:08:06 +0000727 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000728 while (i < l) {
729 switch(wsize) {
730 default:
731 case 1:
732 v = ldub_raw(buf + i);
733 break;
734 case 2:
735 v = lduw_raw(buf + i);
736 break;
737 case 4:
bellard92a31b12005-02-10 22:00:52 +0000738 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000739 break;
740 case 8:
741 v = ldq_raw(buf + i);
742 break;
743 }
aliguori376253e2009-03-05 23:01:23 +0000744 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000745 switch(format) {
746 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000747 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000748 break;
749 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000750 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000751 break;
752 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000753 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000754 break;
755 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000756 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000757 break;
758 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000759 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000760 break;
761 }
762 i += wsize;
763 }
aliguori376253e2009-03-05 23:01:23 +0000764 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000765 addr += l;
766 len -= l;
767 }
768}
769
bellard92a31b12005-02-10 22:00:52 +0000770#if TARGET_LONG_BITS == 64
771#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
772#else
773#define GET_TLONG(h, l) (l)
774#endif
775
aliguori376253e2009-03-05 23:01:23 +0000776static void do_memory_dump(Monitor *mon, int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000777 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000778{
bellard92a31b12005-02-10 22:00:52 +0000779 target_long addr = GET_TLONG(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000780 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000781}
782
blueswir17743e582007-09-24 18:39:04 +0000783#if TARGET_PHYS_ADDR_BITS > 32
784#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
785#else
786#define GET_TPHYSADDR(h, l) (l)
787#endif
788
aliguori376253e2009-03-05 23:01:23 +0000789static void do_physical_memory_dump(Monitor *mon, int count, int format,
790 int size, uint32_t addrh, uint32_t addrl)
bellard92a31b12005-02-10 22:00:52 +0000791
bellard9307c4c2004-04-04 12:57:25 +0000792{
blueswir17743e582007-09-24 18:39:04 +0000793 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000794 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000795}
796
aliguori376253e2009-03-05 23:01:23 +0000797static void do_print(Monitor *mon, int count, int format, int size,
798 unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000799{
blueswir17743e582007-09-24 18:39:04 +0000800 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
801#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000802 switch(format) {
803 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000804 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000805 break;
806 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000807 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000808 break;
809 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000810 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000811 break;
812 default:
813 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000814 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000815 break;
816 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000817 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000818 break;
819 }
bellard92a31b12005-02-10 22:00:52 +0000820#else
821 switch(format) {
822 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000823 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000824 break;
825 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000826 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000827 break;
828 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000829 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000830 break;
831 default:
832 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000833 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000834 break;
835 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000836 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000837 break;
838 }
839#endif
aliguori376253e2009-03-05 23:01:23 +0000840 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000841}
842
aliguori376253e2009-03-05 23:01:23 +0000843static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000844 uint32_t size, const char *filename)
845{
846 FILE *f;
847 target_long addr = GET_TLONG(valh, vall);
848 uint32_t l;
849 CPUState *env;
850 uint8_t buf[1024];
851
852 env = mon_get_cpu();
853 if (!env)
854 return;
855
856 f = fopen(filename, "wb");
857 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000858 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000859 return;
860 }
861 while (size != 0) {
862 l = sizeof(buf);
863 if (l > size)
864 l = size;
865 cpu_memory_rw_debug(env, addr, buf, l, 0);
866 fwrite(buf, 1, l, f);
867 addr += l;
868 size -= l;
869 }
870 fclose(f);
871}
872
aliguori376253e2009-03-05 23:01:23 +0000873static void do_physical_memory_save(Monitor *mon, unsigned int valh,
874 unsigned int vall, uint32_t size,
875 const char *filename)
aurel32a8bdf7a2008-04-11 21:36:14 +0000876{
877 FILE *f;
878 uint32_t l;
879 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000880 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000881
882 f = fopen(filename, "wb");
883 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000884 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000885 return;
886 }
887 while (size != 0) {
888 l = sizeof(buf);
889 if (l > size)
890 l = size;
891 cpu_physical_memory_rw(addr, buf, l, 0);
892 fwrite(buf, 1, l, f);
893 fflush(f);
894 addr += l;
895 size -= l;
896 }
897 fclose(f);
898}
899
aliguori376253e2009-03-05 23:01:23 +0000900static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
bellarde4cf1ad2005-06-04 20:15:57 +0000901{
902 uint32_t addr;
903 uint8_t buf[1];
904 uint16_t sum;
905
906 sum = 0;
907 for(addr = start; addr < (start + size); addr++) {
908 cpu_physical_memory_rw(addr, buf, 1, 0);
909 /* BSD sum algorithm ('sum' Unix command) */
910 sum = (sum >> 1) | (sum << 15);
911 sum += buf[0];
912 }
aliguori376253e2009-03-05 23:01:23 +0000913 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000914}
915
bellarda3a91a32004-06-04 11:06:21 +0000916typedef struct {
917 int keycode;
918 const char *name;
919} KeyDef;
920
921static const KeyDef key_defs[] = {
922 { 0x2a, "shift" },
923 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000924
bellarda3a91a32004-06-04 11:06:21 +0000925 { 0x38, "alt" },
926 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000927 { 0x64, "altgr" },
928 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000929 { 0x1d, "ctrl" },
930 { 0x9d, "ctrl_r" },
931
932 { 0xdd, "menu" },
933
934 { 0x01, "esc" },
935
936 { 0x02, "1" },
937 { 0x03, "2" },
938 { 0x04, "3" },
939 { 0x05, "4" },
940 { 0x06, "5" },
941 { 0x07, "6" },
942 { 0x08, "7" },
943 { 0x09, "8" },
944 { 0x0a, "9" },
945 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000946 { 0x0c, "minus" },
947 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000948 { 0x0e, "backspace" },
949
950 { 0x0f, "tab" },
951 { 0x10, "q" },
952 { 0x11, "w" },
953 { 0x12, "e" },
954 { 0x13, "r" },
955 { 0x14, "t" },
956 { 0x15, "y" },
957 { 0x16, "u" },
958 { 0x17, "i" },
959 { 0x18, "o" },
960 { 0x19, "p" },
961
962 { 0x1c, "ret" },
963
964 { 0x1e, "a" },
965 { 0x1f, "s" },
966 { 0x20, "d" },
967 { 0x21, "f" },
968 { 0x22, "g" },
969 { 0x23, "h" },
970 { 0x24, "j" },
971 { 0x25, "k" },
972 { 0x26, "l" },
973
974 { 0x2c, "z" },
975 { 0x2d, "x" },
976 { 0x2e, "c" },
977 { 0x2f, "v" },
978 { 0x30, "b" },
979 { 0x31, "n" },
980 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000981 { 0x33, "comma" },
982 { 0x34, "dot" },
983 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000984
balrog4d3b6f62008-02-10 16:33:14 +0000985 { 0x37, "asterisk" },
986
bellarda3a91a32004-06-04 11:06:21 +0000987 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000988 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000989 { 0x3b, "f1" },
990 { 0x3c, "f2" },
991 { 0x3d, "f3" },
992 { 0x3e, "f4" },
993 { 0x3f, "f5" },
994 { 0x40, "f6" },
995 { 0x41, "f7" },
996 { 0x42, "f8" },
997 { 0x43, "f9" },
998 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +0000999 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001000 { 0x46, "scroll_lock" },
1001
bellard64866c32006-05-07 18:03:31 +00001002 { 0xb5, "kp_divide" },
1003 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001004 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001005 { 0x4e, "kp_add" },
1006 { 0x9c, "kp_enter" },
1007 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001008 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001009
1010 { 0x52, "kp_0" },
1011 { 0x4f, "kp_1" },
1012 { 0x50, "kp_2" },
1013 { 0x51, "kp_3" },
1014 { 0x4b, "kp_4" },
1015 { 0x4c, "kp_5" },
1016 { 0x4d, "kp_6" },
1017 { 0x47, "kp_7" },
1018 { 0x48, "kp_8" },
1019 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001020
bellarda3a91a32004-06-04 11:06:21 +00001021 { 0x56, "<" },
1022
1023 { 0x57, "f11" },
1024 { 0x58, "f12" },
1025
1026 { 0xb7, "print" },
1027
1028 { 0xc7, "home" },
1029 { 0xc9, "pgup" },
1030 { 0xd1, "pgdn" },
1031 { 0xcf, "end" },
1032
1033 { 0xcb, "left" },
1034 { 0xc8, "up" },
1035 { 0xd0, "down" },
1036 { 0xcd, "right" },
1037
1038 { 0xd2, "insert" },
1039 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001040#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1041 { 0xf0, "stop" },
1042 { 0xf1, "again" },
1043 { 0xf2, "props" },
1044 { 0xf3, "undo" },
1045 { 0xf4, "front" },
1046 { 0xf5, "copy" },
1047 { 0xf6, "open" },
1048 { 0xf7, "paste" },
1049 { 0xf8, "find" },
1050 { 0xf9, "cut" },
1051 { 0xfa, "lf" },
1052 { 0xfb, "help" },
1053 { 0xfc, "meta_l" },
1054 { 0xfd, "meta_r" },
1055 { 0xfe, "compose" },
1056#endif
bellarda3a91a32004-06-04 11:06:21 +00001057 { 0, NULL },
1058};
1059
1060static int get_keycode(const char *key)
1061{
1062 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001063 char *endp;
1064 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001065
1066 for(p = key_defs; p->name != NULL; p++) {
1067 if (!strcmp(key, p->name))
1068 return p->keycode;
1069 }
bellard64866c32006-05-07 18:03:31 +00001070 if (strstart(key, "0x", NULL)) {
1071 ret = strtoul(key, &endp, 0);
1072 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1073 return ret;
1074 }
bellarda3a91a32004-06-04 11:06:21 +00001075 return -1;
1076}
1077
balrogc8256f92008-06-08 22:45:01 +00001078#define MAX_KEYCODES 16
1079static uint8_t keycodes[MAX_KEYCODES];
1080static int nb_pending_keycodes;
1081static QEMUTimer *key_timer;
1082
1083static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001084{
balrogc8256f92008-06-08 22:45:01 +00001085 int keycode;
1086
1087 while (nb_pending_keycodes > 0) {
1088 nb_pending_keycodes--;
1089 keycode = keycodes[nb_pending_keycodes];
1090 if (keycode & 0x80)
1091 kbd_put_keycode(0xe0);
1092 kbd_put_keycode(keycode | 0x80);
1093 }
1094}
1095
aliguori376253e2009-03-05 23:01:23 +00001096static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1097 int hold_time)
balrogc8256f92008-06-08 22:45:01 +00001098{
balrog3401c0d2008-06-04 10:05:59 +00001099 char keyname_buf[16];
1100 char *separator;
1101 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +00001102
balrogc8256f92008-06-08 22:45:01 +00001103 if (nb_pending_keycodes > 0) {
1104 qemu_del_timer(key_timer);
1105 release_keys(NULL);
1106 }
1107 if (!has_hold_time)
1108 hold_time = 100;
1109 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001110 while (1) {
1111 separator = strchr(string, '-');
1112 keyname_len = separator ? separator - string : strlen(string);
1113 if (keyname_len > 0) {
1114 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1115 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001116 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001117 return;
bellarda3a91a32004-06-04 11:06:21 +00001118 }
balrogc8256f92008-06-08 22:45:01 +00001119 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001120 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001121 return;
1122 }
1123 keyname_buf[keyname_len] = 0;
1124 keycode = get_keycode(keyname_buf);
1125 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001126 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001127 return;
1128 }
balrogc8256f92008-06-08 22:45:01 +00001129 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001130 }
balrog3401c0d2008-06-04 10:05:59 +00001131 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001132 break;
balrog3401c0d2008-06-04 10:05:59 +00001133 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001134 }
balrogc8256f92008-06-08 22:45:01 +00001135 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001136 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001137 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001138 keycode = keycodes[i];
1139 if (keycode & 0x80)
1140 kbd_put_keycode(0xe0);
1141 kbd_put_keycode(keycode & 0x7f);
1142 }
balrogc8256f92008-06-08 22:45:01 +00001143 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001144 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1145 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001146}
1147
bellard13224a82006-07-14 22:03:35 +00001148static int mouse_button_state;
1149
aliguori376253e2009-03-05 23:01:23 +00001150static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001151 const char *dz_str)
1152{
1153 int dx, dy, dz;
1154 dx = strtol(dx_str, NULL, 0);
1155 dy = strtol(dy_str, NULL, 0);
1156 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001157 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001158 dz = strtol(dz_str, NULL, 0);
1159 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1160}
1161
aliguori376253e2009-03-05 23:01:23 +00001162static void do_mouse_button(Monitor *mon, int button_state)
bellard13224a82006-07-14 22:03:35 +00001163{
1164 mouse_button_state = button_state;
1165 kbd_mouse_event(0, 0, 0, mouse_button_state);
1166}
1167
aliguori376253e2009-03-05 23:01:23 +00001168static void do_ioport_read(Monitor *mon, int count, int format, int size,
1169 int addr, int has_index, int index)
bellard34405572004-06-08 00:55:58 +00001170{
1171 uint32_t val;
1172 int suffix;
1173
1174 if (has_index) {
Isaku Yamahatad56dd6c2009-07-02 19:32:07 +09001175 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001176 addr++;
1177 }
1178 addr &= 0xffff;
1179
1180 switch(size) {
1181 default:
1182 case 1:
1183 val = cpu_inb(NULL, addr);
1184 suffix = 'b';
1185 break;
1186 case 2:
1187 val = cpu_inw(NULL, addr);
1188 suffix = 'w';
1189 break;
1190 case 4:
1191 val = cpu_inl(NULL, addr);
1192 suffix = 'l';
1193 break;
1194 }
aliguori376253e2009-03-05 23:01:23 +00001195 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1196 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001197}
bellarda3a91a32004-06-04 11:06:21 +00001198
Jan Kiszkaf1147842009-07-14 10:20:11 +02001199static void do_ioport_write(Monitor *mon, int count, int format, int size,
1200 int addr, int val)
1201{
1202 addr &= IOPORTS_MASK;
1203
1204 switch (size) {
1205 default:
1206 case 1:
1207 cpu_outb(NULL, addr, val);
1208 break;
1209 case 2:
1210 cpu_outw(NULL, addr, val);
1211 break;
1212 case 4:
1213 cpu_outl(NULL, addr, val);
1214 break;
1215 }
1216}
1217
aliguori376253e2009-03-05 23:01:23 +00001218static void do_boot_set(Monitor *mon, const char *bootdevice)
aurel320ecdffb2008-05-04 20:11:34 +00001219{
1220 int res;
1221
Jan Kiszka76e30d02009-07-02 00:19:02 +02001222 res = qemu_boot_set(bootdevice);
1223 if (res == 0) {
1224 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1225 } else if (res > 0) {
1226 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001227 } else {
aliguori376253e2009-03-05 23:01:23 +00001228 monitor_printf(mon, "no function defined to set boot device list for "
1229 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001230 }
1231}
1232
aliguori376253e2009-03-05 23:01:23 +00001233static void do_system_reset(Monitor *mon)
bellarde4f90822004-06-20 12:35:44 +00001234{
1235 qemu_system_reset_request();
1236}
1237
aliguori376253e2009-03-05 23:01:23 +00001238static void do_system_powerdown(Monitor *mon)
bellard34751872005-07-02 14:31:34 +00001239{
1240 qemu_system_powerdown_request();
1241}
1242
bellardb86bda52004-09-18 19:32:46 +00001243#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001244static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001245{
aliguori376253e2009-03-05 23:01:23 +00001246 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1247 addr,
1248 pte & mask,
1249 pte & PG_GLOBAL_MASK ? 'G' : '-',
1250 pte & PG_PSE_MASK ? 'P' : '-',
1251 pte & PG_DIRTY_MASK ? 'D' : '-',
1252 pte & PG_ACCESSED_MASK ? 'A' : '-',
1253 pte & PG_PCD_MASK ? 'C' : '-',
1254 pte & PG_PWT_MASK ? 'T' : '-',
1255 pte & PG_USER_MASK ? 'U' : '-',
1256 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001257}
1258
aliguori376253e2009-03-05 23:01:23 +00001259static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001260{
bellard6a00d602005-11-21 23:25:50 +00001261 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001262 int l1, l2;
1263 uint32_t pgd, pde, pte;
1264
bellard6a00d602005-11-21 23:25:50 +00001265 env = mon_get_cpu();
1266 if (!env)
1267 return;
1268
bellardb86bda52004-09-18 19:32:46 +00001269 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001270 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001271 return;
1272 }
1273 pgd = env->cr[3] & ~0xfff;
1274 for(l1 = 0; l1 < 1024; l1++) {
1275 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1276 pde = le32_to_cpu(pde);
1277 if (pde & PG_PRESENT_MASK) {
1278 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001279 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001280 } else {
1281 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001282 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001283 (uint8_t *)&pte, 4);
1284 pte = le32_to_cpu(pte);
1285 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001286 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001287 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001288 ~0xfff);
1289 }
1290 }
1291 }
1292 }
1293 }
1294}
1295
aliguori376253e2009-03-05 23:01:23 +00001296static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001297 uint32_t end, int prot)
1298{
bellard9746b152004-11-11 18:30:24 +00001299 int prot1;
1300 prot1 = *plast_prot;
1301 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001302 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001303 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1304 *pstart, end, end - *pstart,
1305 prot1 & PG_USER_MASK ? 'u' : '-',
1306 'r',
1307 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001308 }
1309 if (prot != 0)
1310 *pstart = end;
1311 else
1312 *pstart = -1;
1313 *plast_prot = prot;
1314 }
1315}
1316
aliguori376253e2009-03-05 23:01:23 +00001317static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001318{
bellard6a00d602005-11-21 23:25:50 +00001319 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001320 int l1, l2, prot, last_prot;
1321 uint32_t pgd, pde, pte, start, end;
1322
bellard6a00d602005-11-21 23:25:50 +00001323 env = mon_get_cpu();
1324 if (!env)
1325 return;
1326
bellardb86bda52004-09-18 19:32:46 +00001327 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001328 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001329 return;
1330 }
1331 pgd = env->cr[3] & ~0xfff;
1332 last_prot = 0;
1333 start = -1;
1334 for(l1 = 0; l1 < 1024; l1++) {
1335 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1336 pde = le32_to_cpu(pde);
1337 end = l1 << 22;
1338 if (pde & PG_PRESENT_MASK) {
1339 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1340 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001341 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001342 } else {
1343 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001344 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001345 (uint8_t *)&pte, 4);
1346 pte = le32_to_cpu(pte);
1347 end = (l1 << 22) + (l2 << 12);
1348 if (pte & PG_PRESENT_MASK) {
1349 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1350 } else {
1351 prot = 0;
1352 }
aliguori376253e2009-03-05 23:01:23 +00001353 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001354 }
1355 }
1356 } else {
1357 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001358 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001359 }
1360 }
1361}
1362#endif
1363
aurel327c664e22009-03-03 06:12:22 +00001364#if defined(TARGET_SH4)
1365
aliguori376253e2009-03-05 23:01:23 +00001366static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001367{
aliguori376253e2009-03-05 23:01:23 +00001368 monitor_printf(mon, " tlb%i:\t"
1369 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1370 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1371 "dirty=%hhu writethrough=%hhu\n",
1372 idx,
1373 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1374 tlb->v, tlb->sh, tlb->c, tlb->pr,
1375 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001376}
1377
aliguori376253e2009-03-05 23:01:23 +00001378static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001379{
1380 CPUState *env = mon_get_cpu();
1381 int i;
1382
aliguori376253e2009-03-05 23:01:23 +00001383 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001384 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001385 print_tlb (mon, i, &env->itlb[i]);
1386 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001387 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001388 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001389}
1390
1391#endif
1392
aliguori376253e2009-03-05 23:01:23 +00001393static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001394{
1395#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001396 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001397 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001398 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001399 else
aliguori376253e2009-03-05 23:01:23 +00001400 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001401#else
aliguori376253e2009-03-05 23:01:23 +00001402 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001403#endif
1404}
1405
aliguori030ea372009-04-21 22:30:47 +00001406static void do_info_numa(Monitor *mon)
1407{
aliguorib28b6232009-04-22 20:20:29 +00001408 int i;
aliguori030ea372009-04-21 22:30:47 +00001409 CPUState *env;
1410
1411 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1412 for (i = 0; i < nb_numa_nodes; i++) {
1413 monitor_printf(mon, "node %d cpus:", i);
1414 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1415 if (env->numa_node == i) {
1416 monitor_printf(mon, " %d", env->cpu_index);
1417 }
1418 }
1419 monitor_printf(mon, "\n");
1420 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1421 node_mem[i] >> 20);
1422 }
1423}
1424
bellard5f1ce942006-02-08 22:40:15 +00001425#ifdef CONFIG_PROFILER
1426
aliguori376253e2009-03-05 23:01:23 +00001427static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001428{
1429 int64_t total;
1430 total = qemu_time;
1431 if (total == 0)
1432 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001433 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1434 dev_time, dev_time / (double)ticks_per_sec);
1435 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1436 qemu_time, qemu_time / (double)ticks_per_sec);
bellard5f1ce942006-02-08 22:40:15 +00001437 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001438 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001439}
1440#else
aliguori376253e2009-03-05 23:01:23 +00001441static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001442{
aliguori376253e2009-03-05 23:01:23 +00001443 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001444}
1445#endif
1446
bellardec36b692006-07-16 18:57:03 +00001447/* Capture support */
1448static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1449
aliguori376253e2009-03-05 23:01:23 +00001450static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001451{
1452 int i;
1453 CaptureState *s;
1454
1455 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001456 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001457 s->ops.info (s->opaque);
1458 }
1459}
1460
Blue Swirl23130862009-06-06 08:22:04 +00001461#ifdef HAS_AUDIO
aliguori376253e2009-03-05 23:01:23 +00001462static void do_stop_capture(Monitor *mon, int n)
bellardec36b692006-07-16 18:57:03 +00001463{
1464 int i;
1465 CaptureState *s;
1466
1467 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1468 if (i == n) {
1469 s->ops.destroy (s->opaque);
1470 LIST_REMOVE (s, entries);
1471 qemu_free (s);
1472 return;
1473 }
1474 }
1475}
1476
aliguori376253e2009-03-05 23:01:23 +00001477static void do_wav_capture(Monitor *mon, const char *path,
1478 int has_freq, int freq,
1479 int has_bits, int bits,
1480 int has_channels, int nchannels)
bellardec36b692006-07-16 18:57:03 +00001481{
1482 CaptureState *s;
1483
1484 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001485
1486 freq = has_freq ? freq : 44100;
1487 bits = has_bits ? bits : 16;
1488 nchannels = has_channels ? nchannels : 2;
1489
1490 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001491 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001492 qemu_free (s);
1493 }
1494 LIST_INSERT_HEAD (&capture_head, s, entries);
1495}
1496#endif
1497
aurel32dc1c0b72008-04-27 23:52:12 +00001498#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001499static void do_inject_nmi(Monitor *mon, int cpu_index)
aurel32dc1c0b72008-04-27 23:52:12 +00001500{
1501 CPUState *env;
1502
1503 for (env = first_cpu; env != NULL; env = env->next_cpu)
1504 if (env->cpu_index == cpu_index) {
1505 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1506 break;
1507 }
1508}
1509#endif
1510
aliguori376253e2009-03-05 23:01:23 +00001511static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001512{
aurel321b530a62009-04-05 20:08:59 +00001513 if (vm_running) {
1514 if (singlestep) {
1515 monitor_printf(mon, "VM status: running (single step mode)\n");
1516 } else {
1517 monitor_printf(mon, "VM status: running\n");
1518 }
1519 } else
aliguori376253e2009-03-05 23:01:23 +00001520 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001521}
1522
1523
aliguori376253e2009-03-05 23:01:23 +00001524static void do_balloon(Monitor *mon, int value)
aliguoridf751fa2008-12-04 20:19:35 +00001525{
1526 ram_addr_t target = value;
1527 qemu_balloon(target << 20);
1528}
1529
aliguori376253e2009-03-05 23:01:23 +00001530static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001531{
1532 ram_addr_t actual;
1533
1534 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001535 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001536 monitor_printf(mon, "Using KVM without synchronous MMU, "
1537 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001538 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001539 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001540 else
aliguori376253e2009-03-05 23:01:23 +00001541 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001542}
1543
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001544static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001545{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001546 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001547
aliguori76655d62009-03-06 20:27:37 +00001548 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001549 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001550 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001551 return acl;
1552}
aliguori76655d62009-03-06 20:27:37 +00001553
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001554static void do_acl_show(Monitor *mon, const char *aclname)
1555{
1556 qemu_acl *acl = find_acl(mon, aclname);
1557 qemu_acl_entry *entry;
1558 int i = 0;
1559
1560 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001561 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001562 acl->defaultDeny ? "deny" : "allow");
aliguori28a76be2009-03-06 20:27:40 +00001563 TAILQ_FOREACH(entry, &acl->entries, next) {
1564 i++;
1565 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001566 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001567 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001568 }
1569}
1570
1571static void do_acl_reset(Monitor *mon, const char *aclname)
1572{
1573 qemu_acl *acl = find_acl(mon, aclname);
1574
1575 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001576 qemu_acl_reset(acl);
1577 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001578 }
1579}
aliguori76655d62009-03-06 20:27:37 +00001580
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001581static void do_acl_policy(Monitor *mon, const char *aclname,
1582 const char *policy)
1583{
1584 qemu_acl *acl = find_acl(mon, aclname);
1585
1586 if (acl) {
1587 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001588 acl->defaultDeny = 0;
1589 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001590 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001591 acl->defaultDeny = 1;
1592 monitor_printf(mon, "acl: policy set to 'deny'\n");
1593 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001594 monitor_printf(mon, "acl: unknown policy '%s', "
1595 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001596 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001597 }
1598}
aliguori76655d62009-03-06 20:27:37 +00001599
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001600static void do_acl_add(Monitor *mon, const char *aclname,
1601 const char *match, const char *policy,
1602 int has_index, int index)
1603{
1604 qemu_acl *acl = find_acl(mon, aclname);
1605 int deny, ret;
1606
1607 if (acl) {
1608 if (strcmp(policy, "allow") == 0) {
1609 deny = 0;
1610 } else if (strcmp(policy, "deny") == 0) {
1611 deny = 1;
1612 } else {
1613 monitor_printf(mon, "acl: unknown policy '%s', "
1614 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001615 return;
1616 }
aliguori28a76be2009-03-06 20:27:40 +00001617 if (has_index)
1618 ret = qemu_acl_insert(acl, deny, match, index);
1619 else
1620 ret = qemu_acl_append(acl, deny, match);
1621 if (ret < 0)
1622 monitor_printf(mon, "acl: unable to add acl entry\n");
1623 else
1624 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001625 }
1626}
aliguori76655d62009-03-06 20:27:37 +00001627
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001628static void do_acl_remove(Monitor *mon, const char *aclname, const char *match)
1629{
1630 qemu_acl *acl = find_acl(mon, aclname);
1631 int ret;
aliguori76655d62009-03-06 20:27:37 +00001632
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001633 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001634 ret = qemu_acl_remove(acl, match);
1635 if (ret < 0)
1636 monitor_printf(mon, "acl: no matching acl entry\n");
1637 else
1638 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001639 }
1640}
1641
Huang Ying79c4f6b2009-06-23 10:05:14 +08001642#if defined(TARGET_I386)
1643static void do_inject_mce(Monitor *mon,
1644 int cpu_index, int bank,
1645 unsigned status_hi, unsigned status_lo,
1646 unsigned mcg_status_hi, unsigned mcg_status_lo,
1647 unsigned addr_hi, unsigned addr_lo,
1648 unsigned misc_hi, unsigned misc_lo)
1649{
1650 CPUState *cenv;
1651 uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
1652 uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
1653 uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
1654 uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
1655
1656 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1657 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1658 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1659 break;
1660 }
1661}
1662#endif
1663
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001664static void do_getfd(Monitor *mon, const char *fdname)
1665{
1666 mon_fd_t *monfd;
1667 int fd;
1668
1669 fd = qemu_chr_get_msgfd(mon->chr);
1670 if (fd == -1) {
1671 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1672 return;
1673 }
1674
1675 if (qemu_isdigit(fdname[0])) {
1676 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1677 return;
1678 }
1679
1680 fd = dup(fd);
1681 if (fd == -1) {
1682 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1683 strerror(errno));
1684 return;
1685 }
1686
1687 LIST_FOREACH(monfd, &mon->fds, next) {
1688 if (strcmp(monfd->name, fdname) != 0) {
1689 continue;
1690 }
1691
1692 close(monfd->fd);
1693 monfd->fd = fd;
1694 return;
1695 }
1696
1697 monfd = qemu_mallocz(sizeof(mon_fd_t));
1698 monfd->name = qemu_strdup(fdname);
1699 monfd->fd = fd;
1700
1701 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1702}
1703
1704static void do_closefd(Monitor *mon, const char *fdname)
1705{
1706 mon_fd_t *monfd;
1707
1708 LIST_FOREACH(monfd, &mon->fds, next) {
1709 if (strcmp(monfd->name, fdname) != 0) {
1710 continue;
1711 }
1712
1713 LIST_REMOVE(monfd, next);
1714 close(monfd->fd);
1715 qemu_free(monfd->name);
1716 qemu_free(monfd);
1717 return;
1718 }
1719
1720 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1721 fdname);
1722}
1723
Juan Quintelac8d41b22009-08-20 19:42:21 +02001724static void do_loadvm(Monitor *mon, const char *name)
1725{
1726 int saved_vm_running = vm_running;
1727
1728 vm_stop(0);
1729
1730 load_vmstate(mon, name);
1731 if (saved_vm_running)
1732 vm_start();
1733}
1734
Mark McLoughlin7768e042009-07-22 09:11:41 +01001735int monitor_get_fd(Monitor *mon, const char *fdname)
1736{
1737 mon_fd_t *monfd;
1738
1739 LIST_FOREACH(monfd, &mon->fds, next) {
1740 int fd;
1741
1742 if (strcmp(monfd->name, fdname) != 0) {
1743 continue;
1744 }
1745
1746 fd = monfd->fd;
1747
1748 /* caller takes ownership of fd */
1749 LIST_REMOVE(monfd, next);
1750 qemu_free(monfd->name);
1751 qemu_free(monfd);
1752
1753 return fd;
1754 }
1755
1756 return -1;
1757}
1758
aliguori376253e2009-03-05 23:01:23 +00001759static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001760#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001761 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001762};
1763
Blue Swirl23130862009-06-06 08:22:04 +00001764/* Please update qemu-monitor.hx when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001765static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001766 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001767 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001768 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001769 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001770 { "chardev", "", qemu_chr_info,
1771 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001772 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001773 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001774 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001775 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001776 { "registers", "", do_info_registers,
1777 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001778 { "cpus", "", do_info_cpus,
1779 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001780 { "history", "", do_info_history,
1781 "", "show the command line history", },
bellard4a0fb712004-05-21 11:39:07 +00001782 { "irq", "", irq_info,
1783 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001784 { "pic", "", pic_info,
1785 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001786 { "pci", "", pci_info,
1787 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001788#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001789 { "tlb", "", tlb_info,
1790 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001791#endif
1792#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001793 { "mem", "", mem_info,
1794 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001795 { "hpet", "", do_info_hpet,
1796 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001797#endif
bellarde3db7222005-01-26 22:00:47 +00001798 { "jit", "", do_info_jit,
1799 "", "show dynamic compiler info", },
aliguori7ba1e612008-11-05 16:04:33 +00001800 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001801 "", "show KVM information", },
aliguori030ea372009-04-21 22:30:47 +00001802 { "numa", "", do_info_numa,
1803 "", "show NUMA information", },
bellarda594cfb2005-11-06 16:13:29 +00001804 { "usb", "", usb_info,
1805 "", "show guest USB devices", },
1806 { "usbhost", "", usb_host_info,
1807 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001808 { "profile", "", do_info_profile,
1809 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001810 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001811 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001812 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001813 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001814 { "status", "", do_info_status,
1815 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001816 { "pcmcia", "", pcmcia_info,
1817 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001818 { "mice", "", do_info_mice,
1819 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001820 { "vnc", "", do_info_vnc,
1821 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001822 { "name", "", do_info_name,
1823 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001824 { "uuid", "", do_info_uuid,
1825 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001826#if defined(TARGET_PPC)
1827 { "cpustats", "", do_info_cpu_stats,
1828 "", "show CPU statistics", },
1829#endif
blueswir131a60e22007-10-26 18:42:59 +00001830#if defined(CONFIG_SLIRP)
Jan Kiszka6dbe5532009-06-24 14:42:29 +02001831 { "usernet", "", do_info_usernet,
1832 "", "show user network stack connection states", },
blueswir131a60e22007-10-26 18:42:59 +00001833#endif
aliguori5bb79102008-10-13 03:12:02 +00001834 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001835 { "balloon", "", do_info_balloon,
1836 "", "show balloon information" },
Gerd Hoffmanncae49562009-06-05 15:53:17 +01001837 { "qtree", "", do_info_qtree,
1838 "", "show device tree" },
Gerd Hoffmannf6c64e02009-08-03 15:03:09 +02001839 { "qdm", "", do_info_qdm,
1840 "", "show qdev device model list" },
bellard9dc39cb2004-03-14 21:38:27 +00001841 { NULL, NULL, },
1842};
1843
bellard9307c4c2004-04-04 12:57:25 +00001844/*******************************************************************/
1845
1846static const char *pch;
1847static jmp_buf expr_env;
1848
bellard92a31b12005-02-10 22:00:52 +00001849#define MD_TLONG 0
1850#define MD_I32 1
1851
bellard9307c4c2004-04-04 12:57:25 +00001852typedef struct MonitorDef {
1853 const char *name;
1854 int offset;
blueswir18662d652008-10-02 18:32:44 +00001855 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001856 int type;
bellard9307c4c2004-04-04 12:57:25 +00001857} MonitorDef;
1858
bellard57206fd2004-04-25 18:54:52 +00001859#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001860static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001861{
bellard6a00d602005-11-21 23:25:50 +00001862 CPUState *env = mon_get_cpu();
1863 if (!env)
1864 return 0;
1865 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001866}
1867#endif
1868
bellarda541f292004-04-12 20:39:29 +00001869#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001870static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001871{
bellard6a00d602005-11-21 23:25:50 +00001872 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001873 unsigned int u;
1874 int i;
1875
bellard6a00d602005-11-21 23:25:50 +00001876 if (!env)
1877 return 0;
1878
bellarda541f292004-04-12 20:39:29 +00001879 u = 0;
1880 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00001881 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001882
1883 return u;
1884}
1885
blueswir18662d652008-10-02 18:32:44 +00001886static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001887{
bellard6a00d602005-11-21 23:25:50 +00001888 CPUState *env = mon_get_cpu();
1889 if (!env)
1890 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001891 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001892}
1893
blueswir18662d652008-10-02 18:32:44 +00001894static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001895{
bellard6a00d602005-11-21 23:25:50 +00001896 CPUState *env = mon_get_cpu();
1897 if (!env)
1898 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001899 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001900}
bellard9fddaa02004-05-21 12:59:32 +00001901
blueswir18662d652008-10-02 18:32:44 +00001902static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001903{
bellard6a00d602005-11-21 23:25:50 +00001904 CPUState *env = mon_get_cpu();
1905 if (!env)
1906 return 0;
1907 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001908}
1909
blueswir18662d652008-10-02 18:32:44 +00001910static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001911{
bellard6a00d602005-11-21 23:25:50 +00001912 CPUState *env = mon_get_cpu();
1913 if (!env)
1914 return 0;
1915 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001916}
1917
blueswir18662d652008-10-02 18:32:44 +00001918static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001919{
bellard6a00d602005-11-21 23:25:50 +00001920 CPUState *env = mon_get_cpu();
1921 if (!env)
1922 return 0;
1923 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001924}
bellarda541f292004-04-12 20:39:29 +00001925#endif
1926
bellarde95c8d52004-09-30 22:22:08 +00001927#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001928#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001929static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001930{
bellard6a00d602005-11-21 23:25:50 +00001931 CPUState *env = mon_get_cpu();
1932 if (!env)
1933 return 0;
1934 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001935}
bellard7b936c02005-10-30 17:05:13 +00001936#endif
bellarde95c8d52004-09-30 22:22:08 +00001937
blueswir18662d652008-10-02 18:32:44 +00001938static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001939{
bellard6a00d602005-11-21 23:25:50 +00001940 CPUState *env = mon_get_cpu();
1941 if (!env)
1942 return 0;
1943 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001944}
1945#endif
1946
blueswir18662d652008-10-02 18:32:44 +00001947static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001948#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001949
1950#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001951 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001952 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001953 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001954
bellard9307c4c2004-04-04 12:57:25 +00001955 { "eax", offsetof(CPUState, regs[0]) },
1956 { "ecx", offsetof(CPUState, regs[1]) },
1957 { "edx", offsetof(CPUState, regs[2]) },
1958 { "ebx", offsetof(CPUState, regs[3]) },
1959 { "esp|sp", offsetof(CPUState, regs[4]) },
1960 { "ebp|fp", offsetof(CPUState, regs[5]) },
1961 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001962 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001963#ifdef TARGET_X86_64
1964 { "r8", offsetof(CPUState, regs[8]) },
1965 { "r9", offsetof(CPUState, regs[9]) },
1966 { "r10", offsetof(CPUState, regs[10]) },
1967 { "r11", offsetof(CPUState, regs[11]) },
1968 { "r12", offsetof(CPUState, regs[12]) },
1969 { "r13", offsetof(CPUState, regs[13]) },
1970 { "r14", offsetof(CPUState, regs[14]) },
1971 { "r15", offsetof(CPUState, regs[15]) },
1972#endif
bellard9307c4c2004-04-04 12:57:25 +00001973 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001974 { "eip", offsetof(CPUState, eip) },
1975 SEG("cs", R_CS)
1976 SEG("ds", R_DS)
1977 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001978 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001979 SEG("fs", R_FS)
1980 SEG("gs", R_GS)
1981 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00001982#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00001983 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00001984 { "r0", offsetof(CPUState, gpr[0]) },
1985 { "r1", offsetof(CPUState, gpr[1]) },
1986 { "r2", offsetof(CPUState, gpr[2]) },
1987 { "r3", offsetof(CPUState, gpr[3]) },
1988 { "r4", offsetof(CPUState, gpr[4]) },
1989 { "r5", offsetof(CPUState, gpr[5]) },
1990 { "r6", offsetof(CPUState, gpr[6]) },
1991 { "r7", offsetof(CPUState, gpr[7]) },
1992 { "r8", offsetof(CPUState, gpr[8]) },
1993 { "r9", offsetof(CPUState, gpr[9]) },
1994 { "r10", offsetof(CPUState, gpr[10]) },
1995 { "r11", offsetof(CPUState, gpr[11]) },
1996 { "r12", offsetof(CPUState, gpr[12]) },
1997 { "r13", offsetof(CPUState, gpr[13]) },
1998 { "r14", offsetof(CPUState, gpr[14]) },
1999 { "r15", offsetof(CPUState, gpr[15]) },
2000 { "r16", offsetof(CPUState, gpr[16]) },
2001 { "r17", offsetof(CPUState, gpr[17]) },
2002 { "r18", offsetof(CPUState, gpr[18]) },
2003 { "r19", offsetof(CPUState, gpr[19]) },
2004 { "r20", offsetof(CPUState, gpr[20]) },
2005 { "r21", offsetof(CPUState, gpr[21]) },
2006 { "r22", offsetof(CPUState, gpr[22]) },
2007 { "r23", offsetof(CPUState, gpr[23]) },
2008 { "r24", offsetof(CPUState, gpr[24]) },
2009 { "r25", offsetof(CPUState, gpr[25]) },
2010 { "r26", offsetof(CPUState, gpr[26]) },
2011 { "r27", offsetof(CPUState, gpr[27]) },
2012 { "r28", offsetof(CPUState, gpr[28]) },
2013 { "r29", offsetof(CPUState, gpr[29]) },
2014 { "r30", offsetof(CPUState, gpr[30]) },
2015 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002016 /* Floating point registers */
2017 { "f0", offsetof(CPUState, fpr[0]) },
2018 { "f1", offsetof(CPUState, fpr[1]) },
2019 { "f2", offsetof(CPUState, fpr[2]) },
2020 { "f3", offsetof(CPUState, fpr[3]) },
2021 { "f4", offsetof(CPUState, fpr[4]) },
2022 { "f5", offsetof(CPUState, fpr[5]) },
2023 { "f6", offsetof(CPUState, fpr[6]) },
2024 { "f7", offsetof(CPUState, fpr[7]) },
2025 { "f8", offsetof(CPUState, fpr[8]) },
2026 { "f9", offsetof(CPUState, fpr[9]) },
2027 { "f10", offsetof(CPUState, fpr[10]) },
2028 { "f11", offsetof(CPUState, fpr[11]) },
2029 { "f12", offsetof(CPUState, fpr[12]) },
2030 { "f13", offsetof(CPUState, fpr[13]) },
2031 { "f14", offsetof(CPUState, fpr[14]) },
2032 { "f15", offsetof(CPUState, fpr[15]) },
2033 { "f16", offsetof(CPUState, fpr[16]) },
2034 { "f17", offsetof(CPUState, fpr[17]) },
2035 { "f18", offsetof(CPUState, fpr[18]) },
2036 { "f19", offsetof(CPUState, fpr[19]) },
2037 { "f20", offsetof(CPUState, fpr[20]) },
2038 { "f21", offsetof(CPUState, fpr[21]) },
2039 { "f22", offsetof(CPUState, fpr[22]) },
2040 { "f23", offsetof(CPUState, fpr[23]) },
2041 { "f24", offsetof(CPUState, fpr[24]) },
2042 { "f25", offsetof(CPUState, fpr[25]) },
2043 { "f26", offsetof(CPUState, fpr[26]) },
2044 { "f27", offsetof(CPUState, fpr[27]) },
2045 { "f28", offsetof(CPUState, fpr[28]) },
2046 { "f29", offsetof(CPUState, fpr[29]) },
2047 { "f30", offsetof(CPUState, fpr[30]) },
2048 { "f31", offsetof(CPUState, fpr[31]) },
2049 { "fpscr", offsetof(CPUState, fpscr) },
2050 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002051 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002052 { "lr", offsetof(CPUState, lr) },
2053 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002054 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002055 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002056 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002057 { "msr", 0, &monitor_get_msr, },
2058 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002059 { "tbu", 0, &monitor_get_tbu, },
2060 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002061#if defined(TARGET_PPC64)
2062 /* Address space register */
2063 { "asr", offsetof(CPUState, asr) },
2064#endif
2065 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002066 { "sdr1", offsetof(CPUState, sdr1) },
2067 { "sr0", offsetof(CPUState, sr[0]) },
2068 { "sr1", offsetof(CPUState, sr[1]) },
2069 { "sr2", offsetof(CPUState, sr[2]) },
2070 { "sr3", offsetof(CPUState, sr[3]) },
2071 { "sr4", offsetof(CPUState, sr[4]) },
2072 { "sr5", offsetof(CPUState, sr[5]) },
2073 { "sr6", offsetof(CPUState, sr[6]) },
2074 { "sr7", offsetof(CPUState, sr[7]) },
2075 { "sr8", offsetof(CPUState, sr[8]) },
2076 { "sr9", offsetof(CPUState, sr[9]) },
2077 { "sr10", offsetof(CPUState, sr[10]) },
2078 { "sr11", offsetof(CPUState, sr[11]) },
2079 { "sr12", offsetof(CPUState, sr[12]) },
2080 { "sr13", offsetof(CPUState, sr[13]) },
2081 { "sr14", offsetof(CPUState, sr[14]) },
2082 { "sr15", offsetof(CPUState, sr[15]) },
2083 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002084#elif defined(TARGET_SPARC)
2085 { "g0", offsetof(CPUState, gregs[0]) },
2086 { "g1", offsetof(CPUState, gregs[1]) },
2087 { "g2", offsetof(CPUState, gregs[2]) },
2088 { "g3", offsetof(CPUState, gregs[3]) },
2089 { "g4", offsetof(CPUState, gregs[4]) },
2090 { "g5", offsetof(CPUState, gregs[5]) },
2091 { "g6", offsetof(CPUState, gregs[6]) },
2092 { "g7", offsetof(CPUState, gregs[7]) },
2093 { "o0", 0, monitor_get_reg },
2094 { "o1", 1, monitor_get_reg },
2095 { "o2", 2, monitor_get_reg },
2096 { "o3", 3, monitor_get_reg },
2097 { "o4", 4, monitor_get_reg },
2098 { "o5", 5, monitor_get_reg },
2099 { "o6", 6, monitor_get_reg },
2100 { "o7", 7, monitor_get_reg },
2101 { "l0", 8, monitor_get_reg },
2102 { "l1", 9, monitor_get_reg },
2103 { "l2", 10, monitor_get_reg },
2104 { "l3", 11, monitor_get_reg },
2105 { "l4", 12, monitor_get_reg },
2106 { "l5", 13, monitor_get_reg },
2107 { "l6", 14, monitor_get_reg },
2108 { "l7", 15, monitor_get_reg },
2109 { "i0", 16, monitor_get_reg },
2110 { "i1", 17, monitor_get_reg },
2111 { "i2", 18, monitor_get_reg },
2112 { "i3", 19, monitor_get_reg },
2113 { "i4", 20, monitor_get_reg },
2114 { "i5", 21, monitor_get_reg },
2115 { "i6", 22, monitor_get_reg },
2116 { "i7", 23, monitor_get_reg },
2117 { "pc", offsetof(CPUState, pc) },
2118 { "npc", offsetof(CPUState, npc) },
2119 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002120#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002121 { "psr", 0, &monitor_get_psr, },
2122 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002123#endif
bellarde95c8d52004-09-30 22:22:08 +00002124 { "tbr", offsetof(CPUState, tbr) },
2125 { "fsr", offsetof(CPUState, fsr) },
2126 { "f0", offsetof(CPUState, fpr[0]) },
2127 { "f1", offsetof(CPUState, fpr[1]) },
2128 { "f2", offsetof(CPUState, fpr[2]) },
2129 { "f3", offsetof(CPUState, fpr[3]) },
2130 { "f4", offsetof(CPUState, fpr[4]) },
2131 { "f5", offsetof(CPUState, fpr[5]) },
2132 { "f6", offsetof(CPUState, fpr[6]) },
2133 { "f7", offsetof(CPUState, fpr[7]) },
2134 { "f8", offsetof(CPUState, fpr[8]) },
2135 { "f9", offsetof(CPUState, fpr[9]) },
2136 { "f10", offsetof(CPUState, fpr[10]) },
2137 { "f11", offsetof(CPUState, fpr[11]) },
2138 { "f12", offsetof(CPUState, fpr[12]) },
2139 { "f13", offsetof(CPUState, fpr[13]) },
2140 { "f14", offsetof(CPUState, fpr[14]) },
2141 { "f15", offsetof(CPUState, fpr[15]) },
2142 { "f16", offsetof(CPUState, fpr[16]) },
2143 { "f17", offsetof(CPUState, fpr[17]) },
2144 { "f18", offsetof(CPUState, fpr[18]) },
2145 { "f19", offsetof(CPUState, fpr[19]) },
2146 { "f20", offsetof(CPUState, fpr[20]) },
2147 { "f21", offsetof(CPUState, fpr[21]) },
2148 { "f22", offsetof(CPUState, fpr[22]) },
2149 { "f23", offsetof(CPUState, fpr[23]) },
2150 { "f24", offsetof(CPUState, fpr[24]) },
2151 { "f25", offsetof(CPUState, fpr[25]) },
2152 { "f26", offsetof(CPUState, fpr[26]) },
2153 { "f27", offsetof(CPUState, fpr[27]) },
2154 { "f28", offsetof(CPUState, fpr[28]) },
2155 { "f29", offsetof(CPUState, fpr[29]) },
2156 { "f30", offsetof(CPUState, fpr[30]) },
2157 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002158#ifdef TARGET_SPARC64
2159 { "f32", offsetof(CPUState, fpr[32]) },
2160 { "f34", offsetof(CPUState, fpr[34]) },
2161 { "f36", offsetof(CPUState, fpr[36]) },
2162 { "f38", offsetof(CPUState, fpr[38]) },
2163 { "f40", offsetof(CPUState, fpr[40]) },
2164 { "f42", offsetof(CPUState, fpr[42]) },
2165 { "f44", offsetof(CPUState, fpr[44]) },
2166 { "f46", offsetof(CPUState, fpr[46]) },
2167 { "f48", offsetof(CPUState, fpr[48]) },
2168 { "f50", offsetof(CPUState, fpr[50]) },
2169 { "f52", offsetof(CPUState, fpr[52]) },
2170 { "f54", offsetof(CPUState, fpr[54]) },
2171 { "f56", offsetof(CPUState, fpr[56]) },
2172 { "f58", offsetof(CPUState, fpr[58]) },
2173 { "f60", offsetof(CPUState, fpr[60]) },
2174 { "f62", offsetof(CPUState, fpr[62]) },
2175 { "asi", offsetof(CPUState, asi) },
2176 { "pstate", offsetof(CPUState, pstate) },
2177 { "cansave", offsetof(CPUState, cansave) },
2178 { "canrestore", offsetof(CPUState, canrestore) },
2179 { "otherwin", offsetof(CPUState, otherwin) },
2180 { "wstate", offsetof(CPUState, wstate) },
2181 { "cleanwin", offsetof(CPUState, cleanwin) },
2182 { "fprs", offsetof(CPUState, fprs) },
2183#endif
bellard9307c4c2004-04-04 12:57:25 +00002184#endif
2185 { NULL },
2186};
2187
aliguori376253e2009-03-05 23:01:23 +00002188static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002189{
aliguori376253e2009-03-05 23:01:23 +00002190 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002191 longjmp(expr_env, 1);
2192}
2193
bellard6a00d602005-11-21 23:25:50 +00002194/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002195static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002196{
blueswir18662d652008-10-02 18:32:44 +00002197 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002198 void *ptr;
2199
bellard9307c4c2004-04-04 12:57:25 +00002200 for(md = monitor_defs; md->name != NULL; md++) {
2201 if (compare_cmd(name, md->name)) {
2202 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002203 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002204 } else {
bellard6a00d602005-11-21 23:25:50 +00002205 CPUState *env = mon_get_cpu();
2206 if (!env)
2207 return -2;
2208 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002209 switch(md->type) {
2210 case MD_I32:
2211 *pval = *(int32_t *)ptr;
2212 break;
2213 case MD_TLONG:
2214 *pval = *(target_long *)ptr;
2215 break;
2216 default:
2217 *pval = 0;
2218 break;
2219 }
bellard9307c4c2004-04-04 12:57:25 +00002220 }
2221 return 0;
2222 }
2223 }
2224 return -1;
2225}
2226
2227static void next(void)
2228{
Blue Swirl660f11b2009-07-31 21:16:51 +00002229 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002230 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002231 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002232 pch++;
2233 }
2234}
2235
aliguori376253e2009-03-05 23:01:23 +00002236static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002237
aliguori376253e2009-03-05 23:01:23 +00002238static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002239{
blueswir1c2efc952007-09-25 17:28:42 +00002240 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002241 char *p;
bellard6a00d602005-11-21 23:25:50 +00002242 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002243
2244 switch(*pch) {
2245 case '+':
2246 next();
aliguori376253e2009-03-05 23:01:23 +00002247 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002248 break;
2249 case '-':
2250 next();
aliguori376253e2009-03-05 23:01:23 +00002251 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002252 break;
2253 case '~':
2254 next();
aliguori376253e2009-03-05 23:01:23 +00002255 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002256 break;
2257 case '(':
2258 next();
aliguori376253e2009-03-05 23:01:23 +00002259 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002260 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002261 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002262 }
2263 next();
2264 break;
bellard81d09122004-07-14 17:21:37 +00002265 case '\'':
2266 pch++;
2267 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002268 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002269 n = *pch;
2270 pch++;
2271 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002272 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002273 next();
2274 break;
bellard9307c4c2004-04-04 12:57:25 +00002275 case '$':
2276 {
2277 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002278 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002279
bellard9307c4c2004-04-04 12:57:25 +00002280 pch++;
2281 q = buf;
2282 while ((*pch >= 'a' && *pch <= 'z') ||
2283 (*pch >= 'A' && *pch <= 'Z') ||
2284 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002285 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002286 if ((q - buf) < sizeof(buf) - 1)
2287 *q++ = *pch;
2288 pch++;
2289 }
blueswir1cd390082008-11-16 13:53:32 +00002290 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002291 pch++;
2292 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002293 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002294 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002295 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002296 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002297 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002298 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002299 }
2300 break;
2301 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002302 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002303 n = 0;
2304 break;
2305 default:
blueswir17743e582007-09-24 18:39:04 +00002306#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002307 n = strtoull(pch, &p, 0);
2308#else
bellard9307c4c2004-04-04 12:57:25 +00002309 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002310#endif
bellard9307c4c2004-04-04 12:57:25 +00002311 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002312 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002313 }
2314 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002315 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002316 pch++;
2317 break;
2318 }
2319 return n;
2320}
2321
2322
aliguori376253e2009-03-05 23:01:23 +00002323static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002324{
blueswir1c2efc952007-09-25 17:28:42 +00002325 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002326 int op;
ths3b46e622007-09-17 08:09:54 +00002327
aliguori376253e2009-03-05 23:01:23 +00002328 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002329 for(;;) {
2330 op = *pch;
2331 if (op != '*' && op != '/' && op != '%')
2332 break;
2333 next();
aliguori376253e2009-03-05 23:01:23 +00002334 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002335 switch(op) {
2336 default:
2337 case '*':
2338 val *= val2;
2339 break;
2340 case '/':
2341 case '%':
ths5fafdf22007-09-16 21:08:06 +00002342 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002343 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002344 if (op == '/')
2345 val /= val2;
2346 else
2347 val %= val2;
2348 break;
2349 }
2350 }
2351 return val;
2352}
2353
aliguori376253e2009-03-05 23:01:23 +00002354static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002355{
blueswir1c2efc952007-09-25 17:28:42 +00002356 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002357 int op;
bellard9307c4c2004-04-04 12:57:25 +00002358
aliguori376253e2009-03-05 23:01:23 +00002359 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002360 for(;;) {
2361 op = *pch;
2362 if (op != '&' && op != '|' && op != '^')
2363 break;
2364 next();
aliguori376253e2009-03-05 23:01:23 +00002365 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002366 switch(op) {
2367 default:
2368 case '&':
2369 val &= val2;
2370 break;
2371 case '|':
2372 val |= val2;
2373 break;
2374 case '^':
2375 val ^= val2;
2376 break;
2377 }
2378 }
2379 return val;
2380}
2381
aliguori376253e2009-03-05 23:01:23 +00002382static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002383{
blueswir1c2efc952007-09-25 17:28:42 +00002384 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002385 int op;
bellard9307c4c2004-04-04 12:57:25 +00002386
aliguori376253e2009-03-05 23:01:23 +00002387 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002388 for(;;) {
2389 op = *pch;
2390 if (op != '+' && op != '-')
2391 break;
2392 next();
aliguori376253e2009-03-05 23:01:23 +00002393 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002394 if (op == '+')
2395 val += val2;
2396 else
2397 val -= val2;
2398 }
2399 return val;
2400}
2401
aliguori376253e2009-03-05 23:01:23 +00002402static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002403{
2404 pch = *pp;
2405 if (setjmp(expr_env)) {
2406 *pp = pch;
2407 return -1;
2408 }
blueswir1cd390082008-11-16 13:53:32 +00002409 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002410 pch++;
aliguori376253e2009-03-05 23:01:23 +00002411 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002412 *pp = pch;
2413 return 0;
2414}
2415
2416static int get_str(char *buf, int buf_size, const char **pp)
2417{
2418 const char *p;
2419 char *q;
2420 int c;
2421
bellard81d09122004-07-14 17:21:37 +00002422 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002423 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002424 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002425 p++;
2426 if (*p == '\0') {
2427 fail:
bellard81d09122004-07-14 17:21:37 +00002428 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002429 *pp = p;
2430 return -1;
2431 }
bellard9307c4c2004-04-04 12:57:25 +00002432 if (*p == '\"') {
2433 p++;
2434 while (*p != '\0' && *p != '\"') {
2435 if (*p == '\\') {
2436 p++;
2437 c = *p++;
2438 switch(c) {
2439 case 'n':
2440 c = '\n';
2441 break;
2442 case 'r':
2443 c = '\r';
2444 break;
2445 case '\\':
2446 case '\'':
2447 case '\"':
2448 break;
2449 default:
2450 qemu_printf("unsupported escape code: '\\%c'\n", c);
2451 goto fail;
2452 }
2453 if ((q - buf) < buf_size - 1) {
2454 *q++ = c;
2455 }
2456 } else {
2457 if ((q - buf) < buf_size - 1) {
2458 *q++ = *p;
2459 }
2460 p++;
2461 }
2462 }
2463 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002464 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002465 goto fail;
2466 }
2467 p++;
2468 } else {
blueswir1cd390082008-11-16 13:53:32 +00002469 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002470 if ((q - buf) < buf_size - 1) {
2471 *q++ = *p;
2472 }
2473 p++;
2474 }
bellard9307c4c2004-04-04 12:57:25 +00002475 }
bellard81d09122004-07-14 17:21:37 +00002476 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002477 *pp = p;
2478 return 0;
2479}
2480
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002481/*
2482 * Store the command-name in cmdname, and return a pointer to
2483 * the remaining of the command string.
2484 */
2485static const char *get_command_name(const char *cmdline,
2486 char *cmdname, size_t nlen)
2487{
2488 size_t len;
2489 const char *p, *pstart;
2490
2491 p = cmdline;
2492 while (qemu_isspace(*p))
2493 p++;
2494 if (*p == '\0')
2495 return NULL;
2496 pstart = p;
2497 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2498 p++;
2499 len = p - pstart;
2500 if (len > nlen - 1)
2501 len = nlen - 1;
2502 memcpy(cmdname, pstart, len);
2503 cmdname[len] = '\0';
2504 return p;
2505}
2506
bellard9307c4c2004-04-04 12:57:25 +00002507static int default_fmt_format = 'x';
2508static int default_fmt_size = 4;
2509
2510#define MAX_ARGS 16
2511
aliguori376253e2009-03-05 23:01:23 +00002512static void monitor_handle_command(Monitor *mon, const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002513{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002514 const char *p, *typestr;
2515 int c, nb_args, i, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002516 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002517 char cmdname[256];
2518 char buf[1024];
2519 void *str_allocated[MAX_ARGS];
2520 void *args[MAX_ARGS];
aliguori376253e2009-03-05 23:01:23 +00002521 void (*handler_0)(Monitor *mon);
2522 void (*handler_1)(Monitor *mon, void *arg0);
2523 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2524 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2525 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2526 void *arg3);
2527 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2528 void *arg3, void *arg4);
2529 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2530 void *arg3, void *arg4, void *arg5);
2531 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2532 void *arg3, void *arg4, void *arg5, void *arg6);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002533 void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2534 void *arg3, void *arg4, void *arg5, void *arg6,
2535 void *arg7);
2536 void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2537 void *arg3, void *arg4, void *arg5, void *arg6,
2538 void *arg7, void *arg8);
2539 void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2540 void *arg3, void *arg4, void *arg5, void *arg6,
2541 void *arg7, void *arg8, void *arg9);
bellard9dc39cb2004-03-14 21:38:27 +00002542
2543#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002544 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002545#endif
ths3b46e622007-09-17 08:09:54 +00002546
bellard9307c4c2004-04-04 12:57:25 +00002547 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002548 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2549 if (!p)
bellard9dc39cb2004-03-14 21:38:27 +00002550 return;
ths3b46e622007-09-17 08:09:54 +00002551
bellard9307c4c2004-04-04 12:57:25 +00002552 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002553 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002554 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002555 break;
bellard9dc39cb2004-03-14 21:38:27 +00002556 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002557
2558 if (cmd->name == NULL) {
2559 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2560 return;
2561 }
bellard9307c4c2004-04-04 12:57:25 +00002562
2563 for(i = 0; i < MAX_ARGS; i++)
2564 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002565
bellard9307c4c2004-04-04 12:57:25 +00002566 /* parse the parameters */
2567 typestr = cmd->args_type;
2568 nb_args = 0;
2569 for(;;) {
2570 c = *typestr;
2571 if (c == '\0')
2572 break;
2573 typestr++;
2574 switch(c) {
2575 case 'F':
bellard81d09122004-07-14 17:21:37 +00002576 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002577 case 's':
2578 {
2579 int ret;
2580 char *str;
ths3b46e622007-09-17 08:09:54 +00002581
blueswir1cd390082008-11-16 13:53:32 +00002582 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002583 p++;
2584 if (*typestr == '?') {
2585 typestr++;
2586 if (*p == '\0') {
2587 /* no optional string: NULL argument */
2588 str = NULL;
2589 goto add_str;
2590 }
2591 }
2592 ret = get_str(buf, sizeof(buf), &p);
2593 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002594 switch(c) {
2595 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002596 monitor_printf(mon, "%s: filename expected\n",
2597 cmdname);
bellard81d09122004-07-14 17:21:37 +00002598 break;
2599 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002600 monitor_printf(mon, "%s: block device name expected\n",
2601 cmdname);
bellard81d09122004-07-14 17:21:37 +00002602 break;
2603 default:
aliguori376253e2009-03-05 23:01:23 +00002604 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002605 break;
2606 }
bellard9307c4c2004-04-04 12:57:25 +00002607 goto fail;
2608 }
2609 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002610 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002611 str_allocated[nb_args] = str;
2612 add_str:
2613 if (nb_args >= MAX_ARGS) {
2614 error_args:
aliguori376253e2009-03-05 23:01:23 +00002615 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002616 goto fail;
2617 }
2618 args[nb_args++] = str;
2619 }
2620 break;
2621 case '/':
2622 {
2623 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002624
blueswir1cd390082008-11-16 13:53:32 +00002625 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002626 p++;
2627 if (*p == '/') {
2628 /* format found */
2629 p++;
2630 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002631 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002632 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002633 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002634 count = count * 10 + (*p - '0');
2635 p++;
2636 }
2637 }
2638 size = -1;
2639 format = -1;
2640 for(;;) {
2641 switch(*p) {
2642 case 'o':
2643 case 'd':
2644 case 'u':
2645 case 'x':
2646 case 'i':
2647 case 'c':
2648 format = *p++;
2649 break;
2650 case 'b':
2651 size = 1;
2652 p++;
2653 break;
2654 case 'h':
2655 size = 2;
2656 p++;
2657 break;
2658 case 'w':
2659 size = 4;
2660 p++;
2661 break;
2662 case 'g':
2663 case 'L':
2664 size = 8;
2665 p++;
2666 break;
2667 default:
2668 goto next;
2669 }
2670 }
2671 next:
blueswir1cd390082008-11-16 13:53:32 +00002672 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002673 monitor_printf(mon, "invalid char in format: '%c'\n",
2674 *p);
bellard9307c4c2004-04-04 12:57:25 +00002675 goto fail;
2676 }
bellard9307c4c2004-04-04 12:57:25 +00002677 if (format < 0)
2678 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002679 if (format != 'i') {
2680 /* for 'i', not specifying a size gives -1 as size */
2681 if (size < 0)
2682 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002683 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002684 }
bellard9307c4c2004-04-04 12:57:25 +00002685 default_fmt_format = format;
2686 } else {
2687 count = 1;
2688 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002689 if (format != 'i') {
2690 size = default_fmt_size;
2691 } else {
2692 size = -1;
2693 }
bellard9307c4c2004-04-04 12:57:25 +00002694 }
2695 if (nb_args + 3 > MAX_ARGS)
2696 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002697 args[nb_args++] = (void*)(long)count;
2698 args[nb_args++] = (void*)(long)format;
2699 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002700 }
2701 break;
2702 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002703 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002704 {
blueswir1c2efc952007-09-25 17:28:42 +00002705 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002706
blueswir1cd390082008-11-16 13:53:32 +00002707 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002708 p++;
bellard34405572004-06-08 00:55:58 +00002709 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002710 if (*typestr == '?') {
2711 if (*p == '\0')
2712 has_arg = 0;
2713 else
2714 has_arg = 1;
2715 } else {
2716 if (*p == '.') {
2717 p++;
blueswir1cd390082008-11-16 13:53:32 +00002718 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002719 p++;
2720 has_arg = 1;
2721 } else {
2722 has_arg = 0;
2723 }
2724 }
bellard13224a82006-07-14 22:03:35 +00002725 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002726 if (nb_args >= MAX_ARGS)
2727 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002728 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002729 if (!has_arg) {
2730 if (nb_args >= MAX_ARGS)
2731 goto error_args;
2732 val = -1;
2733 goto add_num;
2734 }
2735 }
aliguori376253e2009-03-05 23:01:23 +00002736 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002737 goto fail;
2738 add_num:
bellard92a31b12005-02-10 22:00:52 +00002739 if (c == 'i') {
2740 if (nb_args >= MAX_ARGS)
2741 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002742 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002743 } else {
2744 if ((nb_args + 1) >= MAX_ARGS)
2745 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002746#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002747 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002748#else
2749 args[nb_args++] = (void *)0;
2750#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002751 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002752 }
bellard9307c4c2004-04-04 12:57:25 +00002753 }
2754 break;
2755 case '-':
2756 {
2757 int has_option;
2758 /* option */
ths3b46e622007-09-17 08:09:54 +00002759
bellard9307c4c2004-04-04 12:57:25 +00002760 c = *typestr++;
2761 if (c == '\0')
2762 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002763 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002764 p++;
2765 has_option = 0;
2766 if (*p == '-') {
2767 p++;
2768 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002769 monitor_printf(mon, "%s: unsupported option -%c\n",
2770 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002771 goto fail;
2772 }
2773 p++;
2774 has_option = 1;
2775 }
2776 if (nb_args >= MAX_ARGS)
2777 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002778 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002779 }
2780 break;
2781 default:
2782 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002783 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002784 goto fail;
2785 }
2786 }
2787 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002788 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002789 p++;
2790 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002791 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2792 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002793 goto fail;
2794 }
2795
2796 switch(nb_args) {
2797 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002798 handler_0 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002799 handler_0(mon);
bellard9307c4c2004-04-04 12:57:25 +00002800 break;
2801 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002802 handler_1 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002803 handler_1(mon, args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002804 break;
2805 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002806 handler_2 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002807 handler_2(mon, args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002808 break;
2809 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002810 handler_3 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002811 handler_3(mon, args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002812 break;
2813 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002814 handler_4 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002815 handler_4(mon, args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002816 break;
2817 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002818 handler_5 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002819 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002820 break;
bellard34405572004-06-08 00:55:58 +00002821 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002822 handler_6 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002823 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002824 break;
bellardec36b692006-07-16 18:57:03 +00002825 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002826 handler_7 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002827 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2828 args[6]);
bellardec36b692006-07-16 18:57:03 +00002829 break;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002830 case 8:
2831 handler_8 = cmd->handler;
2832 handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2833 args[6], args[7]);
2834 break;
2835 case 9:
2836 handler_9 = cmd->handler;
2837 handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2838 args[6], args[7], args[8]);
2839 break;
2840 case 10:
2841 handler_10 = cmd->handler;
2842 handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2843 args[6], args[7], args[8], args[9]);
2844 break;
bellard9307c4c2004-04-04 12:57:25 +00002845 default:
aliguori376253e2009-03-05 23:01:23 +00002846 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
bellard9307c4c2004-04-04 12:57:25 +00002847 goto fail;
2848 }
2849 fail:
2850 for(i = 0; i < MAX_ARGS; i++)
2851 qemu_free(str_allocated[i]);
bellard9dc39cb2004-03-14 21:38:27 +00002852}
2853
bellard81d09122004-07-14 17:21:37 +00002854static void cmd_completion(const char *name, const char *list)
2855{
2856 const char *p, *pstart;
2857 char cmd[128];
2858 int len;
2859
2860 p = list;
2861 for(;;) {
2862 pstart = p;
2863 p = strchr(p, '|');
2864 if (!p)
2865 p = pstart + strlen(pstart);
2866 len = p - pstart;
2867 if (len > sizeof(cmd) - 2)
2868 len = sizeof(cmd) - 2;
2869 memcpy(cmd, pstart, len);
2870 cmd[len] = '\0';
2871 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002872 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002873 }
2874 if (*p == '\0')
2875 break;
2876 p++;
2877 }
2878}
2879
2880static void file_completion(const char *input)
2881{
2882 DIR *ffs;
2883 struct dirent *d;
2884 char path[1024];
2885 char file[1024], file_prefix[1024];
2886 int input_path_len;
2887 const char *p;
2888
ths5fafdf22007-09-16 21:08:06 +00002889 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002890 if (!p) {
2891 input_path_len = 0;
2892 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002893 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002894 } else {
2895 input_path_len = p - input + 1;
2896 memcpy(path, input, input_path_len);
2897 if (input_path_len > sizeof(path) - 1)
2898 input_path_len = sizeof(path) - 1;
2899 path[input_path_len] = '\0';
2900 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2901 }
2902#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002903 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2904 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002905#endif
2906 ffs = opendir(path);
2907 if (!ffs)
2908 return;
2909 for(;;) {
2910 struct stat sb;
2911 d = readdir(ffs);
2912 if (!d)
2913 break;
2914 if (strstart(d->d_name, file_prefix, NULL)) {
2915 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002916 if (input_path_len < sizeof(file))
2917 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2918 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002919 /* stat the file to find out if it's a directory.
2920 * In that case add a slash to speed up typing long paths
2921 */
2922 stat(file, &sb);
2923 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002924 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00002925 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002926 }
2927 }
2928 closedir(ffs);
2929}
2930
aliguori51de9762009-03-05 23:00:43 +00002931static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002932{
aliguori51de9762009-03-05 23:00:43 +00002933 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002934 const char *input = opaque;
2935
2936 if (input[0] == '\0' ||
2937 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00002938 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00002939 }
2940}
2941
2942/* NOTE: this parser is an approximate form of the real command parser */
2943static void parse_cmdline(const char *cmdline,
2944 int *pnb_args, char **args)
2945{
2946 const char *p;
2947 int nb_args, ret;
2948 char buf[1024];
2949
2950 p = cmdline;
2951 nb_args = 0;
2952 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002953 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002954 p++;
2955 if (*p == '\0')
2956 break;
2957 if (nb_args >= MAX_ARGS)
2958 break;
2959 ret = get_str(buf, sizeof(buf), &p);
2960 args[nb_args] = qemu_strdup(buf);
2961 nb_args++;
2962 if (ret < 0)
2963 break;
2964 }
2965 *pnb_args = nb_args;
2966}
2967
aliguori4c36ba32009-03-05 23:01:37 +00002968static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002969{
2970 const char *cmdname;
2971 char *args[MAX_ARGS];
2972 int nb_args, i, len;
2973 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00002974 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002975 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002976
2977 parse_cmdline(cmdline, &nb_args, args);
2978#ifdef DEBUG_COMPLETION
2979 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00002980 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00002981 }
2982#endif
2983
2984 /* if the line ends with a space, it means we want to complete the
2985 next arg */
2986 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002987 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002988 if (nb_args >= MAX_ARGS)
2989 return;
2990 args[nb_args++] = qemu_strdup("");
2991 }
2992 if (nb_args <= 1) {
2993 /* command completion */
2994 if (nb_args == 0)
2995 cmdname = "";
2996 else
2997 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00002998 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00002999 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003000 cmd_completion(cmdname, cmd->name);
3001 }
3002 } else {
3003 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003004 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003005 if (compare_cmd(args[0], cmd->name))
3006 goto found;
3007 }
3008 return;
3009 found:
3010 ptype = cmd->args_type;
3011 for(i = 0; i < nb_args - 2; i++) {
3012 if (*ptype != '\0') {
3013 ptype++;
3014 while (*ptype == '?')
3015 ptype++;
3016 }
3017 }
3018 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003019 if (*ptype == '-' && ptype[1] != '\0') {
3020 ptype += 2;
3021 }
bellard81d09122004-07-14 17:21:37 +00003022 switch(*ptype) {
3023 case 'F':
3024 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003025 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003026 file_completion(str);
3027 break;
3028 case 'B':
3029 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003030 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003031 bdrv_iterate(block_completion_it, (void *)str);
3032 break;
bellard7fe48482004-10-09 18:08:01 +00003033 case 's':
3034 /* XXX: more generic ? */
3035 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003036 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003037 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3038 cmd_completion(str, cmd->name);
3039 }
bellard64866c32006-05-07 18:03:31 +00003040 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003041 char *sep = strrchr(str, '-');
3042 if (sep)
3043 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003044 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003045 for(key = key_defs; key->name != NULL; key++) {
3046 cmd_completion(str, key->name);
3047 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003048 } else if (!strcmp(cmd->name, "help|?")) {
3049 readline_set_completion_index(cur_mon->rs, strlen(str));
3050 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3051 cmd_completion(str, cmd->name);
3052 }
bellard7fe48482004-10-09 18:08:01 +00003053 }
3054 break;
bellard81d09122004-07-14 17:21:37 +00003055 default:
3056 break;
3057 }
3058 }
3059 for(i = 0; i < nb_args; i++)
3060 qemu_free(args[i]);
3061}
3062
aliguori731b0362009-03-05 23:01:42 +00003063static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003064{
aliguori731b0362009-03-05 23:01:42 +00003065 Monitor *mon = opaque;
3066
3067 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003068}
3069
aliguori731b0362009-03-05 23:01:42 +00003070static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003071{
aliguori731b0362009-03-05 23:01:42 +00003072 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003073 int i;
aliguori376253e2009-03-05 23:01:23 +00003074
aliguori731b0362009-03-05 23:01:42 +00003075 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003076
aliguoricde76ee2009-03-05 23:01:51 +00003077 if (cur_mon->rs) {
3078 for (i = 0; i < size; i++)
3079 readline_handle_byte(cur_mon->rs, buf[i]);
3080 } else {
3081 if (size == 0 || buf[size - 1] != 0)
3082 monitor_printf(cur_mon, "corrupted command\n");
3083 else
3084 monitor_handle_command(cur_mon, (char *)buf);
3085 }
aliguori731b0362009-03-05 23:01:42 +00003086
3087 cur_mon = old_mon;
3088}
aliguorid8f44602008-10-06 13:52:44 +00003089
aliguori376253e2009-03-05 23:01:23 +00003090static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003091{
aliguori731b0362009-03-05 23:01:42 +00003092 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003093 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003094 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003095}
3096
aliguoricde76ee2009-03-05 23:01:51 +00003097int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003098{
aliguoricde76ee2009-03-05 23:01:51 +00003099 if (!mon->rs)
3100 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003101 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003102 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003103}
3104
aliguori376253e2009-03-05 23:01:23 +00003105void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003106{
aliguoricde76ee2009-03-05 23:01:51 +00003107 if (!mon->rs)
3108 return;
aliguori731b0362009-03-05 23:01:42 +00003109 if (--mon->suspend_cnt == 0)
3110 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003111}
3112
aliguori731b0362009-03-05 23:01:42 +00003113static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003114{
aliguori376253e2009-03-05 23:01:23 +00003115 Monitor *mon = opaque;
3116
aliguori2724b182009-03-05 23:01:47 +00003117 switch (event) {
3118 case CHR_EVENT_MUX_IN:
3119 readline_restart(mon->rs);
3120 monitor_resume(mon);
3121 monitor_flush(mon);
3122 break;
ths86e94de2007-01-05 22:01:59 +00003123
aliguori2724b182009-03-05 23:01:47 +00003124 case CHR_EVENT_MUX_OUT:
3125 if (mon->suspend_cnt == 0)
3126 monitor_printf(mon, "\n");
3127 monitor_flush(mon);
3128 monitor_suspend(mon);
3129 break;
3130
3131 case CHR_EVENT_RESET:
3132 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3133 "information\n", QEMU_VERSION);
3134 if (mon->chr->focus == 0)
3135 readline_show_prompt(mon->rs);
3136 break;
3137 }
ths86e94de2007-01-05 22:01:59 +00003138}
3139
aliguori76655d62009-03-06 20:27:37 +00003140
3141/*
3142 * Local variables:
3143 * c-indent-level: 4
3144 * c-basic-offset: 4
3145 * tab-width: 8
3146 * End:
3147 */
3148
aliguori731b0362009-03-05 23:01:42 +00003149void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003150{
aliguori731b0362009-03-05 23:01:42 +00003151 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003152 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003153
3154 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003155 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003156 is_first_init = 0;
3157 }
aliguori87127162009-03-05 23:01:29 +00003158
3159 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003160
aliguori87127162009-03-05 23:01:29 +00003161 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003162 mon->flags = flags;
aliguori2724b182009-03-05 23:01:47 +00003163 if (mon->chr->focus != 0)
3164 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
aliguoricde76ee2009-03-05 23:01:51 +00003165 if (flags & MONITOR_USE_READLINE) {
3166 mon->rs = readline_init(mon, monitor_find_completion);
3167 monitor_read_command(mon, 0);
3168 }
aliguori87127162009-03-05 23:01:29 +00003169
aliguori731b0362009-03-05 23:01:42 +00003170 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3171 mon);
aliguori87127162009-03-05 23:01:29 +00003172
3173 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003174 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003175 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003176}
3177
aliguori376253e2009-03-05 23:01:23 +00003178static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003179{
aliguoribb5fc202009-03-05 23:01:15 +00003180 BlockDriverState *bs = opaque;
3181 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003182
aliguoribb5fc202009-03-05 23:01:15 +00003183 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003184 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003185 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003186 }
aliguori731b0362009-03-05 23:01:42 +00003187 if (mon->password_completion_cb)
3188 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003189
aliguori731b0362009-03-05 23:01:42 +00003190 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003191}
aliguoric0f4ce72009-03-05 23:01:01 +00003192
aliguori376253e2009-03-05 23:01:23 +00003193void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003194 BlockDriverCompletionFunc *completion_cb,
3195 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003196{
aliguoricde76ee2009-03-05 23:01:51 +00003197 int err;
3198
aliguoribb5fc202009-03-05 23:01:15 +00003199 if (!bdrv_key_required(bs)) {
3200 if (completion_cb)
3201 completion_cb(opaque, 0);
3202 return;
3203 }
aliguoric0f4ce72009-03-05 23:01:01 +00003204
aliguori376253e2009-03-05 23:01:23 +00003205 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3206 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003207
aliguori731b0362009-03-05 23:01:42 +00003208 mon->password_completion_cb = completion_cb;
3209 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003210
aliguoricde76ee2009-03-05 23:01:51 +00003211 err = monitor_read_password(mon, bdrv_password_cb, bs);
3212
3213 if (err && completion_cb)
3214 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003215}