blob: 1c74bf85e8e157adcb95cb6dc4d43082c4fa7dca [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{
pbrook4a19f1e2009-04-07 23:17:49 +0000276 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
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
aurel321b530a62009-04-05 20:08:59 +0000530static void do_singlestep(Monitor *mon, const char *option)
531{
532 if (!option || !strcmp(option, "on")) {
533 singlestep = 1;
534 } else if (!strcmp(option, "off")) {
535 singlestep = 0;
536 } else {
537 monitor_printf(mon, "unexpected option %s\n", option);
538 }
539}
540
aliguori376253e2009-03-05 23:01:23 +0000541static void do_stop(Monitor *mon)
bellard8a7ddc32004-03-31 19:00:16 +0000542{
543 vm_stop(EXCP_INTERRUPT);
544}
545
aliguoribb5fc202009-03-05 23:01:15 +0000546static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000547
aliguori376253e2009-03-05 23:01:23 +0000548struct bdrv_iterate_context {
549 Monitor *mon;
550 int err;
551};
aliguoric0f4ce72009-03-05 23:01:01 +0000552
aliguori376253e2009-03-05 23:01:23 +0000553static void do_cont(Monitor *mon)
554{
555 struct bdrv_iterate_context context = { mon, 0 };
556
557 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000558 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000559 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000560 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000561}
562
aliguoribb5fc202009-03-05 23:01:15 +0000563static void bdrv_key_cb(void *opaque, int err)
564{
aliguori376253e2009-03-05 23:01:23 +0000565 Monitor *mon = opaque;
566
aliguoribb5fc202009-03-05 23:01:15 +0000567 /* another key was set successfully, retry to continue */
568 if (!err)
aliguori376253e2009-03-05 23:01:23 +0000569 do_cont(mon);
aliguoribb5fc202009-03-05 23:01:15 +0000570}
571
572static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
573{
aliguori376253e2009-03-05 23:01:23 +0000574 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000575
aliguori376253e2009-03-05 23:01:23 +0000576 if (!context->err && bdrv_key_required(bs)) {
577 context->err = -EBUSY;
578 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
579 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000580 }
581}
582
bellard67b915a2004-03-31 23:37:16 +0000583#ifdef CONFIG_GDBSTUB
aliguori59030a82009-04-05 18:43:41 +0000584static void do_gdbserver(Monitor *mon, const char *device)
bellard8a7ddc32004-03-31 19:00:16 +0000585{
aliguori59030a82009-04-05 18:43:41 +0000586 if (!device)
587 device = "tcp::" DEFAULT_GDBSTUB_PORT;
588 if (gdbserver_start(device) < 0) {
589 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
590 device);
591 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000592 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000593 } else {
aliguori59030a82009-04-05 18:43:41 +0000594 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
595 device);
bellard8a7ddc32004-03-31 19:00:16 +0000596 }
597}
bellard67b915a2004-03-31 23:37:16 +0000598#endif
bellard8a7ddc32004-03-31 19:00:16 +0000599
aliguori376253e2009-03-05 23:01:23 +0000600static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000601{
aliguori376253e2009-03-05 23:01:23 +0000602 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000603 switch(c) {
604 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000605 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000606 break;
607 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000608 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000609 break;
610 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000611 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000612 break;
613 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000614 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000615 break;
616 default:
617 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000618 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000619 } else {
aliguori376253e2009-03-05 23:01:23 +0000620 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000621 }
622 break;
623 }
aliguori376253e2009-03-05 23:01:23 +0000624 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000625}
626
aliguori376253e2009-03-05 23:01:23 +0000627static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000628 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000629{
bellard6a00d602005-11-21 23:25:50 +0000630 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000631 int nb_per_line, l, line_size, i, max_digits, len;
632 uint8_t buf[16];
633 uint64_t v;
634
635 if (format == 'i') {
636 int flags;
637 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000638 env = mon_get_cpu();
639 if (!env && !is_physical)
640 return;
bellard9307c4c2004-04-04 12:57:25 +0000641#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000642 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000643 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000644 } else if (wsize == 4) {
645 flags = 0;
646 } else {
bellard6a15fd12006-04-12 21:07:07 +0000647 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000648 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000649 if (env) {
650#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000651 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000652 (env->segs[R_CS].flags & DESC_L_MASK))
653 flags = 2;
654 else
655#endif
656 if (!(env->segs[R_CS].flags & DESC_B_MASK))
657 flags = 1;
658 }
bellard4c27ba22004-04-25 18:05:08 +0000659 }
660#endif
aliguori376253e2009-03-05 23:01:23 +0000661 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000662 return;
663 }
664
665 len = wsize * count;
666 if (wsize == 1)
667 line_size = 8;
668 else
669 line_size = 16;
670 nb_per_line = line_size / wsize;
671 max_digits = 0;
672
673 switch(format) {
674 case 'o':
675 max_digits = (wsize * 8 + 2) / 3;
676 break;
677 default:
678 case 'x':
679 max_digits = (wsize * 8) / 4;
680 break;
681 case 'u':
682 case 'd':
683 max_digits = (wsize * 8 * 10 + 32) / 33;
684 break;
685 case 'c':
686 wsize = 1;
687 break;
688 }
689
690 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000691 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000692 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000693 else
aliguori376253e2009-03-05 23:01:23 +0000694 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000695 l = len;
696 if (l > line_size)
697 l = line_size;
698 if (is_physical) {
699 cpu_physical_memory_rw(addr, buf, l, 0);
700 } else {
bellard6a00d602005-11-21 23:25:50 +0000701 env = mon_get_cpu();
702 if (!env)
703 break;
aliguoric8f79b62008-08-18 14:00:20 +0000704 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000705 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000706 break;
707 }
bellard9307c4c2004-04-04 12:57:25 +0000708 }
ths5fafdf22007-09-16 21:08:06 +0000709 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000710 while (i < l) {
711 switch(wsize) {
712 default:
713 case 1:
714 v = ldub_raw(buf + i);
715 break;
716 case 2:
717 v = lduw_raw(buf + i);
718 break;
719 case 4:
bellard92a31b12005-02-10 22:00:52 +0000720 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000721 break;
722 case 8:
723 v = ldq_raw(buf + i);
724 break;
725 }
aliguori376253e2009-03-05 23:01:23 +0000726 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000727 switch(format) {
728 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000729 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000730 break;
731 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000732 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000733 break;
734 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000735 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000736 break;
737 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000738 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000739 break;
740 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000741 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000742 break;
743 }
744 i += wsize;
745 }
aliguori376253e2009-03-05 23:01:23 +0000746 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000747 addr += l;
748 len -= l;
749 }
750}
751
bellard92a31b12005-02-10 22:00:52 +0000752#if TARGET_LONG_BITS == 64
753#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
754#else
755#define GET_TLONG(h, l) (l)
756#endif
757
aliguori376253e2009-03-05 23:01:23 +0000758static void do_memory_dump(Monitor *mon, int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000759 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000760{
bellard92a31b12005-02-10 22:00:52 +0000761 target_long addr = GET_TLONG(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000762 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000763}
764
blueswir17743e582007-09-24 18:39:04 +0000765#if TARGET_PHYS_ADDR_BITS > 32
766#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
767#else
768#define GET_TPHYSADDR(h, l) (l)
769#endif
770
aliguori376253e2009-03-05 23:01:23 +0000771static void do_physical_memory_dump(Monitor *mon, int count, int format,
772 int size, uint32_t addrh, uint32_t addrl)
bellard92a31b12005-02-10 22:00:52 +0000773
bellard9307c4c2004-04-04 12:57:25 +0000774{
blueswir17743e582007-09-24 18:39:04 +0000775 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000776 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000777}
778
aliguori376253e2009-03-05 23:01:23 +0000779static void do_print(Monitor *mon, int count, int format, int size,
780 unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000781{
blueswir17743e582007-09-24 18:39:04 +0000782 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
783#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000784 switch(format) {
785 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000786 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000787 break;
788 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000789 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000790 break;
791 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000792 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000793 break;
794 default:
795 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000796 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000797 break;
798 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000799 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000800 break;
801 }
bellard92a31b12005-02-10 22:00:52 +0000802#else
803 switch(format) {
804 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000805 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000806 break;
807 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000808 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000809 break;
810 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000811 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000812 break;
813 default:
814 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000815 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000816 break;
817 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000818 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000819 break;
820 }
821#endif
aliguori376253e2009-03-05 23:01:23 +0000822 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000823}
824
aliguori376253e2009-03-05 23:01:23 +0000825static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000826 uint32_t size, const char *filename)
827{
828 FILE *f;
829 target_long addr = GET_TLONG(valh, vall);
830 uint32_t l;
831 CPUState *env;
832 uint8_t buf[1024];
833
834 env = mon_get_cpu();
835 if (!env)
836 return;
837
838 f = fopen(filename, "wb");
839 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000840 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000841 return;
842 }
843 while (size != 0) {
844 l = sizeof(buf);
845 if (l > size)
846 l = size;
847 cpu_memory_rw_debug(env, addr, buf, l, 0);
848 fwrite(buf, 1, l, f);
849 addr += l;
850 size -= l;
851 }
852 fclose(f);
853}
854
aliguori376253e2009-03-05 23:01:23 +0000855static void do_physical_memory_save(Monitor *mon, unsigned int valh,
856 unsigned int vall, uint32_t size,
857 const char *filename)
aurel32a8bdf7a2008-04-11 21:36:14 +0000858{
859 FILE *f;
860 uint32_t l;
861 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000862 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000863
864 f = fopen(filename, "wb");
865 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000866 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000867 return;
868 }
869 while (size != 0) {
870 l = sizeof(buf);
871 if (l > size)
872 l = size;
873 cpu_physical_memory_rw(addr, buf, l, 0);
874 fwrite(buf, 1, l, f);
875 fflush(f);
876 addr += l;
877 size -= l;
878 }
879 fclose(f);
880}
881
aliguori376253e2009-03-05 23:01:23 +0000882static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
bellarde4cf1ad2005-06-04 20:15:57 +0000883{
884 uint32_t addr;
885 uint8_t buf[1];
886 uint16_t sum;
887
888 sum = 0;
889 for(addr = start; addr < (start + size); addr++) {
890 cpu_physical_memory_rw(addr, buf, 1, 0);
891 /* BSD sum algorithm ('sum' Unix command) */
892 sum = (sum >> 1) | (sum << 15);
893 sum += buf[0];
894 }
aliguori376253e2009-03-05 23:01:23 +0000895 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000896}
897
bellarda3a91a32004-06-04 11:06:21 +0000898typedef struct {
899 int keycode;
900 const char *name;
901} KeyDef;
902
903static const KeyDef key_defs[] = {
904 { 0x2a, "shift" },
905 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000906
bellarda3a91a32004-06-04 11:06:21 +0000907 { 0x38, "alt" },
908 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000909 { 0x64, "altgr" },
910 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000911 { 0x1d, "ctrl" },
912 { 0x9d, "ctrl_r" },
913
914 { 0xdd, "menu" },
915
916 { 0x01, "esc" },
917
918 { 0x02, "1" },
919 { 0x03, "2" },
920 { 0x04, "3" },
921 { 0x05, "4" },
922 { 0x06, "5" },
923 { 0x07, "6" },
924 { 0x08, "7" },
925 { 0x09, "8" },
926 { 0x0a, "9" },
927 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000928 { 0x0c, "minus" },
929 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000930 { 0x0e, "backspace" },
931
932 { 0x0f, "tab" },
933 { 0x10, "q" },
934 { 0x11, "w" },
935 { 0x12, "e" },
936 { 0x13, "r" },
937 { 0x14, "t" },
938 { 0x15, "y" },
939 { 0x16, "u" },
940 { 0x17, "i" },
941 { 0x18, "o" },
942 { 0x19, "p" },
943
944 { 0x1c, "ret" },
945
946 { 0x1e, "a" },
947 { 0x1f, "s" },
948 { 0x20, "d" },
949 { 0x21, "f" },
950 { 0x22, "g" },
951 { 0x23, "h" },
952 { 0x24, "j" },
953 { 0x25, "k" },
954 { 0x26, "l" },
955
956 { 0x2c, "z" },
957 { 0x2d, "x" },
958 { 0x2e, "c" },
959 { 0x2f, "v" },
960 { 0x30, "b" },
961 { 0x31, "n" },
962 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000963 { 0x33, "comma" },
964 { 0x34, "dot" },
965 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000966
balrog4d3b6f62008-02-10 16:33:14 +0000967 { 0x37, "asterisk" },
968
bellarda3a91a32004-06-04 11:06:21 +0000969 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000970 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000971 { 0x3b, "f1" },
972 { 0x3c, "f2" },
973 { 0x3d, "f3" },
974 { 0x3e, "f4" },
975 { 0x3f, "f5" },
976 { 0x40, "f6" },
977 { 0x41, "f7" },
978 { 0x42, "f8" },
979 { 0x43, "f9" },
980 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +0000981 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000982 { 0x46, "scroll_lock" },
983
bellard64866c32006-05-07 18:03:31 +0000984 { 0xb5, "kp_divide" },
985 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +0000986 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +0000987 { 0x4e, "kp_add" },
988 { 0x9c, "kp_enter" },
989 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +0000990 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +0000991
992 { 0x52, "kp_0" },
993 { 0x4f, "kp_1" },
994 { 0x50, "kp_2" },
995 { 0x51, "kp_3" },
996 { 0x4b, "kp_4" },
997 { 0x4c, "kp_5" },
998 { 0x4d, "kp_6" },
999 { 0x47, "kp_7" },
1000 { 0x48, "kp_8" },
1001 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001002
bellarda3a91a32004-06-04 11:06:21 +00001003 { 0x56, "<" },
1004
1005 { 0x57, "f11" },
1006 { 0x58, "f12" },
1007
1008 { 0xb7, "print" },
1009
1010 { 0xc7, "home" },
1011 { 0xc9, "pgup" },
1012 { 0xd1, "pgdn" },
1013 { 0xcf, "end" },
1014
1015 { 0xcb, "left" },
1016 { 0xc8, "up" },
1017 { 0xd0, "down" },
1018 { 0xcd, "right" },
1019
1020 { 0xd2, "insert" },
1021 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001022#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1023 { 0xf0, "stop" },
1024 { 0xf1, "again" },
1025 { 0xf2, "props" },
1026 { 0xf3, "undo" },
1027 { 0xf4, "front" },
1028 { 0xf5, "copy" },
1029 { 0xf6, "open" },
1030 { 0xf7, "paste" },
1031 { 0xf8, "find" },
1032 { 0xf9, "cut" },
1033 { 0xfa, "lf" },
1034 { 0xfb, "help" },
1035 { 0xfc, "meta_l" },
1036 { 0xfd, "meta_r" },
1037 { 0xfe, "compose" },
1038#endif
bellarda3a91a32004-06-04 11:06:21 +00001039 { 0, NULL },
1040};
1041
1042static int get_keycode(const char *key)
1043{
1044 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001045 char *endp;
1046 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001047
1048 for(p = key_defs; p->name != NULL; p++) {
1049 if (!strcmp(key, p->name))
1050 return p->keycode;
1051 }
bellard64866c32006-05-07 18:03:31 +00001052 if (strstart(key, "0x", NULL)) {
1053 ret = strtoul(key, &endp, 0);
1054 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1055 return ret;
1056 }
bellarda3a91a32004-06-04 11:06:21 +00001057 return -1;
1058}
1059
balrogc8256f92008-06-08 22:45:01 +00001060#define MAX_KEYCODES 16
1061static uint8_t keycodes[MAX_KEYCODES];
1062static int nb_pending_keycodes;
1063static QEMUTimer *key_timer;
1064
1065static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001066{
balrogc8256f92008-06-08 22:45:01 +00001067 int keycode;
1068
1069 while (nb_pending_keycodes > 0) {
1070 nb_pending_keycodes--;
1071 keycode = keycodes[nb_pending_keycodes];
1072 if (keycode & 0x80)
1073 kbd_put_keycode(0xe0);
1074 kbd_put_keycode(keycode | 0x80);
1075 }
1076}
1077
aliguori376253e2009-03-05 23:01:23 +00001078static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1079 int hold_time)
balrogc8256f92008-06-08 22:45:01 +00001080{
balrog3401c0d2008-06-04 10:05:59 +00001081 char keyname_buf[16];
1082 char *separator;
1083 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +00001084
balrogc8256f92008-06-08 22:45:01 +00001085 if (nb_pending_keycodes > 0) {
1086 qemu_del_timer(key_timer);
1087 release_keys(NULL);
1088 }
1089 if (!has_hold_time)
1090 hold_time = 100;
1091 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001092 while (1) {
1093 separator = strchr(string, '-');
1094 keyname_len = separator ? separator - string : strlen(string);
1095 if (keyname_len > 0) {
1096 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1097 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001098 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001099 return;
bellarda3a91a32004-06-04 11:06:21 +00001100 }
balrogc8256f92008-06-08 22:45:01 +00001101 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001102 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001103 return;
1104 }
1105 keyname_buf[keyname_len] = 0;
1106 keycode = get_keycode(keyname_buf);
1107 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001108 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001109 return;
1110 }
balrogc8256f92008-06-08 22:45:01 +00001111 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001112 }
balrog3401c0d2008-06-04 10:05:59 +00001113 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001114 break;
balrog3401c0d2008-06-04 10:05:59 +00001115 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001116 }
balrogc8256f92008-06-08 22:45:01 +00001117 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001118 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001119 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001120 keycode = keycodes[i];
1121 if (keycode & 0x80)
1122 kbd_put_keycode(0xe0);
1123 kbd_put_keycode(keycode & 0x7f);
1124 }
balrogc8256f92008-06-08 22:45:01 +00001125 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001126 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1127 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001128}
1129
bellard13224a82006-07-14 22:03:35 +00001130static int mouse_button_state;
1131
aliguori376253e2009-03-05 23:01:23 +00001132static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001133 const char *dz_str)
1134{
1135 int dx, dy, dz;
1136 dx = strtol(dx_str, NULL, 0);
1137 dy = strtol(dy_str, NULL, 0);
1138 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001139 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001140 dz = strtol(dz_str, NULL, 0);
1141 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1142}
1143
aliguori376253e2009-03-05 23:01:23 +00001144static void do_mouse_button(Monitor *mon, int button_state)
bellard13224a82006-07-14 22:03:35 +00001145{
1146 mouse_button_state = button_state;
1147 kbd_mouse_event(0, 0, 0, mouse_button_state);
1148}
1149
aliguori376253e2009-03-05 23:01:23 +00001150static void do_ioport_read(Monitor *mon, int count, int format, int size,
1151 int addr, int has_index, int index)
bellard34405572004-06-08 00:55:58 +00001152{
1153 uint32_t val;
1154 int suffix;
1155
1156 if (has_index) {
1157 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1158 addr++;
1159 }
1160 addr &= 0xffff;
1161
1162 switch(size) {
1163 default:
1164 case 1:
1165 val = cpu_inb(NULL, addr);
1166 suffix = 'b';
1167 break;
1168 case 2:
1169 val = cpu_inw(NULL, addr);
1170 suffix = 'w';
1171 break;
1172 case 4:
1173 val = cpu_inl(NULL, addr);
1174 suffix = 'l';
1175 break;
1176 }
aliguori376253e2009-03-05 23:01:23 +00001177 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1178 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001179}
bellarda3a91a32004-06-04 11:06:21 +00001180
blueswir13b4366d2008-06-20 16:25:06 +00001181/* boot_set handler */
1182static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1183static void *boot_opaque;
1184
1185void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1186{
1187 qemu_boot_set_handler = func;
1188 boot_opaque = opaque;
1189}
1190
aliguori376253e2009-03-05 23:01:23 +00001191static void do_boot_set(Monitor *mon, const char *bootdevice)
aurel320ecdffb2008-05-04 20:11:34 +00001192{
1193 int res;
1194
1195 if (qemu_boot_set_handler) {
blueswir13b4366d2008-06-20 16:25:06 +00001196 res = qemu_boot_set_handler(boot_opaque, bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001197 if (res == 0)
aliguori376253e2009-03-05 23:01:23 +00001198 monitor_printf(mon, "boot device list now set to %s\n",
1199 bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001200 else
aliguori376253e2009-03-05 23:01:23 +00001201 monitor_printf(mon, "setting boot device list failed with "
1202 "error %i\n", res);
aurel320ecdffb2008-05-04 20:11:34 +00001203 } else {
aliguori376253e2009-03-05 23:01:23 +00001204 monitor_printf(mon, "no function defined to set boot device list for "
1205 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001206 }
1207}
1208
aliguori376253e2009-03-05 23:01:23 +00001209static void do_system_reset(Monitor *mon)
bellarde4f90822004-06-20 12:35:44 +00001210{
1211 qemu_system_reset_request();
1212}
1213
aliguori376253e2009-03-05 23:01:23 +00001214static void do_system_powerdown(Monitor *mon)
bellard34751872005-07-02 14:31:34 +00001215{
1216 qemu_system_powerdown_request();
1217}
1218
bellardb86bda52004-09-18 19:32:46 +00001219#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001220static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001221{
aliguori376253e2009-03-05 23:01:23 +00001222 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1223 addr,
1224 pte & mask,
1225 pte & PG_GLOBAL_MASK ? 'G' : '-',
1226 pte & PG_PSE_MASK ? 'P' : '-',
1227 pte & PG_DIRTY_MASK ? 'D' : '-',
1228 pte & PG_ACCESSED_MASK ? 'A' : '-',
1229 pte & PG_PCD_MASK ? 'C' : '-',
1230 pte & PG_PWT_MASK ? 'T' : '-',
1231 pte & PG_USER_MASK ? 'U' : '-',
1232 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001233}
1234
aliguori376253e2009-03-05 23:01:23 +00001235static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001236{
bellard6a00d602005-11-21 23:25:50 +00001237 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001238 int l1, l2;
1239 uint32_t pgd, pde, pte;
1240
bellard6a00d602005-11-21 23:25:50 +00001241 env = mon_get_cpu();
1242 if (!env)
1243 return;
1244
bellardb86bda52004-09-18 19:32:46 +00001245 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001246 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001247 return;
1248 }
1249 pgd = env->cr[3] & ~0xfff;
1250 for(l1 = 0; l1 < 1024; l1++) {
1251 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1252 pde = le32_to_cpu(pde);
1253 if (pde & PG_PRESENT_MASK) {
1254 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001255 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001256 } else {
1257 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001258 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001259 (uint8_t *)&pte, 4);
1260 pte = le32_to_cpu(pte);
1261 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001262 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001263 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001264 ~0xfff);
1265 }
1266 }
1267 }
1268 }
1269 }
1270}
1271
aliguori376253e2009-03-05 23:01:23 +00001272static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001273 uint32_t end, int prot)
1274{
bellard9746b152004-11-11 18:30:24 +00001275 int prot1;
1276 prot1 = *plast_prot;
1277 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001278 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001279 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1280 *pstart, end, end - *pstart,
1281 prot1 & PG_USER_MASK ? 'u' : '-',
1282 'r',
1283 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001284 }
1285 if (prot != 0)
1286 *pstart = end;
1287 else
1288 *pstart = -1;
1289 *plast_prot = prot;
1290 }
1291}
1292
aliguori376253e2009-03-05 23:01:23 +00001293static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001294{
bellard6a00d602005-11-21 23:25:50 +00001295 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001296 int l1, l2, prot, last_prot;
1297 uint32_t pgd, pde, pte, start, end;
1298
bellard6a00d602005-11-21 23:25:50 +00001299 env = mon_get_cpu();
1300 if (!env)
1301 return;
1302
bellardb86bda52004-09-18 19:32:46 +00001303 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001304 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001305 return;
1306 }
1307 pgd = env->cr[3] & ~0xfff;
1308 last_prot = 0;
1309 start = -1;
1310 for(l1 = 0; l1 < 1024; l1++) {
1311 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1312 pde = le32_to_cpu(pde);
1313 end = l1 << 22;
1314 if (pde & PG_PRESENT_MASK) {
1315 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1316 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001317 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001318 } else {
1319 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001320 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001321 (uint8_t *)&pte, 4);
1322 pte = le32_to_cpu(pte);
1323 end = (l1 << 22) + (l2 << 12);
1324 if (pte & PG_PRESENT_MASK) {
1325 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1326 } else {
1327 prot = 0;
1328 }
aliguori376253e2009-03-05 23:01:23 +00001329 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001330 }
1331 }
1332 } else {
1333 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001334 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001335 }
1336 }
1337}
1338#endif
1339
aurel327c664e22009-03-03 06:12:22 +00001340#if defined(TARGET_SH4)
1341
aliguori376253e2009-03-05 23:01:23 +00001342static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001343{
aliguori376253e2009-03-05 23:01:23 +00001344 monitor_printf(mon, " tlb%i:\t"
1345 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1346 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1347 "dirty=%hhu writethrough=%hhu\n",
1348 idx,
1349 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1350 tlb->v, tlb->sh, tlb->c, tlb->pr,
1351 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001352}
1353
aliguori376253e2009-03-05 23:01:23 +00001354static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001355{
1356 CPUState *env = mon_get_cpu();
1357 int i;
1358
aliguori376253e2009-03-05 23:01:23 +00001359 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001360 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001361 print_tlb (mon, i, &env->itlb[i]);
1362 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001363 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001364 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001365}
1366
1367#endif
1368
aliguori376253e2009-03-05 23:01:23 +00001369static void do_info_kqemu(Monitor *mon)
bellard0f4c6412005-07-24 18:10:56 +00001370{
blueswir1640f42e2009-04-19 10:18:01 +00001371#ifdef CONFIG_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001372 CPUState *env;
bellard0f4c6412005-07-24 18:10:56 +00001373 int val;
1374 val = 0;
bellard6a00d602005-11-21 23:25:50 +00001375 env = mon_get_cpu();
1376 if (!env) {
aliguori376253e2009-03-05 23:01:23 +00001377 monitor_printf(mon, "No cpu initialized yet");
bellard6a00d602005-11-21 23:25:50 +00001378 return;
1379 }
1380 val = env->kqemu_enabled;
aliguori376253e2009-03-05 23:01:23 +00001381 monitor_printf(mon, "kqemu support: ");
bellard5f1ce942006-02-08 22:40:15 +00001382 switch(val) {
1383 default:
1384 case 0:
aliguori376253e2009-03-05 23:01:23 +00001385 monitor_printf(mon, "disabled\n");
bellard5f1ce942006-02-08 22:40:15 +00001386 break;
1387 case 1:
aliguori376253e2009-03-05 23:01:23 +00001388 monitor_printf(mon, "enabled for user code\n");
bellard5f1ce942006-02-08 22:40:15 +00001389 break;
1390 case 2:
aliguori376253e2009-03-05 23:01:23 +00001391 monitor_printf(mon, "enabled for user and kernel code\n");
bellard5f1ce942006-02-08 22:40:15 +00001392 break;
1393 }
bellard0f4c6412005-07-24 18:10:56 +00001394#else
aliguori376253e2009-03-05 23:01:23 +00001395 monitor_printf(mon, "kqemu support: not compiled\n");
bellard0f4c6412005-07-24 18:10:56 +00001396#endif
ths5fafdf22007-09-16 21:08:06 +00001397}
bellard0f4c6412005-07-24 18:10:56 +00001398
aliguori376253e2009-03-05 23:01:23 +00001399static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001400{
1401#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001402 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001403 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001404 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001405 else
aliguori376253e2009-03-05 23:01:23 +00001406 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001407#else
aliguori376253e2009-03-05 23:01:23 +00001408 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001409#endif
1410}
1411
bellard5f1ce942006-02-08 22:40:15 +00001412#ifdef CONFIG_PROFILER
1413
1414int64_t kqemu_time;
1415int64_t qemu_time;
1416int64_t kqemu_exec_count;
1417int64_t dev_time;
1418int64_t kqemu_ret_int_count;
1419int64_t kqemu_ret_excp_count;
1420int64_t kqemu_ret_intr_count;
1421
aliguori376253e2009-03-05 23:01:23 +00001422static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001423{
1424 int64_t total;
1425 total = qemu_time;
1426 if (total == 0)
1427 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001428 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1429 dev_time, dev_time / (double)ticks_per_sec);
1430 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1431 qemu_time, qemu_time / (double)ticks_per_sec);
1432 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1433 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1434 PRId64 "\n",
1435 kqemu_time, kqemu_time / (double)ticks_per_sec,
1436 kqemu_time / (double)total * 100.0,
1437 kqemu_exec_count,
1438 kqemu_ret_int_count,
1439 kqemu_ret_excp_count,
1440 kqemu_ret_intr_count);
bellard5f1ce942006-02-08 22:40:15 +00001441 qemu_time = 0;
1442 kqemu_time = 0;
1443 kqemu_exec_count = 0;
1444 dev_time = 0;
1445 kqemu_ret_int_count = 0;
1446 kqemu_ret_excp_count = 0;
1447 kqemu_ret_intr_count = 0;
blueswir1640f42e2009-04-19 10:18:01 +00001448#ifdef CONFIG_KQEMU
bellard5f1ce942006-02-08 22:40:15 +00001449 kqemu_record_dump();
1450#endif
1451}
1452#else
aliguori376253e2009-03-05 23:01:23 +00001453static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001454{
aliguori376253e2009-03-05 23:01:23 +00001455 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001456}
1457#endif
1458
bellardec36b692006-07-16 18:57:03 +00001459/* Capture support */
1460static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1461
aliguori376253e2009-03-05 23:01:23 +00001462static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001463{
1464 int i;
1465 CaptureState *s;
1466
1467 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001468 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001469 s->ops.info (s->opaque);
1470 }
1471}
1472
aliguori376253e2009-03-05 23:01:23 +00001473static void do_stop_capture(Monitor *mon, int n)
bellardec36b692006-07-16 18:57:03 +00001474{
1475 int i;
1476 CaptureState *s;
1477
1478 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1479 if (i == n) {
1480 s->ops.destroy (s->opaque);
1481 LIST_REMOVE (s, entries);
1482 qemu_free (s);
1483 return;
1484 }
1485 }
1486}
1487
1488#ifdef HAS_AUDIO
aliguori376253e2009-03-05 23:01:23 +00001489static void do_wav_capture(Monitor *mon, const char *path,
1490 int has_freq, int freq,
1491 int has_bits, int bits,
1492 int has_channels, int nchannels)
bellardec36b692006-07-16 18:57:03 +00001493{
1494 CaptureState *s;
1495
1496 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001497
1498 freq = has_freq ? freq : 44100;
1499 bits = has_bits ? bits : 16;
1500 nchannels = has_channels ? nchannels : 2;
1501
1502 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001503 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001504 qemu_free (s);
1505 }
1506 LIST_INSERT_HEAD (&capture_head, s, entries);
1507}
1508#endif
1509
aurel32dc1c0b72008-04-27 23:52:12 +00001510#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001511static void do_inject_nmi(Monitor *mon, int cpu_index)
aurel32dc1c0b72008-04-27 23:52:12 +00001512{
1513 CPUState *env;
1514
1515 for (env = first_cpu; env != NULL; env = env->next_cpu)
1516 if (env->cpu_index == cpu_index) {
1517 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1518 break;
1519 }
1520}
1521#endif
1522
aliguori376253e2009-03-05 23:01:23 +00001523static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001524{
aurel321b530a62009-04-05 20:08:59 +00001525 if (vm_running) {
1526 if (singlestep) {
1527 monitor_printf(mon, "VM status: running (single step mode)\n");
1528 } else {
1529 monitor_printf(mon, "VM status: running\n");
1530 }
1531 } else
aliguori376253e2009-03-05 23:01:23 +00001532 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001533}
1534
1535
aliguori376253e2009-03-05 23:01:23 +00001536static void do_balloon(Monitor *mon, int value)
aliguoridf751fa2008-12-04 20:19:35 +00001537{
1538 ram_addr_t target = value;
1539 qemu_balloon(target << 20);
1540}
1541
aliguori376253e2009-03-05 23:01:23 +00001542static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001543{
1544 ram_addr_t actual;
1545
1546 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001547 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001548 monitor_printf(mon, "Using KVM without synchronous MMU, "
1549 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001550 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001551 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001552 else
aliguori376253e2009-03-05 23:01:23 +00001553 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001554}
1555
aliguori76655d62009-03-06 20:27:37 +00001556static void do_acl(Monitor *mon,
1557 const char *command,
aliguori28a76be2009-03-06 20:27:40 +00001558 const char *aclname,
1559 const char *match,
1560 int has_index,
1561 int index)
aliguori76655d62009-03-06 20:27:37 +00001562{
1563 qemu_acl *acl;
1564
1565 acl = qemu_acl_find(aclname);
1566 if (!acl) {
aliguori28a76be2009-03-06 20:27:40 +00001567 monitor_printf(mon, "acl: unknown list '%s'\n", aclname);
1568 return;
aliguori76655d62009-03-06 20:27:37 +00001569 }
1570
1571 if (strcmp(command, "show") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001572 int i = 0;
1573 qemu_acl_entry *entry;
1574 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001575 acl->defaultDeny ? "deny" : "allow");
aliguori28a76be2009-03-06 20:27:40 +00001576 TAILQ_FOREACH(entry, &acl->entries, next) {
1577 i++;
1578 monitor_printf(mon, "%d: %s %s\n", i,
aliguori76655d62009-03-06 20:27:37 +00001579 entry->deny ? "deny" : "allow",
1580 entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001581 }
aliguori76655d62009-03-06 20:27:37 +00001582 } else if (strcmp(command, "reset") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001583 qemu_acl_reset(acl);
1584 monitor_printf(mon, "acl: removed all rules\n");
aliguori76655d62009-03-06 20:27:37 +00001585 } else if (strcmp(command, "policy") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001586 if (!match) {
1587 monitor_printf(mon, "acl: missing policy parameter\n");
1588 return;
1589 }
aliguori76655d62009-03-06 20:27:37 +00001590
aliguori28a76be2009-03-06 20:27:40 +00001591 if (strcmp(match, "allow") == 0) {
1592 acl->defaultDeny = 0;
1593 monitor_printf(mon, "acl: policy set to 'allow'\n");
1594 } else if (strcmp(match, "deny") == 0) {
1595 acl->defaultDeny = 1;
1596 monitor_printf(mon, "acl: policy set to 'deny'\n");
1597 } else {
1598 monitor_printf(mon, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match);
1599 }
aliguori76655d62009-03-06 20:27:37 +00001600 } else if ((strcmp(command, "allow") == 0) ||
aliguori28a76be2009-03-06 20:27:40 +00001601 (strcmp(command, "deny") == 0)) {
1602 int deny = strcmp(command, "deny") == 0 ? 1 : 0;
1603 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 if (has_index)
1611 ret = qemu_acl_insert(acl, deny, match, index);
1612 else
1613 ret = qemu_acl_append(acl, deny, match);
1614 if (ret < 0)
1615 monitor_printf(mon, "acl: unable to add acl entry\n");
1616 else
1617 monitor_printf(mon, "acl: added rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001618 } else if (strcmp(command, "remove") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001619 int ret;
aliguori76655d62009-03-06 20:27:37 +00001620
aliguori28a76be2009-03-06 20:27:40 +00001621 if (!match) {
1622 monitor_printf(mon, "acl: missing match parameter\n");
1623 return;
1624 }
aliguori76655d62009-03-06 20:27:37 +00001625
aliguori28a76be2009-03-06 20:27:40 +00001626 ret = qemu_acl_remove(acl, match);
1627 if (ret < 0)
1628 monitor_printf(mon, "acl: no matching acl entry\n");
1629 else
1630 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001631 } else {
aliguori28a76be2009-03-06 20:27:40 +00001632 monitor_printf(mon, "acl: unknown command '%s'\n", command);
aliguori76655d62009-03-06 20:27:37 +00001633 }
1634}
1635
blueswir1d2c639d2009-01-24 18:19:25 +00001636/* Please update qemu-doc.texi when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001637static const mon_cmd_t mon_cmds[] = {
1638 { "help|?", "s?", help_cmd,
bellard9dc39cb2004-03-14 21:38:27 +00001639 "[cmd]", "show the help" },
ths5fafdf22007-09-16 21:08:06 +00001640 { "commit", "s", do_commit,
bellard7954c732006-08-01 15:52:40 +00001641 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
bellard9307c4c2004-04-04 12:57:25 +00001642 { "info", "s?", do_info,
aliguoricd33fee2009-04-18 15:36:15 +00001643 "[subcommand]", "show various information about the system state" },
bellard9307c4c2004-04-04 12:57:25 +00001644 { "q|quit", "", do_quit,
bellard9dc39cb2004-03-14 21:38:27 +00001645 "", "quit the emulator" },
bellard81d09122004-07-14 17:21:37 +00001646 { "eject", "-fB", do_eject,
thse5987522007-03-30 18:58:01 +00001647 "[-f] device", "eject a removable medium (use -f to force it)" },
aurel322ecea9b2008-06-18 22:10:01 +00001648 { "change", "BFs?", do_change,
1649 "device filename [format]", "change a removable medium, optional format" },
ths5fafdf22007-09-16 21:08:06 +00001650 { "screendump", "F", do_screen_dump,
bellard59a983b2004-03-17 23:17:16 +00001651 "filename", "save screen into PPM image 'filename'" },
balrog788228c2008-05-24 23:15:46 +00001652 { "logfile", "F", do_logfile,
pbrooke735b912007-06-30 13:53:24 +00001653 "filename", "output logs to 'filename'" },
bellard9307c4c2004-04-04 12:57:25 +00001654 { "log", "s", do_log,
ths5fafdf22007-09-16 21:08:06 +00001655 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
bellardfaea38e2006-08-05 21:31:00 +00001656 { "savevm", "s?", do_savevm,
aliguoricd33fee2009-04-18 15:36:15 +00001657 "[tag|id]", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
bellardfaea38e2006-08-05 21:31:00 +00001658 { "loadvm", "s", do_loadvm,
ths5fafdf22007-09-16 21:08:06 +00001659 "tag|id", "restore a VM snapshot from its tag or id" },
bellardfaea38e2006-08-05 21:31:00 +00001660 { "delvm", "s", do_delvm,
ths5fafdf22007-09-16 21:08:06 +00001661 "tag|id", "delete a VM snapshot from its tag or id" },
aurel321b530a62009-04-05 20:08:59 +00001662 { "singlestep", "s?", do_singlestep,
1663 "[on|off]", "run emulation in singlestep mode or switch to normal mode", },
ths5fafdf22007-09-16 21:08:06 +00001664 { "stop", "", do_stop,
bellard9307c4c2004-04-04 12:57:25 +00001665 "", "stop emulation", },
ths5fafdf22007-09-16 21:08:06 +00001666 { "c|cont", "", do_cont,
bellard9307c4c2004-04-04 12:57:25 +00001667 "", "resume emulation", },
bellard67b915a2004-03-31 23:37:16 +00001668#ifdef CONFIG_GDBSTUB
ths5fafdf22007-09-16 21:08:06 +00001669 { "gdbserver", "s?", do_gdbserver,
aliguoricd33fee2009-04-18 15:36:15 +00001670 "[device]", "start gdbserver on given device (default 'tcp::1234'), stop with 'none'", },
bellard67b915a2004-03-31 23:37:16 +00001671#endif
ths5fafdf22007-09-16 21:08:06 +00001672 { "x", "/l", do_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001673 "/fmt addr", "virtual memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001674 { "xp", "/l", do_physical_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001675 "/fmt addr", "physical memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001676 { "p|print", "/l", do_print,
bellard9307c4c2004-04-04 12:57:25 +00001677 "/fmt expr", "print expression value (use $reg for CPU register access)", },
ths5fafdf22007-09-16 21:08:06 +00001678 { "i", "/ii.", do_ioport_read,
bellard34405572004-06-08 00:55:58 +00001679 "/fmt addr", "I/O port read" },
1680
balrogc8256f92008-06-08 22:45:01 +00001681 { "sendkey", "si?", do_sendkey,
1682 "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 +00001683 { "system_reset", "", do_system_reset,
bellarde4f90822004-06-20 12:35:44 +00001684 "", "reset the system" },
ths5fafdf22007-09-16 21:08:06 +00001685 { "system_powerdown", "", do_system_powerdown,
bellard34751872005-07-02 14:31:34 +00001686 "", "send system power down event" },
ths5fafdf22007-09-16 21:08:06 +00001687 { "sum", "ii", do_sum,
bellarde4cf1ad2005-06-04 20:15:57 +00001688 "addr size", "compute the checksum of a memory region" },
bellarda594cfb2005-11-06 16:13:29 +00001689 { "usb_add", "s", do_usb_add,
1690 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1691 { "usb_del", "s", do_usb_del,
1692 "device", "remove USB device 'bus.addr'" },
ths5fafdf22007-09-16 21:08:06 +00001693 { "cpu", "i", do_cpu_set,
bellard6a00d602005-11-21 23:25:50 +00001694 "index", "set the default CPU" },
ths5fafdf22007-09-16 21:08:06 +00001695 { "mouse_move", "sss?", do_mouse_move,
bellard13224a82006-07-14 22:03:35 +00001696 "dx dy [dz]", "send mouse move events" },
ths5fafdf22007-09-16 21:08:06 +00001697 { "mouse_button", "i", do_mouse_button,
bellard13224a82006-07-14 22:03:35 +00001698 "state", "change mouse button state (1=L, 2=M, 4=R)" },
ths455204e2007-01-05 16:42:13 +00001699 { "mouse_set", "i", do_mouse_set,
1700 "index", "set which mouse device receives events" },
bellardec36b692006-07-16 18:57:03 +00001701#ifdef HAS_AUDIO
1702 { "wavcapture", "si?i?i?", do_wav_capture,
aliguoricd33fee2009-04-18 15:36:15 +00001703 "path [frequency [bits [channels]]]",
bellardec36b692006-07-16 18:57:03 +00001704 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1705#endif
blueswir1d2c639d2009-01-24 18:19:25 +00001706 { "stopcapture", "i", do_stop_capture,
1707 "capture index", "stop capture" },
ths5fafdf22007-09-16 21:08:06 +00001708 { "memsave", "lis", do_memory_save,
bellardb371dc52007-01-03 15:20:39 +00001709 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
aurel32a8bdf7a2008-04-11 21:36:14 +00001710 { "pmemsave", "lis", do_physical_memory_save,
1711 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
aurel320ecdffb2008-05-04 20:11:34 +00001712 { "boot_set", "s", do_boot_set,
1713 "bootdevice", "define new values for the boot device list" },
aurel32dc1c0b72008-04-27 23:52:12 +00001714#if defined(TARGET_I386)
1715 { "nmi", "i", do_inject_nmi,
1716 "cpu", "inject an NMI on the given CPU", },
1717#endif
aliguori5bb79102008-10-13 03:12:02 +00001718 { "migrate", "-ds", do_migrate,
1719 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1720 { "migrate_cancel", "", do_migrate_cancel,
1721 "", "cancel the current VM migration" },
1722 { "migrate_set_speed", "s", do_migrate_set_speed,
1723 "value", "set maximum speed (in bytes) for migrations" },
aliguori6f338c32009-02-11 15:21:54 +00001724#if defined(TARGET_I386)
1725 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1726 "[file=file][,if=type][,bus=n]\n"
1727 "[,unit=m][,media=d][index=i]\n"
1728 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1729 "[snapshot=on|off][,cache=on|off]",
1730 "add drive to PCI storage controller" },
1731 { "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" },
1732 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
aliguoria66b11b2009-04-21 19:56:36 +00001733#endif
aliguori5c8be672009-04-21 19:56:32 +00001734 { "host_net_add", "ss?", net_host_device_add,
1735 "tap|user|socket|vde|dump [options]", "add host VLAN client" },
aliguori6f338c32009-02-11 15:21:54 +00001736 { "host_net_remove", "is", net_host_device_remove,
1737 "vlan_id name", "remove host VLAN client" },
aliguorid4ebe192009-04-21 19:56:44 +00001738#ifdef CONFIG_SLIRP
1739 { "host_net_redir", "s", net_slirp_redir,
1740 "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)" },
1741#endif
aliguoridf751fa2008-12-04 20:19:35 +00001742 { "balloon", "i", do_balloon,
1743 "target", "request VM to change it's memory allocation (in MB)" },
aliguorif029bd92009-02-11 15:19:16 +00001744 { "set_link", "ss", do_set_link,
aliguoricd33fee2009-04-18 15:36:15 +00001745 "name up|down", "change the link status of a network adapter" },
1746 { "acl", "sss?i?", do_acl, "<command> <aclname> [<match> [<index>]]\n",
aliguori76655d62009-03-06 20:27:37 +00001747 "acl show vnc.username\n"
1748 "acl policy vnc.username deny\n"
1749 "acl allow vnc.username fred\n"
1750 "acl deny vnc.username bob\n"
1751 "acl reset vnc.username\n" },
ths5fafdf22007-09-16 21:08:06 +00001752 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001753};
1754
blueswir1d2c639d2009-01-24 18:19:25 +00001755/* Please update qemu-doc.texi when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001756static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001757 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001758 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001759 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001760 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001761 { "chardev", "", qemu_chr_info,
1762 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001763 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001764 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001765 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001766 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001767 { "registers", "", do_info_registers,
1768 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001769 { "cpus", "", do_info_cpus,
1770 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001771 { "history", "", do_info_history,
1772 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001773 { "irq", "", irq_info,
1774 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001775 { "pic", "", pic_info,
1776 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001777 { "pci", "", pci_info,
1778 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001779#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001780 { "tlb", "", tlb_info,
1781 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001782#endif
1783#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001784 { "mem", "", mem_info,
1785 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001786 { "hpet", "", do_info_hpet,
1787 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001788#endif
bellarde3db7222005-01-26 22:00:47 +00001789 { "jit", "", do_info_jit,
1790 "", "show dynamic compiler info", },
bellard0f4c6412005-07-24 18:10:56 +00001791 { "kqemu", "", do_info_kqemu,
blueswir1d2c639d2009-01-24 18:19:25 +00001792 "", "show KQEMU information", },
aliguori7ba1e612008-11-05 16:04:33 +00001793 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001794 "", "show KVM information", },
bellarda594cfb2005-11-06 16:13:29 +00001795 { "usb", "", usb_info,
1796 "", "show guest USB devices", },
1797 { "usbhost", "", usb_host_info,
1798 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001799 { "profile", "", do_info_profile,
1800 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001801 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001802 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001803 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001804 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001805 { "status", "", do_info_status,
1806 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001807 { "pcmcia", "", pcmcia_info,
1808 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001809 { "mice", "", do_info_mice,
1810 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001811 { "vnc", "", do_info_vnc,
1812 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001813 { "name", "", do_info_name,
1814 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001815 { "uuid", "", do_info_uuid,
1816 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001817#if defined(TARGET_PPC)
1818 { "cpustats", "", do_info_cpu_stats,
1819 "", "show CPU statistics", },
1820#endif
blueswir131a60e22007-10-26 18:42:59 +00001821#if defined(CONFIG_SLIRP)
1822 { "slirp", "", do_info_slirp,
1823 "", "show SLIRP statistics", },
1824#endif
aliguori5bb79102008-10-13 03:12:02 +00001825 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001826 { "balloon", "", do_info_balloon,
1827 "", "show balloon information" },
bellard9dc39cb2004-03-14 21:38:27 +00001828 { NULL, NULL, },
1829};
1830
bellard9307c4c2004-04-04 12:57:25 +00001831/*******************************************************************/
1832
1833static const char *pch;
1834static jmp_buf expr_env;
1835
bellard92a31b12005-02-10 22:00:52 +00001836#define MD_TLONG 0
1837#define MD_I32 1
1838
bellard9307c4c2004-04-04 12:57:25 +00001839typedef struct MonitorDef {
1840 const char *name;
1841 int offset;
blueswir18662d652008-10-02 18:32:44 +00001842 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001843 int type;
bellard9307c4c2004-04-04 12:57:25 +00001844} MonitorDef;
1845
bellard57206fd2004-04-25 18:54:52 +00001846#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001847static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001848{
bellard6a00d602005-11-21 23:25:50 +00001849 CPUState *env = mon_get_cpu();
1850 if (!env)
1851 return 0;
1852 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001853}
1854#endif
1855
bellarda541f292004-04-12 20:39:29 +00001856#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001857static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001858{
bellard6a00d602005-11-21 23:25:50 +00001859 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001860 unsigned int u;
1861 int i;
1862
bellard6a00d602005-11-21 23:25:50 +00001863 if (!env)
1864 return 0;
1865
bellarda541f292004-04-12 20:39:29 +00001866 u = 0;
1867 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00001868 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001869
1870 return u;
1871}
1872
blueswir18662d652008-10-02 18:32:44 +00001873static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001874{
bellard6a00d602005-11-21 23:25:50 +00001875 CPUState *env = mon_get_cpu();
1876 if (!env)
1877 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001878 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001879}
1880
blueswir18662d652008-10-02 18:32:44 +00001881static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001882{
bellard6a00d602005-11-21 23:25:50 +00001883 CPUState *env = mon_get_cpu();
1884 if (!env)
1885 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001886 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001887}
bellard9fddaa02004-05-21 12:59:32 +00001888
blueswir18662d652008-10-02 18:32:44 +00001889static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001890{
bellard6a00d602005-11-21 23:25:50 +00001891 CPUState *env = mon_get_cpu();
1892 if (!env)
1893 return 0;
1894 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001895}
1896
blueswir18662d652008-10-02 18:32:44 +00001897static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001898{
bellard6a00d602005-11-21 23:25:50 +00001899 CPUState *env = mon_get_cpu();
1900 if (!env)
1901 return 0;
1902 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001903}
1904
blueswir18662d652008-10-02 18:32:44 +00001905static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001906{
bellard6a00d602005-11-21 23:25:50 +00001907 CPUState *env = mon_get_cpu();
1908 if (!env)
1909 return 0;
1910 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001911}
bellarda541f292004-04-12 20:39:29 +00001912#endif
1913
bellarde95c8d52004-09-30 22:22:08 +00001914#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001915#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001916static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001917{
bellard6a00d602005-11-21 23:25:50 +00001918 CPUState *env = mon_get_cpu();
1919 if (!env)
1920 return 0;
1921 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001922}
bellard7b936c02005-10-30 17:05:13 +00001923#endif
bellarde95c8d52004-09-30 22:22:08 +00001924
blueswir18662d652008-10-02 18:32:44 +00001925static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001926{
bellard6a00d602005-11-21 23:25:50 +00001927 CPUState *env = mon_get_cpu();
1928 if (!env)
1929 return 0;
1930 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001931}
1932#endif
1933
blueswir18662d652008-10-02 18:32:44 +00001934static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001935#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001936
1937#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001938 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001939 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001940 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001941
bellard9307c4c2004-04-04 12:57:25 +00001942 { "eax", offsetof(CPUState, regs[0]) },
1943 { "ecx", offsetof(CPUState, regs[1]) },
1944 { "edx", offsetof(CPUState, regs[2]) },
1945 { "ebx", offsetof(CPUState, regs[3]) },
1946 { "esp|sp", offsetof(CPUState, regs[4]) },
1947 { "ebp|fp", offsetof(CPUState, regs[5]) },
1948 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001949 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001950#ifdef TARGET_X86_64
1951 { "r8", offsetof(CPUState, regs[8]) },
1952 { "r9", offsetof(CPUState, regs[9]) },
1953 { "r10", offsetof(CPUState, regs[10]) },
1954 { "r11", offsetof(CPUState, regs[11]) },
1955 { "r12", offsetof(CPUState, regs[12]) },
1956 { "r13", offsetof(CPUState, regs[13]) },
1957 { "r14", offsetof(CPUState, regs[14]) },
1958 { "r15", offsetof(CPUState, regs[15]) },
1959#endif
bellard9307c4c2004-04-04 12:57:25 +00001960 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001961 { "eip", offsetof(CPUState, eip) },
1962 SEG("cs", R_CS)
1963 SEG("ds", R_DS)
1964 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001965 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001966 SEG("fs", R_FS)
1967 SEG("gs", R_GS)
1968 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00001969#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00001970 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00001971 { "r0", offsetof(CPUState, gpr[0]) },
1972 { "r1", offsetof(CPUState, gpr[1]) },
1973 { "r2", offsetof(CPUState, gpr[2]) },
1974 { "r3", offsetof(CPUState, gpr[3]) },
1975 { "r4", offsetof(CPUState, gpr[4]) },
1976 { "r5", offsetof(CPUState, gpr[5]) },
1977 { "r6", offsetof(CPUState, gpr[6]) },
1978 { "r7", offsetof(CPUState, gpr[7]) },
1979 { "r8", offsetof(CPUState, gpr[8]) },
1980 { "r9", offsetof(CPUState, gpr[9]) },
1981 { "r10", offsetof(CPUState, gpr[10]) },
1982 { "r11", offsetof(CPUState, gpr[11]) },
1983 { "r12", offsetof(CPUState, gpr[12]) },
1984 { "r13", offsetof(CPUState, gpr[13]) },
1985 { "r14", offsetof(CPUState, gpr[14]) },
1986 { "r15", offsetof(CPUState, gpr[15]) },
1987 { "r16", offsetof(CPUState, gpr[16]) },
1988 { "r17", offsetof(CPUState, gpr[17]) },
1989 { "r18", offsetof(CPUState, gpr[18]) },
1990 { "r19", offsetof(CPUState, gpr[19]) },
1991 { "r20", offsetof(CPUState, gpr[20]) },
1992 { "r21", offsetof(CPUState, gpr[21]) },
1993 { "r22", offsetof(CPUState, gpr[22]) },
1994 { "r23", offsetof(CPUState, gpr[23]) },
1995 { "r24", offsetof(CPUState, gpr[24]) },
1996 { "r25", offsetof(CPUState, gpr[25]) },
1997 { "r26", offsetof(CPUState, gpr[26]) },
1998 { "r27", offsetof(CPUState, gpr[27]) },
1999 { "r28", offsetof(CPUState, gpr[28]) },
2000 { "r29", offsetof(CPUState, gpr[29]) },
2001 { "r30", offsetof(CPUState, gpr[30]) },
2002 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002003 /* Floating point registers */
2004 { "f0", offsetof(CPUState, fpr[0]) },
2005 { "f1", offsetof(CPUState, fpr[1]) },
2006 { "f2", offsetof(CPUState, fpr[2]) },
2007 { "f3", offsetof(CPUState, fpr[3]) },
2008 { "f4", offsetof(CPUState, fpr[4]) },
2009 { "f5", offsetof(CPUState, fpr[5]) },
2010 { "f6", offsetof(CPUState, fpr[6]) },
2011 { "f7", offsetof(CPUState, fpr[7]) },
2012 { "f8", offsetof(CPUState, fpr[8]) },
2013 { "f9", offsetof(CPUState, fpr[9]) },
2014 { "f10", offsetof(CPUState, fpr[10]) },
2015 { "f11", offsetof(CPUState, fpr[11]) },
2016 { "f12", offsetof(CPUState, fpr[12]) },
2017 { "f13", offsetof(CPUState, fpr[13]) },
2018 { "f14", offsetof(CPUState, fpr[14]) },
2019 { "f15", offsetof(CPUState, fpr[15]) },
2020 { "f16", offsetof(CPUState, fpr[16]) },
2021 { "f17", offsetof(CPUState, fpr[17]) },
2022 { "f18", offsetof(CPUState, fpr[18]) },
2023 { "f19", offsetof(CPUState, fpr[19]) },
2024 { "f20", offsetof(CPUState, fpr[20]) },
2025 { "f21", offsetof(CPUState, fpr[21]) },
2026 { "f22", offsetof(CPUState, fpr[22]) },
2027 { "f23", offsetof(CPUState, fpr[23]) },
2028 { "f24", offsetof(CPUState, fpr[24]) },
2029 { "f25", offsetof(CPUState, fpr[25]) },
2030 { "f26", offsetof(CPUState, fpr[26]) },
2031 { "f27", offsetof(CPUState, fpr[27]) },
2032 { "f28", offsetof(CPUState, fpr[28]) },
2033 { "f29", offsetof(CPUState, fpr[29]) },
2034 { "f30", offsetof(CPUState, fpr[30]) },
2035 { "f31", offsetof(CPUState, fpr[31]) },
2036 { "fpscr", offsetof(CPUState, fpscr) },
2037 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002038 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002039 { "lr", offsetof(CPUState, lr) },
2040 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002041 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002042 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002043 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002044 { "msr", 0, &monitor_get_msr, },
2045 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002046 { "tbu", 0, &monitor_get_tbu, },
2047 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002048#if defined(TARGET_PPC64)
2049 /* Address space register */
2050 { "asr", offsetof(CPUState, asr) },
2051#endif
2052 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002053 { "sdr1", offsetof(CPUState, sdr1) },
2054 { "sr0", offsetof(CPUState, sr[0]) },
2055 { "sr1", offsetof(CPUState, sr[1]) },
2056 { "sr2", offsetof(CPUState, sr[2]) },
2057 { "sr3", offsetof(CPUState, sr[3]) },
2058 { "sr4", offsetof(CPUState, sr[4]) },
2059 { "sr5", offsetof(CPUState, sr[5]) },
2060 { "sr6", offsetof(CPUState, sr[6]) },
2061 { "sr7", offsetof(CPUState, sr[7]) },
2062 { "sr8", offsetof(CPUState, sr[8]) },
2063 { "sr9", offsetof(CPUState, sr[9]) },
2064 { "sr10", offsetof(CPUState, sr[10]) },
2065 { "sr11", offsetof(CPUState, sr[11]) },
2066 { "sr12", offsetof(CPUState, sr[12]) },
2067 { "sr13", offsetof(CPUState, sr[13]) },
2068 { "sr14", offsetof(CPUState, sr[14]) },
2069 { "sr15", offsetof(CPUState, sr[15]) },
2070 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002071#elif defined(TARGET_SPARC)
2072 { "g0", offsetof(CPUState, gregs[0]) },
2073 { "g1", offsetof(CPUState, gregs[1]) },
2074 { "g2", offsetof(CPUState, gregs[2]) },
2075 { "g3", offsetof(CPUState, gregs[3]) },
2076 { "g4", offsetof(CPUState, gregs[4]) },
2077 { "g5", offsetof(CPUState, gregs[5]) },
2078 { "g6", offsetof(CPUState, gregs[6]) },
2079 { "g7", offsetof(CPUState, gregs[7]) },
2080 { "o0", 0, monitor_get_reg },
2081 { "o1", 1, monitor_get_reg },
2082 { "o2", 2, monitor_get_reg },
2083 { "o3", 3, monitor_get_reg },
2084 { "o4", 4, monitor_get_reg },
2085 { "o5", 5, monitor_get_reg },
2086 { "o6", 6, monitor_get_reg },
2087 { "o7", 7, monitor_get_reg },
2088 { "l0", 8, monitor_get_reg },
2089 { "l1", 9, monitor_get_reg },
2090 { "l2", 10, monitor_get_reg },
2091 { "l3", 11, monitor_get_reg },
2092 { "l4", 12, monitor_get_reg },
2093 { "l5", 13, monitor_get_reg },
2094 { "l6", 14, monitor_get_reg },
2095 { "l7", 15, monitor_get_reg },
2096 { "i0", 16, monitor_get_reg },
2097 { "i1", 17, monitor_get_reg },
2098 { "i2", 18, monitor_get_reg },
2099 { "i3", 19, monitor_get_reg },
2100 { "i4", 20, monitor_get_reg },
2101 { "i5", 21, monitor_get_reg },
2102 { "i6", 22, monitor_get_reg },
2103 { "i7", 23, monitor_get_reg },
2104 { "pc", offsetof(CPUState, pc) },
2105 { "npc", offsetof(CPUState, npc) },
2106 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002107#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002108 { "psr", 0, &monitor_get_psr, },
2109 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002110#endif
bellarde95c8d52004-09-30 22:22:08 +00002111 { "tbr", offsetof(CPUState, tbr) },
2112 { "fsr", offsetof(CPUState, fsr) },
2113 { "f0", offsetof(CPUState, fpr[0]) },
2114 { "f1", offsetof(CPUState, fpr[1]) },
2115 { "f2", offsetof(CPUState, fpr[2]) },
2116 { "f3", offsetof(CPUState, fpr[3]) },
2117 { "f4", offsetof(CPUState, fpr[4]) },
2118 { "f5", offsetof(CPUState, fpr[5]) },
2119 { "f6", offsetof(CPUState, fpr[6]) },
2120 { "f7", offsetof(CPUState, fpr[7]) },
2121 { "f8", offsetof(CPUState, fpr[8]) },
2122 { "f9", offsetof(CPUState, fpr[9]) },
2123 { "f10", offsetof(CPUState, fpr[10]) },
2124 { "f11", offsetof(CPUState, fpr[11]) },
2125 { "f12", offsetof(CPUState, fpr[12]) },
2126 { "f13", offsetof(CPUState, fpr[13]) },
2127 { "f14", offsetof(CPUState, fpr[14]) },
2128 { "f15", offsetof(CPUState, fpr[15]) },
2129 { "f16", offsetof(CPUState, fpr[16]) },
2130 { "f17", offsetof(CPUState, fpr[17]) },
2131 { "f18", offsetof(CPUState, fpr[18]) },
2132 { "f19", offsetof(CPUState, fpr[19]) },
2133 { "f20", offsetof(CPUState, fpr[20]) },
2134 { "f21", offsetof(CPUState, fpr[21]) },
2135 { "f22", offsetof(CPUState, fpr[22]) },
2136 { "f23", offsetof(CPUState, fpr[23]) },
2137 { "f24", offsetof(CPUState, fpr[24]) },
2138 { "f25", offsetof(CPUState, fpr[25]) },
2139 { "f26", offsetof(CPUState, fpr[26]) },
2140 { "f27", offsetof(CPUState, fpr[27]) },
2141 { "f28", offsetof(CPUState, fpr[28]) },
2142 { "f29", offsetof(CPUState, fpr[29]) },
2143 { "f30", offsetof(CPUState, fpr[30]) },
2144 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002145#ifdef TARGET_SPARC64
2146 { "f32", offsetof(CPUState, fpr[32]) },
2147 { "f34", offsetof(CPUState, fpr[34]) },
2148 { "f36", offsetof(CPUState, fpr[36]) },
2149 { "f38", offsetof(CPUState, fpr[38]) },
2150 { "f40", offsetof(CPUState, fpr[40]) },
2151 { "f42", offsetof(CPUState, fpr[42]) },
2152 { "f44", offsetof(CPUState, fpr[44]) },
2153 { "f46", offsetof(CPUState, fpr[46]) },
2154 { "f48", offsetof(CPUState, fpr[48]) },
2155 { "f50", offsetof(CPUState, fpr[50]) },
2156 { "f52", offsetof(CPUState, fpr[52]) },
2157 { "f54", offsetof(CPUState, fpr[54]) },
2158 { "f56", offsetof(CPUState, fpr[56]) },
2159 { "f58", offsetof(CPUState, fpr[58]) },
2160 { "f60", offsetof(CPUState, fpr[60]) },
2161 { "f62", offsetof(CPUState, fpr[62]) },
2162 { "asi", offsetof(CPUState, asi) },
2163 { "pstate", offsetof(CPUState, pstate) },
2164 { "cansave", offsetof(CPUState, cansave) },
2165 { "canrestore", offsetof(CPUState, canrestore) },
2166 { "otherwin", offsetof(CPUState, otherwin) },
2167 { "wstate", offsetof(CPUState, wstate) },
2168 { "cleanwin", offsetof(CPUState, cleanwin) },
2169 { "fprs", offsetof(CPUState, fprs) },
2170#endif
bellard9307c4c2004-04-04 12:57:25 +00002171#endif
2172 { NULL },
2173};
2174
aliguori376253e2009-03-05 23:01:23 +00002175static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002176{
aliguori376253e2009-03-05 23:01:23 +00002177 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002178 longjmp(expr_env, 1);
2179}
2180
bellard6a00d602005-11-21 23:25:50 +00002181/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002182static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002183{
blueswir18662d652008-10-02 18:32:44 +00002184 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002185 void *ptr;
2186
bellard9307c4c2004-04-04 12:57:25 +00002187 for(md = monitor_defs; md->name != NULL; md++) {
2188 if (compare_cmd(name, md->name)) {
2189 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002190 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002191 } else {
bellard6a00d602005-11-21 23:25:50 +00002192 CPUState *env = mon_get_cpu();
2193 if (!env)
2194 return -2;
2195 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002196 switch(md->type) {
2197 case MD_I32:
2198 *pval = *(int32_t *)ptr;
2199 break;
2200 case MD_TLONG:
2201 *pval = *(target_long *)ptr;
2202 break;
2203 default:
2204 *pval = 0;
2205 break;
2206 }
bellard9307c4c2004-04-04 12:57:25 +00002207 }
2208 return 0;
2209 }
2210 }
2211 return -1;
2212}
2213
2214static void next(void)
2215{
2216 if (pch != '\0') {
2217 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002218 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002219 pch++;
2220 }
2221}
2222
aliguori376253e2009-03-05 23:01:23 +00002223static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002224
aliguori376253e2009-03-05 23:01:23 +00002225static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002226{
blueswir1c2efc952007-09-25 17:28:42 +00002227 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002228 char *p;
bellard6a00d602005-11-21 23:25:50 +00002229 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002230
2231 switch(*pch) {
2232 case '+':
2233 next();
aliguori376253e2009-03-05 23:01:23 +00002234 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002235 break;
2236 case '-':
2237 next();
aliguori376253e2009-03-05 23:01:23 +00002238 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002239 break;
2240 case '~':
2241 next();
aliguori376253e2009-03-05 23:01:23 +00002242 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002243 break;
2244 case '(':
2245 next();
aliguori376253e2009-03-05 23:01:23 +00002246 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002247 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002248 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002249 }
2250 next();
2251 break;
bellard81d09122004-07-14 17:21:37 +00002252 case '\'':
2253 pch++;
2254 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002255 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002256 n = *pch;
2257 pch++;
2258 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002259 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002260 next();
2261 break;
bellard9307c4c2004-04-04 12:57:25 +00002262 case '$':
2263 {
2264 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002265 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002266
bellard9307c4c2004-04-04 12:57:25 +00002267 pch++;
2268 q = buf;
2269 while ((*pch >= 'a' && *pch <= 'z') ||
2270 (*pch >= 'A' && *pch <= 'Z') ||
2271 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002272 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002273 if ((q - buf) < sizeof(buf) - 1)
2274 *q++ = *pch;
2275 pch++;
2276 }
blueswir1cd390082008-11-16 13:53:32 +00002277 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002278 pch++;
2279 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002280 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002281 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002282 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002283 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002284 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002285 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002286 }
2287 break;
2288 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002289 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002290 n = 0;
2291 break;
2292 default:
blueswir17743e582007-09-24 18:39:04 +00002293#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002294 n = strtoull(pch, &p, 0);
2295#else
bellard9307c4c2004-04-04 12:57:25 +00002296 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002297#endif
bellard9307c4c2004-04-04 12:57:25 +00002298 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002299 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002300 }
2301 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002302 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002303 pch++;
2304 break;
2305 }
2306 return n;
2307}
2308
2309
aliguori376253e2009-03-05 23:01:23 +00002310static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002311{
blueswir1c2efc952007-09-25 17:28:42 +00002312 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002313 int op;
ths3b46e622007-09-17 08:09:54 +00002314
aliguori376253e2009-03-05 23:01:23 +00002315 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002316 for(;;) {
2317 op = *pch;
2318 if (op != '*' && op != '/' && op != '%')
2319 break;
2320 next();
aliguori376253e2009-03-05 23:01:23 +00002321 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002322 switch(op) {
2323 default:
2324 case '*':
2325 val *= val2;
2326 break;
2327 case '/':
2328 case '%':
ths5fafdf22007-09-16 21:08:06 +00002329 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002330 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002331 if (op == '/')
2332 val /= val2;
2333 else
2334 val %= val2;
2335 break;
2336 }
2337 }
2338 return val;
2339}
2340
aliguori376253e2009-03-05 23:01:23 +00002341static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002342{
blueswir1c2efc952007-09-25 17:28:42 +00002343 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002344 int op;
bellard9307c4c2004-04-04 12:57:25 +00002345
aliguori376253e2009-03-05 23:01:23 +00002346 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002347 for(;;) {
2348 op = *pch;
2349 if (op != '&' && op != '|' && op != '^')
2350 break;
2351 next();
aliguori376253e2009-03-05 23:01:23 +00002352 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002353 switch(op) {
2354 default:
2355 case '&':
2356 val &= val2;
2357 break;
2358 case '|':
2359 val |= val2;
2360 break;
2361 case '^':
2362 val ^= val2;
2363 break;
2364 }
2365 }
2366 return val;
2367}
2368
aliguori376253e2009-03-05 23:01:23 +00002369static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002370{
blueswir1c2efc952007-09-25 17:28:42 +00002371 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002372 int op;
bellard9307c4c2004-04-04 12:57:25 +00002373
aliguori376253e2009-03-05 23:01:23 +00002374 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002375 for(;;) {
2376 op = *pch;
2377 if (op != '+' && op != '-')
2378 break;
2379 next();
aliguori376253e2009-03-05 23:01:23 +00002380 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002381 if (op == '+')
2382 val += val2;
2383 else
2384 val -= val2;
2385 }
2386 return val;
2387}
2388
aliguori376253e2009-03-05 23:01:23 +00002389static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002390{
2391 pch = *pp;
2392 if (setjmp(expr_env)) {
2393 *pp = pch;
2394 return -1;
2395 }
blueswir1cd390082008-11-16 13:53:32 +00002396 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002397 pch++;
aliguori376253e2009-03-05 23:01:23 +00002398 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002399 *pp = pch;
2400 return 0;
2401}
2402
2403static int get_str(char *buf, int buf_size, const char **pp)
2404{
2405 const char *p;
2406 char *q;
2407 int c;
2408
bellard81d09122004-07-14 17:21:37 +00002409 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002410 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002411 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002412 p++;
2413 if (*p == '\0') {
2414 fail:
bellard81d09122004-07-14 17:21:37 +00002415 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002416 *pp = p;
2417 return -1;
2418 }
bellard9307c4c2004-04-04 12:57:25 +00002419 if (*p == '\"') {
2420 p++;
2421 while (*p != '\0' && *p != '\"') {
2422 if (*p == '\\') {
2423 p++;
2424 c = *p++;
2425 switch(c) {
2426 case 'n':
2427 c = '\n';
2428 break;
2429 case 'r':
2430 c = '\r';
2431 break;
2432 case '\\':
2433 case '\'':
2434 case '\"':
2435 break;
2436 default:
2437 qemu_printf("unsupported escape code: '\\%c'\n", c);
2438 goto fail;
2439 }
2440 if ((q - buf) < buf_size - 1) {
2441 *q++ = c;
2442 }
2443 } else {
2444 if ((q - buf) < buf_size - 1) {
2445 *q++ = *p;
2446 }
2447 p++;
2448 }
2449 }
2450 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002451 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002452 goto fail;
2453 }
2454 p++;
2455 } else {
blueswir1cd390082008-11-16 13:53:32 +00002456 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002457 if ((q - buf) < buf_size - 1) {
2458 *q++ = *p;
2459 }
2460 p++;
2461 }
bellard9307c4c2004-04-04 12:57:25 +00002462 }
bellard81d09122004-07-14 17:21:37 +00002463 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002464 *pp = p;
2465 return 0;
2466}
2467
2468static int default_fmt_format = 'x';
2469static int default_fmt_size = 4;
2470
2471#define MAX_ARGS 16
2472
aliguori376253e2009-03-05 23:01:23 +00002473static void monitor_handle_command(Monitor *mon, const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002474{
2475 const char *p, *pstart, *typestr;
2476 char *q;
2477 int c, nb_args, len, i, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002478 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002479 char cmdname[256];
2480 char buf[1024];
2481 void *str_allocated[MAX_ARGS];
2482 void *args[MAX_ARGS];
aliguori376253e2009-03-05 23:01:23 +00002483 void (*handler_0)(Monitor *mon);
2484 void (*handler_1)(Monitor *mon, void *arg0);
2485 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2486 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2487 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2488 void *arg3);
2489 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2490 void *arg3, void *arg4);
2491 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2492 void *arg3, void *arg4, void *arg5);
2493 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2494 void *arg3, void *arg4, void *arg5, void *arg6);
bellard9dc39cb2004-03-14 21:38:27 +00002495
2496#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002497 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002498#endif
ths3b46e622007-09-17 08:09:54 +00002499
bellard9307c4c2004-04-04 12:57:25 +00002500 /* extract the command name */
bellard9dc39cb2004-03-14 21:38:27 +00002501 p = cmdline;
bellard9307c4c2004-04-04 12:57:25 +00002502 q = cmdname;
blueswir1cd390082008-11-16 13:53:32 +00002503 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002504 p++;
2505 if (*p == '\0')
bellard9dc39cb2004-03-14 21:38:27 +00002506 return;
bellard9307c4c2004-04-04 12:57:25 +00002507 pstart = p;
blueswir1cd390082008-11-16 13:53:32 +00002508 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002509 p++;
2510 len = p - pstart;
2511 if (len > sizeof(cmdname) - 1)
2512 len = sizeof(cmdname) - 1;
2513 memcpy(cmdname, pstart, len);
2514 cmdname[len] = '\0';
ths3b46e622007-09-17 08:09:54 +00002515
bellard9307c4c2004-04-04 12:57:25 +00002516 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002517 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002518 if (compare_cmd(cmdname, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +00002519 goto found;
2520 }
aliguori376253e2009-03-05 23:01:23 +00002521 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
bellard9dc39cb2004-03-14 21:38:27 +00002522 return;
2523 found:
bellard9307c4c2004-04-04 12:57:25 +00002524
2525 for(i = 0; i < MAX_ARGS; i++)
2526 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002527
bellard9307c4c2004-04-04 12:57:25 +00002528 /* parse the parameters */
2529 typestr = cmd->args_type;
2530 nb_args = 0;
2531 for(;;) {
2532 c = *typestr;
2533 if (c == '\0')
2534 break;
2535 typestr++;
2536 switch(c) {
2537 case 'F':
bellard81d09122004-07-14 17:21:37 +00002538 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002539 case 's':
2540 {
2541 int ret;
2542 char *str;
ths3b46e622007-09-17 08:09:54 +00002543
blueswir1cd390082008-11-16 13:53:32 +00002544 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002545 p++;
2546 if (*typestr == '?') {
2547 typestr++;
2548 if (*p == '\0') {
2549 /* no optional string: NULL argument */
2550 str = NULL;
2551 goto add_str;
2552 }
2553 }
2554 ret = get_str(buf, sizeof(buf), &p);
2555 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002556 switch(c) {
2557 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002558 monitor_printf(mon, "%s: filename expected\n",
2559 cmdname);
bellard81d09122004-07-14 17:21:37 +00002560 break;
2561 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002562 monitor_printf(mon, "%s: block device name expected\n",
2563 cmdname);
bellard81d09122004-07-14 17:21:37 +00002564 break;
2565 default:
aliguori376253e2009-03-05 23:01:23 +00002566 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002567 break;
2568 }
bellard9307c4c2004-04-04 12:57:25 +00002569 goto fail;
2570 }
2571 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002572 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002573 str_allocated[nb_args] = str;
2574 add_str:
2575 if (nb_args >= MAX_ARGS) {
2576 error_args:
aliguori376253e2009-03-05 23:01:23 +00002577 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002578 goto fail;
2579 }
2580 args[nb_args++] = str;
2581 }
2582 break;
2583 case '/':
2584 {
2585 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002586
blueswir1cd390082008-11-16 13:53:32 +00002587 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002588 p++;
2589 if (*p == '/') {
2590 /* format found */
2591 p++;
2592 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002593 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002594 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002595 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002596 count = count * 10 + (*p - '0');
2597 p++;
2598 }
2599 }
2600 size = -1;
2601 format = -1;
2602 for(;;) {
2603 switch(*p) {
2604 case 'o':
2605 case 'd':
2606 case 'u':
2607 case 'x':
2608 case 'i':
2609 case 'c':
2610 format = *p++;
2611 break;
2612 case 'b':
2613 size = 1;
2614 p++;
2615 break;
2616 case 'h':
2617 size = 2;
2618 p++;
2619 break;
2620 case 'w':
2621 size = 4;
2622 p++;
2623 break;
2624 case 'g':
2625 case 'L':
2626 size = 8;
2627 p++;
2628 break;
2629 default:
2630 goto next;
2631 }
2632 }
2633 next:
blueswir1cd390082008-11-16 13:53:32 +00002634 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002635 monitor_printf(mon, "invalid char in format: '%c'\n",
2636 *p);
bellard9307c4c2004-04-04 12:57:25 +00002637 goto fail;
2638 }
bellard9307c4c2004-04-04 12:57:25 +00002639 if (format < 0)
2640 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002641 if (format != 'i') {
2642 /* for 'i', not specifying a size gives -1 as size */
2643 if (size < 0)
2644 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002645 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002646 }
bellard9307c4c2004-04-04 12:57:25 +00002647 default_fmt_format = format;
2648 } else {
2649 count = 1;
2650 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002651 if (format != 'i') {
2652 size = default_fmt_size;
2653 } else {
2654 size = -1;
2655 }
bellard9307c4c2004-04-04 12:57:25 +00002656 }
2657 if (nb_args + 3 > MAX_ARGS)
2658 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002659 args[nb_args++] = (void*)(long)count;
2660 args[nb_args++] = (void*)(long)format;
2661 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002662 }
2663 break;
2664 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002665 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002666 {
blueswir1c2efc952007-09-25 17:28:42 +00002667 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002668
blueswir1cd390082008-11-16 13:53:32 +00002669 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002670 p++;
bellard34405572004-06-08 00:55:58 +00002671 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002672 if (*typestr == '?') {
2673 if (*p == '\0')
2674 has_arg = 0;
2675 else
2676 has_arg = 1;
2677 } else {
2678 if (*p == '.') {
2679 p++;
blueswir1cd390082008-11-16 13:53:32 +00002680 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002681 p++;
2682 has_arg = 1;
2683 } else {
2684 has_arg = 0;
2685 }
2686 }
bellard13224a82006-07-14 22:03:35 +00002687 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002688 if (nb_args >= MAX_ARGS)
2689 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002690 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002691 if (!has_arg) {
2692 if (nb_args >= MAX_ARGS)
2693 goto error_args;
2694 val = -1;
2695 goto add_num;
2696 }
2697 }
aliguori376253e2009-03-05 23:01:23 +00002698 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002699 goto fail;
2700 add_num:
bellard92a31b12005-02-10 22:00:52 +00002701 if (c == 'i') {
2702 if (nb_args >= MAX_ARGS)
2703 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002704 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002705 } else {
2706 if ((nb_args + 1) >= MAX_ARGS)
2707 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002708#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002709 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002710#else
2711 args[nb_args++] = (void *)0;
2712#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002713 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002714 }
bellard9307c4c2004-04-04 12:57:25 +00002715 }
2716 break;
2717 case '-':
2718 {
2719 int has_option;
2720 /* option */
ths3b46e622007-09-17 08:09:54 +00002721
bellard9307c4c2004-04-04 12:57:25 +00002722 c = *typestr++;
2723 if (c == '\0')
2724 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002725 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002726 p++;
2727 has_option = 0;
2728 if (*p == '-') {
2729 p++;
2730 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002731 monitor_printf(mon, "%s: unsupported option -%c\n",
2732 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002733 goto fail;
2734 }
2735 p++;
2736 has_option = 1;
2737 }
2738 if (nb_args >= MAX_ARGS)
2739 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002740 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002741 }
2742 break;
2743 default:
2744 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002745 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002746 goto fail;
2747 }
2748 }
2749 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002750 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002751 p++;
2752 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002753 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2754 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002755 goto fail;
2756 }
2757
2758 switch(nb_args) {
2759 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002760 handler_0 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002761 handler_0(mon);
bellard9307c4c2004-04-04 12:57:25 +00002762 break;
2763 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002764 handler_1 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002765 handler_1(mon, args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002766 break;
2767 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002768 handler_2 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002769 handler_2(mon, args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002770 break;
2771 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002772 handler_3 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002773 handler_3(mon, args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002774 break;
2775 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002776 handler_4 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002777 handler_4(mon, args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002778 break;
2779 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002780 handler_5 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002781 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002782 break;
bellard34405572004-06-08 00:55:58 +00002783 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002784 handler_6 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002785 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002786 break;
bellardec36b692006-07-16 18:57:03 +00002787 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002788 handler_7 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002789 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2790 args[6]);
bellardec36b692006-07-16 18:57:03 +00002791 break;
bellard9307c4c2004-04-04 12:57:25 +00002792 default:
aliguori376253e2009-03-05 23:01:23 +00002793 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
bellard9307c4c2004-04-04 12:57:25 +00002794 goto fail;
2795 }
2796 fail:
2797 for(i = 0; i < MAX_ARGS; i++)
2798 qemu_free(str_allocated[i]);
2799 return;
bellard9dc39cb2004-03-14 21:38:27 +00002800}
2801
bellard81d09122004-07-14 17:21:37 +00002802static void cmd_completion(const char *name, const char *list)
2803{
2804 const char *p, *pstart;
2805 char cmd[128];
2806 int len;
2807
2808 p = list;
2809 for(;;) {
2810 pstart = p;
2811 p = strchr(p, '|');
2812 if (!p)
2813 p = pstart + strlen(pstart);
2814 len = p - pstart;
2815 if (len > sizeof(cmd) - 2)
2816 len = sizeof(cmd) - 2;
2817 memcpy(cmd, pstart, len);
2818 cmd[len] = '\0';
2819 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002820 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002821 }
2822 if (*p == '\0')
2823 break;
2824 p++;
2825 }
2826}
2827
2828static void file_completion(const char *input)
2829{
2830 DIR *ffs;
2831 struct dirent *d;
2832 char path[1024];
2833 char file[1024], file_prefix[1024];
2834 int input_path_len;
2835 const char *p;
2836
ths5fafdf22007-09-16 21:08:06 +00002837 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002838 if (!p) {
2839 input_path_len = 0;
2840 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002841 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002842 } else {
2843 input_path_len = p - input + 1;
2844 memcpy(path, input, input_path_len);
2845 if (input_path_len > sizeof(path) - 1)
2846 input_path_len = sizeof(path) - 1;
2847 path[input_path_len] = '\0';
2848 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2849 }
2850#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002851 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2852 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002853#endif
2854 ffs = opendir(path);
2855 if (!ffs)
2856 return;
2857 for(;;) {
2858 struct stat sb;
2859 d = readdir(ffs);
2860 if (!d)
2861 break;
2862 if (strstart(d->d_name, file_prefix, NULL)) {
2863 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002864 if (input_path_len < sizeof(file))
2865 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2866 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002867 /* stat the file to find out if it's a directory.
2868 * In that case add a slash to speed up typing long paths
2869 */
2870 stat(file, &sb);
2871 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002872 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00002873 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002874 }
2875 }
2876 closedir(ffs);
2877}
2878
aliguori51de9762009-03-05 23:00:43 +00002879static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002880{
aliguori51de9762009-03-05 23:00:43 +00002881 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002882 const char *input = opaque;
2883
2884 if (input[0] == '\0' ||
2885 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00002886 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00002887 }
2888}
2889
2890/* NOTE: this parser is an approximate form of the real command parser */
2891static void parse_cmdline(const char *cmdline,
2892 int *pnb_args, char **args)
2893{
2894 const char *p;
2895 int nb_args, ret;
2896 char buf[1024];
2897
2898 p = cmdline;
2899 nb_args = 0;
2900 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002901 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002902 p++;
2903 if (*p == '\0')
2904 break;
2905 if (nb_args >= MAX_ARGS)
2906 break;
2907 ret = get_str(buf, sizeof(buf), &p);
2908 args[nb_args] = qemu_strdup(buf);
2909 nb_args++;
2910 if (ret < 0)
2911 break;
2912 }
2913 *pnb_args = nb_args;
2914}
2915
aliguori4c36ba32009-03-05 23:01:37 +00002916static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002917{
2918 const char *cmdname;
2919 char *args[MAX_ARGS];
2920 int nb_args, i, len;
2921 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00002922 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002923 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002924
2925 parse_cmdline(cmdline, &nb_args, args);
2926#ifdef DEBUG_COMPLETION
2927 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00002928 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00002929 }
2930#endif
2931
2932 /* if the line ends with a space, it means we want to complete the
2933 next arg */
2934 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002935 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002936 if (nb_args >= MAX_ARGS)
2937 return;
2938 args[nb_args++] = qemu_strdup("");
2939 }
2940 if (nb_args <= 1) {
2941 /* command completion */
2942 if (nb_args == 0)
2943 cmdname = "";
2944 else
2945 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00002946 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00002947 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002948 cmd_completion(cmdname, cmd->name);
2949 }
2950 } else {
2951 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002952 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002953 if (compare_cmd(args[0], cmd->name))
2954 goto found;
2955 }
2956 return;
2957 found:
2958 ptype = cmd->args_type;
2959 for(i = 0; i < nb_args - 2; i++) {
2960 if (*ptype != '\0') {
2961 ptype++;
2962 while (*ptype == '?')
2963 ptype++;
2964 }
2965 }
2966 str = args[nb_args - 1];
2967 switch(*ptype) {
2968 case 'F':
2969 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00002970 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00002971 file_completion(str);
2972 break;
2973 case 'B':
2974 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00002975 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00002976 bdrv_iterate(block_completion_it, (void *)str);
2977 break;
bellard7fe48482004-10-09 18:08:01 +00002978 case 's':
2979 /* XXX: more generic ? */
2980 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00002981 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00002982 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2983 cmd_completion(str, cmd->name);
2984 }
bellard64866c32006-05-07 18:03:31 +00002985 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00002986 char *sep = strrchr(str, '-');
2987 if (sep)
2988 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00002989 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00002990 for(key = key_defs; key->name != NULL; key++) {
2991 cmd_completion(str, key->name);
2992 }
bellard7fe48482004-10-09 18:08:01 +00002993 }
2994 break;
bellard81d09122004-07-14 17:21:37 +00002995 default:
2996 break;
2997 }
2998 }
2999 for(i = 0; i < nb_args; i++)
3000 qemu_free(args[i]);
3001}
3002
aliguori731b0362009-03-05 23:01:42 +00003003static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003004{
aliguori731b0362009-03-05 23:01:42 +00003005 Monitor *mon = opaque;
3006
3007 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003008}
3009
aliguori731b0362009-03-05 23:01:42 +00003010static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003011{
aliguori731b0362009-03-05 23:01:42 +00003012 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003013 int i;
aliguori376253e2009-03-05 23:01:23 +00003014
aliguori731b0362009-03-05 23:01:42 +00003015 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003016
aliguoricde76ee2009-03-05 23:01:51 +00003017 if (cur_mon->rs) {
3018 for (i = 0; i < size; i++)
3019 readline_handle_byte(cur_mon->rs, buf[i]);
3020 } else {
3021 if (size == 0 || buf[size - 1] != 0)
3022 monitor_printf(cur_mon, "corrupted command\n");
3023 else
3024 monitor_handle_command(cur_mon, (char *)buf);
3025 }
aliguori731b0362009-03-05 23:01:42 +00003026
3027 cur_mon = old_mon;
3028}
aliguorid8f44602008-10-06 13:52:44 +00003029
aliguori376253e2009-03-05 23:01:23 +00003030static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003031{
aliguori731b0362009-03-05 23:01:42 +00003032 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003033 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003034 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003035}
3036
aliguoricde76ee2009-03-05 23:01:51 +00003037int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003038{
aliguoricde76ee2009-03-05 23:01:51 +00003039 if (!mon->rs)
3040 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003041 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003042 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003043}
3044
aliguori376253e2009-03-05 23:01:23 +00003045void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003046{
aliguoricde76ee2009-03-05 23:01:51 +00003047 if (!mon->rs)
3048 return;
aliguori731b0362009-03-05 23:01:42 +00003049 if (--mon->suspend_cnt == 0)
3050 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003051}
3052
aliguori731b0362009-03-05 23:01:42 +00003053static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003054{
aliguori376253e2009-03-05 23:01:23 +00003055 Monitor *mon = opaque;
3056
aliguori2724b182009-03-05 23:01:47 +00003057 switch (event) {
3058 case CHR_EVENT_MUX_IN:
3059 readline_restart(mon->rs);
3060 monitor_resume(mon);
3061 monitor_flush(mon);
3062 break;
ths86e94de2007-01-05 22:01:59 +00003063
aliguori2724b182009-03-05 23:01:47 +00003064 case CHR_EVENT_MUX_OUT:
3065 if (mon->suspend_cnt == 0)
3066 monitor_printf(mon, "\n");
3067 monitor_flush(mon);
3068 monitor_suspend(mon);
3069 break;
3070
3071 case CHR_EVENT_RESET:
3072 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3073 "information\n", QEMU_VERSION);
3074 if (mon->chr->focus == 0)
3075 readline_show_prompt(mon->rs);
3076 break;
3077 }
ths86e94de2007-01-05 22:01:59 +00003078}
3079
aliguori76655d62009-03-06 20:27:37 +00003080
3081/*
3082 * Local variables:
3083 * c-indent-level: 4
3084 * c-basic-offset: 4
3085 * tab-width: 8
3086 * End:
3087 */
3088
aliguori731b0362009-03-05 23:01:42 +00003089void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003090{
aliguori731b0362009-03-05 23:01:42 +00003091 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003092 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003093
3094 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003095 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003096 is_first_init = 0;
3097 }
aliguori87127162009-03-05 23:01:29 +00003098
3099 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003100
aliguori87127162009-03-05 23:01:29 +00003101 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003102 mon->flags = flags;
aliguori2724b182009-03-05 23:01:47 +00003103 if (mon->chr->focus != 0)
3104 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
aliguoricde76ee2009-03-05 23:01:51 +00003105 if (flags & MONITOR_USE_READLINE) {
3106 mon->rs = readline_init(mon, monitor_find_completion);
3107 monitor_read_command(mon, 0);
3108 }
aliguori87127162009-03-05 23:01:29 +00003109
aliguori731b0362009-03-05 23:01:42 +00003110 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3111 mon);
aliguori87127162009-03-05 23:01:29 +00003112
3113 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003114 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003115 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003116}
3117
aliguori376253e2009-03-05 23:01:23 +00003118static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003119{
aliguoribb5fc202009-03-05 23:01:15 +00003120 BlockDriverState *bs = opaque;
3121 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003122
aliguoribb5fc202009-03-05 23:01:15 +00003123 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003124 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003125 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003126 }
aliguori731b0362009-03-05 23:01:42 +00003127 if (mon->password_completion_cb)
3128 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003129
aliguori731b0362009-03-05 23:01:42 +00003130 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003131}
aliguoric0f4ce72009-03-05 23:01:01 +00003132
aliguori376253e2009-03-05 23:01:23 +00003133void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003134 BlockDriverCompletionFunc *completion_cb,
3135 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003136{
aliguoricde76ee2009-03-05 23:01:51 +00003137 int err;
3138
aliguoribb5fc202009-03-05 23:01:15 +00003139 if (!bdrv_key_required(bs)) {
3140 if (completion_cb)
3141 completion_cb(opaque, 0);
3142 return;
3143 }
aliguoric0f4ce72009-03-05 23:01:01 +00003144
aliguori376253e2009-03-05 23:01:23 +00003145 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3146 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003147
aliguori731b0362009-03-05 23:01:42 +00003148 mon->password_completion_cb = completion_cb;
3149 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003150
aliguoricde76ee2009-03-05 23:01:51 +00003151 err = monitor_read_password(mon, bdrv_password_cb, bs);
3152
3153 if (err && completion_cb)
3154 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003155}