blob: d03de4f1d5612f3fa437e8051d3ecaaddfe9a506 [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
aliguori376253e2009-03-05 23:01:23 +0000100static void monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
101 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000102{
aliguori731b0362009-03-05 23:01:42 +0000103 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
104 /* prompt is printed on return from the command handler */
aliguoribb5fc202009-03-05 23:01:15 +0000105}
106
aliguori376253e2009-03-05 23:01:23 +0000107void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000108{
aliguori731b0362009-03-05 23:01:42 +0000109 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
110 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
111 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000112 }
113}
114
115/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000116static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000117{
ths60fe76f2007-12-16 03:02:09 +0000118 char c;
aliguori731b0362009-03-05 23:01:42 +0000119
120 if (!mon)
121 return;
122
bellard7e2515e2004-08-01 21:52:19 +0000123 for(;;) {
124 c = *str++;
125 if (c == '\0')
126 break;
bellard7ba12602006-07-14 20:26:42 +0000127 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000128 mon->outbuf[mon->outbuf_index++] = '\r';
129 mon->outbuf[mon->outbuf_index++] = c;
130 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
131 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000132 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000133 }
134}
135
aliguori376253e2009-03-05 23:01:23 +0000136void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000137{
138 char buf[4096];
139 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000140 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000141}
142
aliguori376253e2009-03-05 23:01:23 +0000143void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000144{
145 va_list ap;
146 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000147 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000148 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000149}
150
aliguori376253e2009-03-05 23:01:23 +0000151void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000152{
153 int i;
154
155 for (i = 0; filename[i]; i++) {
156 switch (filename[i]) {
157 case ' ':
158 case '"':
159 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000160 monitor_printf(mon, "\\%c", filename[i]);
thsfef30742006-12-22 14:11:32 +0000161 break;
162 case '\t':
aliguori376253e2009-03-05 23:01:23 +0000163 monitor_printf(mon, "\\t");
thsfef30742006-12-22 14:11:32 +0000164 break;
165 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000166 monitor_printf(mon, "\\r");
thsfef30742006-12-22 14:11:32 +0000167 break;
168 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000169 monitor_printf(mon, "\\n");
thsfef30742006-12-22 14:11:32 +0000170 break;
171 default:
aliguori376253e2009-03-05 23:01:23 +0000172 monitor_printf(mon, "%c", filename[i]);
thsfef30742006-12-22 14:11:32 +0000173 break;
174 }
175 }
176}
177
bellard7fe48482004-10-09 18:08:01 +0000178static int monitor_fprintf(FILE *stream, const char *fmt, ...)
179{
180 va_list ap;
181 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000182 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000183 va_end(ap);
184 return 0;
185}
186
bellard9dc39cb2004-03-14 21:38:27 +0000187static int compare_cmd(const char *name, const char *list)
188{
189 const char *p, *pstart;
190 int len;
191 len = strlen(name);
192 p = list;
193 for(;;) {
194 pstart = p;
195 p = strchr(p, '|');
196 if (!p)
197 p = pstart + strlen(pstart);
198 if ((p - pstart) == len && !memcmp(pstart, name, len))
199 return 1;
200 if (*p == '\0')
201 break;
202 p++;
203 }
204 return 0;
205}
206
aliguori376253e2009-03-05 23:01:23 +0000207static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
208 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000209{
aliguori376253e2009-03-05 23:01:23 +0000210 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000211
212 for(cmd = cmds; cmd->name != NULL; cmd++) {
213 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000214 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
215 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000216 }
217}
218
aliguori376253e2009-03-05 23:01:23 +0000219static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000220{
221 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000222 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000223 } else {
aliguori376253e2009-03-05 23:01:23 +0000224 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000225 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000226 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000227 monitor_printf(mon, "Log items (comma separated):\n");
228 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000229 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000230 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000231 }
232 }
bellard9dc39cb2004-03-14 21:38:27 +0000233 }
234}
235
aliguori376253e2009-03-05 23:01:23 +0000236static void do_commit(Monitor *mon, const char *device)
bellard9dc39cb2004-03-14 21:38:27 +0000237{
bellard7954c732006-08-01 15:52:40 +0000238 int i, all_devices;
balrog2dc7b602007-05-24 18:53:22 +0000239
bellard7954c732006-08-01 15:52:40 +0000240 all_devices = !strcmp(device, "all");
thse4bcb142007-12-02 04:51:10 +0000241 for (i = 0; i < nb_drives; i++) {
ths5fafdf22007-09-16 21:08:06 +0000242 if (all_devices ||
thse4bcb142007-12-02 04:51:10 +0000243 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
244 bdrv_commit(drives_table[i].bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000245 }
246}
247
aliguori376253e2009-03-05 23:01:23 +0000248static void do_info(Monitor *mon, const char *item)
bellard9dc39cb2004-03-14 21:38:27 +0000249{
aliguori376253e2009-03-05 23:01:23 +0000250 const mon_cmd_t *cmd;
251 void (*handler)(Monitor *);
bellard9dc39cb2004-03-14 21:38:27 +0000252
bellard9307c4c2004-04-04 12:57:25 +0000253 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000254 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000255 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000256 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000257 goto found;
258 }
259 help:
aliguori376253e2009-03-05 23:01:23 +0000260 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000261 return;
262 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000263 handler = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +0000264 handler(mon);
bellard9dc39cb2004-03-14 21:38:27 +0000265}
266
aliguori376253e2009-03-05 23:01:23 +0000267static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000268{
aliguori376253e2009-03-05 23:01:23 +0000269 monitor_printf(mon, "%s\n", QEMU_VERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000270}
271
aliguori376253e2009-03-05 23:01:23 +0000272static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000273{
274 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000275 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000276}
277
aurel32bf4f74c2008-12-18 22:42:34 +0000278#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000279static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000280{
aliguori376253e2009-03-05 23:01:23 +0000281 monitor_printf(mon, "HPET is %s by QEMU\n",
282 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000283}
aurel32bf4f74c2008-12-18 22:42:34 +0000284#endif
aliguori16b29ae2008-12-17 23:28:44 +0000285
aliguori376253e2009-03-05 23:01:23 +0000286static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000287{
aliguori376253e2009-03-05 23:01:23 +0000288 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
289 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
290 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
291 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
292 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000293}
294
bellard6a00d602005-11-21 23:25:50 +0000295/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000296static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000297{
298 CPUState *env;
299
300 for(env = first_cpu; env != NULL; env = env->next_cpu) {
301 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000302 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000303 return 0;
304 }
305 }
306 return -1;
307}
308
pbrook9596ebb2007-11-18 01:44:38 +0000309static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000310{
aliguori731b0362009-03-05 23:01:42 +0000311 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000312 mon_set_cpu(0);
313 }
aliguori731b0362009-03-05 23:01:42 +0000314 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000315}
316
aliguori376253e2009-03-05 23:01:23 +0000317static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000318{
bellard6a00d602005-11-21 23:25:50 +0000319 CPUState *env;
320 env = mon_get_cpu();
321 if (!env)
322 return;
bellard9307c4c2004-04-04 12:57:25 +0000323#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000324 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000325 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000326#else
aliguori376253e2009-03-05 23:01:23 +0000327 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000328 0);
bellard9307c4c2004-04-04 12:57:25 +0000329#endif
330}
331
aliguori376253e2009-03-05 23:01:23 +0000332static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000333{
334 CPUState *env;
335
336 /* just to set the default cpu if not already done */
337 mon_get_cpu();
338
339 for(env = first_cpu; env != NULL; env = env->next_cpu) {
aliguori376253e2009-03-05 23:01:23 +0000340 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000341 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000342 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000343#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000344 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
345 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000346#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000347 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000348#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000349 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
350 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000351#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000352 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000353#endif
thsead93602007-09-06 00:18:15 +0000354 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000355 monitor_printf(mon, " (halted)");
356 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000357 }
358}
359
aliguori376253e2009-03-05 23:01:23 +0000360static void do_cpu_set(Monitor *mon, int index)
bellard6a00d602005-11-21 23:25:50 +0000361{
362 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000363 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000364}
365
aliguori376253e2009-03-05 23:01:23 +0000366static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000367{
aliguori376253e2009-03-05 23:01:23 +0000368 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000369}
370
aliguori376253e2009-03-05 23:01:23 +0000371static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000372{
373 int i;
bellard7e2515e2004-08-01 21:52:19 +0000374 const char *str;
ths3b46e622007-09-17 08:09:54 +0000375
bellard7e2515e2004-08-01 21:52:19 +0000376 i = 0;
377 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000378 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000379 if (!str)
380 break;
aliguori376253e2009-03-05 23:01:23 +0000381 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000382 i++;
bellardaa455482004-04-04 13:07:25 +0000383 }
384}
385
j_mayer76a66252007-03-07 08:32:30 +0000386#if defined(TARGET_PPC)
387/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000388static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000389{
390 CPUState *env;
391
392 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000393 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000394}
395#endif
396
aliguori376253e2009-03-05 23:01:23 +0000397static void do_quit(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000398{
399 exit(0);
400}
401
aliguori376253e2009-03-05 23:01:23 +0000402static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000403{
404 if (bdrv_is_inserted(bs)) {
405 if (!force) {
406 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000407 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000408 return -1;
409 }
410 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000411 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000412 return -1;
413 }
414 }
415 bdrv_close(bs);
416 }
417 return 0;
418}
419
aliguori376253e2009-03-05 23:01:23 +0000420static void do_eject(Monitor *mon, int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000421{
422 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000423
bellard9307c4c2004-04-04 12:57:25 +0000424 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000425 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000426 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000427 return;
428 }
aliguori376253e2009-03-05 23:01:23 +0000429 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000430}
431
aliguori376253e2009-03-05 23:01:23 +0000432static void do_change_block(Monitor *mon, const char *device,
433 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000434{
435 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000436 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000437
bellard9307c4c2004-04-04 12:57:25 +0000438 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000439 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000440 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000441 return;
442 }
aurel322ecea9b2008-06-18 22:10:01 +0000443 if (fmt) {
444 drv = bdrv_find_format(fmt);
445 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000446 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000447 return;
448 }
449 }
aliguori376253e2009-03-05 23:01:23 +0000450 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000451 return;
aurel322ecea9b2008-06-18 22:10:01 +0000452 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000453 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000454}
455
aliguori376253e2009-03-05 23:01:23 +0000456static void change_vnc_password_cb(Monitor *mon, const char *password,
457 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000458{
459 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000460 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000461
aliguori731b0362009-03-05 23:01:42 +0000462 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000463}
464
aliguori376253e2009-03-05 23:01:23 +0000465static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000466{
ths70848512007-08-25 01:37:05 +0000467 if (strcmp(target, "passwd") == 0 ||
468 strcmp(target, "password") == 0) {
aliguori2569da02008-12-10 15:14:13 +0000469 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000470 char password[9];
aliguori2569da02008-12-10 15:14:13 +0000471 strncpy(password, arg, sizeof(password));
472 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000473 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000474 } else {
aliguori376253e2009-03-05 23:01:23 +0000475 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000476 }
ths70848512007-08-25 01:37:05 +0000477 } else {
478 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000479 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000480 }
thse25a5822007-08-25 01:36:20 +0000481}
482
aliguori376253e2009-03-05 23:01:23 +0000483static void do_change(Monitor *mon, const char *device, const char *target,
484 const char *arg)
thse25a5822007-08-25 01:36:20 +0000485{
486 if (strcmp(device, "vnc") == 0) {
aliguori376253e2009-03-05 23:01:23 +0000487 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000488 } else {
aliguori376253e2009-03-05 23:01:23 +0000489 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000490 }
491}
492
aliguori376253e2009-03-05 23:01:23 +0000493static void do_screen_dump(Monitor *mon, const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000494{
pbrook95219892006-04-09 01:06:34 +0000495 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000496}
497
aliguori376253e2009-03-05 23:01:23 +0000498static void do_logfile(Monitor *mon, const char *filename)
pbrooke735b912007-06-30 13:53:24 +0000499{
500 cpu_set_log_filename(filename);
501}
502
aliguori376253e2009-03-05 23:01:23 +0000503static void do_log(Monitor *mon, const char *items)
bellardf193c792004-03-21 17:06:25 +0000504{
505 int mask;
ths3b46e622007-09-17 08:09:54 +0000506
bellard9307c4c2004-04-04 12:57:25 +0000507 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000508 mask = 0;
509 } else {
bellard9307c4c2004-04-04 12:57:25 +0000510 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000511 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000512 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000513 return;
514 }
515 }
516 cpu_set_log(mask);
517}
518
aliguori376253e2009-03-05 23:01:23 +0000519static void do_stop(Monitor *mon)
bellard8a7ddc32004-03-31 19:00:16 +0000520{
521 vm_stop(EXCP_INTERRUPT);
522}
523
aliguoribb5fc202009-03-05 23:01:15 +0000524static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000525
aliguori376253e2009-03-05 23:01:23 +0000526struct bdrv_iterate_context {
527 Monitor *mon;
528 int err;
529};
aliguoric0f4ce72009-03-05 23:01:01 +0000530
aliguori376253e2009-03-05 23:01:23 +0000531static void do_cont(Monitor *mon)
532{
533 struct bdrv_iterate_context context = { mon, 0 };
534
535 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000536 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000537 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000538 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000539}
540
aliguoribb5fc202009-03-05 23:01:15 +0000541static void bdrv_key_cb(void *opaque, int err)
542{
aliguori376253e2009-03-05 23:01:23 +0000543 Monitor *mon = opaque;
544
aliguoribb5fc202009-03-05 23:01:15 +0000545 /* another key was set successfully, retry to continue */
546 if (!err)
aliguori376253e2009-03-05 23:01:23 +0000547 do_cont(mon);
aliguoribb5fc202009-03-05 23:01:15 +0000548}
549
550static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
551{
aliguori376253e2009-03-05 23:01:23 +0000552 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000553
aliguori376253e2009-03-05 23:01:23 +0000554 if (!context->err && bdrv_key_required(bs)) {
555 context->err = -EBUSY;
556 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
557 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000558 }
559}
560
bellard67b915a2004-03-31 23:37:16 +0000561#ifdef CONFIG_GDBSTUB
aliguori376253e2009-03-05 23:01:23 +0000562static void do_gdbserver(Monitor *mon, const char *port)
bellard8a7ddc32004-03-31 19:00:16 +0000563{
pbrookcfc34752007-02-22 01:48:01 +0000564 if (!port)
bellard9307c4c2004-04-04 12:57:25 +0000565 port = DEFAULT_GDBSTUB_PORT;
pbrookcfc34752007-02-22 01:48:01 +0000566 if (gdbserver_start(port) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000567 monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",
568 port);
bellard8a7ddc32004-03-31 19:00:16 +0000569 } else {
aliguori376253e2009-03-05 23:01:23 +0000570 monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000571 }
572}
bellard67b915a2004-03-31 23:37:16 +0000573#endif
bellard8a7ddc32004-03-31 19:00:16 +0000574
aliguori376253e2009-03-05 23:01:23 +0000575static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000576{
aliguori376253e2009-03-05 23:01:23 +0000577 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000578 switch(c) {
579 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000580 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000581 break;
582 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000583 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000584 break;
585 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000586 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000587 break;
588 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000589 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000590 break;
591 default:
592 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000593 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000594 } else {
aliguori376253e2009-03-05 23:01:23 +0000595 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000596 }
597 break;
598 }
aliguori376253e2009-03-05 23:01:23 +0000599 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000600}
601
aliguori376253e2009-03-05 23:01:23 +0000602static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000603 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000604{
bellard6a00d602005-11-21 23:25:50 +0000605 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000606 int nb_per_line, l, line_size, i, max_digits, len;
607 uint8_t buf[16];
608 uint64_t v;
609
610 if (format == 'i') {
611 int flags;
612 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000613 env = mon_get_cpu();
614 if (!env && !is_physical)
615 return;
bellard9307c4c2004-04-04 12:57:25 +0000616#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000617 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000618 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000619 } else if (wsize == 4) {
620 flags = 0;
621 } else {
bellard6a15fd12006-04-12 21:07:07 +0000622 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000623 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000624 if (env) {
625#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000626 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000627 (env->segs[R_CS].flags & DESC_L_MASK))
628 flags = 2;
629 else
630#endif
631 if (!(env->segs[R_CS].flags & DESC_B_MASK))
632 flags = 1;
633 }
bellard4c27ba22004-04-25 18:05:08 +0000634 }
635#endif
aliguori376253e2009-03-05 23:01:23 +0000636 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000637 return;
638 }
639
640 len = wsize * count;
641 if (wsize == 1)
642 line_size = 8;
643 else
644 line_size = 16;
645 nb_per_line = line_size / wsize;
646 max_digits = 0;
647
648 switch(format) {
649 case 'o':
650 max_digits = (wsize * 8 + 2) / 3;
651 break;
652 default:
653 case 'x':
654 max_digits = (wsize * 8) / 4;
655 break;
656 case 'u':
657 case 'd':
658 max_digits = (wsize * 8 * 10 + 32) / 33;
659 break;
660 case 'c':
661 wsize = 1;
662 break;
663 }
664
665 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000666 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000667 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000668 else
aliguori376253e2009-03-05 23:01:23 +0000669 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000670 l = len;
671 if (l > line_size)
672 l = line_size;
673 if (is_physical) {
674 cpu_physical_memory_rw(addr, buf, l, 0);
675 } else {
bellard6a00d602005-11-21 23:25:50 +0000676 env = mon_get_cpu();
677 if (!env)
678 break;
aliguoric8f79b62008-08-18 14:00:20 +0000679 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000680 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000681 break;
682 }
bellard9307c4c2004-04-04 12:57:25 +0000683 }
ths5fafdf22007-09-16 21:08:06 +0000684 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000685 while (i < l) {
686 switch(wsize) {
687 default:
688 case 1:
689 v = ldub_raw(buf + i);
690 break;
691 case 2:
692 v = lduw_raw(buf + i);
693 break;
694 case 4:
bellard92a31b12005-02-10 22:00:52 +0000695 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000696 break;
697 case 8:
698 v = ldq_raw(buf + i);
699 break;
700 }
aliguori376253e2009-03-05 23:01:23 +0000701 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000702 switch(format) {
703 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000704 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000705 break;
706 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000707 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000708 break;
709 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000710 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000711 break;
712 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000713 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000714 break;
715 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000716 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000717 break;
718 }
719 i += wsize;
720 }
aliguori376253e2009-03-05 23:01:23 +0000721 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000722 addr += l;
723 len -= l;
724 }
725}
726
bellard92a31b12005-02-10 22:00:52 +0000727#if TARGET_LONG_BITS == 64
728#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
729#else
730#define GET_TLONG(h, l) (l)
731#endif
732
aliguori376253e2009-03-05 23:01:23 +0000733static void do_memory_dump(Monitor *mon, int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000734 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000735{
bellard92a31b12005-02-10 22:00:52 +0000736 target_long addr = GET_TLONG(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000737 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000738}
739
blueswir17743e582007-09-24 18:39:04 +0000740#if TARGET_PHYS_ADDR_BITS > 32
741#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
742#else
743#define GET_TPHYSADDR(h, l) (l)
744#endif
745
aliguori376253e2009-03-05 23:01:23 +0000746static void do_physical_memory_dump(Monitor *mon, int count, int format,
747 int size, uint32_t addrh, uint32_t addrl)
bellard92a31b12005-02-10 22:00:52 +0000748
bellard9307c4c2004-04-04 12:57:25 +0000749{
blueswir17743e582007-09-24 18:39:04 +0000750 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000751 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000752}
753
aliguori376253e2009-03-05 23:01:23 +0000754static void do_print(Monitor *mon, int count, int format, int size,
755 unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000756{
blueswir17743e582007-09-24 18:39:04 +0000757 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
758#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000759 switch(format) {
760 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000761 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000762 break;
763 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000764 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000765 break;
766 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000767 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000768 break;
769 default:
770 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000771 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000772 break;
773 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000774 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000775 break;
776 }
bellard92a31b12005-02-10 22:00:52 +0000777#else
778 switch(format) {
779 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000780 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000781 break;
782 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000783 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000784 break;
785 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000786 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000787 break;
788 default:
789 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000790 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000791 break;
792 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000793 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000794 break;
795 }
796#endif
aliguori376253e2009-03-05 23:01:23 +0000797 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000798}
799
aliguori376253e2009-03-05 23:01:23 +0000800static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000801 uint32_t size, const char *filename)
802{
803 FILE *f;
804 target_long addr = GET_TLONG(valh, vall);
805 uint32_t l;
806 CPUState *env;
807 uint8_t buf[1024];
808
809 env = mon_get_cpu();
810 if (!env)
811 return;
812
813 f = fopen(filename, "wb");
814 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000815 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000816 return;
817 }
818 while (size != 0) {
819 l = sizeof(buf);
820 if (l > size)
821 l = size;
822 cpu_memory_rw_debug(env, addr, buf, l, 0);
823 fwrite(buf, 1, l, f);
824 addr += l;
825 size -= l;
826 }
827 fclose(f);
828}
829
aliguori376253e2009-03-05 23:01:23 +0000830static void do_physical_memory_save(Monitor *mon, unsigned int valh,
831 unsigned int vall, uint32_t size,
832 const char *filename)
aurel32a8bdf7a2008-04-11 21:36:14 +0000833{
834 FILE *f;
835 uint32_t l;
836 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000837 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000838
839 f = fopen(filename, "wb");
840 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000841 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000842 return;
843 }
844 while (size != 0) {
845 l = sizeof(buf);
846 if (l > size)
847 l = size;
848 cpu_physical_memory_rw(addr, buf, l, 0);
849 fwrite(buf, 1, l, f);
850 fflush(f);
851 addr += l;
852 size -= l;
853 }
854 fclose(f);
855}
856
aliguori376253e2009-03-05 23:01:23 +0000857static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
bellarde4cf1ad2005-06-04 20:15:57 +0000858{
859 uint32_t addr;
860 uint8_t buf[1];
861 uint16_t sum;
862
863 sum = 0;
864 for(addr = start; addr < (start + size); addr++) {
865 cpu_physical_memory_rw(addr, buf, 1, 0);
866 /* BSD sum algorithm ('sum' Unix command) */
867 sum = (sum >> 1) | (sum << 15);
868 sum += buf[0];
869 }
aliguori376253e2009-03-05 23:01:23 +0000870 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000871}
872
bellarda3a91a32004-06-04 11:06:21 +0000873typedef struct {
874 int keycode;
875 const char *name;
876} KeyDef;
877
878static const KeyDef key_defs[] = {
879 { 0x2a, "shift" },
880 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000881
bellarda3a91a32004-06-04 11:06:21 +0000882 { 0x38, "alt" },
883 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000884 { 0x64, "altgr" },
885 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000886 { 0x1d, "ctrl" },
887 { 0x9d, "ctrl_r" },
888
889 { 0xdd, "menu" },
890
891 { 0x01, "esc" },
892
893 { 0x02, "1" },
894 { 0x03, "2" },
895 { 0x04, "3" },
896 { 0x05, "4" },
897 { 0x06, "5" },
898 { 0x07, "6" },
899 { 0x08, "7" },
900 { 0x09, "8" },
901 { 0x0a, "9" },
902 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000903 { 0x0c, "minus" },
904 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000905 { 0x0e, "backspace" },
906
907 { 0x0f, "tab" },
908 { 0x10, "q" },
909 { 0x11, "w" },
910 { 0x12, "e" },
911 { 0x13, "r" },
912 { 0x14, "t" },
913 { 0x15, "y" },
914 { 0x16, "u" },
915 { 0x17, "i" },
916 { 0x18, "o" },
917 { 0x19, "p" },
918
919 { 0x1c, "ret" },
920
921 { 0x1e, "a" },
922 { 0x1f, "s" },
923 { 0x20, "d" },
924 { 0x21, "f" },
925 { 0x22, "g" },
926 { 0x23, "h" },
927 { 0x24, "j" },
928 { 0x25, "k" },
929 { 0x26, "l" },
930
931 { 0x2c, "z" },
932 { 0x2d, "x" },
933 { 0x2e, "c" },
934 { 0x2f, "v" },
935 { 0x30, "b" },
936 { 0x31, "n" },
937 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000938 { 0x33, "comma" },
939 { 0x34, "dot" },
940 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000941
balrog4d3b6f62008-02-10 16:33:14 +0000942 { 0x37, "asterisk" },
943
bellarda3a91a32004-06-04 11:06:21 +0000944 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000945 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000946 { 0x3b, "f1" },
947 { 0x3c, "f2" },
948 { 0x3d, "f3" },
949 { 0x3e, "f4" },
950 { 0x3f, "f5" },
951 { 0x40, "f6" },
952 { 0x41, "f7" },
953 { 0x42, "f8" },
954 { 0x43, "f9" },
955 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +0000956 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000957 { 0x46, "scroll_lock" },
958
bellard64866c32006-05-07 18:03:31 +0000959 { 0xb5, "kp_divide" },
960 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +0000961 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +0000962 { 0x4e, "kp_add" },
963 { 0x9c, "kp_enter" },
964 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +0000965 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +0000966
967 { 0x52, "kp_0" },
968 { 0x4f, "kp_1" },
969 { 0x50, "kp_2" },
970 { 0x51, "kp_3" },
971 { 0x4b, "kp_4" },
972 { 0x4c, "kp_5" },
973 { 0x4d, "kp_6" },
974 { 0x47, "kp_7" },
975 { 0x48, "kp_8" },
976 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +0000977
bellarda3a91a32004-06-04 11:06:21 +0000978 { 0x56, "<" },
979
980 { 0x57, "f11" },
981 { 0x58, "f12" },
982
983 { 0xb7, "print" },
984
985 { 0xc7, "home" },
986 { 0xc9, "pgup" },
987 { 0xd1, "pgdn" },
988 { 0xcf, "end" },
989
990 { 0xcb, "left" },
991 { 0xc8, "up" },
992 { 0xd0, "down" },
993 { 0xcd, "right" },
994
995 { 0xd2, "insert" },
996 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +0000997#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
998 { 0xf0, "stop" },
999 { 0xf1, "again" },
1000 { 0xf2, "props" },
1001 { 0xf3, "undo" },
1002 { 0xf4, "front" },
1003 { 0xf5, "copy" },
1004 { 0xf6, "open" },
1005 { 0xf7, "paste" },
1006 { 0xf8, "find" },
1007 { 0xf9, "cut" },
1008 { 0xfa, "lf" },
1009 { 0xfb, "help" },
1010 { 0xfc, "meta_l" },
1011 { 0xfd, "meta_r" },
1012 { 0xfe, "compose" },
1013#endif
bellarda3a91a32004-06-04 11:06:21 +00001014 { 0, NULL },
1015};
1016
1017static int get_keycode(const char *key)
1018{
1019 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001020 char *endp;
1021 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001022
1023 for(p = key_defs; p->name != NULL; p++) {
1024 if (!strcmp(key, p->name))
1025 return p->keycode;
1026 }
bellard64866c32006-05-07 18:03:31 +00001027 if (strstart(key, "0x", NULL)) {
1028 ret = strtoul(key, &endp, 0);
1029 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1030 return ret;
1031 }
bellarda3a91a32004-06-04 11:06:21 +00001032 return -1;
1033}
1034
balrogc8256f92008-06-08 22:45:01 +00001035#define MAX_KEYCODES 16
1036static uint8_t keycodes[MAX_KEYCODES];
1037static int nb_pending_keycodes;
1038static QEMUTimer *key_timer;
1039
1040static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001041{
balrogc8256f92008-06-08 22:45:01 +00001042 int keycode;
1043
1044 while (nb_pending_keycodes > 0) {
1045 nb_pending_keycodes--;
1046 keycode = keycodes[nb_pending_keycodes];
1047 if (keycode & 0x80)
1048 kbd_put_keycode(0xe0);
1049 kbd_put_keycode(keycode | 0x80);
1050 }
1051}
1052
aliguori376253e2009-03-05 23:01:23 +00001053static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1054 int hold_time)
balrogc8256f92008-06-08 22:45:01 +00001055{
balrog3401c0d2008-06-04 10:05:59 +00001056 char keyname_buf[16];
1057 char *separator;
1058 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +00001059
balrogc8256f92008-06-08 22:45:01 +00001060 if (nb_pending_keycodes > 0) {
1061 qemu_del_timer(key_timer);
1062 release_keys(NULL);
1063 }
1064 if (!has_hold_time)
1065 hold_time = 100;
1066 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001067 while (1) {
1068 separator = strchr(string, '-');
1069 keyname_len = separator ? separator - string : strlen(string);
1070 if (keyname_len > 0) {
1071 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1072 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001073 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001074 return;
bellarda3a91a32004-06-04 11:06:21 +00001075 }
balrogc8256f92008-06-08 22:45:01 +00001076 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001077 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001078 return;
1079 }
1080 keyname_buf[keyname_len] = 0;
1081 keycode = get_keycode(keyname_buf);
1082 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001083 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001084 return;
1085 }
balrogc8256f92008-06-08 22:45:01 +00001086 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001087 }
balrog3401c0d2008-06-04 10:05:59 +00001088 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001089 break;
balrog3401c0d2008-06-04 10:05:59 +00001090 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001091 }
balrogc8256f92008-06-08 22:45:01 +00001092 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001093 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001094 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001095 keycode = keycodes[i];
1096 if (keycode & 0x80)
1097 kbd_put_keycode(0xe0);
1098 kbd_put_keycode(keycode & 0x7f);
1099 }
balrogc8256f92008-06-08 22:45:01 +00001100 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001101 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1102 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001103}
1104
bellard13224a82006-07-14 22:03:35 +00001105static int mouse_button_state;
1106
aliguori376253e2009-03-05 23:01:23 +00001107static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001108 const char *dz_str)
1109{
1110 int dx, dy, dz;
1111 dx = strtol(dx_str, NULL, 0);
1112 dy = strtol(dy_str, NULL, 0);
1113 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001114 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001115 dz = strtol(dz_str, NULL, 0);
1116 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1117}
1118
aliguori376253e2009-03-05 23:01:23 +00001119static void do_mouse_button(Monitor *mon, int button_state)
bellard13224a82006-07-14 22:03:35 +00001120{
1121 mouse_button_state = button_state;
1122 kbd_mouse_event(0, 0, 0, mouse_button_state);
1123}
1124
aliguori376253e2009-03-05 23:01:23 +00001125static void do_ioport_read(Monitor *mon, int count, int format, int size,
1126 int addr, int has_index, int index)
bellard34405572004-06-08 00:55:58 +00001127{
1128 uint32_t val;
1129 int suffix;
1130
1131 if (has_index) {
1132 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1133 addr++;
1134 }
1135 addr &= 0xffff;
1136
1137 switch(size) {
1138 default:
1139 case 1:
1140 val = cpu_inb(NULL, addr);
1141 suffix = 'b';
1142 break;
1143 case 2:
1144 val = cpu_inw(NULL, addr);
1145 suffix = 'w';
1146 break;
1147 case 4:
1148 val = cpu_inl(NULL, addr);
1149 suffix = 'l';
1150 break;
1151 }
aliguori376253e2009-03-05 23:01:23 +00001152 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1153 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001154}
bellarda3a91a32004-06-04 11:06:21 +00001155
blueswir13b4366d2008-06-20 16:25:06 +00001156/* boot_set handler */
1157static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1158static void *boot_opaque;
1159
1160void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1161{
1162 qemu_boot_set_handler = func;
1163 boot_opaque = opaque;
1164}
1165
aliguori376253e2009-03-05 23:01:23 +00001166static void do_boot_set(Monitor *mon, const char *bootdevice)
aurel320ecdffb2008-05-04 20:11:34 +00001167{
1168 int res;
1169
1170 if (qemu_boot_set_handler) {
blueswir13b4366d2008-06-20 16:25:06 +00001171 res = qemu_boot_set_handler(boot_opaque, bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001172 if (res == 0)
aliguori376253e2009-03-05 23:01:23 +00001173 monitor_printf(mon, "boot device list now set to %s\n",
1174 bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001175 else
aliguori376253e2009-03-05 23:01:23 +00001176 monitor_printf(mon, "setting boot device list failed with "
1177 "error %i\n", res);
aurel320ecdffb2008-05-04 20:11:34 +00001178 } else {
aliguori376253e2009-03-05 23:01:23 +00001179 monitor_printf(mon, "no function defined to set boot device list for "
1180 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001181 }
1182}
1183
aliguori376253e2009-03-05 23:01:23 +00001184static void do_system_reset(Monitor *mon)
bellarde4f90822004-06-20 12:35:44 +00001185{
1186 qemu_system_reset_request();
1187}
1188
aliguori376253e2009-03-05 23:01:23 +00001189static void do_system_powerdown(Monitor *mon)
bellard34751872005-07-02 14:31:34 +00001190{
1191 qemu_system_powerdown_request();
1192}
1193
bellardb86bda52004-09-18 19:32:46 +00001194#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001195static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001196{
aliguori376253e2009-03-05 23:01:23 +00001197 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1198 addr,
1199 pte & mask,
1200 pte & PG_GLOBAL_MASK ? 'G' : '-',
1201 pte & PG_PSE_MASK ? 'P' : '-',
1202 pte & PG_DIRTY_MASK ? 'D' : '-',
1203 pte & PG_ACCESSED_MASK ? 'A' : '-',
1204 pte & PG_PCD_MASK ? 'C' : '-',
1205 pte & PG_PWT_MASK ? 'T' : '-',
1206 pte & PG_USER_MASK ? 'U' : '-',
1207 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001208}
1209
aliguori376253e2009-03-05 23:01:23 +00001210static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001211{
bellard6a00d602005-11-21 23:25:50 +00001212 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001213 int l1, l2;
1214 uint32_t pgd, pde, pte;
1215
bellard6a00d602005-11-21 23:25:50 +00001216 env = mon_get_cpu();
1217 if (!env)
1218 return;
1219
bellardb86bda52004-09-18 19:32:46 +00001220 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001221 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001222 return;
1223 }
1224 pgd = env->cr[3] & ~0xfff;
1225 for(l1 = 0; l1 < 1024; l1++) {
1226 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1227 pde = le32_to_cpu(pde);
1228 if (pde & PG_PRESENT_MASK) {
1229 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001230 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001231 } else {
1232 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001233 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001234 (uint8_t *)&pte, 4);
1235 pte = le32_to_cpu(pte);
1236 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001237 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001238 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001239 ~0xfff);
1240 }
1241 }
1242 }
1243 }
1244 }
1245}
1246
aliguori376253e2009-03-05 23:01:23 +00001247static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001248 uint32_t end, int prot)
1249{
bellard9746b152004-11-11 18:30:24 +00001250 int prot1;
1251 prot1 = *plast_prot;
1252 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001253 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001254 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1255 *pstart, end, end - *pstart,
1256 prot1 & PG_USER_MASK ? 'u' : '-',
1257 'r',
1258 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001259 }
1260 if (prot != 0)
1261 *pstart = end;
1262 else
1263 *pstart = -1;
1264 *plast_prot = prot;
1265 }
1266}
1267
aliguori376253e2009-03-05 23:01:23 +00001268static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001269{
bellard6a00d602005-11-21 23:25:50 +00001270 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001271 int l1, l2, prot, last_prot;
1272 uint32_t pgd, pde, pte, start, end;
1273
bellard6a00d602005-11-21 23:25:50 +00001274 env = mon_get_cpu();
1275 if (!env)
1276 return;
1277
bellardb86bda52004-09-18 19:32:46 +00001278 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001279 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001280 return;
1281 }
1282 pgd = env->cr[3] & ~0xfff;
1283 last_prot = 0;
1284 start = -1;
1285 for(l1 = 0; l1 < 1024; l1++) {
1286 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1287 pde = le32_to_cpu(pde);
1288 end = l1 << 22;
1289 if (pde & PG_PRESENT_MASK) {
1290 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1291 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001292 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001293 } else {
1294 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001295 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001296 (uint8_t *)&pte, 4);
1297 pte = le32_to_cpu(pte);
1298 end = (l1 << 22) + (l2 << 12);
1299 if (pte & PG_PRESENT_MASK) {
1300 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1301 } else {
1302 prot = 0;
1303 }
aliguori376253e2009-03-05 23:01:23 +00001304 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001305 }
1306 }
1307 } else {
1308 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001309 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001310 }
1311 }
1312}
1313#endif
1314
aurel327c664e22009-03-03 06:12:22 +00001315#if defined(TARGET_SH4)
1316
aliguori376253e2009-03-05 23:01:23 +00001317static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001318{
aliguori376253e2009-03-05 23:01:23 +00001319 monitor_printf(mon, " tlb%i:\t"
1320 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1321 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1322 "dirty=%hhu writethrough=%hhu\n",
1323 idx,
1324 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1325 tlb->v, tlb->sh, tlb->c, tlb->pr,
1326 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001327}
1328
aliguori376253e2009-03-05 23:01:23 +00001329static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001330{
1331 CPUState *env = mon_get_cpu();
1332 int i;
1333
aliguori376253e2009-03-05 23:01:23 +00001334 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001335 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001336 print_tlb (mon, i, &env->itlb[i]);
1337 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001338 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001339 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001340}
1341
1342#endif
1343
aliguori376253e2009-03-05 23:01:23 +00001344static void do_info_kqemu(Monitor *mon)
bellard0f4c6412005-07-24 18:10:56 +00001345{
1346#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001347 CPUState *env;
bellard0f4c6412005-07-24 18:10:56 +00001348 int val;
1349 val = 0;
bellard6a00d602005-11-21 23:25:50 +00001350 env = mon_get_cpu();
1351 if (!env) {
aliguori376253e2009-03-05 23:01:23 +00001352 monitor_printf(mon, "No cpu initialized yet");
bellard6a00d602005-11-21 23:25:50 +00001353 return;
1354 }
1355 val = env->kqemu_enabled;
aliguori376253e2009-03-05 23:01:23 +00001356 monitor_printf(mon, "kqemu support: ");
bellard5f1ce942006-02-08 22:40:15 +00001357 switch(val) {
1358 default:
1359 case 0:
aliguori376253e2009-03-05 23:01:23 +00001360 monitor_printf(mon, "disabled\n");
bellard5f1ce942006-02-08 22:40:15 +00001361 break;
1362 case 1:
aliguori376253e2009-03-05 23:01:23 +00001363 monitor_printf(mon, "enabled for user code\n");
bellard5f1ce942006-02-08 22:40:15 +00001364 break;
1365 case 2:
aliguori376253e2009-03-05 23:01:23 +00001366 monitor_printf(mon, "enabled for user and kernel code\n");
bellard5f1ce942006-02-08 22:40:15 +00001367 break;
1368 }
bellard0f4c6412005-07-24 18:10:56 +00001369#else
aliguori376253e2009-03-05 23:01:23 +00001370 monitor_printf(mon, "kqemu support: not compiled\n");
bellard0f4c6412005-07-24 18:10:56 +00001371#endif
ths5fafdf22007-09-16 21:08:06 +00001372}
bellard0f4c6412005-07-24 18:10:56 +00001373
aliguori376253e2009-03-05 23:01:23 +00001374static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001375{
1376#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001377 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001378 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001379 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001380 else
aliguori376253e2009-03-05 23:01:23 +00001381 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001382#else
aliguori376253e2009-03-05 23:01:23 +00001383 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001384#endif
1385}
1386
bellard5f1ce942006-02-08 22:40:15 +00001387#ifdef CONFIG_PROFILER
1388
1389int64_t kqemu_time;
1390int64_t qemu_time;
1391int64_t kqemu_exec_count;
1392int64_t dev_time;
1393int64_t kqemu_ret_int_count;
1394int64_t kqemu_ret_excp_count;
1395int64_t kqemu_ret_intr_count;
1396
aliguori376253e2009-03-05 23:01:23 +00001397static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001398{
1399 int64_t total;
1400 total = qemu_time;
1401 if (total == 0)
1402 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001403 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1404 dev_time, dev_time / (double)ticks_per_sec);
1405 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1406 qemu_time, qemu_time / (double)ticks_per_sec);
1407 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1408 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1409 PRId64 "\n",
1410 kqemu_time, kqemu_time / (double)ticks_per_sec,
1411 kqemu_time / (double)total * 100.0,
1412 kqemu_exec_count,
1413 kqemu_ret_int_count,
1414 kqemu_ret_excp_count,
1415 kqemu_ret_intr_count);
bellard5f1ce942006-02-08 22:40:15 +00001416 qemu_time = 0;
1417 kqemu_time = 0;
1418 kqemu_exec_count = 0;
1419 dev_time = 0;
1420 kqemu_ret_int_count = 0;
1421 kqemu_ret_excp_count = 0;
1422 kqemu_ret_intr_count = 0;
1423#ifdef USE_KQEMU
1424 kqemu_record_dump();
1425#endif
1426}
1427#else
aliguori376253e2009-03-05 23:01:23 +00001428static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001429{
aliguori376253e2009-03-05 23:01:23 +00001430 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001431}
1432#endif
1433
bellardec36b692006-07-16 18:57:03 +00001434/* Capture support */
1435static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1436
aliguori376253e2009-03-05 23:01:23 +00001437static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001438{
1439 int i;
1440 CaptureState *s;
1441
1442 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001443 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001444 s->ops.info (s->opaque);
1445 }
1446}
1447
aliguori376253e2009-03-05 23:01:23 +00001448static void do_stop_capture(Monitor *mon, int n)
bellardec36b692006-07-16 18:57:03 +00001449{
1450 int i;
1451 CaptureState *s;
1452
1453 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1454 if (i == n) {
1455 s->ops.destroy (s->opaque);
1456 LIST_REMOVE (s, entries);
1457 qemu_free (s);
1458 return;
1459 }
1460 }
1461}
1462
1463#ifdef HAS_AUDIO
aliguori376253e2009-03-05 23:01:23 +00001464static void do_wav_capture(Monitor *mon, const char *path,
1465 int has_freq, int freq,
1466 int has_bits, int bits,
1467 int has_channels, int nchannels)
bellardec36b692006-07-16 18:57:03 +00001468{
1469 CaptureState *s;
1470
1471 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001472
1473 freq = has_freq ? freq : 44100;
1474 bits = has_bits ? bits : 16;
1475 nchannels = has_channels ? nchannels : 2;
1476
1477 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001478 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001479 qemu_free (s);
1480 }
1481 LIST_INSERT_HEAD (&capture_head, s, entries);
1482}
1483#endif
1484
aurel32dc1c0b72008-04-27 23:52:12 +00001485#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001486static void do_inject_nmi(Monitor *mon, int cpu_index)
aurel32dc1c0b72008-04-27 23:52:12 +00001487{
1488 CPUState *env;
1489
1490 for (env = first_cpu; env != NULL; env = env->next_cpu)
1491 if (env->cpu_index == cpu_index) {
1492 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1493 break;
1494 }
1495}
1496#endif
1497
aliguori376253e2009-03-05 23:01:23 +00001498static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001499{
1500 if (vm_running)
aliguori376253e2009-03-05 23:01:23 +00001501 monitor_printf(mon, "VM status: running\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001502 else
aliguori376253e2009-03-05 23:01:23 +00001503 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001504}
1505
1506
aliguori376253e2009-03-05 23:01:23 +00001507static void do_balloon(Monitor *mon, int value)
aliguoridf751fa2008-12-04 20:19:35 +00001508{
1509 ram_addr_t target = value;
1510 qemu_balloon(target << 20);
1511}
1512
aliguori376253e2009-03-05 23:01:23 +00001513static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001514{
1515 ram_addr_t actual;
1516
1517 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001518 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001519 monitor_printf(mon, "Using KVM without synchronous MMU, "
1520 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001521 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001522 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001523 else
aliguori376253e2009-03-05 23:01:23 +00001524 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001525}
1526
blueswir1d2c639d2009-01-24 18:19:25 +00001527/* Please update qemu-doc.texi when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001528static const mon_cmd_t mon_cmds[] = {
1529 { "help|?", "s?", help_cmd,
bellard9dc39cb2004-03-14 21:38:27 +00001530 "[cmd]", "show the help" },
ths5fafdf22007-09-16 21:08:06 +00001531 { "commit", "s", do_commit,
bellard7954c732006-08-01 15:52:40 +00001532 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
bellard9307c4c2004-04-04 12:57:25 +00001533 { "info", "s?", do_info,
bellard9dc39cb2004-03-14 21:38:27 +00001534 "subcommand", "show various information about the system state" },
bellard9307c4c2004-04-04 12:57:25 +00001535 { "q|quit", "", do_quit,
bellard9dc39cb2004-03-14 21:38:27 +00001536 "", "quit the emulator" },
bellard81d09122004-07-14 17:21:37 +00001537 { "eject", "-fB", do_eject,
thse5987522007-03-30 18:58:01 +00001538 "[-f] device", "eject a removable medium (use -f to force it)" },
aurel322ecea9b2008-06-18 22:10:01 +00001539 { "change", "BFs?", do_change,
1540 "device filename [format]", "change a removable medium, optional format" },
ths5fafdf22007-09-16 21:08:06 +00001541 { "screendump", "F", do_screen_dump,
bellard59a983b2004-03-17 23:17:16 +00001542 "filename", "save screen into PPM image 'filename'" },
balrog788228c2008-05-24 23:15:46 +00001543 { "logfile", "F", do_logfile,
pbrooke735b912007-06-30 13:53:24 +00001544 "filename", "output logs to 'filename'" },
bellard9307c4c2004-04-04 12:57:25 +00001545 { "log", "s", do_log,
ths5fafdf22007-09-16 21:08:06 +00001546 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
bellardfaea38e2006-08-05 21:31:00 +00001547 { "savevm", "s?", do_savevm,
ths5fafdf22007-09-16 21:08:06 +00001548 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
bellardfaea38e2006-08-05 21:31:00 +00001549 { "loadvm", "s", do_loadvm,
ths5fafdf22007-09-16 21:08:06 +00001550 "tag|id", "restore a VM snapshot from its tag or id" },
bellardfaea38e2006-08-05 21:31:00 +00001551 { "delvm", "s", do_delvm,
ths5fafdf22007-09-16 21:08:06 +00001552 "tag|id", "delete a VM snapshot from its tag or id" },
1553 { "stop", "", do_stop,
bellard9307c4c2004-04-04 12:57:25 +00001554 "", "stop emulation", },
ths5fafdf22007-09-16 21:08:06 +00001555 { "c|cont", "", do_cont,
bellard9307c4c2004-04-04 12:57:25 +00001556 "", "resume emulation", },
bellard67b915a2004-03-31 23:37:16 +00001557#ifdef CONFIG_GDBSTUB
ths5fafdf22007-09-16 21:08:06 +00001558 { "gdbserver", "s?", do_gdbserver,
bellard9307c4c2004-04-04 12:57:25 +00001559 "[port]", "start gdbserver session (default port=1234)", },
bellard67b915a2004-03-31 23:37:16 +00001560#endif
ths5fafdf22007-09-16 21:08:06 +00001561 { "x", "/l", do_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001562 "/fmt addr", "virtual memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001563 { "xp", "/l", do_physical_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001564 "/fmt addr", "physical memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001565 { "p|print", "/l", do_print,
bellard9307c4c2004-04-04 12:57:25 +00001566 "/fmt expr", "print expression value (use $reg for CPU register access)", },
ths5fafdf22007-09-16 21:08:06 +00001567 { "i", "/ii.", do_ioport_read,
bellard34405572004-06-08 00:55:58 +00001568 "/fmt addr", "I/O port read" },
1569
balrogc8256f92008-06-08 22:45:01 +00001570 { "sendkey", "si?", do_sendkey,
1571 "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 +00001572 { "system_reset", "", do_system_reset,
bellarde4f90822004-06-20 12:35:44 +00001573 "", "reset the system" },
ths5fafdf22007-09-16 21:08:06 +00001574 { "system_powerdown", "", do_system_powerdown,
bellard34751872005-07-02 14:31:34 +00001575 "", "send system power down event" },
ths5fafdf22007-09-16 21:08:06 +00001576 { "sum", "ii", do_sum,
bellarde4cf1ad2005-06-04 20:15:57 +00001577 "addr size", "compute the checksum of a memory region" },
bellarda594cfb2005-11-06 16:13:29 +00001578 { "usb_add", "s", do_usb_add,
1579 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1580 { "usb_del", "s", do_usb_del,
1581 "device", "remove USB device 'bus.addr'" },
ths5fafdf22007-09-16 21:08:06 +00001582 { "cpu", "i", do_cpu_set,
bellard6a00d602005-11-21 23:25:50 +00001583 "index", "set the default CPU" },
ths5fafdf22007-09-16 21:08:06 +00001584 { "mouse_move", "sss?", do_mouse_move,
bellard13224a82006-07-14 22:03:35 +00001585 "dx dy [dz]", "send mouse move events" },
ths5fafdf22007-09-16 21:08:06 +00001586 { "mouse_button", "i", do_mouse_button,
bellard13224a82006-07-14 22:03:35 +00001587 "state", "change mouse button state (1=L, 2=M, 4=R)" },
ths455204e2007-01-05 16:42:13 +00001588 { "mouse_set", "i", do_mouse_set,
1589 "index", "set which mouse device receives events" },
bellardec36b692006-07-16 18:57:03 +00001590#ifdef HAS_AUDIO
1591 { "wavcapture", "si?i?i?", do_wav_capture,
1592 "path [frequency bits channels]",
1593 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1594#endif
blueswir1d2c639d2009-01-24 18:19:25 +00001595 { "stopcapture", "i", do_stop_capture,
1596 "capture index", "stop capture" },
ths5fafdf22007-09-16 21:08:06 +00001597 { "memsave", "lis", do_memory_save,
bellardb371dc52007-01-03 15:20:39 +00001598 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
aurel32a8bdf7a2008-04-11 21:36:14 +00001599 { "pmemsave", "lis", do_physical_memory_save,
1600 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
aurel320ecdffb2008-05-04 20:11:34 +00001601 { "boot_set", "s", do_boot_set,
1602 "bootdevice", "define new values for the boot device list" },
aurel32dc1c0b72008-04-27 23:52:12 +00001603#if defined(TARGET_I386)
1604 { "nmi", "i", do_inject_nmi,
1605 "cpu", "inject an NMI on the given CPU", },
1606#endif
aliguori5bb79102008-10-13 03:12:02 +00001607 { "migrate", "-ds", do_migrate,
1608 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1609 { "migrate_cancel", "", do_migrate_cancel,
1610 "", "cancel the current VM migration" },
1611 { "migrate_set_speed", "s", do_migrate_set_speed,
1612 "value", "set maximum speed (in bytes) for migrations" },
aliguori6f338c32009-02-11 15:21:54 +00001613#if defined(TARGET_I386)
1614 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1615 "[file=file][,if=type][,bus=n]\n"
1616 "[,unit=m][,media=d][index=i]\n"
1617 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1618 "[snapshot=on|off][,cache=on|off]",
1619 "add drive to PCI storage controller" },
1620 { "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" },
1621 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1622 { "host_net_add", "ss", net_host_device_add,
1623 "[tap,user,socket,vde] options", "add host VLAN client" },
1624 { "host_net_remove", "is", net_host_device_remove,
1625 "vlan_id name", "remove host VLAN client" },
1626#endif
aliguoridf751fa2008-12-04 20:19:35 +00001627 { "balloon", "i", do_balloon,
1628 "target", "request VM to change it's memory allocation (in MB)" },
aliguorif029bd92009-02-11 15:19:16 +00001629 { "set_link", "ss", do_set_link,
1630 "name [up|down]", "change the link status of a network adapter" },
ths5fafdf22007-09-16 21:08:06 +00001631 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001632};
1633
blueswir1d2c639d2009-01-24 18:19:25 +00001634/* Please update qemu-doc.texi when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001635static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001636 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001637 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001638 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001639 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001640 { "chardev", "", qemu_chr_info,
1641 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001642 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001643 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001644 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001645 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001646 { "registers", "", do_info_registers,
1647 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001648 { "cpus", "", do_info_cpus,
1649 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001650 { "history", "", do_info_history,
1651 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001652 { "irq", "", irq_info,
1653 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001654 { "pic", "", pic_info,
1655 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001656 { "pci", "", pci_info,
1657 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001658#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001659 { "tlb", "", tlb_info,
1660 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001661#endif
1662#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001663 { "mem", "", mem_info,
1664 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001665 { "hpet", "", do_info_hpet,
1666 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001667#endif
bellarde3db7222005-01-26 22:00:47 +00001668 { "jit", "", do_info_jit,
1669 "", "show dynamic compiler info", },
bellard0f4c6412005-07-24 18:10:56 +00001670 { "kqemu", "", do_info_kqemu,
blueswir1d2c639d2009-01-24 18:19:25 +00001671 "", "show KQEMU information", },
aliguori7ba1e612008-11-05 16:04:33 +00001672 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001673 "", "show KVM information", },
bellarda594cfb2005-11-06 16:13:29 +00001674 { "usb", "", usb_info,
1675 "", "show guest USB devices", },
1676 { "usbhost", "", usb_host_info,
1677 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001678 { "profile", "", do_info_profile,
1679 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001680 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001681 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001682 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001683 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001684 { "status", "", do_info_status,
1685 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001686 { "pcmcia", "", pcmcia_info,
1687 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001688 { "mice", "", do_info_mice,
1689 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001690 { "vnc", "", do_info_vnc,
1691 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001692 { "name", "", do_info_name,
1693 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001694 { "uuid", "", do_info_uuid,
1695 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001696#if defined(TARGET_PPC)
1697 { "cpustats", "", do_info_cpu_stats,
1698 "", "show CPU statistics", },
1699#endif
blueswir131a60e22007-10-26 18:42:59 +00001700#if defined(CONFIG_SLIRP)
1701 { "slirp", "", do_info_slirp,
1702 "", "show SLIRP statistics", },
1703#endif
aliguori5bb79102008-10-13 03:12:02 +00001704 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001705 { "balloon", "", do_info_balloon,
1706 "", "show balloon information" },
bellard9dc39cb2004-03-14 21:38:27 +00001707 { NULL, NULL, },
1708};
1709
bellard9307c4c2004-04-04 12:57:25 +00001710/*******************************************************************/
1711
1712static const char *pch;
1713static jmp_buf expr_env;
1714
bellard92a31b12005-02-10 22:00:52 +00001715#define MD_TLONG 0
1716#define MD_I32 1
1717
bellard9307c4c2004-04-04 12:57:25 +00001718typedef struct MonitorDef {
1719 const char *name;
1720 int offset;
blueswir18662d652008-10-02 18:32:44 +00001721 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001722 int type;
bellard9307c4c2004-04-04 12:57:25 +00001723} MonitorDef;
1724
bellard57206fd2004-04-25 18:54:52 +00001725#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001726static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001727{
bellard6a00d602005-11-21 23:25:50 +00001728 CPUState *env = mon_get_cpu();
1729 if (!env)
1730 return 0;
1731 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001732}
1733#endif
1734
bellarda541f292004-04-12 20:39:29 +00001735#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001736static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001737{
bellard6a00d602005-11-21 23:25:50 +00001738 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001739 unsigned int u;
1740 int i;
1741
bellard6a00d602005-11-21 23:25:50 +00001742 if (!env)
1743 return 0;
1744
bellarda541f292004-04-12 20:39:29 +00001745 u = 0;
1746 for (i = 0; i < 8; i++)
bellard6a00d602005-11-21 23:25:50 +00001747 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001748
1749 return u;
1750}
1751
blueswir18662d652008-10-02 18:32:44 +00001752static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001753{
bellard6a00d602005-11-21 23:25:50 +00001754 CPUState *env = mon_get_cpu();
1755 if (!env)
1756 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001757 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001758}
1759
blueswir18662d652008-10-02 18:32:44 +00001760static target_long monitor_get_xer (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;
aurel323d7b4172008-10-21 11:28:46 +00001765 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001766}
bellard9fddaa02004-05-21 12:59:32 +00001767
blueswir18662d652008-10-02 18:32:44 +00001768static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001769{
bellard6a00d602005-11-21 23:25:50 +00001770 CPUState *env = mon_get_cpu();
1771 if (!env)
1772 return 0;
1773 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001774}
1775
blueswir18662d652008-10-02 18:32:44 +00001776static target_long monitor_get_tbu (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_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001782}
1783
blueswir18662d652008-10-02 18:32:44 +00001784static target_long monitor_get_tbl (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_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001790}
bellarda541f292004-04-12 20:39:29 +00001791#endif
1792
bellarde95c8d52004-09-30 22:22:08 +00001793#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001794#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001795static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001796{
bellard6a00d602005-11-21 23:25:50 +00001797 CPUState *env = mon_get_cpu();
1798 if (!env)
1799 return 0;
1800 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001801}
bellard7b936c02005-10-30 17:05:13 +00001802#endif
bellarde95c8d52004-09-30 22:22:08 +00001803
blueswir18662d652008-10-02 18:32:44 +00001804static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001805{
bellard6a00d602005-11-21 23:25:50 +00001806 CPUState *env = mon_get_cpu();
1807 if (!env)
1808 return 0;
1809 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001810}
1811#endif
1812
blueswir18662d652008-10-02 18:32:44 +00001813static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001814#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001815
1816#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001817 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001818 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001819 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001820
bellard9307c4c2004-04-04 12:57:25 +00001821 { "eax", offsetof(CPUState, regs[0]) },
1822 { "ecx", offsetof(CPUState, regs[1]) },
1823 { "edx", offsetof(CPUState, regs[2]) },
1824 { "ebx", offsetof(CPUState, regs[3]) },
1825 { "esp|sp", offsetof(CPUState, regs[4]) },
1826 { "ebp|fp", offsetof(CPUState, regs[5]) },
1827 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001828 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001829#ifdef TARGET_X86_64
1830 { "r8", offsetof(CPUState, regs[8]) },
1831 { "r9", offsetof(CPUState, regs[9]) },
1832 { "r10", offsetof(CPUState, regs[10]) },
1833 { "r11", offsetof(CPUState, regs[11]) },
1834 { "r12", offsetof(CPUState, regs[12]) },
1835 { "r13", offsetof(CPUState, regs[13]) },
1836 { "r14", offsetof(CPUState, regs[14]) },
1837 { "r15", offsetof(CPUState, regs[15]) },
1838#endif
bellard9307c4c2004-04-04 12:57:25 +00001839 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001840 { "eip", offsetof(CPUState, eip) },
1841 SEG("cs", R_CS)
1842 SEG("ds", R_DS)
1843 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001844 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001845 SEG("fs", R_FS)
1846 SEG("gs", R_GS)
1847 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00001848#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00001849 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00001850 { "r0", offsetof(CPUState, gpr[0]) },
1851 { "r1", offsetof(CPUState, gpr[1]) },
1852 { "r2", offsetof(CPUState, gpr[2]) },
1853 { "r3", offsetof(CPUState, gpr[3]) },
1854 { "r4", offsetof(CPUState, gpr[4]) },
1855 { "r5", offsetof(CPUState, gpr[5]) },
1856 { "r6", offsetof(CPUState, gpr[6]) },
1857 { "r7", offsetof(CPUState, gpr[7]) },
1858 { "r8", offsetof(CPUState, gpr[8]) },
1859 { "r9", offsetof(CPUState, gpr[9]) },
1860 { "r10", offsetof(CPUState, gpr[10]) },
1861 { "r11", offsetof(CPUState, gpr[11]) },
1862 { "r12", offsetof(CPUState, gpr[12]) },
1863 { "r13", offsetof(CPUState, gpr[13]) },
1864 { "r14", offsetof(CPUState, gpr[14]) },
1865 { "r15", offsetof(CPUState, gpr[15]) },
1866 { "r16", offsetof(CPUState, gpr[16]) },
1867 { "r17", offsetof(CPUState, gpr[17]) },
1868 { "r18", offsetof(CPUState, gpr[18]) },
1869 { "r19", offsetof(CPUState, gpr[19]) },
1870 { "r20", offsetof(CPUState, gpr[20]) },
1871 { "r21", offsetof(CPUState, gpr[21]) },
1872 { "r22", offsetof(CPUState, gpr[22]) },
1873 { "r23", offsetof(CPUState, gpr[23]) },
1874 { "r24", offsetof(CPUState, gpr[24]) },
1875 { "r25", offsetof(CPUState, gpr[25]) },
1876 { "r26", offsetof(CPUState, gpr[26]) },
1877 { "r27", offsetof(CPUState, gpr[27]) },
1878 { "r28", offsetof(CPUState, gpr[28]) },
1879 { "r29", offsetof(CPUState, gpr[29]) },
1880 { "r30", offsetof(CPUState, gpr[30]) },
1881 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00001882 /* Floating point registers */
1883 { "f0", offsetof(CPUState, fpr[0]) },
1884 { "f1", offsetof(CPUState, fpr[1]) },
1885 { "f2", offsetof(CPUState, fpr[2]) },
1886 { "f3", offsetof(CPUState, fpr[3]) },
1887 { "f4", offsetof(CPUState, fpr[4]) },
1888 { "f5", offsetof(CPUState, fpr[5]) },
1889 { "f6", offsetof(CPUState, fpr[6]) },
1890 { "f7", offsetof(CPUState, fpr[7]) },
1891 { "f8", offsetof(CPUState, fpr[8]) },
1892 { "f9", offsetof(CPUState, fpr[9]) },
1893 { "f10", offsetof(CPUState, fpr[10]) },
1894 { "f11", offsetof(CPUState, fpr[11]) },
1895 { "f12", offsetof(CPUState, fpr[12]) },
1896 { "f13", offsetof(CPUState, fpr[13]) },
1897 { "f14", offsetof(CPUState, fpr[14]) },
1898 { "f15", offsetof(CPUState, fpr[15]) },
1899 { "f16", offsetof(CPUState, fpr[16]) },
1900 { "f17", offsetof(CPUState, fpr[17]) },
1901 { "f18", offsetof(CPUState, fpr[18]) },
1902 { "f19", offsetof(CPUState, fpr[19]) },
1903 { "f20", offsetof(CPUState, fpr[20]) },
1904 { "f21", offsetof(CPUState, fpr[21]) },
1905 { "f22", offsetof(CPUState, fpr[22]) },
1906 { "f23", offsetof(CPUState, fpr[23]) },
1907 { "f24", offsetof(CPUState, fpr[24]) },
1908 { "f25", offsetof(CPUState, fpr[25]) },
1909 { "f26", offsetof(CPUState, fpr[26]) },
1910 { "f27", offsetof(CPUState, fpr[27]) },
1911 { "f28", offsetof(CPUState, fpr[28]) },
1912 { "f29", offsetof(CPUState, fpr[29]) },
1913 { "f30", offsetof(CPUState, fpr[30]) },
1914 { "f31", offsetof(CPUState, fpr[31]) },
1915 { "fpscr", offsetof(CPUState, fpscr) },
1916 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00001917 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00001918 { "lr", offsetof(CPUState, lr) },
1919 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00001920 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00001921 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00001922 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00001923 { "msr", 0, &monitor_get_msr, },
1924 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00001925 { "tbu", 0, &monitor_get_tbu, },
1926 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00001927#if defined(TARGET_PPC64)
1928 /* Address space register */
1929 { "asr", offsetof(CPUState, asr) },
1930#endif
1931 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00001932 { "sdr1", offsetof(CPUState, sdr1) },
1933 { "sr0", offsetof(CPUState, sr[0]) },
1934 { "sr1", offsetof(CPUState, sr[1]) },
1935 { "sr2", offsetof(CPUState, sr[2]) },
1936 { "sr3", offsetof(CPUState, sr[3]) },
1937 { "sr4", offsetof(CPUState, sr[4]) },
1938 { "sr5", offsetof(CPUState, sr[5]) },
1939 { "sr6", offsetof(CPUState, sr[6]) },
1940 { "sr7", offsetof(CPUState, sr[7]) },
1941 { "sr8", offsetof(CPUState, sr[8]) },
1942 { "sr9", offsetof(CPUState, sr[9]) },
1943 { "sr10", offsetof(CPUState, sr[10]) },
1944 { "sr11", offsetof(CPUState, sr[11]) },
1945 { "sr12", offsetof(CPUState, sr[12]) },
1946 { "sr13", offsetof(CPUState, sr[13]) },
1947 { "sr14", offsetof(CPUState, sr[14]) },
1948 { "sr15", offsetof(CPUState, sr[15]) },
1949 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00001950#elif defined(TARGET_SPARC)
1951 { "g0", offsetof(CPUState, gregs[0]) },
1952 { "g1", offsetof(CPUState, gregs[1]) },
1953 { "g2", offsetof(CPUState, gregs[2]) },
1954 { "g3", offsetof(CPUState, gregs[3]) },
1955 { "g4", offsetof(CPUState, gregs[4]) },
1956 { "g5", offsetof(CPUState, gregs[5]) },
1957 { "g6", offsetof(CPUState, gregs[6]) },
1958 { "g7", offsetof(CPUState, gregs[7]) },
1959 { "o0", 0, monitor_get_reg },
1960 { "o1", 1, monitor_get_reg },
1961 { "o2", 2, monitor_get_reg },
1962 { "o3", 3, monitor_get_reg },
1963 { "o4", 4, monitor_get_reg },
1964 { "o5", 5, monitor_get_reg },
1965 { "o6", 6, monitor_get_reg },
1966 { "o7", 7, monitor_get_reg },
1967 { "l0", 8, monitor_get_reg },
1968 { "l1", 9, monitor_get_reg },
1969 { "l2", 10, monitor_get_reg },
1970 { "l3", 11, monitor_get_reg },
1971 { "l4", 12, monitor_get_reg },
1972 { "l5", 13, monitor_get_reg },
1973 { "l6", 14, monitor_get_reg },
1974 { "l7", 15, monitor_get_reg },
1975 { "i0", 16, monitor_get_reg },
1976 { "i1", 17, monitor_get_reg },
1977 { "i2", 18, monitor_get_reg },
1978 { "i3", 19, monitor_get_reg },
1979 { "i4", 20, monitor_get_reg },
1980 { "i5", 21, monitor_get_reg },
1981 { "i6", 22, monitor_get_reg },
1982 { "i7", 23, monitor_get_reg },
1983 { "pc", offsetof(CPUState, pc) },
1984 { "npc", offsetof(CPUState, npc) },
1985 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00001986#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00001987 { "psr", 0, &monitor_get_psr, },
1988 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00001989#endif
bellarde95c8d52004-09-30 22:22:08 +00001990 { "tbr", offsetof(CPUState, tbr) },
1991 { "fsr", offsetof(CPUState, fsr) },
1992 { "f0", offsetof(CPUState, fpr[0]) },
1993 { "f1", offsetof(CPUState, fpr[1]) },
1994 { "f2", offsetof(CPUState, fpr[2]) },
1995 { "f3", offsetof(CPUState, fpr[3]) },
1996 { "f4", offsetof(CPUState, fpr[4]) },
1997 { "f5", offsetof(CPUState, fpr[5]) },
1998 { "f6", offsetof(CPUState, fpr[6]) },
1999 { "f7", offsetof(CPUState, fpr[7]) },
2000 { "f8", offsetof(CPUState, fpr[8]) },
2001 { "f9", offsetof(CPUState, fpr[9]) },
2002 { "f10", offsetof(CPUState, fpr[10]) },
2003 { "f11", offsetof(CPUState, fpr[11]) },
2004 { "f12", offsetof(CPUState, fpr[12]) },
2005 { "f13", offsetof(CPUState, fpr[13]) },
2006 { "f14", offsetof(CPUState, fpr[14]) },
2007 { "f15", offsetof(CPUState, fpr[15]) },
2008 { "f16", offsetof(CPUState, fpr[16]) },
2009 { "f17", offsetof(CPUState, fpr[17]) },
2010 { "f18", offsetof(CPUState, fpr[18]) },
2011 { "f19", offsetof(CPUState, fpr[19]) },
2012 { "f20", offsetof(CPUState, fpr[20]) },
2013 { "f21", offsetof(CPUState, fpr[21]) },
2014 { "f22", offsetof(CPUState, fpr[22]) },
2015 { "f23", offsetof(CPUState, fpr[23]) },
2016 { "f24", offsetof(CPUState, fpr[24]) },
2017 { "f25", offsetof(CPUState, fpr[25]) },
2018 { "f26", offsetof(CPUState, fpr[26]) },
2019 { "f27", offsetof(CPUState, fpr[27]) },
2020 { "f28", offsetof(CPUState, fpr[28]) },
2021 { "f29", offsetof(CPUState, fpr[29]) },
2022 { "f30", offsetof(CPUState, fpr[30]) },
2023 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002024#ifdef TARGET_SPARC64
2025 { "f32", offsetof(CPUState, fpr[32]) },
2026 { "f34", offsetof(CPUState, fpr[34]) },
2027 { "f36", offsetof(CPUState, fpr[36]) },
2028 { "f38", offsetof(CPUState, fpr[38]) },
2029 { "f40", offsetof(CPUState, fpr[40]) },
2030 { "f42", offsetof(CPUState, fpr[42]) },
2031 { "f44", offsetof(CPUState, fpr[44]) },
2032 { "f46", offsetof(CPUState, fpr[46]) },
2033 { "f48", offsetof(CPUState, fpr[48]) },
2034 { "f50", offsetof(CPUState, fpr[50]) },
2035 { "f52", offsetof(CPUState, fpr[52]) },
2036 { "f54", offsetof(CPUState, fpr[54]) },
2037 { "f56", offsetof(CPUState, fpr[56]) },
2038 { "f58", offsetof(CPUState, fpr[58]) },
2039 { "f60", offsetof(CPUState, fpr[60]) },
2040 { "f62", offsetof(CPUState, fpr[62]) },
2041 { "asi", offsetof(CPUState, asi) },
2042 { "pstate", offsetof(CPUState, pstate) },
2043 { "cansave", offsetof(CPUState, cansave) },
2044 { "canrestore", offsetof(CPUState, canrestore) },
2045 { "otherwin", offsetof(CPUState, otherwin) },
2046 { "wstate", offsetof(CPUState, wstate) },
2047 { "cleanwin", offsetof(CPUState, cleanwin) },
2048 { "fprs", offsetof(CPUState, fprs) },
2049#endif
bellard9307c4c2004-04-04 12:57:25 +00002050#endif
2051 { NULL },
2052};
2053
aliguori376253e2009-03-05 23:01:23 +00002054static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002055{
aliguori376253e2009-03-05 23:01:23 +00002056 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002057 longjmp(expr_env, 1);
2058}
2059
bellard6a00d602005-11-21 23:25:50 +00002060/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002061static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002062{
blueswir18662d652008-10-02 18:32:44 +00002063 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002064 void *ptr;
2065
bellard9307c4c2004-04-04 12:57:25 +00002066 for(md = monitor_defs; md->name != NULL; md++) {
2067 if (compare_cmd(name, md->name)) {
2068 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002069 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002070 } else {
bellard6a00d602005-11-21 23:25:50 +00002071 CPUState *env = mon_get_cpu();
2072 if (!env)
2073 return -2;
2074 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002075 switch(md->type) {
2076 case MD_I32:
2077 *pval = *(int32_t *)ptr;
2078 break;
2079 case MD_TLONG:
2080 *pval = *(target_long *)ptr;
2081 break;
2082 default:
2083 *pval = 0;
2084 break;
2085 }
bellard9307c4c2004-04-04 12:57:25 +00002086 }
2087 return 0;
2088 }
2089 }
2090 return -1;
2091}
2092
2093static void next(void)
2094{
2095 if (pch != '\0') {
2096 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002097 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002098 pch++;
2099 }
2100}
2101
aliguori376253e2009-03-05 23:01:23 +00002102static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002103
aliguori376253e2009-03-05 23:01:23 +00002104static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002105{
blueswir1c2efc952007-09-25 17:28:42 +00002106 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002107 char *p;
bellard6a00d602005-11-21 23:25:50 +00002108 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002109
2110 switch(*pch) {
2111 case '+':
2112 next();
aliguori376253e2009-03-05 23:01:23 +00002113 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002114 break;
2115 case '-':
2116 next();
aliguori376253e2009-03-05 23:01:23 +00002117 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002118 break;
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_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002126 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002127 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002128 }
2129 next();
2130 break;
bellard81d09122004-07-14 17:21:37 +00002131 case '\'':
2132 pch++;
2133 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002134 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002135 n = *pch;
2136 pch++;
2137 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002138 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002139 next();
2140 break;
bellard9307c4c2004-04-04 12:57:25 +00002141 case '$':
2142 {
2143 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002144 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002145
bellard9307c4c2004-04-04 12:57:25 +00002146 pch++;
2147 q = buf;
2148 while ((*pch >= 'a' && *pch <= 'z') ||
2149 (*pch >= 'A' && *pch <= 'Z') ||
2150 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002151 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002152 if ((q - buf) < sizeof(buf) - 1)
2153 *q++ = *pch;
2154 pch++;
2155 }
blueswir1cd390082008-11-16 13:53:32 +00002156 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002157 pch++;
2158 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002159 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002160 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002161 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002162 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002163 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002164 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002165 }
2166 break;
2167 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002168 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002169 n = 0;
2170 break;
2171 default:
blueswir17743e582007-09-24 18:39:04 +00002172#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002173 n = strtoull(pch, &p, 0);
2174#else
bellard9307c4c2004-04-04 12:57:25 +00002175 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002176#endif
bellard9307c4c2004-04-04 12:57:25 +00002177 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002178 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002179 }
2180 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002181 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002182 pch++;
2183 break;
2184 }
2185 return n;
2186}
2187
2188
aliguori376253e2009-03-05 23:01:23 +00002189static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002190{
blueswir1c2efc952007-09-25 17:28:42 +00002191 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002192 int op;
ths3b46e622007-09-17 08:09:54 +00002193
aliguori376253e2009-03-05 23:01:23 +00002194 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002195 for(;;) {
2196 op = *pch;
2197 if (op != '*' && op != '/' && op != '%')
2198 break;
2199 next();
aliguori376253e2009-03-05 23:01:23 +00002200 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002201 switch(op) {
2202 default:
2203 case '*':
2204 val *= val2;
2205 break;
2206 case '/':
2207 case '%':
ths5fafdf22007-09-16 21:08:06 +00002208 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002209 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002210 if (op == '/')
2211 val /= val2;
2212 else
2213 val %= val2;
2214 break;
2215 }
2216 }
2217 return val;
2218}
2219
aliguori376253e2009-03-05 23:01:23 +00002220static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002221{
blueswir1c2efc952007-09-25 17:28:42 +00002222 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002223 int op;
bellard9307c4c2004-04-04 12:57:25 +00002224
aliguori376253e2009-03-05 23:01:23 +00002225 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002226 for(;;) {
2227 op = *pch;
2228 if (op != '&' && op != '|' && op != '^')
2229 break;
2230 next();
aliguori376253e2009-03-05 23:01:23 +00002231 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002232 switch(op) {
2233 default:
2234 case '&':
2235 val &= val2;
2236 break;
2237 case '|':
2238 val |= val2;
2239 break;
2240 case '^':
2241 val ^= val2;
2242 break;
2243 }
2244 }
2245 return val;
2246}
2247
aliguori376253e2009-03-05 23:01:23 +00002248static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002249{
blueswir1c2efc952007-09-25 17:28:42 +00002250 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002251 int op;
bellard9307c4c2004-04-04 12:57:25 +00002252
aliguori376253e2009-03-05 23:01:23 +00002253 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002254 for(;;) {
2255 op = *pch;
2256 if (op != '+' && op != '-')
2257 break;
2258 next();
aliguori376253e2009-03-05 23:01:23 +00002259 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002260 if (op == '+')
2261 val += val2;
2262 else
2263 val -= val2;
2264 }
2265 return val;
2266}
2267
aliguori376253e2009-03-05 23:01:23 +00002268static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002269{
2270 pch = *pp;
2271 if (setjmp(expr_env)) {
2272 *pp = pch;
2273 return -1;
2274 }
blueswir1cd390082008-11-16 13:53:32 +00002275 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002276 pch++;
aliguori376253e2009-03-05 23:01:23 +00002277 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002278 *pp = pch;
2279 return 0;
2280}
2281
2282static int get_str(char *buf, int buf_size, const char **pp)
2283{
2284 const char *p;
2285 char *q;
2286 int c;
2287
bellard81d09122004-07-14 17:21:37 +00002288 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002289 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002290 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002291 p++;
2292 if (*p == '\0') {
2293 fail:
bellard81d09122004-07-14 17:21:37 +00002294 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002295 *pp = p;
2296 return -1;
2297 }
bellard9307c4c2004-04-04 12:57:25 +00002298 if (*p == '\"') {
2299 p++;
2300 while (*p != '\0' && *p != '\"') {
2301 if (*p == '\\') {
2302 p++;
2303 c = *p++;
2304 switch(c) {
2305 case 'n':
2306 c = '\n';
2307 break;
2308 case 'r':
2309 c = '\r';
2310 break;
2311 case '\\':
2312 case '\'':
2313 case '\"':
2314 break;
2315 default:
2316 qemu_printf("unsupported escape code: '\\%c'\n", c);
2317 goto fail;
2318 }
2319 if ((q - buf) < buf_size - 1) {
2320 *q++ = c;
2321 }
2322 } else {
2323 if ((q - buf) < buf_size - 1) {
2324 *q++ = *p;
2325 }
2326 p++;
2327 }
2328 }
2329 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002330 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002331 goto fail;
2332 }
2333 p++;
2334 } else {
blueswir1cd390082008-11-16 13:53:32 +00002335 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002336 if ((q - buf) < buf_size - 1) {
2337 *q++ = *p;
2338 }
2339 p++;
2340 }
bellard9307c4c2004-04-04 12:57:25 +00002341 }
bellard81d09122004-07-14 17:21:37 +00002342 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002343 *pp = p;
2344 return 0;
2345}
2346
2347static int default_fmt_format = 'x';
2348static int default_fmt_size = 4;
2349
2350#define MAX_ARGS 16
2351
aliguori376253e2009-03-05 23:01:23 +00002352static void monitor_handle_command(Monitor *mon, const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002353{
2354 const char *p, *pstart, *typestr;
2355 char *q;
2356 int c, nb_args, len, i, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002357 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002358 char cmdname[256];
2359 char buf[1024];
2360 void *str_allocated[MAX_ARGS];
2361 void *args[MAX_ARGS];
aliguori376253e2009-03-05 23:01:23 +00002362 void (*handler_0)(Monitor *mon);
2363 void (*handler_1)(Monitor *mon, void *arg0);
2364 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2365 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2366 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2367 void *arg3);
2368 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2369 void *arg3, void *arg4);
2370 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2371 void *arg3, void *arg4, void *arg5);
2372 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2373 void *arg3, void *arg4, void *arg5, void *arg6);
bellard9dc39cb2004-03-14 21:38:27 +00002374
2375#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002376 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002377#endif
ths3b46e622007-09-17 08:09:54 +00002378
bellard9307c4c2004-04-04 12:57:25 +00002379 /* extract the command name */
bellard9dc39cb2004-03-14 21:38:27 +00002380 p = cmdline;
bellard9307c4c2004-04-04 12:57:25 +00002381 q = cmdname;
blueswir1cd390082008-11-16 13:53:32 +00002382 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002383 p++;
2384 if (*p == '\0')
bellard9dc39cb2004-03-14 21:38:27 +00002385 return;
bellard9307c4c2004-04-04 12:57:25 +00002386 pstart = p;
blueswir1cd390082008-11-16 13:53:32 +00002387 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002388 p++;
2389 len = p - pstart;
2390 if (len > sizeof(cmdname) - 1)
2391 len = sizeof(cmdname) - 1;
2392 memcpy(cmdname, pstart, len);
2393 cmdname[len] = '\0';
ths3b46e622007-09-17 08:09:54 +00002394
bellard9307c4c2004-04-04 12:57:25 +00002395 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002396 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002397 if (compare_cmd(cmdname, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +00002398 goto found;
2399 }
aliguori376253e2009-03-05 23:01:23 +00002400 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
bellard9dc39cb2004-03-14 21:38:27 +00002401 return;
2402 found:
bellard9307c4c2004-04-04 12:57:25 +00002403
2404 for(i = 0; i < MAX_ARGS; i++)
2405 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002406
bellard9307c4c2004-04-04 12:57:25 +00002407 /* parse the parameters */
2408 typestr = cmd->args_type;
2409 nb_args = 0;
2410 for(;;) {
2411 c = *typestr;
2412 if (c == '\0')
2413 break;
2414 typestr++;
2415 switch(c) {
2416 case 'F':
bellard81d09122004-07-14 17:21:37 +00002417 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002418 case 's':
2419 {
2420 int ret;
2421 char *str;
ths3b46e622007-09-17 08:09:54 +00002422
blueswir1cd390082008-11-16 13:53:32 +00002423 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002424 p++;
2425 if (*typestr == '?') {
2426 typestr++;
2427 if (*p == '\0') {
2428 /* no optional string: NULL argument */
2429 str = NULL;
2430 goto add_str;
2431 }
2432 }
2433 ret = get_str(buf, sizeof(buf), &p);
2434 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002435 switch(c) {
2436 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002437 monitor_printf(mon, "%s: filename expected\n",
2438 cmdname);
bellard81d09122004-07-14 17:21:37 +00002439 break;
2440 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002441 monitor_printf(mon, "%s: block device name expected\n",
2442 cmdname);
bellard81d09122004-07-14 17:21:37 +00002443 break;
2444 default:
aliguori376253e2009-03-05 23:01:23 +00002445 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002446 break;
2447 }
bellard9307c4c2004-04-04 12:57:25 +00002448 goto fail;
2449 }
2450 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002451 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002452 str_allocated[nb_args] = str;
2453 add_str:
2454 if (nb_args >= MAX_ARGS) {
2455 error_args:
aliguori376253e2009-03-05 23:01:23 +00002456 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002457 goto fail;
2458 }
2459 args[nb_args++] = str;
2460 }
2461 break;
2462 case '/':
2463 {
2464 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002465
blueswir1cd390082008-11-16 13:53:32 +00002466 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002467 p++;
2468 if (*p == '/') {
2469 /* format found */
2470 p++;
2471 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002472 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002473 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002474 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002475 count = count * 10 + (*p - '0');
2476 p++;
2477 }
2478 }
2479 size = -1;
2480 format = -1;
2481 for(;;) {
2482 switch(*p) {
2483 case 'o':
2484 case 'd':
2485 case 'u':
2486 case 'x':
2487 case 'i':
2488 case 'c':
2489 format = *p++;
2490 break;
2491 case 'b':
2492 size = 1;
2493 p++;
2494 break;
2495 case 'h':
2496 size = 2;
2497 p++;
2498 break;
2499 case 'w':
2500 size = 4;
2501 p++;
2502 break;
2503 case 'g':
2504 case 'L':
2505 size = 8;
2506 p++;
2507 break;
2508 default:
2509 goto next;
2510 }
2511 }
2512 next:
blueswir1cd390082008-11-16 13:53:32 +00002513 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002514 monitor_printf(mon, "invalid char in format: '%c'\n",
2515 *p);
bellard9307c4c2004-04-04 12:57:25 +00002516 goto fail;
2517 }
bellard9307c4c2004-04-04 12:57:25 +00002518 if (format < 0)
2519 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002520 if (format != 'i') {
2521 /* for 'i', not specifying a size gives -1 as size */
2522 if (size < 0)
2523 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002524 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002525 }
bellard9307c4c2004-04-04 12:57:25 +00002526 default_fmt_format = format;
2527 } else {
2528 count = 1;
2529 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002530 if (format != 'i') {
2531 size = default_fmt_size;
2532 } else {
2533 size = -1;
2534 }
bellard9307c4c2004-04-04 12:57:25 +00002535 }
2536 if (nb_args + 3 > MAX_ARGS)
2537 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002538 args[nb_args++] = (void*)(long)count;
2539 args[nb_args++] = (void*)(long)format;
2540 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002541 }
2542 break;
2543 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002544 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002545 {
blueswir1c2efc952007-09-25 17:28:42 +00002546 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002547
blueswir1cd390082008-11-16 13:53:32 +00002548 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002549 p++;
bellard34405572004-06-08 00:55:58 +00002550 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002551 if (*typestr == '?') {
2552 if (*p == '\0')
2553 has_arg = 0;
2554 else
2555 has_arg = 1;
2556 } else {
2557 if (*p == '.') {
2558 p++;
blueswir1cd390082008-11-16 13:53:32 +00002559 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002560 p++;
2561 has_arg = 1;
2562 } else {
2563 has_arg = 0;
2564 }
2565 }
bellard13224a82006-07-14 22:03:35 +00002566 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002567 if (nb_args >= MAX_ARGS)
2568 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002569 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002570 if (!has_arg) {
2571 if (nb_args >= MAX_ARGS)
2572 goto error_args;
2573 val = -1;
2574 goto add_num;
2575 }
2576 }
aliguori376253e2009-03-05 23:01:23 +00002577 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002578 goto fail;
2579 add_num:
bellard92a31b12005-02-10 22:00:52 +00002580 if (c == 'i') {
2581 if (nb_args >= MAX_ARGS)
2582 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002583 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002584 } else {
2585 if ((nb_args + 1) >= MAX_ARGS)
2586 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002587#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002588 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002589#else
2590 args[nb_args++] = (void *)0;
2591#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002592 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002593 }
bellard9307c4c2004-04-04 12:57:25 +00002594 }
2595 break;
2596 case '-':
2597 {
2598 int has_option;
2599 /* option */
ths3b46e622007-09-17 08:09:54 +00002600
bellard9307c4c2004-04-04 12:57:25 +00002601 c = *typestr++;
2602 if (c == '\0')
2603 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002604 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002605 p++;
2606 has_option = 0;
2607 if (*p == '-') {
2608 p++;
2609 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002610 monitor_printf(mon, "%s: unsupported option -%c\n",
2611 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002612 goto fail;
2613 }
2614 p++;
2615 has_option = 1;
2616 }
2617 if (nb_args >= MAX_ARGS)
2618 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002619 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002620 }
2621 break;
2622 default:
2623 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002624 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002625 goto fail;
2626 }
2627 }
2628 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002629 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002630 p++;
2631 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002632 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2633 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002634 goto fail;
2635 }
2636
2637 switch(nb_args) {
2638 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002639 handler_0 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002640 handler_0(mon);
bellard9307c4c2004-04-04 12:57:25 +00002641 break;
2642 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002643 handler_1 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002644 handler_1(mon, args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002645 break;
2646 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002647 handler_2 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002648 handler_2(mon, args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002649 break;
2650 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002651 handler_3 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002652 handler_3(mon, args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002653 break;
2654 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002655 handler_4 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002656 handler_4(mon, args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002657 break;
2658 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002659 handler_5 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002660 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002661 break;
bellard34405572004-06-08 00:55:58 +00002662 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002663 handler_6 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002664 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002665 break;
bellardec36b692006-07-16 18:57:03 +00002666 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002667 handler_7 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002668 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2669 args[6]);
bellardec36b692006-07-16 18:57:03 +00002670 break;
bellard9307c4c2004-04-04 12:57:25 +00002671 default:
aliguori376253e2009-03-05 23:01:23 +00002672 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
bellard9307c4c2004-04-04 12:57:25 +00002673 goto fail;
2674 }
2675 fail:
2676 for(i = 0; i < MAX_ARGS; i++)
2677 qemu_free(str_allocated[i]);
2678 return;
bellard9dc39cb2004-03-14 21:38:27 +00002679}
2680
bellard81d09122004-07-14 17:21:37 +00002681static void cmd_completion(const char *name, const char *list)
2682{
2683 const char *p, *pstart;
2684 char cmd[128];
2685 int len;
2686
2687 p = list;
2688 for(;;) {
2689 pstart = p;
2690 p = strchr(p, '|');
2691 if (!p)
2692 p = pstart + strlen(pstart);
2693 len = p - pstart;
2694 if (len > sizeof(cmd) - 2)
2695 len = sizeof(cmd) - 2;
2696 memcpy(cmd, pstart, len);
2697 cmd[len] = '\0';
2698 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002699 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002700 }
2701 if (*p == '\0')
2702 break;
2703 p++;
2704 }
2705}
2706
2707static void file_completion(const char *input)
2708{
2709 DIR *ffs;
2710 struct dirent *d;
2711 char path[1024];
2712 char file[1024], file_prefix[1024];
2713 int input_path_len;
2714 const char *p;
2715
ths5fafdf22007-09-16 21:08:06 +00002716 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002717 if (!p) {
2718 input_path_len = 0;
2719 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002720 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002721 } else {
2722 input_path_len = p - input + 1;
2723 memcpy(path, input, input_path_len);
2724 if (input_path_len > sizeof(path) - 1)
2725 input_path_len = sizeof(path) - 1;
2726 path[input_path_len] = '\0';
2727 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2728 }
2729#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002730 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2731 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002732#endif
2733 ffs = opendir(path);
2734 if (!ffs)
2735 return;
2736 for(;;) {
2737 struct stat sb;
2738 d = readdir(ffs);
2739 if (!d)
2740 break;
2741 if (strstart(d->d_name, file_prefix, NULL)) {
2742 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002743 if (input_path_len < sizeof(file))
2744 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2745 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002746 /* stat the file to find out if it's a directory.
2747 * In that case add a slash to speed up typing long paths
2748 */
2749 stat(file, &sb);
2750 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002751 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00002752 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002753 }
2754 }
2755 closedir(ffs);
2756}
2757
aliguori51de9762009-03-05 23:00:43 +00002758static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002759{
aliguori51de9762009-03-05 23:00:43 +00002760 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002761 const char *input = opaque;
2762
2763 if (input[0] == '\0' ||
2764 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00002765 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00002766 }
2767}
2768
2769/* NOTE: this parser is an approximate form of the real command parser */
2770static void parse_cmdline(const char *cmdline,
2771 int *pnb_args, char **args)
2772{
2773 const char *p;
2774 int nb_args, ret;
2775 char buf[1024];
2776
2777 p = cmdline;
2778 nb_args = 0;
2779 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002780 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002781 p++;
2782 if (*p == '\0')
2783 break;
2784 if (nb_args >= MAX_ARGS)
2785 break;
2786 ret = get_str(buf, sizeof(buf), &p);
2787 args[nb_args] = qemu_strdup(buf);
2788 nb_args++;
2789 if (ret < 0)
2790 break;
2791 }
2792 *pnb_args = nb_args;
2793}
2794
aliguori4c36ba32009-03-05 23:01:37 +00002795static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002796{
2797 const char *cmdname;
2798 char *args[MAX_ARGS];
2799 int nb_args, i, len;
2800 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00002801 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002802 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002803
2804 parse_cmdline(cmdline, &nb_args, args);
2805#ifdef DEBUG_COMPLETION
2806 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00002807 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00002808 }
2809#endif
2810
2811 /* if the line ends with a space, it means we want to complete the
2812 next arg */
2813 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002814 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002815 if (nb_args >= MAX_ARGS)
2816 return;
2817 args[nb_args++] = qemu_strdup("");
2818 }
2819 if (nb_args <= 1) {
2820 /* command completion */
2821 if (nb_args == 0)
2822 cmdname = "";
2823 else
2824 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00002825 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00002826 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002827 cmd_completion(cmdname, cmd->name);
2828 }
2829 } else {
2830 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002831 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002832 if (compare_cmd(args[0], cmd->name))
2833 goto found;
2834 }
2835 return;
2836 found:
2837 ptype = cmd->args_type;
2838 for(i = 0; i < nb_args - 2; i++) {
2839 if (*ptype != '\0') {
2840 ptype++;
2841 while (*ptype == '?')
2842 ptype++;
2843 }
2844 }
2845 str = args[nb_args - 1];
2846 switch(*ptype) {
2847 case 'F':
2848 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00002849 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00002850 file_completion(str);
2851 break;
2852 case 'B':
2853 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00002854 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00002855 bdrv_iterate(block_completion_it, (void *)str);
2856 break;
bellard7fe48482004-10-09 18:08:01 +00002857 case 's':
2858 /* XXX: more generic ? */
2859 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00002860 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00002861 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2862 cmd_completion(str, cmd->name);
2863 }
bellard64866c32006-05-07 18:03:31 +00002864 } else if (!strcmp(cmd->name, "sendkey")) {
aliguori731b0362009-03-05 23:01:42 +00002865 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00002866 for(key = key_defs; key->name != NULL; key++) {
2867 cmd_completion(str, key->name);
2868 }
bellard7fe48482004-10-09 18:08:01 +00002869 }
2870 break;
bellard81d09122004-07-14 17:21:37 +00002871 default:
2872 break;
2873 }
2874 }
2875 for(i = 0; i < nb_args; i++)
2876 qemu_free(args[i]);
2877}
2878
aliguori731b0362009-03-05 23:01:42 +00002879static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00002880{
aliguori731b0362009-03-05 23:01:42 +00002881 Monitor *mon = opaque;
2882
2883 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00002884}
2885
aliguori731b0362009-03-05 23:01:42 +00002886static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00002887{
aliguori731b0362009-03-05 23:01:42 +00002888 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00002889 int i;
aliguori376253e2009-03-05 23:01:23 +00002890
aliguori731b0362009-03-05 23:01:42 +00002891 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00002892
aliguori731b0362009-03-05 23:01:42 +00002893 for (i = 0; i < size; i++)
2894 readline_handle_byte(cur_mon->rs, buf[i]);
2895
2896 cur_mon = old_mon;
2897}
aliguorid8f44602008-10-06 13:52:44 +00002898
aliguori376253e2009-03-05 23:01:23 +00002899static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00002900{
aliguori731b0362009-03-05 23:01:42 +00002901 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00002902 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00002903 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00002904}
2905
aliguori376253e2009-03-05 23:01:23 +00002906void monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00002907{
aliguori731b0362009-03-05 23:01:42 +00002908 mon->suspend_cnt++;
aliguorid8f44602008-10-06 13:52:44 +00002909}
2910
aliguori376253e2009-03-05 23:01:23 +00002911void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00002912{
aliguori731b0362009-03-05 23:01:42 +00002913 if (--mon->suspend_cnt == 0)
2914 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00002915}
2916
aliguori731b0362009-03-05 23:01:42 +00002917static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00002918{
aliguori376253e2009-03-05 23:01:23 +00002919 Monitor *mon = opaque;
2920
aliguori2724b182009-03-05 23:01:47 +00002921 switch (event) {
2922 case CHR_EVENT_MUX_IN:
2923 readline_restart(mon->rs);
2924 monitor_resume(mon);
2925 monitor_flush(mon);
2926 break;
ths86e94de2007-01-05 22:01:59 +00002927
aliguori2724b182009-03-05 23:01:47 +00002928 case CHR_EVENT_MUX_OUT:
2929 if (mon->suspend_cnt == 0)
2930 monitor_printf(mon, "\n");
2931 monitor_flush(mon);
2932 monitor_suspend(mon);
2933 break;
2934
2935 case CHR_EVENT_RESET:
2936 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
2937 "information\n", QEMU_VERSION);
2938 if (mon->chr->focus == 0)
2939 readline_show_prompt(mon->rs);
2940 break;
2941 }
ths86e94de2007-01-05 22:01:59 +00002942}
2943
aliguori731b0362009-03-05 23:01:42 +00002944void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00002945{
aliguori731b0362009-03-05 23:01:42 +00002946 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00002947 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00002948
2949 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00002950 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00002951 is_first_init = 0;
2952 }
aliguori87127162009-03-05 23:01:29 +00002953
2954 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00002955
aliguori87127162009-03-05 23:01:29 +00002956 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00002957 mon->flags = flags;
aliguori2724b182009-03-05 23:01:47 +00002958 if (mon->chr->focus != 0)
2959 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
aliguori731b0362009-03-05 23:01:42 +00002960 mon->rs = readline_init(mon, monitor_find_completion);
2961 monitor_read_command(mon, 0);
aliguori87127162009-03-05 23:01:29 +00002962
aliguori731b0362009-03-05 23:01:42 +00002963 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
2964 mon);
aliguori87127162009-03-05 23:01:29 +00002965
2966 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00002967 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00002968 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00002969}
2970
aliguori376253e2009-03-05 23:01:23 +00002971static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00002972{
aliguoribb5fc202009-03-05 23:01:15 +00002973 BlockDriverState *bs = opaque;
2974 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00002975
aliguoribb5fc202009-03-05 23:01:15 +00002976 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00002977 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00002978 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00002979 }
aliguori731b0362009-03-05 23:01:42 +00002980 if (mon->password_completion_cb)
2981 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00002982
aliguori731b0362009-03-05 23:01:42 +00002983 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00002984}
aliguoric0f4ce72009-03-05 23:01:01 +00002985
aliguori376253e2009-03-05 23:01:23 +00002986void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00002987 BlockDriverCompletionFunc *completion_cb,
2988 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00002989{
aliguoribb5fc202009-03-05 23:01:15 +00002990 if (!bdrv_key_required(bs)) {
2991 if (completion_cb)
2992 completion_cb(opaque, 0);
2993 return;
2994 }
aliguoric0f4ce72009-03-05 23:01:01 +00002995
aliguori376253e2009-03-05 23:01:23 +00002996 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
2997 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00002998
aliguori731b0362009-03-05 23:01:42 +00002999 mon->password_completion_cb = completion_cb;
3000 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003001
aliguori376253e2009-03-05 23:01:23 +00003002 monitor_read_password(mon, bdrv_password_cb, bs);
aliguoric0f4ce72009-03-05 23:01:01 +00003003}