blob: c6fe968ff2812733abd0f9a7e027febcbb3d5cf2 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
26#include "hw/usb.h"
27#include "hw/pcmcia.h"
28#include "hw/pc.h"
29#include "hw/pci.h"
30#include "gdbstub.h"
31#include "net.h"
32#include "qemu-char.h"
33#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000034#include "monitor.h"
35#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000036#include "console.h"
37#include "block.h"
38#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000039#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000040#include "balloon.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++) {
aliguori28a76be2009-03-06 20:27:40 +0000163 switch (filename[i]) {
164 case ' ':
165 case '"':
166 case '\\':
167 monitor_printf(mon, "\\%c", filename[i]);
168 break;
169 case '\t':
170 monitor_printf(mon, "\\t");
171 break;
172 case '\r':
173 monitor_printf(mon, "\\r");
174 break;
175 case '\n':
176 monitor_printf(mon, "\\n");
177 break;
178 default:
179 monitor_printf(mon, "%c", filename[i]);
180 break;
181 }
thsfef30742006-12-22 14:11:32 +0000182 }
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 }
aliguorid1546152009-03-12 20:12:57 +0000321 cpu_synchronize_state(cur_mon->mon_cpu, 0);
aliguori731b0362009-03-05 23:01:42 +0000322 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000323}
324
aliguori376253e2009-03-05 23:01:23 +0000325static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000326{
bellard6a00d602005-11-21 23:25:50 +0000327 CPUState *env;
328 env = mon_get_cpu();
329 if (!env)
330 return;
bellard9307c4c2004-04-04 12:57:25 +0000331#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000332 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000333 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000334#else
aliguori376253e2009-03-05 23:01:23 +0000335 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000336 0);
bellard9307c4c2004-04-04 12:57:25 +0000337#endif
338}
339
aliguori376253e2009-03-05 23:01:23 +0000340static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000341{
342 CPUState *env;
343
344 /* just to set the default cpu if not already done */
345 mon_get_cpu();
346
347 for(env = first_cpu; env != NULL; env = env->next_cpu) {
aliguorid1546152009-03-12 20:12:57 +0000348 cpu_synchronize_state(env, 0);
aliguori376253e2009-03-05 23:01:23 +0000349 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000350 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000351 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000352#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000353 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
354 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000355#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000356 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000357#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000358 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
359 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000360#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000361 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000362#endif
thsead93602007-09-06 00:18:15 +0000363 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000364 monitor_printf(mon, " (halted)");
365 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000366 }
367}
368
aliguori376253e2009-03-05 23:01:23 +0000369static void do_cpu_set(Monitor *mon, int index)
bellard6a00d602005-11-21 23:25:50 +0000370{
371 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000372 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000373}
374
aliguori376253e2009-03-05 23:01:23 +0000375static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000376{
aliguori376253e2009-03-05 23:01:23 +0000377 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000378}
379
aliguori376253e2009-03-05 23:01:23 +0000380static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000381{
382 int i;
bellard7e2515e2004-08-01 21:52:19 +0000383 const char *str;
ths3b46e622007-09-17 08:09:54 +0000384
aliguoricde76ee2009-03-05 23:01:51 +0000385 if (!mon->rs)
386 return;
bellard7e2515e2004-08-01 21:52:19 +0000387 i = 0;
388 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000389 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000390 if (!str)
391 break;
aliguori376253e2009-03-05 23:01:23 +0000392 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000393 i++;
bellardaa455482004-04-04 13:07:25 +0000394 }
395}
396
j_mayer76a66252007-03-07 08:32:30 +0000397#if defined(TARGET_PPC)
398/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000399static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000400{
401 CPUState *env;
402
403 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000404 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000405}
406#endif
407
aliguori376253e2009-03-05 23:01:23 +0000408static void do_quit(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000409{
410 exit(0);
411}
412
aliguori376253e2009-03-05 23:01:23 +0000413static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000414{
415 if (bdrv_is_inserted(bs)) {
416 if (!force) {
417 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000418 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000419 return -1;
420 }
421 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000422 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000423 return -1;
424 }
425 }
426 bdrv_close(bs);
427 }
428 return 0;
429}
430
aliguori376253e2009-03-05 23:01:23 +0000431static void do_eject(Monitor *mon, int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000432{
433 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000434
bellard9307c4c2004-04-04 12:57:25 +0000435 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000436 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000437 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000438 return;
439 }
aliguori376253e2009-03-05 23:01:23 +0000440 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000441}
442
aliguori376253e2009-03-05 23:01:23 +0000443static void do_change_block(Monitor *mon, const char *device,
444 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000445{
446 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000447 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000448
bellard9307c4c2004-04-04 12:57:25 +0000449 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000450 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000451 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000452 return;
453 }
aurel322ecea9b2008-06-18 22:10:01 +0000454 if (fmt) {
455 drv = bdrv_find_format(fmt);
456 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000457 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000458 return;
459 }
460 }
aliguori376253e2009-03-05 23:01:23 +0000461 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000462 return;
aurel322ecea9b2008-06-18 22:10:01 +0000463 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000464 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000465}
466
aliguori376253e2009-03-05 23:01:23 +0000467static void change_vnc_password_cb(Monitor *mon, const char *password,
468 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000469{
470 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000471 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000472
aliguori731b0362009-03-05 23:01:42 +0000473 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000474}
475
aliguori376253e2009-03-05 23:01:23 +0000476static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000477{
ths70848512007-08-25 01:37:05 +0000478 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000479 strcmp(target, "password") == 0) {
480 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000481 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000482 strncpy(password, arg, sizeof(password));
483 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000484 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000485 } else {
aliguori376253e2009-03-05 23:01:23 +0000486 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000487 }
ths70848512007-08-25 01:37:05 +0000488 } else {
aliguori28a76be2009-03-06 20:27:40 +0000489 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000490 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000491 }
thse25a5822007-08-25 01:36:20 +0000492}
493
aliguori376253e2009-03-05 23:01:23 +0000494static void do_change(Monitor *mon, const char *device, const char *target,
495 const char *arg)
thse25a5822007-08-25 01:36:20 +0000496{
497 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000498 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000499 } else {
aliguori28a76be2009-03-06 20:27:40 +0000500 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000501 }
502}
503
aliguori376253e2009-03-05 23:01:23 +0000504static void do_screen_dump(Monitor *mon, const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000505{
pbrook95219892006-04-09 01:06:34 +0000506 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000507}
508
aliguori376253e2009-03-05 23:01:23 +0000509static void do_logfile(Monitor *mon, const char *filename)
pbrooke735b912007-06-30 13:53:24 +0000510{
511 cpu_set_log_filename(filename);
512}
513
aliguori376253e2009-03-05 23:01:23 +0000514static void do_log(Monitor *mon, const char *items)
bellardf193c792004-03-21 17:06:25 +0000515{
516 int mask;
ths3b46e622007-09-17 08:09:54 +0000517
bellard9307c4c2004-04-04 12:57:25 +0000518 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000519 mask = 0;
520 } else {
bellard9307c4c2004-04-04 12:57:25 +0000521 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000522 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000523 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000524 return;
525 }
526 }
527 cpu_set_log(mask);
528}
529
aliguori376253e2009-03-05 23:01:23 +0000530static void do_stop(Monitor *mon)
bellard8a7ddc32004-03-31 19:00:16 +0000531{
532 vm_stop(EXCP_INTERRUPT);
533}
534
aliguoribb5fc202009-03-05 23:01:15 +0000535static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000536
aliguori376253e2009-03-05 23:01:23 +0000537struct bdrv_iterate_context {
538 Monitor *mon;
539 int err;
540};
aliguoric0f4ce72009-03-05 23:01:01 +0000541
aliguori376253e2009-03-05 23:01:23 +0000542static void do_cont(Monitor *mon)
543{
544 struct bdrv_iterate_context context = { mon, 0 };
545
546 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000547 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000548 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000549 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000550}
551
aliguoribb5fc202009-03-05 23:01:15 +0000552static void bdrv_key_cb(void *opaque, int err)
553{
aliguori376253e2009-03-05 23:01:23 +0000554 Monitor *mon = opaque;
555
aliguoribb5fc202009-03-05 23:01:15 +0000556 /* another key was set successfully, retry to continue */
557 if (!err)
aliguori376253e2009-03-05 23:01:23 +0000558 do_cont(mon);
aliguoribb5fc202009-03-05 23:01:15 +0000559}
560
561static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
562{
aliguori376253e2009-03-05 23:01:23 +0000563 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000564
aliguori376253e2009-03-05 23:01:23 +0000565 if (!context->err && bdrv_key_required(bs)) {
566 context->err = -EBUSY;
567 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
568 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000569 }
570}
571
bellard67b915a2004-03-31 23:37:16 +0000572#ifdef CONFIG_GDBSTUB
aliguori376253e2009-03-05 23:01:23 +0000573static void do_gdbserver(Monitor *mon, const char *port)
bellard8a7ddc32004-03-31 19:00:16 +0000574{
pbrookcfc34752007-02-22 01:48:01 +0000575 if (!port)
bellard9307c4c2004-04-04 12:57:25 +0000576 port = DEFAULT_GDBSTUB_PORT;
pbrookcfc34752007-02-22 01:48:01 +0000577 if (gdbserver_start(port) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000578 monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",
579 port);
aliguori36556b22009-03-28 18:05:53 +0000580 } else if (strcmp(port, "none") == 0) {
581 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000582 } else {
aliguori376253e2009-03-05 23:01:23 +0000583 monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000584 }
585}
bellard67b915a2004-03-31 23:37:16 +0000586#endif
bellard8a7ddc32004-03-31 19:00:16 +0000587
aliguori376253e2009-03-05 23:01:23 +0000588static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000589{
aliguori376253e2009-03-05 23:01:23 +0000590 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000591 switch(c) {
592 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000593 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000594 break;
595 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000596 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000597 break;
598 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000599 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000600 break;
601 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000602 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000603 break;
604 default:
605 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000606 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000607 } else {
aliguori376253e2009-03-05 23:01:23 +0000608 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000609 }
610 break;
611 }
aliguori376253e2009-03-05 23:01:23 +0000612 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000613}
614
aliguori376253e2009-03-05 23:01:23 +0000615static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000616 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000617{
bellard6a00d602005-11-21 23:25:50 +0000618 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000619 int nb_per_line, l, line_size, i, max_digits, len;
620 uint8_t buf[16];
621 uint64_t v;
622
623 if (format == 'i') {
624 int flags;
625 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000626 env = mon_get_cpu();
627 if (!env && !is_physical)
628 return;
bellard9307c4c2004-04-04 12:57:25 +0000629#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000630 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000631 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000632 } else if (wsize == 4) {
633 flags = 0;
634 } else {
bellard6a15fd12006-04-12 21:07:07 +0000635 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000636 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000637 if (env) {
638#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000639 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000640 (env->segs[R_CS].flags & DESC_L_MASK))
641 flags = 2;
642 else
643#endif
644 if (!(env->segs[R_CS].flags & DESC_B_MASK))
645 flags = 1;
646 }
bellard4c27ba22004-04-25 18:05:08 +0000647 }
648#endif
aliguori376253e2009-03-05 23:01:23 +0000649 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000650 return;
651 }
652
653 len = wsize * count;
654 if (wsize == 1)
655 line_size = 8;
656 else
657 line_size = 16;
658 nb_per_line = line_size / wsize;
659 max_digits = 0;
660
661 switch(format) {
662 case 'o':
663 max_digits = (wsize * 8 + 2) / 3;
664 break;
665 default:
666 case 'x':
667 max_digits = (wsize * 8) / 4;
668 break;
669 case 'u':
670 case 'd':
671 max_digits = (wsize * 8 * 10 + 32) / 33;
672 break;
673 case 'c':
674 wsize = 1;
675 break;
676 }
677
678 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000679 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000680 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000681 else
aliguori376253e2009-03-05 23:01:23 +0000682 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000683 l = len;
684 if (l > line_size)
685 l = line_size;
686 if (is_physical) {
687 cpu_physical_memory_rw(addr, buf, l, 0);
688 } else {
bellard6a00d602005-11-21 23:25:50 +0000689 env = mon_get_cpu();
690 if (!env)
691 break;
aliguoric8f79b62008-08-18 14:00:20 +0000692 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000693 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000694 break;
695 }
bellard9307c4c2004-04-04 12:57:25 +0000696 }
ths5fafdf22007-09-16 21:08:06 +0000697 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000698 while (i < l) {
699 switch(wsize) {
700 default:
701 case 1:
702 v = ldub_raw(buf + i);
703 break;
704 case 2:
705 v = lduw_raw(buf + i);
706 break;
707 case 4:
bellard92a31b12005-02-10 22:00:52 +0000708 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000709 break;
710 case 8:
711 v = ldq_raw(buf + i);
712 break;
713 }
aliguori376253e2009-03-05 23:01:23 +0000714 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000715 switch(format) {
716 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000717 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000718 break;
719 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000720 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000721 break;
722 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000723 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000724 break;
725 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000726 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000727 break;
728 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000729 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000730 break;
731 }
732 i += wsize;
733 }
aliguori376253e2009-03-05 23:01:23 +0000734 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000735 addr += l;
736 len -= l;
737 }
738}
739
bellard92a31b12005-02-10 22:00:52 +0000740#if TARGET_LONG_BITS == 64
741#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
742#else
743#define GET_TLONG(h, l) (l)
744#endif
745
aliguori376253e2009-03-05 23:01:23 +0000746static void do_memory_dump(Monitor *mon, int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000747 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000748{
bellard92a31b12005-02-10 22:00:52 +0000749 target_long addr = GET_TLONG(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000750 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000751}
752
blueswir17743e582007-09-24 18:39:04 +0000753#if TARGET_PHYS_ADDR_BITS > 32
754#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
755#else
756#define GET_TPHYSADDR(h, l) (l)
757#endif
758
aliguori376253e2009-03-05 23:01:23 +0000759static void do_physical_memory_dump(Monitor *mon, int count, int format,
760 int size, uint32_t addrh, uint32_t addrl)
bellard92a31b12005-02-10 22:00:52 +0000761
bellard9307c4c2004-04-04 12:57:25 +0000762{
blueswir17743e582007-09-24 18:39:04 +0000763 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000764 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000765}
766
aliguori376253e2009-03-05 23:01:23 +0000767static void do_print(Monitor *mon, int count, int format, int size,
768 unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000769{
blueswir17743e582007-09-24 18:39:04 +0000770 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
771#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000772 switch(format) {
773 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000774 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000775 break;
776 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000777 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000778 break;
779 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000780 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000781 break;
782 default:
783 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000784 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000785 break;
786 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000787 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000788 break;
789 }
bellard92a31b12005-02-10 22:00:52 +0000790#else
791 switch(format) {
792 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000793 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000794 break;
795 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000796 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000797 break;
798 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000799 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000800 break;
801 default:
802 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000803 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000804 break;
805 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000806 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000807 break;
808 }
809#endif
aliguori376253e2009-03-05 23:01:23 +0000810 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000811}
812
aliguori376253e2009-03-05 23:01:23 +0000813static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000814 uint32_t size, const char *filename)
815{
816 FILE *f;
817 target_long addr = GET_TLONG(valh, vall);
818 uint32_t l;
819 CPUState *env;
820 uint8_t buf[1024];
821
822 env = mon_get_cpu();
823 if (!env)
824 return;
825
826 f = fopen(filename, "wb");
827 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000828 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000829 return;
830 }
831 while (size != 0) {
832 l = sizeof(buf);
833 if (l > size)
834 l = size;
835 cpu_memory_rw_debug(env, addr, buf, l, 0);
836 fwrite(buf, 1, l, f);
837 addr += l;
838 size -= l;
839 }
840 fclose(f);
841}
842
aliguori376253e2009-03-05 23:01:23 +0000843static void do_physical_memory_save(Monitor *mon, unsigned int valh,
844 unsigned int vall, uint32_t size,
845 const char *filename)
aurel32a8bdf7a2008-04-11 21:36:14 +0000846{
847 FILE *f;
848 uint32_t l;
849 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000850 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000851
852 f = fopen(filename, "wb");
853 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000854 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000855 return;
856 }
857 while (size != 0) {
858 l = sizeof(buf);
859 if (l > size)
860 l = size;
861 cpu_physical_memory_rw(addr, buf, l, 0);
862 fwrite(buf, 1, l, f);
863 fflush(f);
864 addr += l;
865 size -= l;
866 }
867 fclose(f);
868}
869
aliguori376253e2009-03-05 23:01:23 +0000870static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
bellarde4cf1ad2005-06-04 20:15:57 +0000871{
872 uint32_t addr;
873 uint8_t buf[1];
874 uint16_t sum;
875
876 sum = 0;
877 for(addr = start; addr < (start + size); addr++) {
878 cpu_physical_memory_rw(addr, buf, 1, 0);
879 /* BSD sum algorithm ('sum' Unix command) */
880 sum = (sum >> 1) | (sum << 15);
881 sum += buf[0];
882 }
aliguori376253e2009-03-05 23:01:23 +0000883 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000884}
885
bellarda3a91a32004-06-04 11:06:21 +0000886typedef struct {
887 int keycode;
888 const char *name;
889} KeyDef;
890
891static const KeyDef key_defs[] = {
892 { 0x2a, "shift" },
893 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000894
bellarda3a91a32004-06-04 11:06:21 +0000895 { 0x38, "alt" },
896 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000897 { 0x64, "altgr" },
898 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000899 { 0x1d, "ctrl" },
900 { 0x9d, "ctrl_r" },
901
902 { 0xdd, "menu" },
903
904 { 0x01, "esc" },
905
906 { 0x02, "1" },
907 { 0x03, "2" },
908 { 0x04, "3" },
909 { 0x05, "4" },
910 { 0x06, "5" },
911 { 0x07, "6" },
912 { 0x08, "7" },
913 { 0x09, "8" },
914 { 0x0a, "9" },
915 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000916 { 0x0c, "minus" },
917 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000918 { 0x0e, "backspace" },
919
920 { 0x0f, "tab" },
921 { 0x10, "q" },
922 { 0x11, "w" },
923 { 0x12, "e" },
924 { 0x13, "r" },
925 { 0x14, "t" },
926 { 0x15, "y" },
927 { 0x16, "u" },
928 { 0x17, "i" },
929 { 0x18, "o" },
930 { 0x19, "p" },
931
932 { 0x1c, "ret" },
933
934 { 0x1e, "a" },
935 { 0x1f, "s" },
936 { 0x20, "d" },
937 { 0x21, "f" },
938 { 0x22, "g" },
939 { 0x23, "h" },
940 { 0x24, "j" },
941 { 0x25, "k" },
942 { 0x26, "l" },
943
944 { 0x2c, "z" },
945 { 0x2d, "x" },
946 { 0x2e, "c" },
947 { 0x2f, "v" },
948 { 0x30, "b" },
949 { 0x31, "n" },
950 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000951 { 0x33, "comma" },
952 { 0x34, "dot" },
953 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000954
balrog4d3b6f62008-02-10 16:33:14 +0000955 { 0x37, "asterisk" },
956
bellarda3a91a32004-06-04 11:06:21 +0000957 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000958 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000959 { 0x3b, "f1" },
960 { 0x3c, "f2" },
961 { 0x3d, "f3" },
962 { 0x3e, "f4" },
963 { 0x3f, "f5" },
964 { 0x40, "f6" },
965 { 0x41, "f7" },
966 { 0x42, "f8" },
967 { 0x43, "f9" },
968 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +0000969 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000970 { 0x46, "scroll_lock" },
971
bellard64866c32006-05-07 18:03:31 +0000972 { 0xb5, "kp_divide" },
973 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +0000974 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +0000975 { 0x4e, "kp_add" },
976 { 0x9c, "kp_enter" },
977 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +0000978 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +0000979
980 { 0x52, "kp_0" },
981 { 0x4f, "kp_1" },
982 { 0x50, "kp_2" },
983 { 0x51, "kp_3" },
984 { 0x4b, "kp_4" },
985 { 0x4c, "kp_5" },
986 { 0x4d, "kp_6" },
987 { 0x47, "kp_7" },
988 { 0x48, "kp_8" },
989 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +0000990
bellarda3a91a32004-06-04 11:06:21 +0000991 { 0x56, "<" },
992
993 { 0x57, "f11" },
994 { 0x58, "f12" },
995
996 { 0xb7, "print" },
997
998 { 0xc7, "home" },
999 { 0xc9, "pgup" },
1000 { 0xd1, "pgdn" },
1001 { 0xcf, "end" },
1002
1003 { 0xcb, "left" },
1004 { 0xc8, "up" },
1005 { 0xd0, "down" },
1006 { 0xcd, "right" },
1007
1008 { 0xd2, "insert" },
1009 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001010#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1011 { 0xf0, "stop" },
1012 { 0xf1, "again" },
1013 { 0xf2, "props" },
1014 { 0xf3, "undo" },
1015 { 0xf4, "front" },
1016 { 0xf5, "copy" },
1017 { 0xf6, "open" },
1018 { 0xf7, "paste" },
1019 { 0xf8, "find" },
1020 { 0xf9, "cut" },
1021 { 0xfa, "lf" },
1022 { 0xfb, "help" },
1023 { 0xfc, "meta_l" },
1024 { 0xfd, "meta_r" },
1025 { 0xfe, "compose" },
1026#endif
bellarda3a91a32004-06-04 11:06:21 +00001027 { 0, NULL },
1028};
1029
1030static int get_keycode(const char *key)
1031{
1032 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001033 char *endp;
1034 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001035
1036 for(p = key_defs; p->name != NULL; p++) {
1037 if (!strcmp(key, p->name))
1038 return p->keycode;
1039 }
bellard64866c32006-05-07 18:03:31 +00001040 if (strstart(key, "0x", NULL)) {
1041 ret = strtoul(key, &endp, 0);
1042 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1043 return ret;
1044 }
bellarda3a91a32004-06-04 11:06:21 +00001045 return -1;
1046}
1047
balrogc8256f92008-06-08 22:45:01 +00001048#define MAX_KEYCODES 16
1049static uint8_t keycodes[MAX_KEYCODES];
1050static int nb_pending_keycodes;
1051static QEMUTimer *key_timer;
1052
1053static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001054{
balrogc8256f92008-06-08 22:45:01 +00001055 int keycode;
1056
1057 while (nb_pending_keycodes > 0) {
1058 nb_pending_keycodes--;
1059 keycode = keycodes[nb_pending_keycodes];
1060 if (keycode & 0x80)
1061 kbd_put_keycode(0xe0);
1062 kbd_put_keycode(keycode | 0x80);
1063 }
1064}
1065
aliguori376253e2009-03-05 23:01:23 +00001066static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1067 int hold_time)
balrogc8256f92008-06-08 22:45:01 +00001068{
balrog3401c0d2008-06-04 10:05:59 +00001069 char keyname_buf[16];
1070 char *separator;
1071 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +00001072
balrogc8256f92008-06-08 22:45:01 +00001073 if (nb_pending_keycodes > 0) {
1074 qemu_del_timer(key_timer);
1075 release_keys(NULL);
1076 }
1077 if (!has_hold_time)
1078 hold_time = 100;
1079 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001080 while (1) {
1081 separator = strchr(string, '-');
1082 keyname_len = separator ? separator - string : strlen(string);
1083 if (keyname_len > 0) {
1084 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1085 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001086 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001087 return;
bellarda3a91a32004-06-04 11:06:21 +00001088 }
balrogc8256f92008-06-08 22:45:01 +00001089 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001090 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001091 return;
1092 }
1093 keyname_buf[keyname_len] = 0;
1094 keycode = get_keycode(keyname_buf);
1095 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001096 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001097 return;
1098 }
balrogc8256f92008-06-08 22:45:01 +00001099 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001100 }
balrog3401c0d2008-06-04 10:05:59 +00001101 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001102 break;
balrog3401c0d2008-06-04 10:05:59 +00001103 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001104 }
balrogc8256f92008-06-08 22:45:01 +00001105 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001106 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001107 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001108 keycode = keycodes[i];
1109 if (keycode & 0x80)
1110 kbd_put_keycode(0xe0);
1111 kbd_put_keycode(keycode & 0x7f);
1112 }
balrogc8256f92008-06-08 22:45:01 +00001113 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001114 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1115 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001116}
1117
bellard13224a82006-07-14 22:03:35 +00001118static int mouse_button_state;
1119
aliguori376253e2009-03-05 23:01:23 +00001120static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001121 const char *dz_str)
1122{
1123 int dx, dy, dz;
1124 dx = strtol(dx_str, NULL, 0);
1125 dy = strtol(dy_str, NULL, 0);
1126 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001127 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001128 dz = strtol(dz_str, NULL, 0);
1129 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1130}
1131
aliguori376253e2009-03-05 23:01:23 +00001132static void do_mouse_button(Monitor *mon, int button_state)
bellard13224a82006-07-14 22:03:35 +00001133{
1134 mouse_button_state = button_state;
1135 kbd_mouse_event(0, 0, 0, mouse_button_state);
1136}
1137
aliguori376253e2009-03-05 23:01:23 +00001138static void do_ioport_read(Monitor *mon, int count, int format, int size,
1139 int addr, int has_index, int index)
bellard34405572004-06-08 00:55:58 +00001140{
1141 uint32_t val;
1142 int suffix;
1143
1144 if (has_index) {
1145 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1146 addr++;
1147 }
1148 addr &= 0xffff;
1149
1150 switch(size) {
1151 default:
1152 case 1:
1153 val = cpu_inb(NULL, addr);
1154 suffix = 'b';
1155 break;
1156 case 2:
1157 val = cpu_inw(NULL, addr);
1158 suffix = 'w';
1159 break;
1160 case 4:
1161 val = cpu_inl(NULL, addr);
1162 suffix = 'l';
1163 break;
1164 }
aliguori376253e2009-03-05 23:01:23 +00001165 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1166 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001167}
bellarda3a91a32004-06-04 11:06:21 +00001168
blueswir13b4366d2008-06-20 16:25:06 +00001169/* boot_set handler */
1170static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1171static void *boot_opaque;
1172
1173void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1174{
1175 qemu_boot_set_handler = func;
1176 boot_opaque = opaque;
1177}
1178
aliguori376253e2009-03-05 23:01:23 +00001179static void do_boot_set(Monitor *mon, const char *bootdevice)
aurel320ecdffb2008-05-04 20:11:34 +00001180{
1181 int res;
1182
1183 if (qemu_boot_set_handler) {
blueswir13b4366d2008-06-20 16:25:06 +00001184 res = qemu_boot_set_handler(boot_opaque, bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001185 if (res == 0)
aliguori376253e2009-03-05 23:01:23 +00001186 monitor_printf(mon, "boot device list now set to %s\n",
1187 bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001188 else
aliguori376253e2009-03-05 23:01:23 +00001189 monitor_printf(mon, "setting boot device list failed with "
1190 "error %i\n", res);
aurel320ecdffb2008-05-04 20:11:34 +00001191 } else {
aliguori376253e2009-03-05 23:01:23 +00001192 monitor_printf(mon, "no function defined to set boot device list for "
1193 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001194 }
1195}
1196
aliguori376253e2009-03-05 23:01:23 +00001197static void do_system_reset(Monitor *mon)
bellarde4f90822004-06-20 12:35:44 +00001198{
1199 qemu_system_reset_request();
1200}
1201
aliguori376253e2009-03-05 23:01:23 +00001202static void do_system_powerdown(Monitor *mon)
bellard34751872005-07-02 14:31:34 +00001203{
1204 qemu_system_powerdown_request();
1205}
1206
bellardb86bda52004-09-18 19:32:46 +00001207#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001208static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001209{
aliguori376253e2009-03-05 23:01:23 +00001210 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1211 addr,
1212 pte & mask,
1213 pte & PG_GLOBAL_MASK ? 'G' : '-',
1214 pte & PG_PSE_MASK ? 'P' : '-',
1215 pte & PG_DIRTY_MASK ? 'D' : '-',
1216 pte & PG_ACCESSED_MASK ? 'A' : '-',
1217 pte & PG_PCD_MASK ? 'C' : '-',
1218 pte & PG_PWT_MASK ? 'T' : '-',
1219 pte & PG_USER_MASK ? 'U' : '-',
1220 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001221}
1222
aliguori376253e2009-03-05 23:01:23 +00001223static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001224{
bellard6a00d602005-11-21 23:25:50 +00001225 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001226 int l1, l2;
1227 uint32_t pgd, pde, pte;
1228
bellard6a00d602005-11-21 23:25:50 +00001229 env = mon_get_cpu();
1230 if (!env)
1231 return;
1232
bellardb86bda52004-09-18 19:32:46 +00001233 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001234 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001235 return;
1236 }
1237 pgd = env->cr[3] & ~0xfff;
1238 for(l1 = 0; l1 < 1024; l1++) {
1239 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1240 pde = le32_to_cpu(pde);
1241 if (pde & PG_PRESENT_MASK) {
1242 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001243 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001244 } else {
1245 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001246 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001247 (uint8_t *)&pte, 4);
1248 pte = le32_to_cpu(pte);
1249 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001250 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001251 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001252 ~0xfff);
1253 }
1254 }
1255 }
1256 }
1257 }
1258}
1259
aliguori376253e2009-03-05 23:01:23 +00001260static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001261 uint32_t end, int prot)
1262{
bellard9746b152004-11-11 18:30:24 +00001263 int prot1;
1264 prot1 = *plast_prot;
1265 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001266 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001267 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1268 *pstart, end, end - *pstart,
1269 prot1 & PG_USER_MASK ? 'u' : '-',
1270 'r',
1271 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001272 }
1273 if (prot != 0)
1274 *pstart = end;
1275 else
1276 *pstart = -1;
1277 *plast_prot = prot;
1278 }
1279}
1280
aliguori376253e2009-03-05 23:01:23 +00001281static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001282{
bellard6a00d602005-11-21 23:25:50 +00001283 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001284 int l1, l2, prot, last_prot;
1285 uint32_t pgd, pde, pte, start, end;
1286
bellard6a00d602005-11-21 23:25:50 +00001287 env = mon_get_cpu();
1288 if (!env)
1289 return;
1290
bellardb86bda52004-09-18 19:32:46 +00001291 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001292 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001293 return;
1294 }
1295 pgd = env->cr[3] & ~0xfff;
1296 last_prot = 0;
1297 start = -1;
1298 for(l1 = 0; l1 < 1024; l1++) {
1299 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1300 pde = le32_to_cpu(pde);
1301 end = l1 << 22;
1302 if (pde & PG_PRESENT_MASK) {
1303 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1304 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001305 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001306 } else {
1307 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001308 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001309 (uint8_t *)&pte, 4);
1310 pte = le32_to_cpu(pte);
1311 end = (l1 << 22) + (l2 << 12);
1312 if (pte & PG_PRESENT_MASK) {
1313 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1314 } else {
1315 prot = 0;
1316 }
aliguori376253e2009-03-05 23:01:23 +00001317 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001318 }
1319 }
1320 } else {
1321 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001322 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001323 }
1324 }
1325}
1326#endif
1327
aurel327c664e22009-03-03 06:12:22 +00001328#if defined(TARGET_SH4)
1329
aliguori376253e2009-03-05 23:01:23 +00001330static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001331{
aliguori376253e2009-03-05 23:01:23 +00001332 monitor_printf(mon, " tlb%i:\t"
1333 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1334 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1335 "dirty=%hhu writethrough=%hhu\n",
1336 idx,
1337 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1338 tlb->v, tlb->sh, tlb->c, tlb->pr,
1339 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001340}
1341
aliguori376253e2009-03-05 23:01:23 +00001342static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001343{
1344 CPUState *env = mon_get_cpu();
1345 int i;
1346
aliguori376253e2009-03-05 23:01:23 +00001347 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001348 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001349 print_tlb (mon, i, &env->itlb[i]);
1350 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001351 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001352 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001353}
1354
1355#endif
1356
aliguori376253e2009-03-05 23:01:23 +00001357static void do_info_kqemu(Monitor *mon)
bellard0f4c6412005-07-24 18:10:56 +00001358{
1359#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001360 CPUState *env;
bellard0f4c6412005-07-24 18:10:56 +00001361 int val;
1362 val = 0;
bellard6a00d602005-11-21 23:25:50 +00001363 env = mon_get_cpu();
1364 if (!env) {
aliguori376253e2009-03-05 23:01:23 +00001365 monitor_printf(mon, "No cpu initialized yet");
bellard6a00d602005-11-21 23:25:50 +00001366 return;
1367 }
1368 val = env->kqemu_enabled;
aliguori376253e2009-03-05 23:01:23 +00001369 monitor_printf(mon, "kqemu support: ");
bellard5f1ce942006-02-08 22:40:15 +00001370 switch(val) {
1371 default:
1372 case 0:
aliguori376253e2009-03-05 23:01:23 +00001373 monitor_printf(mon, "disabled\n");
bellard5f1ce942006-02-08 22:40:15 +00001374 break;
1375 case 1:
aliguori376253e2009-03-05 23:01:23 +00001376 monitor_printf(mon, "enabled for user code\n");
bellard5f1ce942006-02-08 22:40:15 +00001377 break;
1378 case 2:
aliguori376253e2009-03-05 23:01:23 +00001379 monitor_printf(mon, "enabled for user and kernel code\n");
bellard5f1ce942006-02-08 22:40:15 +00001380 break;
1381 }
bellard0f4c6412005-07-24 18:10:56 +00001382#else
aliguori376253e2009-03-05 23:01:23 +00001383 monitor_printf(mon, "kqemu support: not compiled\n");
bellard0f4c6412005-07-24 18:10:56 +00001384#endif
ths5fafdf22007-09-16 21:08:06 +00001385}
bellard0f4c6412005-07-24 18:10:56 +00001386
aliguori376253e2009-03-05 23:01:23 +00001387static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001388{
1389#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001390 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001391 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001392 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001393 else
aliguori376253e2009-03-05 23:01:23 +00001394 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001395#else
aliguori376253e2009-03-05 23:01:23 +00001396 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001397#endif
1398}
1399
bellard5f1ce942006-02-08 22:40:15 +00001400#ifdef CONFIG_PROFILER
1401
1402int64_t kqemu_time;
1403int64_t qemu_time;
1404int64_t kqemu_exec_count;
1405int64_t dev_time;
1406int64_t kqemu_ret_int_count;
1407int64_t kqemu_ret_excp_count;
1408int64_t kqemu_ret_intr_count;
1409
aliguori376253e2009-03-05 23:01:23 +00001410static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001411{
1412 int64_t total;
1413 total = qemu_time;
1414 if (total == 0)
1415 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001416 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1417 dev_time, dev_time / (double)ticks_per_sec);
1418 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1419 qemu_time, qemu_time / (double)ticks_per_sec);
1420 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1421 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1422 PRId64 "\n",
1423 kqemu_time, kqemu_time / (double)ticks_per_sec,
1424 kqemu_time / (double)total * 100.0,
1425 kqemu_exec_count,
1426 kqemu_ret_int_count,
1427 kqemu_ret_excp_count,
1428 kqemu_ret_intr_count);
bellard5f1ce942006-02-08 22:40:15 +00001429 qemu_time = 0;
1430 kqemu_time = 0;
1431 kqemu_exec_count = 0;
1432 dev_time = 0;
1433 kqemu_ret_int_count = 0;
1434 kqemu_ret_excp_count = 0;
1435 kqemu_ret_intr_count = 0;
1436#ifdef USE_KQEMU
1437 kqemu_record_dump();
1438#endif
1439}
1440#else
aliguori376253e2009-03-05 23:01:23 +00001441static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001442{
aliguori376253e2009-03-05 23:01:23 +00001443 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001444}
1445#endif
1446
bellardec36b692006-07-16 18:57:03 +00001447/* Capture support */
1448static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1449
aliguori376253e2009-03-05 23:01:23 +00001450static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001451{
1452 int i;
1453 CaptureState *s;
1454
1455 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001456 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001457 s->ops.info (s->opaque);
1458 }
1459}
1460
aliguori376253e2009-03-05 23:01:23 +00001461static void do_stop_capture(Monitor *mon, int n)
bellardec36b692006-07-16 18:57:03 +00001462{
1463 int i;
1464 CaptureState *s;
1465
1466 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1467 if (i == n) {
1468 s->ops.destroy (s->opaque);
1469 LIST_REMOVE (s, entries);
1470 qemu_free (s);
1471 return;
1472 }
1473 }
1474}
1475
1476#ifdef HAS_AUDIO
aliguori376253e2009-03-05 23:01:23 +00001477static void do_wav_capture(Monitor *mon, const char *path,
1478 int has_freq, int freq,
1479 int has_bits, int bits,
1480 int has_channels, int nchannels)
bellardec36b692006-07-16 18:57:03 +00001481{
1482 CaptureState *s;
1483
1484 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001485
1486 freq = has_freq ? freq : 44100;
1487 bits = has_bits ? bits : 16;
1488 nchannels = has_channels ? nchannels : 2;
1489
1490 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001491 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001492 qemu_free (s);
1493 }
1494 LIST_INSERT_HEAD (&capture_head, s, entries);
1495}
1496#endif
1497
aurel32dc1c0b72008-04-27 23:52:12 +00001498#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001499static void do_inject_nmi(Monitor *mon, int cpu_index)
aurel32dc1c0b72008-04-27 23:52:12 +00001500{
1501 CPUState *env;
1502
1503 for (env = first_cpu; env != NULL; env = env->next_cpu)
1504 if (env->cpu_index == cpu_index) {
1505 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1506 break;
1507 }
1508}
1509#endif
1510
aliguori376253e2009-03-05 23:01:23 +00001511static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001512{
1513 if (vm_running)
aliguori376253e2009-03-05 23:01:23 +00001514 monitor_printf(mon, "VM status: running\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001515 else
aliguori376253e2009-03-05 23:01:23 +00001516 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001517}
1518
1519
aliguori376253e2009-03-05 23:01:23 +00001520static void do_balloon(Monitor *mon, int value)
aliguoridf751fa2008-12-04 20:19:35 +00001521{
1522 ram_addr_t target = value;
1523 qemu_balloon(target << 20);
1524}
1525
aliguori376253e2009-03-05 23:01:23 +00001526static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001527{
1528 ram_addr_t actual;
1529
1530 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001531 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001532 monitor_printf(mon, "Using KVM without synchronous MMU, "
1533 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001534 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001535 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001536 else
aliguori376253e2009-03-05 23:01:23 +00001537 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001538}
1539
aliguori76655d62009-03-06 20:27:37 +00001540static void do_acl(Monitor *mon,
1541 const char *command,
aliguori28a76be2009-03-06 20:27:40 +00001542 const char *aclname,
1543 const char *match,
1544 int has_index,
1545 int index)
aliguori76655d62009-03-06 20:27:37 +00001546{
1547 qemu_acl *acl;
1548
1549 acl = qemu_acl_find(aclname);
1550 if (!acl) {
aliguori28a76be2009-03-06 20:27:40 +00001551 monitor_printf(mon, "acl: unknown list '%s'\n", aclname);
1552 return;
aliguori76655d62009-03-06 20:27:37 +00001553 }
1554
1555 if (strcmp(command, "show") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001556 int i = 0;
1557 qemu_acl_entry *entry;
1558 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001559 acl->defaultDeny ? "deny" : "allow");
aliguori28a76be2009-03-06 20:27:40 +00001560 TAILQ_FOREACH(entry, &acl->entries, next) {
1561 i++;
1562 monitor_printf(mon, "%d: %s %s\n", i,
aliguori76655d62009-03-06 20:27:37 +00001563 entry->deny ? "deny" : "allow",
1564 entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001565 }
aliguori76655d62009-03-06 20:27:37 +00001566 } else if (strcmp(command, "reset") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001567 qemu_acl_reset(acl);
1568 monitor_printf(mon, "acl: removed all rules\n");
aliguori76655d62009-03-06 20:27:37 +00001569 } else if (strcmp(command, "policy") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001570 if (!match) {
1571 monitor_printf(mon, "acl: missing policy parameter\n");
1572 return;
1573 }
aliguori76655d62009-03-06 20:27:37 +00001574
aliguori28a76be2009-03-06 20:27:40 +00001575 if (strcmp(match, "allow") == 0) {
1576 acl->defaultDeny = 0;
1577 monitor_printf(mon, "acl: policy set to 'allow'\n");
1578 } else if (strcmp(match, "deny") == 0) {
1579 acl->defaultDeny = 1;
1580 monitor_printf(mon, "acl: policy set to 'deny'\n");
1581 } else {
1582 monitor_printf(mon, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match);
1583 }
aliguori76655d62009-03-06 20:27:37 +00001584 } else if ((strcmp(command, "allow") == 0) ||
aliguori28a76be2009-03-06 20:27:40 +00001585 (strcmp(command, "deny") == 0)) {
1586 int deny = strcmp(command, "deny") == 0 ? 1 : 0;
1587 int ret;
aliguori76655d62009-03-06 20:27:37 +00001588
aliguori28a76be2009-03-06 20:27:40 +00001589 if (!match) {
1590 monitor_printf(mon, "acl: missing match parameter\n");
1591 return;
1592 }
aliguori76655d62009-03-06 20:27:37 +00001593
aliguori28a76be2009-03-06 20:27:40 +00001594 if (has_index)
1595 ret = qemu_acl_insert(acl, deny, match, index);
1596 else
1597 ret = qemu_acl_append(acl, deny, match);
1598 if (ret < 0)
1599 monitor_printf(mon, "acl: unable to add acl entry\n");
1600 else
1601 monitor_printf(mon, "acl: added rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001602 } else if (strcmp(command, "remove") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001603 int ret;
aliguori76655d62009-03-06 20:27:37 +00001604
aliguori28a76be2009-03-06 20:27:40 +00001605 if (!match) {
1606 monitor_printf(mon, "acl: missing match parameter\n");
1607 return;
1608 }
aliguori76655d62009-03-06 20:27:37 +00001609
aliguori28a76be2009-03-06 20:27:40 +00001610 ret = qemu_acl_remove(acl, match);
1611 if (ret < 0)
1612 monitor_printf(mon, "acl: no matching acl entry\n");
1613 else
1614 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001615 } else {
aliguori28a76be2009-03-06 20:27:40 +00001616 monitor_printf(mon, "acl: unknown command '%s'\n", command);
aliguori76655d62009-03-06 20:27:37 +00001617 }
1618}
1619
blueswir1d2c639d2009-01-24 18:19:25 +00001620/* Please update qemu-doc.texi when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001621static const mon_cmd_t mon_cmds[] = {
1622 { "help|?", "s?", help_cmd,
bellard9dc39cb2004-03-14 21:38:27 +00001623 "[cmd]", "show the help" },
ths5fafdf22007-09-16 21:08:06 +00001624 { "commit", "s", do_commit,
bellard7954c732006-08-01 15:52:40 +00001625 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
bellard9307c4c2004-04-04 12:57:25 +00001626 { "info", "s?", do_info,
bellard9dc39cb2004-03-14 21:38:27 +00001627 "subcommand", "show various information about the system state" },
bellard9307c4c2004-04-04 12:57:25 +00001628 { "q|quit", "", do_quit,
bellard9dc39cb2004-03-14 21:38:27 +00001629 "", "quit the emulator" },
bellard81d09122004-07-14 17:21:37 +00001630 { "eject", "-fB", do_eject,
thse5987522007-03-30 18:58:01 +00001631 "[-f] device", "eject a removable medium (use -f to force it)" },
aurel322ecea9b2008-06-18 22:10:01 +00001632 { "change", "BFs?", do_change,
1633 "device filename [format]", "change a removable medium, optional format" },
ths5fafdf22007-09-16 21:08:06 +00001634 { "screendump", "F", do_screen_dump,
bellard59a983b2004-03-17 23:17:16 +00001635 "filename", "save screen into PPM image 'filename'" },
balrog788228c2008-05-24 23:15:46 +00001636 { "logfile", "F", do_logfile,
pbrooke735b912007-06-30 13:53:24 +00001637 "filename", "output logs to 'filename'" },
bellard9307c4c2004-04-04 12:57:25 +00001638 { "log", "s", do_log,
ths5fafdf22007-09-16 21:08:06 +00001639 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
bellardfaea38e2006-08-05 21:31:00 +00001640 { "savevm", "s?", do_savevm,
ths5fafdf22007-09-16 21:08:06 +00001641 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
bellardfaea38e2006-08-05 21:31:00 +00001642 { "loadvm", "s", do_loadvm,
ths5fafdf22007-09-16 21:08:06 +00001643 "tag|id", "restore a VM snapshot from its tag or id" },
bellardfaea38e2006-08-05 21:31:00 +00001644 { "delvm", "s", do_delvm,
ths5fafdf22007-09-16 21:08:06 +00001645 "tag|id", "delete a VM snapshot from its tag or id" },
1646 { "stop", "", do_stop,
bellard9307c4c2004-04-04 12:57:25 +00001647 "", "stop emulation", },
ths5fafdf22007-09-16 21:08:06 +00001648 { "c|cont", "", do_cont,
bellard9307c4c2004-04-04 12:57:25 +00001649 "", "resume emulation", },
bellard67b915a2004-03-31 23:37:16 +00001650#ifdef CONFIG_GDBSTUB
ths5fafdf22007-09-16 21:08:06 +00001651 { "gdbserver", "s?", do_gdbserver,
bellard9307c4c2004-04-04 12:57:25 +00001652 "[port]", "start gdbserver session (default port=1234)", },
bellard67b915a2004-03-31 23:37:16 +00001653#endif
ths5fafdf22007-09-16 21:08:06 +00001654 { "x", "/l", do_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001655 "/fmt addr", "virtual memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001656 { "xp", "/l", do_physical_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001657 "/fmt addr", "physical memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001658 { "p|print", "/l", do_print,
bellard9307c4c2004-04-04 12:57:25 +00001659 "/fmt expr", "print expression value (use $reg for CPU register access)", },
ths5fafdf22007-09-16 21:08:06 +00001660 { "i", "/ii.", do_ioport_read,
bellard34405572004-06-08 00:55:58 +00001661 "/fmt addr", "I/O port read" },
1662
balrogc8256f92008-06-08 22:45:01 +00001663 { "sendkey", "si?", do_sendkey,
1664 "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 +00001665 { "system_reset", "", do_system_reset,
bellarde4f90822004-06-20 12:35:44 +00001666 "", "reset the system" },
ths5fafdf22007-09-16 21:08:06 +00001667 { "system_powerdown", "", do_system_powerdown,
bellard34751872005-07-02 14:31:34 +00001668 "", "send system power down event" },
ths5fafdf22007-09-16 21:08:06 +00001669 { "sum", "ii", do_sum,
bellarde4cf1ad2005-06-04 20:15:57 +00001670 "addr size", "compute the checksum of a memory region" },
bellarda594cfb2005-11-06 16:13:29 +00001671 { "usb_add", "s", do_usb_add,
1672 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1673 { "usb_del", "s", do_usb_del,
1674 "device", "remove USB device 'bus.addr'" },
ths5fafdf22007-09-16 21:08:06 +00001675 { "cpu", "i", do_cpu_set,
bellard6a00d602005-11-21 23:25:50 +00001676 "index", "set the default CPU" },
ths5fafdf22007-09-16 21:08:06 +00001677 { "mouse_move", "sss?", do_mouse_move,
bellard13224a82006-07-14 22:03:35 +00001678 "dx dy [dz]", "send mouse move events" },
ths5fafdf22007-09-16 21:08:06 +00001679 { "mouse_button", "i", do_mouse_button,
bellard13224a82006-07-14 22:03:35 +00001680 "state", "change mouse button state (1=L, 2=M, 4=R)" },
ths455204e2007-01-05 16:42:13 +00001681 { "mouse_set", "i", do_mouse_set,
1682 "index", "set which mouse device receives events" },
bellardec36b692006-07-16 18:57:03 +00001683#ifdef HAS_AUDIO
1684 { "wavcapture", "si?i?i?", do_wav_capture,
1685 "path [frequency bits channels]",
1686 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1687#endif
blueswir1d2c639d2009-01-24 18:19:25 +00001688 { "stopcapture", "i", do_stop_capture,
1689 "capture index", "stop capture" },
ths5fafdf22007-09-16 21:08:06 +00001690 { "memsave", "lis", do_memory_save,
bellardb371dc52007-01-03 15:20:39 +00001691 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
aurel32a8bdf7a2008-04-11 21:36:14 +00001692 { "pmemsave", "lis", do_physical_memory_save,
1693 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
aurel320ecdffb2008-05-04 20:11:34 +00001694 { "boot_set", "s", do_boot_set,
1695 "bootdevice", "define new values for the boot device list" },
aurel32dc1c0b72008-04-27 23:52:12 +00001696#if defined(TARGET_I386)
1697 { "nmi", "i", do_inject_nmi,
1698 "cpu", "inject an NMI on the given CPU", },
1699#endif
aliguori5bb79102008-10-13 03:12:02 +00001700 { "migrate", "-ds", do_migrate,
1701 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1702 { "migrate_cancel", "", do_migrate_cancel,
1703 "", "cancel the current VM migration" },
1704 { "migrate_set_speed", "s", do_migrate_set_speed,
1705 "value", "set maximum speed (in bytes) for migrations" },
aliguori6f338c32009-02-11 15:21:54 +00001706#if defined(TARGET_I386)
1707 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1708 "[file=file][,if=type][,bus=n]\n"
1709 "[,unit=m][,media=d][index=i]\n"
1710 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1711 "[snapshot=on|off][,cache=on|off]",
1712 "add drive to PCI storage controller" },
1713 { "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" },
1714 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1715 { "host_net_add", "ss", net_host_device_add,
1716 "[tap,user,socket,vde] options", "add host VLAN client" },
1717 { "host_net_remove", "is", net_host_device_remove,
1718 "vlan_id name", "remove host VLAN client" },
1719#endif
aliguoridf751fa2008-12-04 20:19:35 +00001720 { "balloon", "i", do_balloon,
1721 "target", "request VM to change it's memory allocation (in MB)" },
aliguorif029bd92009-02-11 15:19:16 +00001722 { "set_link", "ss", do_set_link,
1723 "name [up|down]", "change the link status of a network adapter" },
aliguori76655d62009-03-06 20:27:37 +00001724 { "acl", "sss?i?", do_acl, "<command> <aclname> [<match>] [<index>]\n",
1725 "acl show vnc.username\n"
1726 "acl policy vnc.username deny\n"
1727 "acl allow vnc.username fred\n"
1728 "acl deny vnc.username bob\n"
1729 "acl reset vnc.username\n" },
ths5fafdf22007-09-16 21:08:06 +00001730 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001731};
1732
blueswir1d2c639d2009-01-24 18:19:25 +00001733/* Please update qemu-doc.texi when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001734static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001735 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001736 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001737 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001738 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001739 { "chardev", "", qemu_chr_info,
1740 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001741 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001742 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001743 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001744 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001745 { "registers", "", do_info_registers,
1746 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001747 { "cpus", "", do_info_cpus,
1748 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001749 { "history", "", do_info_history,
1750 "", "show the command line history", },
bellard4a0fb712004-05-21 11:39:07 +00001751 { "irq", "", irq_info,
1752 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001753 { "pic", "", pic_info,
1754 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001755 { "pci", "", pci_info,
1756 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001757#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001758 { "tlb", "", tlb_info,
1759 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001760#endif
1761#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001762 { "mem", "", mem_info,
1763 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001764 { "hpet", "", do_info_hpet,
1765 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001766#endif
bellarde3db7222005-01-26 22:00:47 +00001767 { "jit", "", do_info_jit,
1768 "", "show dynamic compiler info", },
bellard0f4c6412005-07-24 18:10:56 +00001769 { "kqemu", "", do_info_kqemu,
blueswir1d2c639d2009-01-24 18:19:25 +00001770 "", "show KQEMU information", },
aliguori7ba1e612008-11-05 16:04:33 +00001771 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001772 "", "show KVM information", },
bellarda594cfb2005-11-06 16:13:29 +00001773 { "usb", "", usb_info,
1774 "", "show guest USB devices", },
1775 { "usbhost", "", usb_host_info,
1776 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001777 { "profile", "", do_info_profile,
1778 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001779 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001780 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001781 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001782 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001783 { "status", "", do_info_status,
1784 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001785 { "pcmcia", "", pcmcia_info,
1786 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001787 { "mice", "", do_info_mice,
1788 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001789 { "vnc", "", do_info_vnc,
1790 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001791 { "name", "", do_info_name,
1792 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001793 { "uuid", "", do_info_uuid,
1794 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001795#if defined(TARGET_PPC)
1796 { "cpustats", "", do_info_cpu_stats,
1797 "", "show CPU statistics", },
1798#endif
blueswir131a60e22007-10-26 18:42:59 +00001799#if defined(CONFIG_SLIRP)
1800 { "slirp", "", do_info_slirp,
1801 "", "show SLIRP statistics", },
1802#endif
aliguori5bb79102008-10-13 03:12:02 +00001803 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001804 { "balloon", "", do_info_balloon,
1805 "", "show balloon information" },
bellard9dc39cb2004-03-14 21:38:27 +00001806 { NULL, NULL, },
1807};
1808
bellard9307c4c2004-04-04 12:57:25 +00001809/*******************************************************************/
1810
1811static const char *pch;
1812static jmp_buf expr_env;
1813
bellard92a31b12005-02-10 22:00:52 +00001814#define MD_TLONG 0
1815#define MD_I32 1
1816
bellard9307c4c2004-04-04 12:57:25 +00001817typedef struct MonitorDef {
1818 const char *name;
1819 int offset;
blueswir18662d652008-10-02 18:32:44 +00001820 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001821 int type;
bellard9307c4c2004-04-04 12:57:25 +00001822} MonitorDef;
1823
bellard57206fd2004-04-25 18:54:52 +00001824#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001825static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001826{
bellard6a00d602005-11-21 23:25:50 +00001827 CPUState *env = mon_get_cpu();
1828 if (!env)
1829 return 0;
1830 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001831}
1832#endif
1833
bellarda541f292004-04-12 20:39:29 +00001834#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001835static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001836{
bellard6a00d602005-11-21 23:25:50 +00001837 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001838 unsigned int u;
1839 int i;
1840
bellard6a00d602005-11-21 23:25:50 +00001841 if (!env)
1842 return 0;
1843
bellarda541f292004-04-12 20:39:29 +00001844 u = 0;
1845 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00001846 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001847
1848 return u;
1849}
1850
blueswir18662d652008-10-02 18:32:44 +00001851static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001852{
bellard6a00d602005-11-21 23:25:50 +00001853 CPUState *env = mon_get_cpu();
1854 if (!env)
1855 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001856 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001857}
1858
blueswir18662d652008-10-02 18:32:44 +00001859static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001860{
bellard6a00d602005-11-21 23:25:50 +00001861 CPUState *env = mon_get_cpu();
1862 if (!env)
1863 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001864 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001865}
bellard9fddaa02004-05-21 12:59:32 +00001866
blueswir18662d652008-10-02 18:32:44 +00001867static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001868{
bellard6a00d602005-11-21 23:25:50 +00001869 CPUState *env = mon_get_cpu();
1870 if (!env)
1871 return 0;
1872 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001873}
1874
blueswir18662d652008-10-02 18:32:44 +00001875static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001876{
bellard6a00d602005-11-21 23:25:50 +00001877 CPUState *env = mon_get_cpu();
1878 if (!env)
1879 return 0;
1880 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001881}
1882
blueswir18662d652008-10-02 18:32:44 +00001883static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001884{
bellard6a00d602005-11-21 23:25:50 +00001885 CPUState *env = mon_get_cpu();
1886 if (!env)
1887 return 0;
1888 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001889}
bellarda541f292004-04-12 20:39:29 +00001890#endif
1891
bellarde95c8d52004-09-30 22:22:08 +00001892#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001893#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001894static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001895{
bellard6a00d602005-11-21 23:25:50 +00001896 CPUState *env = mon_get_cpu();
1897 if (!env)
1898 return 0;
1899 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001900}
bellard7b936c02005-10-30 17:05:13 +00001901#endif
bellarde95c8d52004-09-30 22:22:08 +00001902
blueswir18662d652008-10-02 18:32:44 +00001903static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001904{
bellard6a00d602005-11-21 23:25:50 +00001905 CPUState *env = mon_get_cpu();
1906 if (!env)
1907 return 0;
1908 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001909}
1910#endif
1911
blueswir18662d652008-10-02 18:32:44 +00001912static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001913#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001914
1915#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001916 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001917 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001918 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001919
bellard9307c4c2004-04-04 12:57:25 +00001920 { "eax", offsetof(CPUState, regs[0]) },
1921 { "ecx", offsetof(CPUState, regs[1]) },
1922 { "edx", offsetof(CPUState, regs[2]) },
1923 { "ebx", offsetof(CPUState, regs[3]) },
1924 { "esp|sp", offsetof(CPUState, regs[4]) },
1925 { "ebp|fp", offsetof(CPUState, regs[5]) },
1926 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001927 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001928#ifdef TARGET_X86_64
1929 { "r8", offsetof(CPUState, regs[8]) },
1930 { "r9", offsetof(CPUState, regs[9]) },
1931 { "r10", offsetof(CPUState, regs[10]) },
1932 { "r11", offsetof(CPUState, regs[11]) },
1933 { "r12", offsetof(CPUState, regs[12]) },
1934 { "r13", offsetof(CPUState, regs[13]) },
1935 { "r14", offsetof(CPUState, regs[14]) },
1936 { "r15", offsetof(CPUState, regs[15]) },
1937#endif
bellard9307c4c2004-04-04 12:57:25 +00001938 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001939 { "eip", offsetof(CPUState, eip) },
1940 SEG("cs", R_CS)
1941 SEG("ds", R_DS)
1942 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001943 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001944 SEG("fs", R_FS)
1945 SEG("gs", R_GS)
1946 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00001947#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00001948 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00001949 { "r0", offsetof(CPUState, gpr[0]) },
1950 { "r1", offsetof(CPUState, gpr[1]) },
1951 { "r2", offsetof(CPUState, gpr[2]) },
1952 { "r3", offsetof(CPUState, gpr[3]) },
1953 { "r4", offsetof(CPUState, gpr[4]) },
1954 { "r5", offsetof(CPUState, gpr[5]) },
1955 { "r6", offsetof(CPUState, gpr[6]) },
1956 { "r7", offsetof(CPUState, gpr[7]) },
1957 { "r8", offsetof(CPUState, gpr[8]) },
1958 { "r9", offsetof(CPUState, gpr[9]) },
1959 { "r10", offsetof(CPUState, gpr[10]) },
1960 { "r11", offsetof(CPUState, gpr[11]) },
1961 { "r12", offsetof(CPUState, gpr[12]) },
1962 { "r13", offsetof(CPUState, gpr[13]) },
1963 { "r14", offsetof(CPUState, gpr[14]) },
1964 { "r15", offsetof(CPUState, gpr[15]) },
1965 { "r16", offsetof(CPUState, gpr[16]) },
1966 { "r17", offsetof(CPUState, gpr[17]) },
1967 { "r18", offsetof(CPUState, gpr[18]) },
1968 { "r19", offsetof(CPUState, gpr[19]) },
1969 { "r20", offsetof(CPUState, gpr[20]) },
1970 { "r21", offsetof(CPUState, gpr[21]) },
1971 { "r22", offsetof(CPUState, gpr[22]) },
1972 { "r23", offsetof(CPUState, gpr[23]) },
1973 { "r24", offsetof(CPUState, gpr[24]) },
1974 { "r25", offsetof(CPUState, gpr[25]) },
1975 { "r26", offsetof(CPUState, gpr[26]) },
1976 { "r27", offsetof(CPUState, gpr[27]) },
1977 { "r28", offsetof(CPUState, gpr[28]) },
1978 { "r29", offsetof(CPUState, gpr[29]) },
1979 { "r30", offsetof(CPUState, gpr[30]) },
1980 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00001981 /* Floating point registers */
1982 { "f0", offsetof(CPUState, fpr[0]) },
1983 { "f1", offsetof(CPUState, fpr[1]) },
1984 { "f2", offsetof(CPUState, fpr[2]) },
1985 { "f3", offsetof(CPUState, fpr[3]) },
1986 { "f4", offsetof(CPUState, fpr[4]) },
1987 { "f5", offsetof(CPUState, fpr[5]) },
1988 { "f6", offsetof(CPUState, fpr[6]) },
1989 { "f7", offsetof(CPUState, fpr[7]) },
1990 { "f8", offsetof(CPUState, fpr[8]) },
1991 { "f9", offsetof(CPUState, fpr[9]) },
1992 { "f10", offsetof(CPUState, fpr[10]) },
1993 { "f11", offsetof(CPUState, fpr[11]) },
1994 { "f12", offsetof(CPUState, fpr[12]) },
1995 { "f13", offsetof(CPUState, fpr[13]) },
1996 { "f14", offsetof(CPUState, fpr[14]) },
1997 { "f15", offsetof(CPUState, fpr[15]) },
1998 { "f16", offsetof(CPUState, fpr[16]) },
1999 { "f17", offsetof(CPUState, fpr[17]) },
2000 { "f18", offsetof(CPUState, fpr[18]) },
2001 { "f19", offsetof(CPUState, fpr[19]) },
2002 { "f20", offsetof(CPUState, fpr[20]) },
2003 { "f21", offsetof(CPUState, fpr[21]) },
2004 { "f22", offsetof(CPUState, fpr[22]) },
2005 { "f23", offsetof(CPUState, fpr[23]) },
2006 { "f24", offsetof(CPUState, fpr[24]) },
2007 { "f25", offsetof(CPUState, fpr[25]) },
2008 { "f26", offsetof(CPUState, fpr[26]) },
2009 { "f27", offsetof(CPUState, fpr[27]) },
2010 { "f28", offsetof(CPUState, fpr[28]) },
2011 { "f29", offsetof(CPUState, fpr[29]) },
2012 { "f30", offsetof(CPUState, fpr[30]) },
2013 { "f31", offsetof(CPUState, fpr[31]) },
2014 { "fpscr", offsetof(CPUState, fpscr) },
2015 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002016 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002017 { "lr", offsetof(CPUState, lr) },
2018 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002019 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002020 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002021 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002022 { "msr", 0, &monitor_get_msr, },
2023 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002024 { "tbu", 0, &monitor_get_tbu, },
2025 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002026#if defined(TARGET_PPC64)
2027 /* Address space register */
2028 { "asr", offsetof(CPUState, asr) },
2029#endif
2030 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002031 { "sdr1", offsetof(CPUState, sdr1) },
2032 { "sr0", offsetof(CPUState, sr[0]) },
2033 { "sr1", offsetof(CPUState, sr[1]) },
2034 { "sr2", offsetof(CPUState, sr[2]) },
2035 { "sr3", offsetof(CPUState, sr[3]) },
2036 { "sr4", offsetof(CPUState, sr[4]) },
2037 { "sr5", offsetof(CPUState, sr[5]) },
2038 { "sr6", offsetof(CPUState, sr[6]) },
2039 { "sr7", offsetof(CPUState, sr[7]) },
2040 { "sr8", offsetof(CPUState, sr[8]) },
2041 { "sr9", offsetof(CPUState, sr[9]) },
2042 { "sr10", offsetof(CPUState, sr[10]) },
2043 { "sr11", offsetof(CPUState, sr[11]) },
2044 { "sr12", offsetof(CPUState, sr[12]) },
2045 { "sr13", offsetof(CPUState, sr[13]) },
2046 { "sr14", offsetof(CPUState, sr[14]) },
2047 { "sr15", offsetof(CPUState, sr[15]) },
2048 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002049#elif defined(TARGET_SPARC)
2050 { "g0", offsetof(CPUState, gregs[0]) },
2051 { "g1", offsetof(CPUState, gregs[1]) },
2052 { "g2", offsetof(CPUState, gregs[2]) },
2053 { "g3", offsetof(CPUState, gregs[3]) },
2054 { "g4", offsetof(CPUState, gregs[4]) },
2055 { "g5", offsetof(CPUState, gregs[5]) },
2056 { "g6", offsetof(CPUState, gregs[6]) },
2057 { "g7", offsetof(CPUState, gregs[7]) },
2058 { "o0", 0, monitor_get_reg },
2059 { "o1", 1, monitor_get_reg },
2060 { "o2", 2, monitor_get_reg },
2061 { "o3", 3, monitor_get_reg },
2062 { "o4", 4, monitor_get_reg },
2063 { "o5", 5, monitor_get_reg },
2064 { "o6", 6, monitor_get_reg },
2065 { "o7", 7, monitor_get_reg },
2066 { "l0", 8, monitor_get_reg },
2067 { "l1", 9, monitor_get_reg },
2068 { "l2", 10, monitor_get_reg },
2069 { "l3", 11, monitor_get_reg },
2070 { "l4", 12, monitor_get_reg },
2071 { "l5", 13, monitor_get_reg },
2072 { "l6", 14, monitor_get_reg },
2073 { "l7", 15, monitor_get_reg },
2074 { "i0", 16, monitor_get_reg },
2075 { "i1", 17, monitor_get_reg },
2076 { "i2", 18, monitor_get_reg },
2077 { "i3", 19, monitor_get_reg },
2078 { "i4", 20, monitor_get_reg },
2079 { "i5", 21, monitor_get_reg },
2080 { "i6", 22, monitor_get_reg },
2081 { "i7", 23, monitor_get_reg },
2082 { "pc", offsetof(CPUState, pc) },
2083 { "npc", offsetof(CPUState, npc) },
2084 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002085#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002086 { "psr", 0, &monitor_get_psr, },
2087 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002088#endif
bellarde95c8d52004-09-30 22:22:08 +00002089 { "tbr", offsetof(CPUState, tbr) },
2090 { "fsr", offsetof(CPUState, fsr) },
2091 { "f0", offsetof(CPUState, fpr[0]) },
2092 { "f1", offsetof(CPUState, fpr[1]) },
2093 { "f2", offsetof(CPUState, fpr[2]) },
2094 { "f3", offsetof(CPUState, fpr[3]) },
2095 { "f4", offsetof(CPUState, fpr[4]) },
2096 { "f5", offsetof(CPUState, fpr[5]) },
2097 { "f6", offsetof(CPUState, fpr[6]) },
2098 { "f7", offsetof(CPUState, fpr[7]) },
2099 { "f8", offsetof(CPUState, fpr[8]) },
2100 { "f9", offsetof(CPUState, fpr[9]) },
2101 { "f10", offsetof(CPUState, fpr[10]) },
2102 { "f11", offsetof(CPUState, fpr[11]) },
2103 { "f12", offsetof(CPUState, fpr[12]) },
2104 { "f13", offsetof(CPUState, fpr[13]) },
2105 { "f14", offsetof(CPUState, fpr[14]) },
2106 { "f15", offsetof(CPUState, fpr[15]) },
2107 { "f16", offsetof(CPUState, fpr[16]) },
2108 { "f17", offsetof(CPUState, fpr[17]) },
2109 { "f18", offsetof(CPUState, fpr[18]) },
2110 { "f19", offsetof(CPUState, fpr[19]) },
2111 { "f20", offsetof(CPUState, fpr[20]) },
2112 { "f21", offsetof(CPUState, fpr[21]) },
2113 { "f22", offsetof(CPUState, fpr[22]) },
2114 { "f23", offsetof(CPUState, fpr[23]) },
2115 { "f24", offsetof(CPUState, fpr[24]) },
2116 { "f25", offsetof(CPUState, fpr[25]) },
2117 { "f26", offsetof(CPUState, fpr[26]) },
2118 { "f27", offsetof(CPUState, fpr[27]) },
2119 { "f28", offsetof(CPUState, fpr[28]) },
2120 { "f29", offsetof(CPUState, fpr[29]) },
2121 { "f30", offsetof(CPUState, fpr[30]) },
2122 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002123#ifdef TARGET_SPARC64
2124 { "f32", offsetof(CPUState, fpr[32]) },
2125 { "f34", offsetof(CPUState, fpr[34]) },
2126 { "f36", offsetof(CPUState, fpr[36]) },
2127 { "f38", offsetof(CPUState, fpr[38]) },
2128 { "f40", offsetof(CPUState, fpr[40]) },
2129 { "f42", offsetof(CPUState, fpr[42]) },
2130 { "f44", offsetof(CPUState, fpr[44]) },
2131 { "f46", offsetof(CPUState, fpr[46]) },
2132 { "f48", offsetof(CPUState, fpr[48]) },
2133 { "f50", offsetof(CPUState, fpr[50]) },
2134 { "f52", offsetof(CPUState, fpr[52]) },
2135 { "f54", offsetof(CPUState, fpr[54]) },
2136 { "f56", offsetof(CPUState, fpr[56]) },
2137 { "f58", offsetof(CPUState, fpr[58]) },
2138 { "f60", offsetof(CPUState, fpr[60]) },
2139 { "f62", offsetof(CPUState, fpr[62]) },
2140 { "asi", offsetof(CPUState, asi) },
2141 { "pstate", offsetof(CPUState, pstate) },
2142 { "cansave", offsetof(CPUState, cansave) },
2143 { "canrestore", offsetof(CPUState, canrestore) },
2144 { "otherwin", offsetof(CPUState, otherwin) },
2145 { "wstate", offsetof(CPUState, wstate) },
2146 { "cleanwin", offsetof(CPUState, cleanwin) },
2147 { "fprs", offsetof(CPUState, fprs) },
2148#endif
bellard9307c4c2004-04-04 12:57:25 +00002149#endif
2150 { NULL },
2151};
2152
aliguori376253e2009-03-05 23:01:23 +00002153static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002154{
aliguori376253e2009-03-05 23:01:23 +00002155 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002156 longjmp(expr_env, 1);
2157}
2158
bellard6a00d602005-11-21 23:25:50 +00002159/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002160static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002161{
blueswir18662d652008-10-02 18:32:44 +00002162 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002163 void *ptr;
2164
bellard9307c4c2004-04-04 12:57:25 +00002165 for(md = monitor_defs; md->name != NULL; md++) {
2166 if (compare_cmd(name, md->name)) {
2167 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002168 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002169 } else {
bellard6a00d602005-11-21 23:25:50 +00002170 CPUState *env = mon_get_cpu();
2171 if (!env)
2172 return -2;
2173 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002174 switch(md->type) {
2175 case MD_I32:
2176 *pval = *(int32_t *)ptr;
2177 break;
2178 case MD_TLONG:
2179 *pval = *(target_long *)ptr;
2180 break;
2181 default:
2182 *pval = 0;
2183 break;
2184 }
bellard9307c4c2004-04-04 12:57:25 +00002185 }
2186 return 0;
2187 }
2188 }
2189 return -1;
2190}
2191
2192static void next(void)
2193{
2194 if (pch != '\0') {
2195 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002196 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002197 pch++;
2198 }
2199}
2200
aliguori376253e2009-03-05 23:01:23 +00002201static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002202
aliguori376253e2009-03-05 23:01:23 +00002203static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002204{
blueswir1c2efc952007-09-25 17:28:42 +00002205 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002206 char *p;
bellard6a00d602005-11-21 23:25:50 +00002207 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002208
2209 switch(*pch) {
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_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002221 break;
2222 case '(':
2223 next();
aliguori376253e2009-03-05 23:01:23 +00002224 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002225 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002226 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002227 }
2228 next();
2229 break;
bellard81d09122004-07-14 17:21:37 +00002230 case '\'':
2231 pch++;
2232 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002233 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002234 n = *pch;
2235 pch++;
2236 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002237 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002238 next();
2239 break;
bellard9307c4c2004-04-04 12:57:25 +00002240 case '$':
2241 {
2242 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002243 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002244
bellard9307c4c2004-04-04 12:57:25 +00002245 pch++;
2246 q = buf;
2247 while ((*pch >= 'a' && *pch <= 'z') ||
2248 (*pch >= 'A' && *pch <= 'Z') ||
2249 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002250 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002251 if ((q - buf) < sizeof(buf) - 1)
2252 *q++ = *pch;
2253 pch++;
2254 }
blueswir1cd390082008-11-16 13:53:32 +00002255 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002256 pch++;
2257 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002258 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002259 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002260 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002261 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002262 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002263 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002264 }
2265 break;
2266 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002267 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002268 n = 0;
2269 break;
2270 default:
blueswir17743e582007-09-24 18:39:04 +00002271#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002272 n = strtoull(pch, &p, 0);
2273#else
bellard9307c4c2004-04-04 12:57:25 +00002274 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002275#endif
bellard9307c4c2004-04-04 12:57:25 +00002276 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002277 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002278 }
2279 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002280 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002281 pch++;
2282 break;
2283 }
2284 return n;
2285}
2286
2287
aliguori376253e2009-03-05 23:01:23 +00002288static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002289{
blueswir1c2efc952007-09-25 17:28:42 +00002290 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002291 int op;
ths3b46e622007-09-17 08:09:54 +00002292
aliguori376253e2009-03-05 23:01:23 +00002293 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002294 for(;;) {
2295 op = *pch;
2296 if (op != '*' && op != '/' && op != '%')
2297 break;
2298 next();
aliguori376253e2009-03-05 23:01:23 +00002299 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002300 switch(op) {
2301 default:
2302 case '*':
2303 val *= val2;
2304 break;
2305 case '/':
2306 case '%':
ths5fafdf22007-09-16 21:08:06 +00002307 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002308 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002309 if (op == '/')
2310 val /= val2;
2311 else
2312 val %= val2;
2313 break;
2314 }
2315 }
2316 return val;
2317}
2318
aliguori376253e2009-03-05 23:01:23 +00002319static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002320{
blueswir1c2efc952007-09-25 17:28:42 +00002321 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002322 int op;
bellard9307c4c2004-04-04 12:57:25 +00002323
aliguori376253e2009-03-05 23:01:23 +00002324 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002325 for(;;) {
2326 op = *pch;
2327 if (op != '&' && op != '|' && op != '^')
2328 break;
2329 next();
aliguori376253e2009-03-05 23:01:23 +00002330 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002331 switch(op) {
2332 default:
2333 case '&':
2334 val &= val2;
2335 break;
2336 case '|':
2337 val |= val2;
2338 break;
2339 case '^':
2340 val ^= val2;
2341 break;
2342 }
2343 }
2344 return val;
2345}
2346
aliguori376253e2009-03-05 23:01:23 +00002347static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002348{
blueswir1c2efc952007-09-25 17:28:42 +00002349 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002350 int op;
bellard9307c4c2004-04-04 12:57:25 +00002351
aliguori376253e2009-03-05 23:01:23 +00002352 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002353 for(;;) {
2354 op = *pch;
2355 if (op != '+' && op != '-')
2356 break;
2357 next();
aliguori376253e2009-03-05 23:01:23 +00002358 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002359 if (op == '+')
2360 val += val2;
2361 else
2362 val -= val2;
2363 }
2364 return val;
2365}
2366
aliguori376253e2009-03-05 23:01:23 +00002367static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002368{
2369 pch = *pp;
2370 if (setjmp(expr_env)) {
2371 *pp = pch;
2372 return -1;
2373 }
blueswir1cd390082008-11-16 13:53:32 +00002374 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002375 pch++;
aliguori376253e2009-03-05 23:01:23 +00002376 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002377 *pp = pch;
2378 return 0;
2379}
2380
2381static int get_str(char *buf, int buf_size, const char **pp)
2382{
2383 const char *p;
2384 char *q;
2385 int c;
2386
bellard81d09122004-07-14 17:21:37 +00002387 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002388 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002389 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002390 p++;
2391 if (*p == '\0') {
2392 fail:
bellard81d09122004-07-14 17:21:37 +00002393 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002394 *pp = p;
2395 return -1;
2396 }
bellard9307c4c2004-04-04 12:57:25 +00002397 if (*p == '\"') {
2398 p++;
2399 while (*p != '\0' && *p != '\"') {
2400 if (*p == '\\') {
2401 p++;
2402 c = *p++;
2403 switch(c) {
2404 case 'n':
2405 c = '\n';
2406 break;
2407 case 'r':
2408 c = '\r';
2409 break;
2410 case '\\':
2411 case '\'':
2412 case '\"':
2413 break;
2414 default:
2415 qemu_printf("unsupported escape code: '\\%c'\n", c);
2416 goto fail;
2417 }
2418 if ((q - buf) < buf_size - 1) {
2419 *q++ = c;
2420 }
2421 } else {
2422 if ((q - buf) < buf_size - 1) {
2423 *q++ = *p;
2424 }
2425 p++;
2426 }
2427 }
2428 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002429 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002430 goto fail;
2431 }
2432 p++;
2433 } else {
blueswir1cd390082008-11-16 13:53:32 +00002434 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002435 if ((q - buf) < buf_size - 1) {
2436 *q++ = *p;
2437 }
2438 p++;
2439 }
bellard9307c4c2004-04-04 12:57:25 +00002440 }
bellard81d09122004-07-14 17:21:37 +00002441 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002442 *pp = p;
2443 return 0;
2444}
2445
2446static int default_fmt_format = 'x';
2447static int default_fmt_size = 4;
2448
2449#define MAX_ARGS 16
2450
aliguori376253e2009-03-05 23:01:23 +00002451static void monitor_handle_command(Monitor *mon, const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002452{
2453 const char *p, *pstart, *typestr;
2454 char *q;
2455 int c, nb_args, len, i, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002456 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002457 char cmdname[256];
2458 char buf[1024];
2459 void *str_allocated[MAX_ARGS];
2460 void *args[MAX_ARGS];
aliguori376253e2009-03-05 23:01:23 +00002461 void (*handler_0)(Monitor *mon);
2462 void (*handler_1)(Monitor *mon, void *arg0);
2463 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2464 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2465 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2466 void *arg3);
2467 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2468 void *arg3, void *arg4);
2469 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2470 void *arg3, void *arg4, void *arg5);
2471 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2472 void *arg3, void *arg4, void *arg5, void *arg6);
bellard9dc39cb2004-03-14 21:38:27 +00002473
2474#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002475 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002476#endif
ths3b46e622007-09-17 08:09:54 +00002477
bellard9307c4c2004-04-04 12:57:25 +00002478 /* extract the command name */
bellard9dc39cb2004-03-14 21:38:27 +00002479 p = cmdline;
bellard9307c4c2004-04-04 12:57:25 +00002480 q = cmdname;
blueswir1cd390082008-11-16 13:53:32 +00002481 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002482 p++;
2483 if (*p == '\0')
bellard9dc39cb2004-03-14 21:38:27 +00002484 return;
bellard9307c4c2004-04-04 12:57:25 +00002485 pstart = p;
blueswir1cd390082008-11-16 13:53:32 +00002486 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002487 p++;
2488 len = p - pstart;
2489 if (len > sizeof(cmdname) - 1)
2490 len = sizeof(cmdname) - 1;
2491 memcpy(cmdname, pstart, len);
2492 cmdname[len] = '\0';
ths3b46e622007-09-17 08:09:54 +00002493
bellard9307c4c2004-04-04 12:57:25 +00002494 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002495 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002496 if (compare_cmd(cmdname, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +00002497 goto found;
2498 }
aliguori376253e2009-03-05 23:01:23 +00002499 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
bellard9dc39cb2004-03-14 21:38:27 +00002500 return;
2501 found:
bellard9307c4c2004-04-04 12:57:25 +00002502
2503 for(i = 0; i < MAX_ARGS; i++)
2504 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002505
bellard9307c4c2004-04-04 12:57:25 +00002506 /* parse the parameters */
2507 typestr = cmd->args_type;
2508 nb_args = 0;
2509 for(;;) {
2510 c = *typestr;
2511 if (c == '\0')
2512 break;
2513 typestr++;
2514 switch(c) {
2515 case 'F':
bellard81d09122004-07-14 17:21:37 +00002516 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002517 case 's':
2518 {
2519 int ret;
2520 char *str;
ths3b46e622007-09-17 08:09:54 +00002521
blueswir1cd390082008-11-16 13:53:32 +00002522 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002523 p++;
2524 if (*typestr == '?') {
2525 typestr++;
2526 if (*p == '\0') {
2527 /* no optional string: NULL argument */
2528 str = NULL;
2529 goto add_str;
2530 }
2531 }
2532 ret = get_str(buf, sizeof(buf), &p);
2533 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002534 switch(c) {
2535 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002536 monitor_printf(mon, "%s: filename expected\n",
2537 cmdname);
bellard81d09122004-07-14 17:21:37 +00002538 break;
2539 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002540 monitor_printf(mon, "%s: block device name expected\n",
2541 cmdname);
bellard81d09122004-07-14 17:21:37 +00002542 break;
2543 default:
aliguori376253e2009-03-05 23:01:23 +00002544 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002545 break;
2546 }
bellard9307c4c2004-04-04 12:57:25 +00002547 goto fail;
2548 }
2549 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002550 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002551 str_allocated[nb_args] = str;
2552 add_str:
2553 if (nb_args >= MAX_ARGS) {
2554 error_args:
aliguori376253e2009-03-05 23:01:23 +00002555 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002556 goto fail;
2557 }
2558 args[nb_args++] = str;
2559 }
2560 break;
2561 case '/':
2562 {
2563 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002564
blueswir1cd390082008-11-16 13:53:32 +00002565 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002566 p++;
2567 if (*p == '/') {
2568 /* format found */
2569 p++;
2570 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002571 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002572 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002573 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002574 count = count * 10 + (*p - '0');
2575 p++;
2576 }
2577 }
2578 size = -1;
2579 format = -1;
2580 for(;;) {
2581 switch(*p) {
2582 case 'o':
2583 case 'd':
2584 case 'u':
2585 case 'x':
2586 case 'i':
2587 case 'c':
2588 format = *p++;
2589 break;
2590 case 'b':
2591 size = 1;
2592 p++;
2593 break;
2594 case 'h':
2595 size = 2;
2596 p++;
2597 break;
2598 case 'w':
2599 size = 4;
2600 p++;
2601 break;
2602 case 'g':
2603 case 'L':
2604 size = 8;
2605 p++;
2606 break;
2607 default:
2608 goto next;
2609 }
2610 }
2611 next:
blueswir1cd390082008-11-16 13:53:32 +00002612 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002613 monitor_printf(mon, "invalid char in format: '%c'\n",
2614 *p);
bellard9307c4c2004-04-04 12:57:25 +00002615 goto fail;
2616 }
bellard9307c4c2004-04-04 12:57:25 +00002617 if (format < 0)
2618 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002619 if (format != 'i') {
2620 /* for 'i', not specifying a size gives -1 as size */
2621 if (size < 0)
2622 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002623 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002624 }
bellard9307c4c2004-04-04 12:57:25 +00002625 default_fmt_format = format;
2626 } else {
2627 count = 1;
2628 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002629 if (format != 'i') {
2630 size = default_fmt_size;
2631 } else {
2632 size = -1;
2633 }
bellard9307c4c2004-04-04 12:57:25 +00002634 }
2635 if (nb_args + 3 > MAX_ARGS)
2636 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002637 args[nb_args++] = (void*)(long)count;
2638 args[nb_args++] = (void*)(long)format;
2639 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002640 }
2641 break;
2642 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002643 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002644 {
blueswir1c2efc952007-09-25 17:28:42 +00002645 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002646
blueswir1cd390082008-11-16 13:53:32 +00002647 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002648 p++;
bellard34405572004-06-08 00:55:58 +00002649 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002650 if (*typestr == '?') {
2651 if (*p == '\0')
2652 has_arg = 0;
2653 else
2654 has_arg = 1;
2655 } else {
2656 if (*p == '.') {
2657 p++;
blueswir1cd390082008-11-16 13:53:32 +00002658 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002659 p++;
2660 has_arg = 1;
2661 } else {
2662 has_arg = 0;
2663 }
2664 }
bellard13224a82006-07-14 22:03:35 +00002665 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002666 if (nb_args >= MAX_ARGS)
2667 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002668 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002669 if (!has_arg) {
2670 if (nb_args >= MAX_ARGS)
2671 goto error_args;
2672 val = -1;
2673 goto add_num;
2674 }
2675 }
aliguori376253e2009-03-05 23:01:23 +00002676 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002677 goto fail;
2678 add_num:
bellard92a31b12005-02-10 22:00:52 +00002679 if (c == 'i') {
2680 if (nb_args >= MAX_ARGS)
2681 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002682 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002683 } else {
2684 if ((nb_args + 1) >= MAX_ARGS)
2685 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002686#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002687 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002688#else
2689 args[nb_args++] = (void *)0;
2690#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002691 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002692 }
bellard9307c4c2004-04-04 12:57:25 +00002693 }
2694 break;
2695 case '-':
2696 {
2697 int has_option;
2698 /* option */
ths3b46e622007-09-17 08:09:54 +00002699
bellard9307c4c2004-04-04 12:57:25 +00002700 c = *typestr++;
2701 if (c == '\0')
2702 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002703 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002704 p++;
2705 has_option = 0;
2706 if (*p == '-') {
2707 p++;
2708 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002709 monitor_printf(mon, "%s: unsupported option -%c\n",
2710 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002711 goto fail;
2712 }
2713 p++;
2714 has_option = 1;
2715 }
2716 if (nb_args >= MAX_ARGS)
2717 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002718 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002719 }
2720 break;
2721 default:
2722 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002723 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002724 goto fail;
2725 }
2726 }
2727 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002728 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002729 p++;
2730 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002731 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2732 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002733 goto fail;
2734 }
2735
2736 switch(nb_args) {
2737 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002738 handler_0 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002739 handler_0(mon);
bellard9307c4c2004-04-04 12:57:25 +00002740 break;
2741 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002742 handler_1 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002743 handler_1(mon, args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002744 break;
2745 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002746 handler_2 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002747 handler_2(mon, args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002748 break;
2749 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002750 handler_3 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002751 handler_3(mon, args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002752 break;
2753 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002754 handler_4 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002755 handler_4(mon, args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002756 break;
2757 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002758 handler_5 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002759 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002760 break;
bellard34405572004-06-08 00:55:58 +00002761 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002762 handler_6 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002763 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002764 break;
bellardec36b692006-07-16 18:57:03 +00002765 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002766 handler_7 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002767 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2768 args[6]);
bellardec36b692006-07-16 18:57:03 +00002769 break;
bellard9307c4c2004-04-04 12:57:25 +00002770 default:
aliguori376253e2009-03-05 23:01:23 +00002771 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
bellard9307c4c2004-04-04 12:57:25 +00002772 goto fail;
2773 }
2774 fail:
2775 for(i = 0; i < MAX_ARGS; i++)
2776 qemu_free(str_allocated[i]);
2777 return;
bellard9dc39cb2004-03-14 21:38:27 +00002778}
2779
bellard81d09122004-07-14 17:21:37 +00002780static void cmd_completion(const char *name, const char *list)
2781{
2782 const char *p, *pstart;
2783 char cmd[128];
2784 int len;
2785
2786 p = list;
2787 for(;;) {
2788 pstart = p;
2789 p = strchr(p, '|');
2790 if (!p)
2791 p = pstart + strlen(pstart);
2792 len = p - pstart;
2793 if (len > sizeof(cmd) - 2)
2794 len = sizeof(cmd) - 2;
2795 memcpy(cmd, pstart, len);
2796 cmd[len] = '\0';
2797 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002798 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002799 }
2800 if (*p == '\0')
2801 break;
2802 p++;
2803 }
2804}
2805
2806static void file_completion(const char *input)
2807{
2808 DIR *ffs;
2809 struct dirent *d;
2810 char path[1024];
2811 char file[1024], file_prefix[1024];
2812 int input_path_len;
2813 const char *p;
2814
ths5fafdf22007-09-16 21:08:06 +00002815 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002816 if (!p) {
2817 input_path_len = 0;
2818 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002819 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002820 } else {
2821 input_path_len = p - input + 1;
2822 memcpy(path, input, input_path_len);
2823 if (input_path_len > sizeof(path) - 1)
2824 input_path_len = sizeof(path) - 1;
2825 path[input_path_len] = '\0';
2826 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2827 }
2828#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002829 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2830 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002831#endif
2832 ffs = opendir(path);
2833 if (!ffs)
2834 return;
2835 for(;;) {
2836 struct stat sb;
2837 d = readdir(ffs);
2838 if (!d)
2839 break;
2840 if (strstart(d->d_name, file_prefix, NULL)) {
2841 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002842 if (input_path_len < sizeof(file))
2843 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2844 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002845 /* stat the file to find out if it's a directory.
2846 * In that case add a slash to speed up typing long paths
2847 */
2848 stat(file, &sb);
2849 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002850 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00002851 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002852 }
2853 }
2854 closedir(ffs);
2855}
2856
aliguori51de9762009-03-05 23:00:43 +00002857static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002858{
aliguori51de9762009-03-05 23:00:43 +00002859 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002860 const char *input = opaque;
2861
2862 if (input[0] == '\0' ||
2863 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00002864 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00002865 }
2866}
2867
2868/* NOTE: this parser is an approximate form of the real command parser */
2869static void parse_cmdline(const char *cmdline,
2870 int *pnb_args, char **args)
2871{
2872 const char *p;
2873 int nb_args, ret;
2874 char buf[1024];
2875
2876 p = cmdline;
2877 nb_args = 0;
2878 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002879 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002880 p++;
2881 if (*p == '\0')
2882 break;
2883 if (nb_args >= MAX_ARGS)
2884 break;
2885 ret = get_str(buf, sizeof(buf), &p);
2886 args[nb_args] = qemu_strdup(buf);
2887 nb_args++;
2888 if (ret < 0)
2889 break;
2890 }
2891 *pnb_args = nb_args;
2892}
2893
aliguori4c36ba32009-03-05 23:01:37 +00002894static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002895{
2896 const char *cmdname;
2897 char *args[MAX_ARGS];
2898 int nb_args, i, len;
2899 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00002900 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002901 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002902
2903 parse_cmdline(cmdline, &nb_args, args);
2904#ifdef DEBUG_COMPLETION
2905 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00002906 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00002907 }
2908#endif
2909
2910 /* if the line ends with a space, it means we want to complete the
2911 next arg */
2912 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002913 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002914 if (nb_args >= MAX_ARGS)
2915 return;
2916 args[nb_args++] = qemu_strdup("");
2917 }
2918 if (nb_args <= 1) {
2919 /* command completion */
2920 if (nb_args == 0)
2921 cmdname = "";
2922 else
2923 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00002924 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00002925 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002926 cmd_completion(cmdname, cmd->name);
2927 }
2928 } else {
2929 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002930 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002931 if (compare_cmd(args[0], cmd->name))
2932 goto found;
2933 }
2934 return;
2935 found:
2936 ptype = cmd->args_type;
2937 for(i = 0; i < nb_args - 2; i++) {
2938 if (*ptype != '\0') {
2939 ptype++;
2940 while (*ptype == '?')
2941 ptype++;
2942 }
2943 }
2944 str = args[nb_args - 1];
2945 switch(*ptype) {
2946 case 'F':
2947 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00002948 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00002949 file_completion(str);
2950 break;
2951 case 'B':
2952 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00002953 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00002954 bdrv_iterate(block_completion_it, (void *)str);
2955 break;
bellard7fe48482004-10-09 18:08:01 +00002956 case 's':
2957 /* XXX: more generic ? */
2958 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00002959 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00002960 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2961 cmd_completion(str, cmd->name);
2962 }
bellard64866c32006-05-07 18:03:31 +00002963 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00002964 char *sep = strrchr(str, '-');
2965 if (sep)
2966 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00002967 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00002968 for(key = key_defs; key->name != NULL; key++) {
2969 cmd_completion(str, key->name);
2970 }
bellard7fe48482004-10-09 18:08:01 +00002971 }
2972 break;
bellard81d09122004-07-14 17:21:37 +00002973 default:
2974 break;
2975 }
2976 }
2977 for(i = 0; i < nb_args; i++)
2978 qemu_free(args[i]);
2979}
2980
aliguori731b0362009-03-05 23:01:42 +00002981static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00002982{
aliguori731b0362009-03-05 23:01:42 +00002983 Monitor *mon = opaque;
2984
2985 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00002986}
2987
aliguori731b0362009-03-05 23:01:42 +00002988static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00002989{
aliguori731b0362009-03-05 23:01:42 +00002990 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00002991 int i;
aliguori376253e2009-03-05 23:01:23 +00002992
aliguori731b0362009-03-05 23:01:42 +00002993 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00002994
aliguoricde76ee2009-03-05 23:01:51 +00002995 if (cur_mon->rs) {
2996 for (i = 0; i < size; i++)
2997 readline_handle_byte(cur_mon->rs, buf[i]);
2998 } else {
2999 if (size == 0 || buf[size - 1] != 0)
3000 monitor_printf(cur_mon, "corrupted command\n");
3001 else
3002 monitor_handle_command(cur_mon, (char *)buf);
3003 }
aliguori731b0362009-03-05 23:01:42 +00003004
3005 cur_mon = old_mon;
3006}
aliguorid8f44602008-10-06 13:52:44 +00003007
aliguori376253e2009-03-05 23:01:23 +00003008static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003009{
aliguori731b0362009-03-05 23:01:42 +00003010 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003011 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003012 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003013}
3014
aliguoricde76ee2009-03-05 23:01:51 +00003015int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003016{
aliguoricde76ee2009-03-05 23:01:51 +00003017 if (!mon->rs)
3018 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003019 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003020 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003021}
3022
aliguori376253e2009-03-05 23:01:23 +00003023void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003024{
aliguoricde76ee2009-03-05 23:01:51 +00003025 if (!mon->rs)
3026 return;
aliguori731b0362009-03-05 23:01:42 +00003027 if (--mon->suspend_cnt == 0)
3028 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003029}
3030
aliguori731b0362009-03-05 23:01:42 +00003031static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003032{
aliguori376253e2009-03-05 23:01:23 +00003033 Monitor *mon = opaque;
3034
aliguori2724b182009-03-05 23:01:47 +00003035 switch (event) {
3036 case CHR_EVENT_MUX_IN:
3037 readline_restart(mon->rs);
3038 monitor_resume(mon);
3039 monitor_flush(mon);
3040 break;
ths86e94de2007-01-05 22:01:59 +00003041
aliguori2724b182009-03-05 23:01:47 +00003042 case CHR_EVENT_MUX_OUT:
3043 if (mon->suspend_cnt == 0)
3044 monitor_printf(mon, "\n");
3045 monitor_flush(mon);
3046 monitor_suspend(mon);
3047 break;
3048
3049 case CHR_EVENT_RESET:
3050 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3051 "information\n", QEMU_VERSION);
3052 if (mon->chr->focus == 0)
3053 readline_show_prompt(mon->rs);
3054 break;
3055 }
ths86e94de2007-01-05 22:01:59 +00003056}
3057
aliguori76655d62009-03-06 20:27:37 +00003058
3059/*
3060 * Local variables:
3061 * c-indent-level: 4
3062 * c-basic-offset: 4
3063 * tab-width: 8
3064 * End:
3065 */
3066
aliguori731b0362009-03-05 23:01:42 +00003067void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003068{
aliguori731b0362009-03-05 23:01:42 +00003069 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003070 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003071
3072 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003073 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003074 is_first_init = 0;
3075 }
aliguori87127162009-03-05 23:01:29 +00003076
3077 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003078
aliguori87127162009-03-05 23:01:29 +00003079 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003080 mon->flags = flags;
aliguori2724b182009-03-05 23:01:47 +00003081 if (mon->chr->focus != 0)
3082 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
aliguoricde76ee2009-03-05 23:01:51 +00003083 if (flags & MONITOR_USE_READLINE) {
3084 mon->rs = readline_init(mon, monitor_find_completion);
3085 monitor_read_command(mon, 0);
3086 }
aliguori87127162009-03-05 23:01:29 +00003087
aliguori731b0362009-03-05 23:01:42 +00003088 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3089 mon);
aliguori87127162009-03-05 23:01:29 +00003090
3091 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003092 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003093 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003094}
3095
aliguori376253e2009-03-05 23:01:23 +00003096static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003097{
aliguoribb5fc202009-03-05 23:01:15 +00003098 BlockDriverState *bs = opaque;
3099 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003100
aliguoribb5fc202009-03-05 23:01:15 +00003101 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003102 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003103 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003104 }
aliguori731b0362009-03-05 23:01:42 +00003105 if (mon->password_completion_cb)
3106 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003107
aliguori731b0362009-03-05 23:01:42 +00003108 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003109}
aliguoric0f4ce72009-03-05 23:01:01 +00003110
aliguori376253e2009-03-05 23:01:23 +00003111void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003112 BlockDriverCompletionFunc *completion_cb,
3113 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003114{
aliguoricde76ee2009-03-05 23:01:51 +00003115 int err;
3116
aliguoribb5fc202009-03-05 23:01:15 +00003117 if (!bdrv_key_required(bs)) {
3118 if (completion_cb)
3119 completion_cb(opaque, 0);
3120 return;
3121 }
aliguoric0f4ce72009-03-05 23:01:01 +00003122
aliguori376253e2009-03-05 23:01:23 +00003123 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3124 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003125
aliguori731b0362009-03-05 23:01:42 +00003126 mon->password_completion_cb = completion_cb;
3127 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003128
aliguoricde76ee2009-03-05 23:01:51 +00003129 err = monitor_read_password(mon, bdrv_password_cb, bs);
3130
3131 if (err && completion_cb)
3132 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003133}