blob: 3d81fbc887c33c7e124d396b497499d6130e43ad [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 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw/hw.h"
25#include "hw/usb.h"
26#include "hw/pcmcia.h"
27#include "hw/pc.h"
28#include "hw/pci.h"
29#include "gdbstub.h"
30#include "net.h"
31#include "qemu-char.h"
32#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000033#include "monitor.h"
34#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000035#include "console.h"
36#include "block.h"
37#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000038#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000039#include "balloon.h"
bellard81d09122004-07-14 17:21:37 +000040#include <dirent.h>
balrogc8256f92008-06-08 22:45:01 +000041#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000042#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000043#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000044#include "acl.h"
ths6a5bd302007-12-03 17:05:38 +000045
bellard9dc39cb2004-03-14 21:38:27 +000046//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000047//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000048
bellard9307c4c2004-04-04 12:57:25 +000049/*
50 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000051 *
bellard9307c4c2004-04-04 12:57:25 +000052 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000053 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000054 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000055 * 'i' 32 bit integer
56 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000057 * '/' optional gdb-like print format (like "/10x")
58 *
59 * '?' optional type (for 'F', 's' and 'i')
60 *
61 */
62
aliguori376253e2009-03-05 23:01:23 +000063typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000064 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000065 const char *args_type;
blueswir1a5f1b962008-08-17 20:21:51 +000066 void *handler;
bellard9dc39cb2004-03-14 21:38:27 +000067 const char *params;
68 const char *help;
aliguori376253e2009-03-05 23:01:23 +000069} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000070
aliguori87127162009-03-05 23:01:29 +000071struct Monitor {
72 CharDriverState *chr;
aliguori731b0362009-03-05 23:01:42 +000073 int flags;
74 int suspend_cnt;
75 uint8_t outbuf[1024];
76 int outbuf_index;
77 ReadLineState *rs;
78 CPUState *mon_cpu;
79 BlockDriverCompletionFunc *password_completion_cb;
80 void *password_opaque;
aliguori87127162009-03-05 23:01:29 +000081 LIST_ENTRY(Monitor) entry;
82};
83
84static LIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +000085
aliguori376253e2009-03-05 23:01:23 +000086static const mon_cmd_t mon_cmds[];
87static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +000088
aliguori87127162009-03-05 23:01:29 +000089Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +000090
aliguori731b0362009-03-05 23:01:42 +000091static void monitor_command_cb(Monitor *mon, const char *cmdline,
92 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +000093
aliguori731b0362009-03-05 23:01:42 +000094static void monitor_read_command(Monitor *mon, int show_prompt)
95{
96 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
97 if (show_prompt)
98 readline_show_prompt(mon->rs);
99}
bellard6a00d602005-11-21 23:25:50 +0000100
aliguoricde76ee2009-03-05 23:01:51 +0000101static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
102 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000103{
aliguoricde76ee2009-03-05 23:01:51 +0000104 if (mon->rs) {
105 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
106 /* prompt is printed on return from the command handler */
107 return 0;
108 } else {
109 monitor_printf(mon, "terminal does not support password prompting\n");
110 return -ENOTTY;
111 }
aliguoribb5fc202009-03-05 23:01:15 +0000112}
113
aliguori376253e2009-03-05 23:01:23 +0000114void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000115{
aliguori731b0362009-03-05 23:01:42 +0000116 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
117 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
118 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000119 }
120}
121
122/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000123static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000124{
ths60fe76f2007-12-16 03:02:09 +0000125 char c;
aliguori731b0362009-03-05 23:01:42 +0000126
127 if (!mon)
128 return;
129
bellard7e2515e2004-08-01 21:52:19 +0000130 for(;;) {
131 c = *str++;
132 if (c == '\0')
133 break;
bellard7ba12602006-07-14 20:26:42 +0000134 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000135 mon->outbuf[mon->outbuf_index++] = '\r';
136 mon->outbuf[mon->outbuf_index++] = c;
137 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
138 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000139 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000140 }
141}
142
aliguori376253e2009-03-05 23:01:23 +0000143void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000144{
145 char buf[4096];
146 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000147 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000148}
149
aliguori376253e2009-03-05 23:01:23 +0000150void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000151{
152 va_list ap;
153 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000154 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000155 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000156}
157
aliguori376253e2009-03-05 23:01:23 +0000158void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000159{
160 int i;
161
162 for (i = 0; filename[i]; i++) {
163 switch (filename[i]) {
164 case ' ':
165 case '"':
166 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000167 monitor_printf(mon, "\\%c", filename[i]);
thsfef30742006-12-22 14:11:32 +0000168 break;
169 case '\t':
aliguori376253e2009-03-05 23:01:23 +0000170 monitor_printf(mon, "\\t");
thsfef30742006-12-22 14:11:32 +0000171 break;
172 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000173 monitor_printf(mon, "\\r");
thsfef30742006-12-22 14:11:32 +0000174 break;
175 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000176 monitor_printf(mon, "\\n");
thsfef30742006-12-22 14:11:32 +0000177 break;
178 default:
aliguori376253e2009-03-05 23:01:23 +0000179 monitor_printf(mon, "%c", filename[i]);
thsfef30742006-12-22 14:11:32 +0000180 break;
181 }
182 }
183}
184
bellard7fe48482004-10-09 18:08:01 +0000185static int monitor_fprintf(FILE *stream, const char *fmt, ...)
186{
187 va_list ap;
188 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000189 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000190 va_end(ap);
191 return 0;
192}
193
bellard9dc39cb2004-03-14 21:38:27 +0000194static int compare_cmd(const char *name, const char *list)
195{
196 const char *p, *pstart;
197 int len;
198 len = strlen(name);
199 p = list;
200 for(;;) {
201 pstart = p;
202 p = strchr(p, '|');
203 if (!p)
204 p = pstart + strlen(pstart);
205 if ((p - pstart) == len && !memcmp(pstart, name, len))
206 return 1;
207 if (*p == '\0')
208 break;
209 p++;
210 }
211 return 0;
212}
213
aliguori376253e2009-03-05 23:01:23 +0000214static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
215 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000216{
aliguori376253e2009-03-05 23:01:23 +0000217 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000218
219 for(cmd = cmds; cmd->name != NULL; cmd++) {
220 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000221 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
222 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000223 }
224}
225
aliguori376253e2009-03-05 23:01:23 +0000226static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000227{
228 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000229 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000230 } else {
aliguori376253e2009-03-05 23:01:23 +0000231 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000232 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000233 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000234 monitor_printf(mon, "Log items (comma separated):\n");
235 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000236 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000237 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000238 }
239 }
bellard9dc39cb2004-03-14 21:38:27 +0000240 }
241}
242
aliguori376253e2009-03-05 23:01:23 +0000243static void do_commit(Monitor *mon, const char *device)
bellard9dc39cb2004-03-14 21:38:27 +0000244{
bellard7954c732006-08-01 15:52:40 +0000245 int i, all_devices;
balrog2dc7b602007-05-24 18:53:22 +0000246
bellard7954c732006-08-01 15:52:40 +0000247 all_devices = !strcmp(device, "all");
thse4bcb142007-12-02 04:51:10 +0000248 for (i = 0; i < nb_drives; i++) {
ths5fafdf22007-09-16 21:08:06 +0000249 if (all_devices ||
thse4bcb142007-12-02 04:51:10 +0000250 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
251 bdrv_commit(drives_table[i].bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000252 }
253}
254
aliguori376253e2009-03-05 23:01:23 +0000255static void do_info(Monitor *mon, const char *item)
bellard9dc39cb2004-03-14 21:38:27 +0000256{
aliguori376253e2009-03-05 23:01:23 +0000257 const mon_cmd_t *cmd;
258 void (*handler)(Monitor *);
bellard9dc39cb2004-03-14 21:38:27 +0000259
bellard9307c4c2004-04-04 12:57:25 +0000260 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000261 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000262 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000263 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000264 goto found;
265 }
266 help:
aliguori376253e2009-03-05 23:01:23 +0000267 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000268 return;
269 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000270 handler = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +0000271 handler(mon);
bellard9dc39cb2004-03-14 21:38:27 +0000272}
273
aliguori376253e2009-03-05 23:01:23 +0000274static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000275{
aliguori376253e2009-03-05 23:01:23 +0000276 monitor_printf(mon, "%s\n", QEMU_VERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000277}
278
aliguori376253e2009-03-05 23:01:23 +0000279static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000280{
281 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000282 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000283}
284
aurel32bf4f74c2008-12-18 22:42:34 +0000285#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000286static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000287{
aliguori376253e2009-03-05 23:01:23 +0000288 monitor_printf(mon, "HPET is %s by QEMU\n",
289 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000290}
aurel32bf4f74c2008-12-18 22:42:34 +0000291#endif
aliguori16b29ae2008-12-17 23:28:44 +0000292
aliguori376253e2009-03-05 23:01:23 +0000293static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000294{
aliguori376253e2009-03-05 23:01:23 +0000295 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
296 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
297 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
298 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
299 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000300}
301
bellard6a00d602005-11-21 23:25:50 +0000302/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000303static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000304{
305 CPUState *env;
306
307 for(env = first_cpu; env != NULL; env = env->next_cpu) {
308 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000309 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000310 return 0;
311 }
312 }
313 return -1;
314}
315
pbrook9596ebb2007-11-18 01:44:38 +0000316static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000317{
aliguori731b0362009-03-05 23:01:42 +0000318 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000319 mon_set_cpu(0);
320 }
aliguori731b0362009-03-05 23:01:42 +0000321 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000322}
323
aliguori376253e2009-03-05 23:01:23 +0000324static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000325{
bellard6a00d602005-11-21 23:25:50 +0000326 CPUState *env;
327 env = mon_get_cpu();
328 if (!env)
329 return;
bellard9307c4c2004-04-04 12:57:25 +0000330#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000331 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000332 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000333#else
aliguori376253e2009-03-05 23:01:23 +0000334 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000335 0);
bellard9307c4c2004-04-04 12:57:25 +0000336#endif
337}
338
aliguori376253e2009-03-05 23:01:23 +0000339static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000340{
341 CPUState *env;
342
343 /* just to set the default cpu if not already done */
344 mon_get_cpu();
345
346 for(env = first_cpu; env != NULL; env = env->next_cpu) {
aliguori376253e2009-03-05 23:01:23 +0000347 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000348 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000349 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000350#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000351 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
352 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000353#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000354 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000355#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000356 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
357 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000358#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000359 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000360#endif
thsead93602007-09-06 00:18:15 +0000361 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000362 monitor_printf(mon, " (halted)");
363 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000364 }
365}
366
aliguori376253e2009-03-05 23:01:23 +0000367static void do_cpu_set(Monitor *mon, int index)
bellard6a00d602005-11-21 23:25:50 +0000368{
369 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000370 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000371}
372
aliguori376253e2009-03-05 23:01:23 +0000373static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000374{
aliguori376253e2009-03-05 23:01:23 +0000375 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000376}
377
aliguori376253e2009-03-05 23:01:23 +0000378static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000379{
380 int i;
bellard7e2515e2004-08-01 21:52:19 +0000381 const char *str;
ths3b46e622007-09-17 08:09:54 +0000382
aliguoricde76ee2009-03-05 23:01:51 +0000383 if (!mon->rs)
384 return;
bellard7e2515e2004-08-01 21:52:19 +0000385 i = 0;
386 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000387 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000388 if (!str)
389 break;
aliguori376253e2009-03-05 23:01:23 +0000390 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000391 i++;
bellardaa455482004-04-04 13:07:25 +0000392 }
393}
394
j_mayer76a66252007-03-07 08:32:30 +0000395#if defined(TARGET_PPC)
396/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000397static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000398{
399 CPUState *env;
400
401 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000402 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000403}
404#endif
405
aliguori376253e2009-03-05 23:01:23 +0000406static void do_quit(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000407{
408 exit(0);
409}
410
aliguori376253e2009-03-05 23:01:23 +0000411static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000412{
413 if (bdrv_is_inserted(bs)) {
414 if (!force) {
415 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000416 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000417 return -1;
418 }
419 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000420 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000421 return -1;
422 }
423 }
424 bdrv_close(bs);
425 }
426 return 0;
427}
428
aliguori376253e2009-03-05 23:01:23 +0000429static void do_eject(Monitor *mon, int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000430{
431 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000432
bellard9307c4c2004-04-04 12:57:25 +0000433 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000434 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000435 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000436 return;
437 }
aliguori376253e2009-03-05 23:01:23 +0000438 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000439}
440
aliguori376253e2009-03-05 23:01:23 +0000441static void do_change_block(Monitor *mon, const char *device,
442 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000443{
444 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000445 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000446
bellard9307c4c2004-04-04 12:57:25 +0000447 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000448 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000449 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000450 return;
451 }
aurel322ecea9b2008-06-18 22:10:01 +0000452 if (fmt) {
453 drv = bdrv_find_format(fmt);
454 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000455 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000456 return;
457 }
458 }
aliguori376253e2009-03-05 23:01:23 +0000459 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000460 return;
aurel322ecea9b2008-06-18 22:10:01 +0000461 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000462 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000463}
464
aliguori376253e2009-03-05 23:01:23 +0000465static void change_vnc_password_cb(Monitor *mon, const char *password,
466 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000467{
468 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000469 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000470
aliguori731b0362009-03-05 23:01:42 +0000471 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000472}
473
aliguori376253e2009-03-05 23:01:23 +0000474static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000475{
ths70848512007-08-25 01:37:05 +0000476 if (strcmp(target, "passwd") == 0 ||
477 strcmp(target, "password") == 0) {
aliguori2569da02008-12-10 15:14:13 +0000478 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000479 char password[9];
aliguori2569da02008-12-10 15:14:13 +0000480 strncpy(password, arg, sizeof(password));
481 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000482 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000483 } else {
aliguori376253e2009-03-05 23:01:23 +0000484 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000485 }
ths70848512007-08-25 01:37:05 +0000486 } else {
487 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000488 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000489 }
thse25a5822007-08-25 01:36:20 +0000490}
491
aliguori376253e2009-03-05 23:01:23 +0000492static void do_change(Monitor *mon, const char *device, const char *target,
493 const char *arg)
thse25a5822007-08-25 01:36:20 +0000494{
495 if (strcmp(device, "vnc") == 0) {
aliguori376253e2009-03-05 23:01:23 +0000496 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000497 } else {
aliguori376253e2009-03-05 23:01:23 +0000498 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000499 }
500}
501
aliguori376253e2009-03-05 23:01:23 +0000502static void do_screen_dump(Monitor *mon, const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000503{
pbrook95219892006-04-09 01:06:34 +0000504 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000505}
506
aliguori376253e2009-03-05 23:01:23 +0000507static void do_logfile(Monitor *mon, const char *filename)
pbrooke735b912007-06-30 13:53:24 +0000508{
509 cpu_set_log_filename(filename);
510}
511
aliguori376253e2009-03-05 23:01:23 +0000512static void do_log(Monitor *mon, const char *items)
bellardf193c792004-03-21 17:06:25 +0000513{
514 int mask;
ths3b46e622007-09-17 08:09:54 +0000515
bellard9307c4c2004-04-04 12:57:25 +0000516 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000517 mask = 0;
518 } else {
bellard9307c4c2004-04-04 12:57:25 +0000519 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000520 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000521 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000522 return;
523 }
524 }
525 cpu_set_log(mask);
526}
527
aliguori376253e2009-03-05 23:01:23 +0000528static void do_stop(Monitor *mon)
bellard8a7ddc32004-03-31 19:00:16 +0000529{
530 vm_stop(EXCP_INTERRUPT);
531}
532
aliguoribb5fc202009-03-05 23:01:15 +0000533static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000534
aliguori376253e2009-03-05 23:01:23 +0000535struct bdrv_iterate_context {
536 Monitor *mon;
537 int err;
538};
aliguoric0f4ce72009-03-05 23:01:01 +0000539
aliguori376253e2009-03-05 23:01:23 +0000540static void do_cont(Monitor *mon)
541{
542 struct bdrv_iterate_context context = { mon, 0 };
543
544 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000545 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000546 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000547 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000548}
549
aliguoribb5fc202009-03-05 23:01:15 +0000550static void bdrv_key_cb(void *opaque, int err)
551{
aliguori376253e2009-03-05 23:01:23 +0000552 Monitor *mon = opaque;
553
aliguoribb5fc202009-03-05 23:01:15 +0000554 /* another key was set successfully, retry to continue */
555 if (!err)
aliguori376253e2009-03-05 23:01:23 +0000556 do_cont(mon);
aliguoribb5fc202009-03-05 23:01:15 +0000557}
558
559static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
560{
aliguori376253e2009-03-05 23:01:23 +0000561 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000562
aliguori376253e2009-03-05 23:01:23 +0000563 if (!context->err && bdrv_key_required(bs)) {
564 context->err = -EBUSY;
565 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
566 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000567 }
568}
569
bellard67b915a2004-03-31 23:37:16 +0000570#ifdef CONFIG_GDBSTUB
aliguori376253e2009-03-05 23:01:23 +0000571static void do_gdbserver(Monitor *mon, const char *port)
bellard8a7ddc32004-03-31 19:00:16 +0000572{
pbrookcfc34752007-02-22 01:48:01 +0000573 if (!port)
bellard9307c4c2004-04-04 12:57:25 +0000574 port = DEFAULT_GDBSTUB_PORT;
pbrookcfc34752007-02-22 01:48:01 +0000575 if (gdbserver_start(port) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000576 monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",
577 port);
bellard8a7ddc32004-03-31 19:00:16 +0000578 } else {
aliguori376253e2009-03-05 23:01:23 +0000579 monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000580 }
581}
bellard67b915a2004-03-31 23:37:16 +0000582#endif
bellard8a7ddc32004-03-31 19:00:16 +0000583
aliguori376253e2009-03-05 23:01:23 +0000584static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000585{
aliguori376253e2009-03-05 23:01:23 +0000586 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000587 switch(c) {
588 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000589 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000590 break;
591 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000592 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000593 break;
594 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000595 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000596 break;
597 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000598 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000599 break;
600 default:
601 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000602 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000603 } else {
aliguori376253e2009-03-05 23:01:23 +0000604 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000605 }
606 break;
607 }
aliguori376253e2009-03-05 23:01:23 +0000608 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000609}
610
aliguori376253e2009-03-05 23:01:23 +0000611static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000612 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000613{
bellard6a00d602005-11-21 23:25:50 +0000614 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000615 int nb_per_line, l, line_size, i, max_digits, len;
616 uint8_t buf[16];
617 uint64_t v;
618
619 if (format == 'i') {
620 int flags;
621 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000622 env = mon_get_cpu();
623 if (!env && !is_physical)
624 return;
bellard9307c4c2004-04-04 12:57:25 +0000625#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000626 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000627 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000628 } else if (wsize == 4) {
629 flags = 0;
630 } else {
bellard6a15fd12006-04-12 21:07:07 +0000631 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000632 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000633 if (env) {
634#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000635 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000636 (env->segs[R_CS].flags & DESC_L_MASK))
637 flags = 2;
638 else
639#endif
640 if (!(env->segs[R_CS].flags & DESC_B_MASK))
641 flags = 1;
642 }
bellard4c27ba22004-04-25 18:05:08 +0000643 }
644#endif
aliguori376253e2009-03-05 23:01:23 +0000645 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000646 return;
647 }
648
649 len = wsize * count;
650 if (wsize == 1)
651 line_size = 8;
652 else
653 line_size = 16;
654 nb_per_line = line_size / wsize;
655 max_digits = 0;
656
657 switch(format) {
658 case 'o':
659 max_digits = (wsize * 8 + 2) / 3;
660 break;
661 default:
662 case 'x':
663 max_digits = (wsize * 8) / 4;
664 break;
665 case 'u':
666 case 'd':
667 max_digits = (wsize * 8 * 10 + 32) / 33;
668 break;
669 case 'c':
670 wsize = 1;
671 break;
672 }
673
674 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000675 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000676 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000677 else
aliguori376253e2009-03-05 23:01:23 +0000678 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000679 l = len;
680 if (l > line_size)
681 l = line_size;
682 if (is_physical) {
683 cpu_physical_memory_rw(addr, buf, l, 0);
684 } else {
bellard6a00d602005-11-21 23:25:50 +0000685 env = mon_get_cpu();
686 if (!env)
687 break;
aliguoric8f79b62008-08-18 14:00:20 +0000688 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000689 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000690 break;
691 }
bellard9307c4c2004-04-04 12:57:25 +0000692 }
ths5fafdf22007-09-16 21:08:06 +0000693 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000694 while (i < l) {
695 switch(wsize) {
696 default:
697 case 1:
698 v = ldub_raw(buf + i);
699 break;
700 case 2:
701 v = lduw_raw(buf + i);
702 break;
703 case 4:
bellard92a31b12005-02-10 22:00:52 +0000704 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000705 break;
706 case 8:
707 v = ldq_raw(buf + i);
708 break;
709 }
aliguori376253e2009-03-05 23:01:23 +0000710 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000711 switch(format) {
712 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000713 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000714 break;
715 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000716 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000717 break;
718 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000719 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000720 break;
721 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000722 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000723 break;
724 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000725 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000726 break;
727 }
728 i += wsize;
729 }
aliguori376253e2009-03-05 23:01:23 +0000730 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000731 addr += l;
732 len -= l;
733 }
734}
735
bellard92a31b12005-02-10 22:00:52 +0000736#if TARGET_LONG_BITS == 64
737#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
738#else
739#define GET_TLONG(h, l) (l)
740#endif
741
aliguori376253e2009-03-05 23:01:23 +0000742static void do_memory_dump(Monitor *mon, int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000743 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000744{
bellard92a31b12005-02-10 22:00:52 +0000745 target_long addr = GET_TLONG(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000746 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000747}
748
blueswir17743e582007-09-24 18:39:04 +0000749#if TARGET_PHYS_ADDR_BITS > 32
750#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
751#else
752#define GET_TPHYSADDR(h, l) (l)
753#endif
754
aliguori376253e2009-03-05 23:01:23 +0000755static void do_physical_memory_dump(Monitor *mon, int count, int format,
756 int size, uint32_t addrh, uint32_t addrl)
bellard92a31b12005-02-10 22:00:52 +0000757
bellard9307c4c2004-04-04 12:57:25 +0000758{
blueswir17743e582007-09-24 18:39:04 +0000759 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000760 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000761}
762
aliguori376253e2009-03-05 23:01:23 +0000763static void do_print(Monitor *mon, int count, int format, int size,
764 unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000765{
blueswir17743e582007-09-24 18:39:04 +0000766 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
767#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000768 switch(format) {
769 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000770 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000771 break;
772 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000773 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000774 break;
775 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000776 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000777 break;
778 default:
779 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000780 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000781 break;
782 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000783 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000784 break;
785 }
bellard92a31b12005-02-10 22:00:52 +0000786#else
787 switch(format) {
788 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000789 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000790 break;
791 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000792 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000793 break;
794 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000795 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000796 break;
797 default:
798 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000799 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000800 break;
801 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000802 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000803 break;
804 }
805#endif
aliguori376253e2009-03-05 23:01:23 +0000806 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000807}
808
aliguori376253e2009-03-05 23:01:23 +0000809static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000810 uint32_t size, const char *filename)
811{
812 FILE *f;
813 target_long addr = GET_TLONG(valh, vall);
814 uint32_t l;
815 CPUState *env;
816 uint8_t buf[1024];
817
818 env = mon_get_cpu();
819 if (!env)
820 return;
821
822 f = fopen(filename, "wb");
823 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000824 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000825 return;
826 }
827 while (size != 0) {
828 l = sizeof(buf);
829 if (l > size)
830 l = size;
831 cpu_memory_rw_debug(env, addr, buf, l, 0);
832 fwrite(buf, 1, l, f);
833 addr += l;
834 size -= l;
835 }
836 fclose(f);
837}
838
aliguori376253e2009-03-05 23:01:23 +0000839static void do_physical_memory_save(Monitor *mon, unsigned int valh,
840 unsigned int vall, uint32_t size,
841 const char *filename)
aurel32a8bdf7a2008-04-11 21:36:14 +0000842{
843 FILE *f;
844 uint32_t l;
845 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000846 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000847
848 f = fopen(filename, "wb");
849 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000850 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000851 return;
852 }
853 while (size != 0) {
854 l = sizeof(buf);
855 if (l > size)
856 l = size;
857 cpu_physical_memory_rw(addr, buf, l, 0);
858 fwrite(buf, 1, l, f);
859 fflush(f);
860 addr += l;
861 size -= l;
862 }
863 fclose(f);
864}
865
aliguori376253e2009-03-05 23:01:23 +0000866static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
bellarde4cf1ad2005-06-04 20:15:57 +0000867{
868 uint32_t addr;
869 uint8_t buf[1];
870 uint16_t sum;
871
872 sum = 0;
873 for(addr = start; addr < (start + size); addr++) {
874 cpu_physical_memory_rw(addr, buf, 1, 0);
875 /* BSD sum algorithm ('sum' Unix command) */
876 sum = (sum >> 1) | (sum << 15);
877 sum += buf[0];
878 }
aliguori376253e2009-03-05 23:01:23 +0000879 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000880}
881
bellarda3a91a32004-06-04 11:06:21 +0000882typedef struct {
883 int keycode;
884 const char *name;
885} KeyDef;
886
887static const KeyDef key_defs[] = {
888 { 0x2a, "shift" },
889 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000890
bellarda3a91a32004-06-04 11:06:21 +0000891 { 0x38, "alt" },
892 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000893 { 0x64, "altgr" },
894 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000895 { 0x1d, "ctrl" },
896 { 0x9d, "ctrl_r" },
897
898 { 0xdd, "menu" },
899
900 { 0x01, "esc" },
901
902 { 0x02, "1" },
903 { 0x03, "2" },
904 { 0x04, "3" },
905 { 0x05, "4" },
906 { 0x06, "5" },
907 { 0x07, "6" },
908 { 0x08, "7" },
909 { 0x09, "8" },
910 { 0x0a, "9" },
911 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000912 { 0x0c, "minus" },
913 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000914 { 0x0e, "backspace" },
915
916 { 0x0f, "tab" },
917 { 0x10, "q" },
918 { 0x11, "w" },
919 { 0x12, "e" },
920 { 0x13, "r" },
921 { 0x14, "t" },
922 { 0x15, "y" },
923 { 0x16, "u" },
924 { 0x17, "i" },
925 { 0x18, "o" },
926 { 0x19, "p" },
927
928 { 0x1c, "ret" },
929
930 { 0x1e, "a" },
931 { 0x1f, "s" },
932 { 0x20, "d" },
933 { 0x21, "f" },
934 { 0x22, "g" },
935 { 0x23, "h" },
936 { 0x24, "j" },
937 { 0x25, "k" },
938 { 0x26, "l" },
939
940 { 0x2c, "z" },
941 { 0x2d, "x" },
942 { 0x2e, "c" },
943 { 0x2f, "v" },
944 { 0x30, "b" },
945 { 0x31, "n" },
946 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000947 { 0x33, "comma" },
948 { 0x34, "dot" },
949 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000950
balrog4d3b6f62008-02-10 16:33:14 +0000951 { 0x37, "asterisk" },
952
bellarda3a91a32004-06-04 11:06:21 +0000953 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000954 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000955 { 0x3b, "f1" },
956 { 0x3c, "f2" },
957 { 0x3d, "f3" },
958 { 0x3e, "f4" },
959 { 0x3f, "f5" },
960 { 0x40, "f6" },
961 { 0x41, "f7" },
962 { 0x42, "f8" },
963 { 0x43, "f9" },
964 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +0000965 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000966 { 0x46, "scroll_lock" },
967
bellard64866c32006-05-07 18:03:31 +0000968 { 0xb5, "kp_divide" },
969 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +0000970 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +0000971 { 0x4e, "kp_add" },
972 { 0x9c, "kp_enter" },
973 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +0000974 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +0000975
976 { 0x52, "kp_0" },
977 { 0x4f, "kp_1" },
978 { 0x50, "kp_2" },
979 { 0x51, "kp_3" },
980 { 0x4b, "kp_4" },
981 { 0x4c, "kp_5" },
982 { 0x4d, "kp_6" },
983 { 0x47, "kp_7" },
984 { 0x48, "kp_8" },
985 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +0000986
bellarda3a91a32004-06-04 11:06:21 +0000987 { 0x56, "<" },
988
989 { 0x57, "f11" },
990 { 0x58, "f12" },
991
992 { 0xb7, "print" },
993
994 { 0xc7, "home" },
995 { 0xc9, "pgup" },
996 { 0xd1, "pgdn" },
997 { 0xcf, "end" },
998
999 { 0xcb, "left" },
1000 { 0xc8, "up" },
1001 { 0xd0, "down" },
1002 { 0xcd, "right" },
1003
1004 { 0xd2, "insert" },
1005 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001006#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1007 { 0xf0, "stop" },
1008 { 0xf1, "again" },
1009 { 0xf2, "props" },
1010 { 0xf3, "undo" },
1011 { 0xf4, "front" },
1012 { 0xf5, "copy" },
1013 { 0xf6, "open" },
1014 { 0xf7, "paste" },
1015 { 0xf8, "find" },
1016 { 0xf9, "cut" },
1017 { 0xfa, "lf" },
1018 { 0xfb, "help" },
1019 { 0xfc, "meta_l" },
1020 { 0xfd, "meta_r" },
1021 { 0xfe, "compose" },
1022#endif
bellarda3a91a32004-06-04 11:06:21 +00001023 { 0, NULL },
1024};
1025
1026static int get_keycode(const char *key)
1027{
1028 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001029 char *endp;
1030 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001031
1032 for(p = key_defs; p->name != NULL; p++) {
1033 if (!strcmp(key, p->name))
1034 return p->keycode;
1035 }
bellard64866c32006-05-07 18:03:31 +00001036 if (strstart(key, "0x", NULL)) {
1037 ret = strtoul(key, &endp, 0);
1038 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1039 return ret;
1040 }
bellarda3a91a32004-06-04 11:06:21 +00001041 return -1;
1042}
1043
balrogc8256f92008-06-08 22:45:01 +00001044#define MAX_KEYCODES 16
1045static uint8_t keycodes[MAX_KEYCODES];
1046static int nb_pending_keycodes;
1047static QEMUTimer *key_timer;
1048
1049static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001050{
balrogc8256f92008-06-08 22:45:01 +00001051 int keycode;
1052
1053 while (nb_pending_keycodes > 0) {
1054 nb_pending_keycodes--;
1055 keycode = keycodes[nb_pending_keycodes];
1056 if (keycode & 0x80)
1057 kbd_put_keycode(0xe0);
1058 kbd_put_keycode(keycode | 0x80);
1059 }
1060}
1061
aliguori376253e2009-03-05 23:01:23 +00001062static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1063 int hold_time)
balrogc8256f92008-06-08 22:45:01 +00001064{
balrog3401c0d2008-06-04 10:05:59 +00001065 char keyname_buf[16];
1066 char *separator;
1067 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +00001068
balrogc8256f92008-06-08 22:45:01 +00001069 if (nb_pending_keycodes > 0) {
1070 qemu_del_timer(key_timer);
1071 release_keys(NULL);
1072 }
1073 if (!has_hold_time)
1074 hold_time = 100;
1075 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001076 while (1) {
1077 separator = strchr(string, '-');
1078 keyname_len = separator ? separator - string : strlen(string);
1079 if (keyname_len > 0) {
1080 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1081 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001082 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001083 return;
bellarda3a91a32004-06-04 11:06:21 +00001084 }
balrogc8256f92008-06-08 22:45:01 +00001085 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001086 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001087 return;
1088 }
1089 keyname_buf[keyname_len] = 0;
1090 keycode = get_keycode(keyname_buf);
1091 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001092 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001093 return;
1094 }
balrogc8256f92008-06-08 22:45:01 +00001095 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001096 }
balrog3401c0d2008-06-04 10:05:59 +00001097 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001098 break;
balrog3401c0d2008-06-04 10:05:59 +00001099 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001100 }
balrogc8256f92008-06-08 22:45:01 +00001101 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001102 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001103 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001104 keycode = keycodes[i];
1105 if (keycode & 0x80)
1106 kbd_put_keycode(0xe0);
1107 kbd_put_keycode(keycode & 0x7f);
1108 }
balrogc8256f92008-06-08 22:45:01 +00001109 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001110 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1111 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001112}
1113
bellard13224a82006-07-14 22:03:35 +00001114static int mouse_button_state;
1115
aliguori376253e2009-03-05 23:01:23 +00001116static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001117 const char *dz_str)
1118{
1119 int dx, dy, dz;
1120 dx = strtol(dx_str, NULL, 0);
1121 dy = strtol(dy_str, NULL, 0);
1122 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001123 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001124 dz = strtol(dz_str, NULL, 0);
1125 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1126}
1127
aliguori376253e2009-03-05 23:01:23 +00001128static void do_mouse_button(Monitor *mon, int button_state)
bellard13224a82006-07-14 22:03:35 +00001129{
1130 mouse_button_state = button_state;
1131 kbd_mouse_event(0, 0, 0, mouse_button_state);
1132}
1133
aliguori376253e2009-03-05 23:01:23 +00001134static void do_ioport_read(Monitor *mon, int count, int format, int size,
1135 int addr, int has_index, int index)
bellard34405572004-06-08 00:55:58 +00001136{
1137 uint32_t val;
1138 int suffix;
1139
1140 if (has_index) {
1141 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1142 addr++;
1143 }
1144 addr &= 0xffff;
1145
1146 switch(size) {
1147 default:
1148 case 1:
1149 val = cpu_inb(NULL, addr);
1150 suffix = 'b';
1151 break;
1152 case 2:
1153 val = cpu_inw(NULL, addr);
1154 suffix = 'w';
1155 break;
1156 case 4:
1157 val = cpu_inl(NULL, addr);
1158 suffix = 'l';
1159 break;
1160 }
aliguori376253e2009-03-05 23:01:23 +00001161 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1162 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001163}
bellarda3a91a32004-06-04 11:06:21 +00001164
blueswir13b4366d2008-06-20 16:25:06 +00001165/* boot_set handler */
1166static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1167static void *boot_opaque;
1168
1169void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1170{
1171 qemu_boot_set_handler = func;
1172 boot_opaque = opaque;
1173}
1174
aliguori376253e2009-03-05 23:01:23 +00001175static void do_boot_set(Monitor *mon, const char *bootdevice)
aurel320ecdffb2008-05-04 20:11:34 +00001176{
1177 int res;
1178
1179 if (qemu_boot_set_handler) {
blueswir13b4366d2008-06-20 16:25:06 +00001180 res = qemu_boot_set_handler(boot_opaque, bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001181 if (res == 0)
aliguori376253e2009-03-05 23:01:23 +00001182 monitor_printf(mon, "boot device list now set to %s\n",
1183 bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001184 else
aliguori376253e2009-03-05 23:01:23 +00001185 monitor_printf(mon, "setting boot device list failed with "
1186 "error %i\n", res);
aurel320ecdffb2008-05-04 20:11:34 +00001187 } else {
aliguori376253e2009-03-05 23:01:23 +00001188 monitor_printf(mon, "no function defined to set boot device list for "
1189 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001190 }
1191}
1192
aliguori376253e2009-03-05 23:01:23 +00001193static void do_system_reset(Monitor *mon)
bellarde4f90822004-06-20 12:35:44 +00001194{
1195 qemu_system_reset_request();
1196}
1197
aliguori376253e2009-03-05 23:01:23 +00001198static void do_system_powerdown(Monitor *mon)
bellard34751872005-07-02 14:31:34 +00001199{
1200 qemu_system_powerdown_request();
1201}
1202
bellardb86bda52004-09-18 19:32:46 +00001203#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001204static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001205{
aliguori376253e2009-03-05 23:01:23 +00001206 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1207 addr,
1208 pte & mask,
1209 pte & PG_GLOBAL_MASK ? 'G' : '-',
1210 pte & PG_PSE_MASK ? 'P' : '-',
1211 pte & PG_DIRTY_MASK ? 'D' : '-',
1212 pte & PG_ACCESSED_MASK ? 'A' : '-',
1213 pte & PG_PCD_MASK ? 'C' : '-',
1214 pte & PG_PWT_MASK ? 'T' : '-',
1215 pte & PG_USER_MASK ? 'U' : '-',
1216 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001217}
1218
aliguori376253e2009-03-05 23:01:23 +00001219static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001220{
bellard6a00d602005-11-21 23:25:50 +00001221 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001222 int l1, l2;
1223 uint32_t pgd, pde, pte;
1224
bellard6a00d602005-11-21 23:25:50 +00001225 env = mon_get_cpu();
1226 if (!env)
1227 return;
1228
bellardb86bda52004-09-18 19:32:46 +00001229 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001230 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001231 return;
1232 }
1233 pgd = env->cr[3] & ~0xfff;
1234 for(l1 = 0; l1 < 1024; l1++) {
1235 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1236 pde = le32_to_cpu(pde);
1237 if (pde & PG_PRESENT_MASK) {
1238 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001239 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001240 } else {
1241 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001242 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001243 (uint8_t *)&pte, 4);
1244 pte = le32_to_cpu(pte);
1245 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001246 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001247 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001248 ~0xfff);
1249 }
1250 }
1251 }
1252 }
1253 }
1254}
1255
aliguori376253e2009-03-05 23:01:23 +00001256static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001257 uint32_t end, int prot)
1258{
bellard9746b152004-11-11 18:30:24 +00001259 int prot1;
1260 prot1 = *plast_prot;
1261 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001262 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001263 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1264 *pstart, end, end - *pstart,
1265 prot1 & PG_USER_MASK ? 'u' : '-',
1266 'r',
1267 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001268 }
1269 if (prot != 0)
1270 *pstart = end;
1271 else
1272 *pstart = -1;
1273 *plast_prot = prot;
1274 }
1275}
1276
aliguori376253e2009-03-05 23:01:23 +00001277static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001278{
bellard6a00d602005-11-21 23:25:50 +00001279 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001280 int l1, l2, prot, last_prot;
1281 uint32_t pgd, pde, pte, start, end;
1282
bellard6a00d602005-11-21 23:25:50 +00001283 env = mon_get_cpu();
1284 if (!env)
1285 return;
1286
bellardb86bda52004-09-18 19:32:46 +00001287 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001288 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001289 return;
1290 }
1291 pgd = env->cr[3] & ~0xfff;
1292 last_prot = 0;
1293 start = -1;
1294 for(l1 = 0; l1 < 1024; l1++) {
1295 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1296 pde = le32_to_cpu(pde);
1297 end = l1 << 22;
1298 if (pde & PG_PRESENT_MASK) {
1299 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1300 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001301 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001302 } else {
1303 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001304 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001305 (uint8_t *)&pte, 4);
1306 pte = le32_to_cpu(pte);
1307 end = (l1 << 22) + (l2 << 12);
1308 if (pte & PG_PRESENT_MASK) {
1309 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1310 } else {
1311 prot = 0;
1312 }
aliguori376253e2009-03-05 23:01:23 +00001313 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001314 }
1315 }
1316 } else {
1317 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001318 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001319 }
1320 }
1321}
1322#endif
1323
aurel327c664e22009-03-03 06:12:22 +00001324#if defined(TARGET_SH4)
1325
aliguori376253e2009-03-05 23:01:23 +00001326static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001327{
aliguori376253e2009-03-05 23:01:23 +00001328 monitor_printf(mon, " tlb%i:\t"
1329 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1330 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1331 "dirty=%hhu writethrough=%hhu\n",
1332 idx,
1333 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1334 tlb->v, tlb->sh, tlb->c, tlb->pr,
1335 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001336}
1337
aliguori376253e2009-03-05 23:01:23 +00001338static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001339{
1340 CPUState *env = mon_get_cpu();
1341 int i;
1342
aliguori376253e2009-03-05 23:01:23 +00001343 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001344 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001345 print_tlb (mon, i, &env->itlb[i]);
1346 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001347 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001348 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001349}
1350
1351#endif
1352
aliguori376253e2009-03-05 23:01:23 +00001353static void do_info_kqemu(Monitor *mon)
bellard0f4c6412005-07-24 18:10:56 +00001354{
1355#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001356 CPUState *env;
bellard0f4c6412005-07-24 18:10:56 +00001357 int val;
1358 val = 0;
bellard6a00d602005-11-21 23:25:50 +00001359 env = mon_get_cpu();
1360 if (!env) {
aliguori376253e2009-03-05 23:01:23 +00001361 monitor_printf(mon, "No cpu initialized yet");
bellard6a00d602005-11-21 23:25:50 +00001362 return;
1363 }
1364 val = env->kqemu_enabled;
aliguori376253e2009-03-05 23:01:23 +00001365 monitor_printf(mon, "kqemu support: ");
bellard5f1ce942006-02-08 22:40:15 +00001366 switch(val) {
1367 default:
1368 case 0:
aliguori376253e2009-03-05 23:01:23 +00001369 monitor_printf(mon, "disabled\n");
bellard5f1ce942006-02-08 22:40:15 +00001370 break;
1371 case 1:
aliguori376253e2009-03-05 23:01:23 +00001372 monitor_printf(mon, "enabled for user code\n");
bellard5f1ce942006-02-08 22:40:15 +00001373 break;
1374 case 2:
aliguori376253e2009-03-05 23:01:23 +00001375 monitor_printf(mon, "enabled for user and kernel code\n");
bellard5f1ce942006-02-08 22:40:15 +00001376 break;
1377 }
bellard0f4c6412005-07-24 18:10:56 +00001378#else
aliguori376253e2009-03-05 23:01:23 +00001379 monitor_printf(mon, "kqemu support: not compiled\n");
bellard0f4c6412005-07-24 18:10:56 +00001380#endif
ths5fafdf22007-09-16 21:08:06 +00001381}
bellard0f4c6412005-07-24 18:10:56 +00001382
aliguori376253e2009-03-05 23:01:23 +00001383static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001384{
1385#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001386 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001387 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001388 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001389 else
aliguori376253e2009-03-05 23:01:23 +00001390 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001391#else
aliguori376253e2009-03-05 23:01:23 +00001392 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001393#endif
1394}
1395
bellard5f1ce942006-02-08 22:40:15 +00001396#ifdef CONFIG_PROFILER
1397
1398int64_t kqemu_time;
1399int64_t qemu_time;
1400int64_t kqemu_exec_count;
1401int64_t dev_time;
1402int64_t kqemu_ret_int_count;
1403int64_t kqemu_ret_excp_count;
1404int64_t kqemu_ret_intr_count;
1405
aliguori376253e2009-03-05 23:01:23 +00001406static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001407{
1408 int64_t total;
1409 total = qemu_time;
1410 if (total == 0)
1411 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001412 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1413 dev_time, dev_time / (double)ticks_per_sec);
1414 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1415 qemu_time, qemu_time / (double)ticks_per_sec);
1416 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1417 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1418 PRId64 "\n",
1419 kqemu_time, kqemu_time / (double)ticks_per_sec,
1420 kqemu_time / (double)total * 100.0,
1421 kqemu_exec_count,
1422 kqemu_ret_int_count,
1423 kqemu_ret_excp_count,
1424 kqemu_ret_intr_count);
bellard5f1ce942006-02-08 22:40:15 +00001425 qemu_time = 0;
1426 kqemu_time = 0;
1427 kqemu_exec_count = 0;
1428 dev_time = 0;
1429 kqemu_ret_int_count = 0;
1430 kqemu_ret_excp_count = 0;
1431 kqemu_ret_intr_count = 0;
1432#ifdef USE_KQEMU
1433 kqemu_record_dump();
1434#endif
1435}
1436#else
aliguori376253e2009-03-05 23:01:23 +00001437static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001438{
aliguori376253e2009-03-05 23:01:23 +00001439 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001440}
1441#endif
1442
bellardec36b692006-07-16 18:57:03 +00001443/* Capture support */
1444static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1445
aliguori376253e2009-03-05 23:01:23 +00001446static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001447{
1448 int i;
1449 CaptureState *s;
1450
1451 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001452 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001453 s->ops.info (s->opaque);
1454 }
1455}
1456
aliguori376253e2009-03-05 23:01:23 +00001457static void do_stop_capture(Monitor *mon, int n)
bellardec36b692006-07-16 18:57:03 +00001458{
1459 int i;
1460 CaptureState *s;
1461
1462 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1463 if (i == n) {
1464 s->ops.destroy (s->opaque);
1465 LIST_REMOVE (s, entries);
1466 qemu_free (s);
1467 return;
1468 }
1469 }
1470}
1471
1472#ifdef HAS_AUDIO
aliguori376253e2009-03-05 23:01:23 +00001473static void do_wav_capture(Monitor *mon, const char *path,
1474 int has_freq, int freq,
1475 int has_bits, int bits,
1476 int has_channels, int nchannels)
bellardec36b692006-07-16 18:57:03 +00001477{
1478 CaptureState *s;
1479
1480 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001481
1482 freq = has_freq ? freq : 44100;
1483 bits = has_bits ? bits : 16;
1484 nchannels = has_channels ? nchannels : 2;
1485
1486 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001487 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001488 qemu_free (s);
1489 }
1490 LIST_INSERT_HEAD (&capture_head, s, entries);
1491}
1492#endif
1493
aurel32dc1c0b72008-04-27 23:52:12 +00001494#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001495static void do_inject_nmi(Monitor *mon, int cpu_index)
aurel32dc1c0b72008-04-27 23:52:12 +00001496{
1497 CPUState *env;
1498
1499 for (env = first_cpu; env != NULL; env = env->next_cpu)
1500 if (env->cpu_index == cpu_index) {
1501 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1502 break;
1503 }
1504}
1505#endif
1506
aliguori376253e2009-03-05 23:01:23 +00001507static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001508{
1509 if (vm_running)
aliguori376253e2009-03-05 23:01:23 +00001510 monitor_printf(mon, "VM status: running\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001511 else
aliguori376253e2009-03-05 23:01:23 +00001512 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001513}
1514
1515
aliguori376253e2009-03-05 23:01:23 +00001516static void do_balloon(Monitor *mon, int value)
aliguoridf751fa2008-12-04 20:19:35 +00001517{
1518 ram_addr_t target = value;
1519 qemu_balloon(target << 20);
1520}
1521
aliguori376253e2009-03-05 23:01:23 +00001522static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001523{
1524 ram_addr_t actual;
1525
1526 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001527 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001528 monitor_printf(mon, "Using KVM without synchronous MMU, "
1529 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001530 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001531 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001532 else
aliguori376253e2009-03-05 23:01:23 +00001533 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001534}
1535
aliguori76655d62009-03-06 20:27:37 +00001536static void do_acl(Monitor *mon,
1537 const char *command,
1538 const char *aclname,
1539 const char *match,
1540 int has_index,
1541 int index)
1542{
1543 qemu_acl *acl;
1544
1545 acl = qemu_acl_find(aclname);
1546 if (!acl) {
1547 monitor_printf(mon, "acl: unknown list '%s'\n", aclname);
1548 return;
1549 }
1550
1551 if (strcmp(command, "show") == 0) {
1552 int i = 0;
1553 qemu_acl_entry *entry;
1554 monitor_printf(mon, "policy: %s\n",
1555 acl->defaultDeny ? "deny" : "allow");
1556 TAILQ_FOREACH(entry, &acl->entries, next) {
1557 i++;
1558 monitor_printf(mon, "%d: %s %s\n", i,
1559 entry->deny ? "deny" : "allow",
1560 entry->match);
1561 }
1562 } else if (strcmp(command, "reset") == 0) {
1563 qemu_acl_reset(acl);
1564 monitor_printf(mon, "acl: removed all rules\n");
1565 } else if (strcmp(command, "policy") == 0) {
1566 if (!match) {
1567 monitor_printf(mon, "acl: missing policy parameter\n");
1568 return;
1569 }
1570
1571 if (strcmp(match, "allow") == 0) {
1572 acl->defaultDeny = 0;
1573 monitor_printf(mon, "acl: policy set to 'allow'\n");
1574 } else if (strcmp(match, "deny") == 0) {
1575 acl->defaultDeny = 1;
1576 monitor_printf(mon, "acl: policy set to 'deny'\n");
1577 } else {
1578 monitor_printf(mon, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match);
1579 }
1580 } else if ((strcmp(command, "allow") == 0) ||
1581 (strcmp(command, "deny") == 0)) {
1582 int deny = strcmp(command, "deny") == 0 ? 1 : 0;
1583 int ret;
1584
1585 if (!match) {
1586 monitor_printf(mon, "acl: missing match parameter\n");
1587 return;
1588 }
1589
1590 if (has_index)
1591 ret = qemu_acl_insert(acl, deny, match, index);
1592 else
1593 ret = qemu_acl_append(acl, deny, match);
1594 if (ret < 0)
1595 monitor_printf(mon, "acl: unable to add acl entry\n");
1596 else
1597 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1598 } else if (strcmp(command, "remove") == 0) {
1599 int ret;
1600
1601 if (!match) {
1602 monitor_printf(mon, "acl: missing match parameter\n");
1603 return;
1604 }
1605
1606 ret = qemu_acl_remove(acl, match);
1607 if (ret < 0)
1608 monitor_printf(mon, "acl: no matching acl entry\n");
1609 else
1610 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1611 } else {
1612 monitor_printf(mon, "acl: unknown command '%s'\n", command);
1613 }
1614}
1615
blueswir1d2c639d2009-01-24 18:19:25 +00001616/* Please update qemu-doc.texi when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001617static const mon_cmd_t mon_cmds[] = {
1618 { "help|?", "s?", help_cmd,
bellard9dc39cb2004-03-14 21:38:27 +00001619 "[cmd]", "show the help" },
ths5fafdf22007-09-16 21:08:06 +00001620 { "commit", "s", do_commit,
bellard7954c732006-08-01 15:52:40 +00001621 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
bellard9307c4c2004-04-04 12:57:25 +00001622 { "info", "s?", do_info,
bellard9dc39cb2004-03-14 21:38:27 +00001623 "subcommand", "show various information about the system state" },
bellard9307c4c2004-04-04 12:57:25 +00001624 { "q|quit", "", do_quit,
bellard9dc39cb2004-03-14 21:38:27 +00001625 "", "quit the emulator" },
bellard81d09122004-07-14 17:21:37 +00001626 { "eject", "-fB", do_eject,
thse5987522007-03-30 18:58:01 +00001627 "[-f] device", "eject a removable medium (use -f to force it)" },
aurel322ecea9b2008-06-18 22:10:01 +00001628 { "change", "BFs?", do_change,
1629 "device filename [format]", "change a removable medium, optional format" },
ths5fafdf22007-09-16 21:08:06 +00001630 { "screendump", "F", do_screen_dump,
bellard59a983b2004-03-17 23:17:16 +00001631 "filename", "save screen into PPM image 'filename'" },
balrog788228c2008-05-24 23:15:46 +00001632 { "logfile", "F", do_logfile,
pbrooke735b912007-06-30 13:53:24 +00001633 "filename", "output logs to 'filename'" },
bellard9307c4c2004-04-04 12:57:25 +00001634 { "log", "s", do_log,
ths5fafdf22007-09-16 21:08:06 +00001635 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
bellardfaea38e2006-08-05 21:31:00 +00001636 { "savevm", "s?", do_savevm,
ths5fafdf22007-09-16 21:08:06 +00001637 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
bellardfaea38e2006-08-05 21:31:00 +00001638 { "loadvm", "s", do_loadvm,
ths5fafdf22007-09-16 21:08:06 +00001639 "tag|id", "restore a VM snapshot from its tag or id" },
bellardfaea38e2006-08-05 21:31:00 +00001640 { "delvm", "s", do_delvm,
ths5fafdf22007-09-16 21:08:06 +00001641 "tag|id", "delete a VM snapshot from its tag or id" },
1642 { "stop", "", do_stop,
bellard9307c4c2004-04-04 12:57:25 +00001643 "", "stop emulation", },
ths5fafdf22007-09-16 21:08:06 +00001644 { "c|cont", "", do_cont,
bellard9307c4c2004-04-04 12:57:25 +00001645 "", "resume emulation", },
bellard67b915a2004-03-31 23:37:16 +00001646#ifdef CONFIG_GDBSTUB
ths5fafdf22007-09-16 21:08:06 +00001647 { "gdbserver", "s?", do_gdbserver,
bellard9307c4c2004-04-04 12:57:25 +00001648 "[port]", "start gdbserver session (default port=1234)", },
bellard67b915a2004-03-31 23:37:16 +00001649#endif
ths5fafdf22007-09-16 21:08:06 +00001650 { "x", "/l", do_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001651 "/fmt addr", "virtual memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001652 { "xp", "/l", do_physical_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001653 "/fmt addr", "physical memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001654 { "p|print", "/l", do_print,
bellard9307c4c2004-04-04 12:57:25 +00001655 "/fmt expr", "print expression value (use $reg for CPU register access)", },
ths5fafdf22007-09-16 21:08:06 +00001656 { "i", "/ii.", do_ioport_read,
bellard34405572004-06-08 00:55:58 +00001657 "/fmt addr", "I/O port read" },
1658
balrogc8256f92008-06-08 22:45:01 +00001659 { "sendkey", "si?", do_sendkey,
1660 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
ths5fafdf22007-09-16 21:08:06 +00001661 { "system_reset", "", do_system_reset,
bellarde4f90822004-06-20 12:35:44 +00001662 "", "reset the system" },
ths5fafdf22007-09-16 21:08:06 +00001663 { "system_powerdown", "", do_system_powerdown,
bellard34751872005-07-02 14:31:34 +00001664 "", "send system power down event" },
ths5fafdf22007-09-16 21:08:06 +00001665 { "sum", "ii", do_sum,
bellarde4cf1ad2005-06-04 20:15:57 +00001666 "addr size", "compute the checksum of a memory region" },
bellarda594cfb2005-11-06 16:13:29 +00001667 { "usb_add", "s", do_usb_add,
1668 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1669 { "usb_del", "s", do_usb_del,
1670 "device", "remove USB device 'bus.addr'" },
ths5fafdf22007-09-16 21:08:06 +00001671 { "cpu", "i", do_cpu_set,
bellard6a00d602005-11-21 23:25:50 +00001672 "index", "set the default CPU" },
ths5fafdf22007-09-16 21:08:06 +00001673 { "mouse_move", "sss?", do_mouse_move,
bellard13224a82006-07-14 22:03:35 +00001674 "dx dy [dz]", "send mouse move events" },
ths5fafdf22007-09-16 21:08:06 +00001675 { "mouse_button", "i", do_mouse_button,
bellard13224a82006-07-14 22:03:35 +00001676 "state", "change mouse button state (1=L, 2=M, 4=R)" },
ths455204e2007-01-05 16:42:13 +00001677 { "mouse_set", "i", do_mouse_set,
1678 "index", "set which mouse device receives events" },
bellardec36b692006-07-16 18:57:03 +00001679#ifdef HAS_AUDIO
1680 { "wavcapture", "si?i?i?", do_wav_capture,
1681 "path [frequency bits channels]",
1682 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1683#endif
blueswir1d2c639d2009-01-24 18:19:25 +00001684 { "stopcapture", "i", do_stop_capture,
1685 "capture index", "stop capture" },
ths5fafdf22007-09-16 21:08:06 +00001686 { "memsave", "lis", do_memory_save,
bellardb371dc52007-01-03 15:20:39 +00001687 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
aurel32a8bdf7a2008-04-11 21:36:14 +00001688 { "pmemsave", "lis", do_physical_memory_save,
1689 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
aurel320ecdffb2008-05-04 20:11:34 +00001690 { "boot_set", "s", do_boot_set,
1691 "bootdevice", "define new values for the boot device list" },
aurel32dc1c0b72008-04-27 23:52:12 +00001692#if defined(TARGET_I386)
1693 { "nmi", "i", do_inject_nmi,
1694 "cpu", "inject an NMI on the given CPU", },
1695#endif
aliguori5bb79102008-10-13 03:12:02 +00001696 { "migrate", "-ds", do_migrate,
1697 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1698 { "migrate_cancel", "", do_migrate_cancel,
1699 "", "cancel the current VM migration" },
1700 { "migrate_set_speed", "s", do_migrate_set_speed,
1701 "value", "set maximum speed (in bytes) for migrations" },
aliguori6f338c32009-02-11 15:21:54 +00001702#if defined(TARGET_I386)
1703 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1704 "[file=file][,if=type][,bus=n]\n"
1705 "[,unit=m][,media=d][index=i]\n"
1706 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1707 "[snapshot=on|off][,cache=on|off]",
1708 "add drive to PCI storage controller" },
1709 { "pci_add", "sss", pci_device_hot_add, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", "hot-add PCI device" },
1710 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1711 { "host_net_add", "ss", net_host_device_add,
1712 "[tap,user,socket,vde] options", "add host VLAN client" },
1713 { "host_net_remove", "is", net_host_device_remove,
1714 "vlan_id name", "remove host VLAN client" },
1715#endif
aliguoridf751fa2008-12-04 20:19:35 +00001716 { "balloon", "i", do_balloon,
1717 "target", "request VM to change it's memory allocation (in MB)" },
aliguorif029bd92009-02-11 15:19:16 +00001718 { "set_link", "ss", do_set_link,
1719 "name [up|down]", "change the link status of a network adapter" },
aliguori76655d62009-03-06 20:27:37 +00001720 { "acl", "sss?i?", do_acl, "<command> <aclname> [<match>] [<index>]\n",
1721 "acl show vnc.username\n"
1722 "acl policy vnc.username deny\n"
1723 "acl allow vnc.username fred\n"
1724 "acl deny vnc.username bob\n"
1725 "acl reset vnc.username\n" },
ths5fafdf22007-09-16 21:08:06 +00001726 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001727};
1728
blueswir1d2c639d2009-01-24 18:19:25 +00001729/* Please update qemu-doc.texi when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001730static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001731 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001732 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001733 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001734 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001735 { "chardev", "", qemu_chr_info,
1736 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001737 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001738 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001739 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001740 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001741 { "registers", "", do_info_registers,
1742 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001743 { "cpus", "", do_info_cpus,
1744 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001745 { "history", "", do_info_history,
1746 "", "show the command line history", },
bellard4a0fb712004-05-21 11:39:07 +00001747 { "irq", "", irq_info,
1748 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001749 { "pic", "", pic_info,
1750 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001751 { "pci", "", pci_info,
1752 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001753#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001754 { "tlb", "", tlb_info,
1755 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001756#endif
1757#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001758 { "mem", "", mem_info,
1759 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001760 { "hpet", "", do_info_hpet,
1761 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001762#endif
bellarde3db7222005-01-26 22:00:47 +00001763 { "jit", "", do_info_jit,
1764 "", "show dynamic compiler info", },
bellard0f4c6412005-07-24 18:10:56 +00001765 { "kqemu", "", do_info_kqemu,
blueswir1d2c639d2009-01-24 18:19:25 +00001766 "", "show KQEMU information", },
aliguori7ba1e612008-11-05 16:04:33 +00001767 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001768 "", "show KVM information", },
bellarda594cfb2005-11-06 16:13:29 +00001769 { "usb", "", usb_info,
1770 "", "show guest USB devices", },
1771 { "usbhost", "", usb_host_info,
1772 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001773 { "profile", "", do_info_profile,
1774 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001775 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001776 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001777 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001778 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001779 { "status", "", do_info_status,
1780 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001781 { "pcmcia", "", pcmcia_info,
1782 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001783 { "mice", "", do_info_mice,
1784 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001785 { "vnc", "", do_info_vnc,
1786 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001787 { "name", "", do_info_name,
1788 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001789 { "uuid", "", do_info_uuid,
1790 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001791#if defined(TARGET_PPC)
1792 { "cpustats", "", do_info_cpu_stats,
1793 "", "show CPU statistics", },
1794#endif
blueswir131a60e22007-10-26 18:42:59 +00001795#if defined(CONFIG_SLIRP)
1796 { "slirp", "", do_info_slirp,
1797 "", "show SLIRP statistics", },
1798#endif
aliguori5bb79102008-10-13 03:12:02 +00001799 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001800 { "balloon", "", do_info_balloon,
1801 "", "show balloon information" },
bellard9dc39cb2004-03-14 21:38:27 +00001802 { NULL, NULL, },
1803};
1804
bellard9307c4c2004-04-04 12:57:25 +00001805/*******************************************************************/
1806
1807static const char *pch;
1808static jmp_buf expr_env;
1809
bellard92a31b12005-02-10 22:00:52 +00001810#define MD_TLONG 0
1811#define MD_I32 1
1812
bellard9307c4c2004-04-04 12:57:25 +00001813typedef struct MonitorDef {
1814 const char *name;
1815 int offset;
blueswir18662d652008-10-02 18:32:44 +00001816 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001817 int type;
bellard9307c4c2004-04-04 12:57:25 +00001818} MonitorDef;
1819
bellard57206fd2004-04-25 18:54:52 +00001820#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001821static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001822{
bellard6a00d602005-11-21 23:25:50 +00001823 CPUState *env = mon_get_cpu();
1824 if (!env)
1825 return 0;
1826 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001827}
1828#endif
1829
bellarda541f292004-04-12 20:39:29 +00001830#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001831static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001832{
bellard6a00d602005-11-21 23:25:50 +00001833 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001834 unsigned int u;
1835 int i;
1836
bellard6a00d602005-11-21 23:25:50 +00001837 if (!env)
1838 return 0;
1839
bellarda541f292004-04-12 20:39:29 +00001840 u = 0;
1841 for (i = 0; i < 8; i++)
bellard6a00d602005-11-21 23:25:50 +00001842 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001843
1844 return u;
1845}
1846
blueswir18662d652008-10-02 18:32:44 +00001847static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001848{
bellard6a00d602005-11-21 23:25:50 +00001849 CPUState *env = mon_get_cpu();
1850 if (!env)
1851 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001852 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001853}
1854
blueswir18662d652008-10-02 18:32:44 +00001855static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001856{
bellard6a00d602005-11-21 23:25:50 +00001857 CPUState *env = mon_get_cpu();
1858 if (!env)
1859 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001860 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001861}
bellard9fddaa02004-05-21 12:59:32 +00001862
blueswir18662d652008-10-02 18:32:44 +00001863static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001864{
bellard6a00d602005-11-21 23:25:50 +00001865 CPUState *env = mon_get_cpu();
1866 if (!env)
1867 return 0;
1868 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001869}
1870
blueswir18662d652008-10-02 18:32:44 +00001871static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001872{
bellard6a00d602005-11-21 23:25:50 +00001873 CPUState *env = mon_get_cpu();
1874 if (!env)
1875 return 0;
1876 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001877}
1878
blueswir18662d652008-10-02 18:32:44 +00001879static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001880{
bellard6a00d602005-11-21 23:25:50 +00001881 CPUState *env = mon_get_cpu();
1882 if (!env)
1883 return 0;
1884 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001885}
bellarda541f292004-04-12 20:39:29 +00001886#endif
1887
bellarde95c8d52004-09-30 22:22:08 +00001888#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001889#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001890static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001891{
bellard6a00d602005-11-21 23:25:50 +00001892 CPUState *env = mon_get_cpu();
1893 if (!env)
1894 return 0;
1895 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001896}
bellard7b936c02005-10-30 17:05:13 +00001897#endif
bellarde95c8d52004-09-30 22:22:08 +00001898
blueswir18662d652008-10-02 18:32:44 +00001899static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001900{
bellard6a00d602005-11-21 23:25:50 +00001901 CPUState *env = mon_get_cpu();
1902 if (!env)
1903 return 0;
1904 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001905}
1906#endif
1907
blueswir18662d652008-10-02 18:32:44 +00001908static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001909#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001910
1911#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001912 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001913 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001914 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001915
bellard9307c4c2004-04-04 12:57:25 +00001916 { "eax", offsetof(CPUState, regs[0]) },
1917 { "ecx", offsetof(CPUState, regs[1]) },
1918 { "edx", offsetof(CPUState, regs[2]) },
1919 { "ebx", offsetof(CPUState, regs[3]) },
1920 { "esp|sp", offsetof(CPUState, regs[4]) },
1921 { "ebp|fp", offsetof(CPUState, regs[5]) },
1922 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001923 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001924#ifdef TARGET_X86_64
1925 { "r8", offsetof(CPUState, regs[8]) },
1926 { "r9", offsetof(CPUState, regs[9]) },
1927 { "r10", offsetof(CPUState, regs[10]) },
1928 { "r11", offsetof(CPUState, regs[11]) },
1929 { "r12", offsetof(CPUState, regs[12]) },
1930 { "r13", offsetof(CPUState, regs[13]) },
1931 { "r14", offsetof(CPUState, regs[14]) },
1932 { "r15", offsetof(CPUState, regs[15]) },
1933#endif
bellard9307c4c2004-04-04 12:57:25 +00001934 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001935 { "eip", offsetof(CPUState, eip) },
1936 SEG("cs", R_CS)
1937 SEG("ds", R_DS)
1938 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001939 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001940 SEG("fs", R_FS)
1941 SEG("gs", R_GS)
1942 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00001943#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00001944 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00001945 { "r0", offsetof(CPUState, gpr[0]) },
1946 { "r1", offsetof(CPUState, gpr[1]) },
1947 { "r2", offsetof(CPUState, gpr[2]) },
1948 { "r3", offsetof(CPUState, gpr[3]) },
1949 { "r4", offsetof(CPUState, gpr[4]) },
1950 { "r5", offsetof(CPUState, gpr[5]) },
1951 { "r6", offsetof(CPUState, gpr[6]) },
1952 { "r7", offsetof(CPUState, gpr[7]) },
1953 { "r8", offsetof(CPUState, gpr[8]) },
1954 { "r9", offsetof(CPUState, gpr[9]) },
1955 { "r10", offsetof(CPUState, gpr[10]) },
1956 { "r11", offsetof(CPUState, gpr[11]) },
1957 { "r12", offsetof(CPUState, gpr[12]) },
1958 { "r13", offsetof(CPUState, gpr[13]) },
1959 { "r14", offsetof(CPUState, gpr[14]) },
1960 { "r15", offsetof(CPUState, gpr[15]) },
1961 { "r16", offsetof(CPUState, gpr[16]) },
1962 { "r17", offsetof(CPUState, gpr[17]) },
1963 { "r18", offsetof(CPUState, gpr[18]) },
1964 { "r19", offsetof(CPUState, gpr[19]) },
1965 { "r20", offsetof(CPUState, gpr[20]) },
1966 { "r21", offsetof(CPUState, gpr[21]) },
1967 { "r22", offsetof(CPUState, gpr[22]) },
1968 { "r23", offsetof(CPUState, gpr[23]) },
1969 { "r24", offsetof(CPUState, gpr[24]) },
1970 { "r25", offsetof(CPUState, gpr[25]) },
1971 { "r26", offsetof(CPUState, gpr[26]) },
1972 { "r27", offsetof(CPUState, gpr[27]) },
1973 { "r28", offsetof(CPUState, gpr[28]) },
1974 { "r29", offsetof(CPUState, gpr[29]) },
1975 { "r30", offsetof(CPUState, gpr[30]) },
1976 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00001977 /* Floating point registers */
1978 { "f0", offsetof(CPUState, fpr[0]) },
1979 { "f1", offsetof(CPUState, fpr[1]) },
1980 { "f2", offsetof(CPUState, fpr[2]) },
1981 { "f3", offsetof(CPUState, fpr[3]) },
1982 { "f4", offsetof(CPUState, fpr[4]) },
1983 { "f5", offsetof(CPUState, fpr[5]) },
1984 { "f6", offsetof(CPUState, fpr[6]) },
1985 { "f7", offsetof(CPUState, fpr[7]) },
1986 { "f8", offsetof(CPUState, fpr[8]) },
1987 { "f9", offsetof(CPUState, fpr[9]) },
1988 { "f10", offsetof(CPUState, fpr[10]) },
1989 { "f11", offsetof(CPUState, fpr[11]) },
1990 { "f12", offsetof(CPUState, fpr[12]) },
1991 { "f13", offsetof(CPUState, fpr[13]) },
1992 { "f14", offsetof(CPUState, fpr[14]) },
1993 { "f15", offsetof(CPUState, fpr[15]) },
1994 { "f16", offsetof(CPUState, fpr[16]) },
1995 { "f17", offsetof(CPUState, fpr[17]) },
1996 { "f18", offsetof(CPUState, fpr[18]) },
1997 { "f19", offsetof(CPUState, fpr[19]) },
1998 { "f20", offsetof(CPUState, fpr[20]) },
1999 { "f21", offsetof(CPUState, fpr[21]) },
2000 { "f22", offsetof(CPUState, fpr[22]) },
2001 { "f23", offsetof(CPUState, fpr[23]) },
2002 { "f24", offsetof(CPUState, fpr[24]) },
2003 { "f25", offsetof(CPUState, fpr[25]) },
2004 { "f26", offsetof(CPUState, fpr[26]) },
2005 { "f27", offsetof(CPUState, fpr[27]) },
2006 { "f28", offsetof(CPUState, fpr[28]) },
2007 { "f29", offsetof(CPUState, fpr[29]) },
2008 { "f30", offsetof(CPUState, fpr[30]) },
2009 { "f31", offsetof(CPUState, fpr[31]) },
2010 { "fpscr", offsetof(CPUState, fpscr) },
2011 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002012 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002013 { "lr", offsetof(CPUState, lr) },
2014 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002015 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002016 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002017 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002018 { "msr", 0, &monitor_get_msr, },
2019 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002020 { "tbu", 0, &monitor_get_tbu, },
2021 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002022#if defined(TARGET_PPC64)
2023 /* Address space register */
2024 { "asr", offsetof(CPUState, asr) },
2025#endif
2026 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002027 { "sdr1", offsetof(CPUState, sdr1) },
2028 { "sr0", offsetof(CPUState, sr[0]) },
2029 { "sr1", offsetof(CPUState, sr[1]) },
2030 { "sr2", offsetof(CPUState, sr[2]) },
2031 { "sr3", offsetof(CPUState, sr[3]) },
2032 { "sr4", offsetof(CPUState, sr[4]) },
2033 { "sr5", offsetof(CPUState, sr[5]) },
2034 { "sr6", offsetof(CPUState, sr[6]) },
2035 { "sr7", offsetof(CPUState, sr[7]) },
2036 { "sr8", offsetof(CPUState, sr[8]) },
2037 { "sr9", offsetof(CPUState, sr[9]) },
2038 { "sr10", offsetof(CPUState, sr[10]) },
2039 { "sr11", offsetof(CPUState, sr[11]) },
2040 { "sr12", offsetof(CPUState, sr[12]) },
2041 { "sr13", offsetof(CPUState, sr[13]) },
2042 { "sr14", offsetof(CPUState, sr[14]) },
2043 { "sr15", offsetof(CPUState, sr[15]) },
2044 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002045#elif defined(TARGET_SPARC)
2046 { "g0", offsetof(CPUState, gregs[0]) },
2047 { "g1", offsetof(CPUState, gregs[1]) },
2048 { "g2", offsetof(CPUState, gregs[2]) },
2049 { "g3", offsetof(CPUState, gregs[3]) },
2050 { "g4", offsetof(CPUState, gregs[4]) },
2051 { "g5", offsetof(CPUState, gregs[5]) },
2052 { "g6", offsetof(CPUState, gregs[6]) },
2053 { "g7", offsetof(CPUState, gregs[7]) },
2054 { "o0", 0, monitor_get_reg },
2055 { "o1", 1, monitor_get_reg },
2056 { "o2", 2, monitor_get_reg },
2057 { "o3", 3, monitor_get_reg },
2058 { "o4", 4, monitor_get_reg },
2059 { "o5", 5, monitor_get_reg },
2060 { "o6", 6, monitor_get_reg },
2061 { "o7", 7, monitor_get_reg },
2062 { "l0", 8, monitor_get_reg },
2063 { "l1", 9, monitor_get_reg },
2064 { "l2", 10, monitor_get_reg },
2065 { "l3", 11, monitor_get_reg },
2066 { "l4", 12, monitor_get_reg },
2067 { "l5", 13, monitor_get_reg },
2068 { "l6", 14, monitor_get_reg },
2069 { "l7", 15, monitor_get_reg },
2070 { "i0", 16, monitor_get_reg },
2071 { "i1", 17, monitor_get_reg },
2072 { "i2", 18, monitor_get_reg },
2073 { "i3", 19, monitor_get_reg },
2074 { "i4", 20, monitor_get_reg },
2075 { "i5", 21, monitor_get_reg },
2076 { "i6", 22, monitor_get_reg },
2077 { "i7", 23, monitor_get_reg },
2078 { "pc", offsetof(CPUState, pc) },
2079 { "npc", offsetof(CPUState, npc) },
2080 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002081#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002082 { "psr", 0, &monitor_get_psr, },
2083 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002084#endif
bellarde95c8d52004-09-30 22:22:08 +00002085 { "tbr", offsetof(CPUState, tbr) },
2086 { "fsr", offsetof(CPUState, fsr) },
2087 { "f0", offsetof(CPUState, fpr[0]) },
2088 { "f1", offsetof(CPUState, fpr[1]) },
2089 { "f2", offsetof(CPUState, fpr[2]) },
2090 { "f3", offsetof(CPUState, fpr[3]) },
2091 { "f4", offsetof(CPUState, fpr[4]) },
2092 { "f5", offsetof(CPUState, fpr[5]) },
2093 { "f6", offsetof(CPUState, fpr[6]) },
2094 { "f7", offsetof(CPUState, fpr[7]) },
2095 { "f8", offsetof(CPUState, fpr[8]) },
2096 { "f9", offsetof(CPUState, fpr[9]) },
2097 { "f10", offsetof(CPUState, fpr[10]) },
2098 { "f11", offsetof(CPUState, fpr[11]) },
2099 { "f12", offsetof(CPUState, fpr[12]) },
2100 { "f13", offsetof(CPUState, fpr[13]) },
2101 { "f14", offsetof(CPUState, fpr[14]) },
2102 { "f15", offsetof(CPUState, fpr[15]) },
2103 { "f16", offsetof(CPUState, fpr[16]) },
2104 { "f17", offsetof(CPUState, fpr[17]) },
2105 { "f18", offsetof(CPUState, fpr[18]) },
2106 { "f19", offsetof(CPUState, fpr[19]) },
2107 { "f20", offsetof(CPUState, fpr[20]) },
2108 { "f21", offsetof(CPUState, fpr[21]) },
2109 { "f22", offsetof(CPUState, fpr[22]) },
2110 { "f23", offsetof(CPUState, fpr[23]) },
2111 { "f24", offsetof(CPUState, fpr[24]) },
2112 { "f25", offsetof(CPUState, fpr[25]) },
2113 { "f26", offsetof(CPUState, fpr[26]) },
2114 { "f27", offsetof(CPUState, fpr[27]) },
2115 { "f28", offsetof(CPUState, fpr[28]) },
2116 { "f29", offsetof(CPUState, fpr[29]) },
2117 { "f30", offsetof(CPUState, fpr[30]) },
2118 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002119#ifdef TARGET_SPARC64
2120 { "f32", offsetof(CPUState, fpr[32]) },
2121 { "f34", offsetof(CPUState, fpr[34]) },
2122 { "f36", offsetof(CPUState, fpr[36]) },
2123 { "f38", offsetof(CPUState, fpr[38]) },
2124 { "f40", offsetof(CPUState, fpr[40]) },
2125 { "f42", offsetof(CPUState, fpr[42]) },
2126 { "f44", offsetof(CPUState, fpr[44]) },
2127 { "f46", offsetof(CPUState, fpr[46]) },
2128 { "f48", offsetof(CPUState, fpr[48]) },
2129 { "f50", offsetof(CPUState, fpr[50]) },
2130 { "f52", offsetof(CPUState, fpr[52]) },
2131 { "f54", offsetof(CPUState, fpr[54]) },
2132 { "f56", offsetof(CPUState, fpr[56]) },
2133 { "f58", offsetof(CPUState, fpr[58]) },
2134 { "f60", offsetof(CPUState, fpr[60]) },
2135 { "f62", offsetof(CPUState, fpr[62]) },
2136 { "asi", offsetof(CPUState, asi) },
2137 { "pstate", offsetof(CPUState, pstate) },
2138 { "cansave", offsetof(CPUState, cansave) },
2139 { "canrestore", offsetof(CPUState, canrestore) },
2140 { "otherwin", offsetof(CPUState, otherwin) },
2141 { "wstate", offsetof(CPUState, wstate) },
2142 { "cleanwin", offsetof(CPUState, cleanwin) },
2143 { "fprs", offsetof(CPUState, fprs) },
2144#endif
bellard9307c4c2004-04-04 12:57:25 +00002145#endif
2146 { NULL },
2147};
2148
aliguori376253e2009-03-05 23:01:23 +00002149static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002150{
aliguori376253e2009-03-05 23:01:23 +00002151 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002152 longjmp(expr_env, 1);
2153}
2154
bellard6a00d602005-11-21 23:25:50 +00002155/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002156static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002157{
blueswir18662d652008-10-02 18:32:44 +00002158 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002159 void *ptr;
2160
bellard9307c4c2004-04-04 12:57:25 +00002161 for(md = monitor_defs; md->name != NULL; md++) {
2162 if (compare_cmd(name, md->name)) {
2163 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002164 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002165 } else {
bellard6a00d602005-11-21 23:25:50 +00002166 CPUState *env = mon_get_cpu();
2167 if (!env)
2168 return -2;
2169 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002170 switch(md->type) {
2171 case MD_I32:
2172 *pval = *(int32_t *)ptr;
2173 break;
2174 case MD_TLONG:
2175 *pval = *(target_long *)ptr;
2176 break;
2177 default:
2178 *pval = 0;
2179 break;
2180 }
bellard9307c4c2004-04-04 12:57:25 +00002181 }
2182 return 0;
2183 }
2184 }
2185 return -1;
2186}
2187
2188static void next(void)
2189{
2190 if (pch != '\0') {
2191 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002192 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002193 pch++;
2194 }
2195}
2196
aliguori376253e2009-03-05 23:01:23 +00002197static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002198
aliguori376253e2009-03-05 23:01:23 +00002199static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002200{
blueswir1c2efc952007-09-25 17:28:42 +00002201 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002202 char *p;
bellard6a00d602005-11-21 23:25:50 +00002203 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002204
2205 switch(*pch) {
2206 case '+':
2207 next();
aliguori376253e2009-03-05 23:01:23 +00002208 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002209 break;
2210 case '-':
2211 next();
aliguori376253e2009-03-05 23:01:23 +00002212 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002213 break;
2214 case '~':
2215 next();
aliguori376253e2009-03-05 23:01:23 +00002216 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002217 break;
2218 case '(':
2219 next();
aliguori376253e2009-03-05 23:01:23 +00002220 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002221 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002222 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002223 }
2224 next();
2225 break;
bellard81d09122004-07-14 17:21:37 +00002226 case '\'':
2227 pch++;
2228 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002229 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002230 n = *pch;
2231 pch++;
2232 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002233 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002234 next();
2235 break;
bellard9307c4c2004-04-04 12:57:25 +00002236 case '$':
2237 {
2238 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002239 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002240
bellard9307c4c2004-04-04 12:57:25 +00002241 pch++;
2242 q = buf;
2243 while ((*pch >= 'a' && *pch <= 'z') ||
2244 (*pch >= 'A' && *pch <= 'Z') ||
2245 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002246 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002247 if ((q - buf) < sizeof(buf) - 1)
2248 *q++ = *pch;
2249 pch++;
2250 }
blueswir1cd390082008-11-16 13:53:32 +00002251 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002252 pch++;
2253 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002254 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002255 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002256 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002257 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002258 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002259 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002260 }
2261 break;
2262 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002263 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002264 n = 0;
2265 break;
2266 default:
blueswir17743e582007-09-24 18:39:04 +00002267#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002268 n = strtoull(pch, &p, 0);
2269#else
bellard9307c4c2004-04-04 12:57:25 +00002270 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002271#endif
bellard9307c4c2004-04-04 12:57:25 +00002272 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002273 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002274 }
2275 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002276 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002277 pch++;
2278 break;
2279 }
2280 return n;
2281}
2282
2283
aliguori376253e2009-03-05 23:01:23 +00002284static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002285{
blueswir1c2efc952007-09-25 17:28:42 +00002286 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002287 int op;
ths3b46e622007-09-17 08:09:54 +00002288
aliguori376253e2009-03-05 23:01:23 +00002289 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002290 for(;;) {
2291 op = *pch;
2292 if (op != '*' && op != '/' && op != '%')
2293 break;
2294 next();
aliguori376253e2009-03-05 23:01:23 +00002295 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002296 switch(op) {
2297 default:
2298 case '*':
2299 val *= val2;
2300 break;
2301 case '/':
2302 case '%':
ths5fafdf22007-09-16 21:08:06 +00002303 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002304 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002305 if (op == '/')
2306 val /= val2;
2307 else
2308 val %= val2;
2309 break;
2310 }
2311 }
2312 return val;
2313}
2314
aliguori376253e2009-03-05 23:01:23 +00002315static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002316{
blueswir1c2efc952007-09-25 17:28:42 +00002317 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002318 int op;
bellard9307c4c2004-04-04 12:57:25 +00002319
aliguori376253e2009-03-05 23:01:23 +00002320 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002321 for(;;) {
2322 op = *pch;
2323 if (op != '&' && op != '|' && op != '^')
2324 break;
2325 next();
aliguori376253e2009-03-05 23:01:23 +00002326 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002327 switch(op) {
2328 default:
2329 case '&':
2330 val &= val2;
2331 break;
2332 case '|':
2333 val |= val2;
2334 break;
2335 case '^':
2336 val ^= val2;
2337 break;
2338 }
2339 }
2340 return val;
2341}
2342
aliguori376253e2009-03-05 23:01:23 +00002343static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002344{
blueswir1c2efc952007-09-25 17:28:42 +00002345 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002346 int op;
bellard9307c4c2004-04-04 12:57:25 +00002347
aliguori376253e2009-03-05 23:01:23 +00002348 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002349 for(;;) {
2350 op = *pch;
2351 if (op != '+' && op != '-')
2352 break;
2353 next();
aliguori376253e2009-03-05 23:01:23 +00002354 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002355 if (op == '+')
2356 val += val2;
2357 else
2358 val -= val2;
2359 }
2360 return val;
2361}
2362
aliguori376253e2009-03-05 23:01:23 +00002363static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002364{
2365 pch = *pp;
2366 if (setjmp(expr_env)) {
2367 *pp = pch;
2368 return -1;
2369 }
blueswir1cd390082008-11-16 13:53:32 +00002370 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002371 pch++;
aliguori376253e2009-03-05 23:01:23 +00002372 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002373 *pp = pch;
2374 return 0;
2375}
2376
2377static int get_str(char *buf, int buf_size, const char **pp)
2378{
2379 const char *p;
2380 char *q;
2381 int c;
2382
bellard81d09122004-07-14 17:21:37 +00002383 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002384 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002385 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002386 p++;
2387 if (*p == '\0') {
2388 fail:
bellard81d09122004-07-14 17:21:37 +00002389 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002390 *pp = p;
2391 return -1;
2392 }
bellard9307c4c2004-04-04 12:57:25 +00002393 if (*p == '\"') {
2394 p++;
2395 while (*p != '\0' && *p != '\"') {
2396 if (*p == '\\') {
2397 p++;
2398 c = *p++;
2399 switch(c) {
2400 case 'n':
2401 c = '\n';
2402 break;
2403 case 'r':
2404 c = '\r';
2405 break;
2406 case '\\':
2407 case '\'':
2408 case '\"':
2409 break;
2410 default:
2411 qemu_printf("unsupported escape code: '\\%c'\n", c);
2412 goto fail;
2413 }
2414 if ((q - buf) < buf_size - 1) {
2415 *q++ = c;
2416 }
2417 } else {
2418 if ((q - buf) < buf_size - 1) {
2419 *q++ = *p;
2420 }
2421 p++;
2422 }
2423 }
2424 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002425 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002426 goto fail;
2427 }
2428 p++;
2429 } else {
blueswir1cd390082008-11-16 13:53:32 +00002430 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002431 if ((q - buf) < buf_size - 1) {
2432 *q++ = *p;
2433 }
2434 p++;
2435 }
bellard9307c4c2004-04-04 12:57:25 +00002436 }
bellard81d09122004-07-14 17:21:37 +00002437 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002438 *pp = p;
2439 return 0;
2440}
2441
2442static int default_fmt_format = 'x';
2443static int default_fmt_size = 4;
2444
2445#define MAX_ARGS 16
2446
aliguori376253e2009-03-05 23:01:23 +00002447static void monitor_handle_command(Monitor *mon, const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002448{
2449 const char *p, *pstart, *typestr;
2450 char *q;
2451 int c, nb_args, len, i, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002452 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002453 char cmdname[256];
2454 char buf[1024];
2455 void *str_allocated[MAX_ARGS];
2456 void *args[MAX_ARGS];
aliguori376253e2009-03-05 23:01:23 +00002457 void (*handler_0)(Monitor *mon);
2458 void (*handler_1)(Monitor *mon, void *arg0);
2459 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2460 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2461 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2462 void *arg3);
2463 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2464 void *arg3, void *arg4);
2465 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2466 void *arg3, void *arg4, void *arg5);
2467 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2468 void *arg3, void *arg4, void *arg5, void *arg6);
bellard9dc39cb2004-03-14 21:38:27 +00002469
2470#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002471 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002472#endif
ths3b46e622007-09-17 08:09:54 +00002473
bellard9307c4c2004-04-04 12:57:25 +00002474 /* extract the command name */
bellard9dc39cb2004-03-14 21:38:27 +00002475 p = cmdline;
bellard9307c4c2004-04-04 12:57:25 +00002476 q = cmdname;
blueswir1cd390082008-11-16 13:53:32 +00002477 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002478 p++;
2479 if (*p == '\0')
bellard9dc39cb2004-03-14 21:38:27 +00002480 return;
bellard9307c4c2004-04-04 12:57:25 +00002481 pstart = p;
blueswir1cd390082008-11-16 13:53:32 +00002482 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002483 p++;
2484 len = p - pstart;
2485 if (len > sizeof(cmdname) - 1)
2486 len = sizeof(cmdname) - 1;
2487 memcpy(cmdname, pstart, len);
2488 cmdname[len] = '\0';
ths3b46e622007-09-17 08:09:54 +00002489
bellard9307c4c2004-04-04 12:57:25 +00002490 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002491 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002492 if (compare_cmd(cmdname, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +00002493 goto found;
2494 }
aliguori376253e2009-03-05 23:01:23 +00002495 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
bellard9dc39cb2004-03-14 21:38:27 +00002496 return;
2497 found:
bellard9307c4c2004-04-04 12:57:25 +00002498
2499 for(i = 0; i < MAX_ARGS; i++)
2500 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002501
bellard9307c4c2004-04-04 12:57:25 +00002502 /* parse the parameters */
2503 typestr = cmd->args_type;
2504 nb_args = 0;
2505 for(;;) {
2506 c = *typestr;
2507 if (c == '\0')
2508 break;
2509 typestr++;
2510 switch(c) {
2511 case 'F':
bellard81d09122004-07-14 17:21:37 +00002512 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002513 case 's':
2514 {
2515 int ret;
2516 char *str;
ths3b46e622007-09-17 08:09:54 +00002517
blueswir1cd390082008-11-16 13:53:32 +00002518 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002519 p++;
2520 if (*typestr == '?') {
2521 typestr++;
2522 if (*p == '\0') {
2523 /* no optional string: NULL argument */
2524 str = NULL;
2525 goto add_str;
2526 }
2527 }
2528 ret = get_str(buf, sizeof(buf), &p);
2529 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002530 switch(c) {
2531 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002532 monitor_printf(mon, "%s: filename expected\n",
2533 cmdname);
bellard81d09122004-07-14 17:21:37 +00002534 break;
2535 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002536 monitor_printf(mon, "%s: block device name expected\n",
2537 cmdname);
bellard81d09122004-07-14 17:21:37 +00002538 break;
2539 default:
aliguori376253e2009-03-05 23:01:23 +00002540 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002541 break;
2542 }
bellard9307c4c2004-04-04 12:57:25 +00002543 goto fail;
2544 }
2545 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002546 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002547 str_allocated[nb_args] = str;
2548 add_str:
2549 if (nb_args >= MAX_ARGS) {
2550 error_args:
aliguori376253e2009-03-05 23:01:23 +00002551 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002552 goto fail;
2553 }
2554 args[nb_args++] = str;
2555 }
2556 break;
2557 case '/':
2558 {
2559 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002560
blueswir1cd390082008-11-16 13:53:32 +00002561 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002562 p++;
2563 if (*p == '/') {
2564 /* format found */
2565 p++;
2566 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002567 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002568 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002569 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002570 count = count * 10 + (*p - '0');
2571 p++;
2572 }
2573 }
2574 size = -1;
2575 format = -1;
2576 for(;;) {
2577 switch(*p) {
2578 case 'o':
2579 case 'd':
2580 case 'u':
2581 case 'x':
2582 case 'i':
2583 case 'c':
2584 format = *p++;
2585 break;
2586 case 'b':
2587 size = 1;
2588 p++;
2589 break;
2590 case 'h':
2591 size = 2;
2592 p++;
2593 break;
2594 case 'w':
2595 size = 4;
2596 p++;
2597 break;
2598 case 'g':
2599 case 'L':
2600 size = 8;
2601 p++;
2602 break;
2603 default:
2604 goto next;
2605 }
2606 }
2607 next:
blueswir1cd390082008-11-16 13:53:32 +00002608 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002609 monitor_printf(mon, "invalid char in format: '%c'\n",
2610 *p);
bellard9307c4c2004-04-04 12:57:25 +00002611 goto fail;
2612 }
bellard9307c4c2004-04-04 12:57:25 +00002613 if (format < 0)
2614 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002615 if (format != 'i') {
2616 /* for 'i', not specifying a size gives -1 as size */
2617 if (size < 0)
2618 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002619 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002620 }
bellard9307c4c2004-04-04 12:57:25 +00002621 default_fmt_format = format;
2622 } else {
2623 count = 1;
2624 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002625 if (format != 'i') {
2626 size = default_fmt_size;
2627 } else {
2628 size = -1;
2629 }
bellard9307c4c2004-04-04 12:57:25 +00002630 }
2631 if (nb_args + 3 > MAX_ARGS)
2632 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002633 args[nb_args++] = (void*)(long)count;
2634 args[nb_args++] = (void*)(long)format;
2635 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002636 }
2637 break;
2638 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002639 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002640 {
blueswir1c2efc952007-09-25 17:28:42 +00002641 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002642
blueswir1cd390082008-11-16 13:53:32 +00002643 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002644 p++;
bellard34405572004-06-08 00:55:58 +00002645 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002646 if (*typestr == '?') {
2647 if (*p == '\0')
2648 has_arg = 0;
2649 else
2650 has_arg = 1;
2651 } else {
2652 if (*p == '.') {
2653 p++;
blueswir1cd390082008-11-16 13:53:32 +00002654 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002655 p++;
2656 has_arg = 1;
2657 } else {
2658 has_arg = 0;
2659 }
2660 }
bellard13224a82006-07-14 22:03:35 +00002661 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002662 if (nb_args >= MAX_ARGS)
2663 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002664 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002665 if (!has_arg) {
2666 if (nb_args >= MAX_ARGS)
2667 goto error_args;
2668 val = -1;
2669 goto add_num;
2670 }
2671 }
aliguori376253e2009-03-05 23:01:23 +00002672 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002673 goto fail;
2674 add_num:
bellard92a31b12005-02-10 22:00:52 +00002675 if (c == 'i') {
2676 if (nb_args >= MAX_ARGS)
2677 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002678 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002679 } else {
2680 if ((nb_args + 1) >= MAX_ARGS)
2681 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002682#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002683 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002684#else
2685 args[nb_args++] = (void *)0;
2686#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002687 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002688 }
bellard9307c4c2004-04-04 12:57:25 +00002689 }
2690 break;
2691 case '-':
2692 {
2693 int has_option;
2694 /* option */
ths3b46e622007-09-17 08:09:54 +00002695
bellard9307c4c2004-04-04 12:57:25 +00002696 c = *typestr++;
2697 if (c == '\0')
2698 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002699 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002700 p++;
2701 has_option = 0;
2702 if (*p == '-') {
2703 p++;
2704 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002705 monitor_printf(mon, "%s: unsupported option -%c\n",
2706 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002707 goto fail;
2708 }
2709 p++;
2710 has_option = 1;
2711 }
2712 if (nb_args >= MAX_ARGS)
2713 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002714 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002715 }
2716 break;
2717 default:
2718 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002719 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002720 goto fail;
2721 }
2722 }
2723 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002724 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002725 p++;
2726 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002727 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2728 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002729 goto fail;
2730 }
2731
2732 switch(nb_args) {
2733 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002734 handler_0 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002735 handler_0(mon);
bellard9307c4c2004-04-04 12:57:25 +00002736 break;
2737 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002738 handler_1 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002739 handler_1(mon, args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002740 break;
2741 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002742 handler_2 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002743 handler_2(mon, args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002744 break;
2745 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002746 handler_3 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002747 handler_3(mon, args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002748 break;
2749 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002750 handler_4 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002751 handler_4(mon, args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002752 break;
2753 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002754 handler_5 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002755 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002756 break;
bellard34405572004-06-08 00:55:58 +00002757 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002758 handler_6 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002759 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002760 break;
bellardec36b692006-07-16 18:57:03 +00002761 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002762 handler_7 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002763 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2764 args[6]);
bellardec36b692006-07-16 18:57:03 +00002765 break;
bellard9307c4c2004-04-04 12:57:25 +00002766 default:
aliguori376253e2009-03-05 23:01:23 +00002767 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
bellard9307c4c2004-04-04 12:57:25 +00002768 goto fail;
2769 }
2770 fail:
2771 for(i = 0; i < MAX_ARGS; i++)
2772 qemu_free(str_allocated[i]);
2773 return;
bellard9dc39cb2004-03-14 21:38:27 +00002774}
2775
bellard81d09122004-07-14 17:21:37 +00002776static void cmd_completion(const char *name, const char *list)
2777{
2778 const char *p, *pstart;
2779 char cmd[128];
2780 int len;
2781
2782 p = list;
2783 for(;;) {
2784 pstart = p;
2785 p = strchr(p, '|');
2786 if (!p)
2787 p = pstart + strlen(pstart);
2788 len = p - pstart;
2789 if (len > sizeof(cmd) - 2)
2790 len = sizeof(cmd) - 2;
2791 memcpy(cmd, pstart, len);
2792 cmd[len] = '\0';
2793 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002794 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002795 }
2796 if (*p == '\0')
2797 break;
2798 p++;
2799 }
2800}
2801
2802static void file_completion(const char *input)
2803{
2804 DIR *ffs;
2805 struct dirent *d;
2806 char path[1024];
2807 char file[1024], file_prefix[1024];
2808 int input_path_len;
2809 const char *p;
2810
ths5fafdf22007-09-16 21:08:06 +00002811 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002812 if (!p) {
2813 input_path_len = 0;
2814 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002815 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002816 } else {
2817 input_path_len = p - input + 1;
2818 memcpy(path, input, input_path_len);
2819 if (input_path_len > sizeof(path) - 1)
2820 input_path_len = sizeof(path) - 1;
2821 path[input_path_len] = '\0';
2822 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2823 }
2824#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002825 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2826 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002827#endif
2828 ffs = opendir(path);
2829 if (!ffs)
2830 return;
2831 for(;;) {
2832 struct stat sb;
2833 d = readdir(ffs);
2834 if (!d)
2835 break;
2836 if (strstart(d->d_name, file_prefix, NULL)) {
2837 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002838 if (input_path_len < sizeof(file))
2839 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2840 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002841 /* stat the file to find out if it's a directory.
2842 * In that case add a slash to speed up typing long paths
2843 */
2844 stat(file, &sb);
2845 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002846 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00002847 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002848 }
2849 }
2850 closedir(ffs);
2851}
2852
aliguori51de9762009-03-05 23:00:43 +00002853static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002854{
aliguori51de9762009-03-05 23:00:43 +00002855 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002856 const char *input = opaque;
2857
2858 if (input[0] == '\0' ||
2859 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00002860 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00002861 }
2862}
2863
2864/* NOTE: this parser is an approximate form of the real command parser */
2865static void parse_cmdline(const char *cmdline,
2866 int *pnb_args, char **args)
2867{
2868 const char *p;
2869 int nb_args, ret;
2870 char buf[1024];
2871
2872 p = cmdline;
2873 nb_args = 0;
2874 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002875 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002876 p++;
2877 if (*p == '\0')
2878 break;
2879 if (nb_args >= MAX_ARGS)
2880 break;
2881 ret = get_str(buf, sizeof(buf), &p);
2882 args[nb_args] = qemu_strdup(buf);
2883 nb_args++;
2884 if (ret < 0)
2885 break;
2886 }
2887 *pnb_args = nb_args;
2888}
2889
aliguori4c36ba32009-03-05 23:01:37 +00002890static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002891{
2892 const char *cmdname;
2893 char *args[MAX_ARGS];
2894 int nb_args, i, len;
2895 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00002896 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002897 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002898
2899 parse_cmdline(cmdline, &nb_args, args);
2900#ifdef DEBUG_COMPLETION
2901 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00002902 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00002903 }
2904#endif
2905
2906 /* if the line ends with a space, it means we want to complete the
2907 next arg */
2908 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002909 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002910 if (nb_args >= MAX_ARGS)
2911 return;
2912 args[nb_args++] = qemu_strdup("");
2913 }
2914 if (nb_args <= 1) {
2915 /* command completion */
2916 if (nb_args == 0)
2917 cmdname = "";
2918 else
2919 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00002920 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00002921 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002922 cmd_completion(cmdname, cmd->name);
2923 }
2924 } else {
2925 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002926 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002927 if (compare_cmd(args[0], cmd->name))
2928 goto found;
2929 }
2930 return;
2931 found:
2932 ptype = cmd->args_type;
2933 for(i = 0; i < nb_args - 2; i++) {
2934 if (*ptype != '\0') {
2935 ptype++;
2936 while (*ptype == '?')
2937 ptype++;
2938 }
2939 }
2940 str = args[nb_args - 1];
2941 switch(*ptype) {
2942 case 'F':
2943 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00002944 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00002945 file_completion(str);
2946 break;
2947 case 'B':
2948 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00002949 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00002950 bdrv_iterate(block_completion_it, (void *)str);
2951 break;
bellard7fe48482004-10-09 18:08:01 +00002952 case 's':
2953 /* XXX: more generic ? */
2954 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00002955 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00002956 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2957 cmd_completion(str, cmd->name);
2958 }
bellard64866c32006-05-07 18:03:31 +00002959 } else if (!strcmp(cmd->name, "sendkey")) {
aliguori731b0362009-03-05 23:01:42 +00002960 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00002961 for(key = key_defs; key->name != NULL; key++) {
2962 cmd_completion(str, key->name);
2963 }
bellard7fe48482004-10-09 18:08:01 +00002964 }
2965 break;
bellard81d09122004-07-14 17:21:37 +00002966 default:
2967 break;
2968 }
2969 }
2970 for(i = 0; i < nb_args; i++)
2971 qemu_free(args[i]);
2972}
2973
aliguori731b0362009-03-05 23:01:42 +00002974static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00002975{
aliguori731b0362009-03-05 23:01:42 +00002976 Monitor *mon = opaque;
2977
2978 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00002979}
2980
aliguori731b0362009-03-05 23:01:42 +00002981static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00002982{
aliguori731b0362009-03-05 23:01:42 +00002983 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00002984 int i;
aliguori376253e2009-03-05 23:01:23 +00002985
aliguori731b0362009-03-05 23:01:42 +00002986 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00002987
aliguoricde76ee2009-03-05 23:01:51 +00002988 if (cur_mon->rs) {
2989 for (i = 0; i < size; i++)
2990 readline_handle_byte(cur_mon->rs, buf[i]);
2991 } else {
2992 if (size == 0 || buf[size - 1] != 0)
2993 monitor_printf(cur_mon, "corrupted command\n");
2994 else
2995 monitor_handle_command(cur_mon, (char *)buf);
2996 }
aliguori731b0362009-03-05 23:01:42 +00002997
2998 cur_mon = old_mon;
2999}
aliguorid8f44602008-10-06 13:52:44 +00003000
aliguori376253e2009-03-05 23:01:23 +00003001static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003002{
aliguori731b0362009-03-05 23:01:42 +00003003 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003004 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003005 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003006}
3007
aliguoricde76ee2009-03-05 23:01:51 +00003008int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003009{
aliguoricde76ee2009-03-05 23:01:51 +00003010 if (!mon->rs)
3011 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003012 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003013 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003014}
3015
aliguori376253e2009-03-05 23:01:23 +00003016void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003017{
aliguoricde76ee2009-03-05 23:01:51 +00003018 if (!mon->rs)
3019 return;
aliguori731b0362009-03-05 23:01:42 +00003020 if (--mon->suspend_cnt == 0)
3021 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003022}
3023
aliguori731b0362009-03-05 23:01:42 +00003024static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003025{
aliguori376253e2009-03-05 23:01:23 +00003026 Monitor *mon = opaque;
3027
aliguori2724b182009-03-05 23:01:47 +00003028 switch (event) {
3029 case CHR_EVENT_MUX_IN:
3030 readline_restart(mon->rs);
3031 monitor_resume(mon);
3032 monitor_flush(mon);
3033 break;
ths86e94de2007-01-05 22:01:59 +00003034
aliguori2724b182009-03-05 23:01:47 +00003035 case CHR_EVENT_MUX_OUT:
3036 if (mon->suspend_cnt == 0)
3037 monitor_printf(mon, "\n");
3038 monitor_flush(mon);
3039 monitor_suspend(mon);
3040 break;
3041
3042 case CHR_EVENT_RESET:
3043 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3044 "information\n", QEMU_VERSION);
3045 if (mon->chr->focus == 0)
3046 readline_show_prompt(mon->rs);
3047 break;
3048 }
ths86e94de2007-01-05 22:01:59 +00003049}
3050
aliguori76655d62009-03-06 20:27:37 +00003051
3052/*
3053 * Local variables:
3054 * c-indent-level: 4
3055 * c-basic-offset: 4
3056 * tab-width: 8
3057 * End:
3058 */
3059
aliguori731b0362009-03-05 23:01:42 +00003060void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003061{
aliguori731b0362009-03-05 23:01:42 +00003062 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003063 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003064
3065 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003066 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003067 is_first_init = 0;
3068 }
aliguori87127162009-03-05 23:01:29 +00003069
3070 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003071
aliguori87127162009-03-05 23:01:29 +00003072 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003073 mon->flags = flags;
aliguori2724b182009-03-05 23:01:47 +00003074 if (mon->chr->focus != 0)
3075 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
aliguoricde76ee2009-03-05 23:01:51 +00003076 if (flags & MONITOR_USE_READLINE) {
3077 mon->rs = readline_init(mon, monitor_find_completion);
3078 monitor_read_command(mon, 0);
3079 }
aliguori87127162009-03-05 23:01:29 +00003080
aliguori731b0362009-03-05 23:01:42 +00003081 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3082 mon);
aliguori87127162009-03-05 23:01:29 +00003083
3084 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003085 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003086 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003087}
3088
aliguori376253e2009-03-05 23:01:23 +00003089static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003090{
aliguoribb5fc202009-03-05 23:01:15 +00003091 BlockDriverState *bs = opaque;
3092 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003093
aliguoribb5fc202009-03-05 23:01:15 +00003094 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003095 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003096 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003097 }
aliguori731b0362009-03-05 23:01:42 +00003098 if (mon->password_completion_cb)
3099 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003100
aliguori731b0362009-03-05 23:01:42 +00003101 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003102}
aliguoric0f4ce72009-03-05 23:01:01 +00003103
aliguori376253e2009-03-05 23:01:23 +00003104void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003105 BlockDriverCompletionFunc *completion_cb,
3106 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003107{
aliguoricde76ee2009-03-05 23:01:51 +00003108 int err;
3109
aliguoribb5fc202009-03-05 23:01:15 +00003110 if (!bdrv_key_required(bs)) {
3111 if (completion_cb)
3112 completion_cb(opaque, 0);
3113 return;
3114 }
aliguoric0f4ce72009-03-05 23:01:01 +00003115
aliguori376253e2009-03-05 23:01:23 +00003116 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3117 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003118
aliguori731b0362009-03-05 23:01:42 +00003119 mon->password_completion_cb = completion_cb;
3120 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003121
aliguoricde76ee2009-03-05 23:01:51 +00003122 err = monitor_read_password(mon, bdrv_password_cb, bs);
3123
3124 if (err && completion_cb)
3125 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003126}