blob: 817e4b7ed492f8271cf1eb3be286934f4afa6e7e [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Gerd Hoffmanncae49562009-06-05 15:53:17 +010026#include "hw/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
28#include "hw/pcmcia.h"
29#include "hw/pc.h"
30#include "hw/pci.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010031#include "hw/watchdog.h"
pbrook87ecb682007-11-17 17:14:51 +000032#include "gdbstub.h"
33#include "net.h"
34#include "qemu-char.h"
35#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000036#include "monitor.h"
37#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000038#include "console.h"
39#include "block.h"
40#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000041#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000042#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000043#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000044#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000045#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000046#include "acl.h"
ths6a5bd302007-12-03 17:05:38 +000047
bellard9dc39cb2004-03-14 21:38:27 +000048//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000049//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000050
bellard9307c4c2004-04-04 12:57:25 +000051/*
52 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000053 *
bellard9307c4c2004-04-04 12:57:25 +000054 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000055 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000056 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000057 * 'i' 32 bit integer
58 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000059 * '/' optional gdb-like print format (like "/10x")
60 *
61 * '?' optional type (for 'F', 's' and 'i')
62 *
63 */
64
aliguori376253e2009-03-05 23:01:23 +000065typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000066 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000067 const char *args_type;
blueswir1a5f1b962008-08-17 20:21:51 +000068 void *handler;
bellard9dc39cb2004-03-14 21:38:27 +000069 const char *params;
70 const char *help;
aliguori376253e2009-03-05 23:01:23 +000071} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000072
Mark McLoughlinf07918f2009-07-22 09:11:40 +010073/* file descriptors passed via SCM_RIGHTS */
74typedef struct mon_fd_t mon_fd_t;
75struct mon_fd_t {
76 char *name;
77 int fd;
78 LIST_ENTRY(mon_fd_t) next;
79};
80
aliguori87127162009-03-05 23:01:29 +000081struct Monitor {
82 CharDriverState *chr;
aliguori731b0362009-03-05 23:01:42 +000083 int flags;
84 int suspend_cnt;
85 uint8_t outbuf[1024];
86 int outbuf_index;
87 ReadLineState *rs;
88 CPUState *mon_cpu;
89 BlockDriverCompletionFunc *password_completion_cb;
90 void *password_opaque;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010091 LIST_HEAD(,mon_fd_t) fds;
aliguori87127162009-03-05 23:01:29 +000092 LIST_ENTRY(Monitor) entry;
93};
94
95static LIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +000096
aliguori376253e2009-03-05 23:01:23 +000097static const mon_cmd_t mon_cmds[];
98static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +000099
aliguori87127162009-03-05 23:01:29 +0000100Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000101
aliguori731b0362009-03-05 23:01:42 +0000102static void monitor_command_cb(Monitor *mon, const char *cmdline,
103 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000104
aliguori731b0362009-03-05 23:01:42 +0000105static void monitor_read_command(Monitor *mon, int show_prompt)
106{
107 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
108 if (show_prompt)
109 readline_show_prompt(mon->rs);
110}
bellard6a00d602005-11-21 23:25:50 +0000111
aliguoricde76ee2009-03-05 23:01:51 +0000112static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
113 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000114{
aliguoricde76ee2009-03-05 23:01:51 +0000115 if (mon->rs) {
116 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
117 /* prompt is printed on return from the command handler */
118 return 0;
119 } else {
120 monitor_printf(mon, "terminal does not support password prompting\n");
121 return -ENOTTY;
122 }
aliguoribb5fc202009-03-05 23:01:15 +0000123}
124
aliguori376253e2009-03-05 23:01:23 +0000125void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000126{
aliguori731b0362009-03-05 23:01:42 +0000127 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
128 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
129 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000130 }
131}
132
133/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000134static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000135{
ths60fe76f2007-12-16 03:02:09 +0000136 char c;
aliguori731b0362009-03-05 23:01:42 +0000137
138 if (!mon)
139 return;
140
bellard7e2515e2004-08-01 21:52:19 +0000141 for(;;) {
142 c = *str++;
143 if (c == '\0')
144 break;
bellard7ba12602006-07-14 20:26:42 +0000145 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000146 mon->outbuf[mon->outbuf_index++] = '\r';
147 mon->outbuf[mon->outbuf_index++] = c;
148 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
149 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000150 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000151 }
152}
153
aliguori376253e2009-03-05 23:01:23 +0000154void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000155{
156 char buf[4096];
157 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000158 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000159}
160
aliguori376253e2009-03-05 23:01:23 +0000161void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000162{
163 va_list ap;
164 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000165 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000166 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000167}
168
aliguori376253e2009-03-05 23:01:23 +0000169void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000170{
171 int i;
172
173 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000174 switch (filename[i]) {
175 case ' ':
176 case '"':
177 case '\\':
178 monitor_printf(mon, "\\%c", filename[i]);
179 break;
180 case '\t':
181 monitor_printf(mon, "\\t");
182 break;
183 case '\r':
184 monitor_printf(mon, "\\r");
185 break;
186 case '\n':
187 monitor_printf(mon, "\\n");
188 break;
189 default:
190 monitor_printf(mon, "%c", filename[i]);
191 break;
192 }
thsfef30742006-12-22 14:11:32 +0000193 }
194}
195
bellard7fe48482004-10-09 18:08:01 +0000196static int monitor_fprintf(FILE *stream, const char *fmt, ...)
197{
198 va_list ap;
199 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000200 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000201 va_end(ap);
202 return 0;
203}
204
bellard9dc39cb2004-03-14 21:38:27 +0000205static int compare_cmd(const char *name, const char *list)
206{
207 const char *p, *pstart;
208 int len;
209 len = strlen(name);
210 p = list;
211 for(;;) {
212 pstart = p;
213 p = strchr(p, '|');
214 if (!p)
215 p = pstart + strlen(pstart);
216 if ((p - pstart) == len && !memcmp(pstart, name, len))
217 return 1;
218 if (*p == '\0')
219 break;
220 p++;
221 }
222 return 0;
223}
224
aliguori376253e2009-03-05 23:01:23 +0000225static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
226 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000227{
aliguori376253e2009-03-05 23:01:23 +0000228 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000229
230 for(cmd = cmds; cmd->name != NULL; cmd++) {
231 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000232 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
233 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000234 }
235}
236
aliguori376253e2009-03-05 23:01:23 +0000237static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000238{
239 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000240 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000241 } else {
aliguori376253e2009-03-05 23:01:23 +0000242 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000243 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000244 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000245 monitor_printf(mon, "Log items (comma separated):\n");
246 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000247 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000248 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000249 }
250 }
bellard9dc39cb2004-03-14 21:38:27 +0000251 }
252}
253
aliguori376253e2009-03-05 23:01:23 +0000254static void do_commit(Monitor *mon, const char *device)
bellard9dc39cb2004-03-14 21:38:27 +0000255{
bellard7954c732006-08-01 15:52:40 +0000256 int i, all_devices;
balrog2dc7b602007-05-24 18:53:22 +0000257
bellard7954c732006-08-01 15:52:40 +0000258 all_devices = !strcmp(device, "all");
thse4bcb142007-12-02 04:51:10 +0000259 for (i = 0; i < nb_drives; i++) {
ths5fafdf22007-09-16 21:08:06 +0000260 if (all_devices ||
thse4bcb142007-12-02 04:51:10 +0000261 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
262 bdrv_commit(drives_table[i].bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000263 }
264}
265
aliguori376253e2009-03-05 23:01:23 +0000266static void do_info(Monitor *mon, const char *item)
bellard9dc39cb2004-03-14 21:38:27 +0000267{
aliguori376253e2009-03-05 23:01:23 +0000268 const mon_cmd_t *cmd;
269 void (*handler)(Monitor *);
bellard9dc39cb2004-03-14 21:38:27 +0000270
bellard9307c4c2004-04-04 12:57:25 +0000271 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000272 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000273 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000274 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000275 goto found;
276 }
277 help:
aliguori376253e2009-03-05 23:01:23 +0000278 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000279 return;
280 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000281 handler = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +0000282 handler(mon);
bellard9dc39cb2004-03-14 21:38:27 +0000283}
284
aliguori376253e2009-03-05 23:01:23 +0000285static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000286{
pbrook4a19f1e2009-04-07 23:17:49 +0000287 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000288}
289
aliguori376253e2009-03-05 23:01:23 +0000290static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000291{
292 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000293 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000294}
295
aurel32bf4f74c2008-12-18 22:42:34 +0000296#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000297static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000298{
aliguori376253e2009-03-05 23:01:23 +0000299 monitor_printf(mon, "HPET is %s by QEMU\n",
300 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000301}
aurel32bf4f74c2008-12-18 22:42:34 +0000302#endif
aliguori16b29ae2008-12-17 23:28:44 +0000303
aliguori376253e2009-03-05 23:01:23 +0000304static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000305{
aliguori376253e2009-03-05 23:01:23 +0000306 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
307 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
308 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
309 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
310 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000311}
312
bellard6a00d602005-11-21 23:25:50 +0000313/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000314static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000315{
316 CPUState *env;
317
318 for(env = first_cpu; env != NULL; env = env->next_cpu) {
319 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000320 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000321 return 0;
322 }
323 }
324 return -1;
325}
326
pbrook9596ebb2007-11-18 01:44:38 +0000327static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000328{
aliguori731b0362009-03-05 23:01:42 +0000329 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000330 mon_set_cpu(0);
331 }
aliguorid1546152009-03-12 20:12:57 +0000332 cpu_synchronize_state(cur_mon->mon_cpu, 0);
aliguori731b0362009-03-05 23:01:42 +0000333 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000334}
335
aliguori376253e2009-03-05 23:01:23 +0000336static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000337{
bellard6a00d602005-11-21 23:25:50 +0000338 CPUState *env;
339 env = mon_get_cpu();
340 if (!env)
341 return;
bellard9307c4c2004-04-04 12:57:25 +0000342#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000343 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000344 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000345#else
aliguori376253e2009-03-05 23:01:23 +0000346 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000347 0);
bellard9307c4c2004-04-04 12:57:25 +0000348#endif
349}
350
aliguori376253e2009-03-05 23:01:23 +0000351static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000352{
353 CPUState *env;
354
355 /* just to set the default cpu if not already done */
356 mon_get_cpu();
357
358 for(env = first_cpu; env != NULL; env = env->next_cpu) {
aliguorid1546152009-03-12 20:12:57 +0000359 cpu_synchronize_state(env, 0);
aliguori376253e2009-03-05 23:01:23 +0000360 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000361 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000362 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000363#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000364 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
365 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000366#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000367 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000368#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000369 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
370 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000371#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000372 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000373#endif
thsead93602007-09-06 00:18:15 +0000374 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000375 monitor_printf(mon, " (halted)");
376 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000377 }
378}
379
aliguori376253e2009-03-05 23:01:23 +0000380static void do_cpu_set(Monitor *mon, int index)
bellard6a00d602005-11-21 23:25:50 +0000381{
382 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000383 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000384}
385
aliguori376253e2009-03-05 23:01:23 +0000386static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000387{
aliguori376253e2009-03-05 23:01:23 +0000388 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000389}
390
aliguori376253e2009-03-05 23:01:23 +0000391static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000392{
393 int i;
bellard7e2515e2004-08-01 21:52:19 +0000394 const char *str;
ths3b46e622007-09-17 08:09:54 +0000395
aliguoricde76ee2009-03-05 23:01:51 +0000396 if (!mon->rs)
397 return;
bellard7e2515e2004-08-01 21:52:19 +0000398 i = 0;
399 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000400 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000401 if (!str)
402 break;
aliguori376253e2009-03-05 23:01:23 +0000403 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000404 i++;
bellardaa455482004-04-04 13:07:25 +0000405 }
406}
407
j_mayer76a66252007-03-07 08:32:30 +0000408#if defined(TARGET_PPC)
409/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000410static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000411{
412 CPUState *env;
413
414 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000415 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000416}
417#endif
418
aliguori376253e2009-03-05 23:01:23 +0000419static void do_quit(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000420{
421 exit(0);
422}
423
aliguori376253e2009-03-05 23:01:23 +0000424static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000425{
426 if (bdrv_is_inserted(bs)) {
427 if (!force) {
428 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000429 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000430 return -1;
431 }
432 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000433 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000434 return -1;
435 }
436 }
437 bdrv_close(bs);
438 }
439 return 0;
440}
441
aliguori376253e2009-03-05 23:01:23 +0000442static void do_eject(Monitor *mon, int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000443{
444 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000445
bellard9307c4c2004-04-04 12:57:25 +0000446 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000447 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000448 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000449 return;
450 }
aliguori376253e2009-03-05 23:01:23 +0000451 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000452}
453
aliguori376253e2009-03-05 23:01:23 +0000454static void do_change_block(Monitor *mon, const char *device,
455 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000456{
457 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000458 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000459
bellard9307c4c2004-04-04 12:57:25 +0000460 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000461 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000462 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000463 return;
464 }
aurel322ecea9b2008-06-18 22:10:01 +0000465 if (fmt) {
466 drv = bdrv_find_format(fmt);
467 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000468 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000469 return;
470 }
471 }
aliguori376253e2009-03-05 23:01:23 +0000472 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000473 return;
aurel322ecea9b2008-06-18 22:10:01 +0000474 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000475 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000476}
477
aliguori376253e2009-03-05 23:01:23 +0000478static void change_vnc_password_cb(Monitor *mon, const char *password,
479 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000480{
481 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000482 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000483
aliguori731b0362009-03-05 23:01:42 +0000484 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000485}
486
aliguori376253e2009-03-05 23:01:23 +0000487static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000488{
ths70848512007-08-25 01:37:05 +0000489 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000490 strcmp(target, "password") == 0) {
491 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000492 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000493 strncpy(password, arg, sizeof(password));
494 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000495 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000496 } else {
aliguori376253e2009-03-05 23:01:23 +0000497 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000498 }
ths70848512007-08-25 01:37:05 +0000499 } else {
aliguori28a76be2009-03-06 20:27:40 +0000500 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000501 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000502 }
thse25a5822007-08-25 01:36:20 +0000503}
504
aliguori376253e2009-03-05 23:01:23 +0000505static void do_change(Monitor *mon, const char *device, const char *target,
506 const char *arg)
thse25a5822007-08-25 01:36:20 +0000507{
508 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000509 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000510 } else {
aliguori28a76be2009-03-06 20:27:40 +0000511 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000512 }
513}
514
aliguori376253e2009-03-05 23:01:23 +0000515static void do_screen_dump(Monitor *mon, const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000516{
pbrook95219892006-04-09 01:06:34 +0000517 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000518}
519
aliguori376253e2009-03-05 23:01:23 +0000520static void do_logfile(Monitor *mon, const char *filename)
pbrooke735b912007-06-30 13:53:24 +0000521{
522 cpu_set_log_filename(filename);
523}
524
aliguori376253e2009-03-05 23:01:23 +0000525static void do_log(Monitor *mon, const char *items)
bellardf193c792004-03-21 17:06:25 +0000526{
527 int mask;
ths3b46e622007-09-17 08:09:54 +0000528
bellard9307c4c2004-04-04 12:57:25 +0000529 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000530 mask = 0;
531 } else {
bellard9307c4c2004-04-04 12:57:25 +0000532 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000533 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000534 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000535 return;
536 }
537 }
538 cpu_set_log(mask);
539}
540
aurel321b530a62009-04-05 20:08:59 +0000541static void do_singlestep(Monitor *mon, const char *option)
542{
543 if (!option || !strcmp(option, "on")) {
544 singlestep = 1;
545 } else if (!strcmp(option, "off")) {
546 singlestep = 0;
547 } else {
548 monitor_printf(mon, "unexpected option %s\n", option);
549 }
550}
551
aliguori376253e2009-03-05 23:01:23 +0000552static void do_stop(Monitor *mon)
bellard8a7ddc32004-03-31 19:00:16 +0000553{
554 vm_stop(EXCP_INTERRUPT);
555}
556
aliguoribb5fc202009-03-05 23:01:15 +0000557static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000558
aliguori376253e2009-03-05 23:01:23 +0000559struct bdrv_iterate_context {
560 Monitor *mon;
561 int err;
562};
aliguoric0f4ce72009-03-05 23:01:01 +0000563
aliguori376253e2009-03-05 23:01:23 +0000564static void do_cont(Monitor *mon)
565{
566 struct bdrv_iterate_context context = { mon, 0 };
567
568 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000569 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000570 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000571 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000572}
573
aliguoribb5fc202009-03-05 23:01:15 +0000574static void bdrv_key_cb(void *opaque, int err)
575{
aliguori376253e2009-03-05 23:01:23 +0000576 Monitor *mon = opaque;
577
aliguoribb5fc202009-03-05 23:01:15 +0000578 /* another key was set successfully, retry to continue */
579 if (!err)
aliguori376253e2009-03-05 23:01:23 +0000580 do_cont(mon);
aliguoribb5fc202009-03-05 23:01:15 +0000581}
582
583static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
584{
aliguori376253e2009-03-05 23:01:23 +0000585 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000586
aliguori376253e2009-03-05 23:01:23 +0000587 if (!context->err && bdrv_key_required(bs)) {
588 context->err = -EBUSY;
589 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
590 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000591 }
592}
593
aliguori59030a82009-04-05 18:43:41 +0000594static void do_gdbserver(Monitor *mon, const char *device)
bellard8a7ddc32004-03-31 19:00:16 +0000595{
aliguori59030a82009-04-05 18:43:41 +0000596 if (!device)
597 device = "tcp::" DEFAULT_GDBSTUB_PORT;
598 if (gdbserver_start(device) < 0) {
599 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
600 device);
601 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000602 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000603 } else {
aliguori59030a82009-04-05 18:43:41 +0000604 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
605 device);
bellard8a7ddc32004-03-31 19:00:16 +0000606 }
607}
608
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100609static void do_watchdog_action(Monitor *mon, const char *action)
610{
611 if (select_watchdog_action(action) == -1) {
612 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
613 }
614}
615
aliguori376253e2009-03-05 23:01:23 +0000616static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000617{
aliguori376253e2009-03-05 23:01:23 +0000618 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000619 switch(c) {
620 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000621 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000622 break;
623 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000624 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000625 break;
626 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000627 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000628 break;
629 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000630 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000631 break;
632 default:
633 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000634 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000635 } else {
aliguori376253e2009-03-05 23:01:23 +0000636 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000637 }
638 break;
639 }
aliguori376253e2009-03-05 23:01:23 +0000640 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000641}
642
aliguori376253e2009-03-05 23:01:23 +0000643static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000644 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000645{
bellard6a00d602005-11-21 23:25:50 +0000646 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000647 int nb_per_line, l, line_size, i, max_digits, len;
648 uint8_t buf[16];
649 uint64_t v;
650
651 if (format == 'i') {
652 int flags;
653 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000654 env = mon_get_cpu();
655 if (!env && !is_physical)
656 return;
bellard9307c4c2004-04-04 12:57:25 +0000657#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000658 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000659 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000660 } else if (wsize == 4) {
661 flags = 0;
662 } else {
bellard6a15fd12006-04-12 21:07:07 +0000663 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000664 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000665 if (env) {
666#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000667 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000668 (env->segs[R_CS].flags & DESC_L_MASK))
669 flags = 2;
670 else
671#endif
672 if (!(env->segs[R_CS].flags & DESC_B_MASK))
673 flags = 1;
674 }
bellard4c27ba22004-04-25 18:05:08 +0000675 }
676#endif
aliguori376253e2009-03-05 23:01:23 +0000677 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000678 return;
679 }
680
681 len = wsize * count;
682 if (wsize == 1)
683 line_size = 8;
684 else
685 line_size = 16;
686 nb_per_line = line_size / wsize;
687 max_digits = 0;
688
689 switch(format) {
690 case 'o':
691 max_digits = (wsize * 8 + 2) / 3;
692 break;
693 default:
694 case 'x':
695 max_digits = (wsize * 8) / 4;
696 break;
697 case 'u':
698 case 'd':
699 max_digits = (wsize * 8 * 10 + 32) / 33;
700 break;
701 case 'c':
702 wsize = 1;
703 break;
704 }
705
706 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000707 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000708 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000709 else
aliguori376253e2009-03-05 23:01:23 +0000710 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000711 l = len;
712 if (l > line_size)
713 l = line_size;
714 if (is_physical) {
715 cpu_physical_memory_rw(addr, buf, l, 0);
716 } else {
bellard6a00d602005-11-21 23:25:50 +0000717 env = mon_get_cpu();
718 if (!env)
719 break;
aliguoric8f79b62008-08-18 14:00:20 +0000720 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000721 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000722 break;
723 }
bellard9307c4c2004-04-04 12:57:25 +0000724 }
ths5fafdf22007-09-16 21:08:06 +0000725 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000726 while (i < l) {
727 switch(wsize) {
728 default:
729 case 1:
730 v = ldub_raw(buf + i);
731 break;
732 case 2:
733 v = lduw_raw(buf + i);
734 break;
735 case 4:
bellard92a31b12005-02-10 22:00:52 +0000736 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000737 break;
738 case 8:
739 v = ldq_raw(buf + i);
740 break;
741 }
aliguori376253e2009-03-05 23:01:23 +0000742 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000743 switch(format) {
744 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000745 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000746 break;
747 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000748 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000749 break;
750 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000751 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000752 break;
753 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000754 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000755 break;
756 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000757 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000758 break;
759 }
760 i += wsize;
761 }
aliguori376253e2009-03-05 23:01:23 +0000762 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000763 addr += l;
764 len -= l;
765 }
766}
767
bellard92a31b12005-02-10 22:00:52 +0000768#if TARGET_LONG_BITS == 64
769#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
770#else
771#define GET_TLONG(h, l) (l)
772#endif
773
aliguori376253e2009-03-05 23:01:23 +0000774static void do_memory_dump(Monitor *mon, int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000775 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000776{
bellard92a31b12005-02-10 22:00:52 +0000777 target_long addr = GET_TLONG(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000778 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000779}
780
blueswir17743e582007-09-24 18:39:04 +0000781#if TARGET_PHYS_ADDR_BITS > 32
782#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
783#else
784#define GET_TPHYSADDR(h, l) (l)
785#endif
786
aliguori376253e2009-03-05 23:01:23 +0000787static void do_physical_memory_dump(Monitor *mon, int count, int format,
788 int size, uint32_t addrh, uint32_t addrl)
bellard92a31b12005-02-10 22:00:52 +0000789
bellard9307c4c2004-04-04 12:57:25 +0000790{
blueswir17743e582007-09-24 18:39:04 +0000791 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000792 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000793}
794
aliguori376253e2009-03-05 23:01:23 +0000795static void do_print(Monitor *mon, int count, int format, int size,
796 unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000797{
blueswir17743e582007-09-24 18:39:04 +0000798 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
799#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000800 switch(format) {
801 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000802 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000803 break;
804 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000805 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000806 break;
807 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000808 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000809 break;
810 default:
811 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000812 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000813 break;
814 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000815 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000816 break;
817 }
bellard92a31b12005-02-10 22:00:52 +0000818#else
819 switch(format) {
820 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000821 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000822 break;
823 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000824 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000825 break;
826 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000827 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000828 break;
829 default:
830 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000831 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000832 break;
833 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000834 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000835 break;
836 }
837#endif
aliguori376253e2009-03-05 23:01:23 +0000838 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000839}
840
aliguori376253e2009-03-05 23:01:23 +0000841static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000842 uint32_t size, const char *filename)
843{
844 FILE *f;
845 target_long addr = GET_TLONG(valh, vall);
846 uint32_t l;
847 CPUState *env;
848 uint8_t buf[1024];
849
850 env = mon_get_cpu();
851 if (!env)
852 return;
853
854 f = fopen(filename, "wb");
855 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000856 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000857 return;
858 }
859 while (size != 0) {
860 l = sizeof(buf);
861 if (l > size)
862 l = size;
863 cpu_memory_rw_debug(env, addr, buf, l, 0);
864 fwrite(buf, 1, l, f);
865 addr += l;
866 size -= l;
867 }
868 fclose(f);
869}
870
aliguori376253e2009-03-05 23:01:23 +0000871static void do_physical_memory_save(Monitor *mon, unsigned int valh,
872 unsigned int vall, uint32_t size,
873 const char *filename)
aurel32a8bdf7a2008-04-11 21:36:14 +0000874{
875 FILE *f;
876 uint32_t l;
877 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000878 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000879
880 f = fopen(filename, "wb");
881 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000882 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000883 return;
884 }
885 while (size != 0) {
886 l = sizeof(buf);
887 if (l > size)
888 l = size;
889 cpu_physical_memory_rw(addr, buf, l, 0);
890 fwrite(buf, 1, l, f);
891 fflush(f);
892 addr += l;
893 size -= l;
894 }
895 fclose(f);
896}
897
aliguori376253e2009-03-05 23:01:23 +0000898static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
bellarde4cf1ad2005-06-04 20:15:57 +0000899{
900 uint32_t addr;
901 uint8_t buf[1];
902 uint16_t sum;
903
904 sum = 0;
905 for(addr = start; addr < (start + size); addr++) {
906 cpu_physical_memory_rw(addr, buf, 1, 0);
907 /* BSD sum algorithm ('sum' Unix command) */
908 sum = (sum >> 1) | (sum << 15);
909 sum += buf[0];
910 }
aliguori376253e2009-03-05 23:01:23 +0000911 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000912}
913
bellarda3a91a32004-06-04 11:06:21 +0000914typedef struct {
915 int keycode;
916 const char *name;
917} KeyDef;
918
919static const KeyDef key_defs[] = {
920 { 0x2a, "shift" },
921 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000922
bellarda3a91a32004-06-04 11:06:21 +0000923 { 0x38, "alt" },
924 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000925 { 0x64, "altgr" },
926 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000927 { 0x1d, "ctrl" },
928 { 0x9d, "ctrl_r" },
929
930 { 0xdd, "menu" },
931
932 { 0x01, "esc" },
933
934 { 0x02, "1" },
935 { 0x03, "2" },
936 { 0x04, "3" },
937 { 0x05, "4" },
938 { 0x06, "5" },
939 { 0x07, "6" },
940 { 0x08, "7" },
941 { 0x09, "8" },
942 { 0x0a, "9" },
943 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000944 { 0x0c, "minus" },
945 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000946 { 0x0e, "backspace" },
947
948 { 0x0f, "tab" },
949 { 0x10, "q" },
950 { 0x11, "w" },
951 { 0x12, "e" },
952 { 0x13, "r" },
953 { 0x14, "t" },
954 { 0x15, "y" },
955 { 0x16, "u" },
956 { 0x17, "i" },
957 { 0x18, "o" },
958 { 0x19, "p" },
959
960 { 0x1c, "ret" },
961
962 { 0x1e, "a" },
963 { 0x1f, "s" },
964 { 0x20, "d" },
965 { 0x21, "f" },
966 { 0x22, "g" },
967 { 0x23, "h" },
968 { 0x24, "j" },
969 { 0x25, "k" },
970 { 0x26, "l" },
971
972 { 0x2c, "z" },
973 { 0x2d, "x" },
974 { 0x2e, "c" },
975 { 0x2f, "v" },
976 { 0x30, "b" },
977 { 0x31, "n" },
978 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000979 { 0x33, "comma" },
980 { 0x34, "dot" },
981 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000982
balrog4d3b6f62008-02-10 16:33:14 +0000983 { 0x37, "asterisk" },
984
bellarda3a91a32004-06-04 11:06:21 +0000985 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000986 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000987 { 0x3b, "f1" },
988 { 0x3c, "f2" },
989 { 0x3d, "f3" },
990 { 0x3e, "f4" },
991 { 0x3f, "f5" },
992 { 0x40, "f6" },
993 { 0x41, "f7" },
994 { 0x42, "f8" },
995 { 0x43, "f9" },
996 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +0000997 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000998 { 0x46, "scroll_lock" },
999
bellard64866c32006-05-07 18:03:31 +00001000 { 0xb5, "kp_divide" },
1001 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001002 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001003 { 0x4e, "kp_add" },
1004 { 0x9c, "kp_enter" },
1005 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001006 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001007
1008 { 0x52, "kp_0" },
1009 { 0x4f, "kp_1" },
1010 { 0x50, "kp_2" },
1011 { 0x51, "kp_3" },
1012 { 0x4b, "kp_4" },
1013 { 0x4c, "kp_5" },
1014 { 0x4d, "kp_6" },
1015 { 0x47, "kp_7" },
1016 { 0x48, "kp_8" },
1017 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001018
bellarda3a91a32004-06-04 11:06:21 +00001019 { 0x56, "<" },
1020
1021 { 0x57, "f11" },
1022 { 0x58, "f12" },
1023
1024 { 0xb7, "print" },
1025
1026 { 0xc7, "home" },
1027 { 0xc9, "pgup" },
1028 { 0xd1, "pgdn" },
1029 { 0xcf, "end" },
1030
1031 { 0xcb, "left" },
1032 { 0xc8, "up" },
1033 { 0xd0, "down" },
1034 { 0xcd, "right" },
1035
1036 { 0xd2, "insert" },
1037 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001038#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1039 { 0xf0, "stop" },
1040 { 0xf1, "again" },
1041 { 0xf2, "props" },
1042 { 0xf3, "undo" },
1043 { 0xf4, "front" },
1044 { 0xf5, "copy" },
1045 { 0xf6, "open" },
1046 { 0xf7, "paste" },
1047 { 0xf8, "find" },
1048 { 0xf9, "cut" },
1049 { 0xfa, "lf" },
1050 { 0xfb, "help" },
1051 { 0xfc, "meta_l" },
1052 { 0xfd, "meta_r" },
1053 { 0xfe, "compose" },
1054#endif
bellarda3a91a32004-06-04 11:06:21 +00001055 { 0, NULL },
1056};
1057
1058static int get_keycode(const char *key)
1059{
1060 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001061 char *endp;
1062 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001063
1064 for(p = key_defs; p->name != NULL; p++) {
1065 if (!strcmp(key, p->name))
1066 return p->keycode;
1067 }
bellard64866c32006-05-07 18:03:31 +00001068 if (strstart(key, "0x", NULL)) {
1069 ret = strtoul(key, &endp, 0);
1070 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1071 return ret;
1072 }
bellarda3a91a32004-06-04 11:06:21 +00001073 return -1;
1074}
1075
balrogc8256f92008-06-08 22:45:01 +00001076#define MAX_KEYCODES 16
1077static uint8_t keycodes[MAX_KEYCODES];
1078static int nb_pending_keycodes;
1079static QEMUTimer *key_timer;
1080
1081static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001082{
balrogc8256f92008-06-08 22:45:01 +00001083 int keycode;
1084
1085 while (nb_pending_keycodes > 0) {
1086 nb_pending_keycodes--;
1087 keycode = keycodes[nb_pending_keycodes];
1088 if (keycode & 0x80)
1089 kbd_put_keycode(0xe0);
1090 kbd_put_keycode(keycode | 0x80);
1091 }
1092}
1093
aliguori376253e2009-03-05 23:01:23 +00001094static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1095 int hold_time)
balrogc8256f92008-06-08 22:45:01 +00001096{
balrog3401c0d2008-06-04 10:05:59 +00001097 char keyname_buf[16];
1098 char *separator;
1099 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +00001100
balrogc8256f92008-06-08 22:45:01 +00001101 if (nb_pending_keycodes > 0) {
1102 qemu_del_timer(key_timer);
1103 release_keys(NULL);
1104 }
1105 if (!has_hold_time)
1106 hold_time = 100;
1107 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001108 while (1) {
1109 separator = strchr(string, '-');
1110 keyname_len = separator ? separator - string : strlen(string);
1111 if (keyname_len > 0) {
1112 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1113 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001114 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001115 return;
bellarda3a91a32004-06-04 11:06:21 +00001116 }
balrogc8256f92008-06-08 22:45:01 +00001117 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001118 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001119 return;
1120 }
1121 keyname_buf[keyname_len] = 0;
1122 keycode = get_keycode(keyname_buf);
1123 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001124 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001125 return;
1126 }
balrogc8256f92008-06-08 22:45:01 +00001127 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001128 }
balrog3401c0d2008-06-04 10:05:59 +00001129 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001130 break;
balrog3401c0d2008-06-04 10:05:59 +00001131 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001132 }
balrogc8256f92008-06-08 22:45:01 +00001133 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001134 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001135 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001136 keycode = keycodes[i];
1137 if (keycode & 0x80)
1138 kbd_put_keycode(0xe0);
1139 kbd_put_keycode(keycode & 0x7f);
1140 }
balrogc8256f92008-06-08 22:45:01 +00001141 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001142 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1143 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001144}
1145
bellard13224a82006-07-14 22:03:35 +00001146static int mouse_button_state;
1147
aliguori376253e2009-03-05 23:01:23 +00001148static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001149 const char *dz_str)
1150{
1151 int dx, dy, dz;
1152 dx = strtol(dx_str, NULL, 0);
1153 dy = strtol(dy_str, NULL, 0);
1154 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001155 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001156 dz = strtol(dz_str, NULL, 0);
1157 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1158}
1159
aliguori376253e2009-03-05 23:01:23 +00001160static void do_mouse_button(Monitor *mon, int button_state)
bellard13224a82006-07-14 22:03:35 +00001161{
1162 mouse_button_state = button_state;
1163 kbd_mouse_event(0, 0, 0, mouse_button_state);
1164}
1165
aliguori376253e2009-03-05 23:01:23 +00001166static void do_ioport_read(Monitor *mon, int count, int format, int size,
1167 int addr, int has_index, int index)
bellard34405572004-06-08 00:55:58 +00001168{
1169 uint32_t val;
1170 int suffix;
1171
1172 if (has_index) {
Isaku Yamahatad56dd6c2009-07-02 19:32:07 +09001173 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001174 addr++;
1175 }
1176 addr &= 0xffff;
1177
1178 switch(size) {
1179 default:
1180 case 1:
1181 val = cpu_inb(NULL, addr);
1182 suffix = 'b';
1183 break;
1184 case 2:
1185 val = cpu_inw(NULL, addr);
1186 suffix = 'w';
1187 break;
1188 case 4:
1189 val = cpu_inl(NULL, addr);
1190 suffix = 'l';
1191 break;
1192 }
aliguori376253e2009-03-05 23:01:23 +00001193 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1194 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001195}
bellarda3a91a32004-06-04 11:06:21 +00001196
Jan Kiszkaf1147842009-07-14 10:20:11 +02001197static void do_ioport_write(Monitor *mon, int count, int format, int size,
1198 int addr, int val)
1199{
1200 addr &= IOPORTS_MASK;
1201
1202 switch (size) {
1203 default:
1204 case 1:
1205 cpu_outb(NULL, addr, val);
1206 break;
1207 case 2:
1208 cpu_outw(NULL, addr, val);
1209 break;
1210 case 4:
1211 cpu_outl(NULL, addr, val);
1212 break;
1213 }
1214}
1215
aliguori376253e2009-03-05 23:01:23 +00001216static void do_boot_set(Monitor *mon, const char *bootdevice)
aurel320ecdffb2008-05-04 20:11:34 +00001217{
1218 int res;
1219
Jan Kiszka76e30d02009-07-02 00:19:02 +02001220 res = qemu_boot_set(bootdevice);
1221 if (res == 0) {
1222 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1223 } else if (res > 0) {
1224 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001225 } else {
aliguori376253e2009-03-05 23:01:23 +00001226 monitor_printf(mon, "no function defined to set boot device list for "
1227 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001228 }
1229}
1230
aliguori376253e2009-03-05 23:01:23 +00001231static void do_system_reset(Monitor *mon)
bellarde4f90822004-06-20 12:35:44 +00001232{
1233 qemu_system_reset_request();
1234}
1235
aliguori376253e2009-03-05 23:01:23 +00001236static void do_system_powerdown(Monitor *mon)
bellard34751872005-07-02 14:31:34 +00001237{
1238 qemu_system_powerdown_request();
1239}
1240
bellardb86bda52004-09-18 19:32:46 +00001241#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001242static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001243{
aliguori376253e2009-03-05 23:01:23 +00001244 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1245 addr,
1246 pte & mask,
1247 pte & PG_GLOBAL_MASK ? 'G' : '-',
1248 pte & PG_PSE_MASK ? 'P' : '-',
1249 pte & PG_DIRTY_MASK ? 'D' : '-',
1250 pte & PG_ACCESSED_MASK ? 'A' : '-',
1251 pte & PG_PCD_MASK ? 'C' : '-',
1252 pte & PG_PWT_MASK ? 'T' : '-',
1253 pte & PG_USER_MASK ? 'U' : '-',
1254 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001255}
1256
aliguori376253e2009-03-05 23:01:23 +00001257static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001258{
bellard6a00d602005-11-21 23:25:50 +00001259 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001260 int l1, l2;
1261 uint32_t pgd, pde, pte;
1262
bellard6a00d602005-11-21 23:25:50 +00001263 env = mon_get_cpu();
1264 if (!env)
1265 return;
1266
bellardb86bda52004-09-18 19:32:46 +00001267 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001268 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001269 return;
1270 }
1271 pgd = env->cr[3] & ~0xfff;
1272 for(l1 = 0; l1 < 1024; l1++) {
1273 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1274 pde = le32_to_cpu(pde);
1275 if (pde & PG_PRESENT_MASK) {
1276 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001277 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001278 } else {
1279 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001280 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001281 (uint8_t *)&pte, 4);
1282 pte = le32_to_cpu(pte);
1283 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001284 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001285 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001286 ~0xfff);
1287 }
1288 }
1289 }
1290 }
1291 }
1292}
1293
aliguori376253e2009-03-05 23:01:23 +00001294static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001295 uint32_t end, int prot)
1296{
bellard9746b152004-11-11 18:30:24 +00001297 int prot1;
1298 prot1 = *plast_prot;
1299 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001300 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001301 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1302 *pstart, end, end - *pstart,
1303 prot1 & PG_USER_MASK ? 'u' : '-',
1304 'r',
1305 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001306 }
1307 if (prot != 0)
1308 *pstart = end;
1309 else
1310 *pstart = -1;
1311 *plast_prot = prot;
1312 }
1313}
1314
aliguori376253e2009-03-05 23:01:23 +00001315static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001316{
bellard6a00d602005-11-21 23:25:50 +00001317 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001318 int l1, l2, prot, last_prot;
1319 uint32_t pgd, pde, pte, start, end;
1320
bellard6a00d602005-11-21 23:25:50 +00001321 env = mon_get_cpu();
1322 if (!env)
1323 return;
1324
bellardb86bda52004-09-18 19:32:46 +00001325 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001326 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001327 return;
1328 }
1329 pgd = env->cr[3] & ~0xfff;
1330 last_prot = 0;
1331 start = -1;
1332 for(l1 = 0; l1 < 1024; l1++) {
1333 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1334 pde = le32_to_cpu(pde);
1335 end = l1 << 22;
1336 if (pde & PG_PRESENT_MASK) {
1337 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1338 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001339 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001340 } else {
1341 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001342 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001343 (uint8_t *)&pte, 4);
1344 pte = le32_to_cpu(pte);
1345 end = (l1 << 22) + (l2 << 12);
1346 if (pte & PG_PRESENT_MASK) {
1347 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1348 } else {
1349 prot = 0;
1350 }
aliguori376253e2009-03-05 23:01:23 +00001351 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001352 }
1353 }
1354 } else {
1355 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001356 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001357 }
1358 }
1359}
1360#endif
1361
aurel327c664e22009-03-03 06:12:22 +00001362#if defined(TARGET_SH4)
1363
aliguori376253e2009-03-05 23:01:23 +00001364static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001365{
aliguori376253e2009-03-05 23:01:23 +00001366 monitor_printf(mon, " tlb%i:\t"
1367 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1368 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1369 "dirty=%hhu writethrough=%hhu\n",
1370 idx,
1371 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1372 tlb->v, tlb->sh, tlb->c, tlb->pr,
1373 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001374}
1375
aliguori376253e2009-03-05 23:01:23 +00001376static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001377{
1378 CPUState *env = mon_get_cpu();
1379 int i;
1380
aliguori376253e2009-03-05 23:01:23 +00001381 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001382 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001383 print_tlb (mon, i, &env->itlb[i]);
1384 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001385 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001386 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001387}
1388
1389#endif
1390
aliguori376253e2009-03-05 23:01:23 +00001391static void do_info_kqemu(Monitor *mon)
bellard0f4c6412005-07-24 18:10:56 +00001392{
blueswir1640f42e2009-04-19 10:18:01 +00001393#ifdef CONFIG_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001394 CPUState *env;
bellard0f4c6412005-07-24 18:10:56 +00001395 int val;
1396 val = 0;
bellard6a00d602005-11-21 23:25:50 +00001397 env = mon_get_cpu();
1398 if (!env) {
aliguori376253e2009-03-05 23:01:23 +00001399 monitor_printf(mon, "No cpu initialized yet");
bellard6a00d602005-11-21 23:25:50 +00001400 return;
1401 }
1402 val = env->kqemu_enabled;
aliguori376253e2009-03-05 23:01:23 +00001403 monitor_printf(mon, "kqemu support: ");
bellard5f1ce942006-02-08 22:40:15 +00001404 switch(val) {
1405 default:
1406 case 0:
aliguori376253e2009-03-05 23:01:23 +00001407 monitor_printf(mon, "disabled\n");
bellard5f1ce942006-02-08 22:40:15 +00001408 break;
1409 case 1:
aliguori376253e2009-03-05 23:01:23 +00001410 monitor_printf(mon, "enabled for user code\n");
bellard5f1ce942006-02-08 22:40:15 +00001411 break;
1412 case 2:
aliguori376253e2009-03-05 23:01:23 +00001413 monitor_printf(mon, "enabled for user and kernel code\n");
bellard5f1ce942006-02-08 22:40:15 +00001414 break;
1415 }
bellard0f4c6412005-07-24 18:10:56 +00001416#else
aliguori376253e2009-03-05 23:01:23 +00001417 monitor_printf(mon, "kqemu support: not compiled\n");
bellard0f4c6412005-07-24 18:10:56 +00001418#endif
ths5fafdf22007-09-16 21:08:06 +00001419}
bellard0f4c6412005-07-24 18:10:56 +00001420
aliguori376253e2009-03-05 23:01:23 +00001421static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001422{
1423#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001424 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001425 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001426 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001427 else
aliguori376253e2009-03-05 23:01:23 +00001428 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001429#else
aliguori376253e2009-03-05 23:01:23 +00001430 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001431#endif
1432}
1433
aliguori030ea372009-04-21 22:30:47 +00001434static void do_info_numa(Monitor *mon)
1435{
aliguorib28b6232009-04-22 20:20:29 +00001436 int i;
aliguori030ea372009-04-21 22:30:47 +00001437 CPUState *env;
1438
1439 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1440 for (i = 0; i < nb_numa_nodes; i++) {
1441 monitor_printf(mon, "node %d cpus:", i);
1442 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1443 if (env->numa_node == i) {
1444 monitor_printf(mon, " %d", env->cpu_index);
1445 }
1446 }
1447 monitor_printf(mon, "\n");
1448 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1449 node_mem[i] >> 20);
1450 }
1451}
1452
bellard5f1ce942006-02-08 22:40:15 +00001453#ifdef CONFIG_PROFILER
1454
1455int64_t kqemu_time;
1456int64_t qemu_time;
1457int64_t kqemu_exec_count;
1458int64_t dev_time;
1459int64_t kqemu_ret_int_count;
1460int64_t kqemu_ret_excp_count;
1461int64_t kqemu_ret_intr_count;
1462
aliguori376253e2009-03-05 23:01:23 +00001463static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001464{
1465 int64_t total;
1466 total = qemu_time;
1467 if (total == 0)
1468 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001469 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1470 dev_time, dev_time / (double)ticks_per_sec);
1471 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1472 qemu_time, qemu_time / (double)ticks_per_sec);
1473 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1474 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1475 PRId64 "\n",
1476 kqemu_time, kqemu_time / (double)ticks_per_sec,
1477 kqemu_time / (double)total * 100.0,
1478 kqemu_exec_count,
1479 kqemu_ret_int_count,
1480 kqemu_ret_excp_count,
1481 kqemu_ret_intr_count);
bellard5f1ce942006-02-08 22:40:15 +00001482 qemu_time = 0;
1483 kqemu_time = 0;
1484 kqemu_exec_count = 0;
1485 dev_time = 0;
1486 kqemu_ret_int_count = 0;
1487 kqemu_ret_excp_count = 0;
1488 kqemu_ret_intr_count = 0;
blueswir1640f42e2009-04-19 10:18:01 +00001489#ifdef CONFIG_KQEMU
bellard5f1ce942006-02-08 22:40:15 +00001490 kqemu_record_dump();
1491#endif
1492}
1493#else
aliguori376253e2009-03-05 23:01:23 +00001494static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001495{
aliguori376253e2009-03-05 23:01:23 +00001496 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001497}
1498#endif
1499
bellardec36b692006-07-16 18:57:03 +00001500/* Capture support */
1501static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1502
aliguori376253e2009-03-05 23:01:23 +00001503static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001504{
1505 int i;
1506 CaptureState *s;
1507
1508 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001509 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001510 s->ops.info (s->opaque);
1511 }
1512}
1513
Blue Swirl23130862009-06-06 08:22:04 +00001514#ifdef HAS_AUDIO
aliguori376253e2009-03-05 23:01:23 +00001515static void do_stop_capture(Monitor *mon, int n)
bellardec36b692006-07-16 18:57:03 +00001516{
1517 int i;
1518 CaptureState *s;
1519
1520 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1521 if (i == n) {
1522 s->ops.destroy (s->opaque);
1523 LIST_REMOVE (s, entries);
1524 qemu_free (s);
1525 return;
1526 }
1527 }
1528}
1529
aliguori376253e2009-03-05 23:01:23 +00001530static void do_wav_capture(Monitor *mon, const char *path,
1531 int has_freq, int freq,
1532 int has_bits, int bits,
1533 int has_channels, int nchannels)
bellardec36b692006-07-16 18:57:03 +00001534{
1535 CaptureState *s;
1536
1537 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001538
1539 freq = has_freq ? freq : 44100;
1540 bits = has_bits ? bits : 16;
1541 nchannels = has_channels ? nchannels : 2;
1542
1543 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001544 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001545 qemu_free (s);
1546 }
1547 LIST_INSERT_HEAD (&capture_head, s, entries);
1548}
1549#endif
1550
aurel32dc1c0b72008-04-27 23:52:12 +00001551#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001552static void do_inject_nmi(Monitor *mon, int cpu_index)
aurel32dc1c0b72008-04-27 23:52:12 +00001553{
1554 CPUState *env;
1555
1556 for (env = first_cpu; env != NULL; env = env->next_cpu)
1557 if (env->cpu_index == cpu_index) {
1558 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1559 break;
1560 }
1561}
1562#endif
1563
aliguori376253e2009-03-05 23:01:23 +00001564static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001565{
aurel321b530a62009-04-05 20:08:59 +00001566 if (vm_running) {
1567 if (singlestep) {
1568 monitor_printf(mon, "VM status: running (single step mode)\n");
1569 } else {
1570 monitor_printf(mon, "VM status: running\n");
1571 }
1572 } else
aliguori376253e2009-03-05 23:01:23 +00001573 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001574}
1575
1576
aliguori376253e2009-03-05 23:01:23 +00001577static void do_balloon(Monitor *mon, int value)
aliguoridf751fa2008-12-04 20:19:35 +00001578{
1579 ram_addr_t target = value;
1580 qemu_balloon(target << 20);
1581}
1582
aliguori376253e2009-03-05 23:01:23 +00001583static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001584{
1585 ram_addr_t actual;
1586
1587 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001588 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001589 monitor_printf(mon, "Using KVM without synchronous MMU, "
1590 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001591 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001592 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001593 else
aliguori376253e2009-03-05 23:01:23 +00001594 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001595}
1596
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001597static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001598{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001599 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001600
aliguori76655d62009-03-06 20:27:37 +00001601 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001602 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001603 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001604 return acl;
1605}
aliguori76655d62009-03-06 20:27:37 +00001606
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001607static void do_acl_show(Monitor *mon, const char *aclname)
1608{
1609 qemu_acl *acl = find_acl(mon, aclname);
1610 qemu_acl_entry *entry;
1611 int i = 0;
1612
1613 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001614 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001615 acl->defaultDeny ? "deny" : "allow");
aliguori28a76be2009-03-06 20:27:40 +00001616 TAILQ_FOREACH(entry, &acl->entries, next) {
1617 i++;
1618 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001619 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001620 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001621 }
1622}
1623
1624static void do_acl_reset(Monitor *mon, const char *aclname)
1625{
1626 qemu_acl *acl = find_acl(mon, aclname);
1627
1628 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001629 qemu_acl_reset(acl);
1630 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001631 }
1632}
aliguori76655d62009-03-06 20:27:37 +00001633
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001634static void do_acl_policy(Monitor *mon, const char *aclname,
1635 const char *policy)
1636{
1637 qemu_acl *acl = find_acl(mon, aclname);
1638
1639 if (acl) {
1640 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001641 acl->defaultDeny = 0;
1642 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001643 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001644 acl->defaultDeny = 1;
1645 monitor_printf(mon, "acl: policy set to 'deny'\n");
1646 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001647 monitor_printf(mon, "acl: unknown policy '%s', "
1648 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001649 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001650 }
1651}
aliguori76655d62009-03-06 20:27:37 +00001652
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001653static void do_acl_add(Monitor *mon, const char *aclname,
1654 const char *match, const char *policy,
1655 int has_index, int index)
1656{
1657 qemu_acl *acl = find_acl(mon, aclname);
1658 int deny, ret;
1659
1660 if (acl) {
1661 if (strcmp(policy, "allow") == 0) {
1662 deny = 0;
1663 } else if (strcmp(policy, "deny") == 0) {
1664 deny = 1;
1665 } else {
1666 monitor_printf(mon, "acl: unknown policy '%s', "
1667 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001668 return;
1669 }
aliguori28a76be2009-03-06 20:27:40 +00001670 if (has_index)
1671 ret = qemu_acl_insert(acl, deny, match, index);
1672 else
1673 ret = qemu_acl_append(acl, deny, match);
1674 if (ret < 0)
1675 monitor_printf(mon, "acl: unable to add acl entry\n");
1676 else
1677 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001678 }
1679}
aliguori76655d62009-03-06 20:27:37 +00001680
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001681static void do_acl_remove(Monitor *mon, const char *aclname, const char *match)
1682{
1683 qemu_acl *acl = find_acl(mon, aclname);
1684 int ret;
aliguori76655d62009-03-06 20:27:37 +00001685
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001686 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001687 ret = qemu_acl_remove(acl, match);
1688 if (ret < 0)
1689 monitor_printf(mon, "acl: no matching acl entry\n");
1690 else
1691 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001692 }
1693}
1694
Huang Ying79c4f6b2009-06-23 10:05:14 +08001695#if defined(TARGET_I386)
1696static void do_inject_mce(Monitor *mon,
1697 int cpu_index, int bank,
1698 unsigned status_hi, unsigned status_lo,
1699 unsigned mcg_status_hi, unsigned mcg_status_lo,
1700 unsigned addr_hi, unsigned addr_lo,
1701 unsigned misc_hi, unsigned misc_lo)
1702{
1703 CPUState *cenv;
1704 uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
1705 uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
1706 uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
1707 uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
1708
1709 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1710 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1711 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1712 break;
1713 }
1714}
1715#endif
1716
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001717static void do_getfd(Monitor *mon, const char *fdname)
1718{
1719 mon_fd_t *monfd;
1720 int fd;
1721
1722 fd = qemu_chr_get_msgfd(mon->chr);
1723 if (fd == -1) {
1724 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1725 return;
1726 }
1727
1728 if (qemu_isdigit(fdname[0])) {
1729 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1730 return;
1731 }
1732
1733 fd = dup(fd);
1734 if (fd == -1) {
1735 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1736 strerror(errno));
1737 return;
1738 }
1739
1740 LIST_FOREACH(monfd, &mon->fds, next) {
1741 if (strcmp(monfd->name, fdname) != 0) {
1742 continue;
1743 }
1744
1745 close(monfd->fd);
1746 monfd->fd = fd;
1747 return;
1748 }
1749
1750 monfd = qemu_mallocz(sizeof(mon_fd_t));
1751 monfd->name = qemu_strdup(fdname);
1752 monfd->fd = fd;
1753
1754 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1755}
1756
1757static void do_closefd(Monitor *mon, const char *fdname)
1758{
1759 mon_fd_t *monfd;
1760
1761 LIST_FOREACH(monfd, &mon->fds, next) {
1762 if (strcmp(monfd->name, fdname) != 0) {
1763 continue;
1764 }
1765
1766 LIST_REMOVE(monfd, next);
1767 close(monfd->fd);
1768 qemu_free(monfd->name);
1769 qemu_free(monfd);
1770 return;
1771 }
1772
1773 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1774 fdname);
1775}
1776
aliguori376253e2009-03-05 23:01:23 +00001777static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001778#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001779 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001780};
1781
Blue Swirl23130862009-06-06 08:22:04 +00001782/* Please update qemu-monitor.hx when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001783static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001784 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001785 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001786 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001787 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001788 { "chardev", "", qemu_chr_info,
1789 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001790 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001791 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001792 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001793 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001794 { "registers", "", do_info_registers,
1795 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001796 { "cpus", "", do_info_cpus,
1797 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001798 { "history", "", do_info_history,
1799 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001800 { "irq", "", irq_info,
1801 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001802 { "pic", "", pic_info,
1803 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001804 { "pci", "", pci_info,
1805 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001806#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001807 { "tlb", "", tlb_info,
1808 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001809#endif
1810#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001811 { "mem", "", mem_info,
1812 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001813 { "hpet", "", do_info_hpet,
1814 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001815#endif
bellarde3db7222005-01-26 22:00:47 +00001816 { "jit", "", do_info_jit,
1817 "", "show dynamic compiler info", },
bellard0f4c6412005-07-24 18:10:56 +00001818 { "kqemu", "", do_info_kqemu,
blueswir1d2c639d2009-01-24 18:19:25 +00001819 "", "show KQEMU information", },
aliguori7ba1e612008-11-05 16:04:33 +00001820 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001821 "", "show KVM information", },
aliguori030ea372009-04-21 22:30:47 +00001822 { "numa", "", do_info_numa,
1823 "", "show NUMA information", },
bellarda594cfb2005-11-06 16:13:29 +00001824 { "usb", "", usb_info,
1825 "", "show guest USB devices", },
1826 { "usbhost", "", usb_host_info,
1827 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001828 { "profile", "", do_info_profile,
1829 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001830 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001831 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001832 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001833 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001834 { "status", "", do_info_status,
1835 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001836 { "pcmcia", "", pcmcia_info,
1837 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001838 { "mice", "", do_info_mice,
1839 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001840 { "vnc", "", do_info_vnc,
1841 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001842 { "name", "", do_info_name,
1843 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001844 { "uuid", "", do_info_uuid,
1845 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001846#if defined(TARGET_PPC)
1847 { "cpustats", "", do_info_cpu_stats,
1848 "", "show CPU statistics", },
1849#endif
blueswir131a60e22007-10-26 18:42:59 +00001850#if defined(CONFIG_SLIRP)
Jan Kiszka6dbe5532009-06-24 14:42:29 +02001851 { "usernet", "", do_info_usernet,
1852 "", "show user network stack connection states", },
blueswir131a60e22007-10-26 18:42:59 +00001853#endif
aliguori5bb79102008-10-13 03:12:02 +00001854 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001855 { "balloon", "", do_info_balloon,
1856 "", "show balloon information" },
Gerd Hoffmanncae49562009-06-05 15:53:17 +01001857 { "qtree", "", do_info_qtree,
1858 "", "show device tree" },
bellard9dc39cb2004-03-14 21:38:27 +00001859 { NULL, NULL, },
1860};
1861
bellard9307c4c2004-04-04 12:57:25 +00001862/*******************************************************************/
1863
1864static const char *pch;
1865static jmp_buf expr_env;
1866
bellard92a31b12005-02-10 22:00:52 +00001867#define MD_TLONG 0
1868#define MD_I32 1
1869
bellard9307c4c2004-04-04 12:57:25 +00001870typedef struct MonitorDef {
1871 const char *name;
1872 int offset;
blueswir18662d652008-10-02 18:32:44 +00001873 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001874 int type;
bellard9307c4c2004-04-04 12:57:25 +00001875} MonitorDef;
1876
bellard57206fd2004-04-25 18:54:52 +00001877#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001878static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001879{
bellard6a00d602005-11-21 23:25:50 +00001880 CPUState *env = mon_get_cpu();
1881 if (!env)
1882 return 0;
1883 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001884}
1885#endif
1886
bellarda541f292004-04-12 20:39:29 +00001887#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001888static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001889{
bellard6a00d602005-11-21 23:25:50 +00001890 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001891 unsigned int u;
1892 int i;
1893
bellard6a00d602005-11-21 23:25:50 +00001894 if (!env)
1895 return 0;
1896
bellarda541f292004-04-12 20:39:29 +00001897 u = 0;
1898 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00001899 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001900
1901 return u;
1902}
1903
blueswir18662d652008-10-02 18:32:44 +00001904static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001905{
bellard6a00d602005-11-21 23:25:50 +00001906 CPUState *env = mon_get_cpu();
1907 if (!env)
1908 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001909 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001910}
1911
blueswir18662d652008-10-02 18:32:44 +00001912static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001913{
bellard6a00d602005-11-21 23:25:50 +00001914 CPUState *env = mon_get_cpu();
1915 if (!env)
1916 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001917 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001918}
bellard9fddaa02004-05-21 12:59:32 +00001919
blueswir18662d652008-10-02 18:32:44 +00001920static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001921{
bellard6a00d602005-11-21 23:25:50 +00001922 CPUState *env = mon_get_cpu();
1923 if (!env)
1924 return 0;
1925 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001926}
1927
blueswir18662d652008-10-02 18:32:44 +00001928static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001929{
bellard6a00d602005-11-21 23:25:50 +00001930 CPUState *env = mon_get_cpu();
1931 if (!env)
1932 return 0;
1933 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001934}
1935
blueswir18662d652008-10-02 18:32:44 +00001936static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001937{
bellard6a00d602005-11-21 23:25:50 +00001938 CPUState *env = mon_get_cpu();
1939 if (!env)
1940 return 0;
1941 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001942}
bellarda541f292004-04-12 20:39:29 +00001943#endif
1944
bellarde95c8d52004-09-30 22:22:08 +00001945#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001946#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001947static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001948{
bellard6a00d602005-11-21 23:25:50 +00001949 CPUState *env = mon_get_cpu();
1950 if (!env)
1951 return 0;
1952 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001953}
bellard7b936c02005-10-30 17:05:13 +00001954#endif
bellarde95c8d52004-09-30 22:22:08 +00001955
blueswir18662d652008-10-02 18:32:44 +00001956static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001957{
bellard6a00d602005-11-21 23:25:50 +00001958 CPUState *env = mon_get_cpu();
1959 if (!env)
1960 return 0;
1961 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001962}
1963#endif
1964
blueswir18662d652008-10-02 18:32:44 +00001965static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001966#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001967
1968#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001969 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001970 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001971 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001972
bellard9307c4c2004-04-04 12:57:25 +00001973 { "eax", offsetof(CPUState, regs[0]) },
1974 { "ecx", offsetof(CPUState, regs[1]) },
1975 { "edx", offsetof(CPUState, regs[2]) },
1976 { "ebx", offsetof(CPUState, regs[3]) },
1977 { "esp|sp", offsetof(CPUState, regs[4]) },
1978 { "ebp|fp", offsetof(CPUState, regs[5]) },
1979 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001980 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001981#ifdef TARGET_X86_64
1982 { "r8", offsetof(CPUState, regs[8]) },
1983 { "r9", offsetof(CPUState, regs[9]) },
1984 { "r10", offsetof(CPUState, regs[10]) },
1985 { "r11", offsetof(CPUState, regs[11]) },
1986 { "r12", offsetof(CPUState, regs[12]) },
1987 { "r13", offsetof(CPUState, regs[13]) },
1988 { "r14", offsetof(CPUState, regs[14]) },
1989 { "r15", offsetof(CPUState, regs[15]) },
1990#endif
bellard9307c4c2004-04-04 12:57:25 +00001991 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001992 { "eip", offsetof(CPUState, eip) },
1993 SEG("cs", R_CS)
1994 SEG("ds", R_DS)
1995 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001996 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001997 SEG("fs", R_FS)
1998 SEG("gs", R_GS)
1999 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002000#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002001 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002002 { "r0", offsetof(CPUState, gpr[0]) },
2003 { "r1", offsetof(CPUState, gpr[1]) },
2004 { "r2", offsetof(CPUState, gpr[2]) },
2005 { "r3", offsetof(CPUState, gpr[3]) },
2006 { "r4", offsetof(CPUState, gpr[4]) },
2007 { "r5", offsetof(CPUState, gpr[5]) },
2008 { "r6", offsetof(CPUState, gpr[6]) },
2009 { "r7", offsetof(CPUState, gpr[7]) },
2010 { "r8", offsetof(CPUState, gpr[8]) },
2011 { "r9", offsetof(CPUState, gpr[9]) },
2012 { "r10", offsetof(CPUState, gpr[10]) },
2013 { "r11", offsetof(CPUState, gpr[11]) },
2014 { "r12", offsetof(CPUState, gpr[12]) },
2015 { "r13", offsetof(CPUState, gpr[13]) },
2016 { "r14", offsetof(CPUState, gpr[14]) },
2017 { "r15", offsetof(CPUState, gpr[15]) },
2018 { "r16", offsetof(CPUState, gpr[16]) },
2019 { "r17", offsetof(CPUState, gpr[17]) },
2020 { "r18", offsetof(CPUState, gpr[18]) },
2021 { "r19", offsetof(CPUState, gpr[19]) },
2022 { "r20", offsetof(CPUState, gpr[20]) },
2023 { "r21", offsetof(CPUState, gpr[21]) },
2024 { "r22", offsetof(CPUState, gpr[22]) },
2025 { "r23", offsetof(CPUState, gpr[23]) },
2026 { "r24", offsetof(CPUState, gpr[24]) },
2027 { "r25", offsetof(CPUState, gpr[25]) },
2028 { "r26", offsetof(CPUState, gpr[26]) },
2029 { "r27", offsetof(CPUState, gpr[27]) },
2030 { "r28", offsetof(CPUState, gpr[28]) },
2031 { "r29", offsetof(CPUState, gpr[29]) },
2032 { "r30", offsetof(CPUState, gpr[30]) },
2033 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002034 /* Floating point registers */
2035 { "f0", offsetof(CPUState, fpr[0]) },
2036 { "f1", offsetof(CPUState, fpr[1]) },
2037 { "f2", offsetof(CPUState, fpr[2]) },
2038 { "f3", offsetof(CPUState, fpr[3]) },
2039 { "f4", offsetof(CPUState, fpr[4]) },
2040 { "f5", offsetof(CPUState, fpr[5]) },
2041 { "f6", offsetof(CPUState, fpr[6]) },
2042 { "f7", offsetof(CPUState, fpr[7]) },
2043 { "f8", offsetof(CPUState, fpr[8]) },
2044 { "f9", offsetof(CPUState, fpr[9]) },
2045 { "f10", offsetof(CPUState, fpr[10]) },
2046 { "f11", offsetof(CPUState, fpr[11]) },
2047 { "f12", offsetof(CPUState, fpr[12]) },
2048 { "f13", offsetof(CPUState, fpr[13]) },
2049 { "f14", offsetof(CPUState, fpr[14]) },
2050 { "f15", offsetof(CPUState, fpr[15]) },
2051 { "f16", offsetof(CPUState, fpr[16]) },
2052 { "f17", offsetof(CPUState, fpr[17]) },
2053 { "f18", offsetof(CPUState, fpr[18]) },
2054 { "f19", offsetof(CPUState, fpr[19]) },
2055 { "f20", offsetof(CPUState, fpr[20]) },
2056 { "f21", offsetof(CPUState, fpr[21]) },
2057 { "f22", offsetof(CPUState, fpr[22]) },
2058 { "f23", offsetof(CPUState, fpr[23]) },
2059 { "f24", offsetof(CPUState, fpr[24]) },
2060 { "f25", offsetof(CPUState, fpr[25]) },
2061 { "f26", offsetof(CPUState, fpr[26]) },
2062 { "f27", offsetof(CPUState, fpr[27]) },
2063 { "f28", offsetof(CPUState, fpr[28]) },
2064 { "f29", offsetof(CPUState, fpr[29]) },
2065 { "f30", offsetof(CPUState, fpr[30]) },
2066 { "f31", offsetof(CPUState, fpr[31]) },
2067 { "fpscr", offsetof(CPUState, fpscr) },
2068 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002069 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002070 { "lr", offsetof(CPUState, lr) },
2071 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002072 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002073 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002074 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002075 { "msr", 0, &monitor_get_msr, },
2076 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002077 { "tbu", 0, &monitor_get_tbu, },
2078 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002079#if defined(TARGET_PPC64)
2080 /* Address space register */
2081 { "asr", offsetof(CPUState, asr) },
2082#endif
2083 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002084 { "sdr1", offsetof(CPUState, sdr1) },
2085 { "sr0", offsetof(CPUState, sr[0]) },
2086 { "sr1", offsetof(CPUState, sr[1]) },
2087 { "sr2", offsetof(CPUState, sr[2]) },
2088 { "sr3", offsetof(CPUState, sr[3]) },
2089 { "sr4", offsetof(CPUState, sr[4]) },
2090 { "sr5", offsetof(CPUState, sr[5]) },
2091 { "sr6", offsetof(CPUState, sr[6]) },
2092 { "sr7", offsetof(CPUState, sr[7]) },
2093 { "sr8", offsetof(CPUState, sr[8]) },
2094 { "sr9", offsetof(CPUState, sr[9]) },
2095 { "sr10", offsetof(CPUState, sr[10]) },
2096 { "sr11", offsetof(CPUState, sr[11]) },
2097 { "sr12", offsetof(CPUState, sr[12]) },
2098 { "sr13", offsetof(CPUState, sr[13]) },
2099 { "sr14", offsetof(CPUState, sr[14]) },
2100 { "sr15", offsetof(CPUState, sr[15]) },
2101 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002102#elif defined(TARGET_SPARC)
2103 { "g0", offsetof(CPUState, gregs[0]) },
2104 { "g1", offsetof(CPUState, gregs[1]) },
2105 { "g2", offsetof(CPUState, gregs[2]) },
2106 { "g3", offsetof(CPUState, gregs[3]) },
2107 { "g4", offsetof(CPUState, gregs[4]) },
2108 { "g5", offsetof(CPUState, gregs[5]) },
2109 { "g6", offsetof(CPUState, gregs[6]) },
2110 { "g7", offsetof(CPUState, gregs[7]) },
2111 { "o0", 0, monitor_get_reg },
2112 { "o1", 1, monitor_get_reg },
2113 { "o2", 2, monitor_get_reg },
2114 { "o3", 3, monitor_get_reg },
2115 { "o4", 4, monitor_get_reg },
2116 { "o5", 5, monitor_get_reg },
2117 { "o6", 6, monitor_get_reg },
2118 { "o7", 7, monitor_get_reg },
2119 { "l0", 8, monitor_get_reg },
2120 { "l1", 9, monitor_get_reg },
2121 { "l2", 10, monitor_get_reg },
2122 { "l3", 11, monitor_get_reg },
2123 { "l4", 12, monitor_get_reg },
2124 { "l5", 13, monitor_get_reg },
2125 { "l6", 14, monitor_get_reg },
2126 { "l7", 15, monitor_get_reg },
2127 { "i0", 16, monitor_get_reg },
2128 { "i1", 17, monitor_get_reg },
2129 { "i2", 18, monitor_get_reg },
2130 { "i3", 19, monitor_get_reg },
2131 { "i4", 20, monitor_get_reg },
2132 { "i5", 21, monitor_get_reg },
2133 { "i6", 22, monitor_get_reg },
2134 { "i7", 23, monitor_get_reg },
2135 { "pc", offsetof(CPUState, pc) },
2136 { "npc", offsetof(CPUState, npc) },
2137 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002138#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002139 { "psr", 0, &monitor_get_psr, },
2140 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002141#endif
bellarde95c8d52004-09-30 22:22:08 +00002142 { "tbr", offsetof(CPUState, tbr) },
2143 { "fsr", offsetof(CPUState, fsr) },
2144 { "f0", offsetof(CPUState, fpr[0]) },
2145 { "f1", offsetof(CPUState, fpr[1]) },
2146 { "f2", offsetof(CPUState, fpr[2]) },
2147 { "f3", offsetof(CPUState, fpr[3]) },
2148 { "f4", offsetof(CPUState, fpr[4]) },
2149 { "f5", offsetof(CPUState, fpr[5]) },
2150 { "f6", offsetof(CPUState, fpr[6]) },
2151 { "f7", offsetof(CPUState, fpr[7]) },
2152 { "f8", offsetof(CPUState, fpr[8]) },
2153 { "f9", offsetof(CPUState, fpr[9]) },
2154 { "f10", offsetof(CPUState, fpr[10]) },
2155 { "f11", offsetof(CPUState, fpr[11]) },
2156 { "f12", offsetof(CPUState, fpr[12]) },
2157 { "f13", offsetof(CPUState, fpr[13]) },
2158 { "f14", offsetof(CPUState, fpr[14]) },
2159 { "f15", offsetof(CPUState, fpr[15]) },
2160 { "f16", offsetof(CPUState, fpr[16]) },
2161 { "f17", offsetof(CPUState, fpr[17]) },
2162 { "f18", offsetof(CPUState, fpr[18]) },
2163 { "f19", offsetof(CPUState, fpr[19]) },
2164 { "f20", offsetof(CPUState, fpr[20]) },
2165 { "f21", offsetof(CPUState, fpr[21]) },
2166 { "f22", offsetof(CPUState, fpr[22]) },
2167 { "f23", offsetof(CPUState, fpr[23]) },
2168 { "f24", offsetof(CPUState, fpr[24]) },
2169 { "f25", offsetof(CPUState, fpr[25]) },
2170 { "f26", offsetof(CPUState, fpr[26]) },
2171 { "f27", offsetof(CPUState, fpr[27]) },
2172 { "f28", offsetof(CPUState, fpr[28]) },
2173 { "f29", offsetof(CPUState, fpr[29]) },
2174 { "f30", offsetof(CPUState, fpr[30]) },
2175 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002176#ifdef TARGET_SPARC64
2177 { "f32", offsetof(CPUState, fpr[32]) },
2178 { "f34", offsetof(CPUState, fpr[34]) },
2179 { "f36", offsetof(CPUState, fpr[36]) },
2180 { "f38", offsetof(CPUState, fpr[38]) },
2181 { "f40", offsetof(CPUState, fpr[40]) },
2182 { "f42", offsetof(CPUState, fpr[42]) },
2183 { "f44", offsetof(CPUState, fpr[44]) },
2184 { "f46", offsetof(CPUState, fpr[46]) },
2185 { "f48", offsetof(CPUState, fpr[48]) },
2186 { "f50", offsetof(CPUState, fpr[50]) },
2187 { "f52", offsetof(CPUState, fpr[52]) },
2188 { "f54", offsetof(CPUState, fpr[54]) },
2189 { "f56", offsetof(CPUState, fpr[56]) },
2190 { "f58", offsetof(CPUState, fpr[58]) },
2191 { "f60", offsetof(CPUState, fpr[60]) },
2192 { "f62", offsetof(CPUState, fpr[62]) },
2193 { "asi", offsetof(CPUState, asi) },
2194 { "pstate", offsetof(CPUState, pstate) },
2195 { "cansave", offsetof(CPUState, cansave) },
2196 { "canrestore", offsetof(CPUState, canrestore) },
2197 { "otherwin", offsetof(CPUState, otherwin) },
2198 { "wstate", offsetof(CPUState, wstate) },
2199 { "cleanwin", offsetof(CPUState, cleanwin) },
2200 { "fprs", offsetof(CPUState, fprs) },
2201#endif
bellard9307c4c2004-04-04 12:57:25 +00002202#endif
2203 { NULL },
2204};
2205
aliguori376253e2009-03-05 23:01:23 +00002206static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002207{
aliguori376253e2009-03-05 23:01:23 +00002208 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002209 longjmp(expr_env, 1);
2210}
2211
bellard6a00d602005-11-21 23:25:50 +00002212/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002213static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002214{
blueswir18662d652008-10-02 18:32:44 +00002215 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002216 void *ptr;
2217
bellard9307c4c2004-04-04 12:57:25 +00002218 for(md = monitor_defs; md->name != NULL; md++) {
2219 if (compare_cmd(name, md->name)) {
2220 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002221 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002222 } else {
bellard6a00d602005-11-21 23:25:50 +00002223 CPUState *env = mon_get_cpu();
2224 if (!env)
2225 return -2;
2226 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002227 switch(md->type) {
2228 case MD_I32:
2229 *pval = *(int32_t *)ptr;
2230 break;
2231 case MD_TLONG:
2232 *pval = *(target_long *)ptr;
2233 break;
2234 default:
2235 *pval = 0;
2236 break;
2237 }
bellard9307c4c2004-04-04 12:57:25 +00002238 }
2239 return 0;
2240 }
2241 }
2242 return -1;
2243}
2244
2245static void next(void)
2246{
2247 if (pch != '\0') {
2248 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002249 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002250 pch++;
2251 }
2252}
2253
aliguori376253e2009-03-05 23:01:23 +00002254static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002255
aliguori376253e2009-03-05 23:01:23 +00002256static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002257{
blueswir1c2efc952007-09-25 17:28:42 +00002258 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002259 char *p;
bellard6a00d602005-11-21 23:25:50 +00002260 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002261
2262 switch(*pch) {
2263 case '+':
2264 next();
aliguori376253e2009-03-05 23:01:23 +00002265 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002266 break;
2267 case '-':
2268 next();
aliguori376253e2009-03-05 23:01:23 +00002269 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002270 break;
2271 case '~':
2272 next();
aliguori376253e2009-03-05 23:01:23 +00002273 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002274 break;
2275 case '(':
2276 next();
aliguori376253e2009-03-05 23:01:23 +00002277 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002278 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002279 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002280 }
2281 next();
2282 break;
bellard81d09122004-07-14 17:21:37 +00002283 case '\'':
2284 pch++;
2285 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002286 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002287 n = *pch;
2288 pch++;
2289 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002290 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002291 next();
2292 break;
bellard9307c4c2004-04-04 12:57:25 +00002293 case '$':
2294 {
2295 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002296 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002297
bellard9307c4c2004-04-04 12:57:25 +00002298 pch++;
2299 q = buf;
2300 while ((*pch >= 'a' && *pch <= 'z') ||
2301 (*pch >= 'A' && *pch <= 'Z') ||
2302 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002303 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002304 if ((q - buf) < sizeof(buf) - 1)
2305 *q++ = *pch;
2306 pch++;
2307 }
blueswir1cd390082008-11-16 13:53:32 +00002308 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002309 pch++;
2310 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002311 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002312 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002313 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002314 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002315 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002316 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002317 }
2318 break;
2319 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002320 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002321 n = 0;
2322 break;
2323 default:
blueswir17743e582007-09-24 18:39:04 +00002324#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002325 n = strtoull(pch, &p, 0);
2326#else
bellard9307c4c2004-04-04 12:57:25 +00002327 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002328#endif
bellard9307c4c2004-04-04 12:57:25 +00002329 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002330 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002331 }
2332 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002333 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002334 pch++;
2335 break;
2336 }
2337 return n;
2338}
2339
2340
aliguori376253e2009-03-05 23:01:23 +00002341static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002342{
blueswir1c2efc952007-09-25 17:28:42 +00002343 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002344 int op;
ths3b46e622007-09-17 08:09:54 +00002345
aliguori376253e2009-03-05 23:01:23 +00002346 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002347 for(;;) {
2348 op = *pch;
2349 if (op != '*' && op != '/' && op != '%')
2350 break;
2351 next();
aliguori376253e2009-03-05 23:01:23 +00002352 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002353 switch(op) {
2354 default:
2355 case '*':
2356 val *= val2;
2357 break;
2358 case '/':
2359 case '%':
ths5fafdf22007-09-16 21:08:06 +00002360 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002361 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002362 if (op == '/')
2363 val /= val2;
2364 else
2365 val %= val2;
2366 break;
2367 }
2368 }
2369 return val;
2370}
2371
aliguori376253e2009-03-05 23:01:23 +00002372static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002373{
blueswir1c2efc952007-09-25 17:28:42 +00002374 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002375 int op;
bellard9307c4c2004-04-04 12:57:25 +00002376
aliguori376253e2009-03-05 23:01:23 +00002377 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002378 for(;;) {
2379 op = *pch;
2380 if (op != '&' && op != '|' && op != '^')
2381 break;
2382 next();
aliguori376253e2009-03-05 23:01:23 +00002383 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002384 switch(op) {
2385 default:
2386 case '&':
2387 val &= val2;
2388 break;
2389 case '|':
2390 val |= val2;
2391 break;
2392 case '^':
2393 val ^= val2;
2394 break;
2395 }
2396 }
2397 return val;
2398}
2399
aliguori376253e2009-03-05 23:01:23 +00002400static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002401{
blueswir1c2efc952007-09-25 17:28:42 +00002402 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002403 int op;
bellard9307c4c2004-04-04 12:57:25 +00002404
aliguori376253e2009-03-05 23:01:23 +00002405 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002406 for(;;) {
2407 op = *pch;
2408 if (op != '+' && op != '-')
2409 break;
2410 next();
aliguori376253e2009-03-05 23:01:23 +00002411 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002412 if (op == '+')
2413 val += val2;
2414 else
2415 val -= val2;
2416 }
2417 return val;
2418}
2419
aliguori376253e2009-03-05 23:01:23 +00002420static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002421{
2422 pch = *pp;
2423 if (setjmp(expr_env)) {
2424 *pp = pch;
2425 return -1;
2426 }
blueswir1cd390082008-11-16 13:53:32 +00002427 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002428 pch++;
aliguori376253e2009-03-05 23:01:23 +00002429 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002430 *pp = pch;
2431 return 0;
2432}
2433
2434static int get_str(char *buf, int buf_size, const char **pp)
2435{
2436 const char *p;
2437 char *q;
2438 int c;
2439
bellard81d09122004-07-14 17:21:37 +00002440 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002441 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002442 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002443 p++;
2444 if (*p == '\0') {
2445 fail:
bellard81d09122004-07-14 17:21:37 +00002446 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002447 *pp = p;
2448 return -1;
2449 }
bellard9307c4c2004-04-04 12:57:25 +00002450 if (*p == '\"') {
2451 p++;
2452 while (*p != '\0' && *p != '\"') {
2453 if (*p == '\\') {
2454 p++;
2455 c = *p++;
2456 switch(c) {
2457 case 'n':
2458 c = '\n';
2459 break;
2460 case 'r':
2461 c = '\r';
2462 break;
2463 case '\\':
2464 case '\'':
2465 case '\"':
2466 break;
2467 default:
2468 qemu_printf("unsupported escape code: '\\%c'\n", c);
2469 goto fail;
2470 }
2471 if ((q - buf) < buf_size - 1) {
2472 *q++ = c;
2473 }
2474 } else {
2475 if ((q - buf) < buf_size - 1) {
2476 *q++ = *p;
2477 }
2478 p++;
2479 }
2480 }
2481 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002482 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002483 goto fail;
2484 }
2485 p++;
2486 } else {
blueswir1cd390082008-11-16 13:53:32 +00002487 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002488 if ((q - buf) < buf_size - 1) {
2489 *q++ = *p;
2490 }
2491 p++;
2492 }
bellard9307c4c2004-04-04 12:57:25 +00002493 }
bellard81d09122004-07-14 17:21:37 +00002494 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002495 *pp = p;
2496 return 0;
2497}
2498
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002499/*
2500 * Store the command-name in cmdname, and return a pointer to
2501 * the remaining of the command string.
2502 */
2503static const char *get_command_name(const char *cmdline,
2504 char *cmdname, size_t nlen)
2505{
2506 size_t len;
2507 const char *p, *pstart;
2508
2509 p = cmdline;
2510 while (qemu_isspace(*p))
2511 p++;
2512 if (*p == '\0')
2513 return NULL;
2514 pstart = p;
2515 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2516 p++;
2517 len = p - pstart;
2518 if (len > nlen - 1)
2519 len = nlen - 1;
2520 memcpy(cmdname, pstart, len);
2521 cmdname[len] = '\0';
2522 return p;
2523}
2524
bellard9307c4c2004-04-04 12:57:25 +00002525static int default_fmt_format = 'x';
2526static int default_fmt_size = 4;
2527
2528#define MAX_ARGS 16
2529
aliguori376253e2009-03-05 23:01:23 +00002530static void monitor_handle_command(Monitor *mon, const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002531{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002532 const char *p, *typestr;
2533 int c, nb_args, i, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002534 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002535 char cmdname[256];
2536 char buf[1024];
2537 void *str_allocated[MAX_ARGS];
2538 void *args[MAX_ARGS];
aliguori376253e2009-03-05 23:01:23 +00002539 void (*handler_0)(Monitor *mon);
2540 void (*handler_1)(Monitor *mon, void *arg0);
2541 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2542 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2543 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2544 void *arg3);
2545 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2546 void *arg3, void *arg4);
2547 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2548 void *arg3, void *arg4, void *arg5);
2549 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2550 void *arg3, void *arg4, void *arg5, void *arg6);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002551 void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2552 void *arg3, void *arg4, void *arg5, void *arg6,
2553 void *arg7);
2554 void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2555 void *arg3, void *arg4, void *arg5, void *arg6,
2556 void *arg7, void *arg8);
2557 void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2558 void *arg3, void *arg4, void *arg5, void *arg6,
2559 void *arg7, void *arg8, void *arg9);
bellard9dc39cb2004-03-14 21:38:27 +00002560
2561#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002562 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002563#endif
ths3b46e622007-09-17 08:09:54 +00002564
bellard9307c4c2004-04-04 12:57:25 +00002565 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002566 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2567 if (!p)
bellard9dc39cb2004-03-14 21:38:27 +00002568 return;
ths3b46e622007-09-17 08:09:54 +00002569
bellard9307c4c2004-04-04 12:57:25 +00002570 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002571 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002572 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002573 break;
bellard9dc39cb2004-03-14 21:38:27 +00002574 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002575
2576 if (cmd->name == NULL) {
2577 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2578 return;
2579 }
bellard9307c4c2004-04-04 12:57:25 +00002580
2581 for(i = 0; i < MAX_ARGS; i++)
2582 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002583
bellard9307c4c2004-04-04 12:57:25 +00002584 /* parse the parameters */
2585 typestr = cmd->args_type;
2586 nb_args = 0;
2587 for(;;) {
2588 c = *typestr;
2589 if (c == '\0')
2590 break;
2591 typestr++;
2592 switch(c) {
2593 case 'F':
bellard81d09122004-07-14 17:21:37 +00002594 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002595 case 's':
2596 {
2597 int ret;
2598 char *str;
ths3b46e622007-09-17 08:09:54 +00002599
blueswir1cd390082008-11-16 13:53:32 +00002600 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002601 p++;
2602 if (*typestr == '?') {
2603 typestr++;
2604 if (*p == '\0') {
2605 /* no optional string: NULL argument */
2606 str = NULL;
2607 goto add_str;
2608 }
2609 }
2610 ret = get_str(buf, sizeof(buf), &p);
2611 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002612 switch(c) {
2613 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002614 monitor_printf(mon, "%s: filename expected\n",
2615 cmdname);
bellard81d09122004-07-14 17:21:37 +00002616 break;
2617 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002618 monitor_printf(mon, "%s: block device name expected\n",
2619 cmdname);
bellard81d09122004-07-14 17:21:37 +00002620 break;
2621 default:
aliguori376253e2009-03-05 23:01:23 +00002622 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002623 break;
2624 }
bellard9307c4c2004-04-04 12:57:25 +00002625 goto fail;
2626 }
2627 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002628 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002629 str_allocated[nb_args] = str;
2630 add_str:
2631 if (nb_args >= MAX_ARGS) {
2632 error_args:
aliguori376253e2009-03-05 23:01:23 +00002633 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002634 goto fail;
2635 }
2636 args[nb_args++] = str;
2637 }
2638 break;
2639 case '/':
2640 {
2641 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002642
blueswir1cd390082008-11-16 13:53:32 +00002643 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002644 p++;
2645 if (*p == '/') {
2646 /* format found */
2647 p++;
2648 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002649 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002650 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002651 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002652 count = count * 10 + (*p - '0');
2653 p++;
2654 }
2655 }
2656 size = -1;
2657 format = -1;
2658 for(;;) {
2659 switch(*p) {
2660 case 'o':
2661 case 'd':
2662 case 'u':
2663 case 'x':
2664 case 'i':
2665 case 'c':
2666 format = *p++;
2667 break;
2668 case 'b':
2669 size = 1;
2670 p++;
2671 break;
2672 case 'h':
2673 size = 2;
2674 p++;
2675 break;
2676 case 'w':
2677 size = 4;
2678 p++;
2679 break;
2680 case 'g':
2681 case 'L':
2682 size = 8;
2683 p++;
2684 break;
2685 default:
2686 goto next;
2687 }
2688 }
2689 next:
blueswir1cd390082008-11-16 13:53:32 +00002690 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002691 monitor_printf(mon, "invalid char in format: '%c'\n",
2692 *p);
bellard9307c4c2004-04-04 12:57:25 +00002693 goto fail;
2694 }
bellard9307c4c2004-04-04 12:57:25 +00002695 if (format < 0)
2696 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002697 if (format != 'i') {
2698 /* for 'i', not specifying a size gives -1 as size */
2699 if (size < 0)
2700 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002701 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002702 }
bellard9307c4c2004-04-04 12:57:25 +00002703 default_fmt_format = format;
2704 } else {
2705 count = 1;
2706 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002707 if (format != 'i') {
2708 size = default_fmt_size;
2709 } else {
2710 size = -1;
2711 }
bellard9307c4c2004-04-04 12:57:25 +00002712 }
2713 if (nb_args + 3 > MAX_ARGS)
2714 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002715 args[nb_args++] = (void*)(long)count;
2716 args[nb_args++] = (void*)(long)format;
2717 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002718 }
2719 break;
2720 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002721 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002722 {
blueswir1c2efc952007-09-25 17:28:42 +00002723 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002724
blueswir1cd390082008-11-16 13:53:32 +00002725 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002726 p++;
bellard34405572004-06-08 00:55:58 +00002727 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002728 if (*typestr == '?') {
2729 if (*p == '\0')
2730 has_arg = 0;
2731 else
2732 has_arg = 1;
2733 } else {
2734 if (*p == '.') {
2735 p++;
blueswir1cd390082008-11-16 13:53:32 +00002736 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002737 p++;
2738 has_arg = 1;
2739 } else {
2740 has_arg = 0;
2741 }
2742 }
bellard13224a82006-07-14 22:03:35 +00002743 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002744 if (nb_args >= MAX_ARGS)
2745 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002746 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002747 if (!has_arg) {
2748 if (nb_args >= MAX_ARGS)
2749 goto error_args;
2750 val = -1;
2751 goto add_num;
2752 }
2753 }
aliguori376253e2009-03-05 23:01:23 +00002754 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002755 goto fail;
2756 add_num:
bellard92a31b12005-02-10 22:00:52 +00002757 if (c == 'i') {
2758 if (nb_args >= MAX_ARGS)
2759 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002760 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002761 } else {
2762 if ((nb_args + 1) >= MAX_ARGS)
2763 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002764#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002765 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002766#else
2767 args[nb_args++] = (void *)0;
2768#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002769 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002770 }
bellard9307c4c2004-04-04 12:57:25 +00002771 }
2772 break;
2773 case '-':
2774 {
2775 int has_option;
2776 /* option */
ths3b46e622007-09-17 08:09:54 +00002777
bellard9307c4c2004-04-04 12:57:25 +00002778 c = *typestr++;
2779 if (c == '\0')
2780 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002781 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002782 p++;
2783 has_option = 0;
2784 if (*p == '-') {
2785 p++;
2786 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002787 monitor_printf(mon, "%s: unsupported option -%c\n",
2788 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002789 goto fail;
2790 }
2791 p++;
2792 has_option = 1;
2793 }
2794 if (nb_args >= MAX_ARGS)
2795 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002796 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002797 }
2798 break;
2799 default:
2800 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002801 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002802 goto fail;
2803 }
2804 }
2805 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002806 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002807 p++;
2808 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002809 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2810 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002811 goto fail;
2812 }
2813
2814 switch(nb_args) {
2815 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002816 handler_0 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002817 handler_0(mon);
bellard9307c4c2004-04-04 12:57:25 +00002818 break;
2819 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002820 handler_1 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002821 handler_1(mon, args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002822 break;
2823 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002824 handler_2 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002825 handler_2(mon, args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002826 break;
2827 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002828 handler_3 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002829 handler_3(mon, args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002830 break;
2831 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002832 handler_4 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002833 handler_4(mon, args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002834 break;
2835 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002836 handler_5 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002837 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002838 break;
bellard34405572004-06-08 00:55:58 +00002839 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002840 handler_6 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002841 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002842 break;
bellardec36b692006-07-16 18:57:03 +00002843 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002844 handler_7 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002845 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2846 args[6]);
bellardec36b692006-07-16 18:57:03 +00002847 break;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002848 case 8:
2849 handler_8 = cmd->handler;
2850 handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2851 args[6], args[7]);
2852 break;
2853 case 9:
2854 handler_9 = cmd->handler;
2855 handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2856 args[6], args[7], args[8]);
2857 break;
2858 case 10:
2859 handler_10 = cmd->handler;
2860 handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2861 args[6], args[7], args[8], args[9]);
2862 break;
bellard9307c4c2004-04-04 12:57:25 +00002863 default:
aliguori376253e2009-03-05 23:01:23 +00002864 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
bellard9307c4c2004-04-04 12:57:25 +00002865 goto fail;
2866 }
2867 fail:
2868 for(i = 0; i < MAX_ARGS; i++)
2869 qemu_free(str_allocated[i]);
bellard9dc39cb2004-03-14 21:38:27 +00002870}
2871
bellard81d09122004-07-14 17:21:37 +00002872static void cmd_completion(const char *name, const char *list)
2873{
2874 const char *p, *pstart;
2875 char cmd[128];
2876 int len;
2877
2878 p = list;
2879 for(;;) {
2880 pstart = p;
2881 p = strchr(p, '|');
2882 if (!p)
2883 p = pstart + strlen(pstart);
2884 len = p - pstart;
2885 if (len > sizeof(cmd) - 2)
2886 len = sizeof(cmd) - 2;
2887 memcpy(cmd, pstart, len);
2888 cmd[len] = '\0';
2889 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002890 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002891 }
2892 if (*p == '\0')
2893 break;
2894 p++;
2895 }
2896}
2897
2898static void file_completion(const char *input)
2899{
2900 DIR *ffs;
2901 struct dirent *d;
2902 char path[1024];
2903 char file[1024], file_prefix[1024];
2904 int input_path_len;
2905 const char *p;
2906
ths5fafdf22007-09-16 21:08:06 +00002907 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002908 if (!p) {
2909 input_path_len = 0;
2910 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002911 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002912 } else {
2913 input_path_len = p - input + 1;
2914 memcpy(path, input, input_path_len);
2915 if (input_path_len > sizeof(path) - 1)
2916 input_path_len = sizeof(path) - 1;
2917 path[input_path_len] = '\0';
2918 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2919 }
2920#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002921 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2922 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002923#endif
2924 ffs = opendir(path);
2925 if (!ffs)
2926 return;
2927 for(;;) {
2928 struct stat sb;
2929 d = readdir(ffs);
2930 if (!d)
2931 break;
2932 if (strstart(d->d_name, file_prefix, NULL)) {
2933 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002934 if (input_path_len < sizeof(file))
2935 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2936 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002937 /* stat the file to find out if it's a directory.
2938 * In that case add a slash to speed up typing long paths
2939 */
2940 stat(file, &sb);
2941 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002942 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00002943 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002944 }
2945 }
2946 closedir(ffs);
2947}
2948
aliguori51de9762009-03-05 23:00:43 +00002949static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002950{
aliguori51de9762009-03-05 23:00:43 +00002951 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002952 const char *input = opaque;
2953
2954 if (input[0] == '\0' ||
2955 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00002956 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00002957 }
2958}
2959
2960/* NOTE: this parser is an approximate form of the real command parser */
2961static void parse_cmdline(const char *cmdline,
2962 int *pnb_args, char **args)
2963{
2964 const char *p;
2965 int nb_args, ret;
2966 char buf[1024];
2967
2968 p = cmdline;
2969 nb_args = 0;
2970 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002971 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002972 p++;
2973 if (*p == '\0')
2974 break;
2975 if (nb_args >= MAX_ARGS)
2976 break;
2977 ret = get_str(buf, sizeof(buf), &p);
2978 args[nb_args] = qemu_strdup(buf);
2979 nb_args++;
2980 if (ret < 0)
2981 break;
2982 }
2983 *pnb_args = nb_args;
2984}
2985
aliguori4c36ba32009-03-05 23:01:37 +00002986static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002987{
2988 const char *cmdname;
2989 char *args[MAX_ARGS];
2990 int nb_args, i, len;
2991 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00002992 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002993 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002994
2995 parse_cmdline(cmdline, &nb_args, args);
2996#ifdef DEBUG_COMPLETION
2997 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00002998 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00002999 }
3000#endif
3001
3002 /* if the line ends with a space, it means we want to complete the
3003 next arg */
3004 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003005 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003006 if (nb_args >= MAX_ARGS)
3007 return;
3008 args[nb_args++] = qemu_strdup("");
3009 }
3010 if (nb_args <= 1) {
3011 /* command completion */
3012 if (nb_args == 0)
3013 cmdname = "";
3014 else
3015 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003016 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003017 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003018 cmd_completion(cmdname, cmd->name);
3019 }
3020 } else {
3021 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003022 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003023 if (compare_cmd(args[0], cmd->name))
3024 goto found;
3025 }
3026 return;
3027 found:
3028 ptype = cmd->args_type;
3029 for(i = 0; i < nb_args - 2; i++) {
3030 if (*ptype != '\0') {
3031 ptype++;
3032 while (*ptype == '?')
3033 ptype++;
3034 }
3035 }
3036 str = args[nb_args - 1];
3037 switch(*ptype) {
3038 case 'F':
3039 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003040 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003041 file_completion(str);
3042 break;
3043 case 'B':
3044 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003045 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003046 bdrv_iterate(block_completion_it, (void *)str);
3047 break;
bellard7fe48482004-10-09 18:08:01 +00003048 case 's':
3049 /* XXX: more generic ? */
3050 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003051 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003052 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3053 cmd_completion(str, cmd->name);
3054 }
bellard64866c32006-05-07 18:03:31 +00003055 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003056 char *sep = strrchr(str, '-');
3057 if (sep)
3058 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003059 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003060 for(key = key_defs; key->name != NULL; key++) {
3061 cmd_completion(str, key->name);
3062 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003063 } else if (!strcmp(cmd->name, "help|?")) {
3064 readline_set_completion_index(cur_mon->rs, strlen(str));
3065 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3066 cmd_completion(str, cmd->name);
3067 }
bellard7fe48482004-10-09 18:08:01 +00003068 }
3069 break;
bellard81d09122004-07-14 17:21:37 +00003070 default:
3071 break;
3072 }
3073 }
3074 for(i = 0; i < nb_args; i++)
3075 qemu_free(args[i]);
3076}
3077
aliguori731b0362009-03-05 23:01:42 +00003078static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003079{
aliguori731b0362009-03-05 23:01:42 +00003080 Monitor *mon = opaque;
3081
3082 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003083}
3084
aliguori731b0362009-03-05 23:01:42 +00003085static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003086{
aliguori731b0362009-03-05 23:01:42 +00003087 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003088 int i;
aliguori376253e2009-03-05 23:01:23 +00003089
aliguori731b0362009-03-05 23:01:42 +00003090 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003091
aliguoricde76ee2009-03-05 23:01:51 +00003092 if (cur_mon->rs) {
3093 for (i = 0; i < size; i++)
3094 readline_handle_byte(cur_mon->rs, buf[i]);
3095 } else {
3096 if (size == 0 || buf[size - 1] != 0)
3097 monitor_printf(cur_mon, "corrupted command\n");
3098 else
3099 monitor_handle_command(cur_mon, (char *)buf);
3100 }
aliguori731b0362009-03-05 23:01:42 +00003101
3102 cur_mon = old_mon;
3103}
aliguorid8f44602008-10-06 13:52:44 +00003104
aliguori376253e2009-03-05 23:01:23 +00003105static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003106{
aliguori731b0362009-03-05 23:01:42 +00003107 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003108 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003109 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003110}
3111
aliguoricde76ee2009-03-05 23:01:51 +00003112int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003113{
aliguoricde76ee2009-03-05 23:01:51 +00003114 if (!mon->rs)
3115 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003116 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003117 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003118}
3119
aliguori376253e2009-03-05 23:01:23 +00003120void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003121{
aliguoricde76ee2009-03-05 23:01:51 +00003122 if (!mon->rs)
3123 return;
aliguori731b0362009-03-05 23:01:42 +00003124 if (--mon->suspend_cnt == 0)
3125 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003126}
3127
aliguori731b0362009-03-05 23:01:42 +00003128static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003129{
aliguori376253e2009-03-05 23:01:23 +00003130 Monitor *mon = opaque;
3131
aliguori2724b182009-03-05 23:01:47 +00003132 switch (event) {
3133 case CHR_EVENT_MUX_IN:
3134 readline_restart(mon->rs);
3135 monitor_resume(mon);
3136 monitor_flush(mon);
3137 break;
ths86e94de2007-01-05 22:01:59 +00003138
aliguori2724b182009-03-05 23:01:47 +00003139 case CHR_EVENT_MUX_OUT:
3140 if (mon->suspend_cnt == 0)
3141 monitor_printf(mon, "\n");
3142 monitor_flush(mon);
3143 monitor_suspend(mon);
3144 break;
3145
3146 case CHR_EVENT_RESET:
3147 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3148 "information\n", QEMU_VERSION);
3149 if (mon->chr->focus == 0)
3150 readline_show_prompt(mon->rs);
3151 break;
3152 }
ths86e94de2007-01-05 22:01:59 +00003153}
3154
aliguori76655d62009-03-06 20:27:37 +00003155
3156/*
3157 * Local variables:
3158 * c-indent-level: 4
3159 * c-basic-offset: 4
3160 * tab-width: 8
3161 * End:
3162 */
3163
aliguori731b0362009-03-05 23:01:42 +00003164void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003165{
aliguori731b0362009-03-05 23:01:42 +00003166 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003167 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003168
3169 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003170 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003171 is_first_init = 0;
3172 }
aliguori87127162009-03-05 23:01:29 +00003173
3174 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003175
aliguori87127162009-03-05 23:01:29 +00003176 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003177 mon->flags = flags;
aliguori2724b182009-03-05 23:01:47 +00003178 if (mon->chr->focus != 0)
3179 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
aliguoricde76ee2009-03-05 23:01:51 +00003180 if (flags & MONITOR_USE_READLINE) {
3181 mon->rs = readline_init(mon, monitor_find_completion);
3182 monitor_read_command(mon, 0);
3183 }
aliguori87127162009-03-05 23:01:29 +00003184
aliguori731b0362009-03-05 23:01:42 +00003185 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3186 mon);
aliguori87127162009-03-05 23:01:29 +00003187
3188 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003189 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003190 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003191}
3192
aliguori376253e2009-03-05 23:01:23 +00003193static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003194{
aliguoribb5fc202009-03-05 23:01:15 +00003195 BlockDriverState *bs = opaque;
3196 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003197
aliguoribb5fc202009-03-05 23:01:15 +00003198 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003199 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003200 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003201 }
aliguori731b0362009-03-05 23:01:42 +00003202 if (mon->password_completion_cb)
3203 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003204
aliguori731b0362009-03-05 23:01:42 +00003205 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003206}
aliguoric0f4ce72009-03-05 23:01:01 +00003207
aliguori376253e2009-03-05 23:01:23 +00003208void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003209 BlockDriverCompletionFunc *completion_cb,
3210 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003211{
aliguoricde76ee2009-03-05 23:01:51 +00003212 int err;
3213
aliguoribb5fc202009-03-05 23:01:15 +00003214 if (!bdrv_key_required(bs)) {
3215 if (completion_cb)
3216 completion_cb(opaque, 0);
3217 return;
3218 }
aliguoric0f4ce72009-03-05 23:01:01 +00003219
aliguori376253e2009-03-05 23:01:23 +00003220 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3221 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003222
aliguori731b0362009-03-05 23:01:42 +00003223 mon->password_completion_cb = completion_cb;
3224 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003225
aliguoricde76ee2009-03-05 23:01:51 +00003226 err = monitor_read_password(mon, bdrv_password_cb, bs);
3227
3228 if (err && completion_cb)
3229 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003230}