blob: b184973863288f97e6cc94c08562985dfee6480e [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
ths20d8a3e2007-02-18 17:04:49 +000070#define MAX_MON 4
71static CharDriverState *monitor_hd[MAX_MON];
ths86e94de2007-01-05 22:01:59 +000072static int hide_banner;
bellard7e2515e2004-08-01 21:52:19 +000073
aliguori376253e2009-03-05 23:01:23 +000074static const mon_cmd_t mon_cmds[];
75static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +000076
ths60fe76f2007-12-16 03:02:09 +000077static uint8_t term_outbuf[1024];
bellard7e2515e2004-08-01 21:52:19 +000078static int term_outbuf_index;
aliguoribb5fc202009-03-05 23:01:15 +000079static BlockDriverCompletionFunc *password_completion_cb;
80static void *password_opaque;
bellard81d09122004-07-14 17:21:37 +000081
aliguori376253e2009-03-05 23:01:23 +000082Monitor *cur_mon;
83
aliguori83ab7952008-08-19 14:44:22 +000084static void monitor_start_input(void);
85
blueswir1b1d8e522008-10-26 13:43:07 +000086static CPUState *mon_cpu = NULL;
bellard6a00d602005-11-21 23:25:50 +000087
aliguori376253e2009-03-05 23:01:23 +000088static void monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
89 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +000090{
91 readline_start("Password: ", 1, readline_func, opaque);
92}
93
aliguori376253e2009-03-05 23:01:23 +000094void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +000095{
ths20d8a3e2007-02-18 17:04:49 +000096 int i;
bellard7e2515e2004-08-01 21:52:19 +000097 if (term_outbuf_index > 0) {
ths20d8a3e2007-02-18 17:04:49 +000098 for (i = 0; i < MAX_MON; i++)
99 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
100 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
bellard7e2515e2004-08-01 21:52:19 +0000101 term_outbuf_index = 0;
102 }
103}
104
105/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000106static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000107{
ths60fe76f2007-12-16 03:02:09 +0000108 char c;
bellard7e2515e2004-08-01 21:52:19 +0000109 for(;;) {
110 c = *str++;
111 if (c == '\0')
112 break;
bellard7ba12602006-07-14 20:26:42 +0000113 if (c == '\n')
114 term_outbuf[term_outbuf_index++] = '\r';
bellard7e2515e2004-08-01 21:52:19 +0000115 term_outbuf[term_outbuf_index++] = c;
bellard7ba12602006-07-14 20:26:42 +0000116 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
bellard7e2515e2004-08-01 21:52:19 +0000117 c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000118 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000119 }
120}
121
aliguori376253e2009-03-05 23:01:23 +0000122void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000123{
124 char buf[4096];
125 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000126 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000127}
128
aliguori376253e2009-03-05 23:01:23 +0000129void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000130{
131 va_list ap;
132 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000133 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000134 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000135}
136
aliguori376253e2009-03-05 23:01:23 +0000137void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000138{
139 int i;
140
141 for (i = 0; filename[i]; i++) {
142 switch (filename[i]) {
143 case ' ':
144 case '"':
145 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000146 monitor_printf(mon, "\\%c", filename[i]);
thsfef30742006-12-22 14:11:32 +0000147 break;
148 case '\t':
aliguori376253e2009-03-05 23:01:23 +0000149 monitor_printf(mon, "\\t");
thsfef30742006-12-22 14:11:32 +0000150 break;
151 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000152 monitor_printf(mon, "\\r");
thsfef30742006-12-22 14:11:32 +0000153 break;
154 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000155 monitor_printf(mon, "\\n");
thsfef30742006-12-22 14:11:32 +0000156 break;
157 default:
aliguori376253e2009-03-05 23:01:23 +0000158 monitor_printf(mon, "%c", filename[i]);
thsfef30742006-12-22 14:11:32 +0000159 break;
160 }
161 }
162}
163
bellard7fe48482004-10-09 18:08:01 +0000164static int monitor_fprintf(FILE *stream, const char *fmt, ...)
165{
166 va_list ap;
167 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000168 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000169 va_end(ap);
170 return 0;
171}
172
bellard9dc39cb2004-03-14 21:38:27 +0000173static int compare_cmd(const char *name, const char *list)
174{
175 const char *p, *pstart;
176 int len;
177 len = strlen(name);
178 p = list;
179 for(;;) {
180 pstart = p;
181 p = strchr(p, '|');
182 if (!p)
183 p = pstart + strlen(pstart);
184 if ((p - pstart) == len && !memcmp(pstart, name, len))
185 return 1;
186 if (*p == '\0')
187 break;
188 p++;
189 }
190 return 0;
191}
192
aliguori376253e2009-03-05 23:01:23 +0000193static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
194 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000195{
aliguori376253e2009-03-05 23:01:23 +0000196 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000197
198 for(cmd = cmds; cmd->name != NULL; cmd++) {
199 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000200 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
201 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000202 }
203}
204
aliguori376253e2009-03-05 23:01:23 +0000205static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000206{
207 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000208 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000209 } else {
aliguori376253e2009-03-05 23:01:23 +0000210 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000211 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000212 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000213 monitor_printf(mon, "Log items (comma separated):\n");
214 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000215 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000216 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000217 }
218 }
bellard9dc39cb2004-03-14 21:38:27 +0000219 }
220}
221
aliguori376253e2009-03-05 23:01:23 +0000222static void do_commit(Monitor *mon, const char *device)
bellard9dc39cb2004-03-14 21:38:27 +0000223{
bellard7954c732006-08-01 15:52:40 +0000224 int i, all_devices;
balrog2dc7b602007-05-24 18:53:22 +0000225
bellard7954c732006-08-01 15:52:40 +0000226 all_devices = !strcmp(device, "all");
thse4bcb142007-12-02 04:51:10 +0000227 for (i = 0; i < nb_drives; i++) {
ths5fafdf22007-09-16 21:08:06 +0000228 if (all_devices ||
thse4bcb142007-12-02 04:51:10 +0000229 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
230 bdrv_commit(drives_table[i].bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000231 }
232}
233
aliguori376253e2009-03-05 23:01:23 +0000234static void do_info(Monitor *mon, const char *item)
bellard9dc39cb2004-03-14 21:38:27 +0000235{
aliguori376253e2009-03-05 23:01:23 +0000236 const mon_cmd_t *cmd;
237 void (*handler)(Monitor *);
bellard9dc39cb2004-03-14 21:38:27 +0000238
bellard9307c4c2004-04-04 12:57:25 +0000239 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000240 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000241 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000242 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000243 goto found;
244 }
245 help:
aliguori376253e2009-03-05 23:01:23 +0000246 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000247 return;
248 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000249 handler = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +0000250 handler(mon);
bellard9dc39cb2004-03-14 21:38:27 +0000251}
252
aliguori376253e2009-03-05 23:01:23 +0000253static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000254{
aliguori376253e2009-03-05 23:01:23 +0000255 monitor_printf(mon, "%s\n", QEMU_VERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000256}
257
aliguori376253e2009-03-05 23:01:23 +0000258static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000259{
260 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000261 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000262}
263
aurel32bf4f74c2008-12-18 22:42:34 +0000264#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000265static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000266{
aliguori376253e2009-03-05 23:01:23 +0000267 monitor_printf(mon, "HPET is %s by QEMU\n",
268 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000269}
aurel32bf4f74c2008-12-18 22:42:34 +0000270#endif
aliguori16b29ae2008-12-17 23:28:44 +0000271
aliguori376253e2009-03-05 23:01:23 +0000272static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000273{
aliguori376253e2009-03-05 23:01:23 +0000274 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
275 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
276 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
277 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
278 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000279}
280
bellard6a00d602005-11-21 23:25:50 +0000281/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000282static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000283{
284 CPUState *env;
285
286 for(env = first_cpu; env != NULL; env = env->next_cpu) {
287 if (env->cpu_index == cpu_index) {
288 mon_cpu = env;
289 return 0;
290 }
291 }
292 return -1;
293}
294
pbrook9596ebb2007-11-18 01:44:38 +0000295static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000296{
297 if (!mon_cpu) {
298 mon_set_cpu(0);
299 }
300 return mon_cpu;
301}
302
aliguori376253e2009-03-05 23:01:23 +0000303static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000304{
bellard6a00d602005-11-21 23:25:50 +0000305 CPUState *env;
306 env = mon_get_cpu();
307 if (!env)
308 return;
bellard9307c4c2004-04-04 12:57:25 +0000309#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000310 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000311 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000312#else
aliguori376253e2009-03-05 23:01:23 +0000313 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000314 0);
bellard9307c4c2004-04-04 12:57:25 +0000315#endif
316}
317
aliguori376253e2009-03-05 23:01:23 +0000318static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000319{
320 CPUState *env;
321
322 /* just to set the default cpu if not already done */
323 mon_get_cpu();
324
325 for(env = first_cpu; env != NULL; env = env->next_cpu) {
aliguori376253e2009-03-05 23:01:23 +0000326 monitor_printf(mon, "%c CPU #%d:",
327 (env == mon_cpu) ? '*' : ' ',
328 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000329#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000330 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
331 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000332#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000333 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000334#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000335 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
336 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000337#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000338 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000339#endif
thsead93602007-09-06 00:18:15 +0000340 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000341 monitor_printf(mon, " (halted)");
342 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000343 }
344}
345
aliguori376253e2009-03-05 23:01:23 +0000346static void do_cpu_set(Monitor *mon, int index)
bellard6a00d602005-11-21 23:25:50 +0000347{
348 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000349 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000350}
351
aliguori376253e2009-03-05 23:01:23 +0000352static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000353{
aliguori376253e2009-03-05 23:01:23 +0000354 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000355}
356
aliguori376253e2009-03-05 23:01:23 +0000357static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000358{
359 int i;
bellard7e2515e2004-08-01 21:52:19 +0000360 const char *str;
ths3b46e622007-09-17 08:09:54 +0000361
bellard7e2515e2004-08-01 21:52:19 +0000362 i = 0;
363 for(;;) {
364 str = readline_get_history(i);
365 if (!str)
366 break;
aliguori376253e2009-03-05 23:01:23 +0000367 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000368 i++;
bellardaa455482004-04-04 13:07:25 +0000369 }
370}
371
j_mayer76a66252007-03-07 08:32:30 +0000372#if defined(TARGET_PPC)
373/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000374static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000375{
376 CPUState *env;
377
378 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000379 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000380}
381#endif
382
aliguori376253e2009-03-05 23:01:23 +0000383static void do_quit(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000384{
385 exit(0);
386}
387
aliguori376253e2009-03-05 23:01:23 +0000388static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000389{
390 if (bdrv_is_inserted(bs)) {
391 if (!force) {
392 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000393 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000394 return -1;
395 }
396 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000397 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000398 return -1;
399 }
400 }
401 bdrv_close(bs);
402 }
403 return 0;
404}
405
aliguori376253e2009-03-05 23:01:23 +0000406static void do_eject(Monitor *mon, int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000407{
408 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000409
bellard9307c4c2004-04-04 12:57:25 +0000410 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000411 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000412 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000413 return;
414 }
aliguori376253e2009-03-05 23:01:23 +0000415 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000416}
417
aliguori376253e2009-03-05 23:01:23 +0000418static void do_change_block(Monitor *mon, const char *device,
419 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000420{
421 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000422 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000423
bellard9307c4c2004-04-04 12:57:25 +0000424 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000425 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000426 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000427 return;
428 }
aurel322ecea9b2008-06-18 22:10:01 +0000429 if (fmt) {
430 drv = bdrv_find_format(fmt);
431 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000432 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000433 return;
434 }
435 }
aliguori376253e2009-03-05 23:01:23 +0000436 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000437 return;
aurel322ecea9b2008-06-18 22:10:01 +0000438 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000439 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000440}
441
aliguori376253e2009-03-05 23:01:23 +0000442static void change_vnc_password_cb(Monitor *mon, const char *password,
443 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000444{
445 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000446 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000447
448 monitor_start_input();
bellard9dc39cb2004-03-14 21:38:27 +0000449}
450
aliguori376253e2009-03-05 23:01:23 +0000451static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000452{
ths70848512007-08-25 01:37:05 +0000453 if (strcmp(target, "passwd") == 0 ||
454 strcmp(target, "password") == 0) {
aliguori2569da02008-12-10 15:14:13 +0000455 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000456 char password[9];
aliguori2569da02008-12-10 15:14:13 +0000457 strncpy(password, arg, sizeof(password));
458 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000459 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000460 } else {
aliguori376253e2009-03-05 23:01:23 +0000461 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000462 }
ths70848512007-08-25 01:37:05 +0000463 } else {
464 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000465 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000466 }
thse25a5822007-08-25 01:36:20 +0000467}
468
aliguori376253e2009-03-05 23:01:23 +0000469static void do_change(Monitor *mon, const char *device, const char *target,
470 const char *arg)
thse25a5822007-08-25 01:36:20 +0000471{
472 if (strcmp(device, "vnc") == 0) {
aliguori376253e2009-03-05 23:01:23 +0000473 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000474 } else {
aliguori376253e2009-03-05 23:01:23 +0000475 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000476 }
477}
478
aliguori376253e2009-03-05 23:01:23 +0000479static void do_screen_dump(Monitor *mon, const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000480{
pbrook95219892006-04-09 01:06:34 +0000481 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000482}
483
aliguori376253e2009-03-05 23:01:23 +0000484static void do_logfile(Monitor *mon, const char *filename)
pbrooke735b912007-06-30 13:53:24 +0000485{
486 cpu_set_log_filename(filename);
487}
488
aliguori376253e2009-03-05 23:01:23 +0000489static void do_log(Monitor *mon, const char *items)
bellardf193c792004-03-21 17:06:25 +0000490{
491 int mask;
ths3b46e622007-09-17 08:09:54 +0000492
bellard9307c4c2004-04-04 12:57:25 +0000493 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000494 mask = 0;
495 } else {
bellard9307c4c2004-04-04 12:57:25 +0000496 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000497 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000498 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000499 return;
500 }
501 }
502 cpu_set_log(mask);
503}
504
aliguori376253e2009-03-05 23:01:23 +0000505static void do_stop(Monitor *mon)
bellard8a7ddc32004-03-31 19:00:16 +0000506{
507 vm_stop(EXCP_INTERRUPT);
508}
509
aliguoribb5fc202009-03-05 23:01:15 +0000510static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000511
aliguori376253e2009-03-05 23:01:23 +0000512struct bdrv_iterate_context {
513 Monitor *mon;
514 int err;
515};
aliguoric0f4ce72009-03-05 23:01:01 +0000516
aliguori376253e2009-03-05 23:01:23 +0000517static void do_cont(Monitor *mon)
518{
519 struct bdrv_iterate_context context = { mon, 0 };
520
521 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000522 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000523 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000524 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000525}
526
aliguoribb5fc202009-03-05 23:01:15 +0000527static void bdrv_key_cb(void *opaque, int err)
528{
aliguori376253e2009-03-05 23:01:23 +0000529 Monitor *mon = opaque;
530
aliguoribb5fc202009-03-05 23:01:15 +0000531 /* another key was set successfully, retry to continue */
532 if (!err)
aliguori376253e2009-03-05 23:01:23 +0000533 do_cont(mon);
aliguoribb5fc202009-03-05 23:01:15 +0000534}
535
536static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
537{
aliguori376253e2009-03-05 23:01:23 +0000538 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000539
aliguori376253e2009-03-05 23:01:23 +0000540 if (!context->err && bdrv_key_required(bs)) {
541 context->err = -EBUSY;
542 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
543 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000544 }
545}
546
bellard67b915a2004-03-31 23:37:16 +0000547#ifdef CONFIG_GDBSTUB
aliguori376253e2009-03-05 23:01:23 +0000548static void do_gdbserver(Monitor *mon, const char *port)
bellard8a7ddc32004-03-31 19:00:16 +0000549{
pbrookcfc34752007-02-22 01:48:01 +0000550 if (!port)
bellard9307c4c2004-04-04 12:57:25 +0000551 port = DEFAULT_GDBSTUB_PORT;
pbrookcfc34752007-02-22 01:48:01 +0000552 if (gdbserver_start(port) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000553 monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",
554 port);
bellard8a7ddc32004-03-31 19:00:16 +0000555 } else {
aliguori376253e2009-03-05 23:01:23 +0000556 monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000557 }
558}
bellard67b915a2004-03-31 23:37:16 +0000559#endif
bellard8a7ddc32004-03-31 19:00:16 +0000560
aliguori376253e2009-03-05 23:01:23 +0000561static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000562{
aliguori376253e2009-03-05 23:01:23 +0000563 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000564 switch(c) {
565 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000566 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000567 break;
568 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000569 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000570 break;
571 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000572 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000573 break;
574 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000575 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000576 break;
577 default:
578 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000579 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000580 } else {
aliguori376253e2009-03-05 23:01:23 +0000581 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000582 }
583 break;
584 }
aliguori376253e2009-03-05 23:01:23 +0000585 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000586}
587
aliguori376253e2009-03-05 23:01:23 +0000588static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000589 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000590{
bellard6a00d602005-11-21 23:25:50 +0000591 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000592 int nb_per_line, l, line_size, i, max_digits, len;
593 uint8_t buf[16];
594 uint64_t v;
595
596 if (format == 'i') {
597 int flags;
598 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000599 env = mon_get_cpu();
600 if (!env && !is_physical)
601 return;
bellard9307c4c2004-04-04 12:57:25 +0000602#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000603 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000604 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000605 } else if (wsize == 4) {
606 flags = 0;
607 } else {
bellard6a15fd12006-04-12 21:07:07 +0000608 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000609 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000610 if (env) {
611#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000612 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000613 (env->segs[R_CS].flags & DESC_L_MASK))
614 flags = 2;
615 else
616#endif
617 if (!(env->segs[R_CS].flags & DESC_B_MASK))
618 flags = 1;
619 }
bellard4c27ba22004-04-25 18:05:08 +0000620 }
621#endif
aliguori376253e2009-03-05 23:01:23 +0000622 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000623 return;
624 }
625
626 len = wsize * count;
627 if (wsize == 1)
628 line_size = 8;
629 else
630 line_size = 16;
631 nb_per_line = line_size / wsize;
632 max_digits = 0;
633
634 switch(format) {
635 case 'o':
636 max_digits = (wsize * 8 + 2) / 3;
637 break;
638 default:
639 case 'x':
640 max_digits = (wsize * 8) / 4;
641 break;
642 case 'u':
643 case 'd':
644 max_digits = (wsize * 8 * 10 + 32) / 33;
645 break;
646 case 'c':
647 wsize = 1;
648 break;
649 }
650
651 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000652 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000653 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000654 else
aliguori376253e2009-03-05 23:01:23 +0000655 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000656 l = len;
657 if (l > line_size)
658 l = line_size;
659 if (is_physical) {
660 cpu_physical_memory_rw(addr, buf, l, 0);
661 } else {
bellard6a00d602005-11-21 23:25:50 +0000662 env = mon_get_cpu();
663 if (!env)
664 break;
aliguoric8f79b62008-08-18 14:00:20 +0000665 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000666 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000667 break;
668 }
bellard9307c4c2004-04-04 12:57:25 +0000669 }
ths5fafdf22007-09-16 21:08:06 +0000670 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000671 while (i < l) {
672 switch(wsize) {
673 default:
674 case 1:
675 v = ldub_raw(buf + i);
676 break;
677 case 2:
678 v = lduw_raw(buf + i);
679 break;
680 case 4:
bellard92a31b12005-02-10 22:00:52 +0000681 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000682 break;
683 case 8:
684 v = ldq_raw(buf + i);
685 break;
686 }
aliguori376253e2009-03-05 23:01:23 +0000687 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000688 switch(format) {
689 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000690 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000691 break;
692 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000693 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000694 break;
695 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000696 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000697 break;
698 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000699 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000700 break;
701 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000702 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000703 break;
704 }
705 i += wsize;
706 }
aliguori376253e2009-03-05 23:01:23 +0000707 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000708 addr += l;
709 len -= l;
710 }
711}
712
bellard92a31b12005-02-10 22:00:52 +0000713#if TARGET_LONG_BITS == 64
714#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
715#else
716#define GET_TLONG(h, l) (l)
717#endif
718
aliguori376253e2009-03-05 23:01:23 +0000719static void do_memory_dump(Monitor *mon, int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000720 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000721{
bellard92a31b12005-02-10 22:00:52 +0000722 target_long addr = GET_TLONG(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000723 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000724}
725
blueswir17743e582007-09-24 18:39:04 +0000726#if TARGET_PHYS_ADDR_BITS > 32
727#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
728#else
729#define GET_TPHYSADDR(h, l) (l)
730#endif
731
aliguori376253e2009-03-05 23:01:23 +0000732static void do_physical_memory_dump(Monitor *mon, int count, int format,
733 int size, uint32_t addrh, uint32_t addrl)
bellard92a31b12005-02-10 22:00:52 +0000734
bellard9307c4c2004-04-04 12:57:25 +0000735{
blueswir17743e582007-09-24 18:39:04 +0000736 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000737 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000738}
739
aliguori376253e2009-03-05 23:01:23 +0000740static void do_print(Monitor *mon, int count, int format, int size,
741 unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000742{
blueswir17743e582007-09-24 18:39:04 +0000743 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
744#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000745 switch(format) {
746 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000747 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000748 break;
749 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000750 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000751 break;
752 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000753 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000754 break;
755 default:
756 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000757 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000758 break;
759 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000760 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000761 break;
762 }
bellard92a31b12005-02-10 22:00:52 +0000763#else
764 switch(format) {
765 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000766 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000767 break;
768 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000769 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000770 break;
771 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000772 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000773 break;
774 default:
775 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000776 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000777 break;
778 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000779 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000780 break;
781 }
782#endif
aliguori376253e2009-03-05 23:01:23 +0000783 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000784}
785
aliguori376253e2009-03-05 23:01:23 +0000786static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000787 uint32_t size, const char *filename)
788{
789 FILE *f;
790 target_long addr = GET_TLONG(valh, vall);
791 uint32_t l;
792 CPUState *env;
793 uint8_t buf[1024];
794
795 env = mon_get_cpu();
796 if (!env)
797 return;
798
799 f = fopen(filename, "wb");
800 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000801 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000802 return;
803 }
804 while (size != 0) {
805 l = sizeof(buf);
806 if (l > size)
807 l = size;
808 cpu_memory_rw_debug(env, addr, buf, l, 0);
809 fwrite(buf, 1, l, f);
810 addr += l;
811 size -= l;
812 }
813 fclose(f);
814}
815
aliguori376253e2009-03-05 23:01:23 +0000816static void do_physical_memory_save(Monitor *mon, unsigned int valh,
817 unsigned int vall, uint32_t size,
818 const char *filename)
aurel32a8bdf7a2008-04-11 21:36:14 +0000819{
820 FILE *f;
821 uint32_t l;
822 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000823 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000824
825 f = fopen(filename, "wb");
826 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000827 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000828 return;
829 }
830 while (size != 0) {
831 l = sizeof(buf);
832 if (l > size)
833 l = size;
834 cpu_physical_memory_rw(addr, buf, l, 0);
835 fwrite(buf, 1, l, f);
836 fflush(f);
837 addr += l;
838 size -= l;
839 }
840 fclose(f);
841}
842
aliguori376253e2009-03-05 23:01:23 +0000843static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
bellarde4cf1ad2005-06-04 20:15:57 +0000844{
845 uint32_t addr;
846 uint8_t buf[1];
847 uint16_t sum;
848
849 sum = 0;
850 for(addr = start; addr < (start + size); addr++) {
851 cpu_physical_memory_rw(addr, buf, 1, 0);
852 /* BSD sum algorithm ('sum' Unix command) */
853 sum = (sum >> 1) | (sum << 15);
854 sum += buf[0];
855 }
aliguori376253e2009-03-05 23:01:23 +0000856 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000857}
858
bellarda3a91a32004-06-04 11:06:21 +0000859typedef struct {
860 int keycode;
861 const char *name;
862} KeyDef;
863
864static const KeyDef key_defs[] = {
865 { 0x2a, "shift" },
866 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000867
bellarda3a91a32004-06-04 11:06:21 +0000868 { 0x38, "alt" },
869 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000870 { 0x64, "altgr" },
871 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000872 { 0x1d, "ctrl" },
873 { 0x9d, "ctrl_r" },
874
875 { 0xdd, "menu" },
876
877 { 0x01, "esc" },
878
879 { 0x02, "1" },
880 { 0x03, "2" },
881 { 0x04, "3" },
882 { 0x05, "4" },
883 { 0x06, "5" },
884 { 0x07, "6" },
885 { 0x08, "7" },
886 { 0x09, "8" },
887 { 0x0a, "9" },
888 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000889 { 0x0c, "minus" },
890 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000891 { 0x0e, "backspace" },
892
893 { 0x0f, "tab" },
894 { 0x10, "q" },
895 { 0x11, "w" },
896 { 0x12, "e" },
897 { 0x13, "r" },
898 { 0x14, "t" },
899 { 0x15, "y" },
900 { 0x16, "u" },
901 { 0x17, "i" },
902 { 0x18, "o" },
903 { 0x19, "p" },
904
905 { 0x1c, "ret" },
906
907 { 0x1e, "a" },
908 { 0x1f, "s" },
909 { 0x20, "d" },
910 { 0x21, "f" },
911 { 0x22, "g" },
912 { 0x23, "h" },
913 { 0x24, "j" },
914 { 0x25, "k" },
915 { 0x26, "l" },
916
917 { 0x2c, "z" },
918 { 0x2d, "x" },
919 { 0x2e, "c" },
920 { 0x2f, "v" },
921 { 0x30, "b" },
922 { 0x31, "n" },
923 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000924 { 0x33, "comma" },
925 { 0x34, "dot" },
926 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000927
balrog4d3b6f62008-02-10 16:33:14 +0000928 { 0x37, "asterisk" },
929
bellarda3a91a32004-06-04 11:06:21 +0000930 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000931 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000932 { 0x3b, "f1" },
933 { 0x3c, "f2" },
934 { 0x3d, "f3" },
935 { 0x3e, "f4" },
936 { 0x3f, "f5" },
937 { 0x40, "f6" },
938 { 0x41, "f7" },
939 { 0x42, "f8" },
940 { 0x43, "f9" },
941 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +0000942 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000943 { 0x46, "scroll_lock" },
944
bellard64866c32006-05-07 18:03:31 +0000945 { 0xb5, "kp_divide" },
946 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +0000947 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +0000948 { 0x4e, "kp_add" },
949 { 0x9c, "kp_enter" },
950 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +0000951 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +0000952
953 { 0x52, "kp_0" },
954 { 0x4f, "kp_1" },
955 { 0x50, "kp_2" },
956 { 0x51, "kp_3" },
957 { 0x4b, "kp_4" },
958 { 0x4c, "kp_5" },
959 { 0x4d, "kp_6" },
960 { 0x47, "kp_7" },
961 { 0x48, "kp_8" },
962 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +0000963
bellarda3a91a32004-06-04 11:06:21 +0000964 { 0x56, "<" },
965
966 { 0x57, "f11" },
967 { 0x58, "f12" },
968
969 { 0xb7, "print" },
970
971 { 0xc7, "home" },
972 { 0xc9, "pgup" },
973 { 0xd1, "pgdn" },
974 { 0xcf, "end" },
975
976 { 0xcb, "left" },
977 { 0xc8, "up" },
978 { 0xd0, "down" },
979 { 0xcd, "right" },
980
981 { 0xd2, "insert" },
982 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +0000983#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
984 { 0xf0, "stop" },
985 { 0xf1, "again" },
986 { 0xf2, "props" },
987 { 0xf3, "undo" },
988 { 0xf4, "front" },
989 { 0xf5, "copy" },
990 { 0xf6, "open" },
991 { 0xf7, "paste" },
992 { 0xf8, "find" },
993 { 0xf9, "cut" },
994 { 0xfa, "lf" },
995 { 0xfb, "help" },
996 { 0xfc, "meta_l" },
997 { 0xfd, "meta_r" },
998 { 0xfe, "compose" },
999#endif
bellarda3a91a32004-06-04 11:06:21 +00001000 { 0, NULL },
1001};
1002
1003static int get_keycode(const char *key)
1004{
1005 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001006 char *endp;
1007 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001008
1009 for(p = key_defs; p->name != NULL; p++) {
1010 if (!strcmp(key, p->name))
1011 return p->keycode;
1012 }
bellard64866c32006-05-07 18:03:31 +00001013 if (strstart(key, "0x", NULL)) {
1014 ret = strtoul(key, &endp, 0);
1015 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1016 return ret;
1017 }
bellarda3a91a32004-06-04 11:06:21 +00001018 return -1;
1019}
1020
balrogc8256f92008-06-08 22:45:01 +00001021#define MAX_KEYCODES 16
1022static uint8_t keycodes[MAX_KEYCODES];
1023static int nb_pending_keycodes;
1024static QEMUTimer *key_timer;
1025
1026static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001027{
balrogc8256f92008-06-08 22:45:01 +00001028 int keycode;
1029
1030 while (nb_pending_keycodes > 0) {
1031 nb_pending_keycodes--;
1032 keycode = keycodes[nb_pending_keycodes];
1033 if (keycode & 0x80)
1034 kbd_put_keycode(0xe0);
1035 kbd_put_keycode(keycode | 0x80);
1036 }
1037}
1038
aliguori376253e2009-03-05 23:01:23 +00001039static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1040 int hold_time)
balrogc8256f92008-06-08 22:45:01 +00001041{
balrog3401c0d2008-06-04 10:05:59 +00001042 char keyname_buf[16];
1043 char *separator;
1044 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +00001045
balrogc8256f92008-06-08 22:45:01 +00001046 if (nb_pending_keycodes > 0) {
1047 qemu_del_timer(key_timer);
1048 release_keys(NULL);
1049 }
1050 if (!has_hold_time)
1051 hold_time = 100;
1052 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001053 while (1) {
1054 separator = strchr(string, '-');
1055 keyname_len = separator ? separator - string : strlen(string);
1056 if (keyname_len > 0) {
1057 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1058 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001059 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001060 return;
bellarda3a91a32004-06-04 11:06:21 +00001061 }
balrogc8256f92008-06-08 22:45:01 +00001062 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001063 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001064 return;
1065 }
1066 keyname_buf[keyname_len] = 0;
1067 keycode = get_keycode(keyname_buf);
1068 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001069 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001070 return;
1071 }
balrogc8256f92008-06-08 22:45:01 +00001072 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001073 }
balrog3401c0d2008-06-04 10:05:59 +00001074 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001075 break;
balrog3401c0d2008-06-04 10:05:59 +00001076 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001077 }
balrogc8256f92008-06-08 22:45:01 +00001078 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001079 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001080 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001081 keycode = keycodes[i];
1082 if (keycode & 0x80)
1083 kbd_put_keycode(0xe0);
1084 kbd_put_keycode(keycode & 0x7f);
1085 }
balrogc8256f92008-06-08 22:45:01 +00001086 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001087 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1088 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001089}
1090
bellard13224a82006-07-14 22:03:35 +00001091static int mouse_button_state;
1092
aliguori376253e2009-03-05 23:01:23 +00001093static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001094 const char *dz_str)
1095{
1096 int dx, dy, dz;
1097 dx = strtol(dx_str, NULL, 0);
1098 dy = strtol(dy_str, NULL, 0);
1099 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001100 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001101 dz = strtol(dz_str, NULL, 0);
1102 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1103}
1104
aliguori376253e2009-03-05 23:01:23 +00001105static void do_mouse_button(Monitor *mon, int button_state)
bellard13224a82006-07-14 22:03:35 +00001106{
1107 mouse_button_state = button_state;
1108 kbd_mouse_event(0, 0, 0, mouse_button_state);
1109}
1110
aliguori376253e2009-03-05 23:01:23 +00001111static void do_ioport_read(Monitor *mon, int count, int format, int size,
1112 int addr, int has_index, int index)
bellard34405572004-06-08 00:55:58 +00001113{
1114 uint32_t val;
1115 int suffix;
1116
1117 if (has_index) {
1118 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1119 addr++;
1120 }
1121 addr &= 0xffff;
1122
1123 switch(size) {
1124 default:
1125 case 1:
1126 val = cpu_inb(NULL, addr);
1127 suffix = 'b';
1128 break;
1129 case 2:
1130 val = cpu_inw(NULL, addr);
1131 suffix = 'w';
1132 break;
1133 case 4:
1134 val = cpu_inl(NULL, addr);
1135 suffix = 'l';
1136 break;
1137 }
aliguori376253e2009-03-05 23:01:23 +00001138 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1139 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001140}
bellarda3a91a32004-06-04 11:06:21 +00001141
blueswir13b4366d2008-06-20 16:25:06 +00001142/* boot_set handler */
1143static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1144static void *boot_opaque;
1145
1146void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1147{
1148 qemu_boot_set_handler = func;
1149 boot_opaque = opaque;
1150}
1151
aliguori376253e2009-03-05 23:01:23 +00001152static void do_boot_set(Monitor *mon, const char *bootdevice)
aurel320ecdffb2008-05-04 20:11:34 +00001153{
1154 int res;
1155
1156 if (qemu_boot_set_handler) {
blueswir13b4366d2008-06-20 16:25:06 +00001157 res = qemu_boot_set_handler(boot_opaque, bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001158 if (res == 0)
aliguori376253e2009-03-05 23:01:23 +00001159 monitor_printf(mon, "boot device list now set to %s\n",
1160 bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001161 else
aliguori376253e2009-03-05 23:01:23 +00001162 monitor_printf(mon, "setting boot device list failed with "
1163 "error %i\n", res);
aurel320ecdffb2008-05-04 20:11:34 +00001164 } else {
aliguori376253e2009-03-05 23:01:23 +00001165 monitor_printf(mon, "no function defined to set boot device list for "
1166 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001167 }
1168}
1169
aliguori376253e2009-03-05 23:01:23 +00001170static void do_system_reset(Monitor *mon)
bellarde4f90822004-06-20 12:35:44 +00001171{
1172 qemu_system_reset_request();
1173}
1174
aliguori376253e2009-03-05 23:01:23 +00001175static void do_system_powerdown(Monitor *mon)
bellard34751872005-07-02 14:31:34 +00001176{
1177 qemu_system_powerdown_request();
1178}
1179
bellardb86bda52004-09-18 19:32:46 +00001180#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001181static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001182{
aliguori376253e2009-03-05 23:01:23 +00001183 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1184 addr,
1185 pte & mask,
1186 pte & PG_GLOBAL_MASK ? 'G' : '-',
1187 pte & PG_PSE_MASK ? 'P' : '-',
1188 pte & PG_DIRTY_MASK ? 'D' : '-',
1189 pte & PG_ACCESSED_MASK ? 'A' : '-',
1190 pte & PG_PCD_MASK ? 'C' : '-',
1191 pte & PG_PWT_MASK ? 'T' : '-',
1192 pte & PG_USER_MASK ? 'U' : '-',
1193 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001194}
1195
aliguori376253e2009-03-05 23:01:23 +00001196static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001197{
bellard6a00d602005-11-21 23:25:50 +00001198 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001199 int l1, l2;
1200 uint32_t pgd, pde, pte;
1201
bellard6a00d602005-11-21 23:25:50 +00001202 env = mon_get_cpu();
1203 if (!env)
1204 return;
1205
bellardb86bda52004-09-18 19:32:46 +00001206 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001207 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001208 return;
1209 }
1210 pgd = env->cr[3] & ~0xfff;
1211 for(l1 = 0; l1 < 1024; l1++) {
1212 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1213 pde = le32_to_cpu(pde);
1214 if (pde & PG_PRESENT_MASK) {
1215 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001216 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001217 } else {
1218 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001219 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001220 (uint8_t *)&pte, 4);
1221 pte = le32_to_cpu(pte);
1222 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001223 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001224 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001225 ~0xfff);
1226 }
1227 }
1228 }
1229 }
1230 }
1231}
1232
aliguori376253e2009-03-05 23:01:23 +00001233static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001234 uint32_t end, int prot)
1235{
bellard9746b152004-11-11 18:30:24 +00001236 int prot1;
1237 prot1 = *plast_prot;
1238 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001239 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001240 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1241 *pstart, end, end - *pstart,
1242 prot1 & PG_USER_MASK ? 'u' : '-',
1243 'r',
1244 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001245 }
1246 if (prot != 0)
1247 *pstart = end;
1248 else
1249 *pstart = -1;
1250 *plast_prot = prot;
1251 }
1252}
1253
aliguori376253e2009-03-05 23:01:23 +00001254static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001255{
bellard6a00d602005-11-21 23:25:50 +00001256 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001257 int l1, l2, prot, last_prot;
1258 uint32_t pgd, pde, pte, start, end;
1259
bellard6a00d602005-11-21 23:25:50 +00001260 env = mon_get_cpu();
1261 if (!env)
1262 return;
1263
bellardb86bda52004-09-18 19:32:46 +00001264 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001265 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001266 return;
1267 }
1268 pgd = env->cr[3] & ~0xfff;
1269 last_prot = 0;
1270 start = -1;
1271 for(l1 = 0; l1 < 1024; l1++) {
1272 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1273 pde = le32_to_cpu(pde);
1274 end = l1 << 22;
1275 if (pde & PG_PRESENT_MASK) {
1276 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1277 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001278 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001279 } else {
1280 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001281 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001282 (uint8_t *)&pte, 4);
1283 pte = le32_to_cpu(pte);
1284 end = (l1 << 22) + (l2 << 12);
1285 if (pte & PG_PRESENT_MASK) {
1286 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1287 } else {
1288 prot = 0;
1289 }
aliguori376253e2009-03-05 23:01:23 +00001290 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001291 }
1292 }
1293 } else {
1294 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001295 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001296 }
1297 }
1298}
1299#endif
1300
aurel327c664e22009-03-03 06:12:22 +00001301#if defined(TARGET_SH4)
1302
aliguori376253e2009-03-05 23:01:23 +00001303static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001304{
aliguori376253e2009-03-05 23:01:23 +00001305 monitor_printf(mon, " tlb%i:\t"
1306 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1307 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1308 "dirty=%hhu writethrough=%hhu\n",
1309 idx,
1310 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1311 tlb->v, tlb->sh, tlb->c, tlb->pr,
1312 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001313}
1314
aliguori376253e2009-03-05 23:01:23 +00001315static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001316{
1317 CPUState *env = mon_get_cpu();
1318 int i;
1319
aliguori376253e2009-03-05 23:01:23 +00001320 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001321 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001322 print_tlb (mon, i, &env->itlb[i]);
1323 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001324 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001325 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001326}
1327
1328#endif
1329
aliguori376253e2009-03-05 23:01:23 +00001330static void do_info_kqemu(Monitor *mon)
bellard0f4c6412005-07-24 18:10:56 +00001331{
1332#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001333 CPUState *env;
bellard0f4c6412005-07-24 18:10:56 +00001334 int val;
1335 val = 0;
bellard6a00d602005-11-21 23:25:50 +00001336 env = mon_get_cpu();
1337 if (!env) {
aliguori376253e2009-03-05 23:01:23 +00001338 monitor_printf(mon, "No cpu initialized yet");
bellard6a00d602005-11-21 23:25:50 +00001339 return;
1340 }
1341 val = env->kqemu_enabled;
aliguori376253e2009-03-05 23:01:23 +00001342 monitor_printf(mon, "kqemu support: ");
bellard5f1ce942006-02-08 22:40:15 +00001343 switch(val) {
1344 default:
1345 case 0:
aliguori376253e2009-03-05 23:01:23 +00001346 monitor_printf(mon, "disabled\n");
bellard5f1ce942006-02-08 22:40:15 +00001347 break;
1348 case 1:
aliguori376253e2009-03-05 23:01:23 +00001349 monitor_printf(mon, "enabled for user code\n");
bellard5f1ce942006-02-08 22:40:15 +00001350 break;
1351 case 2:
aliguori376253e2009-03-05 23:01:23 +00001352 monitor_printf(mon, "enabled for user and kernel code\n");
bellard5f1ce942006-02-08 22:40:15 +00001353 break;
1354 }
bellard0f4c6412005-07-24 18:10:56 +00001355#else
aliguori376253e2009-03-05 23:01:23 +00001356 monitor_printf(mon, "kqemu support: not compiled\n");
bellard0f4c6412005-07-24 18:10:56 +00001357#endif
ths5fafdf22007-09-16 21:08:06 +00001358}
bellard0f4c6412005-07-24 18:10:56 +00001359
aliguori376253e2009-03-05 23:01:23 +00001360static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001361{
1362#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001363 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001364 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001365 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001366 else
aliguori376253e2009-03-05 23:01:23 +00001367 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001368#else
aliguori376253e2009-03-05 23:01:23 +00001369 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001370#endif
1371}
1372
bellard5f1ce942006-02-08 22:40:15 +00001373#ifdef CONFIG_PROFILER
1374
1375int64_t kqemu_time;
1376int64_t qemu_time;
1377int64_t kqemu_exec_count;
1378int64_t dev_time;
1379int64_t kqemu_ret_int_count;
1380int64_t kqemu_ret_excp_count;
1381int64_t kqemu_ret_intr_count;
1382
aliguori376253e2009-03-05 23:01:23 +00001383static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001384{
1385 int64_t total;
1386 total = qemu_time;
1387 if (total == 0)
1388 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001389 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1390 dev_time, dev_time / (double)ticks_per_sec);
1391 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1392 qemu_time, qemu_time / (double)ticks_per_sec);
1393 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1394 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1395 PRId64 "\n",
1396 kqemu_time, kqemu_time / (double)ticks_per_sec,
1397 kqemu_time / (double)total * 100.0,
1398 kqemu_exec_count,
1399 kqemu_ret_int_count,
1400 kqemu_ret_excp_count,
1401 kqemu_ret_intr_count);
bellard5f1ce942006-02-08 22:40:15 +00001402 qemu_time = 0;
1403 kqemu_time = 0;
1404 kqemu_exec_count = 0;
1405 dev_time = 0;
1406 kqemu_ret_int_count = 0;
1407 kqemu_ret_excp_count = 0;
1408 kqemu_ret_intr_count = 0;
1409#ifdef USE_KQEMU
1410 kqemu_record_dump();
1411#endif
1412}
1413#else
aliguori376253e2009-03-05 23:01:23 +00001414static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001415{
aliguori376253e2009-03-05 23:01:23 +00001416 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001417}
1418#endif
1419
bellardec36b692006-07-16 18:57:03 +00001420/* Capture support */
1421static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1422
aliguori376253e2009-03-05 23:01:23 +00001423static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001424{
1425 int i;
1426 CaptureState *s;
1427
1428 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001429 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001430 s->ops.info (s->opaque);
1431 }
1432}
1433
aliguori376253e2009-03-05 23:01:23 +00001434static void do_stop_capture(Monitor *mon, int n)
bellardec36b692006-07-16 18:57:03 +00001435{
1436 int i;
1437 CaptureState *s;
1438
1439 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1440 if (i == n) {
1441 s->ops.destroy (s->opaque);
1442 LIST_REMOVE (s, entries);
1443 qemu_free (s);
1444 return;
1445 }
1446 }
1447}
1448
1449#ifdef HAS_AUDIO
aliguori376253e2009-03-05 23:01:23 +00001450static void do_wav_capture(Monitor *mon, const char *path,
1451 int has_freq, int freq,
1452 int has_bits, int bits,
1453 int has_channels, int nchannels)
bellardec36b692006-07-16 18:57:03 +00001454{
1455 CaptureState *s;
1456
1457 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001458
1459 freq = has_freq ? freq : 44100;
1460 bits = has_bits ? bits : 16;
1461 nchannels = has_channels ? nchannels : 2;
1462
1463 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001464 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001465 qemu_free (s);
1466 }
1467 LIST_INSERT_HEAD (&capture_head, s, entries);
1468}
1469#endif
1470
aurel32dc1c0b72008-04-27 23:52:12 +00001471#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001472static void do_inject_nmi(Monitor *mon, int cpu_index)
aurel32dc1c0b72008-04-27 23:52:12 +00001473{
1474 CPUState *env;
1475
1476 for (env = first_cpu; env != NULL; env = env->next_cpu)
1477 if (env->cpu_index == cpu_index) {
1478 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1479 break;
1480 }
1481}
1482#endif
1483
aliguori376253e2009-03-05 23:01:23 +00001484static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001485{
1486 if (vm_running)
aliguori376253e2009-03-05 23:01:23 +00001487 monitor_printf(mon, "VM status: running\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001488 else
aliguori376253e2009-03-05 23:01:23 +00001489 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001490}
1491
1492
aliguori376253e2009-03-05 23:01:23 +00001493static void do_balloon(Monitor *mon, int value)
aliguoridf751fa2008-12-04 20:19:35 +00001494{
1495 ram_addr_t target = value;
1496 qemu_balloon(target << 20);
1497}
1498
aliguori376253e2009-03-05 23:01:23 +00001499static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001500{
1501 ram_addr_t actual;
1502
1503 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001504 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001505 monitor_printf(mon, "Using KVM without synchronous MMU, "
1506 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001507 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001508 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001509 else
aliguori376253e2009-03-05 23:01:23 +00001510 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001511}
1512
blueswir1d2c639d2009-01-24 18:19:25 +00001513/* Please update qemu-doc.texi when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001514static const mon_cmd_t mon_cmds[] = {
1515 { "help|?", "s?", help_cmd,
bellard9dc39cb2004-03-14 21:38:27 +00001516 "[cmd]", "show the help" },
ths5fafdf22007-09-16 21:08:06 +00001517 { "commit", "s", do_commit,
bellard7954c732006-08-01 15:52:40 +00001518 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
bellard9307c4c2004-04-04 12:57:25 +00001519 { "info", "s?", do_info,
bellard9dc39cb2004-03-14 21:38:27 +00001520 "subcommand", "show various information about the system state" },
bellard9307c4c2004-04-04 12:57:25 +00001521 { "q|quit", "", do_quit,
bellard9dc39cb2004-03-14 21:38:27 +00001522 "", "quit the emulator" },
bellard81d09122004-07-14 17:21:37 +00001523 { "eject", "-fB", do_eject,
thse5987522007-03-30 18:58:01 +00001524 "[-f] device", "eject a removable medium (use -f to force it)" },
aurel322ecea9b2008-06-18 22:10:01 +00001525 { "change", "BFs?", do_change,
1526 "device filename [format]", "change a removable medium, optional format" },
ths5fafdf22007-09-16 21:08:06 +00001527 { "screendump", "F", do_screen_dump,
bellard59a983b2004-03-17 23:17:16 +00001528 "filename", "save screen into PPM image 'filename'" },
balrog788228c2008-05-24 23:15:46 +00001529 { "logfile", "F", do_logfile,
pbrooke735b912007-06-30 13:53:24 +00001530 "filename", "output logs to 'filename'" },
bellard9307c4c2004-04-04 12:57:25 +00001531 { "log", "s", do_log,
ths5fafdf22007-09-16 21:08:06 +00001532 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
bellardfaea38e2006-08-05 21:31:00 +00001533 { "savevm", "s?", do_savevm,
ths5fafdf22007-09-16 21:08:06 +00001534 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
bellardfaea38e2006-08-05 21:31:00 +00001535 { "loadvm", "s", do_loadvm,
ths5fafdf22007-09-16 21:08:06 +00001536 "tag|id", "restore a VM snapshot from its tag or id" },
bellardfaea38e2006-08-05 21:31:00 +00001537 { "delvm", "s", do_delvm,
ths5fafdf22007-09-16 21:08:06 +00001538 "tag|id", "delete a VM snapshot from its tag or id" },
1539 { "stop", "", do_stop,
bellard9307c4c2004-04-04 12:57:25 +00001540 "", "stop emulation", },
ths5fafdf22007-09-16 21:08:06 +00001541 { "c|cont", "", do_cont,
bellard9307c4c2004-04-04 12:57:25 +00001542 "", "resume emulation", },
bellard67b915a2004-03-31 23:37:16 +00001543#ifdef CONFIG_GDBSTUB
ths5fafdf22007-09-16 21:08:06 +00001544 { "gdbserver", "s?", do_gdbserver,
bellard9307c4c2004-04-04 12:57:25 +00001545 "[port]", "start gdbserver session (default port=1234)", },
bellard67b915a2004-03-31 23:37:16 +00001546#endif
ths5fafdf22007-09-16 21:08:06 +00001547 { "x", "/l", do_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001548 "/fmt addr", "virtual memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001549 { "xp", "/l", do_physical_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001550 "/fmt addr", "physical memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001551 { "p|print", "/l", do_print,
bellard9307c4c2004-04-04 12:57:25 +00001552 "/fmt expr", "print expression value (use $reg for CPU register access)", },
ths5fafdf22007-09-16 21:08:06 +00001553 { "i", "/ii.", do_ioport_read,
bellard34405572004-06-08 00:55:58 +00001554 "/fmt addr", "I/O port read" },
1555
balrogc8256f92008-06-08 22:45:01 +00001556 { "sendkey", "si?", do_sendkey,
1557 "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 +00001558 { "system_reset", "", do_system_reset,
bellarde4f90822004-06-20 12:35:44 +00001559 "", "reset the system" },
ths5fafdf22007-09-16 21:08:06 +00001560 { "system_powerdown", "", do_system_powerdown,
bellard34751872005-07-02 14:31:34 +00001561 "", "send system power down event" },
ths5fafdf22007-09-16 21:08:06 +00001562 { "sum", "ii", do_sum,
bellarde4cf1ad2005-06-04 20:15:57 +00001563 "addr size", "compute the checksum of a memory region" },
bellarda594cfb2005-11-06 16:13:29 +00001564 { "usb_add", "s", do_usb_add,
1565 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1566 { "usb_del", "s", do_usb_del,
1567 "device", "remove USB device 'bus.addr'" },
ths5fafdf22007-09-16 21:08:06 +00001568 { "cpu", "i", do_cpu_set,
bellard6a00d602005-11-21 23:25:50 +00001569 "index", "set the default CPU" },
ths5fafdf22007-09-16 21:08:06 +00001570 { "mouse_move", "sss?", do_mouse_move,
bellard13224a82006-07-14 22:03:35 +00001571 "dx dy [dz]", "send mouse move events" },
ths5fafdf22007-09-16 21:08:06 +00001572 { "mouse_button", "i", do_mouse_button,
bellard13224a82006-07-14 22:03:35 +00001573 "state", "change mouse button state (1=L, 2=M, 4=R)" },
ths455204e2007-01-05 16:42:13 +00001574 { "mouse_set", "i", do_mouse_set,
1575 "index", "set which mouse device receives events" },
bellardec36b692006-07-16 18:57:03 +00001576#ifdef HAS_AUDIO
1577 { "wavcapture", "si?i?i?", do_wav_capture,
1578 "path [frequency bits channels]",
1579 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1580#endif
blueswir1d2c639d2009-01-24 18:19:25 +00001581 { "stopcapture", "i", do_stop_capture,
1582 "capture index", "stop capture" },
ths5fafdf22007-09-16 21:08:06 +00001583 { "memsave", "lis", do_memory_save,
bellardb371dc52007-01-03 15:20:39 +00001584 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
aurel32a8bdf7a2008-04-11 21:36:14 +00001585 { "pmemsave", "lis", do_physical_memory_save,
1586 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
aurel320ecdffb2008-05-04 20:11:34 +00001587 { "boot_set", "s", do_boot_set,
1588 "bootdevice", "define new values for the boot device list" },
aurel32dc1c0b72008-04-27 23:52:12 +00001589#if defined(TARGET_I386)
1590 { "nmi", "i", do_inject_nmi,
1591 "cpu", "inject an NMI on the given CPU", },
1592#endif
aliguori5bb79102008-10-13 03:12:02 +00001593 { "migrate", "-ds", do_migrate,
1594 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1595 { "migrate_cancel", "", do_migrate_cancel,
1596 "", "cancel the current VM migration" },
1597 { "migrate_set_speed", "s", do_migrate_set_speed,
1598 "value", "set maximum speed (in bytes) for migrations" },
aliguori6f338c32009-02-11 15:21:54 +00001599#if defined(TARGET_I386)
1600 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1601 "[file=file][,if=type][,bus=n]\n"
1602 "[,unit=m][,media=d][index=i]\n"
1603 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1604 "[snapshot=on|off][,cache=on|off]",
1605 "add drive to PCI storage controller" },
1606 { "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" },
1607 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1608 { "host_net_add", "ss", net_host_device_add,
1609 "[tap,user,socket,vde] options", "add host VLAN client" },
1610 { "host_net_remove", "is", net_host_device_remove,
1611 "vlan_id name", "remove host VLAN client" },
1612#endif
aliguoridf751fa2008-12-04 20:19:35 +00001613 { "balloon", "i", do_balloon,
1614 "target", "request VM to change it's memory allocation (in MB)" },
aliguorif029bd92009-02-11 15:19:16 +00001615 { "set_link", "ss", do_set_link,
1616 "name [up|down]", "change the link status of a network adapter" },
ths5fafdf22007-09-16 21:08:06 +00001617 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001618};
1619
blueswir1d2c639d2009-01-24 18:19:25 +00001620/* Please update qemu-doc.texi when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001621static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001622 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001623 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001624 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001625 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001626 { "chardev", "", qemu_chr_info,
1627 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001628 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001629 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001630 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001631 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001632 { "registers", "", do_info_registers,
1633 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001634 { "cpus", "", do_info_cpus,
1635 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001636 { "history", "", do_info_history,
1637 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001638 { "irq", "", irq_info,
1639 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001640 { "pic", "", pic_info,
1641 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001642 { "pci", "", pci_info,
1643 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001644#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001645 { "tlb", "", tlb_info,
1646 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001647#endif
1648#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001649 { "mem", "", mem_info,
1650 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001651 { "hpet", "", do_info_hpet,
1652 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001653#endif
bellarde3db7222005-01-26 22:00:47 +00001654 { "jit", "", do_info_jit,
1655 "", "show dynamic compiler info", },
bellard0f4c6412005-07-24 18:10:56 +00001656 { "kqemu", "", do_info_kqemu,
blueswir1d2c639d2009-01-24 18:19:25 +00001657 "", "show KQEMU information", },
aliguori7ba1e612008-11-05 16:04:33 +00001658 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001659 "", "show KVM information", },
bellarda594cfb2005-11-06 16:13:29 +00001660 { "usb", "", usb_info,
1661 "", "show guest USB devices", },
1662 { "usbhost", "", usb_host_info,
1663 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001664 { "profile", "", do_info_profile,
1665 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001666 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001667 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001668 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001669 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001670 { "status", "", do_info_status,
1671 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001672 { "pcmcia", "", pcmcia_info,
1673 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001674 { "mice", "", do_info_mice,
1675 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001676 { "vnc", "", do_info_vnc,
1677 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001678 { "name", "", do_info_name,
1679 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001680 { "uuid", "", do_info_uuid,
1681 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001682#if defined(TARGET_PPC)
1683 { "cpustats", "", do_info_cpu_stats,
1684 "", "show CPU statistics", },
1685#endif
blueswir131a60e22007-10-26 18:42:59 +00001686#if defined(CONFIG_SLIRP)
1687 { "slirp", "", do_info_slirp,
1688 "", "show SLIRP statistics", },
1689#endif
aliguori5bb79102008-10-13 03:12:02 +00001690 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001691 { "balloon", "", do_info_balloon,
1692 "", "show balloon information" },
bellard9dc39cb2004-03-14 21:38:27 +00001693 { NULL, NULL, },
1694};
1695
bellard9307c4c2004-04-04 12:57:25 +00001696/*******************************************************************/
1697
1698static const char *pch;
1699static jmp_buf expr_env;
1700
bellard92a31b12005-02-10 22:00:52 +00001701#define MD_TLONG 0
1702#define MD_I32 1
1703
bellard9307c4c2004-04-04 12:57:25 +00001704typedef struct MonitorDef {
1705 const char *name;
1706 int offset;
blueswir18662d652008-10-02 18:32:44 +00001707 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001708 int type;
bellard9307c4c2004-04-04 12:57:25 +00001709} MonitorDef;
1710
bellard57206fd2004-04-25 18:54:52 +00001711#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001712static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001713{
bellard6a00d602005-11-21 23:25:50 +00001714 CPUState *env = mon_get_cpu();
1715 if (!env)
1716 return 0;
1717 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001718}
1719#endif
1720
bellarda541f292004-04-12 20:39:29 +00001721#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001722static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001723{
bellard6a00d602005-11-21 23:25:50 +00001724 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001725 unsigned int u;
1726 int i;
1727
bellard6a00d602005-11-21 23:25:50 +00001728 if (!env)
1729 return 0;
1730
bellarda541f292004-04-12 20:39:29 +00001731 u = 0;
1732 for (i = 0; i < 8; i++)
bellard6a00d602005-11-21 23:25:50 +00001733 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001734
1735 return u;
1736}
1737
blueswir18662d652008-10-02 18:32:44 +00001738static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001739{
bellard6a00d602005-11-21 23:25:50 +00001740 CPUState *env = mon_get_cpu();
1741 if (!env)
1742 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001743 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001744}
1745
blueswir18662d652008-10-02 18:32:44 +00001746static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001747{
bellard6a00d602005-11-21 23:25:50 +00001748 CPUState *env = mon_get_cpu();
1749 if (!env)
1750 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001751 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001752}
bellard9fddaa02004-05-21 12:59:32 +00001753
blueswir18662d652008-10-02 18:32:44 +00001754static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001755{
bellard6a00d602005-11-21 23:25:50 +00001756 CPUState *env = mon_get_cpu();
1757 if (!env)
1758 return 0;
1759 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001760}
1761
blueswir18662d652008-10-02 18:32:44 +00001762static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001763{
bellard6a00d602005-11-21 23:25:50 +00001764 CPUState *env = mon_get_cpu();
1765 if (!env)
1766 return 0;
1767 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001768}
1769
blueswir18662d652008-10-02 18:32:44 +00001770static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001771{
bellard6a00d602005-11-21 23:25:50 +00001772 CPUState *env = mon_get_cpu();
1773 if (!env)
1774 return 0;
1775 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001776}
bellarda541f292004-04-12 20:39:29 +00001777#endif
1778
bellarde95c8d52004-09-30 22:22:08 +00001779#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001780#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001781static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001782{
bellard6a00d602005-11-21 23:25:50 +00001783 CPUState *env = mon_get_cpu();
1784 if (!env)
1785 return 0;
1786 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001787}
bellard7b936c02005-10-30 17:05:13 +00001788#endif
bellarde95c8d52004-09-30 22:22:08 +00001789
blueswir18662d652008-10-02 18:32:44 +00001790static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001791{
bellard6a00d602005-11-21 23:25:50 +00001792 CPUState *env = mon_get_cpu();
1793 if (!env)
1794 return 0;
1795 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001796}
1797#endif
1798
blueswir18662d652008-10-02 18:32:44 +00001799static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001800#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001801
1802#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001803 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001804 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001805 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001806
bellard9307c4c2004-04-04 12:57:25 +00001807 { "eax", offsetof(CPUState, regs[0]) },
1808 { "ecx", offsetof(CPUState, regs[1]) },
1809 { "edx", offsetof(CPUState, regs[2]) },
1810 { "ebx", offsetof(CPUState, regs[3]) },
1811 { "esp|sp", offsetof(CPUState, regs[4]) },
1812 { "ebp|fp", offsetof(CPUState, regs[5]) },
1813 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001814 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001815#ifdef TARGET_X86_64
1816 { "r8", offsetof(CPUState, regs[8]) },
1817 { "r9", offsetof(CPUState, regs[9]) },
1818 { "r10", offsetof(CPUState, regs[10]) },
1819 { "r11", offsetof(CPUState, regs[11]) },
1820 { "r12", offsetof(CPUState, regs[12]) },
1821 { "r13", offsetof(CPUState, regs[13]) },
1822 { "r14", offsetof(CPUState, regs[14]) },
1823 { "r15", offsetof(CPUState, regs[15]) },
1824#endif
bellard9307c4c2004-04-04 12:57:25 +00001825 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001826 { "eip", offsetof(CPUState, eip) },
1827 SEG("cs", R_CS)
1828 SEG("ds", R_DS)
1829 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001830 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001831 SEG("fs", R_FS)
1832 SEG("gs", R_GS)
1833 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00001834#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00001835 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00001836 { "r0", offsetof(CPUState, gpr[0]) },
1837 { "r1", offsetof(CPUState, gpr[1]) },
1838 { "r2", offsetof(CPUState, gpr[2]) },
1839 { "r3", offsetof(CPUState, gpr[3]) },
1840 { "r4", offsetof(CPUState, gpr[4]) },
1841 { "r5", offsetof(CPUState, gpr[5]) },
1842 { "r6", offsetof(CPUState, gpr[6]) },
1843 { "r7", offsetof(CPUState, gpr[7]) },
1844 { "r8", offsetof(CPUState, gpr[8]) },
1845 { "r9", offsetof(CPUState, gpr[9]) },
1846 { "r10", offsetof(CPUState, gpr[10]) },
1847 { "r11", offsetof(CPUState, gpr[11]) },
1848 { "r12", offsetof(CPUState, gpr[12]) },
1849 { "r13", offsetof(CPUState, gpr[13]) },
1850 { "r14", offsetof(CPUState, gpr[14]) },
1851 { "r15", offsetof(CPUState, gpr[15]) },
1852 { "r16", offsetof(CPUState, gpr[16]) },
1853 { "r17", offsetof(CPUState, gpr[17]) },
1854 { "r18", offsetof(CPUState, gpr[18]) },
1855 { "r19", offsetof(CPUState, gpr[19]) },
1856 { "r20", offsetof(CPUState, gpr[20]) },
1857 { "r21", offsetof(CPUState, gpr[21]) },
1858 { "r22", offsetof(CPUState, gpr[22]) },
1859 { "r23", offsetof(CPUState, gpr[23]) },
1860 { "r24", offsetof(CPUState, gpr[24]) },
1861 { "r25", offsetof(CPUState, gpr[25]) },
1862 { "r26", offsetof(CPUState, gpr[26]) },
1863 { "r27", offsetof(CPUState, gpr[27]) },
1864 { "r28", offsetof(CPUState, gpr[28]) },
1865 { "r29", offsetof(CPUState, gpr[29]) },
1866 { "r30", offsetof(CPUState, gpr[30]) },
1867 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00001868 /* Floating point registers */
1869 { "f0", offsetof(CPUState, fpr[0]) },
1870 { "f1", offsetof(CPUState, fpr[1]) },
1871 { "f2", offsetof(CPUState, fpr[2]) },
1872 { "f3", offsetof(CPUState, fpr[3]) },
1873 { "f4", offsetof(CPUState, fpr[4]) },
1874 { "f5", offsetof(CPUState, fpr[5]) },
1875 { "f6", offsetof(CPUState, fpr[6]) },
1876 { "f7", offsetof(CPUState, fpr[7]) },
1877 { "f8", offsetof(CPUState, fpr[8]) },
1878 { "f9", offsetof(CPUState, fpr[9]) },
1879 { "f10", offsetof(CPUState, fpr[10]) },
1880 { "f11", offsetof(CPUState, fpr[11]) },
1881 { "f12", offsetof(CPUState, fpr[12]) },
1882 { "f13", offsetof(CPUState, fpr[13]) },
1883 { "f14", offsetof(CPUState, fpr[14]) },
1884 { "f15", offsetof(CPUState, fpr[15]) },
1885 { "f16", offsetof(CPUState, fpr[16]) },
1886 { "f17", offsetof(CPUState, fpr[17]) },
1887 { "f18", offsetof(CPUState, fpr[18]) },
1888 { "f19", offsetof(CPUState, fpr[19]) },
1889 { "f20", offsetof(CPUState, fpr[20]) },
1890 { "f21", offsetof(CPUState, fpr[21]) },
1891 { "f22", offsetof(CPUState, fpr[22]) },
1892 { "f23", offsetof(CPUState, fpr[23]) },
1893 { "f24", offsetof(CPUState, fpr[24]) },
1894 { "f25", offsetof(CPUState, fpr[25]) },
1895 { "f26", offsetof(CPUState, fpr[26]) },
1896 { "f27", offsetof(CPUState, fpr[27]) },
1897 { "f28", offsetof(CPUState, fpr[28]) },
1898 { "f29", offsetof(CPUState, fpr[29]) },
1899 { "f30", offsetof(CPUState, fpr[30]) },
1900 { "f31", offsetof(CPUState, fpr[31]) },
1901 { "fpscr", offsetof(CPUState, fpscr) },
1902 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00001903 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00001904 { "lr", offsetof(CPUState, lr) },
1905 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00001906 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00001907 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00001908 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00001909 { "msr", 0, &monitor_get_msr, },
1910 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00001911 { "tbu", 0, &monitor_get_tbu, },
1912 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00001913#if defined(TARGET_PPC64)
1914 /* Address space register */
1915 { "asr", offsetof(CPUState, asr) },
1916#endif
1917 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00001918 { "sdr1", offsetof(CPUState, sdr1) },
1919 { "sr0", offsetof(CPUState, sr[0]) },
1920 { "sr1", offsetof(CPUState, sr[1]) },
1921 { "sr2", offsetof(CPUState, sr[2]) },
1922 { "sr3", offsetof(CPUState, sr[3]) },
1923 { "sr4", offsetof(CPUState, sr[4]) },
1924 { "sr5", offsetof(CPUState, sr[5]) },
1925 { "sr6", offsetof(CPUState, sr[6]) },
1926 { "sr7", offsetof(CPUState, sr[7]) },
1927 { "sr8", offsetof(CPUState, sr[8]) },
1928 { "sr9", offsetof(CPUState, sr[9]) },
1929 { "sr10", offsetof(CPUState, sr[10]) },
1930 { "sr11", offsetof(CPUState, sr[11]) },
1931 { "sr12", offsetof(CPUState, sr[12]) },
1932 { "sr13", offsetof(CPUState, sr[13]) },
1933 { "sr14", offsetof(CPUState, sr[14]) },
1934 { "sr15", offsetof(CPUState, sr[15]) },
1935 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00001936#elif defined(TARGET_SPARC)
1937 { "g0", offsetof(CPUState, gregs[0]) },
1938 { "g1", offsetof(CPUState, gregs[1]) },
1939 { "g2", offsetof(CPUState, gregs[2]) },
1940 { "g3", offsetof(CPUState, gregs[3]) },
1941 { "g4", offsetof(CPUState, gregs[4]) },
1942 { "g5", offsetof(CPUState, gregs[5]) },
1943 { "g6", offsetof(CPUState, gregs[6]) },
1944 { "g7", offsetof(CPUState, gregs[7]) },
1945 { "o0", 0, monitor_get_reg },
1946 { "o1", 1, monitor_get_reg },
1947 { "o2", 2, monitor_get_reg },
1948 { "o3", 3, monitor_get_reg },
1949 { "o4", 4, monitor_get_reg },
1950 { "o5", 5, monitor_get_reg },
1951 { "o6", 6, monitor_get_reg },
1952 { "o7", 7, monitor_get_reg },
1953 { "l0", 8, monitor_get_reg },
1954 { "l1", 9, monitor_get_reg },
1955 { "l2", 10, monitor_get_reg },
1956 { "l3", 11, monitor_get_reg },
1957 { "l4", 12, monitor_get_reg },
1958 { "l5", 13, monitor_get_reg },
1959 { "l6", 14, monitor_get_reg },
1960 { "l7", 15, monitor_get_reg },
1961 { "i0", 16, monitor_get_reg },
1962 { "i1", 17, monitor_get_reg },
1963 { "i2", 18, monitor_get_reg },
1964 { "i3", 19, monitor_get_reg },
1965 { "i4", 20, monitor_get_reg },
1966 { "i5", 21, monitor_get_reg },
1967 { "i6", 22, monitor_get_reg },
1968 { "i7", 23, monitor_get_reg },
1969 { "pc", offsetof(CPUState, pc) },
1970 { "npc", offsetof(CPUState, npc) },
1971 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00001972#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00001973 { "psr", 0, &monitor_get_psr, },
1974 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00001975#endif
bellarde95c8d52004-09-30 22:22:08 +00001976 { "tbr", offsetof(CPUState, tbr) },
1977 { "fsr", offsetof(CPUState, fsr) },
1978 { "f0", offsetof(CPUState, fpr[0]) },
1979 { "f1", offsetof(CPUState, fpr[1]) },
1980 { "f2", offsetof(CPUState, fpr[2]) },
1981 { "f3", offsetof(CPUState, fpr[3]) },
1982 { "f4", offsetof(CPUState, fpr[4]) },
1983 { "f5", offsetof(CPUState, fpr[5]) },
1984 { "f6", offsetof(CPUState, fpr[6]) },
1985 { "f7", offsetof(CPUState, fpr[7]) },
1986 { "f8", offsetof(CPUState, fpr[8]) },
1987 { "f9", offsetof(CPUState, fpr[9]) },
1988 { "f10", offsetof(CPUState, fpr[10]) },
1989 { "f11", offsetof(CPUState, fpr[11]) },
1990 { "f12", offsetof(CPUState, fpr[12]) },
1991 { "f13", offsetof(CPUState, fpr[13]) },
1992 { "f14", offsetof(CPUState, fpr[14]) },
1993 { "f15", offsetof(CPUState, fpr[15]) },
1994 { "f16", offsetof(CPUState, fpr[16]) },
1995 { "f17", offsetof(CPUState, fpr[17]) },
1996 { "f18", offsetof(CPUState, fpr[18]) },
1997 { "f19", offsetof(CPUState, fpr[19]) },
1998 { "f20", offsetof(CPUState, fpr[20]) },
1999 { "f21", offsetof(CPUState, fpr[21]) },
2000 { "f22", offsetof(CPUState, fpr[22]) },
2001 { "f23", offsetof(CPUState, fpr[23]) },
2002 { "f24", offsetof(CPUState, fpr[24]) },
2003 { "f25", offsetof(CPUState, fpr[25]) },
2004 { "f26", offsetof(CPUState, fpr[26]) },
2005 { "f27", offsetof(CPUState, fpr[27]) },
2006 { "f28", offsetof(CPUState, fpr[28]) },
2007 { "f29", offsetof(CPUState, fpr[29]) },
2008 { "f30", offsetof(CPUState, fpr[30]) },
2009 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002010#ifdef TARGET_SPARC64
2011 { "f32", offsetof(CPUState, fpr[32]) },
2012 { "f34", offsetof(CPUState, fpr[34]) },
2013 { "f36", offsetof(CPUState, fpr[36]) },
2014 { "f38", offsetof(CPUState, fpr[38]) },
2015 { "f40", offsetof(CPUState, fpr[40]) },
2016 { "f42", offsetof(CPUState, fpr[42]) },
2017 { "f44", offsetof(CPUState, fpr[44]) },
2018 { "f46", offsetof(CPUState, fpr[46]) },
2019 { "f48", offsetof(CPUState, fpr[48]) },
2020 { "f50", offsetof(CPUState, fpr[50]) },
2021 { "f52", offsetof(CPUState, fpr[52]) },
2022 { "f54", offsetof(CPUState, fpr[54]) },
2023 { "f56", offsetof(CPUState, fpr[56]) },
2024 { "f58", offsetof(CPUState, fpr[58]) },
2025 { "f60", offsetof(CPUState, fpr[60]) },
2026 { "f62", offsetof(CPUState, fpr[62]) },
2027 { "asi", offsetof(CPUState, asi) },
2028 { "pstate", offsetof(CPUState, pstate) },
2029 { "cansave", offsetof(CPUState, cansave) },
2030 { "canrestore", offsetof(CPUState, canrestore) },
2031 { "otherwin", offsetof(CPUState, otherwin) },
2032 { "wstate", offsetof(CPUState, wstate) },
2033 { "cleanwin", offsetof(CPUState, cleanwin) },
2034 { "fprs", offsetof(CPUState, fprs) },
2035#endif
bellard9307c4c2004-04-04 12:57:25 +00002036#endif
2037 { NULL },
2038};
2039
aliguori376253e2009-03-05 23:01:23 +00002040static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002041{
aliguori376253e2009-03-05 23:01:23 +00002042 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002043 longjmp(expr_env, 1);
2044}
2045
bellard6a00d602005-11-21 23:25:50 +00002046/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002047static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002048{
blueswir18662d652008-10-02 18:32:44 +00002049 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002050 void *ptr;
2051
bellard9307c4c2004-04-04 12:57:25 +00002052 for(md = monitor_defs; md->name != NULL; md++) {
2053 if (compare_cmd(name, md->name)) {
2054 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002055 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002056 } else {
bellard6a00d602005-11-21 23:25:50 +00002057 CPUState *env = mon_get_cpu();
2058 if (!env)
2059 return -2;
2060 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002061 switch(md->type) {
2062 case MD_I32:
2063 *pval = *(int32_t *)ptr;
2064 break;
2065 case MD_TLONG:
2066 *pval = *(target_long *)ptr;
2067 break;
2068 default:
2069 *pval = 0;
2070 break;
2071 }
bellard9307c4c2004-04-04 12:57:25 +00002072 }
2073 return 0;
2074 }
2075 }
2076 return -1;
2077}
2078
2079static void next(void)
2080{
2081 if (pch != '\0') {
2082 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002083 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002084 pch++;
2085 }
2086}
2087
aliguori376253e2009-03-05 23:01:23 +00002088static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002089
aliguori376253e2009-03-05 23:01:23 +00002090static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002091{
blueswir1c2efc952007-09-25 17:28:42 +00002092 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002093 char *p;
bellard6a00d602005-11-21 23:25:50 +00002094 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002095
2096 switch(*pch) {
2097 case '+':
2098 next();
aliguori376253e2009-03-05 23:01:23 +00002099 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002100 break;
2101 case '-':
2102 next();
aliguori376253e2009-03-05 23:01:23 +00002103 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002104 break;
2105 case '~':
2106 next();
aliguori376253e2009-03-05 23:01:23 +00002107 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002108 break;
2109 case '(':
2110 next();
aliguori376253e2009-03-05 23:01:23 +00002111 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002112 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002113 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002114 }
2115 next();
2116 break;
bellard81d09122004-07-14 17:21:37 +00002117 case '\'':
2118 pch++;
2119 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002120 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002121 n = *pch;
2122 pch++;
2123 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002124 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002125 next();
2126 break;
bellard9307c4c2004-04-04 12:57:25 +00002127 case '$':
2128 {
2129 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002130 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002131
bellard9307c4c2004-04-04 12:57:25 +00002132 pch++;
2133 q = buf;
2134 while ((*pch >= 'a' && *pch <= 'z') ||
2135 (*pch >= 'A' && *pch <= 'Z') ||
2136 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002137 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002138 if ((q - buf) < sizeof(buf) - 1)
2139 *q++ = *pch;
2140 pch++;
2141 }
blueswir1cd390082008-11-16 13:53:32 +00002142 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002143 pch++;
2144 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002145 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002146 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002147 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002148 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002149 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002150 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002151 }
2152 break;
2153 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002154 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002155 n = 0;
2156 break;
2157 default:
blueswir17743e582007-09-24 18:39:04 +00002158#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002159 n = strtoull(pch, &p, 0);
2160#else
bellard9307c4c2004-04-04 12:57:25 +00002161 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002162#endif
bellard9307c4c2004-04-04 12:57:25 +00002163 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002164 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002165 }
2166 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002167 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002168 pch++;
2169 break;
2170 }
2171 return n;
2172}
2173
2174
aliguori376253e2009-03-05 23:01:23 +00002175static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002176{
blueswir1c2efc952007-09-25 17:28:42 +00002177 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002178 int op;
ths3b46e622007-09-17 08:09:54 +00002179
aliguori376253e2009-03-05 23:01:23 +00002180 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002181 for(;;) {
2182 op = *pch;
2183 if (op != '*' && op != '/' && op != '%')
2184 break;
2185 next();
aliguori376253e2009-03-05 23:01:23 +00002186 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002187 switch(op) {
2188 default:
2189 case '*':
2190 val *= val2;
2191 break;
2192 case '/':
2193 case '%':
ths5fafdf22007-09-16 21:08:06 +00002194 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002195 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002196 if (op == '/')
2197 val /= val2;
2198 else
2199 val %= val2;
2200 break;
2201 }
2202 }
2203 return val;
2204}
2205
aliguori376253e2009-03-05 23:01:23 +00002206static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002207{
blueswir1c2efc952007-09-25 17:28:42 +00002208 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002209 int op;
bellard9307c4c2004-04-04 12:57:25 +00002210
aliguori376253e2009-03-05 23:01:23 +00002211 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002212 for(;;) {
2213 op = *pch;
2214 if (op != '&' && op != '|' && op != '^')
2215 break;
2216 next();
aliguori376253e2009-03-05 23:01:23 +00002217 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002218 switch(op) {
2219 default:
2220 case '&':
2221 val &= val2;
2222 break;
2223 case '|':
2224 val |= val2;
2225 break;
2226 case '^':
2227 val ^= val2;
2228 break;
2229 }
2230 }
2231 return val;
2232}
2233
aliguori376253e2009-03-05 23:01:23 +00002234static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002235{
blueswir1c2efc952007-09-25 17:28:42 +00002236 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002237 int op;
bellard9307c4c2004-04-04 12:57:25 +00002238
aliguori376253e2009-03-05 23:01:23 +00002239 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002240 for(;;) {
2241 op = *pch;
2242 if (op != '+' && op != '-')
2243 break;
2244 next();
aliguori376253e2009-03-05 23:01:23 +00002245 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002246 if (op == '+')
2247 val += val2;
2248 else
2249 val -= val2;
2250 }
2251 return val;
2252}
2253
aliguori376253e2009-03-05 23:01:23 +00002254static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002255{
2256 pch = *pp;
2257 if (setjmp(expr_env)) {
2258 *pp = pch;
2259 return -1;
2260 }
blueswir1cd390082008-11-16 13:53:32 +00002261 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002262 pch++;
aliguori376253e2009-03-05 23:01:23 +00002263 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002264 *pp = pch;
2265 return 0;
2266}
2267
2268static int get_str(char *buf, int buf_size, const char **pp)
2269{
2270 const char *p;
2271 char *q;
2272 int c;
2273
bellard81d09122004-07-14 17:21:37 +00002274 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002275 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002276 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002277 p++;
2278 if (*p == '\0') {
2279 fail:
bellard81d09122004-07-14 17:21:37 +00002280 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002281 *pp = p;
2282 return -1;
2283 }
bellard9307c4c2004-04-04 12:57:25 +00002284 if (*p == '\"') {
2285 p++;
2286 while (*p != '\0' && *p != '\"') {
2287 if (*p == '\\') {
2288 p++;
2289 c = *p++;
2290 switch(c) {
2291 case 'n':
2292 c = '\n';
2293 break;
2294 case 'r':
2295 c = '\r';
2296 break;
2297 case '\\':
2298 case '\'':
2299 case '\"':
2300 break;
2301 default:
2302 qemu_printf("unsupported escape code: '\\%c'\n", c);
2303 goto fail;
2304 }
2305 if ((q - buf) < buf_size - 1) {
2306 *q++ = c;
2307 }
2308 } else {
2309 if ((q - buf) < buf_size - 1) {
2310 *q++ = *p;
2311 }
2312 p++;
2313 }
2314 }
2315 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002316 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002317 goto fail;
2318 }
2319 p++;
2320 } else {
blueswir1cd390082008-11-16 13:53:32 +00002321 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002322 if ((q - buf) < buf_size - 1) {
2323 *q++ = *p;
2324 }
2325 p++;
2326 }
bellard9307c4c2004-04-04 12:57:25 +00002327 }
bellard81d09122004-07-14 17:21:37 +00002328 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002329 *pp = p;
2330 return 0;
2331}
2332
2333static int default_fmt_format = 'x';
2334static int default_fmt_size = 4;
2335
2336#define MAX_ARGS 16
2337
aliguori376253e2009-03-05 23:01:23 +00002338static void monitor_handle_command(Monitor *mon, const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002339{
2340 const char *p, *pstart, *typestr;
2341 char *q;
2342 int c, nb_args, len, i, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002343 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002344 char cmdname[256];
2345 char buf[1024];
2346 void *str_allocated[MAX_ARGS];
2347 void *args[MAX_ARGS];
aliguori376253e2009-03-05 23:01:23 +00002348 void (*handler_0)(Monitor *mon);
2349 void (*handler_1)(Monitor *mon, void *arg0);
2350 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2351 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2352 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2353 void *arg3);
2354 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2355 void *arg3, void *arg4);
2356 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2357 void *arg3, void *arg4, void *arg5);
2358 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2359 void *arg3, void *arg4, void *arg5, void *arg6);
bellard9dc39cb2004-03-14 21:38:27 +00002360
2361#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002362 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002363#endif
ths3b46e622007-09-17 08:09:54 +00002364
bellard9307c4c2004-04-04 12:57:25 +00002365 /* extract the command name */
bellard9dc39cb2004-03-14 21:38:27 +00002366 p = cmdline;
bellard9307c4c2004-04-04 12:57:25 +00002367 q = cmdname;
blueswir1cd390082008-11-16 13:53:32 +00002368 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002369 p++;
2370 if (*p == '\0')
bellard9dc39cb2004-03-14 21:38:27 +00002371 return;
bellard9307c4c2004-04-04 12:57:25 +00002372 pstart = p;
blueswir1cd390082008-11-16 13:53:32 +00002373 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002374 p++;
2375 len = p - pstart;
2376 if (len > sizeof(cmdname) - 1)
2377 len = sizeof(cmdname) - 1;
2378 memcpy(cmdname, pstart, len);
2379 cmdname[len] = '\0';
ths3b46e622007-09-17 08:09:54 +00002380
bellard9307c4c2004-04-04 12:57:25 +00002381 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002382 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002383 if (compare_cmd(cmdname, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +00002384 goto found;
2385 }
aliguori376253e2009-03-05 23:01:23 +00002386 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
bellard9dc39cb2004-03-14 21:38:27 +00002387 return;
2388 found:
bellard9307c4c2004-04-04 12:57:25 +00002389
2390 for(i = 0; i < MAX_ARGS; i++)
2391 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002392
bellard9307c4c2004-04-04 12:57:25 +00002393 /* parse the parameters */
2394 typestr = cmd->args_type;
2395 nb_args = 0;
2396 for(;;) {
2397 c = *typestr;
2398 if (c == '\0')
2399 break;
2400 typestr++;
2401 switch(c) {
2402 case 'F':
bellard81d09122004-07-14 17:21:37 +00002403 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002404 case 's':
2405 {
2406 int ret;
2407 char *str;
ths3b46e622007-09-17 08:09:54 +00002408
blueswir1cd390082008-11-16 13:53:32 +00002409 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002410 p++;
2411 if (*typestr == '?') {
2412 typestr++;
2413 if (*p == '\0') {
2414 /* no optional string: NULL argument */
2415 str = NULL;
2416 goto add_str;
2417 }
2418 }
2419 ret = get_str(buf, sizeof(buf), &p);
2420 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002421 switch(c) {
2422 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002423 monitor_printf(mon, "%s: filename expected\n",
2424 cmdname);
bellard81d09122004-07-14 17:21:37 +00002425 break;
2426 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002427 monitor_printf(mon, "%s: block device name expected\n",
2428 cmdname);
bellard81d09122004-07-14 17:21:37 +00002429 break;
2430 default:
aliguori376253e2009-03-05 23:01:23 +00002431 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002432 break;
2433 }
bellard9307c4c2004-04-04 12:57:25 +00002434 goto fail;
2435 }
2436 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002437 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002438 str_allocated[nb_args] = str;
2439 add_str:
2440 if (nb_args >= MAX_ARGS) {
2441 error_args:
aliguori376253e2009-03-05 23:01:23 +00002442 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002443 goto fail;
2444 }
2445 args[nb_args++] = str;
2446 }
2447 break;
2448 case '/':
2449 {
2450 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002451
blueswir1cd390082008-11-16 13:53:32 +00002452 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002453 p++;
2454 if (*p == '/') {
2455 /* format found */
2456 p++;
2457 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002458 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002459 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002460 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002461 count = count * 10 + (*p - '0');
2462 p++;
2463 }
2464 }
2465 size = -1;
2466 format = -1;
2467 for(;;) {
2468 switch(*p) {
2469 case 'o':
2470 case 'd':
2471 case 'u':
2472 case 'x':
2473 case 'i':
2474 case 'c':
2475 format = *p++;
2476 break;
2477 case 'b':
2478 size = 1;
2479 p++;
2480 break;
2481 case 'h':
2482 size = 2;
2483 p++;
2484 break;
2485 case 'w':
2486 size = 4;
2487 p++;
2488 break;
2489 case 'g':
2490 case 'L':
2491 size = 8;
2492 p++;
2493 break;
2494 default:
2495 goto next;
2496 }
2497 }
2498 next:
blueswir1cd390082008-11-16 13:53:32 +00002499 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002500 monitor_printf(mon, "invalid char in format: '%c'\n",
2501 *p);
bellard9307c4c2004-04-04 12:57:25 +00002502 goto fail;
2503 }
bellard9307c4c2004-04-04 12:57:25 +00002504 if (format < 0)
2505 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002506 if (format != 'i') {
2507 /* for 'i', not specifying a size gives -1 as size */
2508 if (size < 0)
2509 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002510 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002511 }
bellard9307c4c2004-04-04 12:57:25 +00002512 default_fmt_format = format;
2513 } else {
2514 count = 1;
2515 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002516 if (format != 'i') {
2517 size = default_fmt_size;
2518 } else {
2519 size = -1;
2520 }
bellard9307c4c2004-04-04 12:57:25 +00002521 }
2522 if (nb_args + 3 > MAX_ARGS)
2523 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002524 args[nb_args++] = (void*)(long)count;
2525 args[nb_args++] = (void*)(long)format;
2526 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002527 }
2528 break;
2529 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002530 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002531 {
blueswir1c2efc952007-09-25 17:28:42 +00002532 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002533
blueswir1cd390082008-11-16 13:53:32 +00002534 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002535 p++;
bellard34405572004-06-08 00:55:58 +00002536 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002537 if (*typestr == '?') {
2538 if (*p == '\0')
2539 has_arg = 0;
2540 else
2541 has_arg = 1;
2542 } else {
2543 if (*p == '.') {
2544 p++;
blueswir1cd390082008-11-16 13:53:32 +00002545 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002546 p++;
2547 has_arg = 1;
2548 } else {
2549 has_arg = 0;
2550 }
2551 }
bellard13224a82006-07-14 22:03:35 +00002552 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002553 if (nb_args >= MAX_ARGS)
2554 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002555 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002556 if (!has_arg) {
2557 if (nb_args >= MAX_ARGS)
2558 goto error_args;
2559 val = -1;
2560 goto add_num;
2561 }
2562 }
aliguori376253e2009-03-05 23:01:23 +00002563 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002564 goto fail;
2565 add_num:
bellard92a31b12005-02-10 22:00:52 +00002566 if (c == 'i') {
2567 if (nb_args >= MAX_ARGS)
2568 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002569 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002570 } else {
2571 if ((nb_args + 1) >= MAX_ARGS)
2572 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002573#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002574 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002575#else
2576 args[nb_args++] = (void *)0;
2577#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002578 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002579 }
bellard9307c4c2004-04-04 12:57:25 +00002580 }
2581 break;
2582 case '-':
2583 {
2584 int has_option;
2585 /* option */
ths3b46e622007-09-17 08:09:54 +00002586
bellard9307c4c2004-04-04 12:57:25 +00002587 c = *typestr++;
2588 if (c == '\0')
2589 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002590 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002591 p++;
2592 has_option = 0;
2593 if (*p == '-') {
2594 p++;
2595 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002596 monitor_printf(mon, "%s: unsupported option -%c\n",
2597 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002598 goto fail;
2599 }
2600 p++;
2601 has_option = 1;
2602 }
2603 if (nb_args >= MAX_ARGS)
2604 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002605 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002606 }
2607 break;
2608 default:
2609 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002610 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002611 goto fail;
2612 }
2613 }
2614 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002615 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002616 p++;
2617 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002618 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2619 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002620 goto fail;
2621 }
2622
2623 switch(nb_args) {
2624 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002625 handler_0 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002626 handler_0(mon);
bellard9307c4c2004-04-04 12:57:25 +00002627 break;
2628 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002629 handler_1 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002630 handler_1(mon, args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002631 break;
2632 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002633 handler_2 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002634 handler_2(mon, args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002635 break;
2636 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002637 handler_3 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002638 handler_3(mon, args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002639 break;
2640 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002641 handler_4 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002642 handler_4(mon, args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002643 break;
2644 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002645 handler_5 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002646 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002647 break;
bellard34405572004-06-08 00:55:58 +00002648 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002649 handler_6 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002650 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002651 break;
bellardec36b692006-07-16 18:57:03 +00002652 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002653 handler_7 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002654 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2655 args[6]);
bellardec36b692006-07-16 18:57:03 +00002656 break;
bellard9307c4c2004-04-04 12:57:25 +00002657 default:
aliguori376253e2009-03-05 23:01:23 +00002658 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
bellard9307c4c2004-04-04 12:57:25 +00002659 goto fail;
2660 }
2661 fail:
2662 for(i = 0; i < MAX_ARGS; i++)
2663 qemu_free(str_allocated[i]);
2664 return;
bellard9dc39cb2004-03-14 21:38:27 +00002665}
2666
bellard81d09122004-07-14 17:21:37 +00002667static void cmd_completion(const char *name, const char *list)
2668{
2669 const char *p, *pstart;
2670 char cmd[128];
2671 int len;
2672
2673 p = list;
2674 for(;;) {
2675 pstart = p;
2676 p = strchr(p, '|');
2677 if (!p)
2678 p = pstart + strlen(pstart);
2679 len = p - pstart;
2680 if (len > sizeof(cmd) - 2)
2681 len = sizeof(cmd) - 2;
2682 memcpy(cmd, pstart, len);
2683 cmd[len] = '\0';
2684 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori376253e2009-03-05 23:01:23 +00002685 readline_add_completion(cmd);
bellard81d09122004-07-14 17:21:37 +00002686 }
2687 if (*p == '\0')
2688 break;
2689 p++;
2690 }
2691}
2692
2693static void file_completion(const char *input)
2694{
2695 DIR *ffs;
2696 struct dirent *d;
2697 char path[1024];
2698 char file[1024], file_prefix[1024];
2699 int input_path_len;
2700 const char *p;
2701
ths5fafdf22007-09-16 21:08:06 +00002702 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002703 if (!p) {
2704 input_path_len = 0;
2705 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002706 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002707 } else {
2708 input_path_len = p - input + 1;
2709 memcpy(path, input, input_path_len);
2710 if (input_path_len > sizeof(path) - 1)
2711 input_path_len = sizeof(path) - 1;
2712 path[input_path_len] = '\0';
2713 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2714 }
2715#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002716 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2717 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002718#endif
2719 ffs = opendir(path);
2720 if (!ffs)
2721 return;
2722 for(;;) {
2723 struct stat sb;
2724 d = readdir(ffs);
2725 if (!d)
2726 break;
2727 if (strstart(d->d_name, file_prefix, NULL)) {
2728 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002729 if (input_path_len < sizeof(file))
2730 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2731 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002732 /* stat the file to find out if it's a directory.
2733 * In that case add a slash to speed up typing long paths
2734 */
2735 stat(file, &sb);
2736 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002737 pstrcat(file, sizeof(file), "/");
aliguori376253e2009-03-05 23:01:23 +00002738 readline_add_completion(file);
bellard81d09122004-07-14 17:21:37 +00002739 }
2740 }
2741 closedir(ffs);
2742}
2743
aliguori51de9762009-03-05 23:00:43 +00002744static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002745{
aliguori51de9762009-03-05 23:00:43 +00002746 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002747 const char *input = opaque;
2748
2749 if (input[0] == '\0' ||
2750 !strncmp(name, (char *)input, strlen(input))) {
aliguori376253e2009-03-05 23:01:23 +00002751 readline_add_completion(name);
bellard81d09122004-07-14 17:21:37 +00002752 }
2753}
2754
2755/* NOTE: this parser is an approximate form of the real command parser */
2756static void parse_cmdline(const char *cmdline,
2757 int *pnb_args, char **args)
2758{
2759 const char *p;
2760 int nb_args, ret;
2761 char buf[1024];
2762
2763 p = cmdline;
2764 nb_args = 0;
2765 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002766 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002767 p++;
2768 if (*p == '\0')
2769 break;
2770 if (nb_args >= MAX_ARGS)
2771 break;
2772 ret = get_str(buf, sizeof(buf), &p);
2773 args[nb_args] = qemu_strdup(buf);
2774 nb_args++;
2775 if (ret < 0)
2776 break;
2777 }
2778 *pnb_args = nb_args;
2779}
2780
bellard7e2515e2004-08-01 21:52:19 +00002781void readline_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002782{
2783 const char *cmdname;
2784 char *args[MAX_ARGS];
2785 int nb_args, i, len;
2786 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00002787 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002788 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002789
2790 parse_cmdline(cmdline, &nb_args, args);
2791#ifdef DEBUG_COMPLETION
2792 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00002793 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00002794 }
2795#endif
2796
2797 /* if the line ends with a space, it means we want to complete the
2798 next arg */
2799 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002800 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002801 if (nb_args >= MAX_ARGS)
2802 return;
2803 args[nb_args++] = qemu_strdup("");
2804 }
2805 if (nb_args <= 1) {
2806 /* command completion */
2807 if (nb_args == 0)
2808 cmdname = "";
2809 else
2810 cmdname = args[0];
aliguori376253e2009-03-05 23:01:23 +00002811 readline_set_completion_index(strlen(cmdname));
2812 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002813 cmd_completion(cmdname, cmd->name);
2814 }
2815 } else {
2816 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002817 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002818 if (compare_cmd(args[0], cmd->name))
2819 goto found;
2820 }
2821 return;
2822 found:
2823 ptype = cmd->args_type;
2824 for(i = 0; i < nb_args - 2; i++) {
2825 if (*ptype != '\0') {
2826 ptype++;
2827 while (*ptype == '?')
2828 ptype++;
2829 }
2830 }
2831 str = args[nb_args - 1];
2832 switch(*ptype) {
2833 case 'F':
2834 /* file completion */
aliguori376253e2009-03-05 23:01:23 +00002835 readline_set_completion_index(strlen(str));
bellard81d09122004-07-14 17:21:37 +00002836 file_completion(str);
2837 break;
2838 case 'B':
2839 /* block device name completion */
aliguori376253e2009-03-05 23:01:23 +00002840 readline_set_completion_index(strlen(str));
bellard81d09122004-07-14 17:21:37 +00002841 bdrv_iterate(block_completion_it, (void *)str);
2842 break;
bellard7fe48482004-10-09 18:08:01 +00002843 case 's':
2844 /* XXX: more generic ? */
2845 if (!strcmp(cmd->name, "info")) {
aliguori376253e2009-03-05 23:01:23 +00002846 readline_set_completion_index(strlen(str));
bellard7fe48482004-10-09 18:08:01 +00002847 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2848 cmd_completion(str, cmd->name);
2849 }
bellard64866c32006-05-07 18:03:31 +00002850 } else if (!strcmp(cmd->name, "sendkey")) {
aliguori376253e2009-03-05 23:01:23 +00002851 readline_set_completion_index(strlen(str));
bellard64866c32006-05-07 18:03:31 +00002852 for(key = key_defs; key->name != NULL; key++) {
2853 cmd_completion(str, key->name);
2854 }
bellard7fe48482004-10-09 18:08:01 +00002855 }
2856 break;
bellard81d09122004-07-14 17:21:37 +00002857 default:
2858 break;
2859 }
2860 }
2861 for(i = 0; i < nb_args; i++)
2862 qemu_free(args[i]);
2863}
2864
bellard9dc39cb2004-03-14 21:38:27 +00002865static int term_can_read(void *opaque)
2866{
bellard81d09122004-07-14 17:21:37 +00002867 return 128;
bellard9dc39cb2004-03-14 21:38:27 +00002868}
2869
2870static void term_read(void *opaque, const uint8_t *buf, int size)
2871{
2872 int i;
aliguori376253e2009-03-05 23:01:23 +00002873
2874 for (i = 0; i < size; i++)
bellard7e2515e2004-08-01 21:52:19 +00002875 readline_handle_byte(buf[i]);
2876}
2877
aliguorid8f44602008-10-06 13:52:44 +00002878static int monitor_suspended;
2879
aliguori376253e2009-03-05 23:01:23 +00002880static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00002881{
aliguori376253e2009-03-05 23:01:23 +00002882 monitor_handle_command(mon, cmdline);
aliguorid8f44602008-10-06 13:52:44 +00002883 if (!monitor_suspended)
aliguoribb5fc202009-03-05 23:01:15 +00002884 readline_show_prompt();
aliguorid8f44602008-10-06 13:52:44 +00002885 else
2886 monitor_suspended = 2;
2887}
2888
aliguori376253e2009-03-05 23:01:23 +00002889void monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00002890{
2891 monitor_suspended = 1;
2892}
2893
aliguori376253e2009-03-05 23:01:23 +00002894void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00002895{
2896 if (monitor_suspended == 2)
2897 monitor_start_input();
2898 monitor_suspended = 0;
bellard7e2515e2004-08-01 21:52:19 +00002899}
2900
aliguori83ab7952008-08-19 14:44:22 +00002901static void monitor_start_input(void)
bellard7e2515e2004-08-01 21:52:19 +00002902{
aliguori376253e2009-03-05 23:01:23 +00002903 readline_start("(qemu) ", 0, monitor_command_cb, NULL);
aliguori9dd442b2009-03-05 23:01:10 +00002904 readline_show_prompt();
bellard9dc39cb2004-03-14 21:38:27 +00002905}
2906
ths86e94de2007-01-05 22:01:59 +00002907static void term_event(void *opaque, int event)
2908{
aliguori376253e2009-03-05 23:01:23 +00002909 Monitor *mon = opaque;
2910
ths86e94de2007-01-05 22:01:59 +00002911 if (event != CHR_EVENT_RESET)
2912 return;
2913
2914 if (!hide_banner)
aliguori376253e2009-03-05 23:01:23 +00002915 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
2916 "information\n", QEMU_VERSION);
ths86e94de2007-01-05 22:01:59 +00002917 monitor_start_input();
2918}
2919
ths20d8a3e2007-02-18 17:04:49 +00002920static int is_first_init = 1;
2921
aliguori376253e2009-03-05 23:01:23 +00002922void monitor_init(CharDriverState *chr, int show_banner)
bellard9dc39cb2004-03-14 21:38:27 +00002923{
ths20d8a3e2007-02-18 17:04:49 +00002924 int i;
2925
2926 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00002927 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2928 if (!key_timer)
2929 return;
ths20d8a3e2007-02-18 17:04:49 +00002930 for (i = 0; i < MAX_MON; i++) {
2931 monitor_hd[i] = NULL;
2932 }
2933 is_first_init = 0;
2934 }
2935 for (i = 0; i < MAX_MON; i++) {
2936 if (monitor_hd[i] == NULL) {
aliguori376253e2009-03-05 23:01:23 +00002937 monitor_hd[i] = chr;
ths20d8a3e2007-02-18 17:04:49 +00002938 break;
2939 }
2940 }
2941
ths86e94de2007-01-05 22:01:59 +00002942 hide_banner = !show_banner;
2943
aliguori376253e2009-03-05 23:01:23 +00002944 qemu_chr_add_handlers(chr, term_can_read, term_read, term_event, cur_mon);
aliguori83ab7952008-08-19 14:44:22 +00002945
aliguori376253e2009-03-05 23:01:23 +00002946 readline_start("", 0, monitor_command_cb, NULL);
bellard7e2515e2004-08-01 21:52:19 +00002947}
2948
aliguori376253e2009-03-05 23:01:23 +00002949static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00002950{
aliguoribb5fc202009-03-05 23:01:15 +00002951 BlockDriverState *bs = opaque;
2952 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00002953
aliguoribb5fc202009-03-05 23:01:15 +00002954 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00002955 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00002956 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00002957 }
aliguoribb5fc202009-03-05 23:01:15 +00002958 if (password_completion_cb)
2959 password_completion_cb(password_opaque, ret);
2960
2961 monitor_start_input();
bellard9dc39cb2004-03-14 21:38:27 +00002962}
aliguoric0f4ce72009-03-05 23:01:01 +00002963
aliguori376253e2009-03-05 23:01:23 +00002964void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00002965 BlockDriverCompletionFunc *completion_cb,
2966 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00002967{
aliguoribb5fc202009-03-05 23:01:15 +00002968 if (!bdrv_key_required(bs)) {
2969 if (completion_cb)
2970 completion_cb(opaque, 0);
2971 return;
2972 }
aliguoric0f4ce72009-03-05 23:01:01 +00002973
aliguori376253e2009-03-05 23:01:23 +00002974 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
2975 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00002976
2977 password_completion_cb = completion_cb;
2978 password_opaque = opaque;
2979
aliguori376253e2009-03-05 23:01:23 +00002980 monitor_read_password(mon, bdrv_password_cb, bs);
aliguoric0f4ce72009-03-05 23:01:01 +00002981}