blob: 105a49a73a6b93a573112d08dd167654e6e8f0f0 [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
Luiz Capitulino38183182009-08-28 15:27:08 -0300254static void do_help_cmd(Monitor *mon, const char *name)
255{
256 help_cmd(mon, name);
257}
258
aliguori376253e2009-03-05 23:01:23 +0000259static void do_commit(Monitor *mon, const char *device)
bellard9dc39cb2004-03-14 21:38:27 +0000260{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200261 int all_devices;
262 DriveInfo *dinfo;
balrog2dc7b602007-05-24 18:53:22 +0000263
bellard7954c732006-08-01 15:52:40 +0000264 all_devices = !strcmp(device, "all");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200265 TAILQ_FOREACH(dinfo, &drives, next) {
266 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300267 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200268 continue;
269 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000270 }
271}
272
aliguori376253e2009-03-05 23:01:23 +0000273static void do_info(Monitor *mon, const char *item)
bellard9dc39cb2004-03-14 21:38:27 +0000274{
aliguori376253e2009-03-05 23:01:23 +0000275 const mon_cmd_t *cmd;
276 void (*handler)(Monitor *);
bellard9dc39cb2004-03-14 21:38:27 +0000277
bellard9307c4c2004-04-04 12:57:25 +0000278 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000279 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000280 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000281 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000282 goto found;
283 }
284 help:
aliguori376253e2009-03-05 23:01:23 +0000285 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000286 return;
287 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000288 handler = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +0000289 handler(mon);
bellard9dc39cb2004-03-14 21:38:27 +0000290}
291
aliguori376253e2009-03-05 23:01:23 +0000292static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000293{
pbrook4a19f1e2009-04-07 23:17:49 +0000294 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000295}
296
aliguori376253e2009-03-05 23:01:23 +0000297static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000298{
299 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000300 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000301}
302
aurel32bf4f74c2008-12-18 22:42:34 +0000303#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000304static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000305{
aliguori376253e2009-03-05 23:01:23 +0000306 monitor_printf(mon, "HPET is %s by QEMU\n",
307 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000308}
aurel32bf4f74c2008-12-18 22:42:34 +0000309#endif
aliguori16b29ae2008-12-17 23:28:44 +0000310
aliguori376253e2009-03-05 23:01:23 +0000311static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000312{
aliguori376253e2009-03-05 23:01:23 +0000313 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
314 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
315 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
316 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
317 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000318}
319
bellard6a00d602005-11-21 23:25:50 +0000320/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000321static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000322{
323 CPUState *env;
324
325 for(env = first_cpu; env != NULL; env = env->next_cpu) {
326 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000327 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000328 return 0;
329 }
330 }
331 return -1;
332}
333
pbrook9596ebb2007-11-18 01:44:38 +0000334static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000335{
aliguori731b0362009-03-05 23:01:42 +0000336 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000337 mon_set_cpu(0);
338 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300339 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000340 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000341}
342
aliguori376253e2009-03-05 23:01:23 +0000343static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000344{
bellard6a00d602005-11-21 23:25:50 +0000345 CPUState *env;
346 env = mon_get_cpu();
347 if (!env)
348 return;
bellard9307c4c2004-04-04 12:57:25 +0000349#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000350 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000351 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000352#else
aliguori376253e2009-03-05 23:01:23 +0000353 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000354 0);
bellard9307c4c2004-04-04 12:57:25 +0000355#endif
356}
357
aliguori376253e2009-03-05 23:01:23 +0000358static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000359{
360 CPUState *env;
361
362 /* just to set the default cpu if not already done */
363 mon_get_cpu();
364
365 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Avi Kivity4c0960c2009-08-17 23:19:53 +0300366 cpu_synchronize_state(env);
aliguori376253e2009-03-05 23:01:23 +0000367 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000368 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000369 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000370#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000371 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
372 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000373#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000374 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000375#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000376 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
377 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000378#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000379 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000380#endif
thsead93602007-09-06 00:18:15 +0000381 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000382 monitor_printf(mon, " (halted)");
383 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000384 }
385}
386
aliguori376253e2009-03-05 23:01:23 +0000387static void do_cpu_set(Monitor *mon, int index)
bellard6a00d602005-11-21 23:25:50 +0000388{
389 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000390 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000391}
392
aliguori376253e2009-03-05 23:01:23 +0000393static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000394{
aliguori376253e2009-03-05 23:01:23 +0000395 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000396}
397
aliguori376253e2009-03-05 23:01:23 +0000398static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000399{
400 int i;
bellard7e2515e2004-08-01 21:52:19 +0000401 const char *str;
ths3b46e622007-09-17 08:09:54 +0000402
aliguoricde76ee2009-03-05 23:01:51 +0000403 if (!mon->rs)
404 return;
bellard7e2515e2004-08-01 21:52:19 +0000405 i = 0;
406 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000407 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000408 if (!str)
409 break;
aliguori376253e2009-03-05 23:01:23 +0000410 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000411 i++;
bellardaa455482004-04-04 13:07:25 +0000412 }
413}
414
j_mayer76a66252007-03-07 08:32:30 +0000415#if defined(TARGET_PPC)
416/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000417static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000418{
419 CPUState *env;
420
421 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000422 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000423}
424#endif
425
aliguori376253e2009-03-05 23:01:23 +0000426static void do_quit(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000427{
428 exit(0);
429}
430
aliguori376253e2009-03-05 23:01:23 +0000431static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000432{
433 if (bdrv_is_inserted(bs)) {
434 if (!force) {
435 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000436 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000437 return -1;
438 }
439 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000440 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000441 return -1;
442 }
443 }
444 bdrv_close(bs);
445 }
446 return 0;
447}
448
aliguori376253e2009-03-05 23:01:23 +0000449static void do_eject(Monitor *mon, int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000450{
451 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000452
bellard9307c4c2004-04-04 12:57:25 +0000453 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000454 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000455 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000456 return;
457 }
aliguori376253e2009-03-05 23:01:23 +0000458 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000459}
460
aliguori376253e2009-03-05 23:01:23 +0000461static void do_change_block(Monitor *mon, const char *device,
462 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000463{
464 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000465 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000466
bellard9307c4c2004-04-04 12:57:25 +0000467 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000468 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000469 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000470 return;
471 }
aurel322ecea9b2008-06-18 22:10:01 +0000472 if (fmt) {
473 drv = bdrv_find_format(fmt);
474 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000475 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000476 return;
477 }
478 }
aliguori376253e2009-03-05 23:01:23 +0000479 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000480 return;
aurel322ecea9b2008-06-18 22:10:01 +0000481 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000482 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000483}
484
aliguori376253e2009-03-05 23:01:23 +0000485static void change_vnc_password_cb(Monitor *mon, const char *password,
486 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000487{
488 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000489 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000490
aliguori731b0362009-03-05 23:01:42 +0000491 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000492}
493
aliguori376253e2009-03-05 23:01:23 +0000494static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000495{
ths70848512007-08-25 01:37:05 +0000496 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000497 strcmp(target, "password") == 0) {
498 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000499 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000500 strncpy(password, arg, sizeof(password));
501 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000502 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000503 } else {
aliguori376253e2009-03-05 23:01:23 +0000504 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000505 }
ths70848512007-08-25 01:37:05 +0000506 } else {
aliguori28a76be2009-03-06 20:27:40 +0000507 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000508 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000509 }
thse25a5822007-08-25 01:36:20 +0000510}
511
aliguori376253e2009-03-05 23:01:23 +0000512static void do_change(Monitor *mon, const char *device, const char *target,
513 const char *arg)
thse25a5822007-08-25 01:36:20 +0000514{
515 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000516 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000517 } else {
aliguori28a76be2009-03-06 20:27:40 +0000518 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000519 }
520}
521
aliguori376253e2009-03-05 23:01:23 +0000522static void do_screen_dump(Monitor *mon, const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000523{
pbrook95219892006-04-09 01:06:34 +0000524 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000525}
526
aliguori376253e2009-03-05 23:01:23 +0000527static void do_logfile(Monitor *mon, const char *filename)
pbrooke735b912007-06-30 13:53:24 +0000528{
529 cpu_set_log_filename(filename);
530}
531
aliguori376253e2009-03-05 23:01:23 +0000532static void do_log(Monitor *mon, const char *items)
bellardf193c792004-03-21 17:06:25 +0000533{
534 int mask;
ths3b46e622007-09-17 08:09:54 +0000535
bellard9307c4c2004-04-04 12:57:25 +0000536 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000537 mask = 0;
538 } else {
bellard9307c4c2004-04-04 12:57:25 +0000539 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000540 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000541 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000542 return;
543 }
544 }
545 cpu_set_log(mask);
546}
547
aurel321b530a62009-04-05 20:08:59 +0000548static void do_singlestep(Monitor *mon, const char *option)
549{
550 if (!option || !strcmp(option, "on")) {
551 singlestep = 1;
552 } else if (!strcmp(option, "off")) {
553 singlestep = 0;
554 } else {
555 monitor_printf(mon, "unexpected option %s\n", option);
556 }
557}
558
aliguori376253e2009-03-05 23:01:23 +0000559static void do_stop(Monitor *mon)
bellard8a7ddc32004-03-31 19:00:16 +0000560{
561 vm_stop(EXCP_INTERRUPT);
562}
563
aliguoribb5fc202009-03-05 23:01:15 +0000564static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000565
aliguori376253e2009-03-05 23:01:23 +0000566struct bdrv_iterate_context {
567 Monitor *mon;
568 int err;
569};
aliguoric0f4ce72009-03-05 23:01:01 +0000570
aliguori376253e2009-03-05 23:01:23 +0000571static void do_cont(Monitor *mon)
572{
573 struct bdrv_iterate_context context = { mon, 0 };
574
575 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000576 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000577 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000578 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000579}
580
aliguoribb5fc202009-03-05 23:01:15 +0000581static void bdrv_key_cb(void *opaque, int err)
582{
aliguori376253e2009-03-05 23:01:23 +0000583 Monitor *mon = opaque;
584
aliguoribb5fc202009-03-05 23:01:15 +0000585 /* another key was set successfully, retry to continue */
586 if (!err)
aliguori376253e2009-03-05 23:01:23 +0000587 do_cont(mon);
aliguoribb5fc202009-03-05 23:01:15 +0000588}
589
590static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
591{
aliguori376253e2009-03-05 23:01:23 +0000592 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000593
aliguori376253e2009-03-05 23:01:23 +0000594 if (!context->err && bdrv_key_required(bs)) {
595 context->err = -EBUSY;
596 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
597 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000598 }
599}
600
aliguori59030a82009-04-05 18:43:41 +0000601static void do_gdbserver(Monitor *mon, const char *device)
bellard8a7ddc32004-03-31 19:00:16 +0000602{
aliguori59030a82009-04-05 18:43:41 +0000603 if (!device)
604 device = "tcp::" DEFAULT_GDBSTUB_PORT;
605 if (gdbserver_start(device) < 0) {
606 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
607 device);
608 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000609 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000610 } else {
aliguori59030a82009-04-05 18:43:41 +0000611 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
612 device);
bellard8a7ddc32004-03-31 19:00:16 +0000613 }
614}
615
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100616static void do_watchdog_action(Monitor *mon, const char *action)
617{
618 if (select_watchdog_action(action) == -1) {
619 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
620 }
621}
622
aliguori376253e2009-03-05 23:01:23 +0000623static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000624{
aliguori376253e2009-03-05 23:01:23 +0000625 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000626 switch(c) {
627 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000628 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000629 break;
630 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000631 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000632 break;
633 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000634 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000635 break;
636 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000637 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000638 break;
639 default:
640 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000641 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000642 } else {
aliguori376253e2009-03-05 23:01:23 +0000643 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000644 }
645 break;
646 }
aliguori376253e2009-03-05 23:01:23 +0000647 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000648}
649
aliguori376253e2009-03-05 23:01:23 +0000650static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000651 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000652{
bellard6a00d602005-11-21 23:25:50 +0000653 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000654 int nb_per_line, l, line_size, i, max_digits, len;
655 uint8_t buf[16];
656 uint64_t v;
657
658 if (format == 'i') {
659 int flags;
660 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000661 env = mon_get_cpu();
662 if (!env && !is_physical)
663 return;
bellard9307c4c2004-04-04 12:57:25 +0000664#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000665 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000666 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000667 } else if (wsize == 4) {
668 flags = 0;
669 } else {
bellard6a15fd12006-04-12 21:07:07 +0000670 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000671 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000672 if (env) {
673#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000674 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000675 (env->segs[R_CS].flags & DESC_L_MASK))
676 flags = 2;
677 else
678#endif
679 if (!(env->segs[R_CS].flags & DESC_B_MASK))
680 flags = 1;
681 }
bellard4c27ba22004-04-25 18:05:08 +0000682 }
683#endif
aliguori376253e2009-03-05 23:01:23 +0000684 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000685 return;
686 }
687
688 len = wsize * count;
689 if (wsize == 1)
690 line_size = 8;
691 else
692 line_size = 16;
693 nb_per_line = line_size / wsize;
694 max_digits = 0;
695
696 switch(format) {
697 case 'o':
698 max_digits = (wsize * 8 + 2) / 3;
699 break;
700 default:
701 case 'x':
702 max_digits = (wsize * 8) / 4;
703 break;
704 case 'u':
705 case 'd':
706 max_digits = (wsize * 8 * 10 + 32) / 33;
707 break;
708 case 'c':
709 wsize = 1;
710 break;
711 }
712
713 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000714 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000715 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000716 else
aliguori376253e2009-03-05 23:01:23 +0000717 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000718 l = len;
719 if (l > line_size)
720 l = line_size;
721 if (is_physical) {
722 cpu_physical_memory_rw(addr, buf, l, 0);
723 } else {
bellard6a00d602005-11-21 23:25:50 +0000724 env = mon_get_cpu();
725 if (!env)
726 break;
aliguoric8f79b62008-08-18 14:00:20 +0000727 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000728 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000729 break;
730 }
bellard9307c4c2004-04-04 12:57:25 +0000731 }
ths5fafdf22007-09-16 21:08:06 +0000732 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000733 while (i < l) {
734 switch(wsize) {
735 default:
736 case 1:
737 v = ldub_raw(buf + i);
738 break;
739 case 2:
740 v = lduw_raw(buf + i);
741 break;
742 case 4:
bellard92a31b12005-02-10 22:00:52 +0000743 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000744 break;
745 case 8:
746 v = ldq_raw(buf + i);
747 break;
748 }
aliguori376253e2009-03-05 23:01:23 +0000749 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000750 switch(format) {
751 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000752 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000753 break;
754 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000755 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000756 break;
757 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000758 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000759 break;
760 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000761 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000762 break;
763 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000764 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000765 break;
766 }
767 i += wsize;
768 }
aliguori376253e2009-03-05 23:01:23 +0000769 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000770 addr += l;
771 len -= l;
772 }
773}
774
bellard92a31b12005-02-10 22:00:52 +0000775#if TARGET_LONG_BITS == 64
776#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
777#else
778#define GET_TLONG(h, l) (l)
779#endif
780
aliguori376253e2009-03-05 23:01:23 +0000781static void do_memory_dump(Monitor *mon, int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000782 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000783{
bellard92a31b12005-02-10 22:00:52 +0000784 target_long addr = GET_TLONG(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000785 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000786}
787
blueswir17743e582007-09-24 18:39:04 +0000788#if TARGET_PHYS_ADDR_BITS > 32
789#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
790#else
791#define GET_TPHYSADDR(h, l) (l)
792#endif
793
aliguori376253e2009-03-05 23:01:23 +0000794static void do_physical_memory_dump(Monitor *mon, int count, int format,
795 int size, uint32_t addrh, uint32_t addrl)
bellard92a31b12005-02-10 22:00:52 +0000796
bellard9307c4c2004-04-04 12:57:25 +0000797{
blueswir17743e582007-09-24 18:39:04 +0000798 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000799 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000800}
801
aliguori376253e2009-03-05 23:01:23 +0000802static void do_print(Monitor *mon, int count, int format, int size,
803 unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000804{
blueswir17743e582007-09-24 18:39:04 +0000805 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
806#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000807 switch(format) {
808 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000809 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000810 break;
811 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000812 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000813 break;
814 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000815 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000816 break;
817 default:
818 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000819 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000820 break;
821 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000822 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000823 break;
824 }
bellard92a31b12005-02-10 22:00:52 +0000825#else
826 switch(format) {
827 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000828 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000829 break;
830 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000831 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000832 break;
833 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000834 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000835 break;
836 default:
837 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000838 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000839 break;
840 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000841 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000842 break;
843 }
844#endif
aliguori376253e2009-03-05 23:01:23 +0000845 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000846}
847
aliguori376253e2009-03-05 23:01:23 +0000848static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000849 uint32_t size, const char *filename)
850{
851 FILE *f;
852 target_long addr = GET_TLONG(valh, vall);
853 uint32_t l;
854 CPUState *env;
855 uint8_t buf[1024];
856
857 env = mon_get_cpu();
858 if (!env)
859 return;
860
861 f = fopen(filename, "wb");
862 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000863 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000864 return;
865 }
866 while (size != 0) {
867 l = sizeof(buf);
868 if (l > size)
869 l = size;
870 cpu_memory_rw_debug(env, addr, buf, l, 0);
871 fwrite(buf, 1, l, f);
872 addr += l;
873 size -= l;
874 }
875 fclose(f);
876}
877
aliguori376253e2009-03-05 23:01:23 +0000878static void do_physical_memory_save(Monitor *mon, unsigned int valh,
879 unsigned int vall, uint32_t size,
880 const char *filename)
aurel32a8bdf7a2008-04-11 21:36:14 +0000881{
882 FILE *f;
883 uint32_t l;
884 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000885 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000886
887 f = fopen(filename, "wb");
888 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000889 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000890 return;
891 }
892 while (size != 0) {
893 l = sizeof(buf);
894 if (l > size)
895 l = size;
896 cpu_physical_memory_rw(addr, buf, l, 0);
897 fwrite(buf, 1, l, f);
898 fflush(f);
899 addr += l;
900 size -= l;
901 }
902 fclose(f);
903}
904
aliguori376253e2009-03-05 23:01:23 +0000905static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
bellarde4cf1ad2005-06-04 20:15:57 +0000906{
907 uint32_t addr;
908 uint8_t buf[1];
909 uint16_t sum;
910
911 sum = 0;
912 for(addr = start; addr < (start + size); addr++) {
913 cpu_physical_memory_rw(addr, buf, 1, 0);
914 /* BSD sum algorithm ('sum' Unix command) */
915 sum = (sum >> 1) | (sum << 15);
916 sum += buf[0];
917 }
aliguori376253e2009-03-05 23:01:23 +0000918 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000919}
920
bellarda3a91a32004-06-04 11:06:21 +0000921typedef struct {
922 int keycode;
923 const char *name;
924} KeyDef;
925
926static const KeyDef key_defs[] = {
927 { 0x2a, "shift" },
928 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000929
bellarda3a91a32004-06-04 11:06:21 +0000930 { 0x38, "alt" },
931 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000932 { 0x64, "altgr" },
933 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000934 { 0x1d, "ctrl" },
935 { 0x9d, "ctrl_r" },
936
937 { 0xdd, "menu" },
938
939 { 0x01, "esc" },
940
941 { 0x02, "1" },
942 { 0x03, "2" },
943 { 0x04, "3" },
944 { 0x05, "4" },
945 { 0x06, "5" },
946 { 0x07, "6" },
947 { 0x08, "7" },
948 { 0x09, "8" },
949 { 0x0a, "9" },
950 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000951 { 0x0c, "minus" },
952 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000953 { 0x0e, "backspace" },
954
955 { 0x0f, "tab" },
956 { 0x10, "q" },
957 { 0x11, "w" },
958 { 0x12, "e" },
959 { 0x13, "r" },
960 { 0x14, "t" },
961 { 0x15, "y" },
962 { 0x16, "u" },
963 { 0x17, "i" },
964 { 0x18, "o" },
965 { 0x19, "p" },
966
967 { 0x1c, "ret" },
968
969 { 0x1e, "a" },
970 { 0x1f, "s" },
971 { 0x20, "d" },
972 { 0x21, "f" },
973 { 0x22, "g" },
974 { 0x23, "h" },
975 { 0x24, "j" },
976 { 0x25, "k" },
977 { 0x26, "l" },
978
979 { 0x2c, "z" },
980 { 0x2d, "x" },
981 { 0x2e, "c" },
982 { 0x2f, "v" },
983 { 0x30, "b" },
984 { 0x31, "n" },
985 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000986 { 0x33, "comma" },
987 { 0x34, "dot" },
988 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000989
balrog4d3b6f62008-02-10 16:33:14 +0000990 { 0x37, "asterisk" },
991
bellarda3a91a32004-06-04 11:06:21 +0000992 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000993 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000994 { 0x3b, "f1" },
995 { 0x3c, "f2" },
996 { 0x3d, "f3" },
997 { 0x3e, "f4" },
998 { 0x3f, "f5" },
999 { 0x40, "f6" },
1000 { 0x41, "f7" },
1001 { 0x42, "f8" },
1002 { 0x43, "f9" },
1003 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001004 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001005 { 0x46, "scroll_lock" },
1006
bellard64866c32006-05-07 18:03:31 +00001007 { 0xb5, "kp_divide" },
1008 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001009 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001010 { 0x4e, "kp_add" },
1011 { 0x9c, "kp_enter" },
1012 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001013 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001014
1015 { 0x52, "kp_0" },
1016 { 0x4f, "kp_1" },
1017 { 0x50, "kp_2" },
1018 { 0x51, "kp_3" },
1019 { 0x4b, "kp_4" },
1020 { 0x4c, "kp_5" },
1021 { 0x4d, "kp_6" },
1022 { 0x47, "kp_7" },
1023 { 0x48, "kp_8" },
1024 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001025
bellarda3a91a32004-06-04 11:06:21 +00001026 { 0x56, "<" },
1027
1028 { 0x57, "f11" },
1029 { 0x58, "f12" },
1030
1031 { 0xb7, "print" },
1032
1033 { 0xc7, "home" },
1034 { 0xc9, "pgup" },
1035 { 0xd1, "pgdn" },
1036 { 0xcf, "end" },
1037
1038 { 0xcb, "left" },
1039 { 0xc8, "up" },
1040 { 0xd0, "down" },
1041 { 0xcd, "right" },
1042
1043 { 0xd2, "insert" },
1044 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001045#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1046 { 0xf0, "stop" },
1047 { 0xf1, "again" },
1048 { 0xf2, "props" },
1049 { 0xf3, "undo" },
1050 { 0xf4, "front" },
1051 { 0xf5, "copy" },
1052 { 0xf6, "open" },
1053 { 0xf7, "paste" },
1054 { 0xf8, "find" },
1055 { 0xf9, "cut" },
1056 { 0xfa, "lf" },
1057 { 0xfb, "help" },
1058 { 0xfc, "meta_l" },
1059 { 0xfd, "meta_r" },
1060 { 0xfe, "compose" },
1061#endif
bellarda3a91a32004-06-04 11:06:21 +00001062 { 0, NULL },
1063};
1064
1065static int get_keycode(const char *key)
1066{
1067 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001068 char *endp;
1069 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001070
1071 for(p = key_defs; p->name != NULL; p++) {
1072 if (!strcmp(key, p->name))
1073 return p->keycode;
1074 }
bellard64866c32006-05-07 18:03:31 +00001075 if (strstart(key, "0x", NULL)) {
1076 ret = strtoul(key, &endp, 0);
1077 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1078 return ret;
1079 }
bellarda3a91a32004-06-04 11:06:21 +00001080 return -1;
1081}
1082
balrogc8256f92008-06-08 22:45:01 +00001083#define MAX_KEYCODES 16
1084static uint8_t keycodes[MAX_KEYCODES];
1085static int nb_pending_keycodes;
1086static QEMUTimer *key_timer;
1087
1088static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001089{
balrogc8256f92008-06-08 22:45:01 +00001090 int keycode;
1091
1092 while (nb_pending_keycodes > 0) {
1093 nb_pending_keycodes--;
1094 keycode = keycodes[nb_pending_keycodes];
1095 if (keycode & 0x80)
1096 kbd_put_keycode(0xe0);
1097 kbd_put_keycode(keycode | 0x80);
1098 }
1099}
1100
aliguori376253e2009-03-05 23:01:23 +00001101static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1102 int hold_time)
balrogc8256f92008-06-08 22:45:01 +00001103{
balrog3401c0d2008-06-04 10:05:59 +00001104 char keyname_buf[16];
1105 char *separator;
1106 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +00001107
balrogc8256f92008-06-08 22:45:01 +00001108 if (nb_pending_keycodes > 0) {
1109 qemu_del_timer(key_timer);
1110 release_keys(NULL);
1111 }
1112 if (!has_hold_time)
1113 hold_time = 100;
1114 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001115 while (1) {
1116 separator = strchr(string, '-');
1117 keyname_len = separator ? separator - string : strlen(string);
1118 if (keyname_len > 0) {
1119 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1120 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001121 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001122 return;
bellarda3a91a32004-06-04 11:06:21 +00001123 }
balrogc8256f92008-06-08 22:45:01 +00001124 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001125 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001126 return;
1127 }
1128 keyname_buf[keyname_len] = 0;
1129 keycode = get_keycode(keyname_buf);
1130 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001131 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001132 return;
1133 }
balrogc8256f92008-06-08 22:45:01 +00001134 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001135 }
balrog3401c0d2008-06-04 10:05:59 +00001136 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001137 break;
balrog3401c0d2008-06-04 10:05:59 +00001138 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001139 }
balrogc8256f92008-06-08 22:45:01 +00001140 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001141 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001142 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001143 keycode = keycodes[i];
1144 if (keycode & 0x80)
1145 kbd_put_keycode(0xe0);
1146 kbd_put_keycode(keycode & 0x7f);
1147 }
balrogc8256f92008-06-08 22:45:01 +00001148 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001149 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1150 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001151}
1152
bellard13224a82006-07-14 22:03:35 +00001153static int mouse_button_state;
1154
aliguori376253e2009-03-05 23:01:23 +00001155static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001156 const char *dz_str)
1157{
1158 int dx, dy, dz;
1159 dx = strtol(dx_str, NULL, 0);
1160 dy = strtol(dy_str, NULL, 0);
1161 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001162 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001163 dz = strtol(dz_str, NULL, 0);
1164 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1165}
1166
aliguori376253e2009-03-05 23:01:23 +00001167static void do_mouse_button(Monitor *mon, int button_state)
bellard13224a82006-07-14 22:03:35 +00001168{
1169 mouse_button_state = button_state;
1170 kbd_mouse_event(0, 0, 0, mouse_button_state);
1171}
1172
aliguori376253e2009-03-05 23:01:23 +00001173static void do_ioport_read(Monitor *mon, int count, int format, int size,
1174 int addr, int has_index, int index)
bellard34405572004-06-08 00:55:58 +00001175{
1176 uint32_t val;
1177 int suffix;
1178
1179 if (has_index) {
Isaku Yamahatad56dd6c2009-07-02 19:32:07 +09001180 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001181 addr++;
1182 }
1183 addr &= 0xffff;
1184
1185 switch(size) {
1186 default:
1187 case 1:
1188 val = cpu_inb(NULL, addr);
1189 suffix = 'b';
1190 break;
1191 case 2:
1192 val = cpu_inw(NULL, addr);
1193 suffix = 'w';
1194 break;
1195 case 4:
1196 val = cpu_inl(NULL, addr);
1197 suffix = 'l';
1198 break;
1199 }
aliguori376253e2009-03-05 23:01:23 +00001200 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1201 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001202}
bellarda3a91a32004-06-04 11:06:21 +00001203
Jan Kiszkaf1147842009-07-14 10:20:11 +02001204static void do_ioport_write(Monitor *mon, int count, int format, int size,
1205 int addr, int val)
1206{
1207 addr &= IOPORTS_MASK;
1208
1209 switch (size) {
1210 default:
1211 case 1:
1212 cpu_outb(NULL, addr, val);
1213 break;
1214 case 2:
1215 cpu_outw(NULL, addr, val);
1216 break;
1217 case 4:
1218 cpu_outl(NULL, addr, val);
1219 break;
1220 }
1221}
1222
aliguori376253e2009-03-05 23:01:23 +00001223static void do_boot_set(Monitor *mon, const char *bootdevice)
aurel320ecdffb2008-05-04 20:11:34 +00001224{
1225 int res;
1226
Jan Kiszka76e30d02009-07-02 00:19:02 +02001227 res = qemu_boot_set(bootdevice);
1228 if (res == 0) {
1229 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1230 } else if (res > 0) {
1231 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001232 } else {
aliguori376253e2009-03-05 23:01:23 +00001233 monitor_printf(mon, "no function defined to set boot device list for "
1234 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001235 }
1236}
1237
aliguori376253e2009-03-05 23:01:23 +00001238static void do_system_reset(Monitor *mon)
bellarde4f90822004-06-20 12:35:44 +00001239{
1240 qemu_system_reset_request();
1241}
1242
aliguori376253e2009-03-05 23:01:23 +00001243static void do_system_powerdown(Monitor *mon)
bellard34751872005-07-02 14:31:34 +00001244{
1245 qemu_system_powerdown_request();
1246}
1247
bellardb86bda52004-09-18 19:32:46 +00001248#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001249static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001250{
aliguori376253e2009-03-05 23:01:23 +00001251 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1252 addr,
1253 pte & mask,
1254 pte & PG_GLOBAL_MASK ? 'G' : '-',
1255 pte & PG_PSE_MASK ? 'P' : '-',
1256 pte & PG_DIRTY_MASK ? 'D' : '-',
1257 pte & PG_ACCESSED_MASK ? 'A' : '-',
1258 pte & PG_PCD_MASK ? 'C' : '-',
1259 pte & PG_PWT_MASK ? 'T' : '-',
1260 pte & PG_USER_MASK ? 'U' : '-',
1261 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001262}
1263
aliguori376253e2009-03-05 23:01:23 +00001264static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001265{
bellard6a00d602005-11-21 23:25:50 +00001266 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001267 int l1, l2;
1268 uint32_t pgd, pde, pte;
1269
bellard6a00d602005-11-21 23:25:50 +00001270 env = mon_get_cpu();
1271 if (!env)
1272 return;
1273
bellardb86bda52004-09-18 19:32:46 +00001274 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001275 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001276 return;
1277 }
1278 pgd = env->cr[3] & ~0xfff;
1279 for(l1 = 0; l1 < 1024; l1++) {
1280 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1281 pde = le32_to_cpu(pde);
1282 if (pde & PG_PRESENT_MASK) {
1283 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001284 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001285 } else {
1286 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001287 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001288 (uint8_t *)&pte, 4);
1289 pte = le32_to_cpu(pte);
1290 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001291 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001292 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001293 ~0xfff);
1294 }
1295 }
1296 }
1297 }
1298 }
1299}
1300
aliguori376253e2009-03-05 23:01:23 +00001301static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001302 uint32_t end, int prot)
1303{
bellard9746b152004-11-11 18:30:24 +00001304 int prot1;
1305 prot1 = *plast_prot;
1306 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001307 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001308 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1309 *pstart, end, end - *pstart,
1310 prot1 & PG_USER_MASK ? 'u' : '-',
1311 'r',
1312 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001313 }
1314 if (prot != 0)
1315 *pstart = end;
1316 else
1317 *pstart = -1;
1318 *plast_prot = prot;
1319 }
1320}
1321
aliguori376253e2009-03-05 23:01:23 +00001322static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001323{
bellard6a00d602005-11-21 23:25:50 +00001324 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001325 int l1, l2, prot, last_prot;
1326 uint32_t pgd, pde, pte, start, end;
1327
bellard6a00d602005-11-21 23:25:50 +00001328 env = mon_get_cpu();
1329 if (!env)
1330 return;
1331
bellardb86bda52004-09-18 19:32:46 +00001332 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001333 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001334 return;
1335 }
1336 pgd = env->cr[3] & ~0xfff;
1337 last_prot = 0;
1338 start = -1;
1339 for(l1 = 0; l1 < 1024; l1++) {
1340 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1341 pde = le32_to_cpu(pde);
1342 end = l1 << 22;
1343 if (pde & PG_PRESENT_MASK) {
1344 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1345 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001346 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001347 } else {
1348 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001349 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001350 (uint8_t *)&pte, 4);
1351 pte = le32_to_cpu(pte);
1352 end = (l1 << 22) + (l2 << 12);
1353 if (pte & PG_PRESENT_MASK) {
1354 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1355 } else {
1356 prot = 0;
1357 }
aliguori376253e2009-03-05 23:01:23 +00001358 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001359 }
1360 }
1361 } else {
1362 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001363 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001364 }
1365 }
1366}
1367#endif
1368
aurel327c664e22009-03-03 06:12:22 +00001369#if defined(TARGET_SH4)
1370
aliguori376253e2009-03-05 23:01:23 +00001371static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001372{
aliguori376253e2009-03-05 23:01:23 +00001373 monitor_printf(mon, " tlb%i:\t"
1374 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1375 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1376 "dirty=%hhu writethrough=%hhu\n",
1377 idx,
1378 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1379 tlb->v, tlb->sh, tlb->c, tlb->pr,
1380 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001381}
1382
aliguori376253e2009-03-05 23:01:23 +00001383static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001384{
1385 CPUState *env = mon_get_cpu();
1386 int i;
1387
aliguori376253e2009-03-05 23:01:23 +00001388 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001389 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001390 print_tlb (mon, i, &env->itlb[i]);
1391 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001392 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001393 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001394}
1395
1396#endif
1397
aliguori376253e2009-03-05 23:01:23 +00001398static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001399{
1400#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001401 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001402 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001403 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001404 else
aliguori376253e2009-03-05 23:01:23 +00001405 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001406#else
aliguori376253e2009-03-05 23:01:23 +00001407 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001408#endif
1409}
1410
aliguori030ea372009-04-21 22:30:47 +00001411static void do_info_numa(Monitor *mon)
1412{
aliguorib28b6232009-04-22 20:20:29 +00001413 int i;
aliguori030ea372009-04-21 22:30:47 +00001414 CPUState *env;
1415
1416 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1417 for (i = 0; i < nb_numa_nodes; i++) {
1418 monitor_printf(mon, "node %d cpus:", i);
1419 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1420 if (env->numa_node == i) {
1421 monitor_printf(mon, " %d", env->cpu_index);
1422 }
1423 }
1424 monitor_printf(mon, "\n");
1425 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1426 node_mem[i] >> 20);
1427 }
1428}
1429
bellard5f1ce942006-02-08 22:40:15 +00001430#ifdef CONFIG_PROFILER
1431
aliguori376253e2009-03-05 23:01:23 +00001432static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001433{
1434 int64_t total;
1435 total = qemu_time;
1436 if (total == 0)
1437 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001438 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1439 dev_time, dev_time / (double)ticks_per_sec);
1440 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1441 qemu_time, qemu_time / (double)ticks_per_sec);
bellard5f1ce942006-02-08 22:40:15 +00001442 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001443 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001444}
1445#else
aliguori376253e2009-03-05 23:01:23 +00001446static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001447{
aliguori376253e2009-03-05 23:01:23 +00001448 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001449}
1450#endif
1451
bellardec36b692006-07-16 18:57:03 +00001452/* Capture support */
1453static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1454
aliguori376253e2009-03-05 23:01:23 +00001455static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001456{
1457 int i;
1458 CaptureState *s;
1459
1460 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001461 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001462 s->ops.info (s->opaque);
1463 }
1464}
1465
Blue Swirl23130862009-06-06 08:22:04 +00001466#ifdef HAS_AUDIO
aliguori376253e2009-03-05 23:01:23 +00001467static void do_stop_capture(Monitor *mon, int n)
bellardec36b692006-07-16 18:57:03 +00001468{
1469 int i;
1470 CaptureState *s;
1471
1472 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1473 if (i == n) {
1474 s->ops.destroy (s->opaque);
1475 LIST_REMOVE (s, entries);
1476 qemu_free (s);
1477 return;
1478 }
1479 }
1480}
1481
aliguori376253e2009-03-05 23:01:23 +00001482static void do_wav_capture(Monitor *mon, const char *path,
1483 int has_freq, int freq,
1484 int has_bits, int bits,
1485 int has_channels, int nchannels)
bellardec36b692006-07-16 18:57:03 +00001486{
1487 CaptureState *s;
1488
1489 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001490
1491 freq = has_freq ? freq : 44100;
1492 bits = has_bits ? bits : 16;
1493 nchannels = has_channels ? nchannels : 2;
1494
1495 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001496 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001497 qemu_free (s);
1498 }
1499 LIST_INSERT_HEAD (&capture_head, s, entries);
1500}
1501#endif
1502
aurel32dc1c0b72008-04-27 23:52:12 +00001503#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001504static void do_inject_nmi(Monitor *mon, int cpu_index)
aurel32dc1c0b72008-04-27 23:52:12 +00001505{
1506 CPUState *env;
1507
1508 for (env = first_cpu; env != NULL; env = env->next_cpu)
1509 if (env->cpu_index == cpu_index) {
1510 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1511 break;
1512 }
1513}
1514#endif
1515
aliguori376253e2009-03-05 23:01:23 +00001516static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001517{
aurel321b530a62009-04-05 20:08:59 +00001518 if (vm_running) {
1519 if (singlestep) {
1520 monitor_printf(mon, "VM status: running (single step mode)\n");
1521 } else {
1522 monitor_printf(mon, "VM status: running\n");
1523 }
1524 } else
aliguori376253e2009-03-05 23:01:23 +00001525 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001526}
1527
1528
aliguori376253e2009-03-05 23:01:23 +00001529static void do_balloon(Monitor *mon, int value)
aliguoridf751fa2008-12-04 20:19:35 +00001530{
1531 ram_addr_t target = value;
1532 qemu_balloon(target << 20);
1533}
1534
aliguori376253e2009-03-05 23:01:23 +00001535static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001536{
1537 ram_addr_t actual;
1538
1539 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001540 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001541 monitor_printf(mon, "Using KVM without synchronous MMU, "
1542 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001543 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001544 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001545 else
aliguori376253e2009-03-05 23:01:23 +00001546 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001547}
1548
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001549static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001550{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001551 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001552
aliguori76655d62009-03-06 20:27:37 +00001553 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001554 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001555 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001556 return acl;
1557}
aliguori76655d62009-03-06 20:27:37 +00001558
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001559static void do_acl_show(Monitor *mon, const char *aclname)
1560{
1561 qemu_acl *acl = find_acl(mon, aclname);
1562 qemu_acl_entry *entry;
1563 int i = 0;
1564
1565 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001566 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001567 acl->defaultDeny ? "deny" : "allow");
aliguori28a76be2009-03-06 20:27:40 +00001568 TAILQ_FOREACH(entry, &acl->entries, next) {
1569 i++;
1570 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001571 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001572 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001573 }
1574}
1575
1576static void do_acl_reset(Monitor *mon, const char *aclname)
1577{
1578 qemu_acl *acl = find_acl(mon, aclname);
1579
1580 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001581 qemu_acl_reset(acl);
1582 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001583 }
1584}
aliguori76655d62009-03-06 20:27:37 +00001585
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001586static void do_acl_policy(Monitor *mon, const char *aclname,
1587 const char *policy)
1588{
1589 qemu_acl *acl = find_acl(mon, aclname);
1590
1591 if (acl) {
1592 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001593 acl->defaultDeny = 0;
1594 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001595 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001596 acl->defaultDeny = 1;
1597 monitor_printf(mon, "acl: policy set to 'deny'\n");
1598 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001599 monitor_printf(mon, "acl: unknown policy '%s', "
1600 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001601 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001602 }
1603}
aliguori76655d62009-03-06 20:27:37 +00001604
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001605static void do_acl_add(Monitor *mon, const char *aclname,
1606 const char *match, const char *policy,
1607 int has_index, int index)
1608{
1609 qemu_acl *acl = find_acl(mon, aclname);
1610 int deny, ret;
1611
1612 if (acl) {
1613 if (strcmp(policy, "allow") == 0) {
1614 deny = 0;
1615 } else if (strcmp(policy, "deny") == 0) {
1616 deny = 1;
1617 } else {
1618 monitor_printf(mon, "acl: unknown policy '%s', "
1619 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001620 return;
1621 }
aliguori28a76be2009-03-06 20:27:40 +00001622 if (has_index)
1623 ret = qemu_acl_insert(acl, deny, match, index);
1624 else
1625 ret = qemu_acl_append(acl, deny, match);
1626 if (ret < 0)
1627 monitor_printf(mon, "acl: unable to add acl entry\n");
1628 else
1629 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001630 }
1631}
aliguori76655d62009-03-06 20:27:37 +00001632
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001633static void do_acl_remove(Monitor *mon, const char *aclname, const char *match)
1634{
1635 qemu_acl *acl = find_acl(mon, aclname);
1636 int ret;
aliguori76655d62009-03-06 20:27:37 +00001637
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001638 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001639 ret = qemu_acl_remove(acl, match);
1640 if (ret < 0)
1641 monitor_printf(mon, "acl: no matching acl entry\n");
1642 else
1643 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001644 }
1645}
1646
Huang Ying79c4f6b2009-06-23 10:05:14 +08001647#if defined(TARGET_I386)
1648static void do_inject_mce(Monitor *mon,
1649 int cpu_index, int bank,
1650 unsigned status_hi, unsigned status_lo,
1651 unsigned mcg_status_hi, unsigned mcg_status_lo,
1652 unsigned addr_hi, unsigned addr_lo,
1653 unsigned misc_hi, unsigned misc_lo)
1654{
1655 CPUState *cenv;
1656 uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
1657 uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
1658 uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
1659 uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
1660
1661 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1662 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1663 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1664 break;
1665 }
1666}
1667#endif
1668
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001669static void do_getfd(Monitor *mon, const char *fdname)
1670{
1671 mon_fd_t *monfd;
1672 int fd;
1673
1674 fd = qemu_chr_get_msgfd(mon->chr);
1675 if (fd == -1) {
1676 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1677 return;
1678 }
1679
1680 if (qemu_isdigit(fdname[0])) {
1681 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1682 return;
1683 }
1684
1685 fd = dup(fd);
1686 if (fd == -1) {
1687 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1688 strerror(errno));
1689 return;
1690 }
1691
1692 LIST_FOREACH(monfd, &mon->fds, next) {
1693 if (strcmp(monfd->name, fdname) != 0) {
1694 continue;
1695 }
1696
1697 close(monfd->fd);
1698 monfd->fd = fd;
1699 return;
1700 }
1701
1702 monfd = qemu_mallocz(sizeof(mon_fd_t));
1703 monfd->name = qemu_strdup(fdname);
1704 monfd->fd = fd;
1705
1706 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1707}
1708
1709static void do_closefd(Monitor *mon, const char *fdname)
1710{
1711 mon_fd_t *monfd;
1712
1713 LIST_FOREACH(monfd, &mon->fds, next) {
1714 if (strcmp(monfd->name, fdname) != 0) {
1715 continue;
1716 }
1717
1718 LIST_REMOVE(monfd, next);
1719 close(monfd->fd);
1720 qemu_free(monfd->name);
1721 qemu_free(monfd);
1722 return;
1723 }
1724
1725 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1726 fdname);
1727}
1728
Juan Quintelac8d41b22009-08-20 19:42:21 +02001729static void do_loadvm(Monitor *mon, const char *name)
1730{
1731 int saved_vm_running = vm_running;
1732
1733 vm_stop(0);
1734
Juan Quintela05f24012009-08-20 19:42:22 +02001735 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001736 vm_start();
1737}
1738
Mark McLoughlin7768e042009-07-22 09:11:41 +01001739int monitor_get_fd(Monitor *mon, const char *fdname)
1740{
1741 mon_fd_t *monfd;
1742
1743 LIST_FOREACH(monfd, &mon->fds, next) {
1744 int fd;
1745
1746 if (strcmp(monfd->name, fdname) != 0) {
1747 continue;
1748 }
1749
1750 fd = monfd->fd;
1751
1752 /* caller takes ownership of fd */
1753 LIST_REMOVE(monfd, next);
1754 qemu_free(monfd->name);
1755 qemu_free(monfd);
1756
1757 return fd;
1758 }
1759
1760 return -1;
1761}
1762
aliguori376253e2009-03-05 23:01:23 +00001763static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001764#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001765 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001766};
1767
Blue Swirl23130862009-06-06 08:22:04 +00001768/* Please update qemu-monitor.hx when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001769static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001770 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001771 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001772 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001773 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001774 { "chardev", "", qemu_chr_info,
1775 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001776 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001777 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001778 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001779 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001780 { "registers", "", do_info_registers,
1781 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001782 { "cpus", "", do_info_cpus,
1783 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001784 { "history", "", do_info_history,
1785 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001786 { "irq", "", irq_info,
1787 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001788 { "pic", "", pic_info,
1789 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001790 { "pci", "", pci_info,
1791 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001792#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001793 { "tlb", "", tlb_info,
1794 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001795#endif
1796#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001797 { "mem", "", mem_info,
1798 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001799 { "hpet", "", do_info_hpet,
1800 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001801#endif
bellarde3db7222005-01-26 22:00:47 +00001802 { "jit", "", do_info_jit,
1803 "", "show dynamic compiler info", },
aliguori7ba1e612008-11-05 16:04:33 +00001804 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001805 "", "show KVM information", },
aliguori030ea372009-04-21 22:30:47 +00001806 { "numa", "", do_info_numa,
1807 "", "show NUMA information", },
bellarda594cfb2005-11-06 16:13:29 +00001808 { "usb", "", usb_info,
1809 "", "show guest USB devices", },
1810 { "usbhost", "", usb_host_info,
1811 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001812 { "profile", "", do_info_profile,
1813 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001814 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001815 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001816 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001817 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001818 { "status", "", do_info_status,
1819 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001820 { "pcmcia", "", pcmcia_info,
1821 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001822 { "mice", "", do_info_mice,
1823 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001824 { "vnc", "", do_info_vnc,
1825 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001826 { "name", "", do_info_name,
1827 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001828 { "uuid", "", do_info_uuid,
1829 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001830#if defined(TARGET_PPC)
1831 { "cpustats", "", do_info_cpu_stats,
1832 "", "show CPU statistics", },
1833#endif
blueswir131a60e22007-10-26 18:42:59 +00001834#if defined(CONFIG_SLIRP)
Jan Kiszka6dbe5532009-06-24 14:42:29 +02001835 { "usernet", "", do_info_usernet,
1836 "", "show user network stack connection states", },
blueswir131a60e22007-10-26 18:42:59 +00001837#endif
aliguori5bb79102008-10-13 03:12:02 +00001838 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001839 { "balloon", "", do_info_balloon,
1840 "", "show balloon information" },
Gerd Hoffmanncae49562009-06-05 15:53:17 +01001841 { "qtree", "", do_info_qtree,
1842 "", "show device tree" },
Gerd Hoffmannf6c64e02009-08-03 15:03:09 +02001843 { "qdm", "", do_info_qdm,
1844 "", "show qdev device model list" },
bellard9dc39cb2004-03-14 21:38:27 +00001845 { NULL, NULL, },
1846};
1847
bellard9307c4c2004-04-04 12:57:25 +00001848/*******************************************************************/
1849
1850static const char *pch;
1851static jmp_buf expr_env;
1852
bellard92a31b12005-02-10 22:00:52 +00001853#define MD_TLONG 0
1854#define MD_I32 1
1855
bellard9307c4c2004-04-04 12:57:25 +00001856typedef struct MonitorDef {
1857 const char *name;
1858 int offset;
blueswir18662d652008-10-02 18:32:44 +00001859 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001860 int type;
bellard9307c4c2004-04-04 12:57:25 +00001861} MonitorDef;
1862
bellard57206fd2004-04-25 18:54:52 +00001863#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001864static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001865{
bellard6a00d602005-11-21 23:25:50 +00001866 CPUState *env = mon_get_cpu();
1867 if (!env)
1868 return 0;
1869 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001870}
1871#endif
1872
bellarda541f292004-04-12 20:39:29 +00001873#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001874static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001875{
bellard6a00d602005-11-21 23:25:50 +00001876 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001877 unsigned int u;
1878 int i;
1879
bellard6a00d602005-11-21 23:25:50 +00001880 if (!env)
1881 return 0;
1882
bellarda541f292004-04-12 20:39:29 +00001883 u = 0;
1884 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00001885 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001886
1887 return u;
1888}
1889
blueswir18662d652008-10-02 18:32:44 +00001890static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001891{
bellard6a00d602005-11-21 23:25:50 +00001892 CPUState *env = mon_get_cpu();
1893 if (!env)
1894 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001895 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001896}
1897
blueswir18662d652008-10-02 18:32:44 +00001898static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001899{
bellard6a00d602005-11-21 23:25:50 +00001900 CPUState *env = mon_get_cpu();
1901 if (!env)
1902 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001903 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001904}
bellard9fddaa02004-05-21 12:59:32 +00001905
blueswir18662d652008-10-02 18:32:44 +00001906static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001907{
bellard6a00d602005-11-21 23:25:50 +00001908 CPUState *env = mon_get_cpu();
1909 if (!env)
1910 return 0;
1911 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001912}
1913
blueswir18662d652008-10-02 18:32:44 +00001914static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001915{
bellard6a00d602005-11-21 23:25:50 +00001916 CPUState *env = mon_get_cpu();
1917 if (!env)
1918 return 0;
1919 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001920}
1921
blueswir18662d652008-10-02 18:32:44 +00001922static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001923{
bellard6a00d602005-11-21 23:25:50 +00001924 CPUState *env = mon_get_cpu();
1925 if (!env)
1926 return 0;
1927 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001928}
bellarda541f292004-04-12 20:39:29 +00001929#endif
1930
bellarde95c8d52004-09-30 22:22:08 +00001931#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001932#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001933static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001934{
bellard6a00d602005-11-21 23:25:50 +00001935 CPUState *env = mon_get_cpu();
1936 if (!env)
1937 return 0;
1938 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001939}
bellard7b936c02005-10-30 17:05:13 +00001940#endif
bellarde95c8d52004-09-30 22:22:08 +00001941
blueswir18662d652008-10-02 18:32:44 +00001942static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001943{
bellard6a00d602005-11-21 23:25:50 +00001944 CPUState *env = mon_get_cpu();
1945 if (!env)
1946 return 0;
1947 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001948}
1949#endif
1950
blueswir18662d652008-10-02 18:32:44 +00001951static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001952#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001953
1954#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001955 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001956 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001957 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001958
bellard9307c4c2004-04-04 12:57:25 +00001959 { "eax", offsetof(CPUState, regs[0]) },
1960 { "ecx", offsetof(CPUState, regs[1]) },
1961 { "edx", offsetof(CPUState, regs[2]) },
1962 { "ebx", offsetof(CPUState, regs[3]) },
1963 { "esp|sp", offsetof(CPUState, regs[4]) },
1964 { "ebp|fp", offsetof(CPUState, regs[5]) },
1965 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001966 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001967#ifdef TARGET_X86_64
1968 { "r8", offsetof(CPUState, regs[8]) },
1969 { "r9", offsetof(CPUState, regs[9]) },
1970 { "r10", offsetof(CPUState, regs[10]) },
1971 { "r11", offsetof(CPUState, regs[11]) },
1972 { "r12", offsetof(CPUState, regs[12]) },
1973 { "r13", offsetof(CPUState, regs[13]) },
1974 { "r14", offsetof(CPUState, regs[14]) },
1975 { "r15", offsetof(CPUState, regs[15]) },
1976#endif
bellard9307c4c2004-04-04 12:57:25 +00001977 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001978 { "eip", offsetof(CPUState, eip) },
1979 SEG("cs", R_CS)
1980 SEG("ds", R_DS)
1981 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001982 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001983 SEG("fs", R_FS)
1984 SEG("gs", R_GS)
1985 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00001986#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00001987 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00001988 { "r0", offsetof(CPUState, gpr[0]) },
1989 { "r1", offsetof(CPUState, gpr[1]) },
1990 { "r2", offsetof(CPUState, gpr[2]) },
1991 { "r3", offsetof(CPUState, gpr[3]) },
1992 { "r4", offsetof(CPUState, gpr[4]) },
1993 { "r5", offsetof(CPUState, gpr[5]) },
1994 { "r6", offsetof(CPUState, gpr[6]) },
1995 { "r7", offsetof(CPUState, gpr[7]) },
1996 { "r8", offsetof(CPUState, gpr[8]) },
1997 { "r9", offsetof(CPUState, gpr[9]) },
1998 { "r10", offsetof(CPUState, gpr[10]) },
1999 { "r11", offsetof(CPUState, gpr[11]) },
2000 { "r12", offsetof(CPUState, gpr[12]) },
2001 { "r13", offsetof(CPUState, gpr[13]) },
2002 { "r14", offsetof(CPUState, gpr[14]) },
2003 { "r15", offsetof(CPUState, gpr[15]) },
2004 { "r16", offsetof(CPUState, gpr[16]) },
2005 { "r17", offsetof(CPUState, gpr[17]) },
2006 { "r18", offsetof(CPUState, gpr[18]) },
2007 { "r19", offsetof(CPUState, gpr[19]) },
2008 { "r20", offsetof(CPUState, gpr[20]) },
2009 { "r21", offsetof(CPUState, gpr[21]) },
2010 { "r22", offsetof(CPUState, gpr[22]) },
2011 { "r23", offsetof(CPUState, gpr[23]) },
2012 { "r24", offsetof(CPUState, gpr[24]) },
2013 { "r25", offsetof(CPUState, gpr[25]) },
2014 { "r26", offsetof(CPUState, gpr[26]) },
2015 { "r27", offsetof(CPUState, gpr[27]) },
2016 { "r28", offsetof(CPUState, gpr[28]) },
2017 { "r29", offsetof(CPUState, gpr[29]) },
2018 { "r30", offsetof(CPUState, gpr[30]) },
2019 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002020 /* Floating point registers */
2021 { "f0", offsetof(CPUState, fpr[0]) },
2022 { "f1", offsetof(CPUState, fpr[1]) },
2023 { "f2", offsetof(CPUState, fpr[2]) },
2024 { "f3", offsetof(CPUState, fpr[3]) },
2025 { "f4", offsetof(CPUState, fpr[4]) },
2026 { "f5", offsetof(CPUState, fpr[5]) },
2027 { "f6", offsetof(CPUState, fpr[6]) },
2028 { "f7", offsetof(CPUState, fpr[7]) },
2029 { "f8", offsetof(CPUState, fpr[8]) },
2030 { "f9", offsetof(CPUState, fpr[9]) },
2031 { "f10", offsetof(CPUState, fpr[10]) },
2032 { "f11", offsetof(CPUState, fpr[11]) },
2033 { "f12", offsetof(CPUState, fpr[12]) },
2034 { "f13", offsetof(CPUState, fpr[13]) },
2035 { "f14", offsetof(CPUState, fpr[14]) },
2036 { "f15", offsetof(CPUState, fpr[15]) },
2037 { "f16", offsetof(CPUState, fpr[16]) },
2038 { "f17", offsetof(CPUState, fpr[17]) },
2039 { "f18", offsetof(CPUState, fpr[18]) },
2040 { "f19", offsetof(CPUState, fpr[19]) },
2041 { "f20", offsetof(CPUState, fpr[20]) },
2042 { "f21", offsetof(CPUState, fpr[21]) },
2043 { "f22", offsetof(CPUState, fpr[22]) },
2044 { "f23", offsetof(CPUState, fpr[23]) },
2045 { "f24", offsetof(CPUState, fpr[24]) },
2046 { "f25", offsetof(CPUState, fpr[25]) },
2047 { "f26", offsetof(CPUState, fpr[26]) },
2048 { "f27", offsetof(CPUState, fpr[27]) },
2049 { "f28", offsetof(CPUState, fpr[28]) },
2050 { "f29", offsetof(CPUState, fpr[29]) },
2051 { "f30", offsetof(CPUState, fpr[30]) },
2052 { "f31", offsetof(CPUState, fpr[31]) },
2053 { "fpscr", offsetof(CPUState, fpscr) },
2054 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002055 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002056 { "lr", offsetof(CPUState, lr) },
2057 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002058 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002059 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002060 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002061 { "msr", 0, &monitor_get_msr, },
2062 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002063 { "tbu", 0, &monitor_get_tbu, },
2064 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002065#if defined(TARGET_PPC64)
2066 /* Address space register */
2067 { "asr", offsetof(CPUState, asr) },
2068#endif
2069 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002070 { "sdr1", offsetof(CPUState, sdr1) },
2071 { "sr0", offsetof(CPUState, sr[0]) },
2072 { "sr1", offsetof(CPUState, sr[1]) },
2073 { "sr2", offsetof(CPUState, sr[2]) },
2074 { "sr3", offsetof(CPUState, sr[3]) },
2075 { "sr4", offsetof(CPUState, sr[4]) },
2076 { "sr5", offsetof(CPUState, sr[5]) },
2077 { "sr6", offsetof(CPUState, sr[6]) },
2078 { "sr7", offsetof(CPUState, sr[7]) },
2079 { "sr8", offsetof(CPUState, sr[8]) },
2080 { "sr9", offsetof(CPUState, sr[9]) },
2081 { "sr10", offsetof(CPUState, sr[10]) },
2082 { "sr11", offsetof(CPUState, sr[11]) },
2083 { "sr12", offsetof(CPUState, sr[12]) },
2084 { "sr13", offsetof(CPUState, sr[13]) },
2085 { "sr14", offsetof(CPUState, sr[14]) },
2086 { "sr15", offsetof(CPUState, sr[15]) },
2087 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002088#elif defined(TARGET_SPARC)
2089 { "g0", offsetof(CPUState, gregs[0]) },
2090 { "g1", offsetof(CPUState, gregs[1]) },
2091 { "g2", offsetof(CPUState, gregs[2]) },
2092 { "g3", offsetof(CPUState, gregs[3]) },
2093 { "g4", offsetof(CPUState, gregs[4]) },
2094 { "g5", offsetof(CPUState, gregs[5]) },
2095 { "g6", offsetof(CPUState, gregs[6]) },
2096 { "g7", offsetof(CPUState, gregs[7]) },
2097 { "o0", 0, monitor_get_reg },
2098 { "o1", 1, monitor_get_reg },
2099 { "o2", 2, monitor_get_reg },
2100 { "o3", 3, monitor_get_reg },
2101 { "o4", 4, monitor_get_reg },
2102 { "o5", 5, monitor_get_reg },
2103 { "o6", 6, monitor_get_reg },
2104 { "o7", 7, monitor_get_reg },
2105 { "l0", 8, monitor_get_reg },
2106 { "l1", 9, monitor_get_reg },
2107 { "l2", 10, monitor_get_reg },
2108 { "l3", 11, monitor_get_reg },
2109 { "l4", 12, monitor_get_reg },
2110 { "l5", 13, monitor_get_reg },
2111 { "l6", 14, monitor_get_reg },
2112 { "l7", 15, monitor_get_reg },
2113 { "i0", 16, monitor_get_reg },
2114 { "i1", 17, monitor_get_reg },
2115 { "i2", 18, monitor_get_reg },
2116 { "i3", 19, monitor_get_reg },
2117 { "i4", 20, monitor_get_reg },
2118 { "i5", 21, monitor_get_reg },
2119 { "i6", 22, monitor_get_reg },
2120 { "i7", 23, monitor_get_reg },
2121 { "pc", offsetof(CPUState, pc) },
2122 { "npc", offsetof(CPUState, npc) },
2123 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002124#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002125 { "psr", 0, &monitor_get_psr, },
2126 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002127#endif
bellarde95c8d52004-09-30 22:22:08 +00002128 { "tbr", offsetof(CPUState, tbr) },
2129 { "fsr", offsetof(CPUState, fsr) },
2130 { "f0", offsetof(CPUState, fpr[0]) },
2131 { "f1", offsetof(CPUState, fpr[1]) },
2132 { "f2", offsetof(CPUState, fpr[2]) },
2133 { "f3", offsetof(CPUState, fpr[3]) },
2134 { "f4", offsetof(CPUState, fpr[4]) },
2135 { "f5", offsetof(CPUState, fpr[5]) },
2136 { "f6", offsetof(CPUState, fpr[6]) },
2137 { "f7", offsetof(CPUState, fpr[7]) },
2138 { "f8", offsetof(CPUState, fpr[8]) },
2139 { "f9", offsetof(CPUState, fpr[9]) },
2140 { "f10", offsetof(CPUState, fpr[10]) },
2141 { "f11", offsetof(CPUState, fpr[11]) },
2142 { "f12", offsetof(CPUState, fpr[12]) },
2143 { "f13", offsetof(CPUState, fpr[13]) },
2144 { "f14", offsetof(CPUState, fpr[14]) },
2145 { "f15", offsetof(CPUState, fpr[15]) },
2146 { "f16", offsetof(CPUState, fpr[16]) },
2147 { "f17", offsetof(CPUState, fpr[17]) },
2148 { "f18", offsetof(CPUState, fpr[18]) },
2149 { "f19", offsetof(CPUState, fpr[19]) },
2150 { "f20", offsetof(CPUState, fpr[20]) },
2151 { "f21", offsetof(CPUState, fpr[21]) },
2152 { "f22", offsetof(CPUState, fpr[22]) },
2153 { "f23", offsetof(CPUState, fpr[23]) },
2154 { "f24", offsetof(CPUState, fpr[24]) },
2155 { "f25", offsetof(CPUState, fpr[25]) },
2156 { "f26", offsetof(CPUState, fpr[26]) },
2157 { "f27", offsetof(CPUState, fpr[27]) },
2158 { "f28", offsetof(CPUState, fpr[28]) },
2159 { "f29", offsetof(CPUState, fpr[29]) },
2160 { "f30", offsetof(CPUState, fpr[30]) },
2161 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002162#ifdef TARGET_SPARC64
2163 { "f32", offsetof(CPUState, fpr[32]) },
2164 { "f34", offsetof(CPUState, fpr[34]) },
2165 { "f36", offsetof(CPUState, fpr[36]) },
2166 { "f38", offsetof(CPUState, fpr[38]) },
2167 { "f40", offsetof(CPUState, fpr[40]) },
2168 { "f42", offsetof(CPUState, fpr[42]) },
2169 { "f44", offsetof(CPUState, fpr[44]) },
2170 { "f46", offsetof(CPUState, fpr[46]) },
2171 { "f48", offsetof(CPUState, fpr[48]) },
2172 { "f50", offsetof(CPUState, fpr[50]) },
2173 { "f52", offsetof(CPUState, fpr[52]) },
2174 { "f54", offsetof(CPUState, fpr[54]) },
2175 { "f56", offsetof(CPUState, fpr[56]) },
2176 { "f58", offsetof(CPUState, fpr[58]) },
2177 { "f60", offsetof(CPUState, fpr[60]) },
2178 { "f62", offsetof(CPUState, fpr[62]) },
2179 { "asi", offsetof(CPUState, asi) },
2180 { "pstate", offsetof(CPUState, pstate) },
2181 { "cansave", offsetof(CPUState, cansave) },
2182 { "canrestore", offsetof(CPUState, canrestore) },
2183 { "otherwin", offsetof(CPUState, otherwin) },
2184 { "wstate", offsetof(CPUState, wstate) },
2185 { "cleanwin", offsetof(CPUState, cleanwin) },
2186 { "fprs", offsetof(CPUState, fprs) },
2187#endif
bellard9307c4c2004-04-04 12:57:25 +00002188#endif
2189 { NULL },
2190};
2191
aliguori376253e2009-03-05 23:01:23 +00002192static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002193{
aliguori376253e2009-03-05 23:01:23 +00002194 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002195 longjmp(expr_env, 1);
2196}
2197
bellard6a00d602005-11-21 23:25:50 +00002198/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002199static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002200{
blueswir18662d652008-10-02 18:32:44 +00002201 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002202 void *ptr;
2203
bellard9307c4c2004-04-04 12:57:25 +00002204 for(md = monitor_defs; md->name != NULL; md++) {
2205 if (compare_cmd(name, md->name)) {
2206 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002207 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002208 } else {
bellard6a00d602005-11-21 23:25:50 +00002209 CPUState *env = mon_get_cpu();
2210 if (!env)
2211 return -2;
2212 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002213 switch(md->type) {
2214 case MD_I32:
2215 *pval = *(int32_t *)ptr;
2216 break;
2217 case MD_TLONG:
2218 *pval = *(target_long *)ptr;
2219 break;
2220 default:
2221 *pval = 0;
2222 break;
2223 }
bellard9307c4c2004-04-04 12:57:25 +00002224 }
2225 return 0;
2226 }
2227 }
2228 return -1;
2229}
2230
2231static void next(void)
2232{
Blue Swirl660f11b2009-07-31 21:16:51 +00002233 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002234 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002235 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002236 pch++;
2237 }
2238}
2239
aliguori376253e2009-03-05 23:01:23 +00002240static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002241
aliguori376253e2009-03-05 23:01:23 +00002242static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002243{
blueswir1c2efc952007-09-25 17:28:42 +00002244 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002245 char *p;
bellard6a00d602005-11-21 23:25:50 +00002246 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002247
2248 switch(*pch) {
2249 case '+':
2250 next();
aliguori376253e2009-03-05 23:01:23 +00002251 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002252 break;
2253 case '-':
2254 next();
aliguori376253e2009-03-05 23:01:23 +00002255 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002256 break;
2257 case '~':
2258 next();
aliguori376253e2009-03-05 23:01:23 +00002259 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002260 break;
2261 case '(':
2262 next();
aliguori376253e2009-03-05 23:01:23 +00002263 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002264 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002265 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002266 }
2267 next();
2268 break;
bellard81d09122004-07-14 17:21:37 +00002269 case '\'':
2270 pch++;
2271 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002272 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002273 n = *pch;
2274 pch++;
2275 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002276 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002277 next();
2278 break;
bellard9307c4c2004-04-04 12:57:25 +00002279 case '$':
2280 {
2281 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002282 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002283
bellard9307c4c2004-04-04 12:57:25 +00002284 pch++;
2285 q = buf;
2286 while ((*pch >= 'a' && *pch <= 'z') ||
2287 (*pch >= 'A' && *pch <= 'Z') ||
2288 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002289 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002290 if ((q - buf) < sizeof(buf) - 1)
2291 *q++ = *pch;
2292 pch++;
2293 }
blueswir1cd390082008-11-16 13:53:32 +00002294 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002295 pch++;
2296 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002297 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002298 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002299 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002300 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002301 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002302 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002303 }
2304 break;
2305 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002306 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002307 n = 0;
2308 break;
2309 default:
blueswir17743e582007-09-24 18:39:04 +00002310#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002311 n = strtoull(pch, &p, 0);
2312#else
bellard9307c4c2004-04-04 12:57:25 +00002313 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002314#endif
bellard9307c4c2004-04-04 12:57:25 +00002315 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002316 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002317 }
2318 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002319 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002320 pch++;
2321 break;
2322 }
2323 return n;
2324}
2325
2326
aliguori376253e2009-03-05 23:01:23 +00002327static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002328{
blueswir1c2efc952007-09-25 17:28:42 +00002329 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002330 int op;
ths3b46e622007-09-17 08:09:54 +00002331
aliguori376253e2009-03-05 23:01:23 +00002332 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002333 for(;;) {
2334 op = *pch;
2335 if (op != '*' && op != '/' && op != '%')
2336 break;
2337 next();
aliguori376253e2009-03-05 23:01:23 +00002338 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002339 switch(op) {
2340 default:
2341 case '*':
2342 val *= val2;
2343 break;
2344 case '/':
2345 case '%':
ths5fafdf22007-09-16 21:08:06 +00002346 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002347 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002348 if (op == '/')
2349 val /= val2;
2350 else
2351 val %= val2;
2352 break;
2353 }
2354 }
2355 return val;
2356}
2357
aliguori376253e2009-03-05 23:01:23 +00002358static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002359{
blueswir1c2efc952007-09-25 17:28:42 +00002360 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002361 int op;
bellard9307c4c2004-04-04 12:57:25 +00002362
aliguori376253e2009-03-05 23:01:23 +00002363 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002364 for(;;) {
2365 op = *pch;
2366 if (op != '&' && op != '|' && op != '^')
2367 break;
2368 next();
aliguori376253e2009-03-05 23:01:23 +00002369 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002370 switch(op) {
2371 default:
2372 case '&':
2373 val &= val2;
2374 break;
2375 case '|':
2376 val |= val2;
2377 break;
2378 case '^':
2379 val ^= val2;
2380 break;
2381 }
2382 }
2383 return val;
2384}
2385
aliguori376253e2009-03-05 23:01:23 +00002386static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002387{
blueswir1c2efc952007-09-25 17:28:42 +00002388 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002389 int op;
bellard9307c4c2004-04-04 12:57:25 +00002390
aliguori376253e2009-03-05 23:01:23 +00002391 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002392 for(;;) {
2393 op = *pch;
2394 if (op != '+' && op != '-')
2395 break;
2396 next();
aliguori376253e2009-03-05 23:01:23 +00002397 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002398 if (op == '+')
2399 val += val2;
2400 else
2401 val -= val2;
2402 }
2403 return val;
2404}
2405
aliguori376253e2009-03-05 23:01:23 +00002406static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002407{
2408 pch = *pp;
2409 if (setjmp(expr_env)) {
2410 *pp = pch;
2411 return -1;
2412 }
blueswir1cd390082008-11-16 13:53:32 +00002413 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002414 pch++;
aliguori376253e2009-03-05 23:01:23 +00002415 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002416 *pp = pch;
2417 return 0;
2418}
2419
2420static int get_str(char *buf, int buf_size, const char **pp)
2421{
2422 const char *p;
2423 char *q;
2424 int c;
2425
bellard81d09122004-07-14 17:21:37 +00002426 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002427 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002428 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002429 p++;
2430 if (*p == '\0') {
2431 fail:
bellard81d09122004-07-14 17:21:37 +00002432 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002433 *pp = p;
2434 return -1;
2435 }
bellard9307c4c2004-04-04 12:57:25 +00002436 if (*p == '\"') {
2437 p++;
2438 while (*p != '\0' && *p != '\"') {
2439 if (*p == '\\') {
2440 p++;
2441 c = *p++;
2442 switch(c) {
2443 case 'n':
2444 c = '\n';
2445 break;
2446 case 'r':
2447 c = '\r';
2448 break;
2449 case '\\':
2450 case '\'':
2451 case '\"':
2452 break;
2453 default:
2454 qemu_printf("unsupported escape code: '\\%c'\n", c);
2455 goto fail;
2456 }
2457 if ((q - buf) < buf_size - 1) {
2458 *q++ = c;
2459 }
2460 } else {
2461 if ((q - buf) < buf_size - 1) {
2462 *q++ = *p;
2463 }
2464 p++;
2465 }
2466 }
2467 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002468 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002469 goto fail;
2470 }
2471 p++;
2472 } else {
blueswir1cd390082008-11-16 13:53:32 +00002473 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002474 if ((q - buf) < buf_size - 1) {
2475 *q++ = *p;
2476 }
2477 p++;
2478 }
bellard9307c4c2004-04-04 12:57:25 +00002479 }
bellard81d09122004-07-14 17:21:37 +00002480 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002481 *pp = p;
2482 return 0;
2483}
2484
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002485/*
2486 * Store the command-name in cmdname, and return a pointer to
2487 * the remaining of the command string.
2488 */
2489static const char *get_command_name(const char *cmdline,
2490 char *cmdname, size_t nlen)
2491{
2492 size_t len;
2493 const char *p, *pstart;
2494
2495 p = cmdline;
2496 while (qemu_isspace(*p))
2497 p++;
2498 if (*p == '\0')
2499 return NULL;
2500 pstart = p;
2501 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2502 p++;
2503 len = p - pstart;
2504 if (len > nlen - 1)
2505 len = nlen - 1;
2506 memcpy(cmdname, pstart, len);
2507 cmdname[len] = '\0';
2508 return p;
2509}
2510
bellard9307c4c2004-04-04 12:57:25 +00002511static int default_fmt_format = 'x';
2512static int default_fmt_size = 4;
2513
2514#define MAX_ARGS 16
2515
aliguori376253e2009-03-05 23:01:23 +00002516static void monitor_handle_command(Monitor *mon, const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002517{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002518 const char *p, *typestr;
2519 int c, nb_args, i, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002520 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002521 char cmdname[256];
2522 char buf[1024];
2523 void *str_allocated[MAX_ARGS];
2524 void *args[MAX_ARGS];
aliguori376253e2009-03-05 23:01:23 +00002525 void (*handler_0)(Monitor *mon);
2526 void (*handler_1)(Monitor *mon, void *arg0);
2527 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2528 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2529 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2530 void *arg3);
2531 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2532 void *arg3, void *arg4);
2533 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2534 void *arg3, void *arg4, void *arg5);
2535 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2536 void *arg3, void *arg4, void *arg5, void *arg6);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002537 void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2538 void *arg3, void *arg4, void *arg5, void *arg6,
2539 void *arg7);
2540 void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2541 void *arg3, void *arg4, void *arg5, void *arg6,
2542 void *arg7, void *arg8);
2543 void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2544 void *arg3, void *arg4, void *arg5, void *arg6,
2545 void *arg7, void *arg8, void *arg9);
bellard9dc39cb2004-03-14 21:38:27 +00002546
2547#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002548 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002549#endif
ths3b46e622007-09-17 08:09:54 +00002550
bellard9307c4c2004-04-04 12:57:25 +00002551 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002552 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2553 if (!p)
bellard9dc39cb2004-03-14 21:38:27 +00002554 return;
ths3b46e622007-09-17 08:09:54 +00002555
bellard9307c4c2004-04-04 12:57:25 +00002556 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002557 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002558 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002559 break;
bellard9dc39cb2004-03-14 21:38:27 +00002560 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002561
2562 if (cmd->name == NULL) {
2563 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2564 return;
2565 }
bellard9307c4c2004-04-04 12:57:25 +00002566
2567 for(i = 0; i < MAX_ARGS; i++)
2568 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002569
bellard9307c4c2004-04-04 12:57:25 +00002570 /* parse the parameters */
2571 typestr = cmd->args_type;
2572 nb_args = 0;
2573 for(;;) {
2574 c = *typestr;
2575 if (c == '\0')
2576 break;
2577 typestr++;
2578 switch(c) {
2579 case 'F':
bellard81d09122004-07-14 17:21:37 +00002580 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002581 case 's':
2582 {
2583 int ret;
2584 char *str;
ths3b46e622007-09-17 08:09:54 +00002585
blueswir1cd390082008-11-16 13:53:32 +00002586 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002587 p++;
2588 if (*typestr == '?') {
2589 typestr++;
2590 if (*p == '\0') {
2591 /* no optional string: NULL argument */
2592 str = NULL;
2593 goto add_str;
2594 }
2595 }
2596 ret = get_str(buf, sizeof(buf), &p);
2597 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002598 switch(c) {
2599 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002600 monitor_printf(mon, "%s: filename expected\n",
2601 cmdname);
bellard81d09122004-07-14 17:21:37 +00002602 break;
2603 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002604 monitor_printf(mon, "%s: block device name expected\n",
2605 cmdname);
bellard81d09122004-07-14 17:21:37 +00002606 break;
2607 default:
aliguori376253e2009-03-05 23:01:23 +00002608 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002609 break;
2610 }
bellard9307c4c2004-04-04 12:57:25 +00002611 goto fail;
2612 }
2613 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002614 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002615 str_allocated[nb_args] = str;
2616 add_str:
2617 if (nb_args >= MAX_ARGS) {
2618 error_args:
aliguori376253e2009-03-05 23:01:23 +00002619 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002620 goto fail;
2621 }
2622 args[nb_args++] = str;
2623 }
2624 break;
2625 case '/':
2626 {
2627 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002628
blueswir1cd390082008-11-16 13:53:32 +00002629 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002630 p++;
2631 if (*p == '/') {
2632 /* format found */
2633 p++;
2634 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002635 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002636 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002637 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002638 count = count * 10 + (*p - '0');
2639 p++;
2640 }
2641 }
2642 size = -1;
2643 format = -1;
2644 for(;;) {
2645 switch(*p) {
2646 case 'o':
2647 case 'd':
2648 case 'u':
2649 case 'x':
2650 case 'i':
2651 case 'c':
2652 format = *p++;
2653 break;
2654 case 'b':
2655 size = 1;
2656 p++;
2657 break;
2658 case 'h':
2659 size = 2;
2660 p++;
2661 break;
2662 case 'w':
2663 size = 4;
2664 p++;
2665 break;
2666 case 'g':
2667 case 'L':
2668 size = 8;
2669 p++;
2670 break;
2671 default:
2672 goto next;
2673 }
2674 }
2675 next:
blueswir1cd390082008-11-16 13:53:32 +00002676 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002677 monitor_printf(mon, "invalid char in format: '%c'\n",
2678 *p);
bellard9307c4c2004-04-04 12:57:25 +00002679 goto fail;
2680 }
bellard9307c4c2004-04-04 12:57:25 +00002681 if (format < 0)
2682 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002683 if (format != 'i') {
2684 /* for 'i', not specifying a size gives -1 as size */
2685 if (size < 0)
2686 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002687 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002688 }
bellard9307c4c2004-04-04 12:57:25 +00002689 default_fmt_format = format;
2690 } else {
2691 count = 1;
2692 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002693 if (format != 'i') {
2694 size = default_fmt_size;
2695 } else {
2696 size = -1;
2697 }
bellard9307c4c2004-04-04 12:57:25 +00002698 }
2699 if (nb_args + 3 > MAX_ARGS)
2700 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002701 args[nb_args++] = (void*)(long)count;
2702 args[nb_args++] = (void*)(long)format;
2703 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002704 }
2705 break;
2706 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002707 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002708 {
blueswir1c2efc952007-09-25 17:28:42 +00002709 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002710
blueswir1cd390082008-11-16 13:53:32 +00002711 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002712 p++;
bellard34405572004-06-08 00:55:58 +00002713 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002714 if (*typestr == '?') {
2715 if (*p == '\0')
2716 has_arg = 0;
2717 else
2718 has_arg = 1;
2719 } else {
2720 if (*p == '.') {
2721 p++;
blueswir1cd390082008-11-16 13:53:32 +00002722 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002723 p++;
2724 has_arg = 1;
2725 } else {
2726 has_arg = 0;
2727 }
2728 }
bellard13224a82006-07-14 22:03:35 +00002729 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002730 if (nb_args >= MAX_ARGS)
2731 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002732 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002733 if (!has_arg) {
2734 if (nb_args >= MAX_ARGS)
2735 goto error_args;
2736 val = -1;
2737 goto add_num;
2738 }
2739 }
aliguori376253e2009-03-05 23:01:23 +00002740 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002741 goto fail;
2742 add_num:
bellard92a31b12005-02-10 22:00:52 +00002743 if (c == 'i') {
2744 if (nb_args >= MAX_ARGS)
2745 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002746 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002747 } else {
2748 if ((nb_args + 1) >= MAX_ARGS)
2749 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002750#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002751 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002752#else
2753 args[nb_args++] = (void *)0;
2754#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002755 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002756 }
bellard9307c4c2004-04-04 12:57:25 +00002757 }
2758 break;
2759 case '-':
2760 {
2761 int has_option;
2762 /* option */
ths3b46e622007-09-17 08:09:54 +00002763
bellard9307c4c2004-04-04 12:57:25 +00002764 c = *typestr++;
2765 if (c == '\0')
2766 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002767 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002768 p++;
2769 has_option = 0;
2770 if (*p == '-') {
2771 p++;
2772 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002773 monitor_printf(mon, "%s: unsupported option -%c\n",
2774 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002775 goto fail;
2776 }
2777 p++;
2778 has_option = 1;
2779 }
2780 if (nb_args >= MAX_ARGS)
2781 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002782 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002783 }
2784 break;
2785 default:
2786 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002787 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002788 goto fail;
2789 }
2790 }
2791 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002792 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002793 p++;
2794 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002795 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2796 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002797 goto fail;
2798 }
2799
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002800 qemu_errors_to_mon(mon);
bellard9307c4c2004-04-04 12:57:25 +00002801 switch(nb_args) {
2802 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002803 handler_0 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002804 handler_0(mon);
bellard9307c4c2004-04-04 12:57:25 +00002805 break;
2806 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002807 handler_1 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002808 handler_1(mon, args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002809 break;
2810 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002811 handler_2 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002812 handler_2(mon, args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002813 break;
2814 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002815 handler_3 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002816 handler_3(mon, args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002817 break;
2818 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002819 handler_4 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002820 handler_4(mon, args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002821 break;
2822 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002823 handler_5 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002824 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002825 break;
bellard34405572004-06-08 00:55:58 +00002826 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002827 handler_6 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002828 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002829 break;
bellardec36b692006-07-16 18:57:03 +00002830 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002831 handler_7 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002832 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2833 args[6]);
bellardec36b692006-07-16 18:57:03 +00002834 break;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002835 case 8:
2836 handler_8 = cmd->handler;
2837 handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2838 args[6], args[7]);
2839 break;
2840 case 9:
2841 handler_9 = cmd->handler;
2842 handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2843 args[6], args[7], args[8]);
2844 break;
2845 case 10:
2846 handler_10 = cmd->handler;
2847 handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2848 args[6], args[7], args[8], args[9]);
2849 break;
bellard9307c4c2004-04-04 12:57:25 +00002850 default:
aliguori376253e2009-03-05 23:01:23 +00002851 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002852 break;
bellard9307c4c2004-04-04 12:57:25 +00002853 }
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002854 qemu_errors_to_previous();
2855
bellard9307c4c2004-04-04 12:57:25 +00002856 fail:
2857 for(i = 0; i < MAX_ARGS; i++)
2858 qemu_free(str_allocated[i]);
bellard9dc39cb2004-03-14 21:38:27 +00002859}
2860
bellard81d09122004-07-14 17:21:37 +00002861static void cmd_completion(const char *name, const char *list)
2862{
2863 const char *p, *pstart;
2864 char cmd[128];
2865 int len;
2866
2867 p = list;
2868 for(;;) {
2869 pstart = p;
2870 p = strchr(p, '|');
2871 if (!p)
2872 p = pstart + strlen(pstart);
2873 len = p - pstart;
2874 if (len > sizeof(cmd) - 2)
2875 len = sizeof(cmd) - 2;
2876 memcpy(cmd, pstart, len);
2877 cmd[len] = '\0';
2878 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002879 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002880 }
2881 if (*p == '\0')
2882 break;
2883 p++;
2884 }
2885}
2886
2887static void file_completion(const char *input)
2888{
2889 DIR *ffs;
2890 struct dirent *d;
2891 char path[1024];
2892 char file[1024], file_prefix[1024];
2893 int input_path_len;
2894 const char *p;
2895
ths5fafdf22007-09-16 21:08:06 +00002896 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002897 if (!p) {
2898 input_path_len = 0;
2899 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002900 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002901 } else {
2902 input_path_len = p - input + 1;
2903 memcpy(path, input, input_path_len);
2904 if (input_path_len > sizeof(path) - 1)
2905 input_path_len = sizeof(path) - 1;
2906 path[input_path_len] = '\0';
2907 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2908 }
2909#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002910 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2911 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002912#endif
2913 ffs = opendir(path);
2914 if (!ffs)
2915 return;
2916 for(;;) {
2917 struct stat sb;
2918 d = readdir(ffs);
2919 if (!d)
2920 break;
2921 if (strstart(d->d_name, file_prefix, NULL)) {
2922 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002923 if (input_path_len < sizeof(file))
2924 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2925 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002926 /* stat the file to find out if it's a directory.
2927 * In that case add a slash to speed up typing long paths
2928 */
2929 stat(file, &sb);
2930 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002931 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00002932 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002933 }
2934 }
2935 closedir(ffs);
2936}
2937
aliguori51de9762009-03-05 23:00:43 +00002938static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002939{
aliguori51de9762009-03-05 23:00:43 +00002940 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002941 const char *input = opaque;
2942
2943 if (input[0] == '\0' ||
2944 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00002945 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00002946 }
2947}
2948
2949/* NOTE: this parser is an approximate form of the real command parser */
2950static void parse_cmdline(const char *cmdline,
2951 int *pnb_args, char **args)
2952{
2953 const char *p;
2954 int nb_args, ret;
2955 char buf[1024];
2956
2957 p = cmdline;
2958 nb_args = 0;
2959 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002960 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002961 p++;
2962 if (*p == '\0')
2963 break;
2964 if (nb_args >= MAX_ARGS)
2965 break;
2966 ret = get_str(buf, sizeof(buf), &p);
2967 args[nb_args] = qemu_strdup(buf);
2968 nb_args++;
2969 if (ret < 0)
2970 break;
2971 }
2972 *pnb_args = nb_args;
2973}
2974
aliguori4c36ba32009-03-05 23:01:37 +00002975static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002976{
2977 const char *cmdname;
2978 char *args[MAX_ARGS];
2979 int nb_args, i, len;
2980 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00002981 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002982 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002983
2984 parse_cmdline(cmdline, &nb_args, args);
2985#ifdef DEBUG_COMPLETION
2986 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00002987 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00002988 }
2989#endif
2990
2991 /* if the line ends with a space, it means we want to complete the
2992 next arg */
2993 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002994 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002995 if (nb_args >= MAX_ARGS)
2996 return;
2997 args[nb_args++] = qemu_strdup("");
2998 }
2999 if (nb_args <= 1) {
3000 /* command completion */
3001 if (nb_args == 0)
3002 cmdname = "";
3003 else
3004 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003005 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003006 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003007 cmd_completion(cmdname, cmd->name);
3008 }
3009 } else {
3010 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003011 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003012 if (compare_cmd(args[0], cmd->name))
3013 goto found;
3014 }
3015 return;
3016 found:
3017 ptype = cmd->args_type;
3018 for(i = 0; i < nb_args - 2; i++) {
3019 if (*ptype != '\0') {
3020 ptype++;
3021 while (*ptype == '?')
3022 ptype++;
3023 }
3024 }
3025 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003026 if (*ptype == '-' && ptype[1] != '\0') {
3027 ptype += 2;
3028 }
bellard81d09122004-07-14 17:21:37 +00003029 switch(*ptype) {
3030 case 'F':
3031 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003032 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003033 file_completion(str);
3034 break;
3035 case 'B':
3036 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003037 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003038 bdrv_iterate(block_completion_it, (void *)str);
3039 break;
bellard7fe48482004-10-09 18:08:01 +00003040 case 's':
3041 /* XXX: more generic ? */
3042 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003043 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003044 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3045 cmd_completion(str, cmd->name);
3046 }
bellard64866c32006-05-07 18:03:31 +00003047 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003048 char *sep = strrchr(str, '-');
3049 if (sep)
3050 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003051 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003052 for(key = key_defs; key->name != NULL; key++) {
3053 cmd_completion(str, key->name);
3054 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003055 } else if (!strcmp(cmd->name, "help|?")) {
3056 readline_set_completion_index(cur_mon->rs, strlen(str));
3057 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3058 cmd_completion(str, cmd->name);
3059 }
bellard7fe48482004-10-09 18:08:01 +00003060 }
3061 break;
bellard81d09122004-07-14 17:21:37 +00003062 default:
3063 break;
3064 }
3065 }
3066 for(i = 0; i < nb_args; i++)
3067 qemu_free(args[i]);
3068}
3069
aliguori731b0362009-03-05 23:01:42 +00003070static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003071{
aliguori731b0362009-03-05 23:01:42 +00003072 Monitor *mon = opaque;
3073
3074 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003075}
3076
aliguori731b0362009-03-05 23:01:42 +00003077static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003078{
aliguori731b0362009-03-05 23:01:42 +00003079 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003080 int i;
aliguori376253e2009-03-05 23:01:23 +00003081
aliguori731b0362009-03-05 23:01:42 +00003082 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003083
aliguoricde76ee2009-03-05 23:01:51 +00003084 if (cur_mon->rs) {
3085 for (i = 0; i < size; i++)
3086 readline_handle_byte(cur_mon->rs, buf[i]);
3087 } else {
3088 if (size == 0 || buf[size - 1] != 0)
3089 monitor_printf(cur_mon, "corrupted command\n");
3090 else
3091 monitor_handle_command(cur_mon, (char *)buf);
3092 }
aliguori731b0362009-03-05 23:01:42 +00003093
3094 cur_mon = old_mon;
3095}
aliguorid8f44602008-10-06 13:52:44 +00003096
aliguori376253e2009-03-05 23:01:23 +00003097static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003098{
aliguori731b0362009-03-05 23:01:42 +00003099 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003100 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003101 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003102}
3103
aliguoricde76ee2009-03-05 23:01:51 +00003104int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003105{
aliguoricde76ee2009-03-05 23:01:51 +00003106 if (!mon->rs)
3107 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003108 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003109 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003110}
3111
aliguori376253e2009-03-05 23:01:23 +00003112void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003113{
aliguoricde76ee2009-03-05 23:01:51 +00003114 if (!mon->rs)
3115 return;
aliguori731b0362009-03-05 23:01:42 +00003116 if (--mon->suspend_cnt == 0)
3117 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003118}
3119
aliguori731b0362009-03-05 23:01:42 +00003120static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003121{
aliguori376253e2009-03-05 23:01:23 +00003122 Monitor *mon = opaque;
3123
aliguori2724b182009-03-05 23:01:47 +00003124 switch (event) {
3125 case CHR_EVENT_MUX_IN:
3126 readline_restart(mon->rs);
3127 monitor_resume(mon);
3128 monitor_flush(mon);
3129 break;
ths86e94de2007-01-05 22:01:59 +00003130
aliguori2724b182009-03-05 23:01:47 +00003131 case CHR_EVENT_MUX_OUT:
3132 if (mon->suspend_cnt == 0)
3133 monitor_printf(mon, "\n");
3134 monitor_flush(mon);
3135 monitor_suspend(mon);
3136 break;
3137
3138 case CHR_EVENT_RESET:
3139 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3140 "information\n", QEMU_VERSION);
3141 if (mon->chr->focus == 0)
3142 readline_show_prompt(mon->rs);
3143 break;
3144 }
ths86e94de2007-01-05 22:01:59 +00003145}
3146
aliguori76655d62009-03-06 20:27:37 +00003147
3148/*
3149 * Local variables:
3150 * c-indent-level: 4
3151 * c-basic-offset: 4
3152 * tab-width: 8
3153 * End:
3154 */
3155
aliguori731b0362009-03-05 23:01:42 +00003156void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003157{
aliguori731b0362009-03-05 23:01:42 +00003158 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003159 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003160
3161 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003162 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003163 is_first_init = 0;
3164 }
aliguori87127162009-03-05 23:01:29 +00003165
3166 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003167
aliguori87127162009-03-05 23:01:29 +00003168 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003169 mon->flags = flags;
aliguori2724b182009-03-05 23:01:47 +00003170 if (mon->chr->focus != 0)
3171 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
aliguoricde76ee2009-03-05 23:01:51 +00003172 if (flags & MONITOR_USE_READLINE) {
3173 mon->rs = readline_init(mon, monitor_find_completion);
3174 monitor_read_command(mon, 0);
3175 }
aliguori87127162009-03-05 23:01:29 +00003176
aliguori731b0362009-03-05 23:01:42 +00003177 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3178 mon);
aliguori87127162009-03-05 23:01:29 +00003179
3180 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003181 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003182 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003183}
3184
aliguori376253e2009-03-05 23:01:23 +00003185static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003186{
aliguoribb5fc202009-03-05 23:01:15 +00003187 BlockDriverState *bs = opaque;
3188 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003189
aliguoribb5fc202009-03-05 23:01:15 +00003190 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003191 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003192 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003193 }
aliguori731b0362009-03-05 23:01:42 +00003194 if (mon->password_completion_cb)
3195 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003196
aliguori731b0362009-03-05 23:01:42 +00003197 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003198}
aliguoric0f4ce72009-03-05 23:01:01 +00003199
aliguori376253e2009-03-05 23:01:23 +00003200void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003201 BlockDriverCompletionFunc *completion_cb,
3202 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003203{
aliguoricde76ee2009-03-05 23:01:51 +00003204 int err;
3205
aliguoribb5fc202009-03-05 23:01:15 +00003206 if (!bdrv_key_required(bs)) {
3207 if (completion_cb)
3208 completion_cb(opaque, 0);
3209 return;
3210 }
aliguoric0f4ce72009-03-05 23:01:01 +00003211
aliguori376253e2009-03-05 23:01:23 +00003212 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3213 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003214
aliguori731b0362009-03-05 23:01:42 +00003215 mon->password_completion_cb = completion_cb;
3216 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003217
aliguoricde76ee2009-03-05 23:01:51 +00003218 err = monitor_read_password(mon, bdrv_password_cb, bs);
3219
3220 if (err && completion_cb)
3221 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003222}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003223
3224typedef struct QemuErrorSink QemuErrorSink;
3225struct QemuErrorSink {
3226 enum {
3227 ERR_SINK_FILE,
3228 ERR_SINK_MONITOR,
3229 } dest;
3230 union {
3231 FILE *fp;
3232 Monitor *mon;
3233 };
3234 QemuErrorSink *previous;
3235};
3236
Blue Swirl528e93a2009-08-31 15:14:40 +00003237static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003238
3239void qemu_errors_to_file(FILE *fp)
3240{
3241 QemuErrorSink *sink;
3242
3243 sink = qemu_mallocz(sizeof(*sink));
3244 sink->dest = ERR_SINK_FILE;
3245 sink->fp = fp;
3246 sink->previous = qemu_error_sink;
3247 qemu_error_sink = sink;
3248}
3249
3250void qemu_errors_to_mon(Monitor *mon)
3251{
3252 QemuErrorSink *sink;
3253
3254 sink = qemu_mallocz(sizeof(*sink));
3255 sink->dest = ERR_SINK_MONITOR;
3256 sink->mon = mon;
3257 sink->previous = qemu_error_sink;
3258 qemu_error_sink = sink;
3259}
3260
3261void qemu_errors_to_previous(void)
3262{
3263 QemuErrorSink *sink;
3264
3265 assert(qemu_error_sink != NULL);
3266 sink = qemu_error_sink;
3267 qemu_error_sink = sink->previous;
3268 qemu_free(sink);
3269}
3270
3271void qemu_error(const char *fmt, ...)
3272{
3273 va_list args;
3274
3275 assert(qemu_error_sink != NULL);
3276 switch (qemu_error_sink->dest) {
3277 case ERR_SINK_FILE:
3278 va_start(args, fmt);
3279 vfprintf(qemu_error_sink->fp, fmt, args);
3280 va_end(args);
3281 break;
3282 case ERR_SINK_MONITOR:
3283 va_start(args, fmt);
3284 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3285 va_end(args);
3286 break;
3287 }
3288}