blob: 85da2390b1887a93a8088abd0ecdad49d46c6e41 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw/hw.h"
25#include "hw/usb.h"
26#include "hw/pcmcia.h"
27#include "hw/pc.h"
28#include "hw/pci.h"
29#include "gdbstub.h"
30#include "net.h"
31#include "qemu-char.h"
32#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000033#include "monitor.h"
34#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000035#include "console.h"
36#include "block.h"
37#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000038#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000039#include "balloon.h"
bellard81d09122004-07-14 17:21:37 +000040#include <dirent.h>
balrogc8256f92008-06-08 22:45:01 +000041#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000042#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000043#include "kvm.h"
ths6a5bd302007-12-03 17:05:38 +000044
bellard9dc39cb2004-03-14 21:38:27 +000045//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000046//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000047
bellard9307c4c2004-04-04 12:57:25 +000048/*
49 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000050 *
bellard9307c4c2004-04-04 12:57:25 +000051 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000052 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000053 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000054 * 'i' 32 bit integer
55 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000056 * '/' optional gdb-like print format (like "/10x")
57 *
58 * '?' optional type (for 'F', 's' and 'i')
59 *
60 */
61
aliguori376253e2009-03-05 23:01:23 +000062typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000063 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000064 const char *args_type;
blueswir1a5f1b962008-08-17 20:21:51 +000065 void *handler;
bellard9dc39cb2004-03-14 21:38:27 +000066 const char *params;
67 const char *help;
aliguori376253e2009-03-05 23:01:23 +000068} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000069
aliguori87127162009-03-05 23:01:29 +000070struct Monitor {
71 CharDriverState *chr;
aliguori731b0362009-03-05 23:01:42 +000072 int flags;
73 int suspend_cnt;
74 uint8_t outbuf[1024];
75 int outbuf_index;
76 ReadLineState *rs;
77 CPUState *mon_cpu;
78 BlockDriverCompletionFunc *password_completion_cb;
79 void *password_opaque;
aliguori87127162009-03-05 23:01:29 +000080 LIST_ENTRY(Monitor) entry;
81};
82
83static LIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +000084
aliguori376253e2009-03-05 23:01:23 +000085static const mon_cmd_t mon_cmds[];
86static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +000087
aliguori87127162009-03-05 23:01:29 +000088Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +000089
aliguori731b0362009-03-05 23:01:42 +000090static void monitor_command_cb(Monitor *mon, const char *cmdline,
91 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +000092
aliguori731b0362009-03-05 23:01:42 +000093static void monitor_read_command(Monitor *mon, int show_prompt)
94{
95 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
96 if (show_prompt)
97 readline_show_prompt(mon->rs);
98}
bellard6a00d602005-11-21 23:25:50 +000099
aliguoricde76ee2009-03-05 23:01:51 +0000100static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
101 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000102{
aliguoricde76ee2009-03-05 23:01:51 +0000103 if (mon->rs) {
104 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
105 /* prompt is printed on return from the command handler */
106 return 0;
107 } else {
108 monitor_printf(mon, "terminal does not support password prompting\n");
109 return -ENOTTY;
110 }
aliguoribb5fc202009-03-05 23:01:15 +0000111}
112
aliguori376253e2009-03-05 23:01:23 +0000113void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000114{
aliguori731b0362009-03-05 23:01:42 +0000115 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
116 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
117 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000118 }
119}
120
121/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000122static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000123{
ths60fe76f2007-12-16 03:02:09 +0000124 char c;
aliguori731b0362009-03-05 23:01:42 +0000125
126 if (!mon)
127 return;
128
bellard7e2515e2004-08-01 21:52:19 +0000129 for(;;) {
130 c = *str++;
131 if (c == '\0')
132 break;
bellard7ba12602006-07-14 20:26:42 +0000133 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000134 mon->outbuf[mon->outbuf_index++] = '\r';
135 mon->outbuf[mon->outbuf_index++] = c;
136 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
137 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000138 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000139 }
140}
141
aliguori376253e2009-03-05 23:01:23 +0000142void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000143{
144 char buf[4096];
145 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000146 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000147}
148
aliguori376253e2009-03-05 23:01:23 +0000149void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000150{
151 va_list ap;
152 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000153 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000154 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000155}
156
aliguori376253e2009-03-05 23:01:23 +0000157void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000158{
159 int i;
160
161 for (i = 0; filename[i]; i++) {
162 switch (filename[i]) {
163 case ' ':
164 case '"':
165 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000166 monitor_printf(mon, "\\%c", filename[i]);
thsfef30742006-12-22 14:11:32 +0000167 break;
168 case '\t':
aliguori376253e2009-03-05 23:01:23 +0000169 monitor_printf(mon, "\\t");
thsfef30742006-12-22 14:11:32 +0000170 break;
171 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000172 monitor_printf(mon, "\\r");
thsfef30742006-12-22 14:11:32 +0000173 break;
174 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000175 monitor_printf(mon, "\\n");
thsfef30742006-12-22 14:11:32 +0000176 break;
177 default:
aliguori376253e2009-03-05 23:01:23 +0000178 monitor_printf(mon, "%c", filename[i]);
thsfef30742006-12-22 14:11:32 +0000179 break;
180 }
181 }
182}
183
bellard7fe48482004-10-09 18:08:01 +0000184static int monitor_fprintf(FILE *stream, const char *fmt, ...)
185{
186 va_list ap;
187 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000188 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000189 va_end(ap);
190 return 0;
191}
192
bellard9dc39cb2004-03-14 21:38:27 +0000193static int compare_cmd(const char *name, const char *list)
194{
195 const char *p, *pstart;
196 int len;
197 len = strlen(name);
198 p = list;
199 for(;;) {
200 pstart = p;
201 p = strchr(p, '|');
202 if (!p)
203 p = pstart + strlen(pstart);
204 if ((p - pstart) == len && !memcmp(pstart, name, len))
205 return 1;
206 if (*p == '\0')
207 break;
208 p++;
209 }
210 return 0;
211}
212
aliguori376253e2009-03-05 23:01:23 +0000213static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
214 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000215{
aliguori376253e2009-03-05 23:01:23 +0000216 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000217
218 for(cmd = cmds; cmd->name != NULL; cmd++) {
219 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000220 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
221 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000222 }
223}
224
aliguori376253e2009-03-05 23:01:23 +0000225static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000226{
227 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000228 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000229 } else {
aliguori376253e2009-03-05 23:01:23 +0000230 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000231 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000232 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000233 monitor_printf(mon, "Log items (comma separated):\n");
234 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000235 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000236 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000237 }
238 }
bellard9dc39cb2004-03-14 21:38:27 +0000239 }
240}
241
aliguori376253e2009-03-05 23:01:23 +0000242static void do_commit(Monitor *mon, const char *device)
bellard9dc39cb2004-03-14 21:38:27 +0000243{
bellard7954c732006-08-01 15:52:40 +0000244 int i, all_devices;
balrog2dc7b602007-05-24 18:53:22 +0000245
bellard7954c732006-08-01 15:52:40 +0000246 all_devices = !strcmp(device, "all");
thse4bcb142007-12-02 04:51:10 +0000247 for (i = 0; i < nb_drives; i++) {
ths5fafdf22007-09-16 21:08:06 +0000248 if (all_devices ||
thse4bcb142007-12-02 04:51:10 +0000249 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
250 bdrv_commit(drives_table[i].bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000251 }
252}
253
aliguori376253e2009-03-05 23:01:23 +0000254static void do_info(Monitor *mon, const char *item)
bellard9dc39cb2004-03-14 21:38:27 +0000255{
aliguori376253e2009-03-05 23:01:23 +0000256 const mon_cmd_t *cmd;
257 void (*handler)(Monitor *);
bellard9dc39cb2004-03-14 21:38:27 +0000258
bellard9307c4c2004-04-04 12:57:25 +0000259 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000260 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000261 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000262 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000263 goto found;
264 }
265 help:
aliguori376253e2009-03-05 23:01:23 +0000266 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000267 return;
268 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000269 handler = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +0000270 handler(mon);
bellard9dc39cb2004-03-14 21:38:27 +0000271}
272
aliguori376253e2009-03-05 23:01:23 +0000273static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000274{
aliguori376253e2009-03-05 23:01:23 +0000275 monitor_printf(mon, "%s\n", QEMU_VERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000276}
277
aliguori376253e2009-03-05 23:01:23 +0000278static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000279{
280 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000281 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000282}
283
aurel32bf4f74c2008-12-18 22:42:34 +0000284#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000285static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000286{
aliguori376253e2009-03-05 23:01:23 +0000287 monitor_printf(mon, "HPET is %s by QEMU\n",
288 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000289}
aurel32bf4f74c2008-12-18 22:42:34 +0000290#endif
aliguori16b29ae2008-12-17 23:28:44 +0000291
aliguori376253e2009-03-05 23:01:23 +0000292static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000293{
aliguori376253e2009-03-05 23:01:23 +0000294 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
295 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
296 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
297 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
298 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000299}
300
bellard6a00d602005-11-21 23:25:50 +0000301/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000302static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000303{
304 CPUState *env;
305
306 for(env = first_cpu; env != NULL; env = env->next_cpu) {
307 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000308 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000309 return 0;
310 }
311 }
312 return -1;
313}
314
pbrook9596ebb2007-11-18 01:44:38 +0000315static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000316{
aliguori731b0362009-03-05 23:01:42 +0000317 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000318 mon_set_cpu(0);
319 }
aliguori731b0362009-03-05 23:01:42 +0000320 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000321}
322
aliguori376253e2009-03-05 23:01:23 +0000323static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000324{
bellard6a00d602005-11-21 23:25:50 +0000325 CPUState *env;
326 env = mon_get_cpu();
327 if (!env)
328 return;
bellard9307c4c2004-04-04 12:57:25 +0000329#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000330 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000331 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000332#else
aliguori376253e2009-03-05 23:01:23 +0000333 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000334 0);
bellard9307c4c2004-04-04 12:57:25 +0000335#endif
336}
337
aliguori376253e2009-03-05 23:01:23 +0000338static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000339{
340 CPUState *env;
341
342 /* just to set the default cpu if not already done */
343 mon_get_cpu();
344
345 for(env = first_cpu; env != NULL; env = env->next_cpu) {
aliguori376253e2009-03-05 23:01:23 +0000346 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000347 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000348 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000349#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000350 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
351 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000352#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000353 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000354#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000355 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
356 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000357#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000358 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000359#endif
thsead93602007-09-06 00:18:15 +0000360 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000361 monitor_printf(mon, " (halted)");
362 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000363 }
364}
365
aliguori376253e2009-03-05 23:01:23 +0000366static void do_cpu_set(Monitor *mon, int index)
bellard6a00d602005-11-21 23:25:50 +0000367{
368 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000369 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000370}
371
aliguori376253e2009-03-05 23:01:23 +0000372static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000373{
aliguori376253e2009-03-05 23:01:23 +0000374 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000375}
376
aliguori376253e2009-03-05 23:01:23 +0000377static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000378{
379 int i;
bellard7e2515e2004-08-01 21:52:19 +0000380 const char *str;
ths3b46e622007-09-17 08:09:54 +0000381
aliguoricde76ee2009-03-05 23:01:51 +0000382 if (!mon->rs)
383 return;
bellard7e2515e2004-08-01 21:52:19 +0000384 i = 0;
385 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000386 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000387 if (!str)
388 break;
aliguori376253e2009-03-05 23:01:23 +0000389 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000390 i++;
bellardaa455482004-04-04 13:07:25 +0000391 }
392}
393
j_mayer76a66252007-03-07 08:32:30 +0000394#if defined(TARGET_PPC)
395/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000396static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000397{
398 CPUState *env;
399
400 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000401 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000402}
403#endif
404
aliguori376253e2009-03-05 23:01:23 +0000405static void do_quit(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000406{
407 exit(0);
408}
409
aliguori376253e2009-03-05 23:01:23 +0000410static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000411{
412 if (bdrv_is_inserted(bs)) {
413 if (!force) {
414 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000415 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000416 return -1;
417 }
418 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000419 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000420 return -1;
421 }
422 }
423 bdrv_close(bs);
424 }
425 return 0;
426}
427
aliguori376253e2009-03-05 23:01:23 +0000428static void do_eject(Monitor *mon, int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000429{
430 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000431
bellard9307c4c2004-04-04 12:57:25 +0000432 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000433 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000434 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000435 return;
436 }
aliguori376253e2009-03-05 23:01:23 +0000437 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000438}
439
aliguori376253e2009-03-05 23:01:23 +0000440static void do_change_block(Monitor *mon, const char *device,
441 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000442{
443 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000444 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000445
bellard9307c4c2004-04-04 12:57:25 +0000446 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000447 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000448 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000449 return;
450 }
aurel322ecea9b2008-06-18 22:10:01 +0000451 if (fmt) {
452 drv = bdrv_find_format(fmt);
453 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000454 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000455 return;
456 }
457 }
aliguori376253e2009-03-05 23:01:23 +0000458 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000459 return;
aurel322ecea9b2008-06-18 22:10:01 +0000460 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000461 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000462}
463
aliguori376253e2009-03-05 23:01:23 +0000464static void change_vnc_password_cb(Monitor *mon, const char *password,
465 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000466{
467 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000468 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000469
aliguori731b0362009-03-05 23:01:42 +0000470 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000471}
472
aliguori376253e2009-03-05 23:01:23 +0000473static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000474{
ths70848512007-08-25 01:37:05 +0000475 if (strcmp(target, "passwd") == 0 ||
476 strcmp(target, "password") == 0) {
aliguori2569da02008-12-10 15:14:13 +0000477 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000478 char password[9];
aliguori2569da02008-12-10 15:14:13 +0000479 strncpy(password, arg, sizeof(password));
480 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000481 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000482 } else {
aliguori376253e2009-03-05 23:01:23 +0000483 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000484 }
ths70848512007-08-25 01:37:05 +0000485 } else {
486 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000487 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000488 }
thse25a5822007-08-25 01:36:20 +0000489}
490
aliguori376253e2009-03-05 23:01:23 +0000491static void do_change(Monitor *mon, const char *device, const char *target,
492 const char *arg)
thse25a5822007-08-25 01:36:20 +0000493{
494 if (strcmp(device, "vnc") == 0) {
aliguori376253e2009-03-05 23:01:23 +0000495 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000496 } else {
aliguori376253e2009-03-05 23:01:23 +0000497 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000498 }
499}
500
aliguori376253e2009-03-05 23:01:23 +0000501static void do_screen_dump(Monitor *mon, const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000502{
pbrook95219892006-04-09 01:06:34 +0000503 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000504}
505
aliguori376253e2009-03-05 23:01:23 +0000506static void do_logfile(Monitor *mon, const char *filename)
pbrooke735b912007-06-30 13:53:24 +0000507{
508 cpu_set_log_filename(filename);
509}
510
aliguori376253e2009-03-05 23:01:23 +0000511static void do_log(Monitor *mon, const char *items)
bellardf193c792004-03-21 17:06:25 +0000512{
513 int mask;
ths3b46e622007-09-17 08:09:54 +0000514
bellard9307c4c2004-04-04 12:57:25 +0000515 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000516 mask = 0;
517 } else {
bellard9307c4c2004-04-04 12:57:25 +0000518 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000519 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000520 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000521 return;
522 }
523 }
524 cpu_set_log(mask);
525}
526
aliguori376253e2009-03-05 23:01:23 +0000527static void do_stop(Monitor *mon)
bellard8a7ddc32004-03-31 19:00:16 +0000528{
529 vm_stop(EXCP_INTERRUPT);
530}
531
aliguoribb5fc202009-03-05 23:01:15 +0000532static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000533
aliguori376253e2009-03-05 23:01:23 +0000534struct bdrv_iterate_context {
535 Monitor *mon;
536 int err;
537};
aliguoric0f4ce72009-03-05 23:01:01 +0000538
aliguori376253e2009-03-05 23:01:23 +0000539static void do_cont(Monitor *mon)
540{
541 struct bdrv_iterate_context context = { mon, 0 };
542
543 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000544 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000545 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000546 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000547}
548
aliguoribb5fc202009-03-05 23:01:15 +0000549static void bdrv_key_cb(void *opaque, int err)
550{
aliguori376253e2009-03-05 23:01:23 +0000551 Monitor *mon = opaque;
552
aliguoribb5fc202009-03-05 23:01:15 +0000553 /* another key was set successfully, retry to continue */
554 if (!err)
aliguori376253e2009-03-05 23:01:23 +0000555 do_cont(mon);
aliguoribb5fc202009-03-05 23:01:15 +0000556}
557
558static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
559{
aliguori376253e2009-03-05 23:01:23 +0000560 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000561
aliguori376253e2009-03-05 23:01:23 +0000562 if (!context->err && bdrv_key_required(bs)) {
563 context->err = -EBUSY;
564 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
565 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000566 }
567}
568
bellard67b915a2004-03-31 23:37:16 +0000569#ifdef CONFIG_GDBSTUB
aliguori376253e2009-03-05 23:01:23 +0000570static void do_gdbserver(Monitor *mon, const char *port)
bellard8a7ddc32004-03-31 19:00:16 +0000571{
pbrookcfc34752007-02-22 01:48:01 +0000572 if (!port)
bellard9307c4c2004-04-04 12:57:25 +0000573 port = DEFAULT_GDBSTUB_PORT;
pbrookcfc34752007-02-22 01:48:01 +0000574 if (gdbserver_start(port) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000575 monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",
576 port);
bellard8a7ddc32004-03-31 19:00:16 +0000577 } else {
aliguori376253e2009-03-05 23:01:23 +0000578 monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000579 }
580}
bellard67b915a2004-03-31 23:37:16 +0000581#endif
bellard8a7ddc32004-03-31 19:00:16 +0000582
aliguori376253e2009-03-05 23:01:23 +0000583static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000584{
aliguori376253e2009-03-05 23:01:23 +0000585 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000586 switch(c) {
587 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000588 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000589 break;
590 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000591 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000592 break;
593 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000594 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000595 break;
596 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000597 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000598 break;
599 default:
600 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000601 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000602 } else {
aliguori376253e2009-03-05 23:01:23 +0000603 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000604 }
605 break;
606 }
aliguori376253e2009-03-05 23:01:23 +0000607 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000608}
609
aliguori376253e2009-03-05 23:01:23 +0000610static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000611 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000612{
bellard6a00d602005-11-21 23:25:50 +0000613 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000614 int nb_per_line, l, line_size, i, max_digits, len;
615 uint8_t buf[16];
616 uint64_t v;
617
618 if (format == 'i') {
619 int flags;
620 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000621 env = mon_get_cpu();
622 if (!env && !is_physical)
623 return;
bellard9307c4c2004-04-04 12:57:25 +0000624#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000625 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000626 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000627 } else if (wsize == 4) {
628 flags = 0;
629 } else {
bellard6a15fd12006-04-12 21:07:07 +0000630 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000631 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000632 if (env) {
633#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000634 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000635 (env->segs[R_CS].flags & DESC_L_MASK))
636 flags = 2;
637 else
638#endif
639 if (!(env->segs[R_CS].flags & DESC_B_MASK))
640 flags = 1;
641 }
bellard4c27ba22004-04-25 18:05:08 +0000642 }
643#endif
aliguori376253e2009-03-05 23:01:23 +0000644 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000645 return;
646 }
647
648 len = wsize * count;
649 if (wsize == 1)
650 line_size = 8;
651 else
652 line_size = 16;
653 nb_per_line = line_size / wsize;
654 max_digits = 0;
655
656 switch(format) {
657 case 'o':
658 max_digits = (wsize * 8 + 2) / 3;
659 break;
660 default:
661 case 'x':
662 max_digits = (wsize * 8) / 4;
663 break;
664 case 'u':
665 case 'd':
666 max_digits = (wsize * 8 * 10 + 32) / 33;
667 break;
668 case 'c':
669 wsize = 1;
670 break;
671 }
672
673 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000674 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000675 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000676 else
aliguori376253e2009-03-05 23:01:23 +0000677 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000678 l = len;
679 if (l > line_size)
680 l = line_size;
681 if (is_physical) {
682 cpu_physical_memory_rw(addr, buf, l, 0);
683 } else {
bellard6a00d602005-11-21 23:25:50 +0000684 env = mon_get_cpu();
685 if (!env)
686 break;
aliguoric8f79b62008-08-18 14:00:20 +0000687 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000688 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000689 break;
690 }
bellard9307c4c2004-04-04 12:57:25 +0000691 }
ths5fafdf22007-09-16 21:08:06 +0000692 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000693 while (i < l) {
694 switch(wsize) {
695 default:
696 case 1:
697 v = ldub_raw(buf + i);
698 break;
699 case 2:
700 v = lduw_raw(buf + i);
701 break;
702 case 4:
bellard92a31b12005-02-10 22:00:52 +0000703 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000704 break;
705 case 8:
706 v = ldq_raw(buf + i);
707 break;
708 }
aliguori376253e2009-03-05 23:01:23 +0000709 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000710 switch(format) {
711 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000712 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000713 break;
714 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000715 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000716 break;
717 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000718 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000719 break;
720 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000721 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000722 break;
723 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000724 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000725 break;
726 }
727 i += wsize;
728 }
aliguori376253e2009-03-05 23:01:23 +0000729 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000730 addr += l;
731 len -= l;
732 }
733}
734
bellard92a31b12005-02-10 22:00:52 +0000735#if TARGET_LONG_BITS == 64
736#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
737#else
738#define GET_TLONG(h, l) (l)
739#endif
740
aliguori376253e2009-03-05 23:01:23 +0000741static void do_memory_dump(Monitor *mon, int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000742 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000743{
bellard92a31b12005-02-10 22:00:52 +0000744 target_long addr = GET_TLONG(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000745 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000746}
747
blueswir17743e582007-09-24 18:39:04 +0000748#if TARGET_PHYS_ADDR_BITS > 32
749#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
750#else
751#define GET_TPHYSADDR(h, l) (l)
752#endif
753
aliguori376253e2009-03-05 23:01:23 +0000754static void do_physical_memory_dump(Monitor *mon, int count, int format,
755 int size, uint32_t addrh, uint32_t addrl)
bellard92a31b12005-02-10 22:00:52 +0000756
bellard9307c4c2004-04-04 12:57:25 +0000757{
blueswir17743e582007-09-24 18:39:04 +0000758 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000759 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000760}
761
aliguori376253e2009-03-05 23:01:23 +0000762static void do_print(Monitor *mon, int count, int format, int size,
763 unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000764{
blueswir17743e582007-09-24 18:39:04 +0000765 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
766#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000767 switch(format) {
768 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000769 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000770 break;
771 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000772 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000773 break;
774 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000775 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000776 break;
777 default:
778 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000779 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000780 break;
781 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000782 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000783 break;
784 }
bellard92a31b12005-02-10 22:00:52 +0000785#else
786 switch(format) {
787 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000788 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000789 break;
790 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000791 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000792 break;
793 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000794 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000795 break;
796 default:
797 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000798 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000799 break;
800 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000801 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000802 break;
803 }
804#endif
aliguori376253e2009-03-05 23:01:23 +0000805 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000806}
807
aliguori376253e2009-03-05 23:01:23 +0000808static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000809 uint32_t size, const char *filename)
810{
811 FILE *f;
812 target_long addr = GET_TLONG(valh, vall);
813 uint32_t l;
814 CPUState *env;
815 uint8_t buf[1024];
816
817 env = mon_get_cpu();
818 if (!env)
819 return;
820
821 f = fopen(filename, "wb");
822 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000823 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000824 return;
825 }
826 while (size != 0) {
827 l = sizeof(buf);
828 if (l > size)
829 l = size;
830 cpu_memory_rw_debug(env, addr, buf, l, 0);
831 fwrite(buf, 1, l, f);
832 addr += l;
833 size -= l;
834 }
835 fclose(f);
836}
837
aliguori376253e2009-03-05 23:01:23 +0000838static void do_physical_memory_save(Monitor *mon, unsigned int valh,
839 unsigned int vall, uint32_t size,
840 const char *filename)
aurel32a8bdf7a2008-04-11 21:36:14 +0000841{
842 FILE *f;
843 uint32_t l;
844 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000845 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000846
847 f = fopen(filename, "wb");
848 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000849 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000850 return;
851 }
852 while (size != 0) {
853 l = sizeof(buf);
854 if (l > size)
855 l = size;
856 cpu_physical_memory_rw(addr, buf, l, 0);
857 fwrite(buf, 1, l, f);
858 fflush(f);
859 addr += l;
860 size -= l;
861 }
862 fclose(f);
863}
864
aliguori376253e2009-03-05 23:01:23 +0000865static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
bellarde4cf1ad2005-06-04 20:15:57 +0000866{
867 uint32_t addr;
868 uint8_t buf[1];
869 uint16_t sum;
870
871 sum = 0;
872 for(addr = start; addr < (start + size); addr++) {
873 cpu_physical_memory_rw(addr, buf, 1, 0);
874 /* BSD sum algorithm ('sum' Unix command) */
875 sum = (sum >> 1) | (sum << 15);
876 sum += buf[0];
877 }
aliguori376253e2009-03-05 23:01:23 +0000878 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000879}
880
bellarda3a91a32004-06-04 11:06:21 +0000881typedef struct {
882 int keycode;
883 const char *name;
884} KeyDef;
885
886static const KeyDef key_defs[] = {
887 { 0x2a, "shift" },
888 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000889
bellarda3a91a32004-06-04 11:06:21 +0000890 { 0x38, "alt" },
891 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000892 { 0x64, "altgr" },
893 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000894 { 0x1d, "ctrl" },
895 { 0x9d, "ctrl_r" },
896
897 { 0xdd, "menu" },
898
899 { 0x01, "esc" },
900
901 { 0x02, "1" },
902 { 0x03, "2" },
903 { 0x04, "3" },
904 { 0x05, "4" },
905 { 0x06, "5" },
906 { 0x07, "6" },
907 { 0x08, "7" },
908 { 0x09, "8" },
909 { 0x0a, "9" },
910 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000911 { 0x0c, "minus" },
912 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000913 { 0x0e, "backspace" },
914
915 { 0x0f, "tab" },
916 { 0x10, "q" },
917 { 0x11, "w" },
918 { 0x12, "e" },
919 { 0x13, "r" },
920 { 0x14, "t" },
921 { 0x15, "y" },
922 { 0x16, "u" },
923 { 0x17, "i" },
924 { 0x18, "o" },
925 { 0x19, "p" },
926
927 { 0x1c, "ret" },
928
929 { 0x1e, "a" },
930 { 0x1f, "s" },
931 { 0x20, "d" },
932 { 0x21, "f" },
933 { 0x22, "g" },
934 { 0x23, "h" },
935 { 0x24, "j" },
936 { 0x25, "k" },
937 { 0x26, "l" },
938
939 { 0x2c, "z" },
940 { 0x2d, "x" },
941 { 0x2e, "c" },
942 { 0x2f, "v" },
943 { 0x30, "b" },
944 { 0x31, "n" },
945 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000946 { 0x33, "comma" },
947 { 0x34, "dot" },
948 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000949
balrog4d3b6f62008-02-10 16:33:14 +0000950 { 0x37, "asterisk" },
951
bellarda3a91a32004-06-04 11:06:21 +0000952 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000953 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000954 { 0x3b, "f1" },
955 { 0x3c, "f2" },
956 { 0x3d, "f3" },
957 { 0x3e, "f4" },
958 { 0x3f, "f5" },
959 { 0x40, "f6" },
960 { 0x41, "f7" },
961 { 0x42, "f8" },
962 { 0x43, "f9" },
963 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +0000964 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000965 { 0x46, "scroll_lock" },
966
bellard64866c32006-05-07 18:03:31 +0000967 { 0xb5, "kp_divide" },
968 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +0000969 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +0000970 { 0x4e, "kp_add" },
971 { 0x9c, "kp_enter" },
972 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +0000973 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +0000974
975 { 0x52, "kp_0" },
976 { 0x4f, "kp_1" },
977 { 0x50, "kp_2" },
978 { 0x51, "kp_3" },
979 { 0x4b, "kp_4" },
980 { 0x4c, "kp_5" },
981 { 0x4d, "kp_6" },
982 { 0x47, "kp_7" },
983 { 0x48, "kp_8" },
984 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +0000985
bellarda3a91a32004-06-04 11:06:21 +0000986 { 0x56, "<" },
987
988 { 0x57, "f11" },
989 { 0x58, "f12" },
990
991 { 0xb7, "print" },
992
993 { 0xc7, "home" },
994 { 0xc9, "pgup" },
995 { 0xd1, "pgdn" },
996 { 0xcf, "end" },
997
998 { 0xcb, "left" },
999 { 0xc8, "up" },
1000 { 0xd0, "down" },
1001 { 0xcd, "right" },
1002
1003 { 0xd2, "insert" },
1004 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001005#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1006 { 0xf0, "stop" },
1007 { 0xf1, "again" },
1008 { 0xf2, "props" },
1009 { 0xf3, "undo" },
1010 { 0xf4, "front" },
1011 { 0xf5, "copy" },
1012 { 0xf6, "open" },
1013 { 0xf7, "paste" },
1014 { 0xf8, "find" },
1015 { 0xf9, "cut" },
1016 { 0xfa, "lf" },
1017 { 0xfb, "help" },
1018 { 0xfc, "meta_l" },
1019 { 0xfd, "meta_r" },
1020 { 0xfe, "compose" },
1021#endif
bellarda3a91a32004-06-04 11:06:21 +00001022 { 0, NULL },
1023};
1024
1025static int get_keycode(const char *key)
1026{
1027 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001028 char *endp;
1029 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001030
1031 for(p = key_defs; p->name != NULL; p++) {
1032 if (!strcmp(key, p->name))
1033 return p->keycode;
1034 }
bellard64866c32006-05-07 18:03:31 +00001035 if (strstart(key, "0x", NULL)) {
1036 ret = strtoul(key, &endp, 0);
1037 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1038 return ret;
1039 }
bellarda3a91a32004-06-04 11:06:21 +00001040 return -1;
1041}
1042
balrogc8256f92008-06-08 22:45:01 +00001043#define MAX_KEYCODES 16
1044static uint8_t keycodes[MAX_KEYCODES];
1045static int nb_pending_keycodes;
1046static QEMUTimer *key_timer;
1047
1048static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001049{
balrogc8256f92008-06-08 22:45:01 +00001050 int keycode;
1051
1052 while (nb_pending_keycodes > 0) {
1053 nb_pending_keycodes--;
1054 keycode = keycodes[nb_pending_keycodes];
1055 if (keycode & 0x80)
1056 kbd_put_keycode(0xe0);
1057 kbd_put_keycode(keycode | 0x80);
1058 }
1059}
1060
aliguori376253e2009-03-05 23:01:23 +00001061static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1062 int hold_time)
balrogc8256f92008-06-08 22:45:01 +00001063{
balrog3401c0d2008-06-04 10:05:59 +00001064 char keyname_buf[16];
1065 char *separator;
1066 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +00001067
balrogc8256f92008-06-08 22:45:01 +00001068 if (nb_pending_keycodes > 0) {
1069 qemu_del_timer(key_timer);
1070 release_keys(NULL);
1071 }
1072 if (!has_hold_time)
1073 hold_time = 100;
1074 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001075 while (1) {
1076 separator = strchr(string, '-');
1077 keyname_len = separator ? separator - string : strlen(string);
1078 if (keyname_len > 0) {
1079 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1080 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001081 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001082 return;
bellarda3a91a32004-06-04 11:06:21 +00001083 }
balrogc8256f92008-06-08 22:45:01 +00001084 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001085 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001086 return;
1087 }
1088 keyname_buf[keyname_len] = 0;
1089 keycode = get_keycode(keyname_buf);
1090 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001091 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001092 return;
1093 }
balrogc8256f92008-06-08 22:45:01 +00001094 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001095 }
balrog3401c0d2008-06-04 10:05:59 +00001096 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001097 break;
balrog3401c0d2008-06-04 10:05:59 +00001098 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001099 }
balrogc8256f92008-06-08 22:45:01 +00001100 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001101 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001102 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001103 keycode = keycodes[i];
1104 if (keycode & 0x80)
1105 kbd_put_keycode(0xe0);
1106 kbd_put_keycode(keycode & 0x7f);
1107 }
balrogc8256f92008-06-08 22:45:01 +00001108 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001109 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1110 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001111}
1112
bellard13224a82006-07-14 22:03:35 +00001113static int mouse_button_state;
1114
aliguori376253e2009-03-05 23:01:23 +00001115static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001116 const char *dz_str)
1117{
1118 int dx, dy, dz;
1119 dx = strtol(dx_str, NULL, 0);
1120 dy = strtol(dy_str, NULL, 0);
1121 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001122 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001123 dz = strtol(dz_str, NULL, 0);
1124 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1125}
1126
aliguori376253e2009-03-05 23:01:23 +00001127static void do_mouse_button(Monitor *mon, int button_state)
bellard13224a82006-07-14 22:03:35 +00001128{
1129 mouse_button_state = button_state;
1130 kbd_mouse_event(0, 0, 0, mouse_button_state);
1131}
1132
aliguori376253e2009-03-05 23:01:23 +00001133static void do_ioport_read(Monitor *mon, int count, int format, int size,
1134 int addr, int has_index, int index)
bellard34405572004-06-08 00:55:58 +00001135{
1136 uint32_t val;
1137 int suffix;
1138
1139 if (has_index) {
1140 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1141 addr++;
1142 }
1143 addr &= 0xffff;
1144
1145 switch(size) {
1146 default:
1147 case 1:
1148 val = cpu_inb(NULL, addr);
1149 suffix = 'b';
1150 break;
1151 case 2:
1152 val = cpu_inw(NULL, addr);
1153 suffix = 'w';
1154 break;
1155 case 4:
1156 val = cpu_inl(NULL, addr);
1157 suffix = 'l';
1158 break;
1159 }
aliguori376253e2009-03-05 23:01:23 +00001160 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1161 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001162}
bellarda3a91a32004-06-04 11:06:21 +00001163
blueswir13b4366d2008-06-20 16:25:06 +00001164/* boot_set handler */
1165static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1166static void *boot_opaque;
1167
1168void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1169{
1170 qemu_boot_set_handler = func;
1171 boot_opaque = opaque;
1172}
1173
aliguori376253e2009-03-05 23:01:23 +00001174static void do_boot_set(Monitor *mon, const char *bootdevice)
aurel320ecdffb2008-05-04 20:11:34 +00001175{
1176 int res;
1177
1178 if (qemu_boot_set_handler) {
blueswir13b4366d2008-06-20 16:25:06 +00001179 res = qemu_boot_set_handler(boot_opaque, bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001180 if (res == 0)
aliguori376253e2009-03-05 23:01:23 +00001181 monitor_printf(mon, "boot device list now set to %s\n",
1182 bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001183 else
aliguori376253e2009-03-05 23:01:23 +00001184 monitor_printf(mon, "setting boot device list failed with "
1185 "error %i\n", res);
aurel320ecdffb2008-05-04 20:11:34 +00001186 } else {
aliguori376253e2009-03-05 23:01:23 +00001187 monitor_printf(mon, "no function defined to set boot device list for "
1188 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001189 }
1190}
1191
aliguori376253e2009-03-05 23:01:23 +00001192static void do_system_reset(Monitor *mon)
bellarde4f90822004-06-20 12:35:44 +00001193{
1194 qemu_system_reset_request();
1195}
1196
aliguori376253e2009-03-05 23:01:23 +00001197static void do_system_powerdown(Monitor *mon)
bellard34751872005-07-02 14:31:34 +00001198{
1199 qemu_system_powerdown_request();
1200}
1201
bellardb86bda52004-09-18 19:32:46 +00001202#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001203static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001204{
aliguori376253e2009-03-05 23:01:23 +00001205 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1206 addr,
1207 pte & mask,
1208 pte & PG_GLOBAL_MASK ? 'G' : '-',
1209 pte & PG_PSE_MASK ? 'P' : '-',
1210 pte & PG_DIRTY_MASK ? 'D' : '-',
1211 pte & PG_ACCESSED_MASK ? 'A' : '-',
1212 pte & PG_PCD_MASK ? 'C' : '-',
1213 pte & PG_PWT_MASK ? 'T' : '-',
1214 pte & PG_USER_MASK ? 'U' : '-',
1215 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001216}
1217
aliguori376253e2009-03-05 23:01:23 +00001218static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001219{
bellard6a00d602005-11-21 23:25:50 +00001220 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001221 int l1, l2;
1222 uint32_t pgd, pde, pte;
1223
bellard6a00d602005-11-21 23:25:50 +00001224 env = mon_get_cpu();
1225 if (!env)
1226 return;
1227
bellardb86bda52004-09-18 19:32:46 +00001228 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001229 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001230 return;
1231 }
1232 pgd = env->cr[3] & ~0xfff;
1233 for(l1 = 0; l1 < 1024; l1++) {
1234 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1235 pde = le32_to_cpu(pde);
1236 if (pde & PG_PRESENT_MASK) {
1237 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001238 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001239 } else {
1240 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001241 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001242 (uint8_t *)&pte, 4);
1243 pte = le32_to_cpu(pte);
1244 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001245 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001246 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001247 ~0xfff);
1248 }
1249 }
1250 }
1251 }
1252 }
1253}
1254
aliguori376253e2009-03-05 23:01:23 +00001255static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001256 uint32_t end, int prot)
1257{
bellard9746b152004-11-11 18:30:24 +00001258 int prot1;
1259 prot1 = *plast_prot;
1260 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001261 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001262 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1263 *pstart, end, end - *pstart,
1264 prot1 & PG_USER_MASK ? 'u' : '-',
1265 'r',
1266 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001267 }
1268 if (prot != 0)
1269 *pstart = end;
1270 else
1271 *pstart = -1;
1272 *plast_prot = prot;
1273 }
1274}
1275
aliguori376253e2009-03-05 23:01:23 +00001276static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001277{
bellard6a00d602005-11-21 23:25:50 +00001278 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001279 int l1, l2, prot, last_prot;
1280 uint32_t pgd, pde, pte, start, end;
1281
bellard6a00d602005-11-21 23:25:50 +00001282 env = mon_get_cpu();
1283 if (!env)
1284 return;
1285
bellardb86bda52004-09-18 19:32:46 +00001286 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001287 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001288 return;
1289 }
1290 pgd = env->cr[3] & ~0xfff;
1291 last_prot = 0;
1292 start = -1;
1293 for(l1 = 0; l1 < 1024; l1++) {
1294 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1295 pde = le32_to_cpu(pde);
1296 end = l1 << 22;
1297 if (pde & PG_PRESENT_MASK) {
1298 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1299 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001300 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001301 } else {
1302 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001303 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001304 (uint8_t *)&pte, 4);
1305 pte = le32_to_cpu(pte);
1306 end = (l1 << 22) + (l2 << 12);
1307 if (pte & PG_PRESENT_MASK) {
1308 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1309 } else {
1310 prot = 0;
1311 }
aliguori376253e2009-03-05 23:01:23 +00001312 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001313 }
1314 }
1315 } else {
1316 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001317 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001318 }
1319 }
1320}
1321#endif
1322
aurel327c664e22009-03-03 06:12:22 +00001323#if defined(TARGET_SH4)
1324
aliguori376253e2009-03-05 23:01:23 +00001325static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001326{
aliguori376253e2009-03-05 23:01:23 +00001327 monitor_printf(mon, " tlb%i:\t"
1328 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1329 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1330 "dirty=%hhu writethrough=%hhu\n",
1331 idx,
1332 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1333 tlb->v, tlb->sh, tlb->c, tlb->pr,
1334 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001335}
1336
aliguori376253e2009-03-05 23:01:23 +00001337static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001338{
1339 CPUState *env = mon_get_cpu();
1340 int i;
1341
aliguori376253e2009-03-05 23:01:23 +00001342 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001343 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001344 print_tlb (mon, i, &env->itlb[i]);
1345 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001346 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001347 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001348}
1349
1350#endif
1351
aliguori376253e2009-03-05 23:01:23 +00001352static void do_info_kqemu(Monitor *mon)
bellard0f4c6412005-07-24 18:10:56 +00001353{
1354#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001355 CPUState *env;
bellard0f4c6412005-07-24 18:10:56 +00001356 int val;
1357 val = 0;
bellard6a00d602005-11-21 23:25:50 +00001358 env = mon_get_cpu();
1359 if (!env) {
aliguori376253e2009-03-05 23:01:23 +00001360 monitor_printf(mon, "No cpu initialized yet");
bellard6a00d602005-11-21 23:25:50 +00001361 return;
1362 }
1363 val = env->kqemu_enabled;
aliguori376253e2009-03-05 23:01:23 +00001364 monitor_printf(mon, "kqemu support: ");
bellard5f1ce942006-02-08 22:40:15 +00001365 switch(val) {
1366 default:
1367 case 0:
aliguori376253e2009-03-05 23:01:23 +00001368 monitor_printf(mon, "disabled\n");
bellard5f1ce942006-02-08 22:40:15 +00001369 break;
1370 case 1:
aliguori376253e2009-03-05 23:01:23 +00001371 monitor_printf(mon, "enabled for user code\n");
bellard5f1ce942006-02-08 22:40:15 +00001372 break;
1373 case 2:
aliguori376253e2009-03-05 23:01:23 +00001374 monitor_printf(mon, "enabled for user and kernel code\n");
bellard5f1ce942006-02-08 22:40:15 +00001375 break;
1376 }
bellard0f4c6412005-07-24 18:10:56 +00001377#else
aliguori376253e2009-03-05 23:01:23 +00001378 monitor_printf(mon, "kqemu support: not compiled\n");
bellard0f4c6412005-07-24 18:10:56 +00001379#endif
ths5fafdf22007-09-16 21:08:06 +00001380}
bellard0f4c6412005-07-24 18:10:56 +00001381
aliguori376253e2009-03-05 23:01:23 +00001382static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001383{
1384#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001385 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001386 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001387 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001388 else
aliguori376253e2009-03-05 23:01:23 +00001389 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001390#else
aliguori376253e2009-03-05 23:01:23 +00001391 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001392#endif
1393}
1394
bellard5f1ce942006-02-08 22:40:15 +00001395#ifdef CONFIG_PROFILER
1396
1397int64_t kqemu_time;
1398int64_t qemu_time;
1399int64_t kqemu_exec_count;
1400int64_t dev_time;
1401int64_t kqemu_ret_int_count;
1402int64_t kqemu_ret_excp_count;
1403int64_t kqemu_ret_intr_count;
1404
aliguori376253e2009-03-05 23:01:23 +00001405static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001406{
1407 int64_t total;
1408 total = qemu_time;
1409 if (total == 0)
1410 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001411 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1412 dev_time, dev_time / (double)ticks_per_sec);
1413 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1414 qemu_time, qemu_time / (double)ticks_per_sec);
1415 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1416 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1417 PRId64 "\n",
1418 kqemu_time, kqemu_time / (double)ticks_per_sec,
1419 kqemu_time / (double)total * 100.0,
1420 kqemu_exec_count,
1421 kqemu_ret_int_count,
1422 kqemu_ret_excp_count,
1423 kqemu_ret_intr_count);
bellard5f1ce942006-02-08 22:40:15 +00001424 qemu_time = 0;
1425 kqemu_time = 0;
1426 kqemu_exec_count = 0;
1427 dev_time = 0;
1428 kqemu_ret_int_count = 0;
1429 kqemu_ret_excp_count = 0;
1430 kqemu_ret_intr_count = 0;
1431#ifdef USE_KQEMU
1432 kqemu_record_dump();
1433#endif
1434}
1435#else
aliguori376253e2009-03-05 23:01:23 +00001436static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001437{
aliguori376253e2009-03-05 23:01:23 +00001438 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001439}
1440#endif
1441
bellardec36b692006-07-16 18:57:03 +00001442/* Capture support */
1443static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1444
aliguori376253e2009-03-05 23:01:23 +00001445static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001446{
1447 int i;
1448 CaptureState *s;
1449
1450 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001451 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001452 s->ops.info (s->opaque);
1453 }
1454}
1455
aliguori376253e2009-03-05 23:01:23 +00001456static void do_stop_capture(Monitor *mon, int n)
bellardec36b692006-07-16 18:57:03 +00001457{
1458 int i;
1459 CaptureState *s;
1460
1461 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1462 if (i == n) {
1463 s->ops.destroy (s->opaque);
1464 LIST_REMOVE (s, entries);
1465 qemu_free (s);
1466 return;
1467 }
1468 }
1469}
1470
1471#ifdef HAS_AUDIO
aliguori376253e2009-03-05 23:01:23 +00001472static void do_wav_capture(Monitor *mon, const char *path,
1473 int has_freq, int freq,
1474 int has_bits, int bits,
1475 int has_channels, int nchannels)
bellardec36b692006-07-16 18:57:03 +00001476{
1477 CaptureState *s;
1478
1479 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001480
1481 freq = has_freq ? freq : 44100;
1482 bits = has_bits ? bits : 16;
1483 nchannels = has_channels ? nchannels : 2;
1484
1485 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001486 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001487 qemu_free (s);
1488 }
1489 LIST_INSERT_HEAD (&capture_head, s, entries);
1490}
1491#endif
1492
aurel32dc1c0b72008-04-27 23:52:12 +00001493#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001494static void do_inject_nmi(Monitor *mon, int cpu_index)
aurel32dc1c0b72008-04-27 23:52:12 +00001495{
1496 CPUState *env;
1497
1498 for (env = first_cpu; env != NULL; env = env->next_cpu)
1499 if (env->cpu_index == cpu_index) {
1500 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1501 break;
1502 }
1503}
1504#endif
1505
aliguori376253e2009-03-05 23:01:23 +00001506static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001507{
1508 if (vm_running)
aliguori376253e2009-03-05 23:01:23 +00001509 monitor_printf(mon, "VM status: running\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001510 else
aliguori376253e2009-03-05 23:01:23 +00001511 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001512}
1513
1514
aliguori376253e2009-03-05 23:01:23 +00001515static void do_balloon(Monitor *mon, int value)
aliguoridf751fa2008-12-04 20:19:35 +00001516{
1517 ram_addr_t target = value;
1518 qemu_balloon(target << 20);
1519}
1520
aliguori376253e2009-03-05 23:01:23 +00001521static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001522{
1523 ram_addr_t actual;
1524
1525 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001526 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001527 monitor_printf(mon, "Using KVM without synchronous MMU, "
1528 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001529 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001530 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001531 else
aliguori376253e2009-03-05 23:01:23 +00001532 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001533}
1534
blueswir1d2c639d2009-01-24 18:19:25 +00001535/* Please update qemu-doc.texi when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001536static const mon_cmd_t mon_cmds[] = {
1537 { "help|?", "s?", help_cmd,
bellard9dc39cb2004-03-14 21:38:27 +00001538 "[cmd]", "show the help" },
ths5fafdf22007-09-16 21:08:06 +00001539 { "commit", "s", do_commit,
bellard7954c732006-08-01 15:52:40 +00001540 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
bellard9307c4c2004-04-04 12:57:25 +00001541 { "info", "s?", do_info,
bellard9dc39cb2004-03-14 21:38:27 +00001542 "subcommand", "show various information about the system state" },
bellard9307c4c2004-04-04 12:57:25 +00001543 { "q|quit", "", do_quit,
bellard9dc39cb2004-03-14 21:38:27 +00001544 "", "quit the emulator" },
bellard81d09122004-07-14 17:21:37 +00001545 { "eject", "-fB", do_eject,
thse5987522007-03-30 18:58:01 +00001546 "[-f] device", "eject a removable medium (use -f to force it)" },
aurel322ecea9b2008-06-18 22:10:01 +00001547 { "change", "BFs?", do_change,
1548 "device filename [format]", "change a removable medium, optional format" },
ths5fafdf22007-09-16 21:08:06 +00001549 { "screendump", "F", do_screen_dump,
bellard59a983b2004-03-17 23:17:16 +00001550 "filename", "save screen into PPM image 'filename'" },
balrog788228c2008-05-24 23:15:46 +00001551 { "logfile", "F", do_logfile,
pbrooke735b912007-06-30 13:53:24 +00001552 "filename", "output logs to 'filename'" },
bellard9307c4c2004-04-04 12:57:25 +00001553 { "log", "s", do_log,
ths5fafdf22007-09-16 21:08:06 +00001554 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
bellardfaea38e2006-08-05 21:31:00 +00001555 { "savevm", "s?", do_savevm,
ths5fafdf22007-09-16 21:08:06 +00001556 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
bellardfaea38e2006-08-05 21:31:00 +00001557 { "loadvm", "s", do_loadvm,
ths5fafdf22007-09-16 21:08:06 +00001558 "tag|id", "restore a VM snapshot from its tag or id" },
bellardfaea38e2006-08-05 21:31:00 +00001559 { "delvm", "s", do_delvm,
ths5fafdf22007-09-16 21:08:06 +00001560 "tag|id", "delete a VM snapshot from its tag or id" },
1561 { "stop", "", do_stop,
bellard9307c4c2004-04-04 12:57:25 +00001562 "", "stop emulation", },
ths5fafdf22007-09-16 21:08:06 +00001563 { "c|cont", "", do_cont,
bellard9307c4c2004-04-04 12:57:25 +00001564 "", "resume emulation", },
bellard67b915a2004-03-31 23:37:16 +00001565#ifdef CONFIG_GDBSTUB
ths5fafdf22007-09-16 21:08:06 +00001566 { "gdbserver", "s?", do_gdbserver,
bellard9307c4c2004-04-04 12:57:25 +00001567 "[port]", "start gdbserver session (default port=1234)", },
bellard67b915a2004-03-31 23:37:16 +00001568#endif
ths5fafdf22007-09-16 21:08:06 +00001569 { "x", "/l", do_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001570 "/fmt addr", "virtual memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001571 { "xp", "/l", do_physical_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001572 "/fmt addr", "physical memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001573 { "p|print", "/l", do_print,
bellard9307c4c2004-04-04 12:57:25 +00001574 "/fmt expr", "print expression value (use $reg for CPU register access)", },
ths5fafdf22007-09-16 21:08:06 +00001575 { "i", "/ii.", do_ioport_read,
bellard34405572004-06-08 00:55:58 +00001576 "/fmt addr", "I/O port read" },
1577
balrogc8256f92008-06-08 22:45:01 +00001578 { "sendkey", "si?", do_sendkey,
1579 "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 +00001580 { "system_reset", "", do_system_reset,
bellarde4f90822004-06-20 12:35:44 +00001581 "", "reset the system" },
ths5fafdf22007-09-16 21:08:06 +00001582 { "system_powerdown", "", do_system_powerdown,
bellard34751872005-07-02 14:31:34 +00001583 "", "send system power down event" },
ths5fafdf22007-09-16 21:08:06 +00001584 { "sum", "ii", do_sum,
bellarde4cf1ad2005-06-04 20:15:57 +00001585 "addr size", "compute the checksum of a memory region" },
bellarda594cfb2005-11-06 16:13:29 +00001586 { "usb_add", "s", do_usb_add,
1587 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1588 { "usb_del", "s", do_usb_del,
1589 "device", "remove USB device 'bus.addr'" },
ths5fafdf22007-09-16 21:08:06 +00001590 { "cpu", "i", do_cpu_set,
bellard6a00d602005-11-21 23:25:50 +00001591 "index", "set the default CPU" },
ths5fafdf22007-09-16 21:08:06 +00001592 { "mouse_move", "sss?", do_mouse_move,
bellard13224a82006-07-14 22:03:35 +00001593 "dx dy [dz]", "send mouse move events" },
ths5fafdf22007-09-16 21:08:06 +00001594 { "mouse_button", "i", do_mouse_button,
bellard13224a82006-07-14 22:03:35 +00001595 "state", "change mouse button state (1=L, 2=M, 4=R)" },
ths455204e2007-01-05 16:42:13 +00001596 { "mouse_set", "i", do_mouse_set,
1597 "index", "set which mouse device receives events" },
bellardec36b692006-07-16 18:57:03 +00001598#ifdef HAS_AUDIO
1599 { "wavcapture", "si?i?i?", do_wav_capture,
1600 "path [frequency bits channels]",
1601 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1602#endif
blueswir1d2c639d2009-01-24 18:19:25 +00001603 { "stopcapture", "i", do_stop_capture,
1604 "capture index", "stop capture" },
ths5fafdf22007-09-16 21:08:06 +00001605 { "memsave", "lis", do_memory_save,
bellardb371dc52007-01-03 15:20:39 +00001606 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
aurel32a8bdf7a2008-04-11 21:36:14 +00001607 { "pmemsave", "lis", do_physical_memory_save,
1608 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
aurel320ecdffb2008-05-04 20:11:34 +00001609 { "boot_set", "s", do_boot_set,
1610 "bootdevice", "define new values for the boot device list" },
aurel32dc1c0b72008-04-27 23:52:12 +00001611#if defined(TARGET_I386)
1612 { "nmi", "i", do_inject_nmi,
1613 "cpu", "inject an NMI on the given CPU", },
1614#endif
aliguori5bb79102008-10-13 03:12:02 +00001615 { "migrate", "-ds", do_migrate,
1616 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1617 { "migrate_cancel", "", do_migrate_cancel,
1618 "", "cancel the current VM migration" },
1619 { "migrate_set_speed", "s", do_migrate_set_speed,
1620 "value", "set maximum speed (in bytes) for migrations" },
aliguori6f338c32009-02-11 15:21:54 +00001621#if defined(TARGET_I386)
1622 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1623 "[file=file][,if=type][,bus=n]\n"
1624 "[,unit=m][,media=d][index=i]\n"
1625 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1626 "[snapshot=on|off][,cache=on|off]",
1627 "add drive to PCI storage controller" },
1628 { "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" },
1629 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1630 { "host_net_add", "ss", net_host_device_add,
1631 "[tap,user,socket,vde] options", "add host VLAN client" },
1632 { "host_net_remove", "is", net_host_device_remove,
1633 "vlan_id name", "remove host VLAN client" },
1634#endif
aliguoridf751fa2008-12-04 20:19:35 +00001635 { "balloon", "i", do_balloon,
1636 "target", "request VM to change it's memory allocation (in MB)" },
aliguorif029bd92009-02-11 15:19:16 +00001637 { "set_link", "ss", do_set_link,
1638 "name [up|down]", "change the link status of a network adapter" },
ths5fafdf22007-09-16 21:08:06 +00001639 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001640};
1641
blueswir1d2c639d2009-01-24 18:19:25 +00001642/* Please update qemu-doc.texi when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001643static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001644 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001645 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001646 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001647 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001648 { "chardev", "", qemu_chr_info,
1649 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001650 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001651 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001652 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001653 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001654 { "registers", "", do_info_registers,
1655 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001656 { "cpus", "", do_info_cpus,
1657 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001658 { "history", "", do_info_history,
1659 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001660 { "irq", "", irq_info,
1661 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001662 { "pic", "", pic_info,
1663 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001664 { "pci", "", pci_info,
1665 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001666#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001667 { "tlb", "", tlb_info,
1668 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001669#endif
1670#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001671 { "mem", "", mem_info,
1672 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001673 { "hpet", "", do_info_hpet,
1674 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001675#endif
bellarde3db7222005-01-26 22:00:47 +00001676 { "jit", "", do_info_jit,
1677 "", "show dynamic compiler info", },
bellard0f4c6412005-07-24 18:10:56 +00001678 { "kqemu", "", do_info_kqemu,
blueswir1d2c639d2009-01-24 18:19:25 +00001679 "", "show KQEMU information", },
aliguori7ba1e612008-11-05 16:04:33 +00001680 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001681 "", "show KVM information", },
bellarda594cfb2005-11-06 16:13:29 +00001682 { "usb", "", usb_info,
1683 "", "show guest USB devices", },
1684 { "usbhost", "", usb_host_info,
1685 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001686 { "profile", "", do_info_profile,
1687 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001688 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001689 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001690 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001691 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001692 { "status", "", do_info_status,
1693 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001694 { "pcmcia", "", pcmcia_info,
1695 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001696 { "mice", "", do_info_mice,
1697 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001698 { "vnc", "", do_info_vnc,
1699 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001700 { "name", "", do_info_name,
1701 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001702 { "uuid", "", do_info_uuid,
1703 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001704#if defined(TARGET_PPC)
1705 { "cpustats", "", do_info_cpu_stats,
1706 "", "show CPU statistics", },
1707#endif
blueswir131a60e22007-10-26 18:42:59 +00001708#if defined(CONFIG_SLIRP)
1709 { "slirp", "", do_info_slirp,
1710 "", "show SLIRP statistics", },
1711#endif
aliguori5bb79102008-10-13 03:12:02 +00001712 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001713 { "balloon", "", do_info_balloon,
1714 "", "show balloon information" },
bellard9dc39cb2004-03-14 21:38:27 +00001715 { NULL, NULL, },
1716};
1717
bellard9307c4c2004-04-04 12:57:25 +00001718/*******************************************************************/
1719
1720static const char *pch;
1721static jmp_buf expr_env;
1722
bellard92a31b12005-02-10 22:00:52 +00001723#define MD_TLONG 0
1724#define MD_I32 1
1725
bellard9307c4c2004-04-04 12:57:25 +00001726typedef struct MonitorDef {
1727 const char *name;
1728 int offset;
blueswir18662d652008-10-02 18:32:44 +00001729 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001730 int type;
bellard9307c4c2004-04-04 12:57:25 +00001731} MonitorDef;
1732
bellard57206fd2004-04-25 18:54:52 +00001733#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001734static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001735{
bellard6a00d602005-11-21 23:25:50 +00001736 CPUState *env = mon_get_cpu();
1737 if (!env)
1738 return 0;
1739 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001740}
1741#endif
1742
bellarda541f292004-04-12 20:39:29 +00001743#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001744static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001745{
bellard6a00d602005-11-21 23:25:50 +00001746 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001747 unsigned int u;
1748 int i;
1749
bellard6a00d602005-11-21 23:25:50 +00001750 if (!env)
1751 return 0;
1752
bellarda541f292004-04-12 20:39:29 +00001753 u = 0;
1754 for (i = 0; i < 8; i++)
bellard6a00d602005-11-21 23:25:50 +00001755 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001756
1757 return u;
1758}
1759
blueswir18662d652008-10-02 18:32:44 +00001760static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001761{
bellard6a00d602005-11-21 23:25:50 +00001762 CPUState *env = mon_get_cpu();
1763 if (!env)
1764 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001765 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001766}
1767
blueswir18662d652008-10-02 18:32:44 +00001768static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001769{
bellard6a00d602005-11-21 23:25:50 +00001770 CPUState *env = mon_get_cpu();
1771 if (!env)
1772 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001773 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001774}
bellard9fddaa02004-05-21 12:59:32 +00001775
blueswir18662d652008-10-02 18:32:44 +00001776static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001777{
bellard6a00d602005-11-21 23:25:50 +00001778 CPUState *env = mon_get_cpu();
1779 if (!env)
1780 return 0;
1781 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001782}
1783
blueswir18662d652008-10-02 18:32:44 +00001784static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001785{
bellard6a00d602005-11-21 23:25:50 +00001786 CPUState *env = mon_get_cpu();
1787 if (!env)
1788 return 0;
1789 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001790}
1791
blueswir18662d652008-10-02 18:32:44 +00001792static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001793{
bellard6a00d602005-11-21 23:25:50 +00001794 CPUState *env = mon_get_cpu();
1795 if (!env)
1796 return 0;
1797 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001798}
bellarda541f292004-04-12 20:39:29 +00001799#endif
1800
bellarde95c8d52004-09-30 22:22:08 +00001801#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001802#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001803static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001804{
bellard6a00d602005-11-21 23:25:50 +00001805 CPUState *env = mon_get_cpu();
1806 if (!env)
1807 return 0;
1808 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001809}
bellard7b936c02005-10-30 17:05:13 +00001810#endif
bellarde95c8d52004-09-30 22:22:08 +00001811
blueswir18662d652008-10-02 18:32:44 +00001812static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001813{
bellard6a00d602005-11-21 23:25:50 +00001814 CPUState *env = mon_get_cpu();
1815 if (!env)
1816 return 0;
1817 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001818}
1819#endif
1820
blueswir18662d652008-10-02 18:32:44 +00001821static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001822#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001823
1824#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001825 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001826 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001827 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001828
bellard9307c4c2004-04-04 12:57:25 +00001829 { "eax", offsetof(CPUState, regs[0]) },
1830 { "ecx", offsetof(CPUState, regs[1]) },
1831 { "edx", offsetof(CPUState, regs[2]) },
1832 { "ebx", offsetof(CPUState, regs[3]) },
1833 { "esp|sp", offsetof(CPUState, regs[4]) },
1834 { "ebp|fp", offsetof(CPUState, regs[5]) },
1835 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001836 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001837#ifdef TARGET_X86_64
1838 { "r8", offsetof(CPUState, regs[8]) },
1839 { "r9", offsetof(CPUState, regs[9]) },
1840 { "r10", offsetof(CPUState, regs[10]) },
1841 { "r11", offsetof(CPUState, regs[11]) },
1842 { "r12", offsetof(CPUState, regs[12]) },
1843 { "r13", offsetof(CPUState, regs[13]) },
1844 { "r14", offsetof(CPUState, regs[14]) },
1845 { "r15", offsetof(CPUState, regs[15]) },
1846#endif
bellard9307c4c2004-04-04 12:57:25 +00001847 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001848 { "eip", offsetof(CPUState, eip) },
1849 SEG("cs", R_CS)
1850 SEG("ds", R_DS)
1851 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001852 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001853 SEG("fs", R_FS)
1854 SEG("gs", R_GS)
1855 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00001856#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00001857 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00001858 { "r0", offsetof(CPUState, gpr[0]) },
1859 { "r1", offsetof(CPUState, gpr[1]) },
1860 { "r2", offsetof(CPUState, gpr[2]) },
1861 { "r3", offsetof(CPUState, gpr[3]) },
1862 { "r4", offsetof(CPUState, gpr[4]) },
1863 { "r5", offsetof(CPUState, gpr[5]) },
1864 { "r6", offsetof(CPUState, gpr[6]) },
1865 { "r7", offsetof(CPUState, gpr[7]) },
1866 { "r8", offsetof(CPUState, gpr[8]) },
1867 { "r9", offsetof(CPUState, gpr[9]) },
1868 { "r10", offsetof(CPUState, gpr[10]) },
1869 { "r11", offsetof(CPUState, gpr[11]) },
1870 { "r12", offsetof(CPUState, gpr[12]) },
1871 { "r13", offsetof(CPUState, gpr[13]) },
1872 { "r14", offsetof(CPUState, gpr[14]) },
1873 { "r15", offsetof(CPUState, gpr[15]) },
1874 { "r16", offsetof(CPUState, gpr[16]) },
1875 { "r17", offsetof(CPUState, gpr[17]) },
1876 { "r18", offsetof(CPUState, gpr[18]) },
1877 { "r19", offsetof(CPUState, gpr[19]) },
1878 { "r20", offsetof(CPUState, gpr[20]) },
1879 { "r21", offsetof(CPUState, gpr[21]) },
1880 { "r22", offsetof(CPUState, gpr[22]) },
1881 { "r23", offsetof(CPUState, gpr[23]) },
1882 { "r24", offsetof(CPUState, gpr[24]) },
1883 { "r25", offsetof(CPUState, gpr[25]) },
1884 { "r26", offsetof(CPUState, gpr[26]) },
1885 { "r27", offsetof(CPUState, gpr[27]) },
1886 { "r28", offsetof(CPUState, gpr[28]) },
1887 { "r29", offsetof(CPUState, gpr[29]) },
1888 { "r30", offsetof(CPUState, gpr[30]) },
1889 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00001890 /* Floating point registers */
1891 { "f0", offsetof(CPUState, fpr[0]) },
1892 { "f1", offsetof(CPUState, fpr[1]) },
1893 { "f2", offsetof(CPUState, fpr[2]) },
1894 { "f3", offsetof(CPUState, fpr[3]) },
1895 { "f4", offsetof(CPUState, fpr[4]) },
1896 { "f5", offsetof(CPUState, fpr[5]) },
1897 { "f6", offsetof(CPUState, fpr[6]) },
1898 { "f7", offsetof(CPUState, fpr[7]) },
1899 { "f8", offsetof(CPUState, fpr[8]) },
1900 { "f9", offsetof(CPUState, fpr[9]) },
1901 { "f10", offsetof(CPUState, fpr[10]) },
1902 { "f11", offsetof(CPUState, fpr[11]) },
1903 { "f12", offsetof(CPUState, fpr[12]) },
1904 { "f13", offsetof(CPUState, fpr[13]) },
1905 { "f14", offsetof(CPUState, fpr[14]) },
1906 { "f15", offsetof(CPUState, fpr[15]) },
1907 { "f16", offsetof(CPUState, fpr[16]) },
1908 { "f17", offsetof(CPUState, fpr[17]) },
1909 { "f18", offsetof(CPUState, fpr[18]) },
1910 { "f19", offsetof(CPUState, fpr[19]) },
1911 { "f20", offsetof(CPUState, fpr[20]) },
1912 { "f21", offsetof(CPUState, fpr[21]) },
1913 { "f22", offsetof(CPUState, fpr[22]) },
1914 { "f23", offsetof(CPUState, fpr[23]) },
1915 { "f24", offsetof(CPUState, fpr[24]) },
1916 { "f25", offsetof(CPUState, fpr[25]) },
1917 { "f26", offsetof(CPUState, fpr[26]) },
1918 { "f27", offsetof(CPUState, fpr[27]) },
1919 { "f28", offsetof(CPUState, fpr[28]) },
1920 { "f29", offsetof(CPUState, fpr[29]) },
1921 { "f30", offsetof(CPUState, fpr[30]) },
1922 { "f31", offsetof(CPUState, fpr[31]) },
1923 { "fpscr", offsetof(CPUState, fpscr) },
1924 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00001925 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00001926 { "lr", offsetof(CPUState, lr) },
1927 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00001928 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00001929 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00001930 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00001931 { "msr", 0, &monitor_get_msr, },
1932 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00001933 { "tbu", 0, &monitor_get_tbu, },
1934 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00001935#if defined(TARGET_PPC64)
1936 /* Address space register */
1937 { "asr", offsetof(CPUState, asr) },
1938#endif
1939 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00001940 { "sdr1", offsetof(CPUState, sdr1) },
1941 { "sr0", offsetof(CPUState, sr[0]) },
1942 { "sr1", offsetof(CPUState, sr[1]) },
1943 { "sr2", offsetof(CPUState, sr[2]) },
1944 { "sr3", offsetof(CPUState, sr[3]) },
1945 { "sr4", offsetof(CPUState, sr[4]) },
1946 { "sr5", offsetof(CPUState, sr[5]) },
1947 { "sr6", offsetof(CPUState, sr[6]) },
1948 { "sr7", offsetof(CPUState, sr[7]) },
1949 { "sr8", offsetof(CPUState, sr[8]) },
1950 { "sr9", offsetof(CPUState, sr[9]) },
1951 { "sr10", offsetof(CPUState, sr[10]) },
1952 { "sr11", offsetof(CPUState, sr[11]) },
1953 { "sr12", offsetof(CPUState, sr[12]) },
1954 { "sr13", offsetof(CPUState, sr[13]) },
1955 { "sr14", offsetof(CPUState, sr[14]) },
1956 { "sr15", offsetof(CPUState, sr[15]) },
1957 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00001958#elif defined(TARGET_SPARC)
1959 { "g0", offsetof(CPUState, gregs[0]) },
1960 { "g1", offsetof(CPUState, gregs[1]) },
1961 { "g2", offsetof(CPUState, gregs[2]) },
1962 { "g3", offsetof(CPUState, gregs[3]) },
1963 { "g4", offsetof(CPUState, gregs[4]) },
1964 { "g5", offsetof(CPUState, gregs[5]) },
1965 { "g6", offsetof(CPUState, gregs[6]) },
1966 { "g7", offsetof(CPUState, gregs[7]) },
1967 { "o0", 0, monitor_get_reg },
1968 { "o1", 1, monitor_get_reg },
1969 { "o2", 2, monitor_get_reg },
1970 { "o3", 3, monitor_get_reg },
1971 { "o4", 4, monitor_get_reg },
1972 { "o5", 5, monitor_get_reg },
1973 { "o6", 6, monitor_get_reg },
1974 { "o7", 7, monitor_get_reg },
1975 { "l0", 8, monitor_get_reg },
1976 { "l1", 9, monitor_get_reg },
1977 { "l2", 10, monitor_get_reg },
1978 { "l3", 11, monitor_get_reg },
1979 { "l4", 12, monitor_get_reg },
1980 { "l5", 13, monitor_get_reg },
1981 { "l6", 14, monitor_get_reg },
1982 { "l7", 15, monitor_get_reg },
1983 { "i0", 16, monitor_get_reg },
1984 { "i1", 17, monitor_get_reg },
1985 { "i2", 18, monitor_get_reg },
1986 { "i3", 19, monitor_get_reg },
1987 { "i4", 20, monitor_get_reg },
1988 { "i5", 21, monitor_get_reg },
1989 { "i6", 22, monitor_get_reg },
1990 { "i7", 23, monitor_get_reg },
1991 { "pc", offsetof(CPUState, pc) },
1992 { "npc", offsetof(CPUState, npc) },
1993 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00001994#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00001995 { "psr", 0, &monitor_get_psr, },
1996 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00001997#endif
bellarde95c8d52004-09-30 22:22:08 +00001998 { "tbr", offsetof(CPUState, tbr) },
1999 { "fsr", offsetof(CPUState, fsr) },
2000 { "f0", offsetof(CPUState, fpr[0]) },
2001 { "f1", offsetof(CPUState, fpr[1]) },
2002 { "f2", offsetof(CPUState, fpr[2]) },
2003 { "f3", offsetof(CPUState, fpr[3]) },
2004 { "f4", offsetof(CPUState, fpr[4]) },
2005 { "f5", offsetof(CPUState, fpr[5]) },
2006 { "f6", offsetof(CPUState, fpr[6]) },
2007 { "f7", offsetof(CPUState, fpr[7]) },
2008 { "f8", offsetof(CPUState, fpr[8]) },
2009 { "f9", offsetof(CPUState, fpr[9]) },
2010 { "f10", offsetof(CPUState, fpr[10]) },
2011 { "f11", offsetof(CPUState, fpr[11]) },
2012 { "f12", offsetof(CPUState, fpr[12]) },
2013 { "f13", offsetof(CPUState, fpr[13]) },
2014 { "f14", offsetof(CPUState, fpr[14]) },
2015 { "f15", offsetof(CPUState, fpr[15]) },
2016 { "f16", offsetof(CPUState, fpr[16]) },
2017 { "f17", offsetof(CPUState, fpr[17]) },
2018 { "f18", offsetof(CPUState, fpr[18]) },
2019 { "f19", offsetof(CPUState, fpr[19]) },
2020 { "f20", offsetof(CPUState, fpr[20]) },
2021 { "f21", offsetof(CPUState, fpr[21]) },
2022 { "f22", offsetof(CPUState, fpr[22]) },
2023 { "f23", offsetof(CPUState, fpr[23]) },
2024 { "f24", offsetof(CPUState, fpr[24]) },
2025 { "f25", offsetof(CPUState, fpr[25]) },
2026 { "f26", offsetof(CPUState, fpr[26]) },
2027 { "f27", offsetof(CPUState, fpr[27]) },
2028 { "f28", offsetof(CPUState, fpr[28]) },
2029 { "f29", offsetof(CPUState, fpr[29]) },
2030 { "f30", offsetof(CPUState, fpr[30]) },
2031 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002032#ifdef TARGET_SPARC64
2033 { "f32", offsetof(CPUState, fpr[32]) },
2034 { "f34", offsetof(CPUState, fpr[34]) },
2035 { "f36", offsetof(CPUState, fpr[36]) },
2036 { "f38", offsetof(CPUState, fpr[38]) },
2037 { "f40", offsetof(CPUState, fpr[40]) },
2038 { "f42", offsetof(CPUState, fpr[42]) },
2039 { "f44", offsetof(CPUState, fpr[44]) },
2040 { "f46", offsetof(CPUState, fpr[46]) },
2041 { "f48", offsetof(CPUState, fpr[48]) },
2042 { "f50", offsetof(CPUState, fpr[50]) },
2043 { "f52", offsetof(CPUState, fpr[52]) },
2044 { "f54", offsetof(CPUState, fpr[54]) },
2045 { "f56", offsetof(CPUState, fpr[56]) },
2046 { "f58", offsetof(CPUState, fpr[58]) },
2047 { "f60", offsetof(CPUState, fpr[60]) },
2048 { "f62", offsetof(CPUState, fpr[62]) },
2049 { "asi", offsetof(CPUState, asi) },
2050 { "pstate", offsetof(CPUState, pstate) },
2051 { "cansave", offsetof(CPUState, cansave) },
2052 { "canrestore", offsetof(CPUState, canrestore) },
2053 { "otherwin", offsetof(CPUState, otherwin) },
2054 { "wstate", offsetof(CPUState, wstate) },
2055 { "cleanwin", offsetof(CPUState, cleanwin) },
2056 { "fprs", offsetof(CPUState, fprs) },
2057#endif
bellard9307c4c2004-04-04 12:57:25 +00002058#endif
2059 { NULL },
2060};
2061
aliguori376253e2009-03-05 23:01:23 +00002062static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002063{
aliguori376253e2009-03-05 23:01:23 +00002064 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002065 longjmp(expr_env, 1);
2066}
2067
bellard6a00d602005-11-21 23:25:50 +00002068/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002069static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002070{
blueswir18662d652008-10-02 18:32:44 +00002071 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002072 void *ptr;
2073
bellard9307c4c2004-04-04 12:57:25 +00002074 for(md = monitor_defs; md->name != NULL; md++) {
2075 if (compare_cmd(name, md->name)) {
2076 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002077 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002078 } else {
bellard6a00d602005-11-21 23:25:50 +00002079 CPUState *env = mon_get_cpu();
2080 if (!env)
2081 return -2;
2082 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002083 switch(md->type) {
2084 case MD_I32:
2085 *pval = *(int32_t *)ptr;
2086 break;
2087 case MD_TLONG:
2088 *pval = *(target_long *)ptr;
2089 break;
2090 default:
2091 *pval = 0;
2092 break;
2093 }
bellard9307c4c2004-04-04 12:57:25 +00002094 }
2095 return 0;
2096 }
2097 }
2098 return -1;
2099}
2100
2101static void next(void)
2102{
2103 if (pch != '\0') {
2104 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002105 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002106 pch++;
2107 }
2108}
2109
aliguori376253e2009-03-05 23:01:23 +00002110static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002111
aliguori376253e2009-03-05 23:01:23 +00002112static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002113{
blueswir1c2efc952007-09-25 17:28:42 +00002114 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002115 char *p;
bellard6a00d602005-11-21 23:25:50 +00002116 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002117
2118 switch(*pch) {
2119 case '+':
2120 next();
aliguori376253e2009-03-05 23:01:23 +00002121 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002122 break;
2123 case '-':
2124 next();
aliguori376253e2009-03-05 23:01:23 +00002125 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002126 break;
2127 case '~':
2128 next();
aliguori376253e2009-03-05 23:01:23 +00002129 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002130 break;
2131 case '(':
2132 next();
aliguori376253e2009-03-05 23:01:23 +00002133 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002134 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002135 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002136 }
2137 next();
2138 break;
bellard81d09122004-07-14 17:21:37 +00002139 case '\'':
2140 pch++;
2141 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002142 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002143 n = *pch;
2144 pch++;
2145 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002146 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002147 next();
2148 break;
bellard9307c4c2004-04-04 12:57:25 +00002149 case '$':
2150 {
2151 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002152 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002153
bellard9307c4c2004-04-04 12:57:25 +00002154 pch++;
2155 q = buf;
2156 while ((*pch >= 'a' && *pch <= 'z') ||
2157 (*pch >= 'A' && *pch <= 'Z') ||
2158 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002159 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002160 if ((q - buf) < sizeof(buf) - 1)
2161 *q++ = *pch;
2162 pch++;
2163 }
blueswir1cd390082008-11-16 13:53:32 +00002164 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002165 pch++;
2166 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002167 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002168 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002169 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002170 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002171 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002172 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002173 }
2174 break;
2175 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002176 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002177 n = 0;
2178 break;
2179 default:
blueswir17743e582007-09-24 18:39:04 +00002180#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002181 n = strtoull(pch, &p, 0);
2182#else
bellard9307c4c2004-04-04 12:57:25 +00002183 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002184#endif
bellard9307c4c2004-04-04 12:57:25 +00002185 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002186 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002187 }
2188 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002189 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002190 pch++;
2191 break;
2192 }
2193 return n;
2194}
2195
2196
aliguori376253e2009-03-05 23:01:23 +00002197static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002198{
blueswir1c2efc952007-09-25 17:28:42 +00002199 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002200 int op;
ths3b46e622007-09-17 08:09:54 +00002201
aliguori376253e2009-03-05 23:01:23 +00002202 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002203 for(;;) {
2204 op = *pch;
2205 if (op != '*' && op != '/' && op != '%')
2206 break;
2207 next();
aliguori376253e2009-03-05 23:01:23 +00002208 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002209 switch(op) {
2210 default:
2211 case '*':
2212 val *= val2;
2213 break;
2214 case '/':
2215 case '%':
ths5fafdf22007-09-16 21:08:06 +00002216 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002217 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002218 if (op == '/')
2219 val /= val2;
2220 else
2221 val %= val2;
2222 break;
2223 }
2224 }
2225 return val;
2226}
2227
aliguori376253e2009-03-05 23:01:23 +00002228static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002229{
blueswir1c2efc952007-09-25 17:28:42 +00002230 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002231 int op;
bellard9307c4c2004-04-04 12:57:25 +00002232
aliguori376253e2009-03-05 23:01:23 +00002233 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002234 for(;;) {
2235 op = *pch;
2236 if (op != '&' && op != '|' && op != '^')
2237 break;
2238 next();
aliguori376253e2009-03-05 23:01:23 +00002239 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002240 switch(op) {
2241 default:
2242 case '&':
2243 val &= val2;
2244 break;
2245 case '|':
2246 val |= val2;
2247 break;
2248 case '^':
2249 val ^= val2;
2250 break;
2251 }
2252 }
2253 return val;
2254}
2255
aliguori376253e2009-03-05 23:01:23 +00002256static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002257{
blueswir1c2efc952007-09-25 17:28:42 +00002258 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002259 int op;
bellard9307c4c2004-04-04 12:57:25 +00002260
aliguori376253e2009-03-05 23:01:23 +00002261 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002262 for(;;) {
2263 op = *pch;
2264 if (op != '+' && op != '-')
2265 break;
2266 next();
aliguori376253e2009-03-05 23:01:23 +00002267 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002268 if (op == '+')
2269 val += val2;
2270 else
2271 val -= val2;
2272 }
2273 return val;
2274}
2275
aliguori376253e2009-03-05 23:01:23 +00002276static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002277{
2278 pch = *pp;
2279 if (setjmp(expr_env)) {
2280 *pp = pch;
2281 return -1;
2282 }
blueswir1cd390082008-11-16 13:53:32 +00002283 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002284 pch++;
aliguori376253e2009-03-05 23:01:23 +00002285 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002286 *pp = pch;
2287 return 0;
2288}
2289
2290static int get_str(char *buf, int buf_size, const char **pp)
2291{
2292 const char *p;
2293 char *q;
2294 int c;
2295
bellard81d09122004-07-14 17:21:37 +00002296 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002297 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002298 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002299 p++;
2300 if (*p == '\0') {
2301 fail:
bellard81d09122004-07-14 17:21:37 +00002302 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002303 *pp = p;
2304 return -1;
2305 }
bellard9307c4c2004-04-04 12:57:25 +00002306 if (*p == '\"') {
2307 p++;
2308 while (*p != '\0' && *p != '\"') {
2309 if (*p == '\\') {
2310 p++;
2311 c = *p++;
2312 switch(c) {
2313 case 'n':
2314 c = '\n';
2315 break;
2316 case 'r':
2317 c = '\r';
2318 break;
2319 case '\\':
2320 case '\'':
2321 case '\"':
2322 break;
2323 default:
2324 qemu_printf("unsupported escape code: '\\%c'\n", c);
2325 goto fail;
2326 }
2327 if ((q - buf) < buf_size - 1) {
2328 *q++ = c;
2329 }
2330 } else {
2331 if ((q - buf) < buf_size - 1) {
2332 *q++ = *p;
2333 }
2334 p++;
2335 }
2336 }
2337 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002338 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002339 goto fail;
2340 }
2341 p++;
2342 } else {
blueswir1cd390082008-11-16 13:53:32 +00002343 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002344 if ((q - buf) < buf_size - 1) {
2345 *q++ = *p;
2346 }
2347 p++;
2348 }
bellard9307c4c2004-04-04 12:57:25 +00002349 }
bellard81d09122004-07-14 17:21:37 +00002350 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002351 *pp = p;
2352 return 0;
2353}
2354
2355static int default_fmt_format = 'x';
2356static int default_fmt_size = 4;
2357
2358#define MAX_ARGS 16
2359
aliguori376253e2009-03-05 23:01:23 +00002360static void monitor_handle_command(Monitor *mon, const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002361{
2362 const char *p, *pstart, *typestr;
2363 char *q;
2364 int c, nb_args, len, i, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002365 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002366 char cmdname[256];
2367 char buf[1024];
2368 void *str_allocated[MAX_ARGS];
2369 void *args[MAX_ARGS];
aliguori376253e2009-03-05 23:01:23 +00002370 void (*handler_0)(Monitor *mon);
2371 void (*handler_1)(Monitor *mon, void *arg0);
2372 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2373 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2374 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2375 void *arg3);
2376 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2377 void *arg3, void *arg4);
2378 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2379 void *arg3, void *arg4, void *arg5);
2380 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2381 void *arg3, void *arg4, void *arg5, void *arg6);
bellard9dc39cb2004-03-14 21:38:27 +00002382
2383#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002384 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002385#endif
ths3b46e622007-09-17 08:09:54 +00002386
bellard9307c4c2004-04-04 12:57:25 +00002387 /* extract the command name */
bellard9dc39cb2004-03-14 21:38:27 +00002388 p = cmdline;
bellard9307c4c2004-04-04 12:57:25 +00002389 q = cmdname;
blueswir1cd390082008-11-16 13:53:32 +00002390 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002391 p++;
2392 if (*p == '\0')
bellard9dc39cb2004-03-14 21:38:27 +00002393 return;
bellard9307c4c2004-04-04 12:57:25 +00002394 pstart = p;
blueswir1cd390082008-11-16 13:53:32 +00002395 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002396 p++;
2397 len = p - pstart;
2398 if (len > sizeof(cmdname) - 1)
2399 len = sizeof(cmdname) - 1;
2400 memcpy(cmdname, pstart, len);
2401 cmdname[len] = '\0';
ths3b46e622007-09-17 08:09:54 +00002402
bellard9307c4c2004-04-04 12:57:25 +00002403 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002404 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002405 if (compare_cmd(cmdname, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +00002406 goto found;
2407 }
aliguori376253e2009-03-05 23:01:23 +00002408 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
bellard9dc39cb2004-03-14 21:38:27 +00002409 return;
2410 found:
bellard9307c4c2004-04-04 12:57:25 +00002411
2412 for(i = 0; i < MAX_ARGS; i++)
2413 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002414
bellard9307c4c2004-04-04 12:57:25 +00002415 /* parse the parameters */
2416 typestr = cmd->args_type;
2417 nb_args = 0;
2418 for(;;) {
2419 c = *typestr;
2420 if (c == '\0')
2421 break;
2422 typestr++;
2423 switch(c) {
2424 case 'F':
bellard81d09122004-07-14 17:21:37 +00002425 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002426 case 's':
2427 {
2428 int ret;
2429 char *str;
ths3b46e622007-09-17 08:09:54 +00002430
blueswir1cd390082008-11-16 13:53:32 +00002431 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002432 p++;
2433 if (*typestr == '?') {
2434 typestr++;
2435 if (*p == '\0') {
2436 /* no optional string: NULL argument */
2437 str = NULL;
2438 goto add_str;
2439 }
2440 }
2441 ret = get_str(buf, sizeof(buf), &p);
2442 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002443 switch(c) {
2444 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002445 monitor_printf(mon, "%s: filename expected\n",
2446 cmdname);
bellard81d09122004-07-14 17:21:37 +00002447 break;
2448 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002449 monitor_printf(mon, "%s: block device name expected\n",
2450 cmdname);
bellard81d09122004-07-14 17:21:37 +00002451 break;
2452 default:
aliguori376253e2009-03-05 23:01:23 +00002453 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002454 break;
2455 }
bellard9307c4c2004-04-04 12:57:25 +00002456 goto fail;
2457 }
2458 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002459 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002460 str_allocated[nb_args] = str;
2461 add_str:
2462 if (nb_args >= MAX_ARGS) {
2463 error_args:
aliguori376253e2009-03-05 23:01:23 +00002464 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002465 goto fail;
2466 }
2467 args[nb_args++] = str;
2468 }
2469 break;
2470 case '/':
2471 {
2472 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002473
blueswir1cd390082008-11-16 13:53:32 +00002474 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002475 p++;
2476 if (*p == '/') {
2477 /* format found */
2478 p++;
2479 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002480 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002481 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002482 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002483 count = count * 10 + (*p - '0');
2484 p++;
2485 }
2486 }
2487 size = -1;
2488 format = -1;
2489 for(;;) {
2490 switch(*p) {
2491 case 'o':
2492 case 'd':
2493 case 'u':
2494 case 'x':
2495 case 'i':
2496 case 'c':
2497 format = *p++;
2498 break;
2499 case 'b':
2500 size = 1;
2501 p++;
2502 break;
2503 case 'h':
2504 size = 2;
2505 p++;
2506 break;
2507 case 'w':
2508 size = 4;
2509 p++;
2510 break;
2511 case 'g':
2512 case 'L':
2513 size = 8;
2514 p++;
2515 break;
2516 default:
2517 goto next;
2518 }
2519 }
2520 next:
blueswir1cd390082008-11-16 13:53:32 +00002521 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002522 monitor_printf(mon, "invalid char in format: '%c'\n",
2523 *p);
bellard9307c4c2004-04-04 12:57:25 +00002524 goto fail;
2525 }
bellard9307c4c2004-04-04 12:57:25 +00002526 if (format < 0)
2527 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002528 if (format != 'i') {
2529 /* for 'i', not specifying a size gives -1 as size */
2530 if (size < 0)
2531 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002532 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002533 }
bellard9307c4c2004-04-04 12:57:25 +00002534 default_fmt_format = format;
2535 } else {
2536 count = 1;
2537 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002538 if (format != 'i') {
2539 size = default_fmt_size;
2540 } else {
2541 size = -1;
2542 }
bellard9307c4c2004-04-04 12:57:25 +00002543 }
2544 if (nb_args + 3 > MAX_ARGS)
2545 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002546 args[nb_args++] = (void*)(long)count;
2547 args[nb_args++] = (void*)(long)format;
2548 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002549 }
2550 break;
2551 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002552 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002553 {
blueswir1c2efc952007-09-25 17:28:42 +00002554 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002555
blueswir1cd390082008-11-16 13:53:32 +00002556 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002557 p++;
bellard34405572004-06-08 00:55:58 +00002558 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002559 if (*typestr == '?') {
2560 if (*p == '\0')
2561 has_arg = 0;
2562 else
2563 has_arg = 1;
2564 } else {
2565 if (*p == '.') {
2566 p++;
blueswir1cd390082008-11-16 13:53:32 +00002567 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002568 p++;
2569 has_arg = 1;
2570 } else {
2571 has_arg = 0;
2572 }
2573 }
bellard13224a82006-07-14 22:03:35 +00002574 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002575 if (nb_args >= MAX_ARGS)
2576 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002577 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002578 if (!has_arg) {
2579 if (nb_args >= MAX_ARGS)
2580 goto error_args;
2581 val = -1;
2582 goto add_num;
2583 }
2584 }
aliguori376253e2009-03-05 23:01:23 +00002585 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002586 goto fail;
2587 add_num:
bellard92a31b12005-02-10 22:00:52 +00002588 if (c == 'i') {
2589 if (nb_args >= MAX_ARGS)
2590 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002591 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002592 } else {
2593 if ((nb_args + 1) >= MAX_ARGS)
2594 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002595#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002596 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002597#else
2598 args[nb_args++] = (void *)0;
2599#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002600 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002601 }
bellard9307c4c2004-04-04 12:57:25 +00002602 }
2603 break;
2604 case '-':
2605 {
2606 int has_option;
2607 /* option */
ths3b46e622007-09-17 08:09:54 +00002608
bellard9307c4c2004-04-04 12:57:25 +00002609 c = *typestr++;
2610 if (c == '\0')
2611 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002612 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002613 p++;
2614 has_option = 0;
2615 if (*p == '-') {
2616 p++;
2617 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002618 monitor_printf(mon, "%s: unsupported option -%c\n",
2619 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002620 goto fail;
2621 }
2622 p++;
2623 has_option = 1;
2624 }
2625 if (nb_args >= MAX_ARGS)
2626 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002627 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002628 }
2629 break;
2630 default:
2631 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002632 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002633 goto fail;
2634 }
2635 }
2636 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002637 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002638 p++;
2639 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002640 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2641 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002642 goto fail;
2643 }
2644
2645 switch(nb_args) {
2646 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002647 handler_0 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002648 handler_0(mon);
bellard9307c4c2004-04-04 12:57:25 +00002649 break;
2650 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002651 handler_1 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002652 handler_1(mon, args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002653 break;
2654 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002655 handler_2 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002656 handler_2(mon, args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002657 break;
2658 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002659 handler_3 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002660 handler_3(mon, args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002661 break;
2662 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002663 handler_4 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002664 handler_4(mon, args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002665 break;
2666 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002667 handler_5 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002668 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002669 break;
bellard34405572004-06-08 00:55:58 +00002670 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002671 handler_6 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002672 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002673 break;
bellardec36b692006-07-16 18:57:03 +00002674 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002675 handler_7 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002676 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2677 args[6]);
bellardec36b692006-07-16 18:57:03 +00002678 break;
bellard9307c4c2004-04-04 12:57:25 +00002679 default:
aliguori376253e2009-03-05 23:01:23 +00002680 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
bellard9307c4c2004-04-04 12:57:25 +00002681 goto fail;
2682 }
2683 fail:
2684 for(i = 0; i < MAX_ARGS; i++)
2685 qemu_free(str_allocated[i]);
2686 return;
bellard9dc39cb2004-03-14 21:38:27 +00002687}
2688
bellard81d09122004-07-14 17:21:37 +00002689static void cmd_completion(const char *name, const char *list)
2690{
2691 const char *p, *pstart;
2692 char cmd[128];
2693 int len;
2694
2695 p = list;
2696 for(;;) {
2697 pstart = p;
2698 p = strchr(p, '|');
2699 if (!p)
2700 p = pstart + strlen(pstart);
2701 len = p - pstart;
2702 if (len > sizeof(cmd) - 2)
2703 len = sizeof(cmd) - 2;
2704 memcpy(cmd, pstart, len);
2705 cmd[len] = '\0';
2706 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002707 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002708 }
2709 if (*p == '\0')
2710 break;
2711 p++;
2712 }
2713}
2714
2715static void file_completion(const char *input)
2716{
2717 DIR *ffs;
2718 struct dirent *d;
2719 char path[1024];
2720 char file[1024], file_prefix[1024];
2721 int input_path_len;
2722 const char *p;
2723
ths5fafdf22007-09-16 21:08:06 +00002724 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002725 if (!p) {
2726 input_path_len = 0;
2727 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002728 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002729 } else {
2730 input_path_len = p - input + 1;
2731 memcpy(path, input, input_path_len);
2732 if (input_path_len > sizeof(path) - 1)
2733 input_path_len = sizeof(path) - 1;
2734 path[input_path_len] = '\0';
2735 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2736 }
2737#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002738 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2739 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002740#endif
2741 ffs = opendir(path);
2742 if (!ffs)
2743 return;
2744 for(;;) {
2745 struct stat sb;
2746 d = readdir(ffs);
2747 if (!d)
2748 break;
2749 if (strstart(d->d_name, file_prefix, NULL)) {
2750 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002751 if (input_path_len < sizeof(file))
2752 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2753 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002754 /* stat the file to find out if it's a directory.
2755 * In that case add a slash to speed up typing long paths
2756 */
2757 stat(file, &sb);
2758 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002759 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00002760 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002761 }
2762 }
2763 closedir(ffs);
2764}
2765
aliguori51de9762009-03-05 23:00:43 +00002766static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002767{
aliguori51de9762009-03-05 23:00:43 +00002768 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002769 const char *input = opaque;
2770
2771 if (input[0] == '\0' ||
2772 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00002773 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00002774 }
2775}
2776
2777/* NOTE: this parser is an approximate form of the real command parser */
2778static void parse_cmdline(const char *cmdline,
2779 int *pnb_args, char **args)
2780{
2781 const char *p;
2782 int nb_args, ret;
2783 char buf[1024];
2784
2785 p = cmdline;
2786 nb_args = 0;
2787 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002788 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002789 p++;
2790 if (*p == '\0')
2791 break;
2792 if (nb_args >= MAX_ARGS)
2793 break;
2794 ret = get_str(buf, sizeof(buf), &p);
2795 args[nb_args] = qemu_strdup(buf);
2796 nb_args++;
2797 if (ret < 0)
2798 break;
2799 }
2800 *pnb_args = nb_args;
2801}
2802
aliguori4c36ba32009-03-05 23:01:37 +00002803static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002804{
2805 const char *cmdname;
2806 char *args[MAX_ARGS];
2807 int nb_args, i, len;
2808 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00002809 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002810 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002811
2812 parse_cmdline(cmdline, &nb_args, args);
2813#ifdef DEBUG_COMPLETION
2814 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00002815 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00002816 }
2817#endif
2818
2819 /* if the line ends with a space, it means we want to complete the
2820 next arg */
2821 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002822 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002823 if (nb_args >= MAX_ARGS)
2824 return;
2825 args[nb_args++] = qemu_strdup("");
2826 }
2827 if (nb_args <= 1) {
2828 /* command completion */
2829 if (nb_args == 0)
2830 cmdname = "";
2831 else
2832 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00002833 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00002834 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002835 cmd_completion(cmdname, cmd->name);
2836 }
2837 } else {
2838 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002839 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002840 if (compare_cmd(args[0], cmd->name))
2841 goto found;
2842 }
2843 return;
2844 found:
2845 ptype = cmd->args_type;
2846 for(i = 0; i < nb_args - 2; i++) {
2847 if (*ptype != '\0') {
2848 ptype++;
2849 while (*ptype == '?')
2850 ptype++;
2851 }
2852 }
2853 str = args[nb_args - 1];
2854 switch(*ptype) {
2855 case 'F':
2856 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00002857 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00002858 file_completion(str);
2859 break;
2860 case 'B':
2861 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00002862 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00002863 bdrv_iterate(block_completion_it, (void *)str);
2864 break;
bellard7fe48482004-10-09 18:08:01 +00002865 case 's':
2866 /* XXX: more generic ? */
2867 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00002868 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00002869 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2870 cmd_completion(str, cmd->name);
2871 }
bellard64866c32006-05-07 18:03:31 +00002872 } else if (!strcmp(cmd->name, "sendkey")) {
aliguori731b0362009-03-05 23:01:42 +00002873 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00002874 for(key = key_defs; key->name != NULL; key++) {
2875 cmd_completion(str, key->name);
2876 }
bellard7fe48482004-10-09 18:08:01 +00002877 }
2878 break;
bellard81d09122004-07-14 17:21:37 +00002879 default:
2880 break;
2881 }
2882 }
2883 for(i = 0; i < nb_args; i++)
2884 qemu_free(args[i]);
2885}
2886
aliguori731b0362009-03-05 23:01:42 +00002887static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00002888{
aliguori731b0362009-03-05 23:01:42 +00002889 Monitor *mon = opaque;
2890
2891 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00002892}
2893
aliguori731b0362009-03-05 23:01:42 +00002894static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00002895{
aliguori731b0362009-03-05 23:01:42 +00002896 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00002897 int i;
aliguori376253e2009-03-05 23:01:23 +00002898
aliguori731b0362009-03-05 23:01:42 +00002899 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00002900
aliguoricde76ee2009-03-05 23:01:51 +00002901 if (cur_mon->rs) {
2902 for (i = 0; i < size; i++)
2903 readline_handle_byte(cur_mon->rs, buf[i]);
2904 } else {
2905 if (size == 0 || buf[size - 1] != 0)
2906 monitor_printf(cur_mon, "corrupted command\n");
2907 else
2908 monitor_handle_command(cur_mon, (char *)buf);
2909 }
aliguori731b0362009-03-05 23:01:42 +00002910
2911 cur_mon = old_mon;
2912}
aliguorid8f44602008-10-06 13:52:44 +00002913
aliguori376253e2009-03-05 23:01:23 +00002914static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00002915{
aliguori731b0362009-03-05 23:01:42 +00002916 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00002917 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00002918 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00002919}
2920
aliguoricde76ee2009-03-05 23:01:51 +00002921int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00002922{
aliguoricde76ee2009-03-05 23:01:51 +00002923 if (!mon->rs)
2924 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00002925 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00002926 return 0;
aliguorid8f44602008-10-06 13:52:44 +00002927}
2928
aliguori376253e2009-03-05 23:01:23 +00002929void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00002930{
aliguoricde76ee2009-03-05 23:01:51 +00002931 if (!mon->rs)
2932 return;
aliguori731b0362009-03-05 23:01:42 +00002933 if (--mon->suspend_cnt == 0)
2934 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00002935}
2936
aliguori731b0362009-03-05 23:01:42 +00002937static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00002938{
aliguori376253e2009-03-05 23:01:23 +00002939 Monitor *mon = opaque;
2940
aliguori2724b182009-03-05 23:01:47 +00002941 switch (event) {
2942 case CHR_EVENT_MUX_IN:
2943 readline_restart(mon->rs);
2944 monitor_resume(mon);
2945 monitor_flush(mon);
2946 break;
ths86e94de2007-01-05 22:01:59 +00002947
aliguori2724b182009-03-05 23:01:47 +00002948 case CHR_EVENT_MUX_OUT:
2949 if (mon->suspend_cnt == 0)
2950 monitor_printf(mon, "\n");
2951 monitor_flush(mon);
2952 monitor_suspend(mon);
2953 break;
2954
2955 case CHR_EVENT_RESET:
2956 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
2957 "information\n", QEMU_VERSION);
2958 if (mon->chr->focus == 0)
2959 readline_show_prompt(mon->rs);
2960 break;
2961 }
ths86e94de2007-01-05 22:01:59 +00002962}
2963
aliguori731b0362009-03-05 23:01:42 +00002964void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00002965{
aliguori731b0362009-03-05 23:01:42 +00002966 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00002967 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00002968
2969 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00002970 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00002971 is_first_init = 0;
2972 }
aliguori87127162009-03-05 23:01:29 +00002973
2974 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00002975
aliguori87127162009-03-05 23:01:29 +00002976 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00002977 mon->flags = flags;
aliguori2724b182009-03-05 23:01:47 +00002978 if (mon->chr->focus != 0)
2979 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
aliguoricde76ee2009-03-05 23:01:51 +00002980 if (flags & MONITOR_USE_READLINE) {
2981 mon->rs = readline_init(mon, monitor_find_completion);
2982 monitor_read_command(mon, 0);
2983 }
aliguori87127162009-03-05 23:01:29 +00002984
aliguori731b0362009-03-05 23:01:42 +00002985 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
2986 mon);
aliguori87127162009-03-05 23:01:29 +00002987
2988 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00002989 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00002990 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00002991}
2992
aliguori376253e2009-03-05 23:01:23 +00002993static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00002994{
aliguoribb5fc202009-03-05 23:01:15 +00002995 BlockDriverState *bs = opaque;
2996 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00002997
aliguoribb5fc202009-03-05 23:01:15 +00002998 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00002999 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003000 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003001 }
aliguori731b0362009-03-05 23:01:42 +00003002 if (mon->password_completion_cb)
3003 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003004
aliguori731b0362009-03-05 23:01:42 +00003005 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003006}
aliguoric0f4ce72009-03-05 23:01:01 +00003007
aliguori376253e2009-03-05 23:01:23 +00003008void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003009 BlockDriverCompletionFunc *completion_cb,
3010 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003011{
aliguoricde76ee2009-03-05 23:01:51 +00003012 int err;
3013
aliguoribb5fc202009-03-05 23:01:15 +00003014 if (!bdrv_key_required(bs)) {
3015 if (completion_cb)
3016 completion_cb(opaque, 0);
3017 return;
3018 }
aliguoric0f4ce72009-03-05 23:01:01 +00003019
aliguori376253e2009-03-05 23:01:23 +00003020 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3021 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003022
aliguori731b0362009-03-05 23:01:42 +00003023 mon->password_completion_cb = completion_cb;
3024 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003025
aliguoricde76ee2009-03-05 23:01:51 +00003026 err = monitor_read_password(mon, bdrv_password_cb, bs);
3027
3028 if (err && completion_cb)
3029 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003030}