blob: d64f459a6ef844b4152b0f9ceb7b67e55103d006 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
26#include "hw/usb.h"
27#include "hw/pcmcia.h"
28#include "hw/pc.h"
29#include "hw/pci.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010030#include "hw/watchdog.h"
pbrook87ecb682007-11-17 17:14:51 +000031#include "gdbstub.h"
32#include "net.h"
33#include "qemu-char.h"
34#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000035#include "monitor.h"
36#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000037#include "console.h"
38#include "block.h"
39#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000040#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000041#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000042#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000043#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000044#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000045#include "acl.h"
ths6a5bd302007-12-03 17:05:38 +000046
bellard9dc39cb2004-03-14 21:38:27 +000047//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000048//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000049
bellard9307c4c2004-04-04 12:57:25 +000050/*
51 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000052 *
bellard9307c4c2004-04-04 12:57:25 +000053 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000054 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000055 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000056 * 'i' 32 bit integer
57 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000058 * '/' optional gdb-like print format (like "/10x")
59 *
60 * '?' optional type (for 'F', 's' and 'i')
61 *
62 */
63
aliguori376253e2009-03-05 23:01:23 +000064typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000065 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000066 const char *args_type;
blueswir1a5f1b962008-08-17 20:21:51 +000067 void *handler;
bellard9dc39cb2004-03-14 21:38:27 +000068 const char *params;
69 const char *help;
aliguori376253e2009-03-05 23:01:23 +000070} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000071
aliguori87127162009-03-05 23:01:29 +000072struct Monitor {
73 CharDriverState *chr;
aliguori731b0362009-03-05 23:01:42 +000074 int flags;
75 int suspend_cnt;
76 uint8_t outbuf[1024];
77 int outbuf_index;
78 ReadLineState *rs;
79 CPUState *mon_cpu;
80 BlockDriverCompletionFunc *password_completion_cb;
81 void *password_opaque;
aliguori87127162009-03-05 23:01:29 +000082 LIST_ENTRY(Monitor) entry;
83};
84
85static LIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +000086
aliguori376253e2009-03-05 23:01:23 +000087static const mon_cmd_t mon_cmds[];
88static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +000089
aliguori87127162009-03-05 23:01:29 +000090Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +000091
aliguori731b0362009-03-05 23:01:42 +000092static void monitor_command_cb(Monitor *mon, const char *cmdline,
93 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +000094
aliguori731b0362009-03-05 23:01:42 +000095static void monitor_read_command(Monitor *mon, int show_prompt)
96{
97 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
98 if (show_prompt)
99 readline_show_prompt(mon->rs);
100}
bellard6a00d602005-11-21 23:25:50 +0000101
aliguoricde76ee2009-03-05 23:01:51 +0000102static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
103 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000104{
aliguoricde76ee2009-03-05 23:01:51 +0000105 if (mon->rs) {
106 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
107 /* prompt is printed on return from the command handler */
108 return 0;
109 } else {
110 monitor_printf(mon, "terminal does not support password prompting\n");
111 return -ENOTTY;
112 }
aliguoribb5fc202009-03-05 23:01:15 +0000113}
114
aliguori376253e2009-03-05 23:01:23 +0000115void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000116{
aliguori731b0362009-03-05 23:01:42 +0000117 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
118 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
119 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000120 }
121}
122
123/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000124static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000125{
ths60fe76f2007-12-16 03:02:09 +0000126 char c;
aliguori731b0362009-03-05 23:01:42 +0000127
128 if (!mon)
129 return;
130
bellard7e2515e2004-08-01 21:52:19 +0000131 for(;;) {
132 c = *str++;
133 if (c == '\0')
134 break;
bellard7ba12602006-07-14 20:26:42 +0000135 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000136 mon->outbuf[mon->outbuf_index++] = '\r';
137 mon->outbuf[mon->outbuf_index++] = c;
138 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
139 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000140 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000141 }
142}
143
aliguori376253e2009-03-05 23:01:23 +0000144void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000145{
146 char buf[4096];
147 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000148 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000149}
150
aliguori376253e2009-03-05 23:01:23 +0000151void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000152{
153 va_list ap;
154 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000155 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000156 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000157}
158
aliguori376253e2009-03-05 23:01:23 +0000159void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000160{
161 int i;
162
163 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000164 switch (filename[i]) {
165 case ' ':
166 case '"':
167 case '\\':
168 monitor_printf(mon, "\\%c", filename[i]);
169 break;
170 case '\t':
171 monitor_printf(mon, "\\t");
172 break;
173 case '\r':
174 monitor_printf(mon, "\\r");
175 break;
176 case '\n':
177 monitor_printf(mon, "\\n");
178 break;
179 default:
180 monitor_printf(mon, "%c", filename[i]);
181 break;
182 }
thsfef30742006-12-22 14:11:32 +0000183 }
184}
185
bellard7fe48482004-10-09 18:08:01 +0000186static int monitor_fprintf(FILE *stream, const char *fmt, ...)
187{
188 va_list ap;
189 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000190 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000191 va_end(ap);
192 return 0;
193}
194
bellard9dc39cb2004-03-14 21:38:27 +0000195static int compare_cmd(const char *name, const char *list)
196{
197 const char *p, *pstart;
198 int len;
199 len = strlen(name);
200 p = list;
201 for(;;) {
202 pstart = p;
203 p = strchr(p, '|');
204 if (!p)
205 p = pstart + strlen(pstart);
206 if ((p - pstart) == len && !memcmp(pstart, name, len))
207 return 1;
208 if (*p == '\0')
209 break;
210 p++;
211 }
212 return 0;
213}
214
aliguori376253e2009-03-05 23:01:23 +0000215static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
216 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000217{
aliguori376253e2009-03-05 23:01:23 +0000218 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000219
220 for(cmd = cmds; cmd->name != NULL; cmd++) {
221 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000222 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
223 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000224 }
225}
226
aliguori376253e2009-03-05 23:01:23 +0000227static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000228{
229 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000230 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000231 } else {
aliguori376253e2009-03-05 23:01:23 +0000232 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000233 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000234 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000235 monitor_printf(mon, "Log items (comma separated):\n");
236 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000237 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000238 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000239 }
240 }
bellard9dc39cb2004-03-14 21:38:27 +0000241 }
242}
243
aliguori376253e2009-03-05 23:01:23 +0000244static void do_commit(Monitor *mon, const char *device)
bellard9dc39cb2004-03-14 21:38:27 +0000245{
bellard7954c732006-08-01 15:52:40 +0000246 int i, all_devices;
balrog2dc7b602007-05-24 18:53:22 +0000247
bellard7954c732006-08-01 15:52:40 +0000248 all_devices = !strcmp(device, "all");
thse4bcb142007-12-02 04:51:10 +0000249 for (i = 0; i < nb_drives; i++) {
ths5fafdf22007-09-16 21:08:06 +0000250 if (all_devices ||
thse4bcb142007-12-02 04:51:10 +0000251 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
252 bdrv_commit(drives_table[i].bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000253 }
254}
255
aliguori376253e2009-03-05 23:01:23 +0000256static void do_info(Monitor *mon, const char *item)
bellard9dc39cb2004-03-14 21:38:27 +0000257{
aliguori376253e2009-03-05 23:01:23 +0000258 const mon_cmd_t *cmd;
259 void (*handler)(Monitor *);
bellard9dc39cb2004-03-14 21:38:27 +0000260
bellard9307c4c2004-04-04 12:57:25 +0000261 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000262 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000263 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000264 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000265 goto found;
266 }
267 help:
aliguori376253e2009-03-05 23:01:23 +0000268 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000269 return;
270 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000271 handler = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +0000272 handler(mon);
bellard9dc39cb2004-03-14 21:38:27 +0000273}
274
aliguori376253e2009-03-05 23:01:23 +0000275static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000276{
pbrook4a19f1e2009-04-07 23:17:49 +0000277 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000278}
279
aliguori376253e2009-03-05 23:01:23 +0000280static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000281{
282 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000283 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000284}
285
aurel32bf4f74c2008-12-18 22:42:34 +0000286#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000287static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000288{
aliguori376253e2009-03-05 23:01:23 +0000289 monitor_printf(mon, "HPET is %s by QEMU\n",
290 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000291}
aurel32bf4f74c2008-12-18 22:42:34 +0000292#endif
aliguori16b29ae2008-12-17 23:28:44 +0000293
aliguori376253e2009-03-05 23:01:23 +0000294static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000295{
aliguori376253e2009-03-05 23:01:23 +0000296 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
297 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
298 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
299 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
300 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000301}
302
bellard6a00d602005-11-21 23:25:50 +0000303/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000304static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000305{
306 CPUState *env;
307
308 for(env = first_cpu; env != NULL; env = env->next_cpu) {
309 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000310 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000311 return 0;
312 }
313 }
314 return -1;
315}
316
pbrook9596ebb2007-11-18 01:44:38 +0000317static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000318{
aliguori731b0362009-03-05 23:01:42 +0000319 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000320 mon_set_cpu(0);
321 }
aliguorid1546152009-03-12 20:12:57 +0000322 cpu_synchronize_state(cur_mon->mon_cpu, 0);
aliguori731b0362009-03-05 23:01:42 +0000323 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000324}
325
aliguori376253e2009-03-05 23:01:23 +0000326static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000327{
bellard6a00d602005-11-21 23:25:50 +0000328 CPUState *env;
329 env = mon_get_cpu();
330 if (!env)
331 return;
bellard9307c4c2004-04-04 12:57:25 +0000332#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000333 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000334 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000335#else
aliguori376253e2009-03-05 23:01:23 +0000336 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000337 0);
bellard9307c4c2004-04-04 12:57:25 +0000338#endif
339}
340
aliguori376253e2009-03-05 23:01:23 +0000341static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000342{
343 CPUState *env;
344
345 /* just to set the default cpu if not already done */
346 mon_get_cpu();
347
348 for(env = first_cpu; env != NULL; env = env->next_cpu) {
aliguorid1546152009-03-12 20:12:57 +0000349 cpu_synchronize_state(env, 0);
aliguori376253e2009-03-05 23:01:23 +0000350 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000351 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000352 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000353#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000354 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
355 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000356#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000357 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000358#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000359 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
360 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000361#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000362 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000363#endif
thsead93602007-09-06 00:18:15 +0000364 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000365 monitor_printf(mon, " (halted)");
366 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000367 }
368}
369
aliguori376253e2009-03-05 23:01:23 +0000370static void do_cpu_set(Monitor *mon, int index)
bellard6a00d602005-11-21 23:25:50 +0000371{
372 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000373 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000374}
375
aliguori376253e2009-03-05 23:01:23 +0000376static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000377{
aliguori376253e2009-03-05 23:01:23 +0000378 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000379}
380
aliguori376253e2009-03-05 23:01:23 +0000381static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000382{
383 int i;
bellard7e2515e2004-08-01 21:52:19 +0000384 const char *str;
ths3b46e622007-09-17 08:09:54 +0000385
aliguoricde76ee2009-03-05 23:01:51 +0000386 if (!mon->rs)
387 return;
bellard7e2515e2004-08-01 21:52:19 +0000388 i = 0;
389 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000390 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000391 if (!str)
392 break;
aliguori376253e2009-03-05 23:01:23 +0000393 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000394 i++;
bellardaa455482004-04-04 13:07:25 +0000395 }
396}
397
j_mayer76a66252007-03-07 08:32:30 +0000398#if defined(TARGET_PPC)
399/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000400static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000401{
402 CPUState *env;
403
404 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000405 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000406}
407#endif
408
aliguori376253e2009-03-05 23:01:23 +0000409static void do_quit(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000410{
411 exit(0);
412}
413
aliguori376253e2009-03-05 23:01:23 +0000414static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000415{
416 if (bdrv_is_inserted(bs)) {
417 if (!force) {
418 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000419 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000420 return -1;
421 }
422 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000423 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000424 return -1;
425 }
426 }
427 bdrv_close(bs);
428 }
429 return 0;
430}
431
aliguori376253e2009-03-05 23:01:23 +0000432static void do_eject(Monitor *mon, int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000433{
434 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000435
bellard9307c4c2004-04-04 12:57:25 +0000436 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000437 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000438 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000439 return;
440 }
aliguori376253e2009-03-05 23:01:23 +0000441 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000442}
443
aliguori376253e2009-03-05 23:01:23 +0000444static void do_change_block(Monitor *mon, const char *device,
445 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000446{
447 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000448 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000449
bellard9307c4c2004-04-04 12:57:25 +0000450 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000451 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000452 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000453 return;
454 }
aurel322ecea9b2008-06-18 22:10:01 +0000455 if (fmt) {
456 drv = bdrv_find_format(fmt);
457 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000458 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000459 return;
460 }
461 }
aliguori376253e2009-03-05 23:01:23 +0000462 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000463 return;
aurel322ecea9b2008-06-18 22:10:01 +0000464 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000465 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000466}
467
aliguori376253e2009-03-05 23:01:23 +0000468static void change_vnc_password_cb(Monitor *mon, const char *password,
469 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000470{
471 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000472 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000473
aliguori731b0362009-03-05 23:01:42 +0000474 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000475}
476
aliguori376253e2009-03-05 23:01:23 +0000477static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000478{
ths70848512007-08-25 01:37:05 +0000479 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000480 strcmp(target, "password") == 0) {
481 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000482 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000483 strncpy(password, arg, sizeof(password));
484 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000485 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000486 } else {
aliguori376253e2009-03-05 23:01:23 +0000487 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000488 }
ths70848512007-08-25 01:37:05 +0000489 } else {
aliguori28a76be2009-03-06 20:27:40 +0000490 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000491 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000492 }
thse25a5822007-08-25 01:36:20 +0000493}
494
aliguori376253e2009-03-05 23:01:23 +0000495static void do_change(Monitor *mon, const char *device, const char *target,
496 const char *arg)
thse25a5822007-08-25 01:36:20 +0000497{
498 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000499 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000500 } else {
aliguori28a76be2009-03-06 20:27:40 +0000501 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000502 }
503}
504
aliguori376253e2009-03-05 23:01:23 +0000505static void do_screen_dump(Monitor *mon, const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000506{
pbrook95219892006-04-09 01:06:34 +0000507 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000508}
509
aliguori376253e2009-03-05 23:01:23 +0000510static void do_logfile(Monitor *mon, const char *filename)
pbrooke735b912007-06-30 13:53:24 +0000511{
512 cpu_set_log_filename(filename);
513}
514
aliguori376253e2009-03-05 23:01:23 +0000515static void do_log(Monitor *mon, const char *items)
bellardf193c792004-03-21 17:06:25 +0000516{
517 int mask;
ths3b46e622007-09-17 08:09:54 +0000518
bellard9307c4c2004-04-04 12:57:25 +0000519 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000520 mask = 0;
521 } else {
bellard9307c4c2004-04-04 12:57:25 +0000522 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000523 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000524 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000525 return;
526 }
527 }
528 cpu_set_log(mask);
529}
530
aurel321b530a62009-04-05 20:08:59 +0000531static void do_singlestep(Monitor *mon, const char *option)
532{
533 if (!option || !strcmp(option, "on")) {
534 singlestep = 1;
535 } else if (!strcmp(option, "off")) {
536 singlestep = 0;
537 } else {
538 monitor_printf(mon, "unexpected option %s\n", option);
539 }
540}
541
aliguori376253e2009-03-05 23:01:23 +0000542static void do_stop(Monitor *mon)
bellard8a7ddc32004-03-31 19:00:16 +0000543{
544 vm_stop(EXCP_INTERRUPT);
545}
546
aliguoribb5fc202009-03-05 23:01:15 +0000547static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000548
aliguori376253e2009-03-05 23:01:23 +0000549struct bdrv_iterate_context {
550 Monitor *mon;
551 int err;
552};
aliguoric0f4ce72009-03-05 23:01:01 +0000553
aliguori376253e2009-03-05 23:01:23 +0000554static void do_cont(Monitor *mon)
555{
556 struct bdrv_iterate_context context = { mon, 0 };
557
558 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000559 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000560 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000561 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000562}
563
aliguoribb5fc202009-03-05 23:01:15 +0000564static void bdrv_key_cb(void *opaque, int err)
565{
aliguori376253e2009-03-05 23:01:23 +0000566 Monitor *mon = opaque;
567
aliguoribb5fc202009-03-05 23:01:15 +0000568 /* another key was set successfully, retry to continue */
569 if (!err)
aliguori376253e2009-03-05 23:01:23 +0000570 do_cont(mon);
aliguoribb5fc202009-03-05 23:01:15 +0000571}
572
573static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
574{
aliguori376253e2009-03-05 23:01:23 +0000575 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000576
aliguori376253e2009-03-05 23:01:23 +0000577 if (!context->err && bdrv_key_required(bs)) {
578 context->err = -EBUSY;
579 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
580 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000581 }
582}
583
bellard67b915a2004-03-31 23:37:16 +0000584#ifdef CONFIG_GDBSTUB
aliguori59030a82009-04-05 18:43:41 +0000585static void do_gdbserver(Monitor *mon, const char *device)
bellard8a7ddc32004-03-31 19:00:16 +0000586{
aliguori59030a82009-04-05 18:43:41 +0000587 if (!device)
588 device = "tcp::" DEFAULT_GDBSTUB_PORT;
589 if (gdbserver_start(device) < 0) {
590 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
591 device);
592 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000593 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000594 } else {
aliguori59030a82009-04-05 18:43:41 +0000595 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
596 device);
bellard8a7ddc32004-03-31 19:00:16 +0000597 }
598}
bellard67b915a2004-03-31 23:37:16 +0000599#endif
bellard8a7ddc32004-03-31 19:00:16 +0000600
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100601static void do_watchdog_action(Monitor *mon, const char *action)
602{
603 if (select_watchdog_action(action) == -1) {
604 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
605 }
606}
607
aliguori376253e2009-03-05 23:01:23 +0000608static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000609{
aliguori376253e2009-03-05 23:01:23 +0000610 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000611 switch(c) {
612 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000613 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000614 break;
615 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000616 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000617 break;
618 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000619 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000620 break;
621 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000622 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000623 break;
624 default:
625 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000626 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000627 } else {
aliguori376253e2009-03-05 23:01:23 +0000628 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000629 }
630 break;
631 }
aliguori376253e2009-03-05 23:01:23 +0000632 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000633}
634
aliguori376253e2009-03-05 23:01:23 +0000635static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000636 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000637{
bellard6a00d602005-11-21 23:25:50 +0000638 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000639 int nb_per_line, l, line_size, i, max_digits, len;
640 uint8_t buf[16];
641 uint64_t v;
642
643 if (format == 'i') {
644 int flags;
645 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000646 env = mon_get_cpu();
647 if (!env && !is_physical)
648 return;
bellard9307c4c2004-04-04 12:57:25 +0000649#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000650 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000651 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000652 } else if (wsize == 4) {
653 flags = 0;
654 } else {
bellard6a15fd12006-04-12 21:07:07 +0000655 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000656 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000657 if (env) {
658#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000659 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000660 (env->segs[R_CS].flags & DESC_L_MASK))
661 flags = 2;
662 else
663#endif
664 if (!(env->segs[R_CS].flags & DESC_B_MASK))
665 flags = 1;
666 }
bellard4c27ba22004-04-25 18:05:08 +0000667 }
668#endif
aliguori376253e2009-03-05 23:01:23 +0000669 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000670 return;
671 }
672
673 len = wsize * count;
674 if (wsize == 1)
675 line_size = 8;
676 else
677 line_size = 16;
678 nb_per_line = line_size / wsize;
679 max_digits = 0;
680
681 switch(format) {
682 case 'o':
683 max_digits = (wsize * 8 + 2) / 3;
684 break;
685 default:
686 case 'x':
687 max_digits = (wsize * 8) / 4;
688 break;
689 case 'u':
690 case 'd':
691 max_digits = (wsize * 8 * 10 + 32) / 33;
692 break;
693 case 'c':
694 wsize = 1;
695 break;
696 }
697
698 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000699 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000700 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000701 else
aliguori376253e2009-03-05 23:01:23 +0000702 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000703 l = len;
704 if (l > line_size)
705 l = line_size;
706 if (is_physical) {
707 cpu_physical_memory_rw(addr, buf, l, 0);
708 } else {
bellard6a00d602005-11-21 23:25:50 +0000709 env = mon_get_cpu();
710 if (!env)
711 break;
aliguoric8f79b62008-08-18 14:00:20 +0000712 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000713 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000714 break;
715 }
bellard9307c4c2004-04-04 12:57:25 +0000716 }
ths5fafdf22007-09-16 21:08:06 +0000717 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000718 while (i < l) {
719 switch(wsize) {
720 default:
721 case 1:
722 v = ldub_raw(buf + i);
723 break;
724 case 2:
725 v = lduw_raw(buf + i);
726 break;
727 case 4:
bellard92a31b12005-02-10 22:00:52 +0000728 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000729 break;
730 case 8:
731 v = ldq_raw(buf + i);
732 break;
733 }
aliguori376253e2009-03-05 23:01:23 +0000734 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000735 switch(format) {
736 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000737 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000738 break;
739 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000740 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000741 break;
742 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000743 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000744 break;
745 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000746 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000747 break;
748 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000749 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000750 break;
751 }
752 i += wsize;
753 }
aliguori376253e2009-03-05 23:01:23 +0000754 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000755 addr += l;
756 len -= l;
757 }
758}
759
bellard92a31b12005-02-10 22:00:52 +0000760#if TARGET_LONG_BITS == 64
761#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
762#else
763#define GET_TLONG(h, l) (l)
764#endif
765
aliguori376253e2009-03-05 23:01:23 +0000766static void do_memory_dump(Monitor *mon, int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000767 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000768{
bellard92a31b12005-02-10 22:00:52 +0000769 target_long addr = GET_TLONG(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000770 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000771}
772
blueswir17743e582007-09-24 18:39:04 +0000773#if TARGET_PHYS_ADDR_BITS > 32
774#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
775#else
776#define GET_TPHYSADDR(h, l) (l)
777#endif
778
aliguori376253e2009-03-05 23:01:23 +0000779static void do_physical_memory_dump(Monitor *mon, int count, int format,
780 int size, uint32_t addrh, uint32_t addrl)
bellard92a31b12005-02-10 22:00:52 +0000781
bellard9307c4c2004-04-04 12:57:25 +0000782{
blueswir17743e582007-09-24 18:39:04 +0000783 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000784 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000785}
786
aliguori376253e2009-03-05 23:01:23 +0000787static void do_print(Monitor *mon, int count, int format, int size,
788 unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000789{
blueswir17743e582007-09-24 18:39:04 +0000790 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
791#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000792 switch(format) {
793 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000794 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000795 break;
796 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000797 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000798 break;
799 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000800 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000801 break;
802 default:
803 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000804 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000805 break;
806 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000807 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000808 break;
809 }
bellard92a31b12005-02-10 22:00:52 +0000810#else
811 switch(format) {
812 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000813 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000814 break;
815 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000816 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000817 break;
818 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000819 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000820 break;
821 default:
822 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000823 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000824 break;
825 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000826 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000827 break;
828 }
829#endif
aliguori376253e2009-03-05 23:01:23 +0000830 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000831}
832
aliguori376253e2009-03-05 23:01:23 +0000833static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000834 uint32_t size, const char *filename)
835{
836 FILE *f;
837 target_long addr = GET_TLONG(valh, vall);
838 uint32_t l;
839 CPUState *env;
840 uint8_t buf[1024];
841
842 env = mon_get_cpu();
843 if (!env)
844 return;
845
846 f = fopen(filename, "wb");
847 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000848 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000849 return;
850 }
851 while (size != 0) {
852 l = sizeof(buf);
853 if (l > size)
854 l = size;
855 cpu_memory_rw_debug(env, addr, buf, l, 0);
856 fwrite(buf, 1, l, f);
857 addr += l;
858 size -= l;
859 }
860 fclose(f);
861}
862
aliguori376253e2009-03-05 23:01:23 +0000863static void do_physical_memory_save(Monitor *mon, unsigned int valh,
864 unsigned int vall, uint32_t size,
865 const char *filename)
aurel32a8bdf7a2008-04-11 21:36:14 +0000866{
867 FILE *f;
868 uint32_t l;
869 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000870 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000871
872 f = fopen(filename, "wb");
873 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000874 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000875 return;
876 }
877 while (size != 0) {
878 l = sizeof(buf);
879 if (l > size)
880 l = size;
881 cpu_physical_memory_rw(addr, buf, l, 0);
882 fwrite(buf, 1, l, f);
883 fflush(f);
884 addr += l;
885 size -= l;
886 }
887 fclose(f);
888}
889
aliguori376253e2009-03-05 23:01:23 +0000890static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
bellarde4cf1ad2005-06-04 20:15:57 +0000891{
892 uint32_t addr;
893 uint8_t buf[1];
894 uint16_t sum;
895
896 sum = 0;
897 for(addr = start; addr < (start + size); addr++) {
898 cpu_physical_memory_rw(addr, buf, 1, 0);
899 /* BSD sum algorithm ('sum' Unix command) */
900 sum = (sum >> 1) | (sum << 15);
901 sum += buf[0];
902 }
aliguori376253e2009-03-05 23:01:23 +0000903 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000904}
905
bellarda3a91a32004-06-04 11:06:21 +0000906typedef struct {
907 int keycode;
908 const char *name;
909} KeyDef;
910
911static const KeyDef key_defs[] = {
912 { 0x2a, "shift" },
913 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000914
bellarda3a91a32004-06-04 11:06:21 +0000915 { 0x38, "alt" },
916 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000917 { 0x64, "altgr" },
918 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000919 { 0x1d, "ctrl" },
920 { 0x9d, "ctrl_r" },
921
922 { 0xdd, "menu" },
923
924 { 0x01, "esc" },
925
926 { 0x02, "1" },
927 { 0x03, "2" },
928 { 0x04, "3" },
929 { 0x05, "4" },
930 { 0x06, "5" },
931 { 0x07, "6" },
932 { 0x08, "7" },
933 { 0x09, "8" },
934 { 0x0a, "9" },
935 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000936 { 0x0c, "minus" },
937 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000938 { 0x0e, "backspace" },
939
940 { 0x0f, "tab" },
941 { 0x10, "q" },
942 { 0x11, "w" },
943 { 0x12, "e" },
944 { 0x13, "r" },
945 { 0x14, "t" },
946 { 0x15, "y" },
947 { 0x16, "u" },
948 { 0x17, "i" },
949 { 0x18, "o" },
950 { 0x19, "p" },
951
952 { 0x1c, "ret" },
953
954 { 0x1e, "a" },
955 { 0x1f, "s" },
956 { 0x20, "d" },
957 { 0x21, "f" },
958 { 0x22, "g" },
959 { 0x23, "h" },
960 { 0x24, "j" },
961 { 0x25, "k" },
962 { 0x26, "l" },
963
964 { 0x2c, "z" },
965 { 0x2d, "x" },
966 { 0x2e, "c" },
967 { 0x2f, "v" },
968 { 0x30, "b" },
969 { 0x31, "n" },
970 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000971 { 0x33, "comma" },
972 { 0x34, "dot" },
973 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000974
balrog4d3b6f62008-02-10 16:33:14 +0000975 { 0x37, "asterisk" },
976
bellarda3a91a32004-06-04 11:06:21 +0000977 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000978 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000979 { 0x3b, "f1" },
980 { 0x3c, "f2" },
981 { 0x3d, "f3" },
982 { 0x3e, "f4" },
983 { 0x3f, "f5" },
984 { 0x40, "f6" },
985 { 0x41, "f7" },
986 { 0x42, "f8" },
987 { 0x43, "f9" },
988 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +0000989 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000990 { 0x46, "scroll_lock" },
991
bellard64866c32006-05-07 18:03:31 +0000992 { 0xb5, "kp_divide" },
993 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +0000994 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +0000995 { 0x4e, "kp_add" },
996 { 0x9c, "kp_enter" },
997 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +0000998 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +0000999
1000 { 0x52, "kp_0" },
1001 { 0x4f, "kp_1" },
1002 { 0x50, "kp_2" },
1003 { 0x51, "kp_3" },
1004 { 0x4b, "kp_4" },
1005 { 0x4c, "kp_5" },
1006 { 0x4d, "kp_6" },
1007 { 0x47, "kp_7" },
1008 { 0x48, "kp_8" },
1009 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001010
bellarda3a91a32004-06-04 11:06:21 +00001011 { 0x56, "<" },
1012
1013 { 0x57, "f11" },
1014 { 0x58, "f12" },
1015
1016 { 0xb7, "print" },
1017
1018 { 0xc7, "home" },
1019 { 0xc9, "pgup" },
1020 { 0xd1, "pgdn" },
1021 { 0xcf, "end" },
1022
1023 { 0xcb, "left" },
1024 { 0xc8, "up" },
1025 { 0xd0, "down" },
1026 { 0xcd, "right" },
1027
1028 { 0xd2, "insert" },
1029 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001030#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1031 { 0xf0, "stop" },
1032 { 0xf1, "again" },
1033 { 0xf2, "props" },
1034 { 0xf3, "undo" },
1035 { 0xf4, "front" },
1036 { 0xf5, "copy" },
1037 { 0xf6, "open" },
1038 { 0xf7, "paste" },
1039 { 0xf8, "find" },
1040 { 0xf9, "cut" },
1041 { 0xfa, "lf" },
1042 { 0xfb, "help" },
1043 { 0xfc, "meta_l" },
1044 { 0xfd, "meta_r" },
1045 { 0xfe, "compose" },
1046#endif
bellarda3a91a32004-06-04 11:06:21 +00001047 { 0, NULL },
1048};
1049
1050static int get_keycode(const char *key)
1051{
1052 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001053 char *endp;
1054 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001055
1056 for(p = key_defs; p->name != NULL; p++) {
1057 if (!strcmp(key, p->name))
1058 return p->keycode;
1059 }
bellard64866c32006-05-07 18:03:31 +00001060 if (strstart(key, "0x", NULL)) {
1061 ret = strtoul(key, &endp, 0);
1062 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1063 return ret;
1064 }
bellarda3a91a32004-06-04 11:06:21 +00001065 return -1;
1066}
1067
balrogc8256f92008-06-08 22:45:01 +00001068#define MAX_KEYCODES 16
1069static uint8_t keycodes[MAX_KEYCODES];
1070static int nb_pending_keycodes;
1071static QEMUTimer *key_timer;
1072
1073static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001074{
balrogc8256f92008-06-08 22:45:01 +00001075 int keycode;
1076
1077 while (nb_pending_keycodes > 0) {
1078 nb_pending_keycodes--;
1079 keycode = keycodes[nb_pending_keycodes];
1080 if (keycode & 0x80)
1081 kbd_put_keycode(0xe0);
1082 kbd_put_keycode(keycode | 0x80);
1083 }
1084}
1085
aliguori376253e2009-03-05 23:01:23 +00001086static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1087 int hold_time)
balrogc8256f92008-06-08 22:45:01 +00001088{
balrog3401c0d2008-06-04 10:05:59 +00001089 char keyname_buf[16];
1090 char *separator;
1091 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +00001092
balrogc8256f92008-06-08 22:45:01 +00001093 if (nb_pending_keycodes > 0) {
1094 qemu_del_timer(key_timer);
1095 release_keys(NULL);
1096 }
1097 if (!has_hold_time)
1098 hold_time = 100;
1099 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001100 while (1) {
1101 separator = strchr(string, '-');
1102 keyname_len = separator ? separator - string : strlen(string);
1103 if (keyname_len > 0) {
1104 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1105 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001106 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001107 return;
bellarda3a91a32004-06-04 11:06:21 +00001108 }
balrogc8256f92008-06-08 22:45:01 +00001109 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001110 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001111 return;
1112 }
1113 keyname_buf[keyname_len] = 0;
1114 keycode = get_keycode(keyname_buf);
1115 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001116 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001117 return;
1118 }
balrogc8256f92008-06-08 22:45:01 +00001119 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001120 }
balrog3401c0d2008-06-04 10:05:59 +00001121 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001122 break;
balrog3401c0d2008-06-04 10:05:59 +00001123 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001124 }
balrogc8256f92008-06-08 22:45:01 +00001125 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001126 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001127 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001128 keycode = keycodes[i];
1129 if (keycode & 0x80)
1130 kbd_put_keycode(0xe0);
1131 kbd_put_keycode(keycode & 0x7f);
1132 }
balrogc8256f92008-06-08 22:45:01 +00001133 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001134 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1135 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001136}
1137
bellard13224a82006-07-14 22:03:35 +00001138static int mouse_button_state;
1139
aliguori376253e2009-03-05 23:01:23 +00001140static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001141 const char *dz_str)
1142{
1143 int dx, dy, dz;
1144 dx = strtol(dx_str, NULL, 0);
1145 dy = strtol(dy_str, NULL, 0);
1146 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001147 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001148 dz = strtol(dz_str, NULL, 0);
1149 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1150}
1151
aliguori376253e2009-03-05 23:01:23 +00001152static void do_mouse_button(Monitor *mon, int button_state)
bellard13224a82006-07-14 22:03:35 +00001153{
1154 mouse_button_state = button_state;
1155 kbd_mouse_event(0, 0, 0, mouse_button_state);
1156}
1157
aliguori376253e2009-03-05 23:01:23 +00001158static void do_ioport_read(Monitor *mon, int count, int format, int size,
1159 int addr, int has_index, int index)
bellard34405572004-06-08 00:55:58 +00001160{
1161 uint32_t val;
1162 int suffix;
1163
1164 if (has_index) {
1165 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1166 addr++;
1167 }
1168 addr &= 0xffff;
1169
1170 switch(size) {
1171 default:
1172 case 1:
1173 val = cpu_inb(NULL, addr);
1174 suffix = 'b';
1175 break;
1176 case 2:
1177 val = cpu_inw(NULL, addr);
1178 suffix = 'w';
1179 break;
1180 case 4:
1181 val = cpu_inl(NULL, addr);
1182 suffix = 'l';
1183 break;
1184 }
aliguori376253e2009-03-05 23:01:23 +00001185 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1186 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001187}
bellarda3a91a32004-06-04 11:06:21 +00001188
blueswir13b4366d2008-06-20 16:25:06 +00001189/* boot_set handler */
1190static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1191static void *boot_opaque;
1192
1193void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1194{
1195 qemu_boot_set_handler = func;
1196 boot_opaque = opaque;
1197}
1198
aliguori376253e2009-03-05 23:01:23 +00001199static void do_boot_set(Monitor *mon, const char *bootdevice)
aurel320ecdffb2008-05-04 20:11:34 +00001200{
1201 int res;
1202
1203 if (qemu_boot_set_handler) {
blueswir13b4366d2008-06-20 16:25:06 +00001204 res = qemu_boot_set_handler(boot_opaque, bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001205 if (res == 0)
aliguori376253e2009-03-05 23:01:23 +00001206 monitor_printf(mon, "boot device list now set to %s\n",
1207 bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001208 else
aliguori376253e2009-03-05 23:01:23 +00001209 monitor_printf(mon, "setting boot device list failed with "
1210 "error %i\n", res);
aurel320ecdffb2008-05-04 20:11:34 +00001211 } else {
aliguori376253e2009-03-05 23:01:23 +00001212 monitor_printf(mon, "no function defined to set boot device list for "
1213 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001214 }
1215}
1216
aliguori376253e2009-03-05 23:01:23 +00001217static void do_system_reset(Monitor *mon)
bellarde4f90822004-06-20 12:35:44 +00001218{
1219 qemu_system_reset_request();
1220}
1221
aliguori376253e2009-03-05 23:01:23 +00001222static void do_system_powerdown(Monitor *mon)
bellard34751872005-07-02 14:31:34 +00001223{
1224 qemu_system_powerdown_request();
1225}
1226
bellardb86bda52004-09-18 19:32:46 +00001227#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001228static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001229{
aliguori376253e2009-03-05 23:01:23 +00001230 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1231 addr,
1232 pte & mask,
1233 pte & PG_GLOBAL_MASK ? 'G' : '-',
1234 pte & PG_PSE_MASK ? 'P' : '-',
1235 pte & PG_DIRTY_MASK ? 'D' : '-',
1236 pte & PG_ACCESSED_MASK ? 'A' : '-',
1237 pte & PG_PCD_MASK ? 'C' : '-',
1238 pte & PG_PWT_MASK ? 'T' : '-',
1239 pte & PG_USER_MASK ? 'U' : '-',
1240 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001241}
1242
aliguori376253e2009-03-05 23:01:23 +00001243static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001244{
bellard6a00d602005-11-21 23:25:50 +00001245 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001246 int l1, l2;
1247 uint32_t pgd, pde, pte;
1248
bellard6a00d602005-11-21 23:25:50 +00001249 env = mon_get_cpu();
1250 if (!env)
1251 return;
1252
bellardb86bda52004-09-18 19:32:46 +00001253 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001254 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001255 return;
1256 }
1257 pgd = env->cr[3] & ~0xfff;
1258 for(l1 = 0; l1 < 1024; l1++) {
1259 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1260 pde = le32_to_cpu(pde);
1261 if (pde & PG_PRESENT_MASK) {
1262 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001263 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001264 } else {
1265 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001266 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001267 (uint8_t *)&pte, 4);
1268 pte = le32_to_cpu(pte);
1269 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001270 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001271 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001272 ~0xfff);
1273 }
1274 }
1275 }
1276 }
1277 }
1278}
1279
aliguori376253e2009-03-05 23:01:23 +00001280static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001281 uint32_t end, int prot)
1282{
bellard9746b152004-11-11 18:30:24 +00001283 int prot1;
1284 prot1 = *plast_prot;
1285 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001286 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001287 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1288 *pstart, end, end - *pstart,
1289 prot1 & PG_USER_MASK ? 'u' : '-',
1290 'r',
1291 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001292 }
1293 if (prot != 0)
1294 *pstart = end;
1295 else
1296 *pstart = -1;
1297 *plast_prot = prot;
1298 }
1299}
1300
aliguori376253e2009-03-05 23:01:23 +00001301static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001302{
bellard6a00d602005-11-21 23:25:50 +00001303 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001304 int l1, l2, prot, last_prot;
1305 uint32_t pgd, pde, pte, start, end;
1306
bellard6a00d602005-11-21 23:25:50 +00001307 env = mon_get_cpu();
1308 if (!env)
1309 return;
1310
bellardb86bda52004-09-18 19:32:46 +00001311 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001312 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001313 return;
1314 }
1315 pgd = env->cr[3] & ~0xfff;
1316 last_prot = 0;
1317 start = -1;
1318 for(l1 = 0; l1 < 1024; l1++) {
1319 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1320 pde = le32_to_cpu(pde);
1321 end = l1 << 22;
1322 if (pde & PG_PRESENT_MASK) {
1323 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1324 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001325 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001326 } else {
1327 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001328 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001329 (uint8_t *)&pte, 4);
1330 pte = le32_to_cpu(pte);
1331 end = (l1 << 22) + (l2 << 12);
1332 if (pte & PG_PRESENT_MASK) {
1333 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1334 } else {
1335 prot = 0;
1336 }
aliguori376253e2009-03-05 23:01:23 +00001337 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001338 }
1339 }
1340 } else {
1341 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001342 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001343 }
1344 }
1345}
1346#endif
1347
aurel327c664e22009-03-03 06:12:22 +00001348#if defined(TARGET_SH4)
1349
aliguori376253e2009-03-05 23:01:23 +00001350static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001351{
aliguori376253e2009-03-05 23:01:23 +00001352 monitor_printf(mon, " tlb%i:\t"
1353 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1354 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1355 "dirty=%hhu writethrough=%hhu\n",
1356 idx,
1357 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1358 tlb->v, tlb->sh, tlb->c, tlb->pr,
1359 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001360}
1361
aliguori376253e2009-03-05 23:01:23 +00001362static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001363{
1364 CPUState *env = mon_get_cpu();
1365 int i;
1366
aliguori376253e2009-03-05 23:01:23 +00001367 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001368 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001369 print_tlb (mon, i, &env->itlb[i]);
1370 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001371 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001372 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001373}
1374
1375#endif
1376
aliguori376253e2009-03-05 23:01:23 +00001377static void do_info_kqemu(Monitor *mon)
bellard0f4c6412005-07-24 18:10:56 +00001378{
blueswir1640f42e2009-04-19 10:18:01 +00001379#ifdef CONFIG_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001380 CPUState *env;
bellard0f4c6412005-07-24 18:10:56 +00001381 int val;
1382 val = 0;
bellard6a00d602005-11-21 23:25:50 +00001383 env = mon_get_cpu();
1384 if (!env) {
aliguori376253e2009-03-05 23:01:23 +00001385 monitor_printf(mon, "No cpu initialized yet");
bellard6a00d602005-11-21 23:25:50 +00001386 return;
1387 }
1388 val = env->kqemu_enabled;
aliguori376253e2009-03-05 23:01:23 +00001389 monitor_printf(mon, "kqemu support: ");
bellard5f1ce942006-02-08 22:40:15 +00001390 switch(val) {
1391 default:
1392 case 0:
aliguori376253e2009-03-05 23:01:23 +00001393 monitor_printf(mon, "disabled\n");
bellard5f1ce942006-02-08 22:40:15 +00001394 break;
1395 case 1:
aliguori376253e2009-03-05 23:01:23 +00001396 monitor_printf(mon, "enabled for user code\n");
bellard5f1ce942006-02-08 22:40:15 +00001397 break;
1398 case 2:
aliguori376253e2009-03-05 23:01:23 +00001399 monitor_printf(mon, "enabled for user and kernel code\n");
bellard5f1ce942006-02-08 22:40:15 +00001400 break;
1401 }
bellard0f4c6412005-07-24 18:10:56 +00001402#else
aliguori376253e2009-03-05 23:01:23 +00001403 monitor_printf(mon, "kqemu support: not compiled\n");
bellard0f4c6412005-07-24 18:10:56 +00001404#endif
ths5fafdf22007-09-16 21:08:06 +00001405}
bellard0f4c6412005-07-24 18:10:56 +00001406
aliguori376253e2009-03-05 23:01:23 +00001407static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001408{
1409#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001410 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001411 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001412 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001413 else
aliguori376253e2009-03-05 23:01:23 +00001414 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001415#else
aliguori376253e2009-03-05 23:01:23 +00001416 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001417#endif
1418}
1419
aliguori030ea372009-04-21 22:30:47 +00001420static void do_info_numa(Monitor *mon)
1421{
aliguorib28b6232009-04-22 20:20:29 +00001422 int i;
aliguori030ea372009-04-21 22:30:47 +00001423 CPUState *env;
1424
1425 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1426 for (i = 0; i < nb_numa_nodes; i++) {
1427 monitor_printf(mon, "node %d cpus:", i);
1428 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1429 if (env->numa_node == i) {
1430 monitor_printf(mon, " %d", env->cpu_index);
1431 }
1432 }
1433 monitor_printf(mon, "\n");
1434 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1435 node_mem[i] >> 20);
1436 }
1437}
1438
bellard5f1ce942006-02-08 22:40:15 +00001439#ifdef CONFIG_PROFILER
1440
1441int64_t kqemu_time;
1442int64_t qemu_time;
1443int64_t kqemu_exec_count;
1444int64_t dev_time;
1445int64_t kqemu_ret_int_count;
1446int64_t kqemu_ret_excp_count;
1447int64_t kqemu_ret_intr_count;
1448
aliguori376253e2009-03-05 23:01:23 +00001449static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001450{
1451 int64_t total;
1452 total = qemu_time;
1453 if (total == 0)
1454 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001455 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1456 dev_time, dev_time / (double)ticks_per_sec);
1457 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1458 qemu_time, qemu_time / (double)ticks_per_sec);
1459 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1460 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1461 PRId64 "\n",
1462 kqemu_time, kqemu_time / (double)ticks_per_sec,
1463 kqemu_time / (double)total * 100.0,
1464 kqemu_exec_count,
1465 kqemu_ret_int_count,
1466 kqemu_ret_excp_count,
1467 kqemu_ret_intr_count);
bellard5f1ce942006-02-08 22:40:15 +00001468 qemu_time = 0;
1469 kqemu_time = 0;
1470 kqemu_exec_count = 0;
1471 dev_time = 0;
1472 kqemu_ret_int_count = 0;
1473 kqemu_ret_excp_count = 0;
1474 kqemu_ret_intr_count = 0;
blueswir1640f42e2009-04-19 10:18:01 +00001475#ifdef CONFIG_KQEMU
bellard5f1ce942006-02-08 22:40:15 +00001476 kqemu_record_dump();
1477#endif
1478}
1479#else
aliguori376253e2009-03-05 23:01:23 +00001480static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001481{
aliguori376253e2009-03-05 23:01:23 +00001482 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001483}
1484#endif
1485
bellardec36b692006-07-16 18:57:03 +00001486/* Capture support */
1487static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1488
aliguori376253e2009-03-05 23:01:23 +00001489static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001490{
1491 int i;
1492 CaptureState *s;
1493
1494 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001495 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001496 s->ops.info (s->opaque);
1497 }
1498}
1499
aliguori376253e2009-03-05 23:01:23 +00001500static void do_stop_capture(Monitor *mon, int n)
bellardec36b692006-07-16 18:57:03 +00001501{
1502 int i;
1503 CaptureState *s;
1504
1505 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1506 if (i == n) {
1507 s->ops.destroy (s->opaque);
1508 LIST_REMOVE (s, entries);
1509 qemu_free (s);
1510 return;
1511 }
1512 }
1513}
1514
1515#ifdef HAS_AUDIO
aliguori376253e2009-03-05 23:01:23 +00001516static void do_wav_capture(Monitor *mon, const char *path,
1517 int has_freq, int freq,
1518 int has_bits, int bits,
1519 int has_channels, int nchannels)
bellardec36b692006-07-16 18:57:03 +00001520{
1521 CaptureState *s;
1522
1523 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001524
1525 freq = has_freq ? freq : 44100;
1526 bits = has_bits ? bits : 16;
1527 nchannels = has_channels ? nchannels : 2;
1528
1529 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001530 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001531 qemu_free (s);
1532 }
1533 LIST_INSERT_HEAD (&capture_head, s, entries);
1534}
1535#endif
1536
aurel32dc1c0b72008-04-27 23:52:12 +00001537#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001538static void do_inject_nmi(Monitor *mon, int cpu_index)
aurel32dc1c0b72008-04-27 23:52:12 +00001539{
1540 CPUState *env;
1541
1542 for (env = first_cpu; env != NULL; env = env->next_cpu)
1543 if (env->cpu_index == cpu_index) {
1544 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1545 break;
1546 }
1547}
1548#endif
1549
aliguori376253e2009-03-05 23:01:23 +00001550static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001551{
aurel321b530a62009-04-05 20:08:59 +00001552 if (vm_running) {
1553 if (singlestep) {
1554 monitor_printf(mon, "VM status: running (single step mode)\n");
1555 } else {
1556 monitor_printf(mon, "VM status: running\n");
1557 }
1558 } else
aliguori376253e2009-03-05 23:01:23 +00001559 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001560}
1561
1562
aliguori376253e2009-03-05 23:01:23 +00001563static void do_balloon(Monitor *mon, int value)
aliguoridf751fa2008-12-04 20:19:35 +00001564{
1565 ram_addr_t target = value;
1566 qemu_balloon(target << 20);
1567}
1568
aliguori376253e2009-03-05 23:01:23 +00001569static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001570{
1571 ram_addr_t actual;
1572
1573 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001574 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001575 monitor_printf(mon, "Using KVM without synchronous MMU, "
1576 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001577 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001578 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001579 else
aliguori376253e2009-03-05 23:01:23 +00001580 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001581}
1582
aliguori76655d62009-03-06 20:27:37 +00001583static void do_acl(Monitor *mon,
1584 const char *command,
aliguori28a76be2009-03-06 20:27:40 +00001585 const char *aclname,
1586 const char *match,
1587 int has_index,
1588 int index)
aliguori76655d62009-03-06 20:27:37 +00001589{
1590 qemu_acl *acl;
1591
1592 acl = qemu_acl_find(aclname);
1593 if (!acl) {
aliguori28a76be2009-03-06 20:27:40 +00001594 monitor_printf(mon, "acl: unknown list '%s'\n", aclname);
1595 return;
aliguori76655d62009-03-06 20:27:37 +00001596 }
1597
1598 if (strcmp(command, "show") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001599 int i = 0;
1600 qemu_acl_entry *entry;
1601 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001602 acl->defaultDeny ? "deny" : "allow");
aliguori28a76be2009-03-06 20:27:40 +00001603 TAILQ_FOREACH(entry, &acl->entries, next) {
1604 i++;
1605 monitor_printf(mon, "%d: %s %s\n", i,
aliguori76655d62009-03-06 20:27:37 +00001606 entry->deny ? "deny" : "allow",
1607 entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001608 }
aliguori76655d62009-03-06 20:27:37 +00001609 } else if (strcmp(command, "reset") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001610 qemu_acl_reset(acl);
1611 monitor_printf(mon, "acl: removed all rules\n");
aliguori76655d62009-03-06 20:27:37 +00001612 } else if (strcmp(command, "policy") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001613 if (!match) {
1614 monitor_printf(mon, "acl: missing policy parameter\n");
1615 return;
1616 }
aliguori76655d62009-03-06 20:27:37 +00001617
aliguori28a76be2009-03-06 20:27:40 +00001618 if (strcmp(match, "allow") == 0) {
1619 acl->defaultDeny = 0;
1620 monitor_printf(mon, "acl: policy set to 'allow'\n");
1621 } else if (strcmp(match, "deny") == 0) {
1622 acl->defaultDeny = 1;
1623 monitor_printf(mon, "acl: policy set to 'deny'\n");
1624 } else {
1625 monitor_printf(mon, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match);
1626 }
aliguori76655d62009-03-06 20:27:37 +00001627 } else if ((strcmp(command, "allow") == 0) ||
aliguori28a76be2009-03-06 20:27:40 +00001628 (strcmp(command, "deny") == 0)) {
1629 int deny = strcmp(command, "deny") == 0 ? 1 : 0;
1630 int ret;
aliguori76655d62009-03-06 20:27:37 +00001631
aliguori28a76be2009-03-06 20:27:40 +00001632 if (!match) {
1633 monitor_printf(mon, "acl: missing match parameter\n");
1634 return;
1635 }
aliguori76655d62009-03-06 20:27:37 +00001636
aliguori28a76be2009-03-06 20:27:40 +00001637 if (has_index)
1638 ret = qemu_acl_insert(acl, deny, match, index);
1639 else
1640 ret = qemu_acl_append(acl, deny, match);
1641 if (ret < 0)
1642 monitor_printf(mon, "acl: unable to add acl entry\n");
1643 else
1644 monitor_printf(mon, "acl: added rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001645 } else if (strcmp(command, "remove") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001646 int ret;
aliguori76655d62009-03-06 20:27:37 +00001647
aliguori28a76be2009-03-06 20:27:40 +00001648 if (!match) {
1649 monitor_printf(mon, "acl: missing match parameter\n");
1650 return;
1651 }
aliguori76655d62009-03-06 20:27:37 +00001652
aliguori28a76be2009-03-06 20:27:40 +00001653 ret = qemu_acl_remove(acl, match);
1654 if (ret < 0)
1655 monitor_printf(mon, "acl: no matching acl entry\n");
1656 else
1657 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001658 } else {
aliguori28a76be2009-03-06 20:27:40 +00001659 monitor_printf(mon, "acl: unknown command '%s'\n", command);
aliguori76655d62009-03-06 20:27:37 +00001660 }
1661}
1662
blueswir1d2c639d2009-01-24 18:19:25 +00001663/* Please update qemu-doc.texi when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001664static const mon_cmd_t mon_cmds[] = {
1665 { "help|?", "s?", help_cmd,
bellard9dc39cb2004-03-14 21:38:27 +00001666 "[cmd]", "show the help" },
ths5fafdf22007-09-16 21:08:06 +00001667 { "commit", "s", do_commit,
bellard7954c732006-08-01 15:52:40 +00001668 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
bellard9307c4c2004-04-04 12:57:25 +00001669 { "info", "s?", do_info,
aliguoricd33fee2009-04-18 15:36:15 +00001670 "[subcommand]", "show various information about the system state" },
bellard9307c4c2004-04-04 12:57:25 +00001671 { "q|quit", "", do_quit,
bellard9dc39cb2004-03-14 21:38:27 +00001672 "", "quit the emulator" },
bellard81d09122004-07-14 17:21:37 +00001673 { "eject", "-fB", do_eject,
thse5987522007-03-30 18:58:01 +00001674 "[-f] device", "eject a removable medium (use -f to force it)" },
aurel322ecea9b2008-06-18 22:10:01 +00001675 { "change", "BFs?", do_change,
1676 "device filename [format]", "change a removable medium, optional format" },
ths5fafdf22007-09-16 21:08:06 +00001677 { "screendump", "F", do_screen_dump,
bellard59a983b2004-03-17 23:17:16 +00001678 "filename", "save screen into PPM image 'filename'" },
balrog788228c2008-05-24 23:15:46 +00001679 { "logfile", "F", do_logfile,
pbrooke735b912007-06-30 13:53:24 +00001680 "filename", "output logs to 'filename'" },
bellard9307c4c2004-04-04 12:57:25 +00001681 { "log", "s", do_log,
ths5fafdf22007-09-16 21:08:06 +00001682 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
bellardfaea38e2006-08-05 21:31:00 +00001683 { "savevm", "s?", do_savevm,
aliguoricd33fee2009-04-18 15:36:15 +00001684 "[tag|id]", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
bellardfaea38e2006-08-05 21:31:00 +00001685 { "loadvm", "s", do_loadvm,
ths5fafdf22007-09-16 21:08:06 +00001686 "tag|id", "restore a VM snapshot from its tag or id" },
bellardfaea38e2006-08-05 21:31:00 +00001687 { "delvm", "s", do_delvm,
ths5fafdf22007-09-16 21:08:06 +00001688 "tag|id", "delete a VM snapshot from its tag or id" },
aurel321b530a62009-04-05 20:08:59 +00001689 { "singlestep", "s?", do_singlestep,
1690 "[on|off]", "run emulation in singlestep mode or switch to normal mode", },
ths5fafdf22007-09-16 21:08:06 +00001691 { "stop", "", do_stop,
bellard9307c4c2004-04-04 12:57:25 +00001692 "", "stop emulation", },
ths5fafdf22007-09-16 21:08:06 +00001693 { "c|cont", "", do_cont,
bellard9307c4c2004-04-04 12:57:25 +00001694 "", "resume emulation", },
bellard67b915a2004-03-31 23:37:16 +00001695#ifdef CONFIG_GDBSTUB
ths5fafdf22007-09-16 21:08:06 +00001696 { "gdbserver", "s?", do_gdbserver,
aliguoricd33fee2009-04-18 15:36:15 +00001697 "[device]", "start gdbserver on given device (default 'tcp::1234'), stop with 'none'", },
bellard67b915a2004-03-31 23:37:16 +00001698#endif
ths5fafdf22007-09-16 21:08:06 +00001699 { "x", "/l", do_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001700 "/fmt addr", "virtual memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001701 { "xp", "/l", do_physical_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001702 "/fmt addr", "physical memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001703 { "p|print", "/l", do_print,
bellard9307c4c2004-04-04 12:57:25 +00001704 "/fmt expr", "print expression value (use $reg for CPU register access)", },
ths5fafdf22007-09-16 21:08:06 +00001705 { "i", "/ii.", do_ioport_read,
bellard34405572004-06-08 00:55:58 +00001706 "/fmt addr", "I/O port read" },
1707
balrogc8256f92008-06-08 22:45:01 +00001708 { "sendkey", "si?", do_sendkey,
1709 "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 +00001710 { "system_reset", "", do_system_reset,
bellarde4f90822004-06-20 12:35:44 +00001711 "", "reset the system" },
ths5fafdf22007-09-16 21:08:06 +00001712 { "system_powerdown", "", do_system_powerdown,
bellard34751872005-07-02 14:31:34 +00001713 "", "send system power down event" },
ths5fafdf22007-09-16 21:08:06 +00001714 { "sum", "ii", do_sum,
bellarde4cf1ad2005-06-04 20:15:57 +00001715 "addr size", "compute the checksum of a memory region" },
bellarda594cfb2005-11-06 16:13:29 +00001716 { "usb_add", "s", do_usb_add,
1717 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1718 { "usb_del", "s", do_usb_del,
1719 "device", "remove USB device 'bus.addr'" },
ths5fafdf22007-09-16 21:08:06 +00001720 { "cpu", "i", do_cpu_set,
bellard6a00d602005-11-21 23:25:50 +00001721 "index", "set the default CPU" },
ths5fafdf22007-09-16 21:08:06 +00001722 { "mouse_move", "sss?", do_mouse_move,
bellard13224a82006-07-14 22:03:35 +00001723 "dx dy [dz]", "send mouse move events" },
ths5fafdf22007-09-16 21:08:06 +00001724 { "mouse_button", "i", do_mouse_button,
bellard13224a82006-07-14 22:03:35 +00001725 "state", "change mouse button state (1=L, 2=M, 4=R)" },
ths455204e2007-01-05 16:42:13 +00001726 { "mouse_set", "i", do_mouse_set,
1727 "index", "set which mouse device receives events" },
bellardec36b692006-07-16 18:57:03 +00001728#ifdef HAS_AUDIO
1729 { "wavcapture", "si?i?i?", do_wav_capture,
aliguoricd33fee2009-04-18 15:36:15 +00001730 "path [frequency [bits [channels]]]",
bellardec36b692006-07-16 18:57:03 +00001731 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1732#endif
blueswir1d2c639d2009-01-24 18:19:25 +00001733 { "stopcapture", "i", do_stop_capture,
1734 "capture index", "stop capture" },
ths5fafdf22007-09-16 21:08:06 +00001735 { "memsave", "lis", do_memory_save,
bellardb371dc52007-01-03 15:20:39 +00001736 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
aurel32a8bdf7a2008-04-11 21:36:14 +00001737 { "pmemsave", "lis", do_physical_memory_save,
1738 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
aurel320ecdffb2008-05-04 20:11:34 +00001739 { "boot_set", "s", do_boot_set,
1740 "bootdevice", "define new values for the boot device list" },
aurel32dc1c0b72008-04-27 23:52:12 +00001741#if defined(TARGET_I386)
1742 { "nmi", "i", do_inject_nmi,
1743 "cpu", "inject an NMI on the given CPU", },
1744#endif
aliguori5bb79102008-10-13 03:12:02 +00001745 { "migrate", "-ds", do_migrate,
1746 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1747 { "migrate_cancel", "", do_migrate_cancel,
1748 "", "cancel the current VM migration" },
1749 { "migrate_set_speed", "s", do_migrate_set_speed,
1750 "value", "set maximum speed (in bytes) for migrations" },
aliguori6f338c32009-02-11 15:21:54 +00001751#if defined(TARGET_I386)
1752 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1753 "[file=file][,if=type][,bus=n]\n"
1754 "[,unit=m][,media=d][index=i]\n"
1755 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1756 "[snapshot=on|off][,cache=on|off]",
1757 "add drive to PCI storage controller" },
1758 { "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" },
1759 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
aliguoria66b11b2009-04-21 19:56:36 +00001760#endif
aliguori5c8be672009-04-21 19:56:32 +00001761 { "host_net_add", "ss?", net_host_device_add,
1762 "tap|user|socket|vde|dump [options]", "add host VLAN client" },
aliguori6f338c32009-02-11 15:21:54 +00001763 { "host_net_remove", "is", net_host_device_remove,
1764 "vlan_id name", "remove host VLAN client" },
aliguorid4ebe192009-04-21 19:56:44 +00001765#ifdef CONFIG_SLIRP
1766 { "host_net_redir", "s", net_slirp_redir,
1767 "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)" },
1768#endif
aliguoridf751fa2008-12-04 20:19:35 +00001769 { "balloon", "i", do_balloon,
1770 "target", "request VM to change it's memory allocation (in MB)" },
aliguorif029bd92009-02-11 15:19:16 +00001771 { "set_link", "ss", do_set_link,
aliguoricd33fee2009-04-18 15:36:15 +00001772 "name up|down", "change the link status of a network adapter" },
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001773 { "watchdog_action", "s", do_watchdog_action,
1774 "[reset|shutdown|poweroff|pause|debug|none]", "change watchdog action" },
aliguoricd33fee2009-04-18 15:36:15 +00001775 { "acl", "sss?i?", do_acl, "<command> <aclname> [<match> [<index>]]\n",
aliguori76655d62009-03-06 20:27:37 +00001776 "acl show vnc.username\n"
1777 "acl policy vnc.username deny\n"
1778 "acl allow vnc.username fred\n"
1779 "acl deny vnc.username bob\n"
1780 "acl reset vnc.username\n" },
ths5fafdf22007-09-16 21:08:06 +00001781 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001782};
1783
blueswir1d2c639d2009-01-24 18:19:25 +00001784/* Please update qemu-doc.texi when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001785static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001786 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001787 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001788 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001789 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001790 { "chardev", "", qemu_chr_info,
1791 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001792 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001793 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001794 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001795 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001796 { "registers", "", do_info_registers,
1797 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001798 { "cpus", "", do_info_cpus,
1799 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001800 { "history", "", do_info_history,
1801 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001802 { "irq", "", irq_info,
1803 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001804 { "pic", "", pic_info,
1805 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001806 { "pci", "", pci_info,
1807 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001808#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001809 { "tlb", "", tlb_info,
1810 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001811#endif
1812#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001813 { "mem", "", mem_info,
1814 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001815 { "hpet", "", do_info_hpet,
1816 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001817#endif
bellarde3db7222005-01-26 22:00:47 +00001818 { "jit", "", do_info_jit,
1819 "", "show dynamic compiler info", },
bellard0f4c6412005-07-24 18:10:56 +00001820 { "kqemu", "", do_info_kqemu,
blueswir1d2c639d2009-01-24 18:19:25 +00001821 "", "show KQEMU information", },
aliguori7ba1e612008-11-05 16:04:33 +00001822 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001823 "", "show KVM information", },
aliguori030ea372009-04-21 22:30:47 +00001824 { "numa", "", do_info_numa,
1825 "", "show NUMA information", },
bellarda594cfb2005-11-06 16:13:29 +00001826 { "usb", "", usb_info,
1827 "", "show guest USB devices", },
1828 { "usbhost", "", usb_host_info,
1829 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001830 { "profile", "", do_info_profile,
1831 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001832 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001833 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001834 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001835 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001836 { "status", "", do_info_status,
1837 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001838 { "pcmcia", "", pcmcia_info,
1839 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001840 { "mice", "", do_info_mice,
1841 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001842 { "vnc", "", do_info_vnc,
1843 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001844 { "name", "", do_info_name,
1845 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001846 { "uuid", "", do_info_uuid,
1847 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001848#if defined(TARGET_PPC)
1849 { "cpustats", "", do_info_cpu_stats,
1850 "", "show CPU statistics", },
1851#endif
blueswir131a60e22007-10-26 18:42:59 +00001852#if defined(CONFIG_SLIRP)
1853 { "slirp", "", do_info_slirp,
1854 "", "show SLIRP statistics", },
1855#endif
aliguori5bb79102008-10-13 03:12:02 +00001856 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001857 { "balloon", "", do_info_balloon,
1858 "", "show balloon information" },
bellard9dc39cb2004-03-14 21:38:27 +00001859 { NULL, NULL, },
1860};
1861
bellard9307c4c2004-04-04 12:57:25 +00001862/*******************************************************************/
1863
1864static const char *pch;
1865static jmp_buf expr_env;
1866
bellard92a31b12005-02-10 22:00:52 +00001867#define MD_TLONG 0
1868#define MD_I32 1
1869
bellard9307c4c2004-04-04 12:57:25 +00001870typedef struct MonitorDef {
1871 const char *name;
1872 int offset;
blueswir18662d652008-10-02 18:32:44 +00001873 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001874 int type;
bellard9307c4c2004-04-04 12:57:25 +00001875} MonitorDef;
1876
bellard57206fd2004-04-25 18:54:52 +00001877#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001878static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001879{
bellard6a00d602005-11-21 23:25:50 +00001880 CPUState *env = mon_get_cpu();
1881 if (!env)
1882 return 0;
1883 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001884}
1885#endif
1886
bellarda541f292004-04-12 20:39:29 +00001887#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001888static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001889{
bellard6a00d602005-11-21 23:25:50 +00001890 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001891 unsigned int u;
1892 int i;
1893
bellard6a00d602005-11-21 23:25:50 +00001894 if (!env)
1895 return 0;
1896
bellarda541f292004-04-12 20:39:29 +00001897 u = 0;
1898 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00001899 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001900
1901 return u;
1902}
1903
blueswir18662d652008-10-02 18:32:44 +00001904static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001905{
bellard6a00d602005-11-21 23:25:50 +00001906 CPUState *env = mon_get_cpu();
1907 if (!env)
1908 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001909 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001910}
1911
blueswir18662d652008-10-02 18:32:44 +00001912static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001913{
bellard6a00d602005-11-21 23:25:50 +00001914 CPUState *env = mon_get_cpu();
1915 if (!env)
1916 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001917 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001918}
bellard9fddaa02004-05-21 12:59:32 +00001919
blueswir18662d652008-10-02 18:32:44 +00001920static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001921{
bellard6a00d602005-11-21 23:25:50 +00001922 CPUState *env = mon_get_cpu();
1923 if (!env)
1924 return 0;
1925 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001926}
1927
blueswir18662d652008-10-02 18:32:44 +00001928static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001929{
bellard6a00d602005-11-21 23:25:50 +00001930 CPUState *env = mon_get_cpu();
1931 if (!env)
1932 return 0;
1933 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001934}
1935
blueswir18662d652008-10-02 18:32:44 +00001936static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001937{
bellard6a00d602005-11-21 23:25:50 +00001938 CPUState *env = mon_get_cpu();
1939 if (!env)
1940 return 0;
1941 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001942}
bellarda541f292004-04-12 20:39:29 +00001943#endif
1944
bellarde95c8d52004-09-30 22:22:08 +00001945#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001946#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001947static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001948{
bellard6a00d602005-11-21 23:25:50 +00001949 CPUState *env = mon_get_cpu();
1950 if (!env)
1951 return 0;
1952 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001953}
bellard7b936c02005-10-30 17:05:13 +00001954#endif
bellarde95c8d52004-09-30 22:22:08 +00001955
blueswir18662d652008-10-02 18:32:44 +00001956static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001957{
bellard6a00d602005-11-21 23:25:50 +00001958 CPUState *env = mon_get_cpu();
1959 if (!env)
1960 return 0;
1961 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001962}
1963#endif
1964
blueswir18662d652008-10-02 18:32:44 +00001965static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001966#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001967
1968#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001969 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001970 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001971 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001972
bellard9307c4c2004-04-04 12:57:25 +00001973 { "eax", offsetof(CPUState, regs[0]) },
1974 { "ecx", offsetof(CPUState, regs[1]) },
1975 { "edx", offsetof(CPUState, regs[2]) },
1976 { "ebx", offsetof(CPUState, regs[3]) },
1977 { "esp|sp", offsetof(CPUState, regs[4]) },
1978 { "ebp|fp", offsetof(CPUState, regs[5]) },
1979 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001980 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001981#ifdef TARGET_X86_64
1982 { "r8", offsetof(CPUState, regs[8]) },
1983 { "r9", offsetof(CPUState, regs[9]) },
1984 { "r10", offsetof(CPUState, regs[10]) },
1985 { "r11", offsetof(CPUState, regs[11]) },
1986 { "r12", offsetof(CPUState, regs[12]) },
1987 { "r13", offsetof(CPUState, regs[13]) },
1988 { "r14", offsetof(CPUState, regs[14]) },
1989 { "r15", offsetof(CPUState, regs[15]) },
1990#endif
bellard9307c4c2004-04-04 12:57:25 +00001991 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001992 { "eip", offsetof(CPUState, eip) },
1993 SEG("cs", R_CS)
1994 SEG("ds", R_DS)
1995 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001996 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001997 SEG("fs", R_FS)
1998 SEG("gs", R_GS)
1999 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002000#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002001 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002002 { "r0", offsetof(CPUState, gpr[0]) },
2003 { "r1", offsetof(CPUState, gpr[1]) },
2004 { "r2", offsetof(CPUState, gpr[2]) },
2005 { "r3", offsetof(CPUState, gpr[3]) },
2006 { "r4", offsetof(CPUState, gpr[4]) },
2007 { "r5", offsetof(CPUState, gpr[5]) },
2008 { "r6", offsetof(CPUState, gpr[6]) },
2009 { "r7", offsetof(CPUState, gpr[7]) },
2010 { "r8", offsetof(CPUState, gpr[8]) },
2011 { "r9", offsetof(CPUState, gpr[9]) },
2012 { "r10", offsetof(CPUState, gpr[10]) },
2013 { "r11", offsetof(CPUState, gpr[11]) },
2014 { "r12", offsetof(CPUState, gpr[12]) },
2015 { "r13", offsetof(CPUState, gpr[13]) },
2016 { "r14", offsetof(CPUState, gpr[14]) },
2017 { "r15", offsetof(CPUState, gpr[15]) },
2018 { "r16", offsetof(CPUState, gpr[16]) },
2019 { "r17", offsetof(CPUState, gpr[17]) },
2020 { "r18", offsetof(CPUState, gpr[18]) },
2021 { "r19", offsetof(CPUState, gpr[19]) },
2022 { "r20", offsetof(CPUState, gpr[20]) },
2023 { "r21", offsetof(CPUState, gpr[21]) },
2024 { "r22", offsetof(CPUState, gpr[22]) },
2025 { "r23", offsetof(CPUState, gpr[23]) },
2026 { "r24", offsetof(CPUState, gpr[24]) },
2027 { "r25", offsetof(CPUState, gpr[25]) },
2028 { "r26", offsetof(CPUState, gpr[26]) },
2029 { "r27", offsetof(CPUState, gpr[27]) },
2030 { "r28", offsetof(CPUState, gpr[28]) },
2031 { "r29", offsetof(CPUState, gpr[29]) },
2032 { "r30", offsetof(CPUState, gpr[30]) },
2033 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002034 /* Floating point registers */
2035 { "f0", offsetof(CPUState, fpr[0]) },
2036 { "f1", offsetof(CPUState, fpr[1]) },
2037 { "f2", offsetof(CPUState, fpr[2]) },
2038 { "f3", offsetof(CPUState, fpr[3]) },
2039 { "f4", offsetof(CPUState, fpr[4]) },
2040 { "f5", offsetof(CPUState, fpr[5]) },
2041 { "f6", offsetof(CPUState, fpr[6]) },
2042 { "f7", offsetof(CPUState, fpr[7]) },
2043 { "f8", offsetof(CPUState, fpr[8]) },
2044 { "f9", offsetof(CPUState, fpr[9]) },
2045 { "f10", offsetof(CPUState, fpr[10]) },
2046 { "f11", offsetof(CPUState, fpr[11]) },
2047 { "f12", offsetof(CPUState, fpr[12]) },
2048 { "f13", offsetof(CPUState, fpr[13]) },
2049 { "f14", offsetof(CPUState, fpr[14]) },
2050 { "f15", offsetof(CPUState, fpr[15]) },
2051 { "f16", offsetof(CPUState, fpr[16]) },
2052 { "f17", offsetof(CPUState, fpr[17]) },
2053 { "f18", offsetof(CPUState, fpr[18]) },
2054 { "f19", offsetof(CPUState, fpr[19]) },
2055 { "f20", offsetof(CPUState, fpr[20]) },
2056 { "f21", offsetof(CPUState, fpr[21]) },
2057 { "f22", offsetof(CPUState, fpr[22]) },
2058 { "f23", offsetof(CPUState, fpr[23]) },
2059 { "f24", offsetof(CPUState, fpr[24]) },
2060 { "f25", offsetof(CPUState, fpr[25]) },
2061 { "f26", offsetof(CPUState, fpr[26]) },
2062 { "f27", offsetof(CPUState, fpr[27]) },
2063 { "f28", offsetof(CPUState, fpr[28]) },
2064 { "f29", offsetof(CPUState, fpr[29]) },
2065 { "f30", offsetof(CPUState, fpr[30]) },
2066 { "f31", offsetof(CPUState, fpr[31]) },
2067 { "fpscr", offsetof(CPUState, fpscr) },
2068 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002069 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002070 { "lr", offsetof(CPUState, lr) },
2071 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002072 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002073 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002074 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002075 { "msr", 0, &monitor_get_msr, },
2076 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002077 { "tbu", 0, &monitor_get_tbu, },
2078 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002079#if defined(TARGET_PPC64)
2080 /* Address space register */
2081 { "asr", offsetof(CPUState, asr) },
2082#endif
2083 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002084 { "sdr1", offsetof(CPUState, sdr1) },
2085 { "sr0", offsetof(CPUState, sr[0]) },
2086 { "sr1", offsetof(CPUState, sr[1]) },
2087 { "sr2", offsetof(CPUState, sr[2]) },
2088 { "sr3", offsetof(CPUState, sr[3]) },
2089 { "sr4", offsetof(CPUState, sr[4]) },
2090 { "sr5", offsetof(CPUState, sr[5]) },
2091 { "sr6", offsetof(CPUState, sr[6]) },
2092 { "sr7", offsetof(CPUState, sr[7]) },
2093 { "sr8", offsetof(CPUState, sr[8]) },
2094 { "sr9", offsetof(CPUState, sr[9]) },
2095 { "sr10", offsetof(CPUState, sr[10]) },
2096 { "sr11", offsetof(CPUState, sr[11]) },
2097 { "sr12", offsetof(CPUState, sr[12]) },
2098 { "sr13", offsetof(CPUState, sr[13]) },
2099 { "sr14", offsetof(CPUState, sr[14]) },
2100 { "sr15", offsetof(CPUState, sr[15]) },
2101 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002102#elif defined(TARGET_SPARC)
2103 { "g0", offsetof(CPUState, gregs[0]) },
2104 { "g1", offsetof(CPUState, gregs[1]) },
2105 { "g2", offsetof(CPUState, gregs[2]) },
2106 { "g3", offsetof(CPUState, gregs[3]) },
2107 { "g4", offsetof(CPUState, gregs[4]) },
2108 { "g5", offsetof(CPUState, gregs[5]) },
2109 { "g6", offsetof(CPUState, gregs[6]) },
2110 { "g7", offsetof(CPUState, gregs[7]) },
2111 { "o0", 0, monitor_get_reg },
2112 { "o1", 1, monitor_get_reg },
2113 { "o2", 2, monitor_get_reg },
2114 { "o3", 3, monitor_get_reg },
2115 { "o4", 4, monitor_get_reg },
2116 { "o5", 5, monitor_get_reg },
2117 { "o6", 6, monitor_get_reg },
2118 { "o7", 7, monitor_get_reg },
2119 { "l0", 8, monitor_get_reg },
2120 { "l1", 9, monitor_get_reg },
2121 { "l2", 10, monitor_get_reg },
2122 { "l3", 11, monitor_get_reg },
2123 { "l4", 12, monitor_get_reg },
2124 { "l5", 13, monitor_get_reg },
2125 { "l6", 14, monitor_get_reg },
2126 { "l7", 15, monitor_get_reg },
2127 { "i0", 16, monitor_get_reg },
2128 { "i1", 17, monitor_get_reg },
2129 { "i2", 18, monitor_get_reg },
2130 { "i3", 19, monitor_get_reg },
2131 { "i4", 20, monitor_get_reg },
2132 { "i5", 21, monitor_get_reg },
2133 { "i6", 22, monitor_get_reg },
2134 { "i7", 23, monitor_get_reg },
2135 { "pc", offsetof(CPUState, pc) },
2136 { "npc", offsetof(CPUState, npc) },
2137 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002138#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002139 { "psr", 0, &monitor_get_psr, },
2140 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002141#endif
bellarde95c8d52004-09-30 22:22:08 +00002142 { "tbr", offsetof(CPUState, tbr) },
2143 { "fsr", offsetof(CPUState, fsr) },
2144 { "f0", offsetof(CPUState, fpr[0]) },
2145 { "f1", offsetof(CPUState, fpr[1]) },
2146 { "f2", offsetof(CPUState, fpr[2]) },
2147 { "f3", offsetof(CPUState, fpr[3]) },
2148 { "f4", offsetof(CPUState, fpr[4]) },
2149 { "f5", offsetof(CPUState, fpr[5]) },
2150 { "f6", offsetof(CPUState, fpr[6]) },
2151 { "f7", offsetof(CPUState, fpr[7]) },
2152 { "f8", offsetof(CPUState, fpr[8]) },
2153 { "f9", offsetof(CPUState, fpr[9]) },
2154 { "f10", offsetof(CPUState, fpr[10]) },
2155 { "f11", offsetof(CPUState, fpr[11]) },
2156 { "f12", offsetof(CPUState, fpr[12]) },
2157 { "f13", offsetof(CPUState, fpr[13]) },
2158 { "f14", offsetof(CPUState, fpr[14]) },
2159 { "f15", offsetof(CPUState, fpr[15]) },
2160 { "f16", offsetof(CPUState, fpr[16]) },
2161 { "f17", offsetof(CPUState, fpr[17]) },
2162 { "f18", offsetof(CPUState, fpr[18]) },
2163 { "f19", offsetof(CPUState, fpr[19]) },
2164 { "f20", offsetof(CPUState, fpr[20]) },
2165 { "f21", offsetof(CPUState, fpr[21]) },
2166 { "f22", offsetof(CPUState, fpr[22]) },
2167 { "f23", offsetof(CPUState, fpr[23]) },
2168 { "f24", offsetof(CPUState, fpr[24]) },
2169 { "f25", offsetof(CPUState, fpr[25]) },
2170 { "f26", offsetof(CPUState, fpr[26]) },
2171 { "f27", offsetof(CPUState, fpr[27]) },
2172 { "f28", offsetof(CPUState, fpr[28]) },
2173 { "f29", offsetof(CPUState, fpr[29]) },
2174 { "f30", offsetof(CPUState, fpr[30]) },
2175 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002176#ifdef TARGET_SPARC64
2177 { "f32", offsetof(CPUState, fpr[32]) },
2178 { "f34", offsetof(CPUState, fpr[34]) },
2179 { "f36", offsetof(CPUState, fpr[36]) },
2180 { "f38", offsetof(CPUState, fpr[38]) },
2181 { "f40", offsetof(CPUState, fpr[40]) },
2182 { "f42", offsetof(CPUState, fpr[42]) },
2183 { "f44", offsetof(CPUState, fpr[44]) },
2184 { "f46", offsetof(CPUState, fpr[46]) },
2185 { "f48", offsetof(CPUState, fpr[48]) },
2186 { "f50", offsetof(CPUState, fpr[50]) },
2187 { "f52", offsetof(CPUState, fpr[52]) },
2188 { "f54", offsetof(CPUState, fpr[54]) },
2189 { "f56", offsetof(CPUState, fpr[56]) },
2190 { "f58", offsetof(CPUState, fpr[58]) },
2191 { "f60", offsetof(CPUState, fpr[60]) },
2192 { "f62", offsetof(CPUState, fpr[62]) },
2193 { "asi", offsetof(CPUState, asi) },
2194 { "pstate", offsetof(CPUState, pstate) },
2195 { "cansave", offsetof(CPUState, cansave) },
2196 { "canrestore", offsetof(CPUState, canrestore) },
2197 { "otherwin", offsetof(CPUState, otherwin) },
2198 { "wstate", offsetof(CPUState, wstate) },
2199 { "cleanwin", offsetof(CPUState, cleanwin) },
2200 { "fprs", offsetof(CPUState, fprs) },
2201#endif
bellard9307c4c2004-04-04 12:57:25 +00002202#endif
2203 { NULL },
2204};
2205
aliguori376253e2009-03-05 23:01:23 +00002206static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002207{
aliguori376253e2009-03-05 23:01:23 +00002208 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002209 longjmp(expr_env, 1);
2210}
2211
bellard6a00d602005-11-21 23:25:50 +00002212/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002213static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002214{
blueswir18662d652008-10-02 18:32:44 +00002215 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002216 void *ptr;
2217
bellard9307c4c2004-04-04 12:57:25 +00002218 for(md = monitor_defs; md->name != NULL; md++) {
2219 if (compare_cmd(name, md->name)) {
2220 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002221 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002222 } else {
bellard6a00d602005-11-21 23:25:50 +00002223 CPUState *env = mon_get_cpu();
2224 if (!env)
2225 return -2;
2226 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002227 switch(md->type) {
2228 case MD_I32:
2229 *pval = *(int32_t *)ptr;
2230 break;
2231 case MD_TLONG:
2232 *pval = *(target_long *)ptr;
2233 break;
2234 default:
2235 *pval = 0;
2236 break;
2237 }
bellard9307c4c2004-04-04 12:57:25 +00002238 }
2239 return 0;
2240 }
2241 }
2242 return -1;
2243}
2244
2245static void next(void)
2246{
2247 if (pch != '\0') {
2248 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002249 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002250 pch++;
2251 }
2252}
2253
aliguori376253e2009-03-05 23:01:23 +00002254static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002255
aliguori376253e2009-03-05 23:01:23 +00002256static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002257{
blueswir1c2efc952007-09-25 17:28:42 +00002258 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002259 char *p;
bellard6a00d602005-11-21 23:25:50 +00002260 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002261
2262 switch(*pch) {
2263 case '+':
2264 next();
aliguori376253e2009-03-05 23:01:23 +00002265 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002266 break;
2267 case '-':
2268 next();
aliguori376253e2009-03-05 23:01:23 +00002269 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002270 break;
2271 case '~':
2272 next();
aliguori376253e2009-03-05 23:01:23 +00002273 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002274 break;
2275 case '(':
2276 next();
aliguori376253e2009-03-05 23:01:23 +00002277 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002278 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002279 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002280 }
2281 next();
2282 break;
bellard81d09122004-07-14 17:21:37 +00002283 case '\'':
2284 pch++;
2285 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002286 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002287 n = *pch;
2288 pch++;
2289 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002290 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002291 next();
2292 break;
bellard9307c4c2004-04-04 12:57:25 +00002293 case '$':
2294 {
2295 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002296 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002297
bellard9307c4c2004-04-04 12:57:25 +00002298 pch++;
2299 q = buf;
2300 while ((*pch >= 'a' && *pch <= 'z') ||
2301 (*pch >= 'A' && *pch <= 'Z') ||
2302 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002303 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002304 if ((q - buf) < sizeof(buf) - 1)
2305 *q++ = *pch;
2306 pch++;
2307 }
blueswir1cd390082008-11-16 13:53:32 +00002308 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002309 pch++;
2310 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002311 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002312 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002313 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002314 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002315 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002316 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002317 }
2318 break;
2319 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002320 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002321 n = 0;
2322 break;
2323 default:
blueswir17743e582007-09-24 18:39:04 +00002324#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002325 n = strtoull(pch, &p, 0);
2326#else
bellard9307c4c2004-04-04 12:57:25 +00002327 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002328#endif
bellard9307c4c2004-04-04 12:57:25 +00002329 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002330 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002331 }
2332 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002333 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002334 pch++;
2335 break;
2336 }
2337 return n;
2338}
2339
2340
aliguori376253e2009-03-05 23:01:23 +00002341static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002342{
blueswir1c2efc952007-09-25 17:28:42 +00002343 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002344 int op;
ths3b46e622007-09-17 08:09:54 +00002345
aliguori376253e2009-03-05 23:01:23 +00002346 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002347 for(;;) {
2348 op = *pch;
2349 if (op != '*' && op != '/' && op != '%')
2350 break;
2351 next();
aliguori376253e2009-03-05 23:01:23 +00002352 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002353 switch(op) {
2354 default:
2355 case '*':
2356 val *= val2;
2357 break;
2358 case '/':
2359 case '%':
ths5fafdf22007-09-16 21:08:06 +00002360 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002361 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002362 if (op == '/')
2363 val /= val2;
2364 else
2365 val %= val2;
2366 break;
2367 }
2368 }
2369 return val;
2370}
2371
aliguori376253e2009-03-05 23:01:23 +00002372static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002373{
blueswir1c2efc952007-09-25 17:28:42 +00002374 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002375 int op;
bellard9307c4c2004-04-04 12:57:25 +00002376
aliguori376253e2009-03-05 23:01:23 +00002377 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002378 for(;;) {
2379 op = *pch;
2380 if (op != '&' && op != '|' && op != '^')
2381 break;
2382 next();
aliguori376253e2009-03-05 23:01:23 +00002383 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002384 switch(op) {
2385 default:
2386 case '&':
2387 val &= val2;
2388 break;
2389 case '|':
2390 val |= val2;
2391 break;
2392 case '^':
2393 val ^= val2;
2394 break;
2395 }
2396 }
2397 return val;
2398}
2399
aliguori376253e2009-03-05 23:01:23 +00002400static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002401{
blueswir1c2efc952007-09-25 17:28:42 +00002402 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002403 int op;
bellard9307c4c2004-04-04 12:57:25 +00002404
aliguori376253e2009-03-05 23:01:23 +00002405 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002406 for(;;) {
2407 op = *pch;
2408 if (op != '+' && op != '-')
2409 break;
2410 next();
aliguori376253e2009-03-05 23:01:23 +00002411 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002412 if (op == '+')
2413 val += val2;
2414 else
2415 val -= val2;
2416 }
2417 return val;
2418}
2419
aliguori376253e2009-03-05 23:01:23 +00002420static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002421{
2422 pch = *pp;
2423 if (setjmp(expr_env)) {
2424 *pp = pch;
2425 return -1;
2426 }
blueswir1cd390082008-11-16 13:53:32 +00002427 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002428 pch++;
aliguori376253e2009-03-05 23:01:23 +00002429 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002430 *pp = pch;
2431 return 0;
2432}
2433
2434static int get_str(char *buf, int buf_size, const char **pp)
2435{
2436 const char *p;
2437 char *q;
2438 int c;
2439
bellard81d09122004-07-14 17:21:37 +00002440 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002441 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002442 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002443 p++;
2444 if (*p == '\0') {
2445 fail:
bellard81d09122004-07-14 17:21:37 +00002446 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002447 *pp = p;
2448 return -1;
2449 }
bellard9307c4c2004-04-04 12:57:25 +00002450 if (*p == '\"') {
2451 p++;
2452 while (*p != '\0' && *p != '\"') {
2453 if (*p == '\\') {
2454 p++;
2455 c = *p++;
2456 switch(c) {
2457 case 'n':
2458 c = '\n';
2459 break;
2460 case 'r':
2461 c = '\r';
2462 break;
2463 case '\\':
2464 case '\'':
2465 case '\"':
2466 break;
2467 default:
2468 qemu_printf("unsupported escape code: '\\%c'\n", c);
2469 goto fail;
2470 }
2471 if ((q - buf) < buf_size - 1) {
2472 *q++ = c;
2473 }
2474 } else {
2475 if ((q - buf) < buf_size - 1) {
2476 *q++ = *p;
2477 }
2478 p++;
2479 }
2480 }
2481 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002482 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002483 goto fail;
2484 }
2485 p++;
2486 } else {
blueswir1cd390082008-11-16 13:53:32 +00002487 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002488 if ((q - buf) < buf_size - 1) {
2489 *q++ = *p;
2490 }
2491 p++;
2492 }
bellard9307c4c2004-04-04 12:57:25 +00002493 }
bellard81d09122004-07-14 17:21:37 +00002494 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002495 *pp = p;
2496 return 0;
2497}
2498
2499static int default_fmt_format = 'x';
2500static int default_fmt_size = 4;
2501
2502#define MAX_ARGS 16
2503
aliguori376253e2009-03-05 23:01:23 +00002504static void monitor_handle_command(Monitor *mon, const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002505{
2506 const char *p, *pstart, *typestr;
2507 char *q;
2508 int c, nb_args, len, i, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002509 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002510 char cmdname[256];
2511 char buf[1024];
2512 void *str_allocated[MAX_ARGS];
2513 void *args[MAX_ARGS];
aliguori376253e2009-03-05 23:01:23 +00002514 void (*handler_0)(Monitor *mon);
2515 void (*handler_1)(Monitor *mon, void *arg0);
2516 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2517 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2518 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2519 void *arg3);
2520 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2521 void *arg3, void *arg4);
2522 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2523 void *arg3, void *arg4, void *arg5);
2524 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2525 void *arg3, void *arg4, void *arg5, void *arg6);
bellard9dc39cb2004-03-14 21:38:27 +00002526
2527#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002528 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002529#endif
ths3b46e622007-09-17 08:09:54 +00002530
bellard9307c4c2004-04-04 12:57:25 +00002531 /* extract the command name */
bellard9dc39cb2004-03-14 21:38:27 +00002532 p = cmdline;
bellard9307c4c2004-04-04 12:57:25 +00002533 q = cmdname;
blueswir1cd390082008-11-16 13:53:32 +00002534 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002535 p++;
2536 if (*p == '\0')
bellard9dc39cb2004-03-14 21:38:27 +00002537 return;
bellard9307c4c2004-04-04 12:57:25 +00002538 pstart = p;
blueswir1cd390082008-11-16 13:53:32 +00002539 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002540 p++;
2541 len = p - pstart;
2542 if (len > sizeof(cmdname) - 1)
2543 len = sizeof(cmdname) - 1;
2544 memcpy(cmdname, pstart, len);
2545 cmdname[len] = '\0';
ths3b46e622007-09-17 08:09:54 +00002546
bellard9307c4c2004-04-04 12:57:25 +00002547 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002548 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002549 if (compare_cmd(cmdname, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +00002550 goto found;
2551 }
aliguori376253e2009-03-05 23:01:23 +00002552 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
bellard9dc39cb2004-03-14 21:38:27 +00002553 return;
2554 found:
bellard9307c4c2004-04-04 12:57:25 +00002555
2556 for(i = 0; i < MAX_ARGS; i++)
2557 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002558
bellard9307c4c2004-04-04 12:57:25 +00002559 /* parse the parameters */
2560 typestr = cmd->args_type;
2561 nb_args = 0;
2562 for(;;) {
2563 c = *typestr;
2564 if (c == '\0')
2565 break;
2566 typestr++;
2567 switch(c) {
2568 case 'F':
bellard81d09122004-07-14 17:21:37 +00002569 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002570 case 's':
2571 {
2572 int ret;
2573 char *str;
ths3b46e622007-09-17 08:09:54 +00002574
blueswir1cd390082008-11-16 13:53:32 +00002575 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002576 p++;
2577 if (*typestr == '?') {
2578 typestr++;
2579 if (*p == '\0') {
2580 /* no optional string: NULL argument */
2581 str = NULL;
2582 goto add_str;
2583 }
2584 }
2585 ret = get_str(buf, sizeof(buf), &p);
2586 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002587 switch(c) {
2588 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002589 monitor_printf(mon, "%s: filename expected\n",
2590 cmdname);
bellard81d09122004-07-14 17:21:37 +00002591 break;
2592 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002593 monitor_printf(mon, "%s: block device name expected\n",
2594 cmdname);
bellard81d09122004-07-14 17:21:37 +00002595 break;
2596 default:
aliguori376253e2009-03-05 23:01:23 +00002597 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002598 break;
2599 }
bellard9307c4c2004-04-04 12:57:25 +00002600 goto fail;
2601 }
2602 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002603 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002604 str_allocated[nb_args] = str;
2605 add_str:
2606 if (nb_args >= MAX_ARGS) {
2607 error_args:
aliguori376253e2009-03-05 23:01:23 +00002608 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002609 goto fail;
2610 }
2611 args[nb_args++] = str;
2612 }
2613 break;
2614 case '/':
2615 {
2616 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002617
blueswir1cd390082008-11-16 13:53:32 +00002618 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002619 p++;
2620 if (*p == '/') {
2621 /* format found */
2622 p++;
2623 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002624 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002625 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002626 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002627 count = count * 10 + (*p - '0');
2628 p++;
2629 }
2630 }
2631 size = -1;
2632 format = -1;
2633 for(;;) {
2634 switch(*p) {
2635 case 'o':
2636 case 'd':
2637 case 'u':
2638 case 'x':
2639 case 'i':
2640 case 'c':
2641 format = *p++;
2642 break;
2643 case 'b':
2644 size = 1;
2645 p++;
2646 break;
2647 case 'h':
2648 size = 2;
2649 p++;
2650 break;
2651 case 'w':
2652 size = 4;
2653 p++;
2654 break;
2655 case 'g':
2656 case 'L':
2657 size = 8;
2658 p++;
2659 break;
2660 default:
2661 goto next;
2662 }
2663 }
2664 next:
blueswir1cd390082008-11-16 13:53:32 +00002665 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002666 monitor_printf(mon, "invalid char in format: '%c'\n",
2667 *p);
bellard9307c4c2004-04-04 12:57:25 +00002668 goto fail;
2669 }
bellard9307c4c2004-04-04 12:57:25 +00002670 if (format < 0)
2671 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002672 if (format != 'i') {
2673 /* for 'i', not specifying a size gives -1 as size */
2674 if (size < 0)
2675 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002676 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002677 }
bellard9307c4c2004-04-04 12:57:25 +00002678 default_fmt_format = format;
2679 } else {
2680 count = 1;
2681 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002682 if (format != 'i') {
2683 size = default_fmt_size;
2684 } else {
2685 size = -1;
2686 }
bellard9307c4c2004-04-04 12:57:25 +00002687 }
2688 if (nb_args + 3 > MAX_ARGS)
2689 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002690 args[nb_args++] = (void*)(long)count;
2691 args[nb_args++] = (void*)(long)format;
2692 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002693 }
2694 break;
2695 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002696 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002697 {
blueswir1c2efc952007-09-25 17:28:42 +00002698 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002699
blueswir1cd390082008-11-16 13:53:32 +00002700 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002701 p++;
bellard34405572004-06-08 00:55:58 +00002702 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002703 if (*typestr == '?') {
2704 if (*p == '\0')
2705 has_arg = 0;
2706 else
2707 has_arg = 1;
2708 } else {
2709 if (*p == '.') {
2710 p++;
blueswir1cd390082008-11-16 13:53:32 +00002711 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002712 p++;
2713 has_arg = 1;
2714 } else {
2715 has_arg = 0;
2716 }
2717 }
bellard13224a82006-07-14 22:03:35 +00002718 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002719 if (nb_args >= MAX_ARGS)
2720 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002721 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002722 if (!has_arg) {
2723 if (nb_args >= MAX_ARGS)
2724 goto error_args;
2725 val = -1;
2726 goto add_num;
2727 }
2728 }
aliguori376253e2009-03-05 23:01:23 +00002729 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002730 goto fail;
2731 add_num:
bellard92a31b12005-02-10 22:00:52 +00002732 if (c == 'i') {
2733 if (nb_args >= MAX_ARGS)
2734 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002735 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002736 } else {
2737 if ((nb_args + 1) >= MAX_ARGS)
2738 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002739#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002740 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002741#else
2742 args[nb_args++] = (void *)0;
2743#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002744 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002745 }
bellard9307c4c2004-04-04 12:57:25 +00002746 }
2747 break;
2748 case '-':
2749 {
2750 int has_option;
2751 /* option */
ths3b46e622007-09-17 08:09:54 +00002752
bellard9307c4c2004-04-04 12:57:25 +00002753 c = *typestr++;
2754 if (c == '\0')
2755 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002756 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002757 p++;
2758 has_option = 0;
2759 if (*p == '-') {
2760 p++;
2761 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002762 monitor_printf(mon, "%s: unsupported option -%c\n",
2763 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002764 goto fail;
2765 }
2766 p++;
2767 has_option = 1;
2768 }
2769 if (nb_args >= MAX_ARGS)
2770 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002771 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002772 }
2773 break;
2774 default:
2775 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002776 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002777 goto fail;
2778 }
2779 }
2780 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002781 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002782 p++;
2783 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002784 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2785 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002786 goto fail;
2787 }
2788
2789 switch(nb_args) {
2790 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002791 handler_0 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002792 handler_0(mon);
bellard9307c4c2004-04-04 12:57:25 +00002793 break;
2794 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002795 handler_1 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002796 handler_1(mon, args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002797 break;
2798 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002799 handler_2 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002800 handler_2(mon, args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002801 break;
2802 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002803 handler_3 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002804 handler_3(mon, args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002805 break;
2806 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002807 handler_4 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002808 handler_4(mon, args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002809 break;
2810 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002811 handler_5 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002812 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002813 break;
bellard34405572004-06-08 00:55:58 +00002814 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002815 handler_6 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002816 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002817 break;
bellardec36b692006-07-16 18:57:03 +00002818 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002819 handler_7 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002820 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2821 args[6]);
bellardec36b692006-07-16 18:57:03 +00002822 break;
bellard9307c4c2004-04-04 12:57:25 +00002823 default:
aliguori376253e2009-03-05 23:01:23 +00002824 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
bellard9307c4c2004-04-04 12:57:25 +00002825 goto fail;
2826 }
2827 fail:
2828 for(i = 0; i < MAX_ARGS; i++)
2829 qemu_free(str_allocated[i]);
2830 return;
bellard9dc39cb2004-03-14 21:38:27 +00002831}
2832
bellard81d09122004-07-14 17:21:37 +00002833static void cmd_completion(const char *name, const char *list)
2834{
2835 const char *p, *pstart;
2836 char cmd[128];
2837 int len;
2838
2839 p = list;
2840 for(;;) {
2841 pstart = p;
2842 p = strchr(p, '|');
2843 if (!p)
2844 p = pstart + strlen(pstart);
2845 len = p - pstart;
2846 if (len > sizeof(cmd) - 2)
2847 len = sizeof(cmd) - 2;
2848 memcpy(cmd, pstart, len);
2849 cmd[len] = '\0';
2850 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002851 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002852 }
2853 if (*p == '\0')
2854 break;
2855 p++;
2856 }
2857}
2858
2859static void file_completion(const char *input)
2860{
2861 DIR *ffs;
2862 struct dirent *d;
2863 char path[1024];
2864 char file[1024], file_prefix[1024];
2865 int input_path_len;
2866 const char *p;
2867
ths5fafdf22007-09-16 21:08:06 +00002868 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002869 if (!p) {
2870 input_path_len = 0;
2871 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002872 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002873 } else {
2874 input_path_len = p - input + 1;
2875 memcpy(path, input, input_path_len);
2876 if (input_path_len > sizeof(path) - 1)
2877 input_path_len = sizeof(path) - 1;
2878 path[input_path_len] = '\0';
2879 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2880 }
2881#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002882 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2883 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002884#endif
2885 ffs = opendir(path);
2886 if (!ffs)
2887 return;
2888 for(;;) {
2889 struct stat sb;
2890 d = readdir(ffs);
2891 if (!d)
2892 break;
2893 if (strstart(d->d_name, file_prefix, NULL)) {
2894 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002895 if (input_path_len < sizeof(file))
2896 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2897 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002898 /* stat the file to find out if it's a directory.
2899 * In that case add a slash to speed up typing long paths
2900 */
2901 stat(file, &sb);
2902 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002903 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00002904 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002905 }
2906 }
2907 closedir(ffs);
2908}
2909
aliguori51de9762009-03-05 23:00:43 +00002910static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002911{
aliguori51de9762009-03-05 23:00:43 +00002912 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002913 const char *input = opaque;
2914
2915 if (input[0] == '\0' ||
2916 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00002917 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00002918 }
2919}
2920
2921/* NOTE: this parser is an approximate form of the real command parser */
2922static void parse_cmdline(const char *cmdline,
2923 int *pnb_args, char **args)
2924{
2925 const char *p;
2926 int nb_args, ret;
2927 char buf[1024];
2928
2929 p = cmdline;
2930 nb_args = 0;
2931 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002932 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002933 p++;
2934 if (*p == '\0')
2935 break;
2936 if (nb_args >= MAX_ARGS)
2937 break;
2938 ret = get_str(buf, sizeof(buf), &p);
2939 args[nb_args] = qemu_strdup(buf);
2940 nb_args++;
2941 if (ret < 0)
2942 break;
2943 }
2944 *pnb_args = nb_args;
2945}
2946
aliguori4c36ba32009-03-05 23:01:37 +00002947static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002948{
2949 const char *cmdname;
2950 char *args[MAX_ARGS];
2951 int nb_args, i, len;
2952 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00002953 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002954 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002955
2956 parse_cmdline(cmdline, &nb_args, args);
2957#ifdef DEBUG_COMPLETION
2958 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00002959 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00002960 }
2961#endif
2962
2963 /* if the line ends with a space, it means we want to complete the
2964 next arg */
2965 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002966 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002967 if (nb_args >= MAX_ARGS)
2968 return;
2969 args[nb_args++] = qemu_strdup("");
2970 }
2971 if (nb_args <= 1) {
2972 /* command completion */
2973 if (nb_args == 0)
2974 cmdname = "";
2975 else
2976 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00002977 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00002978 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002979 cmd_completion(cmdname, cmd->name);
2980 }
2981 } else {
2982 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002983 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002984 if (compare_cmd(args[0], cmd->name))
2985 goto found;
2986 }
2987 return;
2988 found:
2989 ptype = cmd->args_type;
2990 for(i = 0; i < nb_args - 2; i++) {
2991 if (*ptype != '\0') {
2992 ptype++;
2993 while (*ptype == '?')
2994 ptype++;
2995 }
2996 }
2997 str = args[nb_args - 1];
2998 switch(*ptype) {
2999 case 'F':
3000 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003001 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003002 file_completion(str);
3003 break;
3004 case 'B':
3005 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003006 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003007 bdrv_iterate(block_completion_it, (void *)str);
3008 break;
bellard7fe48482004-10-09 18:08:01 +00003009 case 's':
3010 /* XXX: more generic ? */
3011 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003012 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003013 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3014 cmd_completion(str, cmd->name);
3015 }
bellard64866c32006-05-07 18:03:31 +00003016 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003017 char *sep = strrchr(str, '-');
3018 if (sep)
3019 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003020 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003021 for(key = key_defs; key->name != NULL; key++) {
3022 cmd_completion(str, key->name);
3023 }
bellard7fe48482004-10-09 18:08:01 +00003024 }
3025 break;
bellard81d09122004-07-14 17:21:37 +00003026 default:
3027 break;
3028 }
3029 }
3030 for(i = 0; i < nb_args; i++)
3031 qemu_free(args[i]);
3032}
3033
aliguori731b0362009-03-05 23:01:42 +00003034static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003035{
aliguori731b0362009-03-05 23:01:42 +00003036 Monitor *mon = opaque;
3037
3038 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003039}
3040
aliguori731b0362009-03-05 23:01:42 +00003041static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003042{
aliguori731b0362009-03-05 23:01:42 +00003043 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003044 int i;
aliguori376253e2009-03-05 23:01:23 +00003045
aliguori731b0362009-03-05 23:01:42 +00003046 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003047
aliguoricde76ee2009-03-05 23:01:51 +00003048 if (cur_mon->rs) {
3049 for (i = 0; i < size; i++)
3050 readline_handle_byte(cur_mon->rs, buf[i]);
3051 } else {
3052 if (size == 0 || buf[size - 1] != 0)
3053 monitor_printf(cur_mon, "corrupted command\n");
3054 else
3055 monitor_handle_command(cur_mon, (char *)buf);
3056 }
aliguori731b0362009-03-05 23:01:42 +00003057
3058 cur_mon = old_mon;
3059}
aliguorid8f44602008-10-06 13:52:44 +00003060
aliguori376253e2009-03-05 23:01:23 +00003061static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003062{
aliguori731b0362009-03-05 23:01:42 +00003063 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003064 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003065 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003066}
3067
aliguoricde76ee2009-03-05 23:01:51 +00003068int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003069{
aliguoricde76ee2009-03-05 23:01:51 +00003070 if (!mon->rs)
3071 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003072 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003073 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003074}
3075
aliguori376253e2009-03-05 23:01:23 +00003076void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003077{
aliguoricde76ee2009-03-05 23:01:51 +00003078 if (!mon->rs)
3079 return;
aliguori731b0362009-03-05 23:01:42 +00003080 if (--mon->suspend_cnt == 0)
3081 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003082}
3083
aliguori731b0362009-03-05 23:01:42 +00003084static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003085{
aliguori376253e2009-03-05 23:01:23 +00003086 Monitor *mon = opaque;
3087
aliguori2724b182009-03-05 23:01:47 +00003088 switch (event) {
3089 case CHR_EVENT_MUX_IN:
3090 readline_restart(mon->rs);
3091 monitor_resume(mon);
3092 monitor_flush(mon);
3093 break;
ths86e94de2007-01-05 22:01:59 +00003094
aliguori2724b182009-03-05 23:01:47 +00003095 case CHR_EVENT_MUX_OUT:
3096 if (mon->suspend_cnt == 0)
3097 monitor_printf(mon, "\n");
3098 monitor_flush(mon);
3099 monitor_suspend(mon);
3100 break;
3101
3102 case CHR_EVENT_RESET:
3103 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3104 "information\n", QEMU_VERSION);
3105 if (mon->chr->focus == 0)
3106 readline_show_prompt(mon->rs);
3107 break;
3108 }
ths86e94de2007-01-05 22:01:59 +00003109}
3110
aliguori76655d62009-03-06 20:27:37 +00003111
3112/*
3113 * Local variables:
3114 * c-indent-level: 4
3115 * c-basic-offset: 4
3116 * tab-width: 8
3117 * End:
3118 */
3119
aliguori731b0362009-03-05 23:01:42 +00003120void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003121{
aliguori731b0362009-03-05 23:01:42 +00003122 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003123 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003124
3125 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003126 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003127 is_first_init = 0;
3128 }
aliguori87127162009-03-05 23:01:29 +00003129
3130 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003131
aliguori87127162009-03-05 23:01:29 +00003132 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003133 mon->flags = flags;
aliguori2724b182009-03-05 23:01:47 +00003134 if (mon->chr->focus != 0)
3135 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
aliguoricde76ee2009-03-05 23:01:51 +00003136 if (flags & MONITOR_USE_READLINE) {
3137 mon->rs = readline_init(mon, monitor_find_completion);
3138 monitor_read_command(mon, 0);
3139 }
aliguori87127162009-03-05 23:01:29 +00003140
aliguori731b0362009-03-05 23:01:42 +00003141 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3142 mon);
aliguori87127162009-03-05 23:01:29 +00003143
3144 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003145 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003146 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003147}
3148
aliguori376253e2009-03-05 23:01:23 +00003149static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003150{
aliguoribb5fc202009-03-05 23:01:15 +00003151 BlockDriverState *bs = opaque;
3152 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003153
aliguoribb5fc202009-03-05 23:01:15 +00003154 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003155 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003156 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003157 }
aliguori731b0362009-03-05 23:01:42 +00003158 if (mon->password_completion_cb)
3159 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003160
aliguori731b0362009-03-05 23:01:42 +00003161 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003162}
aliguoric0f4ce72009-03-05 23:01:01 +00003163
aliguori376253e2009-03-05 23:01:23 +00003164void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003165 BlockDriverCompletionFunc *completion_cb,
3166 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003167{
aliguoricde76ee2009-03-05 23:01:51 +00003168 int err;
3169
aliguoribb5fc202009-03-05 23:01:15 +00003170 if (!bdrv_key_required(bs)) {
3171 if (completion_cb)
3172 completion_cb(opaque, 0);
3173 return;
3174 }
aliguoric0f4ce72009-03-05 23:01:01 +00003175
aliguori376253e2009-03-05 23:01:23 +00003176 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3177 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003178
aliguori731b0362009-03-05 23:01:42 +00003179 mon->password_completion_cb = completion_cb;
3180 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003181
aliguoricde76ee2009-03-05 23:01:51 +00003182 err = monitor_read_password(mon, bdrv_password_cb, bs);
3183
3184 if (err && completion_cb)
3185 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003186}