blob: 12a89e936426bd7d379cf7799251546e652e0e70 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw/hw.h"
25#include "hw/usb.h"
26#include "hw/pcmcia.h"
27#include "hw/pc.h"
28#include "hw/pci.h"
29#include "gdbstub.h"
30#include "net.h"
31#include "qemu-char.h"
32#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000033#include "monitor.h"
34#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000035#include "console.h"
36#include "block.h"
37#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000038#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000039#include "balloon.h"
bellard81d09122004-07-14 17:21:37 +000040#include <dirent.h>
balrogc8256f92008-06-08 22:45:01 +000041#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000042#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000043#include "kvm.h"
ths6a5bd302007-12-03 17:05:38 +000044
bellard9dc39cb2004-03-14 21:38:27 +000045//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000046//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000047
bellard9307c4c2004-04-04 12:57:25 +000048/*
49 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000050 *
bellard9307c4c2004-04-04 12:57:25 +000051 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000052 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000053 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000054 * 'i' 32 bit integer
55 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000056 * '/' optional gdb-like print format (like "/10x")
57 *
58 * '?' optional type (for 'F', 's' and 'i')
59 *
60 */
61
aliguori376253e2009-03-05 23:01:23 +000062typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000063 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000064 const char *args_type;
blueswir1a5f1b962008-08-17 20:21:51 +000065 void *handler;
bellard9dc39cb2004-03-14 21:38:27 +000066 const char *params;
67 const char *help;
aliguori376253e2009-03-05 23:01:23 +000068} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000069
aliguori87127162009-03-05 23:01:29 +000070struct Monitor {
71 CharDriverState *chr;
72 LIST_ENTRY(Monitor) entry;
73};
74
75static LIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +000076
aliguori376253e2009-03-05 23:01:23 +000077static const mon_cmd_t mon_cmds[];
78static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +000079
ths60fe76f2007-12-16 03:02:09 +000080static uint8_t term_outbuf[1024];
bellard7e2515e2004-08-01 21:52:19 +000081static int term_outbuf_index;
aliguoribb5fc202009-03-05 23:01:15 +000082static BlockDriverCompletionFunc *password_completion_cb;
83static void *password_opaque;
aliguori4c36ba32009-03-05 23:01:37 +000084ReadLineState *rs;
bellard81d09122004-07-14 17:21:37 +000085
aliguori87127162009-03-05 23:01:29 +000086Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +000087
aliguori83ab7952008-08-19 14:44:22 +000088static void monitor_start_input(void);
89
blueswir1b1d8e522008-10-26 13:43:07 +000090static CPUState *mon_cpu = NULL;
bellard6a00d602005-11-21 23:25:50 +000091
aliguori376253e2009-03-05 23:01:23 +000092static void monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
93 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +000094{
aliguori4c36ba32009-03-05 23:01:37 +000095 readline_start(rs, "Password: ", 1, readline_func, opaque);
aliguoribb5fc202009-03-05 23:01:15 +000096}
97
aliguori376253e2009-03-05 23:01:23 +000098void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +000099{
aliguori87127162009-03-05 23:01:29 +0000100 Monitor *m;
101
bellard7e2515e2004-08-01 21:52:19 +0000102 if (term_outbuf_index > 0) {
aliguori87127162009-03-05 23:01:29 +0000103 LIST_FOREACH(m, &mon_list, entry) {
104 if (m->chr->focus == 0)
105 qemu_chr_write(m->chr, term_outbuf, term_outbuf_index);
106 }
bellard7e2515e2004-08-01 21:52:19 +0000107 term_outbuf_index = 0;
108 }
109}
110
111/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000112static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000113{
ths60fe76f2007-12-16 03:02:09 +0000114 char c;
bellard7e2515e2004-08-01 21:52:19 +0000115 for(;;) {
116 c = *str++;
117 if (c == '\0')
118 break;
bellard7ba12602006-07-14 20:26:42 +0000119 if (c == '\n')
120 term_outbuf[term_outbuf_index++] = '\r';
bellard7e2515e2004-08-01 21:52:19 +0000121 term_outbuf[term_outbuf_index++] = c;
bellard7ba12602006-07-14 20:26:42 +0000122 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
bellard7e2515e2004-08-01 21:52:19 +0000123 c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000124 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000125 }
126}
127
aliguori376253e2009-03-05 23:01:23 +0000128void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000129{
130 char buf[4096];
131 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000132 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000133}
134
aliguori376253e2009-03-05 23:01:23 +0000135void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000136{
137 va_list ap;
138 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000139 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000140 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000141}
142
aliguori376253e2009-03-05 23:01:23 +0000143void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000144{
145 int i;
146
147 for (i = 0; filename[i]; i++) {
148 switch (filename[i]) {
149 case ' ':
150 case '"':
151 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000152 monitor_printf(mon, "\\%c", filename[i]);
thsfef30742006-12-22 14:11:32 +0000153 break;
154 case '\t':
aliguori376253e2009-03-05 23:01:23 +0000155 monitor_printf(mon, "\\t");
thsfef30742006-12-22 14:11:32 +0000156 break;
157 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000158 monitor_printf(mon, "\\r");
thsfef30742006-12-22 14:11:32 +0000159 break;
160 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000161 monitor_printf(mon, "\\n");
thsfef30742006-12-22 14:11:32 +0000162 break;
163 default:
aliguori376253e2009-03-05 23:01:23 +0000164 monitor_printf(mon, "%c", filename[i]);
thsfef30742006-12-22 14:11:32 +0000165 break;
166 }
167 }
168}
169
bellard7fe48482004-10-09 18:08:01 +0000170static int monitor_fprintf(FILE *stream, const char *fmt, ...)
171{
172 va_list ap;
173 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000174 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000175 va_end(ap);
176 return 0;
177}
178
bellard9dc39cb2004-03-14 21:38:27 +0000179static int compare_cmd(const char *name, const char *list)
180{
181 const char *p, *pstart;
182 int len;
183 len = strlen(name);
184 p = list;
185 for(;;) {
186 pstart = p;
187 p = strchr(p, '|');
188 if (!p)
189 p = pstart + strlen(pstart);
190 if ((p - pstart) == len && !memcmp(pstart, name, len))
191 return 1;
192 if (*p == '\0')
193 break;
194 p++;
195 }
196 return 0;
197}
198
aliguori376253e2009-03-05 23:01:23 +0000199static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
200 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000201{
aliguori376253e2009-03-05 23:01:23 +0000202 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000203
204 for(cmd = cmds; cmd->name != NULL; cmd++) {
205 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000206 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
207 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000208 }
209}
210
aliguori376253e2009-03-05 23:01:23 +0000211static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000212{
213 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000214 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000215 } else {
aliguori376253e2009-03-05 23:01:23 +0000216 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000217 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000218 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000219 monitor_printf(mon, "Log items (comma separated):\n");
220 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000221 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000222 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000223 }
224 }
bellard9dc39cb2004-03-14 21:38:27 +0000225 }
226}
227
aliguori376253e2009-03-05 23:01:23 +0000228static void do_commit(Monitor *mon, const char *device)
bellard9dc39cb2004-03-14 21:38:27 +0000229{
bellard7954c732006-08-01 15:52:40 +0000230 int i, all_devices;
balrog2dc7b602007-05-24 18:53:22 +0000231
bellard7954c732006-08-01 15:52:40 +0000232 all_devices = !strcmp(device, "all");
thse4bcb142007-12-02 04:51:10 +0000233 for (i = 0; i < nb_drives; i++) {
ths5fafdf22007-09-16 21:08:06 +0000234 if (all_devices ||
thse4bcb142007-12-02 04:51:10 +0000235 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
236 bdrv_commit(drives_table[i].bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000237 }
238}
239
aliguori376253e2009-03-05 23:01:23 +0000240static void do_info(Monitor *mon, const char *item)
bellard9dc39cb2004-03-14 21:38:27 +0000241{
aliguori376253e2009-03-05 23:01:23 +0000242 const mon_cmd_t *cmd;
243 void (*handler)(Monitor *);
bellard9dc39cb2004-03-14 21:38:27 +0000244
bellard9307c4c2004-04-04 12:57:25 +0000245 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000246 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000247 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000248 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000249 goto found;
250 }
251 help:
aliguori376253e2009-03-05 23:01:23 +0000252 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000253 return;
254 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000255 handler = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +0000256 handler(mon);
bellard9dc39cb2004-03-14 21:38:27 +0000257}
258
aliguori376253e2009-03-05 23:01:23 +0000259static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000260{
aliguori376253e2009-03-05 23:01:23 +0000261 monitor_printf(mon, "%s\n", QEMU_VERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000262}
263
aliguori376253e2009-03-05 23:01:23 +0000264static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000265{
266 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000267 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000268}
269
aurel32bf4f74c2008-12-18 22:42:34 +0000270#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000271static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000272{
aliguori376253e2009-03-05 23:01:23 +0000273 monitor_printf(mon, "HPET is %s by QEMU\n",
274 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000275}
aurel32bf4f74c2008-12-18 22:42:34 +0000276#endif
aliguori16b29ae2008-12-17 23:28:44 +0000277
aliguori376253e2009-03-05 23:01:23 +0000278static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000279{
aliguori376253e2009-03-05 23:01:23 +0000280 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
281 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
282 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
283 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
284 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000285}
286
bellard6a00d602005-11-21 23:25:50 +0000287/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000288static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000289{
290 CPUState *env;
291
292 for(env = first_cpu; env != NULL; env = env->next_cpu) {
293 if (env->cpu_index == cpu_index) {
294 mon_cpu = env;
295 return 0;
296 }
297 }
298 return -1;
299}
300
pbrook9596ebb2007-11-18 01:44:38 +0000301static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000302{
303 if (!mon_cpu) {
304 mon_set_cpu(0);
305 }
306 return mon_cpu;
307}
308
aliguori376253e2009-03-05 23:01:23 +0000309static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000310{
bellard6a00d602005-11-21 23:25:50 +0000311 CPUState *env;
312 env = mon_get_cpu();
313 if (!env)
314 return;
bellard9307c4c2004-04-04 12:57:25 +0000315#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000316 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000317 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000318#else
aliguori376253e2009-03-05 23:01:23 +0000319 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000320 0);
bellard9307c4c2004-04-04 12:57:25 +0000321#endif
322}
323
aliguori376253e2009-03-05 23:01:23 +0000324static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000325{
326 CPUState *env;
327
328 /* just to set the default cpu if not already done */
329 mon_get_cpu();
330
331 for(env = first_cpu; env != NULL; env = env->next_cpu) {
aliguori376253e2009-03-05 23:01:23 +0000332 monitor_printf(mon, "%c CPU #%d:",
333 (env == mon_cpu) ? '*' : ' ',
334 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000335#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000336 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
337 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000338#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000339 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000340#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000341 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
342 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000343#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000344 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000345#endif
thsead93602007-09-06 00:18:15 +0000346 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000347 monitor_printf(mon, " (halted)");
348 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000349 }
350}
351
aliguori376253e2009-03-05 23:01:23 +0000352static void do_cpu_set(Monitor *mon, int index)
bellard6a00d602005-11-21 23:25:50 +0000353{
354 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000355 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000356}
357
aliguori376253e2009-03-05 23:01:23 +0000358static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000359{
aliguori376253e2009-03-05 23:01:23 +0000360 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000361}
362
aliguori376253e2009-03-05 23:01:23 +0000363static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000364{
365 int i;
bellard7e2515e2004-08-01 21:52:19 +0000366 const char *str;
ths3b46e622007-09-17 08:09:54 +0000367
bellard7e2515e2004-08-01 21:52:19 +0000368 i = 0;
369 for(;;) {
aliguori4c36ba32009-03-05 23:01:37 +0000370 str = readline_get_history(rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000371 if (!str)
372 break;
aliguori376253e2009-03-05 23:01:23 +0000373 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000374 i++;
bellardaa455482004-04-04 13:07:25 +0000375 }
376}
377
j_mayer76a66252007-03-07 08:32:30 +0000378#if defined(TARGET_PPC)
379/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000380static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000381{
382 CPUState *env;
383
384 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000385 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000386}
387#endif
388
aliguori376253e2009-03-05 23:01:23 +0000389static void do_quit(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000390{
391 exit(0);
392}
393
aliguori376253e2009-03-05 23:01:23 +0000394static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000395{
396 if (bdrv_is_inserted(bs)) {
397 if (!force) {
398 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000399 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000400 return -1;
401 }
402 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000403 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000404 return -1;
405 }
406 }
407 bdrv_close(bs);
408 }
409 return 0;
410}
411
aliguori376253e2009-03-05 23:01:23 +0000412static void do_eject(Monitor *mon, int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000413{
414 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000415
bellard9307c4c2004-04-04 12:57:25 +0000416 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000417 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000418 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000419 return;
420 }
aliguori376253e2009-03-05 23:01:23 +0000421 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000422}
423
aliguori376253e2009-03-05 23:01:23 +0000424static void do_change_block(Monitor *mon, const char *device,
425 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000426{
427 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000428 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000429
bellard9307c4c2004-04-04 12:57:25 +0000430 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000431 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000432 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000433 return;
434 }
aurel322ecea9b2008-06-18 22:10:01 +0000435 if (fmt) {
436 drv = bdrv_find_format(fmt);
437 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000438 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000439 return;
440 }
441 }
aliguori376253e2009-03-05 23:01:23 +0000442 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000443 return;
aurel322ecea9b2008-06-18 22:10:01 +0000444 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000445 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000446}
447
aliguori376253e2009-03-05 23:01:23 +0000448static void change_vnc_password_cb(Monitor *mon, const char *password,
449 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000450{
451 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000452 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000453
454 monitor_start_input();
bellard9dc39cb2004-03-14 21:38:27 +0000455}
456
aliguori376253e2009-03-05 23:01:23 +0000457static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000458{
ths70848512007-08-25 01:37:05 +0000459 if (strcmp(target, "passwd") == 0 ||
460 strcmp(target, "password") == 0) {
aliguori2569da02008-12-10 15:14:13 +0000461 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000462 char password[9];
aliguori2569da02008-12-10 15:14:13 +0000463 strncpy(password, arg, sizeof(password));
464 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000465 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000466 } else {
aliguori376253e2009-03-05 23:01:23 +0000467 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000468 }
ths70848512007-08-25 01:37:05 +0000469 } else {
470 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000471 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000472 }
thse25a5822007-08-25 01:36:20 +0000473}
474
aliguori376253e2009-03-05 23:01:23 +0000475static void do_change(Monitor *mon, const char *device, const char *target,
476 const char *arg)
thse25a5822007-08-25 01:36:20 +0000477{
478 if (strcmp(device, "vnc") == 0) {
aliguori376253e2009-03-05 23:01:23 +0000479 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000480 } else {
aliguori376253e2009-03-05 23:01:23 +0000481 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000482 }
483}
484
aliguori376253e2009-03-05 23:01:23 +0000485static void do_screen_dump(Monitor *mon, const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000486{
pbrook95219892006-04-09 01:06:34 +0000487 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000488}
489
aliguori376253e2009-03-05 23:01:23 +0000490static void do_logfile(Monitor *mon, const char *filename)
pbrooke735b912007-06-30 13:53:24 +0000491{
492 cpu_set_log_filename(filename);
493}
494
aliguori376253e2009-03-05 23:01:23 +0000495static void do_log(Monitor *mon, const char *items)
bellardf193c792004-03-21 17:06:25 +0000496{
497 int mask;
ths3b46e622007-09-17 08:09:54 +0000498
bellard9307c4c2004-04-04 12:57:25 +0000499 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000500 mask = 0;
501 } else {
bellard9307c4c2004-04-04 12:57:25 +0000502 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000503 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000504 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000505 return;
506 }
507 }
508 cpu_set_log(mask);
509}
510
aliguori376253e2009-03-05 23:01:23 +0000511static void do_stop(Monitor *mon)
bellard8a7ddc32004-03-31 19:00:16 +0000512{
513 vm_stop(EXCP_INTERRUPT);
514}
515
aliguoribb5fc202009-03-05 23:01:15 +0000516static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000517
aliguori376253e2009-03-05 23:01:23 +0000518struct bdrv_iterate_context {
519 Monitor *mon;
520 int err;
521};
aliguoric0f4ce72009-03-05 23:01:01 +0000522
aliguori376253e2009-03-05 23:01:23 +0000523static void do_cont(Monitor *mon)
524{
525 struct bdrv_iterate_context context = { mon, 0 };
526
527 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000528 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000529 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000530 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000531}
532
aliguoribb5fc202009-03-05 23:01:15 +0000533static void bdrv_key_cb(void *opaque, int err)
534{
aliguori376253e2009-03-05 23:01:23 +0000535 Monitor *mon = opaque;
536
aliguoribb5fc202009-03-05 23:01:15 +0000537 /* another key was set successfully, retry to continue */
538 if (!err)
aliguori376253e2009-03-05 23:01:23 +0000539 do_cont(mon);
aliguoribb5fc202009-03-05 23:01:15 +0000540}
541
542static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
543{
aliguori376253e2009-03-05 23:01:23 +0000544 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000545
aliguori376253e2009-03-05 23:01:23 +0000546 if (!context->err && bdrv_key_required(bs)) {
547 context->err = -EBUSY;
548 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
549 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000550 }
551}
552
bellard67b915a2004-03-31 23:37:16 +0000553#ifdef CONFIG_GDBSTUB
aliguori376253e2009-03-05 23:01:23 +0000554static void do_gdbserver(Monitor *mon, const char *port)
bellard8a7ddc32004-03-31 19:00:16 +0000555{
pbrookcfc34752007-02-22 01:48:01 +0000556 if (!port)
bellard9307c4c2004-04-04 12:57:25 +0000557 port = DEFAULT_GDBSTUB_PORT;
pbrookcfc34752007-02-22 01:48:01 +0000558 if (gdbserver_start(port) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000559 monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",
560 port);
bellard8a7ddc32004-03-31 19:00:16 +0000561 } else {
aliguori376253e2009-03-05 23:01:23 +0000562 monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000563 }
564}
bellard67b915a2004-03-31 23:37:16 +0000565#endif
bellard8a7ddc32004-03-31 19:00:16 +0000566
aliguori376253e2009-03-05 23:01:23 +0000567static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000568{
aliguori376253e2009-03-05 23:01:23 +0000569 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000570 switch(c) {
571 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000572 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000573 break;
574 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000575 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000576 break;
577 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000578 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000579 break;
580 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000581 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000582 break;
583 default:
584 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000585 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000586 } else {
aliguori376253e2009-03-05 23:01:23 +0000587 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000588 }
589 break;
590 }
aliguori376253e2009-03-05 23:01:23 +0000591 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000592}
593
aliguori376253e2009-03-05 23:01:23 +0000594static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000595 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000596{
bellard6a00d602005-11-21 23:25:50 +0000597 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000598 int nb_per_line, l, line_size, i, max_digits, len;
599 uint8_t buf[16];
600 uint64_t v;
601
602 if (format == 'i') {
603 int flags;
604 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000605 env = mon_get_cpu();
606 if (!env && !is_physical)
607 return;
bellard9307c4c2004-04-04 12:57:25 +0000608#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000609 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000610 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000611 } else if (wsize == 4) {
612 flags = 0;
613 } else {
bellard6a15fd12006-04-12 21:07:07 +0000614 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000615 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000616 if (env) {
617#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000618 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000619 (env->segs[R_CS].flags & DESC_L_MASK))
620 flags = 2;
621 else
622#endif
623 if (!(env->segs[R_CS].flags & DESC_B_MASK))
624 flags = 1;
625 }
bellard4c27ba22004-04-25 18:05:08 +0000626 }
627#endif
aliguori376253e2009-03-05 23:01:23 +0000628 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000629 return;
630 }
631
632 len = wsize * count;
633 if (wsize == 1)
634 line_size = 8;
635 else
636 line_size = 16;
637 nb_per_line = line_size / wsize;
638 max_digits = 0;
639
640 switch(format) {
641 case 'o':
642 max_digits = (wsize * 8 + 2) / 3;
643 break;
644 default:
645 case 'x':
646 max_digits = (wsize * 8) / 4;
647 break;
648 case 'u':
649 case 'd':
650 max_digits = (wsize * 8 * 10 + 32) / 33;
651 break;
652 case 'c':
653 wsize = 1;
654 break;
655 }
656
657 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000658 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000659 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000660 else
aliguori376253e2009-03-05 23:01:23 +0000661 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000662 l = len;
663 if (l > line_size)
664 l = line_size;
665 if (is_physical) {
666 cpu_physical_memory_rw(addr, buf, l, 0);
667 } else {
bellard6a00d602005-11-21 23:25:50 +0000668 env = mon_get_cpu();
669 if (!env)
670 break;
aliguoric8f79b62008-08-18 14:00:20 +0000671 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000672 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000673 break;
674 }
bellard9307c4c2004-04-04 12:57:25 +0000675 }
ths5fafdf22007-09-16 21:08:06 +0000676 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000677 while (i < l) {
678 switch(wsize) {
679 default:
680 case 1:
681 v = ldub_raw(buf + i);
682 break;
683 case 2:
684 v = lduw_raw(buf + i);
685 break;
686 case 4:
bellard92a31b12005-02-10 22:00:52 +0000687 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000688 break;
689 case 8:
690 v = ldq_raw(buf + i);
691 break;
692 }
aliguori376253e2009-03-05 23:01:23 +0000693 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000694 switch(format) {
695 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000696 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000697 break;
698 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000699 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000700 break;
701 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000702 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000703 break;
704 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000705 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000706 break;
707 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000708 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000709 break;
710 }
711 i += wsize;
712 }
aliguori376253e2009-03-05 23:01:23 +0000713 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000714 addr += l;
715 len -= l;
716 }
717}
718
bellard92a31b12005-02-10 22:00:52 +0000719#if TARGET_LONG_BITS == 64
720#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
721#else
722#define GET_TLONG(h, l) (l)
723#endif
724
aliguori376253e2009-03-05 23:01:23 +0000725static void do_memory_dump(Monitor *mon, int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000726 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000727{
bellard92a31b12005-02-10 22:00:52 +0000728 target_long addr = GET_TLONG(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000729 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000730}
731
blueswir17743e582007-09-24 18:39:04 +0000732#if TARGET_PHYS_ADDR_BITS > 32
733#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
734#else
735#define GET_TPHYSADDR(h, l) (l)
736#endif
737
aliguori376253e2009-03-05 23:01:23 +0000738static void do_physical_memory_dump(Monitor *mon, int count, int format,
739 int size, uint32_t addrh, uint32_t addrl)
bellard92a31b12005-02-10 22:00:52 +0000740
bellard9307c4c2004-04-04 12:57:25 +0000741{
blueswir17743e582007-09-24 18:39:04 +0000742 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000743 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000744}
745
aliguori376253e2009-03-05 23:01:23 +0000746static void do_print(Monitor *mon, int count, int format, int size,
747 unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000748{
blueswir17743e582007-09-24 18:39:04 +0000749 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
750#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000751 switch(format) {
752 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000753 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000754 break;
755 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000756 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000757 break;
758 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000759 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000760 break;
761 default:
762 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000763 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000764 break;
765 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000766 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000767 break;
768 }
bellard92a31b12005-02-10 22:00:52 +0000769#else
770 switch(format) {
771 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000772 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000773 break;
774 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000775 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000776 break;
777 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000778 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000779 break;
780 default:
781 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000782 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000783 break;
784 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000785 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000786 break;
787 }
788#endif
aliguori376253e2009-03-05 23:01:23 +0000789 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000790}
791
aliguori376253e2009-03-05 23:01:23 +0000792static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000793 uint32_t size, const char *filename)
794{
795 FILE *f;
796 target_long addr = GET_TLONG(valh, vall);
797 uint32_t l;
798 CPUState *env;
799 uint8_t buf[1024];
800
801 env = mon_get_cpu();
802 if (!env)
803 return;
804
805 f = fopen(filename, "wb");
806 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000807 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000808 return;
809 }
810 while (size != 0) {
811 l = sizeof(buf);
812 if (l > size)
813 l = size;
814 cpu_memory_rw_debug(env, addr, buf, l, 0);
815 fwrite(buf, 1, l, f);
816 addr += l;
817 size -= l;
818 }
819 fclose(f);
820}
821
aliguori376253e2009-03-05 23:01:23 +0000822static void do_physical_memory_save(Monitor *mon, unsigned int valh,
823 unsigned int vall, uint32_t size,
824 const char *filename)
aurel32a8bdf7a2008-04-11 21:36:14 +0000825{
826 FILE *f;
827 uint32_t l;
828 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000829 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000830
831 f = fopen(filename, "wb");
832 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000833 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000834 return;
835 }
836 while (size != 0) {
837 l = sizeof(buf);
838 if (l > size)
839 l = size;
840 cpu_physical_memory_rw(addr, buf, l, 0);
841 fwrite(buf, 1, l, f);
842 fflush(f);
843 addr += l;
844 size -= l;
845 }
846 fclose(f);
847}
848
aliguori376253e2009-03-05 23:01:23 +0000849static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
bellarde4cf1ad2005-06-04 20:15:57 +0000850{
851 uint32_t addr;
852 uint8_t buf[1];
853 uint16_t sum;
854
855 sum = 0;
856 for(addr = start; addr < (start + size); addr++) {
857 cpu_physical_memory_rw(addr, buf, 1, 0);
858 /* BSD sum algorithm ('sum' Unix command) */
859 sum = (sum >> 1) | (sum << 15);
860 sum += buf[0];
861 }
aliguori376253e2009-03-05 23:01:23 +0000862 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000863}
864
bellarda3a91a32004-06-04 11:06:21 +0000865typedef struct {
866 int keycode;
867 const char *name;
868} KeyDef;
869
870static const KeyDef key_defs[] = {
871 { 0x2a, "shift" },
872 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000873
bellarda3a91a32004-06-04 11:06:21 +0000874 { 0x38, "alt" },
875 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000876 { 0x64, "altgr" },
877 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000878 { 0x1d, "ctrl" },
879 { 0x9d, "ctrl_r" },
880
881 { 0xdd, "menu" },
882
883 { 0x01, "esc" },
884
885 { 0x02, "1" },
886 { 0x03, "2" },
887 { 0x04, "3" },
888 { 0x05, "4" },
889 { 0x06, "5" },
890 { 0x07, "6" },
891 { 0x08, "7" },
892 { 0x09, "8" },
893 { 0x0a, "9" },
894 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000895 { 0x0c, "minus" },
896 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000897 { 0x0e, "backspace" },
898
899 { 0x0f, "tab" },
900 { 0x10, "q" },
901 { 0x11, "w" },
902 { 0x12, "e" },
903 { 0x13, "r" },
904 { 0x14, "t" },
905 { 0x15, "y" },
906 { 0x16, "u" },
907 { 0x17, "i" },
908 { 0x18, "o" },
909 { 0x19, "p" },
910
911 { 0x1c, "ret" },
912
913 { 0x1e, "a" },
914 { 0x1f, "s" },
915 { 0x20, "d" },
916 { 0x21, "f" },
917 { 0x22, "g" },
918 { 0x23, "h" },
919 { 0x24, "j" },
920 { 0x25, "k" },
921 { 0x26, "l" },
922
923 { 0x2c, "z" },
924 { 0x2d, "x" },
925 { 0x2e, "c" },
926 { 0x2f, "v" },
927 { 0x30, "b" },
928 { 0x31, "n" },
929 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000930 { 0x33, "comma" },
931 { 0x34, "dot" },
932 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000933
balrog4d3b6f62008-02-10 16:33:14 +0000934 { 0x37, "asterisk" },
935
bellarda3a91a32004-06-04 11:06:21 +0000936 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000937 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000938 { 0x3b, "f1" },
939 { 0x3c, "f2" },
940 { 0x3d, "f3" },
941 { 0x3e, "f4" },
942 { 0x3f, "f5" },
943 { 0x40, "f6" },
944 { 0x41, "f7" },
945 { 0x42, "f8" },
946 { 0x43, "f9" },
947 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +0000948 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000949 { 0x46, "scroll_lock" },
950
bellard64866c32006-05-07 18:03:31 +0000951 { 0xb5, "kp_divide" },
952 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +0000953 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +0000954 { 0x4e, "kp_add" },
955 { 0x9c, "kp_enter" },
956 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +0000957 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +0000958
959 { 0x52, "kp_0" },
960 { 0x4f, "kp_1" },
961 { 0x50, "kp_2" },
962 { 0x51, "kp_3" },
963 { 0x4b, "kp_4" },
964 { 0x4c, "kp_5" },
965 { 0x4d, "kp_6" },
966 { 0x47, "kp_7" },
967 { 0x48, "kp_8" },
968 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +0000969
bellarda3a91a32004-06-04 11:06:21 +0000970 { 0x56, "<" },
971
972 { 0x57, "f11" },
973 { 0x58, "f12" },
974
975 { 0xb7, "print" },
976
977 { 0xc7, "home" },
978 { 0xc9, "pgup" },
979 { 0xd1, "pgdn" },
980 { 0xcf, "end" },
981
982 { 0xcb, "left" },
983 { 0xc8, "up" },
984 { 0xd0, "down" },
985 { 0xcd, "right" },
986
987 { 0xd2, "insert" },
988 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +0000989#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
990 { 0xf0, "stop" },
991 { 0xf1, "again" },
992 { 0xf2, "props" },
993 { 0xf3, "undo" },
994 { 0xf4, "front" },
995 { 0xf5, "copy" },
996 { 0xf6, "open" },
997 { 0xf7, "paste" },
998 { 0xf8, "find" },
999 { 0xf9, "cut" },
1000 { 0xfa, "lf" },
1001 { 0xfb, "help" },
1002 { 0xfc, "meta_l" },
1003 { 0xfd, "meta_r" },
1004 { 0xfe, "compose" },
1005#endif
bellarda3a91a32004-06-04 11:06:21 +00001006 { 0, NULL },
1007};
1008
1009static int get_keycode(const char *key)
1010{
1011 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001012 char *endp;
1013 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001014
1015 for(p = key_defs; p->name != NULL; p++) {
1016 if (!strcmp(key, p->name))
1017 return p->keycode;
1018 }
bellard64866c32006-05-07 18:03:31 +00001019 if (strstart(key, "0x", NULL)) {
1020 ret = strtoul(key, &endp, 0);
1021 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1022 return ret;
1023 }
bellarda3a91a32004-06-04 11:06:21 +00001024 return -1;
1025}
1026
balrogc8256f92008-06-08 22:45:01 +00001027#define MAX_KEYCODES 16
1028static uint8_t keycodes[MAX_KEYCODES];
1029static int nb_pending_keycodes;
1030static QEMUTimer *key_timer;
1031
1032static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001033{
balrogc8256f92008-06-08 22:45:01 +00001034 int keycode;
1035
1036 while (nb_pending_keycodes > 0) {
1037 nb_pending_keycodes--;
1038 keycode = keycodes[nb_pending_keycodes];
1039 if (keycode & 0x80)
1040 kbd_put_keycode(0xe0);
1041 kbd_put_keycode(keycode | 0x80);
1042 }
1043}
1044
aliguori376253e2009-03-05 23:01:23 +00001045static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1046 int hold_time)
balrogc8256f92008-06-08 22:45:01 +00001047{
balrog3401c0d2008-06-04 10:05:59 +00001048 char keyname_buf[16];
1049 char *separator;
1050 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +00001051
balrogc8256f92008-06-08 22:45:01 +00001052 if (nb_pending_keycodes > 0) {
1053 qemu_del_timer(key_timer);
1054 release_keys(NULL);
1055 }
1056 if (!has_hold_time)
1057 hold_time = 100;
1058 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001059 while (1) {
1060 separator = strchr(string, '-');
1061 keyname_len = separator ? separator - string : strlen(string);
1062 if (keyname_len > 0) {
1063 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1064 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001065 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001066 return;
bellarda3a91a32004-06-04 11:06:21 +00001067 }
balrogc8256f92008-06-08 22:45:01 +00001068 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001069 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001070 return;
1071 }
1072 keyname_buf[keyname_len] = 0;
1073 keycode = get_keycode(keyname_buf);
1074 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001075 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001076 return;
1077 }
balrogc8256f92008-06-08 22:45:01 +00001078 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001079 }
balrog3401c0d2008-06-04 10:05:59 +00001080 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001081 break;
balrog3401c0d2008-06-04 10:05:59 +00001082 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001083 }
balrogc8256f92008-06-08 22:45:01 +00001084 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001085 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001086 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001087 keycode = keycodes[i];
1088 if (keycode & 0x80)
1089 kbd_put_keycode(0xe0);
1090 kbd_put_keycode(keycode & 0x7f);
1091 }
balrogc8256f92008-06-08 22:45:01 +00001092 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001093 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1094 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001095}
1096
bellard13224a82006-07-14 22:03:35 +00001097static int mouse_button_state;
1098
aliguori376253e2009-03-05 23:01:23 +00001099static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001100 const char *dz_str)
1101{
1102 int dx, dy, dz;
1103 dx = strtol(dx_str, NULL, 0);
1104 dy = strtol(dy_str, NULL, 0);
1105 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001106 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001107 dz = strtol(dz_str, NULL, 0);
1108 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1109}
1110
aliguori376253e2009-03-05 23:01:23 +00001111static void do_mouse_button(Monitor *mon, int button_state)
bellard13224a82006-07-14 22:03:35 +00001112{
1113 mouse_button_state = button_state;
1114 kbd_mouse_event(0, 0, 0, mouse_button_state);
1115}
1116
aliguori376253e2009-03-05 23:01:23 +00001117static void do_ioport_read(Monitor *mon, int count, int format, int size,
1118 int addr, int has_index, int index)
bellard34405572004-06-08 00:55:58 +00001119{
1120 uint32_t val;
1121 int suffix;
1122
1123 if (has_index) {
1124 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1125 addr++;
1126 }
1127 addr &= 0xffff;
1128
1129 switch(size) {
1130 default:
1131 case 1:
1132 val = cpu_inb(NULL, addr);
1133 suffix = 'b';
1134 break;
1135 case 2:
1136 val = cpu_inw(NULL, addr);
1137 suffix = 'w';
1138 break;
1139 case 4:
1140 val = cpu_inl(NULL, addr);
1141 suffix = 'l';
1142 break;
1143 }
aliguori376253e2009-03-05 23:01:23 +00001144 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1145 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001146}
bellarda3a91a32004-06-04 11:06:21 +00001147
blueswir13b4366d2008-06-20 16:25:06 +00001148/* boot_set handler */
1149static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1150static void *boot_opaque;
1151
1152void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1153{
1154 qemu_boot_set_handler = func;
1155 boot_opaque = opaque;
1156}
1157
aliguori376253e2009-03-05 23:01:23 +00001158static void do_boot_set(Monitor *mon, const char *bootdevice)
aurel320ecdffb2008-05-04 20:11:34 +00001159{
1160 int res;
1161
1162 if (qemu_boot_set_handler) {
blueswir13b4366d2008-06-20 16:25:06 +00001163 res = qemu_boot_set_handler(boot_opaque, bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001164 if (res == 0)
aliguori376253e2009-03-05 23:01:23 +00001165 monitor_printf(mon, "boot device list now set to %s\n",
1166 bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001167 else
aliguori376253e2009-03-05 23:01:23 +00001168 monitor_printf(mon, "setting boot device list failed with "
1169 "error %i\n", res);
aurel320ecdffb2008-05-04 20:11:34 +00001170 } else {
aliguori376253e2009-03-05 23:01:23 +00001171 monitor_printf(mon, "no function defined to set boot device list for "
1172 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001173 }
1174}
1175
aliguori376253e2009-03-05 23:01:23 +00001176static void do_system_reset(Monitor *mon)
bellarde4f90822004-06-20 12:35:44 +00001177{
1178 qemu_system_reset_request();
1179}
1180
aliguori376253e2009-03-05 23:01:23 +00001181static void do_system_powerdown(Monitor *mon)
bellard34751872005-07-02 14:31:34 +00001182{
1183 qemu_system_powerdown_request();
1184}
1185
bellardb86bda52004-09-18 19:32:46 +00001186#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001187static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001188{
aliguori376253e2009-03-05 23:01:23 +00001189 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1190 addr,
1191 pte & mask,
1192 pte & PG_GLOBAL_MASK ? 'G' : '-',
1193 pte & PG_PSE_MASK ? 'P' : '-',
1194 pte & PG_DIRTY_MASK ? 'D' : '-',
1195 pte & PG_ACCESSED_MASK ? 'A' : '-',
1196 pte & PG_PCD_MASK ? 'C' : '-',
1197 pte & PG_PWT_MASK ? 'T' : '-',
1198 pte & PG_USER_MASK ? 'U' : '-',
1199 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001200}
1201
aliguori376253e2009-03-05 23:01:23 +00001202static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001203{
bellard6a00d602005-11-21 23:25:50 +00001204 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001205 int l1, l2;
1206 uint32_t pgd, pde, pte;
1207
bellard6a00d602005-11-21 23:25:50 +00001208 env = mon_get_cpu();
1209 if (!env)
1210 return;
1211
bellardb86bda52004-09-18 19:32:46 +00001212 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001213 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001214 return;
1215 }
1216 pgd = env->cr[3] & ~0xfff;
1217 for(l1 = 0; l1 < 1024; l1++) {
1218 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1219 pde = le32_to_cpu(pde);
1220 if (pde & PG_PRESENT_MASK) {
1221 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001222 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001223 } else {
1224 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001225 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001226 (uint8_t *)&pte, 4);
1227 pte = le32_to_cpu(pte);
1228 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001229 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001230 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001231 ~0xfff);
1232 }
1233 }
1234 }
1235 }
1236 }
1237}
1238
aliguori376253e2009-03-05 23:01:23 +00001239static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001240 uint32_t end, int prot)
1241{
bellard9746b152004-11-11 18:30:24 +00001242 int prot1;
1243 prot1 = *plast_prot;
1244 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001245 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001246 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1247 *pstart, end, end - *pstart,
1248 prot1 & PG_USER_MASK ? 'u' : '-',
1249 'r',
1250 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001251 }
1252 if (prot != 0)
1253 *pstart = end;
1254 else
1255 *pstart = -1;
1256 *plast_prot = prot;
1257 }
1258}
1259
aliguori376253e2009-03-05 23:01:23 +00001260static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001261{
bellard6a00d602005-11-21 23:25:50 +00001262 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001263 int l1, l2, prot, last_prot;
1264 uint32_t pgd, pde, pte, start, end;
1265
bellard6a00d602005-11-21 23:25:50 +00001266 env = mon_get_cpu();
1267 if (!env)
1268 return;
1269
bellardb86bda52004-09-18 19:32:46 +00001270 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001271 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001272 return;
1273 }
1274 pgd = env->cr[3] & ~0xfff;
1275 last_prot = 0;
1276 start = -1;
1277 for(l1 = 0; l1 < 1024; l1++) {
1278 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1279 pde = le32_to_cpu(pde);
1280 end = l1 << 22;
1281 if (pde & PG_PRESENT_MASK) {
1282 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1283 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001284 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001285 } else {
1286 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001287 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001288 (uint8_t *)&pte, 4);
1289 pte = le32_to_cpu(pte);
1290 end = (l1 << 22) + (l2 << 12);
1291 if (pte & PG_PRESENT_MASK) {
1292 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1293 } else {
1294 prot = 0;
1295 }
aliguori376253e2009-03-05 23:01:23 +00001296 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001297 }
1298 }
1299 } else {
1300 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001301 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001302 }
1303 }
1304}
1305#endif
1306
aurel327c664e22009-03-03 06:12:22 +00001307#if defined(TARGET_SH4)
1308
aliguori376253e2009-03-05 23:01:23 +00001309static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001310{
aliguori376253e2009-03-05 23:01:23 +00001311 monitor_printf(mon, " tlb%i:\t"
1312 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1313 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1314 "dirty=%hhu writethrough=%hhu\n",
1315 idx,
1316 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1317 tlb->v, tlb->sh, tlb->c, tlb->pr,
1318 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001319}
1320
aliguori376253e2009-03-05 23:01:23 +00001321static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001322{
1323 CPUState *env = mon_get_cpu();
1324 int i;
1325
aliguori376253e2009-03-05 23:01:23 +00001326 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001327 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001328 print_tlb (mon, i, &env->itlb[i]);
1329 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001330 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001331 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001332}
1333
1334#endif
1335
aliguori376253e2009-03-05 23:01:23 +00001336static void do_info_kqemu(Monitor *mon)
bellard0f4c6412005-07-24 18:10:56 +00001337{
1338#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001339 CPUState *env;
bellard0f4c6412005-07-24 18:10:56 +00001340 int val;
1341 val = 0;
bellard6a00d602005-11-21 23:25:50 +00001342 env = mon_get_cpu();
1343 if (!env) {
aliguori376253e2009-03-05 23:01:23 +00001344 monitor_printf(mon, "No cpu initialized yet");
bellard6a00d602005-11-21 23:25:50 +00001345 return;
1346 }
1347 val = env->kqemu_enabled;
aliguori376253e2009-03-05 23:01:23 +00001348 monitor_printf(mon, "kqemu support: ");
bellard5f1ce942006-02-08 22:40:15 +00001349 switch(val) {
1350 default:
1351 case 0:
aliguori376253e2009-03-05 23:01:23 +00001352 monitor_printf(mon, "disabled\n");
bellard5f1ce942006-02-08 22:40:15 +00001353 break;
1354 case 1:
aliguori376253e2009-03-05 23:01:23 +00001355 monitor_printf(mon, "enabled for user code\n");
bellard5f1ce942006-02-08 22:40:15 +00001356 break;
1357 case 2:
aliguori376253e2009-03-05 23:01:23 +00001358 monitor_printf(mon, "enabled for user and kernel code\n");
bellard5f1ce942006-02-08 22:40:15 +00001359 break;
1360 }
bellard0f4c6412005-07-24 18:10:56 +00001361#else
aliguori376253e2009-03-05 23:01:23 +00001362 monitor_printf(mon, "kqemu support: not compiled\n");
bellard0f4c6412005-07-24 18:10:56 +00001363#endif
ths5fafdf22007-09-16 21:08:06 +00001364}
bellard0f4c6412005-07-24 18:10:56 +00001365
aliguori376253e2009-03-05 23:01:23 +00001366static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001367{
1368#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001369 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001370 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001371 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001372 else
aliguori376253e2009-03-05 23:01:23 +00001373 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001374#else
aliguori376253e2009-03-05 23:01:23 +00001375 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001376#endif
1377}
1378
bellard5f1ce942006-02-08 22:40:15 +00001379#ifdef CONFIG_PROFILER
1380
1381int64_t kqemu_time;
1382int64_t qemu_time;
1383int64_t kqemu_exec_count;
1384int64_t dev_time;
1385int64_t kqemu_ret_int_count;
1386int64_t kqemu_ret_excp_count;
1387int64_t kqemu_ret_intr_count;
1388
aliguori376253e2009-03-05 23:01:23 +00001389static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001390{
1391 int64_t total;
1392 total = qemu_time;
1393 if (total == 0)
1394 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001395 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1396 dev_time, dev_time / (double)ticks_per_sec);
1397 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1398 qemu_time, qemu_time / (double)ticks_per_sec);
1399 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1400 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1401 PRId64 "\n",
1402 kqemu_time, kqemu_time / (double)ticks_per_sec,
1403 kqemu_time / (double)total * 100.0,
1404 kqemu_exec_count,
1405 kqemu_ret_int_count,
1406 kqemu_ret_excp_count,
1407 kqemu_ret_intr_count);
bellard5f1ce942006-02-08 22:40:15 +00001408 qemu_time = 0;
1409 kqemu_time = 0;
1410 kqemu_exec_count = 0;
1411 dev_time = 0;
1412 kqemu_ret_int_count = 0;
1413 kqemu_ret_excp_count = 0;
1414 kqemu_ret_intr_count = 0;
1415#ifdef USE_KQEMU
1416 kqemu_record_dump();
1417#endif
1418}
1419#else
aliguori376253e2009-03-05 23:01:23 +00001420static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001421{
aliguori376253e2009-03-05 23:01:23 +00001422 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001423}
1424#endif
1425
bellardec36b692006-07-16 18:57:03 +00001426/* Capture support */
1427static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1428
aliguori376253e2009-03-05 23:01:23 +00001429static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001430{
1431 int i;
1432 CaptureState *s;
1433
1434 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001435 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001436 s->ops.info (s->opaque);
1437 }
1438}
1439
aliguori376253e2009-03-05 23:01:23 +00001440static void do_stop_capture(Monitor *mon, int n)
bellardec36b692006-07-16 18:57:03 +00001441{
1442 int i;
1443 CaptureState *s;
1444
1445 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1446 if (i == n) {
1447 s->ops.destroy (s->opaque);
1448 LIST_REMOVE (s, entries);
1449 qemu_free (s);
1450 return;
1451 }
1452 }
1453}
1454
1455#ifdef HAS_AUDIO
aliguori376253e2009-03-05 23:01:23 +00001456static void do_wav_capture(Monitor *mon, const char *path,
1457 int has_freq, int freq,
1458 int has_bits, int bits,
1459 int has_channels, int nchannels)
bellardec36b692006-07-16 18:57:03 +00001460{
1461 CaptureState *s;
1462
1463 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001464
1465 freq = has_freq ? freq : 44100;
1466 bits = has_bits ? bits : 16;
1467 nchannels = has_channels ? nchannels : 2;
1468
1469 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001470 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001471 qemu_free (s);
1472 }
1473 LIST_INSERT_HEAD (&capture_head, s, entries);
1474}
1475#endif
1476
aurel32dc1c0b72008-04-27 23:52:12 +00001477#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001478static void do_inject_nmi(Monitor *mon, int cpu_index)
aurel32dc1c0b72008-04-27 23:52:12 +00001479{
1480 CPUState *env;
1481
1482 for (env = first_cpu; env != NULL; env = env->next_cpu)
1483 if (env->cpu_index == cpu_index) {
1484 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1485 break;
1486 }
1487}
1488#endif
1489
aliguori376253e2009-03-05 23:01:23 +00001490static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001491{
1492 if (vm_running)
aliguori376253e2009-03-05 23:01:23 +00001493 monitor_printf(mon, "VM status: running\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001494 else
aliguori376253e2009-03-05 23:01:23 +00001495 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001496}
1497
1498
aliguori376253e2009-03-05 23:01:23 +00001499static void do_balloon(Monitor *mon, int value)
aliguoridf751fa2008-12-04 20:19:35 +00001500{
1501 ram_addr_t target = value;
1502 qemu_balloon(target << 20);
1503}
1504
aliguori376253e2009-03-05 23:01:23 +00001505static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001506{
1507 ram_addr_t actual;
1508
1509 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001510 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001511 monitor_printf(mon, "Using KVM without synchronous MMU, "
1512 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001513 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001514 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001515 else
aliguori376253e2009-03-05 23:01:23 +00001516 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001517}
1518
blueswir1d2c639d2009-01-24 18:19:25 +00001519/* Please update qemu-doc.texi when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001520static const mon_cmd_t mon_cmds[] = {
1521 { "help|?", "s?", help_cmd,
bellard9dc39cb2004-03-14 21:38:27 +00001522 "[cmd]", "show the help" },
ths5fafdf22007-09-16 21:08:06 +00001523 { "commit", "s", do_commit,
bellard7954c732006-08-01 15:52:40 +00001524 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
bellard9307c4c2004-04-04 12:57:25 +00001525 { "info", "s?", do_info,
bellard9dc39cb2004-03-14 21:38:27 +00001526 "subcommand", "show various information about the system state" },
bellard9307c4c2004-04-04 12:57:25 +00001527 { "q|quit", "", do_quit,
bellard9dc39cb2004-03-14 21:38:27 +00001528 "", "quit the emulator" },
bellard81d09122004-07-14 17:21:37 +00001529 { "eject", "-fB", do_eject,
thse5987522007-03-30 18:58:01 +00001530 "[-f] device", "eject a removable medium (use -f to force it)" },
aurel322ecea9b2008-06-18 22:10:01 +00001531 { "change", "BFs?", do_change,
1532 "device filename [format]", "change a removable medium, optional format" },
ths5fafdf22007-09-16 21:08:06 +00001533 { "screendump", "F", do_screen_dump,
bellard59a983b2004-03-17 23:17:16 +00001534 "filename", "save screen into PPM image 'filename'" },
balrog788228c2008-05-24 23:15:46 +00001535 { "logfile", "F", do_logfile,
pbrooke735b912007-06-30 13:53:24 +00001536 "filename", "output logs to 'filename'" },
bellard9307c4c2004-04-04 12:57:25 +00001537 { "log", "s", do_log,
ths5fafdf22007-09-16 21:08:06 +00001538 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
bellardfaea38e2006-08-05 21:31:00 +00001539 { "savevm", "s?", do_savevm,
ths5fafdf22007-09-16 21:08:06 +00001540 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
bellardfaea38e2006-08-05 21:31:00 +00001541 { "loadvm", "s", do_loadvm,
ths5fafdf22007-09-16 21:08:06 +00001542 "tag|id", "restore a VM snapshot from its tag or id" },
bellardfaea38e2006-08-05 21:31:00 +00001543 { "delvm", "s", do_delvm,
ths5fafdf22007-09-16 21:08:06 +00001544 "tag|id", "delete a VM snapshot from its tag or id" },
1545 { "stop", "", do_stop,
bellard9307c4c2004-04-04 12:57:25 +00001546 "", "stop emulation", },
ths5fafdf22007-09-16 21:08:06 +00001547 { "c|cont", "", do_cont,
bellard9307c4c2004-04-04 12:57:25 +00001548 "", "resume emulation", },
bellard67b915a2004-03-31 23:37:16 +00001549#ifdef CONFIG_GDBSTUB
ths5fafdf22007-09-16 21:08:06 +00001550 { "gdbserver", "s?", do_gdbserver,
bellard9307c4c2004-04-04 12:57:25 +00001551 "[port]", "start gdbserver session (default port=1234)", },
bellard67b915a2004-03-31 23:37:16 +00001552#endif
ths5fafdf22007-09-16 21:08:06 +00001553 { "x", "/l", do_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001554 "/fmt addr", "virtual memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001555 { "xp", "/l", do_physical_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001556 "/fmt addr", "physical memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001557 { "p|print", "/l", do_print,
bellard9307c4c2004-04-04 12:57:25 +00001558 "/fmt expr", "print expression value (use $reg for CPU register access)", },
ths5fafdf22007-09-16 21:08:06 +00001559 { "i", "/ii.", do_ioport_read,
bellard34405572004-06-08 00:55:58 +00001560 "/fmt addr", "I/O port read" },
1561
balrogc8256f92008-06-08 22:45:01 +00001562 { "sendkey", "si?", do_sendkey,
1563 "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 +00001564 { "system_reset", "", do_system_reset,
bellarde4f90822004-06-20 12:35:44 +00001565 "", "reset the system" },
ths5fafdf22007-09-16 21:08:06 +00001566 { "system_powerdown", "", do_system_powerdown,
bellard34751872005-07-02 14:31:34 +00001567 "", "send system power down event" },
ths5fafdf22007-09-16 21:08:06 +00001568 { "sum", "ii", do_sum,
bellarde4cf1ad2005-06-04 20:15:57 +00001569 "addr size", "compute the checksum of a memory region" },
bellarda594cfb2005-11-06 16:13:29 +00001570 { "usb_add", "s", do_usb_add,
1571 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1572 { "usb_del", "s", do_usb_del,
1573 "device", "remove USB device 'bus.addr'" },
ths5fafdf22007-09-16 21:08:06 +00001574 { "cpu", "i", do_cpu_set,
bellard6a00d602005-11-21 23:25:50 +00001575 "index", "set the default CPU" },
ths5fafdf22007-09-16 21:08:06 +00001576 { "mouse_move", "sss?", do_mouse_move,
bellard13224a82006-07-14 22:03:35 +00001577 "dx dy [dz]", "send mouse move events" },
ths5fafdf22007-09-16 21:08:06 +00001578 { "mouse_button", "i", do_mouse_button,
bellard13224a82006-07-14 22:03:35 +00001579 "state", "change mouse button state (1=L, 2=M, 4=R)" },
ths455204e2007-01-05 16:42:13 +00001580 { "mouse_set", "i", do_mouse_set,
1581 "index", "set which mouse device receives events" },
bellardec36b692006-07-16 18:57:03 +00001582#ifdef HAS_AUDIO
1583 { "wavcapture", "si?i?i?", do_wav_capture,
1584 "path [frequency bits channels]",
1585 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1586#endif
blueswir1d2c639d2009-01-24 18:19:25 +00001587 { "stopcapture", "i", do_stop_capture,
1588 "capture index", "stop capture" },
ths5fafdf22007-09-16 21:08:06 +00001589 { "memsave", "lis", do_memory_save,
bellardb371dc52007-01-03 15:20:39 +00001590 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
aurel32a8bdf7a2008-04-11 21:36:14 +00001591 { "pmemsave", "lis", do_physical_memory_save,
1592 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
aurel320ecdffb2008-05-04 20:11:34 +00001593 { "boot_set", "s", do_boot_set,
1594 "bootdevice", "define new values for the boot device list" },
aurel32dc1c0b72008-04-27 23:52:12 +00001595#if defined(TARGET_I386)
1596 { "nmi", "i", do_inject_nmi,
1597 "cpu", "inject an NMI on the given CPU", },
1598#endif
aliguori5bb79102008-10-13 03:12:02 +00001599 { "migrate", "-ds", do_migrate,
1600 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1601 { "migrate_cancel", "", do_migrate_cancel,
1602 "", "cancel the current VM migration" },
1603 { "migrate_set_speed", "s", do_migrate_set_speed,
1604 "value", "set maximum speed (in bytes) for migrations" },
aliguori6f338c32009-02-11 15:21:54 +00001605#if defined(TARGET_I386)
1606 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1607 "[file=file][,if=type][,bus=n]\n"
1608 "[,unit=m][,media=d][index=i]\n"
1609 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1610 "[snapshot=on|off][,cache=on|off]",
1611 "add drive to PCI storage controller" },
1612 { "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" },
1613 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1614 { "host_net_add", "ss", net_host_device_add,
1615 "[tap,user,socket,vde] options", "add host VLAN client" },
1616 { "host_net_remove", "is", net_host_device_remove,
1617 "vlan_id name", "remove host VLAN client" },
1618#endif
aliguoridf751fa2008-12-04 20:19:35 +00001619 { "balloon", "i", do_balloon,
1620 "target", "request VM to change it's memory allocation (in MB)" },
aliguorif029bd92009-02-11 15:19:16 +00001621 { "set_link", "ss", do_set_link,
1622 "name [up|down]", "change the link status of a network adapter" },
ths5fafdf22007-09-16 21:08:06 +00001623 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001624};
1625
blueswir1d2c639d2009-01-24 18:19:25 +00001626/* Please update qemu-doc.texi when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001627static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001628 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001629 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001630 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001631 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001632 { "chardev", "", qemu_chr_info,
1633 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001634 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001635 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001636 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001637 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001638 { "registers", "", do_info_registers,
1639 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001640 { "cpus", "", do_info_cpus,
1641 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001642 { "history", "", do_info_history,
1643 "", "show the command line history", },
bellard4a0fb712004-05-21 11:39:07 +00001644 { "irq", "", irq_info,
1645 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001646 { "pic", "", pic_info,
1647 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001648 { "pci", "", pci_info,
1649 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001650#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001651 { "tlb", "", tlb_info,
1652 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001653#endif
1654#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001655 { "mem", "", mem_info,
1656 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001657 { "hpet", "", do_info_hpet,
1658 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001659#endif
bellarde3db7222005-01-26 22:00:47 +00001660 { "jit", "", do_info_jit,
1661 "", "show dynamic compiler info", },
bellard0f4c6412005-07-24 18:10:56 +00001662 { "kqemu", "", do_info_kqemu,
blueswir1d2c639d2009-01-24 18:19:25 +00001663 "", "show KQEMU information", },
aliguori7ba1e612008-11-05 16:04:33 +00001664 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001665 "", "show KVM information", },
bellarda594cfb2005-11-06 16:13:29 +00001666 { "usb", "", usb_info,
1667 "", "show guest USB devices", },
1668 { "usbhost", "", usb_host_info,
1669 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001670 { "profile", "", do_info_profile,
1671 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001672 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001673 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001674 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001675 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001676 { "status", "", do_info_status,
1677 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001678 { "pcmcia", "", pcmcia_info,
1679 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001680 { "mice", "", do_info_mice,
1681 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001682 { "vnc", "", do_info_vnc,
1683 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001684 { "name", "", do_info_name,
1685 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001686 { "uuid", "", do_info_uuid,
1687 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001688#if defined(TARGET_PPC)
1689 { "cpustats", "", do_info_cpu_stats,
1690 "", "show CPU statistics", },
1691#endif
blueswir131a60e22007-10-26 18:42:59 +00001692#if defined(CONFIG_SLIRP)
1693 { "slirp", "", do_info_slirp,
1694 "", "show SLIRP statistics", },
1695#endif
aliguori5bb79102008-10-13 03:12:02 +00001696 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001697 { "balloon", "", do_info_balloon,
1698 "", "show balloon information" },
bellard9dc39cb2004-03-14 21:38:27 +00001699 { NULL, NULL, },
1700};
1701
bellard9307c4c2004-04-04 12:57:25 +00001702/*******************************************************************/
1703
1704static const char *pch;
1705static jmp_buf expr_env;
1706
bellard92a31b12005-02-10 22:00:52 +00001707#define MD_TLONG 0
1708#define MD_I32 1
1709
bellard9307c4c2004-04-04 12:57:25 +00001710typedef struct MonitorDef {
1711 const char *name;
1712 int offset;
blueswir18662d652008-10-02 18:32:44 +00001713 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001714 int type;
bellard9307c4c2004-04-04 12:57:25 +00001715} MonitorDef;
1716
bellard57206fd2004-04-25 18:54:52 +00001717#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001718static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001719{
bellard6a00d602005-11-21 23:25:50 +00001720 CPUState *env = mon_get_cpu();
1721 if (!env)
1722 return 0;
1723 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001724}
1725#endif
1726
bellarda541f292004-04-12 20:39:29 +00001727#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001728static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001729{
bellard6a00d602005-11-21 23:25:50 +00001730 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001731 unsigned int u;
1732 int i;
1733
bellard6a00d602005-11-21 23:25:50 +00001734 if (!env)
1735 return 0;
1736
bellarda541f292004-04-12 20:39:29 +00001737 u = 0;
1738 for (i = 0; i < 8; i++)
bellard6a00d602005-11-21 23:25:50 +00001739 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001740
1741 return u;
1742}
1743
blueswir18662d652008-10-02 18:32:44 +00001744static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001745{
bellard6a00d602005-11-21 23:25:50 +00001746 CPUState *env = mon_get_cpu();
1747 if (!env)
1748 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001749 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001750}
1751
blueswir18662d652008-10-02 18:32:44 +00001752static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001753{
bellard6a00d602005-11-21 23:25:50 +00001754 CPUState *env = mon_get_cpu();
1755 if (!env)
1756 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001757 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001758}
bellard9fddaa02004-05-21 12:59:32 +00001759
blueswir18662d652008-10-02 18:32:44 +00001760static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001761{
bellard6a00d602005-11-21 23:25:50 +00001762 CPUState *env = mon_get_cpu();
1763 if (!env)
1764 return 0;
1765 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001766}
1767
blueswir18662d652008-10-02 18:32:44 +00001768static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001769{
bellard6a00d602005-11-21 23:25:50 +00001770 CPUState *env = mon_get_cpu();
1771 if (!env)
1772 return 0;
1773 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001774}
1775
blueswir18662d652008-10-02 18:32:44 +00001776static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001777{
bellard6a00d602005-11-21 23:25:50 +00001778 CPUState *env = mon_get_cpu();
1779 if (!env)
1780 return 0;
1781 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001782}
bellarda541f292004-04-12 20:39:29 +00001783#endif
1784
bellarde95c8d52004-09-30 22:22:08 +00001785#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001786#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001787static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001788{
bellard6a00d602005-11-21 23:25:50 +00001789 CPUState *env = mon_get_cpu();
1790 if (!env)
1791 return 0;
1792 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001793}
bellard7b936c02005-10-30 17:05:13 +00001794#endif
bellarde95c8d52004-09-30 22:22:08 +00001795
blueswir18662d652008-10-02 18:32:44 +00001796static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001797{
bellard6a00d602005-11-21 23:25:50 +00001798 CPUState *env = mon_get_cpu();
1799 if (!env)
1800 return 0;
1801 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001802}
1803#endif
1804
blueswir18662d652008-10-02 18:32:44 +00001805static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001806#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001807
1808#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001809 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001810 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001811 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001812
bellard9307c4c2004-04-04 12:57:25 +00001813 { "eax", offsetof(CPUState, regs[0]) },
1814 { "ecx", offsetof(CPUState, regs[1]) },
1815 { "edx", offsetof(CPUState, regs[2]) },
1816 { "ebx", offsetof(CPUState, regs[3]) },
1817 { "esp|sp", offsetof(CPUState, regs[4]) },
1818 { "ebp|fp", offsetof(CPUState, regs[5]) },
1819 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001820 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001821#ifdef TARGET_X86_64
1822 { "r8", offsetof(CPUState, regs[8]) },
1823 { "r9", offsetof(CPUState, regs[9]) },
1824 { "r10", offsetof(CPUState, regs[10]) },
1825 { "r11", offsetof(CPUState, regs[11]) },
1826 { "r12", offsetof(CPUState, regs[12]) },
1827 { "r13", offsetof(CPUState, regs[13]) },
1828 { "r14", offsetof(CPUState, regs[14]) },
1829 { "r15", offsetof(CPUState, regs[15]) },
1830#endif
bellard9307c4c2004-04-04 12:57:25 +00001831 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001832 { "eip", offsetof(CPUState, eip) },
1833 SEG("cs", R_CS)
1834 SEG("ds", R_DS)
1835 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001836 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001837 SEG("fs", R_FS)
1838 SEG("gs", R_GS)
1839 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00001840#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00001841 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00001842 { "r0", offsetof(CPUState, gpr[0]) },
1843 { "r1", offsetof(CPUState, gpr[1]) },
1844 { "r2", offsetof(CPUState, gpr[2]) },
1845 { "r3", offsetof(CPUState, gpr[3]) },
1846 { "r4", offsetof(CPUState, gpr[4]) },
1847 { "r5", offsetof(CPUState, gpr[5]) },
1848 { "r6", offsetof(CPUState, gpr[6]) },
1849 { "r7", offsetof(CPUState, gpr[7]) },
1850 { "r8", offsetof(CPUState, gpr[8]) },
1851 { "r9", offsetof(CPUState, gpr[9]) },
1852 { "r10", offsetof(CPUState, gpr[10]) },
1853 { "r11", offsetof(CPUState, gpr[11]) },
1854 { "r12", offsetof(CPUState, gpr[12]) },
1855 { "r13", offsetof(CPUState, gpr[13]) },
1856 { "r14", offsetof(CPUState, gpr[14]) },
1857 { "r15", offsetof(CPUState, gpr[15]) },
1858 { "r16", offsetof(CPUState, gpr[16]) },
1859 { "r17", offsetof(CPUState, gpr[17]) },
1860 { "r18", offsetof(CPUState, gpr[18]) },
1861 { "r19", offsetof(CPUState, gpr[19]) },
1862 { "r20", offsetof(CPUState, gpr[20]) },
1863 { "r21", offsetof(CPUState, gpr[21]) },
1864 { "r22", offsetof(CPUState, gpr[22]) },
1865 { "r23", offsetof(CPUState, gpr[23]) },
1866 { "r24", offsetof(CPUState, gpr[24]) },
1867 { "r25", offsetof(CPUState, gpr[25]) },
1868 { "r26", offsetof(CPUState, gpr[26]) },
1869 { "r27", offsetof(CPUState, gpr[27]) },
1870 { "r28", offsetof(CPUState, gpr[28]) },
1871 { "r29", offsetof(CPUState, gpr[29]) },
1872 { "r30", offsetof(CPUState, gpr[30]) },
1873 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00001874 /* Floating point registers */
1875 { "f0", offsetof(CPUState, fpr[0]) },
1876 { "f1", offsetof(CPUState, fpr[1]) },
1877 { "f2", offsetof(CPUState, fpr[2]) },
1878 { "f3", offsetof(CPUState, fpr[3]) },
1879 { "f4", offsetof(CPUState, fpr[4]) },
1880 { "f5", offsetof(CPUState, fpr[5]) },
1881 { "f6", offsetof(CPUState, fpr[6]) },
1882 { "f7", offsetof(CPUState, fpr[7]) },
1883 { "f8", offsetof(CPUState, fpr[8]) },
1884 { "f9", offsetof(CPUState, fpr[9]) },
1885 { "f10", offsetof(CPUState, fpr[10]) },
1886 { "f11", offsetof(CPUState, fpr[11]) },
1887 { "f12", offsetof(CPUState, fpr[12]) },
1888 { "f13", offsetof(CPUState, fpr[13]) },
1889 { "f14", offsetof(CPUState, fpr[14]) },
1890 { "f15", offsetof(CPUState, fpr[15]) },
1891 { "f16", offsetof(CPUState, fpr[16]) },
1892 { "f17", offsetof(CPUState, fpr[17]) },
1893 { "f18", offsetof(CPUState, fpr[18]) },
1894 { "f19", offsetof(CPUState, fpr[19]) },
1895 { "f20", offsetof(CPUState, fpr[20]) },
1896 { "f21", offsetof(CPUState, fpr[21]) },
1897 { "f22", offsetof(CPUState, fpr[22]) },
1898 { "f23", offsetof(CPUState, fpr[23]) },
1899 { "f24", offsetof(CPUState, fpr[24]) },
1900 { "f25", offsetof(CPUState, fpr[25]) },
1901 { "f26", offsetof(CPUState, fpr[26]) },
1902 { "f27", offsetof(CPUState, fpr[27]) },
1903 { "f28", offsetof(CPUState, fpr[28]) },
1904 { "f29", offsetof(CPUState, fpr[29]) },
1905 { "f30", offsetof(CPUState, fpr[30]) },
1906 { "f31", offsetof(CPUState, fpr[31]) },
1907 { "fpscr", offsetof(CPUState, fpscr) },
1908 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00001909 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00001910 { "lr", offsetof(CPUState, lr) },
1911 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00001912 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00001913 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00001914 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00001915 { "msr", 0, &monitor_get_msr, },
1916 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00001917 { "tbu", 0, &monitor_get_tbu, },
1918 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00001919#if defined(TARGET_PPC64)
1920 /* Address space register */
1921 { "asr", offsetof(CPUState, asr) },
1922#endif
1923 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00001924 { "sdr1", offsetof(CPUState, sdr1) },
1925 { "sr0", offsetof(CPUState, sr[0]) },
1926 { "sr1", offsetof(CPUState, sr[1]) },
1927 { "sr2", offsetof(CPUState, sr[2]) },
1928 { "sr3", offsetof(CPUState, sr[3]) },
1929 { "sr4", offsetof(CPUState, sr[4]) },
1930 { "sr5", offsetof(CPUState, sr[5]) },
1931 { "sr6", offsetof(CPUState, sr[6]) },
1932 { "sr7", offsetof(CPUState, sr[7]) },
1933 { "sr8", offsetof(CPUState, sr[8]) },
1934 { "sr9", offsetof(CPUState, sr[9]) },
1935 { "sr10", offsetof(CPUState, sr[10]) },
1936 { "sr11", offsetof(CPUState, sr[11]) },
1937 { "sr12", offsetof(CPUState, sr[12]) },
1938 { "sr13", offsetof(CPUState, sr[13]) },
1939 { "sr14", offsetof(CPUState, sr[14]) },
1940 { "sr15", offsetof(CPUState, sr[15]) },
1941 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00001942#elif defined(TARGET_SPARC)
1943 { "g0", offsetof(CPUState, gregs[0]) },
1944 { "g1", offsetof(CPUState, gregs[1]) },
1945 { "g2", offsetof(CPUState, gregs[2]) },
1946 { "g3", offsetof(CPUState, gregs[3]) },
1947 { "g4", offsetof(CPUState, gregs[4]) },
1948 { "g5", offsetof(CPUState, gregs[5]) },
1949 { "g6", offsetof(CPUState, gregs[6]) },
1950 { "g7", offsetof(CPUState, gregs[7]) },
1951 { "o0", 0, monitor_get_reg },
1952 { "o1", 1, monitor_get_reg },
1953 { "o2", 2, monitor_get_reg },
1954 { "o3", 3, monitor_get_reg },
1955 { "o4", 4, monitor_get_reg },
1956 { "o5", 5, monitor_get_reg },
1957 { "o6", 6, monitor_get_reg },
1958 { "o7", 7, monitor_get_reg },
1959 { "l0", 8, monitor_get_reg },
1960 { "l1", 9, monitor_get_reg },
1961 { "l2", 10, monitor_get_reg },
1962 { "l3", 11, monitor_get_reg },
1963 { "l4", 12, monitor_get_reg },
1964 { "l5", 13, monitor_get_reg },
1965 { "l6", 14, monitor_get_reg },
1966 { "l7", 15, monitor_get_reg },
1967 { "i0", 16, monitor_get_reg },
1968 { "i1", 17, monitor_get_reg },
1969 { "i2", 18, monitor_get_reg },
1970 { "i3", 19, monitor_get_reg },
1971 { "i4", 20, monitor_get_reg },
1972 { "i5", 21, monitor_get_reg },
1973 { "i6", 22, monitor_get_reg },
1974 { "i7", 23, monitor_get_reg },
1975 { "pc", offsetof(CPUState, pc) },
1976 { "npc", offsetof(CPUState, npc) },
1977 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00001978#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00001979 { "psr", 0, &monitor_get_psr, },
1980 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00001981#endif
bellarde95c8d52004-09-30 22:22:08 +00001982 { "tbr", offsetof(CPUState, tbr) },
1983 { "fsr", offsetof(CPUState, fsr) },
1984 { "f0", offsetof(CPUState, fpr[0]) },
1985 { "f1", offsetof(CPUState, fpr[1]) },
1986 { "f2", offsetof(CPUState, fpr[2]) },
1987 { "f3", offsetof(CPUState, fpr[3]) },
1988 { "f4", offsetof(CPUState, fpr[4]) },
1989 { "f5", offsetof(CPUState, fpr[5]) },
1990 { "f6", offsetof(CPUState, fpr[6]) },
1991 { "f7", offsetof(CPUState, fpr[7]) },
1992 { "f8", offsetof(CPUState, fpr[8]) },
1993 { "f9", offsetof(CPUState, fpr[9]) },
1994 { "f10", offsetof(CPUState, fpr[10]) },
1995 { "f11", offsetof(CPUState, fpr[11]) },
1996 { "f12", offsetof(CPUState, fpr[12]) },
1997 { "f13", offsetof(CPUState, fpr[13]) },
1998 { "f14", offsetof(CPUState, fpr[14]) },
1999 { "f15", offsetof(CPUState, fpr[15]) },
2000 { "f16", offsetof(CPUState, fpr[16]) },
2001 { "f17", offsetof(CPUState, fpr[17]) },
2002 { "f18", offsetof(CPUState, fpr[18]) },
2003 { "f19", offsetof(CPUState, fpr[19]) },
2004 { "f20", offsetof(CPUState, fpr[20]) },
2005 { "f21", offsetof(CPUState, fpr[21]) },
2006 { "f22", offsetof(CPUState, fpr[22]) },
2007 { "f23", offsetof(CPUState, fpr[23]) },
2008 { "f24", offsetof(CPUState, fpr[24]) },
2009 { "f25", offsetof(CPUState, fpr[25]) },
2010 { "f26", offsetof(CPUState, fpr[26]) },
2011 { "f27", offsetof(CPUState, fpr[27]) },
2012 { "f28", offsetof(CPUState, fpr[28]) },
2013 { "f29", offsetof(CPUState, fpr[29]) },
2014 { "f30", offsetof(CPUState, fpr[30]) },
2015 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002016#ifdef TARGET_SPARC64
2017 { "f32", offsetof(CPUState, fpr[32]) },
2018 { "f34", offsetof(CPUState, fpr[34]) },
2019 { "f36", offsetof(CPUState, fpr[36]) },
2020 { "f38", offsetof(CPUState, fpr[38]) },
2021 { "f40", offsetof(CPUState, fpr[40]) },
2022 { "f42", offsetof(CPUState, fpr[42]) },
2023 { "f44", offsetof(CPUState, fpr[44]) },
2024 { "f46", offsetof(CPUState, fpr[46]) },
2025 { "f48", offsetof(CPUState, fpr[48]) },
2026 { "f50", offsetof(CPUState, fpr[50]) },
2027 { "f52", offsetof(CPUState, fpr[52]) },
2028 { "f54", offsetof(CPUState, fpr[54]) },
2029 { "f56", offsetof(CPUState, fpr[56]) },
2030 { "f58", offsetof(CPUState, fpr[58]) },
2031 { "f60", offsetof(CPUState, fpr[60]) },
2032 { "f62", offsetof(CPUState, fpr[62]) },
2033 { "asi", offsetof(CPUState, asi) },
2034 { "pstate", offsetof(CPUState, pstate) },
2035 { "cansave", offsetof(CPUState, cansave) },
2036 { "canrestore", offsetof(CPUState, canrestore) },
2037 { "otherwin", offsetof(CPUState, otherwin) },
2038 { "wstate", offsetof(CPUState, wstate) },
2039 { "cleanwin", offsetof(CPUState, cleanwin) },
2040 { "fprs", offsetof(CPUState, fprs) },
2041#endif
bellard9307c4c2004-04-04 12:57:25 +00002042#endif
2043 { NULL },
2044};
2045
aliguori376253e2009-03-05 23:01:23 +00002046static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002047{
aliguori376253e2009-03-05 23:01:23 +00002048 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002049 longjmp(expr_env, 1);
2050}
2051
bellard6a00d602005-11-21 23:25:50 +00002052/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002053static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002054{
blueswir18662d652008-10-02 18:32:44 +00002055 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002056 void *ptr;
2057
bellard9307c4c2004-04-04 12:57:25 +00002058 for(md = monitor_defs; md->name != NULL; md++) {
2059 if (compare_cmd(name, md->name)) {
2060 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002061 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002062 } else {
bellard6a00d602005-11-21 23:25:50 +00002063 CPUState *env = mon_get_cpu();
2064 if (!env)
2065 return -2;
2066 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002067 switch(md->type) {
2068 case MD_I32:
2069 *pval = *(int32_t *)ptr;
2070 break;
2071 case MD_TLONG:
2072 *pval = *(target_long *)ptr;
2073 break;
2074 default:
2075 *pval = 0;
2076 break;
2077 }
bellard9307c4c2004-04-04 12:57:25 +00002078 }
2079 return 0;
2080 }
2081 }
2082 return -1;
2083}
2084
2085static void next(void)
2086{
2087 if (pch != '\0') {
2088 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002089 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002090 pch++;
2091 }
2092}
2093
aliguori376253e2009-03-05 23:01:23 +00002094static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002095
aliguori376253e2009-03-05 23:01:23 +00002096static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002097{
blueswir1c2efc952007-09-25 17:28:42 +00002098 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002099 char *p;
bellard6a00d602005-11-21 23:25:50 +00002100 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002101
2102 switch(*pch) {
2103 case '+':
2104 next();
aliguori376253e2009-03-05 23:01:23 +00002105 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002106 break;
2107 case '-':
2108 next();
aliguori376253e2009-03-05 23:01:23 +00002109 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002110 break;
2111 case '~':
2112 next();
aliguori376253e2009-03-05 23:01:23 +00002113 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002114 break;
2115 case '(':
2116 next();
aliguori376253e2009-03-05 23:01:23 +00002117 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002118 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002119 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002120 }
2121 next();
2122 break;
bellard81d09122004-07-14 17:21:37 +00002123 case '\'':
2124 pch++;
2125 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002126 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002127 n = *pch;
2128 pch++;
2129 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002130 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002131 next();
2132 break;
bellard9307c4c2004-04-04 12:57:25 +00002133 case '$':
2134 {
2135 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002136 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002137
bellard9307c4c2004-04-04 12:57:25 +00002138 pch++;
2139 q = buf;
2140 while ((*pch >= 'a' && *pch <= 'z') ||
2141 (*pch >= 'A' && *pch <= 'Z') ||
2142 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002143 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002144 if ((q - buf) < sizeof(buf) - 1)
2145 *q++ = *pch;
2146 pch++;
2147 }
blueswir1cd390082008-11-16 13:53:32 +00002148 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002149 pch++;
2150 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002151 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002152 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002153 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002154 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002155 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002156 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002157 }
2158 break;
2159 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002160 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002161 n = 0;
2162 break;
2163 default:
blueswir17743e582007-09-24 18:39:04 +00002164#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002165 n = strtoull(pch, &p, 0);
2166#else
bellard9307c4c2004-04-04 12:57:25 +00002167 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002168#endif
bellard9307c4c2004-04-04 12:57:25 +00002169 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002170 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002171 }
2172 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002173 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002174 pch++;
2175 break;
2176 }
2177 return n;
2178}
2179
2180
aliguori376253e2009-03-05 23:01:23 +00002181static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002182{
blueswir1c2efc952007-09-25 17:28:42 +00002183 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002184 int op;
ths3b46e622007-09-17 08:09:54 +00002185
aliguori376253e2009-03-05 23:01:23 +00002186 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002187 for(;;) {
2188 op = *pch;
2189 if (op != '*' && op != '/' && op != '%')
2190 break;
2191 next();
aliguori376253e2009-03-05 23:01:23 +00002192 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002193 switch(op) {
2194 default:
2195 case '*':
2196 val *= val2;
2197 break;
2198 case '/':
2199 case '%':
ths5fafdf22007-09-16 21:08:06 +00002200 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002201 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002202 if (op == '/')
2203 val /= val2;
2204 else
2205 val %= val2;
2206 break;
2207 }
2208 }
2209 return val;
2210}
2211
aliguori376253e2009-03-05 23:01:23 +00002212static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002213{
blueswir1c2efc952007-09-25 17:28:42 +00002214 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002215 int op;
bellard9307c4c2004-04-04 12:57:25 +00002216
aliguori376253e2009-03-05 23:01:23 +00002217 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002218 for(;;) {
2219 op = *pch;
2220 if (op != '&' && op != '|' && op != '^')
2221 break;
2222 next();
aliguori376253e2009-03-05 23:01:23 +00002223 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002224 switch(op) {
2225 default:
2226 case '&':
2227 val &= val2;
2228 break;
2229 case '|':
2230 val |= val2;
2231 break;
2232 case '^':
2233 val ^= val2;
2234 break;
2235 }
2236 }
2237 return val;
2238}
2239
aliguori376253e2009-03-05 23:01:23 +00002240static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002241{
blueswir1c2efc952007-09-25 17:28:42 +00002242 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002243 int op;
bellard9307c4c2004-04-04 12:57:25 +00002244
aliguori376253e2009-03-05 23:01:23 +00002245 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002246 for(;;) {
2247 op = *pch;
2248 if (op != '+' && op != '-')
2249 break;
2250 next();
aliguori376253e2009-03-05 23:01:23 +00002251 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002252 if (op == '+')
2253 val += val2;
2254 else
2255 val -= val2;
2256 }
2257 return val;
2258}
2259
aliguori376253e2009-03-05 23:01:23 +00002260static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002261{
2262 pch = *pp;
2263 if (setjmp(expr_env)) {
2264 *pp = pch;
2265 return -1;
2266 }
blueswir1cd390082008-11-16 13:53:32 +00002267 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002268 pch++;
aliguori376253e2009-03-05 23:01:23 +00002269 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002270 *pp = pch;
2271 return 0;
2272}
2273
2274static int get_str(char *buf, int buf_size, const char **pp)
2275{
2276 const char *p;
2277 char *q;
2278 int c;
2279
bellard81d09122004-07-14 17:21:37 +00002280 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002281 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002282 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002283 p++;
2284 if (*p == '\0') {
2285 fail:
bellard81d09122004-07-14 17:21:37 +00002286 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002287 *pp = p;
2288 return -1;
2289 }
bellard9307c4c2004-04-04 12:57:25 +00002290 if (*p == '\"') {
2291 p++;
2292 while (*p != '\0' && *p != '\"') {
2293 if (*p == '\\') {
2294 p++;
2295 c = *p++;
2296 switch(c) {
2297 case 'n':
2298 c = '\n';
2299 break;
2300 case 'r':
2301 c = '\r';
2302 break;
2303 case '\\':
2304 case '\'':
2305 case '\"':
2306 break;
2307 default:
2308 qemu_printf("unsupported escape code: '\\%c'\n", c);
2309 goto fail;
2310 }
2311 if ((q - buf) < buf_size - 1) {
2312 *q++ = c;
2313 }
2314 } else {
2315 if ((q - buf) < buf_size - 1) {
2316 *q++ = *p;
2317 }
2318 p++;
2319 }
2320 }
2321 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002322 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002323 goto fail;
2324 }
2325 p++;
2326 } else {
blueswir1cd390082008-11-16 13:53:32 +00002327 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002328 if ((q - buf) < buf_size - 1) {
2329 *q++ = *p;
2330 }
2331 p++;
2332 }
bellard9307c4c2004-04-04 12:57:25 +00002333 }
bellard81d09122004-07-14 17:21:37 +00002334 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002335 *pp = p;
2336 return 0;
2337}
2338
2339static int default_fmt_format = 'x';
2340static int default_fmt_size = 4;
2341
2342#define MAX_ARGS 16
2343
aliguori376253e2009-03-05 23:01:23 +00002344static void monitor_handle_command(Monitor *mon, const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002345{
2346 const char *p, *pstart, *typestr;
2347 char *q;
2348 int c, nb_args, len, i, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002349 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002350 char cmdname[256];
2351 char buf[1024];
2352 void *str_allocated[MAX_ARGS];
2353 void *args[MAX_ARGS];
aliguori376253e2009-03-05 23:01:23 +00002354 void (*handler_0)(Monitor *mon);
2355 void (*handler_1)(Monitor *mon, void *arg0);
2356 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2357 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2358 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2359 void *arg3);
2360 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2361 void *arg3, void *arg4);
2362 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2363 void *arg3, void *arg4, void *arg5);
2364 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2365 void *arg3, void *arg4, void *arg5, void *arg6);
bellard9dc39cb2004-03-14 21:38:27 +00002366
2367#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002368 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002369#endif
ths3b46e622007-09-17 08:09:54 +00002370
bellard9307c4c2004-04-04 12:57:25 +00002371 /* extract the command name */
bellard9dc39cb2004-03-14 21:38:27 +00002372 p = cmdline;
bellard9307c4c2004-04-04 12:57:25 +00002373 q = cmdname;
blueswir1cd390082008-11-16 13:53:32 +00002374 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002375 p++;
2376 if (*p == '\0')
bellard9dc39cb2004-03-14 21:38:27 +00002377 return;
bellard9307c4c2004-04-04 12:57:25 +00002378 pstart = p;
blueswir1cd390082008-11-16 13:53:32 +00002379 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002380 p++;
2381 len = p - pstart;
2382 if (len > sizeof(cmdname) - 1)
2383 len = sizeof(cmdname) - 1;
2384 memcpy(cmdname, pstart, len);
2385 cmdname[len] = '\0';
ths3b46e622007-09-17 08:09:54 +00002386
bellard9307c4c2004-04-04 12:57:25 +00002387 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002388 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002389 if (compare_cmd(cmdname, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +00002390 goto found;
2391 }
aliguori376253e2009-03-05 23:01:23 +00002392 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
bellard9dc39cb2004-03-14 21:38:27 +00002393 return;
2394 found:
bellard9307c4c2004-04-04 12:57:25 +00002395
2396 for(i = 0; i < MAX_ARGS; i++)
2397 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002398
bellard9307c4c2004-04-04 12:57:25 +00002399 /* parse the parameters */
2400 typestr = cmd->args_type;
2401 nb_args = 0;
2402 for(;;) {
2403 c = *typestr;
2404 if (c == '\0')
2405 break;
2406 typestr++;
2407 switch(c) {
2408 case 'F':
bellard81d09122004-07-14 17:21:37 +00002409 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002410 case 's':
2411 {
2412 int ret;
2413 char *str;
ths3b46e622007-09-17 08:09:54 +00002414
blueswir1cd390082008-11-16 13:53:32 +00002415 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002416 p++;
2417 if (*typestr == '?') {
2418 typestr++;
2419 if (*p == '\0') {
2420 /* no optional string: NULL argument */
2421 str = NULL;
2422 goto add_str;
2423 }
2424 }
2425 ret = get_str(buf, sizeof(buf), &p);
2426 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002427 switch(c) {
2428 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002429 monitor_printf(mon, "%s: filename expected\n",
2430 cmdname);
bellard81d09122004-07-14 17:21:37 +00002431 break;
2432 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002433 monitor_printf(mon, "%s: block device name expected\n",
2434 cmdname);
bellard81d09122004-07-14 17:21:37 +00002435 break;
2436 default:
aliguori376253e2009-03-05 23:01:23 +00002437 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002438 break;
2439 }
bellard9307c4c2004-04-04 12:57:25 +00002440 goto fail;
2441 }
2442 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002443 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002444 str_allocated[nb_args] = str;
2445 add_str:
2446 if (nb_args >= MAX_ARGS) {
2447 error_args:
aliguori376253e2009-03-05 23:01:23 +00002448 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002449 goto fail;
2450 }
2451 args[nb_args++] = str;
2452 }
2453 break;
2454 case '/':
2455 {
2456 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002457
blueswir1cd390082008-11-16 13:53:32 +00002458 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002459 p++;
2460 if (*p == '/') {
2461 /* format found */
2462 p++;
2463 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002464 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002465 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002466 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002467 count = count * 10 + (*p - '0');
2468 p++;
2469 }
2470 }
2471 size = -1;
2472 format = -1;
2473 for(;;) {
2474 switch(*p) {
2475 case 'o':
2476 case 'd':
2477 case 'u':
2478 case 'x':
2479 case 'i':
2480 case 'c':
2481 format = *p++;
2482 break;
2483 case 'b':
2484 size = 1;
2485 p++;
2486 break;
2487 case 'h':
2488 size = 2;
2489 p++;
2490 break;
2491 case 'w':
2492 size = 4;
2493 p++;
2494 break;
2495 case 'g':
2496 case 'L':
2497 size = 8;
2498 p++;
2499 break;
2500 default:
2501 goto next;
2502 }
2503 }
2504 next:
blueswir1cd390082008-11-16 13:53:32 +00002505 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002506 monitor_printf(mon, "invalid char in format: '%c'\n",
2507 *p);
bellard9307c4c2004-04-04 12:57:25 +00002508 goto fail;
2509 }
bellard9307c4c2004-04-04 12:57:25 +00002510 if (format < 0)
2511 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002512 if (format != 'i') {
2513 /* for 'i', not specifying a size gives -1 as size */
2514 if (size < 0)
2515 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002516 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002517 }
bellard9307c4c2004-04-04 12:57:25 +00002518 default_fmt_format = format;
2519 } else {
2520 count = 1;
2521 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002522 if (format != 'i') {
2523 size = default_fmt_size;
2524 } else {
2525 size = -1;
2526 }
bellard9307c4c2004-04-04 12:57:25 +00002527 }
2528 if (nb_args + 3 > MAX_ARGS)
2529 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002530 args[nb_args++] = (void*)(long)count;
2531 args[nb_args++] = (void*)(long)format;
2532 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002533 }
2534 break;
2535 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002536 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002537 {
blueswir1c2efc952007-09-25 17:28:42 +00002538 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002539
blueswir1cd390082008-11-16 13:53:32 +00002540 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002541 p++;
bellard34405572004-06-08 00:55:58 +00002542 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002543 if (*typestr == '?') {
2544 if (*p == '\0')
2545 has_arg = 0;
2546 else
2547 has_arg = 1;
2548 } else {
2549 if (*p == '.') {
2550 p++;
blueswir1cd390082008-11-16 13:53:32 +00002551 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002552 p++;
2553 has_arg = 1;
2554 } else {
2555 has_arg = 0;
2556 }
2557 }
bellard13224a82006-07-14 22:03:35 +00002558 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002559 if (nb_args >= MAX_ARGS)
2560 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002561 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002562 if (!has_arg) {
2563 if (nb_args >= MAX_ARGS)
2564 goto error_args;
2565 val = -1;
2566 goto add_num;
2567 }
2568 }
aliguori376253e2009-03-05 23:01:23 +00002569 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002570 goto fail;
2571 add_num:
bellard92a31b12005-02-10 22:00:52 +00002572 if (c == 'i') {
2573 if (nb_args >= MAX_ARGS)
2574 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002575 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002576 } else {
2577 if ((nb_args + 1) >= MAX_ARGS)
2578 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002579#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002580 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002581#else
2582 args[nb_args++] = (void *)0;
2583#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002584 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002585 }
bellard9307c4c2004-04-04 12:57:25 +00002586 }
2587 break;
2588 case '-':
2589 {
2590 int has_option;
2591 /* option */
ths3b46e622007-09-17 08:09:54 +00002592
bellard9307c4c2004-04-04 12:57:25 +00002593 c = *typestr++;
2594 if (c == '\0')
2595 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002596 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002597 p++;
2598 has_option = 0;
2599 if (*p == '-') {
2600 p++;
2601 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002602 monitor_printf(mon, "%s: unsupported option -%c\n",
2603 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002604 goto fail;
2605 }
2606 p++;
2607 has_option = 1;
2608 }
2609 if (nb_args >= MAX_ARGS)
2610 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002611 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002612 }
2613 break;
2614 default:
2615 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002616 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002617 goto fail;
2618 }
2619 }
2620 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002621 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002622 p++;
2623 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002624 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2625 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002626 goto fail;
2627 }
2628
2629 switch(nb_args) {
2630 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002631 handler_0 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002632 handler_0(mon);
bellard9307c4c2004-04-04 12:57:25 +00002633 break;
2634 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002635 handler_1 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002636 handler_1(mon, args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002637 break;
2638 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002639 handler_2 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002640 handler_2(mon, args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002641 break;
2642 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002643 handler_3 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002644 handler_3(mon, args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002645 break;
2646 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002647 handler_4 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002648 handler_4(mon, args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002649 break;
2650 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002651 handler_5 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002652 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002653 break;
bellard34405572004-06-08 00:55:58 +00002654 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002655 handler_6 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002656 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002657 break;
bellardec36b692006-07-16 18:57:03 +00002658 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002659 handler_7 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002660 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2661 args[6]);
bellardec36b692006-07-16 18:57:03 +00002662 break;
bellard9307c4c2004-04-04 12:57:25 +00002663 default:
aliguori376253e2009-03-05 23:01:23 +00002664 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
bellard9307c4c2004-04-04 12:57:25 +00002665 goto fail;
2666 }
2667 fail:
2668 for(i = 0; i < MAX_ARGS; i++)
2669 qemu_free(str_allocated[i]);
2670 return;
bellard9dc39cb2004-03-14 21:38:27 +00002671}
2672
bellard81d09122004-07-14 17:21:37 +00002673static void cmd_completion(const char *name, const char *list)
2674{
2675 const char *p, *pstart;
2676 char cmd[128];
2677 int len;
2678
2679 p = list;
2680 for(;;) {
2681 pstart = p;
2682 p = strchr(p, '|');
2683 if (!p)
2684 p = pstart + strlen(pstart);
2685 len = p - pstart;
2686 if (len > sizeof(cmd) - 2)
2687 len = sizeof(cmd) - 2;
2688 memcpy(cmd, pstart, len);
2689 cmd[len] = '\0';
2690 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori4c36ba32009-03-05 23:01:37 +00002691 readline_add_completion(rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002692 }
2693 if (*p == '\0')
2694 break;
2695 p++;
2696 }
2697}
2698
2699static void file_completion(const char *input)
2700{
2701 DIR *ffs;
2702 struct dirent *d;
2703 char path[1024];
2704 char file[1024], file_prefix[1024];
2705 int input_path_len;
2706 const char *p;
2707
ths5fafdf22007-09-16 21:08:06 +00002708 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002709 if (!p) {
2710 input_path_len = 0;
2711 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002712 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002713 } else {
2714 input_path_len = p - input + 1;
2715 memcpy(path, input, input_path_len);
2716 if (input_path_len > sizeof(path) - 1)
2717 input_path_len = sizeof(path) - 1;
2718 path[input_path_len] = '\0';
2719 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2720 }
2721#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002722 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2723 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002724#endif
2725 ffs = opendir(path);
2726 if (!ffs)
2727 return;
2728 for(;;) {
2729 struct stat sb;
2730 d = readdir(ffs);
2731 if (!d)
2732 break;
2733 if (strstart(d->d_name, file_prefix, NULL)) {
2734 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002735 if (input_path_len < sizeof(file))
2736 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2737 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002738 /* stat the file to find out if it's a directory.
2739 * In that case add a slash to speed up typing long paths
2740 */
2741 stat(file, &sb);
2742 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002743 pstrcat(file, sizeof(file), "/");
aliguori4c36ba32009-03-05 23:01:37 +00002744 readline_add_completion(rs, file);
bellard81d09122004-07-14 17:21:37 +00002745 }
2746 }
2747 closedir(ffs);
2748}
2749
aliguori51de9762009-03-05 23:00:43 +00002750static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002751{
aliguori51de9762009-03-05 23:00:43 +00002752 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002753 const char *input = opaque;
2754
2755 if (input[0] == '\0' ||
2756 !strncmp(name, (char *)input, strlen(input))) {
aliguori4c36ba32009-03-05 23:01:37 +00002757 readline_add_completion(rs, name);
bellard81d09122004-07-14 17:21:37 +00002758 }
2759}
2760
2761/* NOTE: this parser is an approximate form of the real command parser */
2762static void parse_cmdline(const char *cmdline,
2763 int *pnb_args, char **args)
2764{
2765 const char *p;
2766 int nb_args, ret;
2767 char buf[1024];
2768
2769 p = cmdline;
2770 nb_args = 0;
2771 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002772 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002773 p++;
2774 if (*p == '\0')
2775 break;
2776 if (nb_args >= MAX_ARGS)
2777 break;
2778 ret = get_str(buf, sizeof(buf), &p);
2779 args[nb_args] = qemu_strdup(buf);
2780 nb_args++;
2781 if (ret < 0)
2782 break;
2783 }
2784 *pnb_args = nb_args;
2785}
2786
aliguori4c36ba32009-03-05 23:01:37 +00002787static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002788{
2789 const char *cmdname;
2790 char *args[MAX_ARGS];
2791 int nb_args, i, len;
2792 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00002793 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002794 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002795
2796 parse_cmdline(cmdline, &nb_args, args);
2797#ifdef DEBUG_COMPLETION
2798 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00002799 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00002800 }
2801#endif
2802
2803 /* if the line ends with a space, it means we want to complete the
2804 next arg */
2805 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002806 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002807 if (nb_args >= MAX_ARGS)
2808 return;
2809 args[nb_args++] = qemu_strdup("");
2810 }
2811 if (nb_args <= 1) {
2812 /* command completion */
2813 if (nb_args == 0)
2814 cmdname = "";
2815 else
2816 cmdname = args[0];
aliguori4c36ba32009-03-05 23:01:37 +00002817 readline_set_completion_index(rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00002818 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002819 cmd_completion(cmdname, cmd->name);
2820 }
2821 } else {
2822 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002823 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002824 if (compare_cmd(args[0], cmd->name))
2825 goto found;
2826 }
2827 return;
2828 found:
2829 ptype = cmd->args_type;
2830 for(i = 0; i < nb_args - 2; i++) {
2831 if (*ptype != '\0') {
2832 ptype++;
2833 while (*ptype == '?')
2834 ptype++;
2835 }
2836 }
2837 str = args[nb_args - 1];
2838 switch(*ptype) {
2839 case 'F':
2840 /* file completion */
aliguori4c36ba32009-03-05 23:01:37 +00002841 readline_set_completion_index(rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00002842 file_completion(str);
2843 break;
2844 case 'B':
2845 /* block device name completion */
aliguori4c36ba32009-03-05 23:01:37 +00002846 readline_set_completion_index(rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00002847 bdrv_iterate(block_completion_it, (void *)str);
2848 break;
bellard7fe48482004-10-09 18:08:01 +00002849 case 's':
2850 /* XXX: more generic ? */
2851 if (!strcmp(cmd->name, "info")) {
aliguori4c36ba32009-03-05 23:01:37 +00002852 readline_set_completion_index(rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00002853 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2854 cmd_completion(str, cmd->name);
2855 }
bellard64866c32006-05-07 18:03:31 +00002856 } else if (!strcmp(cmd->name, "sendkey")) {
aliguori4c36ba32009-03-05 23:01:37 +00002857 readline_set_completion_index(rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00002858 for(key = key_defs; key->name != NULL; key++) {
2859 cmd_completion(str, key->name);
2860 }
bellard7fe48482004-10-09 18:08:01 +00002861 }
2862 break;
bellard81d09122004-07-14 17:21:37 +00002863 default:
2864 break;
2865 }
2866 }
2867 for(i = 0; i < nb_args; i++)
2868 qemu_free(args[i]);
2869}
2870
bellard9dc39cb2004-03-14 21:38:27 +00002871static int term_can_read(void *opaque)
2872{
bellard81d09122004-07-14 17:21:37 +00002873 return 128;
bellard9dc39cb2004-03-14 21:38:27 +00002874}
2875
2876static void term_read(void *opaque, const uint8_t *buf, int size)
2877{
2878 int i;
aliguori376253e2009-03-05 23:01:23 +00002879
aliguori4c36ba32009-03-05 23:01:37 +00002880 for(i = 0; i < size; i++)
2881 readline_handle_byte(rs, buf[i]);
bellard7e2515e2004-08-01 21:52:19 +00002882}
2883
aliguorid8f44602008-10-06 13:52:44 +00002884static int monitor_suspended;
2885
aliguori376253e2009-03-05 23:01:23 +00002886static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00002887{
aliguori376253e2009-03-05 23:01:23 +00002888 monitor_handle_command(mon, cmdline);
aliguorid8f44602008-10-06 13:52:44 +00002889 if (!monitor_suspended)
aliguori4c36ba32009-03-05 23:01:37 +00002890 readline_show_prompt(rs);
aliguorid8f44602008-10-06 13:52:44 +00002891 else
2892 monitor_suspended = 2;
2893}
2894
aliguori376253e2009-03-05 23:01:23 +00002895void monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00002896{
2897 monitor_suspended = 1;
2898}
2899
aliguori376253e2009-03-05 23:01:23 +00002900void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00002901{
2902 if (monitor_suspended == 2)
2903 monitor_start_input();
2904 monitor_suspended = 0;
bellard7e2515e2004-08-01 21:52:19 +00002905}
2906
aliguori83ab7952008-08-19 14:44:22 +00002907static void monitor_start_input(void)
bellard7e2515e2004-08-01 21:52:19 +00002908{
aliguori4c36ba32009-03-05 23:01:37 +00002909 readline_start(rs, "(qemu) ", 0, monitor_command_cb, NULL);
2910 readline_show_prompt(rs);
bellard9dc39cb2004-03-14 21:38:27 +00002911}
2912
ths86e94de2007-01-05 22:01:59 +00002913static void term_event(void *opaque, int event)
2914{
aliguori376253e2009-03-05 23:01:23 +00002915 Monitor *mon = opaque;
2916
ths86e94de2007-01-05 22:01:59 +00002917 if (event != CHR_EVENT_RESET)
2918 return;
2919
aliguoribb806042009-03-05 23:01:33 +00002920 monitor_printf(mon, "QEMU %s monitor - type 'help' for more information\n",
2921 QEMU_VERSION);
ths86e94de2007-01-05 22:01:59 +00002922 monitor_start_input();
2923}
2924
ths20d8a3e2007-02-18 17:04:49 +00002925static int is_first_init = 1;
2926
aliguoribb806042009-03-05 23:01:33 +00002927void monitor_init(CharDriverState *chr)
bellard9dc39cb2004-03-14 21:38:27 +00002928{
aliguori87127162009-03-05 23:01:29 +00002929 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00002930
2931 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00002932 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00002933 is_first_init = 0;
2934 }
aliguori87127162009-03-05 23:01:29 +00002935
2936 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00002937
aliguori87127162009-03-05 23:01:29 +00002938 mon->chr = chr;
aliguori4c36ba32009-03-05 23:01:37 +00002939 rs = readline_init(mon, monitor_find_completion);
aliguori87127162009-03-05 23:01:29 +00002940
2941 qemu_chr_add_handlers(chr, term_can_read, term_read, term_event, mon);
2942
2943 LIST_INSERT_HEAD(&mon_list, mon, entry);
2944 if (!cur_mon)
2945 cur_mon = mon;
aliguori83ab7952008-08-19 14:44:22 +00002946
aliguori4c36ba32009-03-05 23:01:37 +00002947 readline_start(rs, "", 0, monitor_command_cb, NULL);
bellard7e2515e2004-08-01 21:52:19 +00002948}
2949
aliguori376253e2009-03-05 23:01:23 +00002950static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00002951{
aliguoribb5fc202009-03-05 23:01:15 +00002952 BlockDriverState *bs = opaque;
2953 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00002954
aliguoribb5fc202009-03-05 23:01:15 +00002955 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00002956 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00002957 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00002958 }
aliguoribb5fc202009-03-05 23:01:15 +00002959 if (password_completion_cb)
2960 password_completion_cb(password_opaque, ret);
2961
2962 monitor_start_input();
bellard9dc39cb2004-03-14 21:38:27 +00002963}
aliguoric0f4ce72009-03-05 23:01:01 +00002964
aliguori376253e2009-03-05 23:01:23 +00002965void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00002966 BlockDriverCompletionFunc *completion_cb,
2967 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00002968{
aliguoribb5fc202009-03-05 23:01:15 +00002969 if (!bdrv_key_required(bs)) {
2970 if (completion_cb)
2971 completion_cb(opaque, 0);
2972 return;
2973 }
aliguoric0f4ce72009-03-05 23:01:01 +00002974
aliguori376253e2009-03-05 23:01:23 +00002975 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
2976 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00002977
2978 password_completion_cb = completion_cb;
2979 password_opaque = opaque;
2980
aliguori376253e2009-03-05 23:01:23 +00002981 monitor_read_password(mon, bdrv_password_cb, bs);
aliguoric0f4ce72009-03-05 23:01:01 +00002982}