blob: 727848d027ec03dbce72f023a08e19fafff23023 [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"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030047#include "qint.h"
48#include "qdict.h"
49#include "qstring.h"
ths6a5bd302007-12-03 17:05:38 +000050
bellard9dc39cb2004-03-14 21:38:27 +000051//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000052//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000053
bellard9307c4c2004-04-04 12:57:25 +000054/*
55 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000056 *
bellard9307c4c2004-04-04 12:57:25 +000057 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000058 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000059 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000060 * 'i' 32 bit integer
61 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000062 * '/' optional gdb-like print format (like "/10x")
63 *
64 * '?' optional type (for 'F', 's' and 'i')
65 *
66 */
67
aliguori376253e2009-03-05 23:01:23 +000068typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000069 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000070 const char *args_type;
blueswir1a5f1b962008-08-17 20:21:51 +000071 void *handler;
bellard9dc39cb2004-03-14 21:38:27 +000072 const char *params;
73 const char *help;
aliguori376253e2009-03-05 23:01:23 +000074} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000075
Mark McLoughlinf07918f2009-07-22 09:11:40 +010076/* file descriptors passed via SCM_RIGHTS */
77typedef struct mon_fd_t mon_fd_t;
78struct mon_fd_t {
79 char *name;
80 int fd;
81 LIST_ENTRY(mon_fd_t) next;
82};
83
aliguori87127162009-03-05 23:01:29 +000084struct Monitor {
85 CharDriverState *chr;
aliguori731b0362009-03-05 23:01:42 +000086 int flags;
87 int suspend_cnt;
88 uint8_t outbuf[1024];
89 int outbuf_index;
90 ReadLineState *rs;
91 CPUState *mon_cpu;
92 BlockDriverCompletionFunc *password_completion_cb;
93 void *password_opaque;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010094 LIST_HEAD(,mon_fd_t) fds;
aliguori87127162009-03-05 23:01:29 +000095 LIST_ENTRY(Monitor) entry;
96};
97
98static LIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +000099
aliguori376253e2009-03-05 23:01:23 +0000100static const mon_cmd_t mon_cmds[];
101static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000102
aliguori87127162009-03-05 23:01:29 +0000103Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000104
aliguori731b0362009-03-05 23:01:42 +0000105static void monitor_command_cb(Monitor *mon, const char *cmdline,
106 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000107
aliguori731b0362009-03-05 23:01:42 +0000108static void monitor_read_command(Monitor *mon, int show_prompt)
109{
110 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
111 if (show_prompt)
112 readline_show_prompt(mon->rs);
113}
bellard6a00d602005-11-21 23:25:50 +0000114
aliguoricde76ee2009-03-05 23:01:51 +0000115static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
116 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000117{
aliguoricde76ee2009-03-05 23:01:51 +0000118 if (mon->rs) {
119 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
120 /* prompt is printed on return from the command handler */
121 return 0;
122 } else {
123 monitor_printf(mon, "terminal does not support password prompting\n");
124 return -ENOTTY;
125 }
aliguoribb5fc202009-03-05 23:01:15 +0000126}
127
aliguori376253e2009-03-05 23:01:23 +0000128void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000129{
aliguori731b0362009-03-05 23:01:42 +0000130 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
131 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
132 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000133 }
134}
135
136/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000137static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000138{
ths60fe76f2007-12-16 03:02:09 +0000139 char c;
aliguori731b0362009-03-05 23:01:42 +0000140
141 if (!mon)
142 return;
143
bellard7e2515e2004-08-01 21:52:19 +0000144 for(;;) {
145 c = *str++;
146 if (c == '\0')
147 break;
bellard7ba12602006-07-14 20:26:42 +0000148 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000149 mon->outbuf[mon->outbuf_index++] = '\r';
150 mon->outbuf[mon->outbuf_index++] = c;
151 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
152 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000153 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000154 }
155}
156
aliguori376253e2009-03-05 23:01:23 +0000157void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000158{
159 char buf[4096];
160 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000161 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000162}
163
aliguori376253e2009-03-05 23:01:23 +0000164void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000165{
166 va_list ap;
167 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000168 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000169 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000170}
171
aliguori376253e2009-03-05 23:01:23 +0000172void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000173{
174 int i;
175
176 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000177 switch (filename[i]) {
178 case ' ':
179 case '"':
180 case '\\':
181 monitor_printf(mon, "\\%c", filename[i]);
182 break;
183 case '\t':
184 monitor_printf(mon, "\\t");
185 break;
186 case '\r':
187 monitor_printf(mon, "\\r");
188 break;
189 case '\n':
190 monitor_printf(mon, "\\n");
191 break;
192 default:
193 monitor_printf(mon, "%c", filename[i]);
194 break;
195 }
thsfef30742006-12-22 14:11:32 +0000196 }
197}
198
bellard7fe48482004-10-09 18:08:01 +0000199static int monitor_fprintf(FILE *stream, const char *fmt, ...)
200{
201 va_list ap;
202 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000203 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000204 va_end(ap);
205 return 0;
206}
207
bellard9dc39cb2004-03-14 21:38:27 +0000208static int compare_cmd(const char *name, const char *list)
209{
210 const char *p, *pstart;
211 int len;
212 len = strlen(name);
213 p = list;
214 for(;;) {
215 pstart = p;
216 p = strchr(p, '|');
217 if (!p)
218 p = pstart + strlen(pstart);
219 if ((p - pstart) == len && !memcmp(pstart, name, len))
220 return 1;
221 if (*p == '\0')
222 break;
223 p++;
224 }
225 return 0;
226}
227
aliguori376253e2009-03-05 23:01:23 +0000228static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
229 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000230{
aliguori376253e2009-03-05 23:01:23 +0000231 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000232
233 for(cmd = cmds; cmd->name != NULL; cmd++) {
234 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000235 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
236 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000237 }
238}
239
aliguori376253e2009-03-05 23:01:23 +0000240static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000241{
242 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000243 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000244 } else {
aliguori376253e2009-03-05 23:01:23 +0000245 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000246 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000247 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000248 monitor_printf(mon, "Log items (comma separated):\n");
249 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000250 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000251 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000252 }
253 }
bellard9dc39cb2004-03-14 21:38:27 +0000254 }
255}
256
Luiz Capitulino38183182009-08-28 15:27:08 -0300257static void do_help_cmd(Monitor *mon, const char *name)
258{
259 help_cmd(mon, name);
260}
261
aliguori376253e2009-03-05 23:01:23 +0000262static void do_commit(Monitor *mon, const char *device)
bellard9dc39cb2004-03-14 21:38:27 +0000263{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200264 int all_devices;
265 DriveInfo *dinfo;
balrog2dc7b602007-05-24 18:53:22 +0000266
bellard7954c732006-08-01 15:52:40 +0000267 all_devices = !strcmp(device, "all");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200268 TAILQ_FOREACH(dinfo, &drives, next) {
269 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300270 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200271 continue;
272 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000273 }
274}
275
aliguori376253e2009-03-05 23:01:23 +0000276static void do_info(Monitor *mon, const char *item)
bellard9dc39cb2004-03-14 21:38:27 +0000277{
aliguori376253e2009-03-05 23:01:23 +0000278 const mon_cmd_t *cmd;
279 void (*handler)(Monitor *);
bellard9dc39cb2004-03-14 21:38:27 +0000280
bellard9307c4c2004-04-04 12:57:25 +0000281 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000282 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000283 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000284 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000285 goto found;
286 }
287 help:
aliguori376253e2009-03-05 23:01:23 +0000288 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000289 return;
290 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000291 handler = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +0000292 handler(mon);
bellard9dc39cb2004-03-14 21:38:27 +0000293}
294
aliguori376253e2009-03-05 23:01:23 +0000295static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000296{
pbrook4a19f1e2009-04-07 23:17:49 +0000297 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000298}
299
aliguori376253e2009-03-05 23:01:23 +0000300static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000301{
302 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000303 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000304}
305
aurel32bf4f74c2008-12-18 22:42:34 +0000306#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000307static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000308{
aliguori376253e2009-03-05 23:01:23 +0000309 monitor_printf(mon, "HPET is %s by QEMU\n",
310 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000311}
aurel32bf4f74c2008-12-18 22:42:34 +0000312#endif
aliguori16b29ae2008-12-17 23:28:44 +0000313
aliguori376253e2009-03-05 23:01:23 +0000314static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000315{
aliguori376253e2009-03-05 23:01:23 +0000316 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
317 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
318 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
319 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
320 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000321}
322
bellard6a00d602005-11-21 23:25:50 +0000323/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000324static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000325{
326 CPUState *env;
327
328 for(env = first_cpu; env != NULL; env = env->next_cpu) {
329 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000330 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000331 return 0;
332 }
333 }
334 return -1;
335}
336
pbrook9596ebb2007-11-18 01:44:38 +0000337static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000338{
aliguori731b0362009-03-05 23:01:42 +0000339 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000340 mon_set_cpu(0);
341 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300342 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000343 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000344}
345
aliguori376253e2009-03-05 23:01:23 +0000346static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000347{
bellard6a00d602005-11-21 23:25:50 +0000348 CPUState *env;
349 env = mon_get_cpu();
350 if (!env)
351 return;
bellard9307c4c2004-04-04 12:57:25 +0000352#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000353 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000354 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000355#else
aliguori376253e2009-03-05 23:01:23 +0000356 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000357 0);
bellard9307c4c2004-04-04 12:57:25 +0000358#endif
359}
360
aliguori376253e2009-03-05 23:01:23 +0000361static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000362{
363 CPUState *env;
364
365 /* just to set the default cpu if not already done */
366 mon_get_cpu();
367
368 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Avi Kivity4c0960c2009-08-17 23:19:53 +0300369 cpu_synchronize_state(env);
aliguori376253e2009-03-05 23:01:23 +0000370 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000371 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000372 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000373#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000374 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
375 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000376#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000377 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000378#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000379 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
380 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000381#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000382 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000383#endif
thsead93602007-09-06 00:18:15 +0000384 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000385 monitor_printf(mon, " (halted)");
386 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000387 }
388}
389
aliguori376253e2009-03-05 23:01:23 +0000390static void do_cpu_set(Monitor *mon, int index)
bellard6a00d602005-11-21 23:25:50 +0000391{
392 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000393 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000394}
395
aliguori376253e2009-03-05 23:01:23 +0000396static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000397{
aliguori376253e2009-03-05 23:01:23 +0000398 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000399}
400
aliguori376253e2009-03-05 23:01:23 +0000401static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000402{
403 int i;
bellard7e2515e2004-08-01 21:52:19 +0000404 const char *str;
ths3b46e622007-09-17 08:09:54 +0000405
aliguoricde76ee2009-03-05 23:01:51 +0000406 if (!mon->rs)
407 return;
bellard7e2515e2004-08-01 21:52:19 +0000408 i = 0;
409 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000410 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000411 if (!str)
412 break;
aliguori376253e2009-03-05 23:01:23 +0000413 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000414 i++;
bellardaa455482004-04-04 13:07:25 +0000415 }
416}
417
j_mayer76a66252007-03-07 08:32:30 +0000418#if defined(TARGET_PPC)
419/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000420static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000421{
422 CPUState *env;
423
424 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000425 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000426}
427#endif
428
aliguori376253e2009-03-05 23:01:23 +0000429static void do_quit(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000430{
431 exit(0);
432}
433
aliguori376253e2009-03-05 23:01:23 +0000434static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000435{
436 if (bdrv_is_inserted(bs)) {
437 if (!force) {
438 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000439 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000440 return -1;
441 }
442 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000443 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000444 return -1;
445 }
446 }
447 bdrv_close(bs);
448 }
449 return 0;
450}
451
aliguori376253e2009-03-05 23:01:23 +0000452static void do_eject(Monitor *mon, int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000453{
454 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000455
bellard9307c4c2004-04-04 12:57:25 +0000456 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000457 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000458 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000459 return;
460 }
aliguori376253e2009-03-05 23:01:23 +0000461 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000462}
463
aliguori376253e2009-03-05 23:01:23 +0000464static void do_change_block(Monitor *mon, const char *device,
465 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000466{
467 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000468 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000469
bellard9307c4c2004-04-04 12:57:25 +0000470 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000471 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000472 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000473 return;
474 }
aurel322ecea9b2008-06-18 22:10:01 +0000475 if (fmt) {
476 drv = bdrv_find_format(fmt);
477 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000478 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000479 return;
480 }
481 }
aliguori376253e2009-03-05 23:01:23 +0000482 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000483 return;
aurel322ecea9b2008-06-18 22:10:01 +0000484 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000485 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000486}
487
aliguori376253e2009-03-05 23:01:23 +0000488static void change_vnc_password_cb(Monitor *mon, const char *password,
489 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000490{
491 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000492 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000493
aliguori731b0362009-03-05 23:01:42 +0000494 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000495}
496
aliguori376253e2009-03-05 23:01:23 +0000497static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000498{
ths70848512007-08-25 01:37:05 +0000499 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000500 strcmp(target, "password") == 0) {
501 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000502 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000503 strncpy(password, arg, sizeof(password));
504 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000505 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000506 } else {
aliguori376253e2009-03-05 23:01:23 +0000507 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000508 }
ths70848512007-08-25 01:37:05 +0000509 } else {
aliguori28a76be2009-03-06 20:27:40 +0000510 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000511 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000512 }
thse25a5822007-08-25 01:36:20 +0000513}
514
aliguori376253e2009-03-05 23:01:23 +0000515static void do_change(Monitor *mon, const char *device, const char *target,
516 const char *arg)
thse25a5822007-08-25 01:36:20 +0000517{
518 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000519 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000520 } else {
aliguori28a76be2009-03-06 20:27:40 +0000521 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000522 }
523}
524
aliguori376253e2009-03-05 23:01:23 +0000525static void do_screen_dump(Monitor *mon, const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000526{
pbrook95219892006-04-09 01:06:34 +0000527 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000528}
529
aliguori376253e2009-03-05 23:01:23 +0000530static void do_logfile(Monitor *mon, const char *filename)
pbrooke735b912007-06-30 13:53:24 +0000531{
532 cpu_set_log_filename(filename);
533}
534
aliguori376253e2009-03-05 23:01:23 +0000535static void do_log(Monitor *mon, const char *items)
bellardf193c792004-03-21 17:06:25 +0000536{
537 int mask;
ths3b46e622007-09-17 08:09:54 +0000538
bellard9307c4c2004-04-04 12:57:25 +0000539 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000540 mask = 0;
541 } else {
bellard9307c4c2004-04-04 12:57:25 +0000542 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000543 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000544 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000545 return;
546 }
547 }
548 cpu_set_log(mask);
549}
550
aurel321b530a62009-04-05 20:08:59 +0000551static void do_singlestep(Monitor *mon, const char *option)
552{
553 if (!option || !strcmp(option, "on")) {
554 singlestep = 1;
555 } else if (!strcmp(option, "off")) {
556 singlestep = 0;
557 } else {
558 monitor_printf(mon, "unexpected option %s\n", option);
559 }
560}
561
aliguori376253e2009-03-05 23:01:23 +0000562static void do_stop(Monitor *mon)
bellard8a7ddc32004-03-31 19:00:16 +0000563{
564 vm_stop(EXCP_INTERRUPT);
565}
566
aliguoribb5fc202009-03-05 23:01:15 +0000567static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000568
aliguori376253e2009-03-05 23:01:23 +0000569struct bdrv_iterate_context {
570 Monitor *mon;
571 int err;
572};
aliguoric0f4ce72009-03-05 23:01:01 +0000573
aliguori376253e2009-03-05 23:01:23 +0000574static void do_cont(Monitor *mon)
575{
576 struct bdrv_iterate_context context = { mon, 0 };
577
578 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000579 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000580 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000581 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000582}
583
aliguoribb5fc202009-03-05 23:01:15 +0000584static void bdrv_key_cb(void *opaque, int err)
585{
aliguori376253e2009-03-05 23:01:23 +0000586 Monitor *mon = opaque;
587
aliguoribb5fc202009-03-05 23:01:15 +0000588 /* another key was set successfully, retry to continue */
589 if (!err)
aliguori376253e2009-03-05 23:01:23 +0000590 do_cont(mon);
aliguoribb5fc202009-03-05 23:01:15 +0000591}
592
593static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
594{
aliguori376253e2009-03-05 23:01:23 +0000595 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000596
aliguori376253e2009-03-05 23:01:23 +0000597 if (!context->err && bdrv_key_required(bs)) {
598 context->err = -EBUSY;
599 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
600 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000601 }
602}
603
aliguori59030a82009-04-05 18:43:41 +0000604static void do_gdbserver(Monitor *mon, const char *device)
bellard8a7ddc32004-03-31 19:00:16 +0000605{
aliguori59030a82009-04-05 18:43:41 +0000606 if (!device)
607 device = "tcp::" DEFAULT_GDBSTUB_PORT;
608 if (gdbserver_start(device) < 0) {
609 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
610 device);
611 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000612 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000613 } else {
aliguori59030a82009-04-05 18:43:41 +0000614 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
615 device);
bellard8a7ddc32004-03-31 19:00:16 +0000616 }
617}
618
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100619static void do_watchdog_action(Monitor *mon, const char *action)
620{
621 if (select_watchdog_action(action) == -1) {
622 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
623 }
624}
625
aliguori376253e2009-03-05 23:01:23 +0000626static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000627{
aliguori376253e2009-03-05 23:01:23 +0000628 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000629 switch(c) {
630 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000631 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000632 break;
633 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000634 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000635 break;
636 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000637 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000638 break;
639 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000640 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000641 break;
642 default:
643 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000644 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000645 } else {
aliguori376253e2009-03-05 23:01:23 +0000646 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000647 }
648 break;
649 }
aliguori376253e2009-03-05 23:01:23 +0000650 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000651}
652
aliguori376253e2009-03-05 23:01:23 +0000653static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000654 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000655{
bellard6a00d602005-11-21 23:25:50 +0000656 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000657 int nb_per_line, l, line_size, i, max_digits, len;
658 uint8_t buf[16];
659 uint64_t v;
660
661 if (format == 'i') {
662 int flags;
663 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000664 env = mon_get_cpu();
665 if (!env && !is_physical)
666 return;
bellard9307c4c2004-04-04 12:57:25 +0000667#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000668 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000669 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000670 } else if (wsize == 4) {
671 flags = 0;
672 } else {
bellard6a15fd12006-04-12 21:07:07 +0000673 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000674 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000675 if (env) {
676#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000677 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000678 (env->segs[R_CS].flags & DESC_L_MASK))
679 flags = 2;
680 else
681#endif
682 if (!(env->segs[R_CS].flags & DESC_B_MASK))
683 flags = 1;
684 }
bellard4c27ba22004-04-25 18:05:08 +0000685 }
686#endif
aliguori376253e2009-03-05 23:01:23 +0000687 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000688 return;
689 }
690
691 len = wsize * count;
692 if (wsize == 1)
693 line_size = 8;
694 else
695 line_size = 16;
696 nb_per_line = line_size / wsize;
697 max_digits = 0;
698
699 switch(format) {
700 case 'o':
701 max_digits = (wsize * 8 + 2) / 3;
702 break;
703 default:
704 case 'x':
705 max_digits = (wsize * 8) / 4;
706 break;
707 case 'u':
708 case 'd':
709 max_digits = (wsize * 8 * 10 + 32) / 33;
710 break;
711 case 'c':
712 wsize = 1;
713 break;
714 }
715
716 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000717 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000718 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000719 else
aliguori376253e2009-03-05 23:01:23 +0000720 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000721 l = len;
722 if (l > line_size)
723 l = line_size;
724 if (is_physical) {
725 cpu_physical_memory_rw(addr, buf, l, 0);
726 } else {
bellard6a00d602005-11-21 23:25:50 +0000727 env = mon_get_cpu();
728 if (!env)
729 break;
aliguoric8f79b62008-08-18 14:00:20 +0000730 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000731 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000732 break;
733 }
bellard9307c4c2004-04-04 12:57:25 +0000734 }
ths5fafdf22007-09-16 21:08:06 +0000735 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000736 while (i < l) {
737 switch(wsize) {
738 default:
739 case 1:
740 v = ldub_raw(buf + i);
741 break;
742 case 2:
743 v = lduw_raw(buf + i);
744 break;
745 case 4:
bellard92a31b12005-02-10 22:00:52 +0000746 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000747 break;
748 case 8:
749 v = ldq_raw(buf + i);
750 break;
751 }
aliguori376253e2009-03-05 23:01:23 +0000752 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000753 switch(format) {
754 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000755 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000756 break;
757 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000758 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000759 break;
760 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000761 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000762 break;
763 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000764 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000765 break;
766 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000767 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000768 break;
769 }
770 i += wsize;
771 }
aliguori376253e2009-03-05 23:01:23 +0000772 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000773 addr += l;
774 len -= l;
775 }
776}
777
bellard92a31b12005-02-10 22:00:52 +0000778#if TARGET_LONG_BITS == 64
779#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
780#else
781#define GET_TLONG(h, l) (l)
782#endif
783
aliguori376253e2009-03-05 23:01:23 +0000784static void do_memory_dump(Monitor *mon, int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000785 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000786{
bellard92a31b12005-02-10 22:00:52 +0000787 target_long addr = GET_TLONG(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000788 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000789}
790
blueswir17743e582007-09-24 18:39:04 +0000791#if TARGET_PHYS_ADDR_BITS > 32
792#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
793#else
794#define GET_TPHYSADDR(h, l) (l)
795#endif
796
aliguori376253e2009-03-05 23:01:23 +0000797static void do_physical_memory_dump(Monitor *mon, int count, int format,
798 int size, uint32_t addrh, uint32_t addrl)
bellard92a31b12005-02-10 22:00:52 +0000799
bellard9307c4c2004-04-04 12:57:25 +0000800{
blueswir17743e582007-09-24 18:39:04 +0000801 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000802 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000803}
804
aliguori376253e2009-03-05 23:01:23 +0000805static void do_print(Monitor *mon, int count, int format, int size,
806 unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000807{
blueswir17743e582007-09-24 18:39:04 +0000808 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
809#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000810 switch(format) {
811 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000812 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000813 break;
814 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000815 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000816 break;
817 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000818 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000819 break;
820 default:
821 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000822 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000823 break;
824 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000825 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000826 break;
827 }
bellard92a31b12005-02-10 22:00:52 +0000828#else
829 switch(format) {
830 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000831 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000832 break;
833 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000834 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000835 break;
836 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000837 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000838 break;
839 default:
840 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000841 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000842 break;
843 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000844 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000845 break;
846 }
847#endif
aliguori376253e2009-03-05 23:01:23 +0000848 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000849}
850
aliguori376253e2009-03-05 23:01:23 +0000851static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000852 uint32_t size, const char *filename)
853{
854 FILE *f;
855 target_long addr = GET_TLONG(valh, vall);
856 uint32_t l;
857 CPUState *env;
858 uint8_t buf[1024];
859
860 env = mon_get_cpu();
861 if (!env)
862 return;
863
864 f = fopen(filename, "wb");
865 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000866 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000867 return;
868 }
869 while (size != 0) {
870 l = sizeof(buf);
871 if (l > size)
872 l = size;
873 cpu_memory_rw_debug(env, addr, buf, l, 0);
874 fwrite(buf, 1, l, f);
875 addr += l;
876 size -= l;
877 }
878 fclose(f);
879}
880
aliguori376253e2009-03-05 23:01:23 +0000881static void do_physical_memory_save(Monitor *mon, unsigned int valh,
882 unsigned int vall, uint32_t size,
883 const char *filename)
aurel32a8bdf7a2008-04-11 21:36:14 +0000884{
885 FILE *f;
886 uint32_t l;
887 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000888 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000889
890 f = fopen(filename, "wb");
891 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000892 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000893 return;
894 }
895 while (size != 0) {
896 l = sizeof(buf);
897 if (l > size)
898 l = size;
899 cpu_physical_memory_rw(addr, buf, l, 0);
900 fwrite(buf, 1, l, f);
901 fflush(f);
902 addr += l;
903 size -= l;
904 }
905 fclose(f);
906}
907
aliguori376253e2009-03-05 23:01:23 +0000908static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
bellarde4cf1ad2005-06-04 20:15:57 +0000909{
910 uint32_t addr;
911 uint8_t buf[1];
912 uint16_t sum;
913
914 sum = 0;
915 for(addr = start; addr < (start + size); addr++) {
916 cpu_physical_memory_rw(addr, buf, 1, 0);
917 /* BSD sum algorithm ('sum' Unix command) */
918 sum = (sum >> 1) | (sum << 15);
919 sum += buf[0];
920 }
aliguori376253e2009-03-05 23:01:23 +0000921 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000922}
923
bellarda3a91a32004-06-04 11:06:21 +0000924typedef struct {
925 int keycode;
926 const char *name;
927} KeyDef;
928
929static const KeyDef key_defs[] = {
930 { 0x2a, "shift" },
931 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000932
bellarda3a91a32004-06-04 11:06:21 +0000933 { 0x38, "alt" },
934 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000935 { 0x64, "altgr" },
936 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000937 { 0x1d, "ctrl" },
938 { 0x9d, "ctrl_r" },
939
940 { 0xdd, "menu" },
941
942 { 0x01, "esc" },
943
944 { 0x02, "1" },
945 { 0x03, "2" },
946 { 0x04, "3" },
947 { 0x05, "4" },
948 { 0x06, "5" },
949 { 0x07, "6" },
950 { 0x08, "7" },
951 { 0x09, "8" },
952 { 0x0a, "9" },
953 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000954 { 0x0c, "minus" },
955 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000956 { 0x0e, "backspace" },
957
958 { 0x0f, "tab" },
959 { 0x10, "q" },
960 { 0x11, "w" },
961 { 0x12, "e" },
962 { 0x13, "r" },
963 { 0x14, "t" },
964 { 0x15, "y" },
965 { 0x16, "u" },
966 { 0x17, "i" },
967 { 0x18, "o" },
968 { 0x19, "p" },
969
970 { 0x1c, "ret" },
971
972 { 0x1e, "a" },
973 { 0x1f, "s" },
974 { 0x20, "d" },
975 { 0x21, "f" },
976 { 0x22, "g" },
977 { 0x23, "h" },
978 { 0x24, "j" },
979 { 0x25, "k" },
980 { 0x26, "l" },
981
982 { 0x2c, "z" },
983 { 0x2d, "x" },
984 { 0x2e, "c" },
985 { 0x2f, "v" },
986 { 0x30, "b" },
987 { 0x31, "n" },
988 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000989 { 0x33, "comma" },
990 { 0x34, "dot" },
991 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000992
balrog4d3b6f62008-02-10 16:33:14 +0000993 { 0x37, "asterisk" },
994
bellarda3a91a32004-06-04 11:06:21 +0000995 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000996 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000997 { 0x3b, "f1" },
998 { 0x3c, "f2" },
999 { 0x3d, "f3" },
1000 { 0x3e, "f4" },
1001 { 0x3f, "f5" },
1002 { 0x40, "f6" },
1003 { 0x41, "f7" },
1004 { 0x42, "f8" },
1005 { 0x43, "f9" },
1006 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001007 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001008 { 0x46, "scroll_lock" },
1009
bellard64866c32006-05-07 18:03:31 +00001010 { 0xb5, "kp_divide" },
1011 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001012 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001013 { 0x4e, "kp_add" },
1014 { 0x9c, "kp_enter" },
1015 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001016 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001017
1018 { 0x52, "kp_0" },
1019 { 0x4f, "kp_1" },
1020 { 0x50, "kp_2" },
1021 { 0x51, "kp_3" },
1022 { 0x4b, "kp_4" },
1023 { 0x4c, "kp_5" },
1024 { 0x4d, "kp_6" },
1025 { 0x47, "kp_7" },
1026 { 0x48, "kp_8" },
1027 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001028
bellarda3a91a32004-06-04 11:06:21 +00001029 { 0x56, "<" },
1030
1031 { 0x57, "f11" },
1032 { 0x58, "f12" },
1033
1034 { 0xb7, "print" },
1035
1036 { 0xc7, "home" },
1037 { 0xc9, "pgup" },
1038 { 0xd1, "pgdn" },
1039 { 0xcf, "end" },
1040
1041 { 0xcb, "left" },
1042 { 0xc8, "up" },
1043 { 0xd0, "down" },
1044 { 0xcd, "right" },
1045
1046 { 0xd2, "insert" },
1047 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001048#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1049 { 0xf0, "stop" },
1050 { 0xf1, "again" },
1051 { 0xf2, "props" },
1052 { 0xf3, "undo" },
1053 { 0xf4, "front" },
1054 { 0xf5, "copy" },
1055 { 0xf6, "open" },
1056 { 0xf7, "paste" },
1057 { 0xf8, "find" },
1058 { 0xf9, "cut" },
1059 { 0xfa, "lf" },
1060 { 0xfb, "help" },
1061 { 0xfc, "meta_l" },
1062 { 0xfd, "meta_r" },
1063 { 0xfe, "compose" },
1064#endif
bellarda3a91a32004-06-04 11:06:21 +00001065 { 0, NULL },
1066};
1067
1068static int get_keycode(const char *key)
1069{
1070 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001071 char *endp;
1072 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001073
1074 for(p = key_defs; p->name != NULL; p++) {
1075 if (!strcmp(key, p->name))
1076 return p->keycode;
1077 }
bellard64866c32006-05-07 18:03:31 +00001078 if (strstart(key, "0x", NULL)) {
1079 ret = strtoul(key, &endp, 0);
1080 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1081 return ret;
1082 }
bellarda3a91a32004-06-04 11:06:21 +00001083 return -1;
1084}
1085
balrogc8256f92008-06-08 22:45:01 +00001086#define MAX_KEYCODES 16
1087static uint8_t keycodes[MAX_KEYCODES];
1088static int nb_pending_keycodes;
1089static QEMUTimer *key_timer;
1090
1091static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001092{
balrogc8256f92008-06-08 22:45:01 +00001093 int keycode;
1094
1095 while (nb_pending_keycodes > 0) {
1096 nb_pending_keycodes--;
1097 keycode = keycodes[nb_pending_keycodes];
1098 if (keycode & 0x80)
1099 kbd_put_keycode(0xe0);
1100 kbd_put_keycode(keycode | 0x80);
1101 }
1102}
1103
aliguori376253e2009-03-05 23:01:23 +00001104static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1105 int hold_time)
balrogc8256f92008-06-08 22:45:01 +00001106{
balrog3401c0d2008-06-04 10:05:59 +00001107 char keyname_buf[16];
1108 char *separator;
1109 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +00001110
balrogc8256f92008-06-08 22:45:01 +00001111 if (nb_pending_keycodes > 0) {
1112 qemu_del_timer(key_timer);
1113 release_keys(NULL);
1114 }
1115 if (!has_hold_time)
1116 hold_time = 100;
1117 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001118 while (1) {
1119 separator = strchr(string, '-');
1120 keyname_len = separator ? separator - string : strlen(string);
1121 if (keyname_len > 0) {
1122 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1123 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001124 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001125 return;
bellarda3a91a32004-06-04 11:06:21 +00001126 }
balrogc8256f92008-06-08 22:45:01 +00001127 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001128 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001129 return;
1130 }
1131 keyname_buf[keyname_len] = 0;
1132 keycode = get_keycode(keyname_buf);
1133 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001134 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001135 return;
1136 }
balrogc8256f92008-06-08 22:45:01 +00001137 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001138 }
balrog3401c0d2008-06-04 10:05:59 +00001139 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001140 break;
balrog3401c0d2008-06-04 10:05:59 +00001141 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001142 }
balrogc8256f92008-06-08 22:45:01 +00001143 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001144 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001145 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001146 keycode = keycodes[i];
1147 if (keycode & 0x80)
1148 kbd_put_keycode(0xe0);
1149 kbd_put_keycode(keycode & 0x7f);
1150 }
balrogc8256f92008-06-08 22:45:01 +00001151 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001152 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1153 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001154}
1155
bellard13224a82006-07-14 22:03:35 +00001156static int mouse_button_state;
1157
aliguori376253e2009-03-05 23:01:23 +00001158static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001159 const char *dz_str)
1160{
1161 int dx, dy, dz;
1162 dx = strtol(dx_str, NULL, 0);
1163 dy = strtol(dy_str, NULL, 0);
1164 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001165 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001166 dz = strtol(dz_str, NULL, 0);
1167 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1168}
1169
aliguori376253e2009-03-05 23:01:23 +00001170static void do_mouse_button(Monitor *mon, int button_state)
bellard13224a82006-07-14 22:03:35 +00001171{
1172 mouse_button_state = button_state;
1173 kbd_mouse_event(0, 0, 0, mouse_button_state);
1174}
1175
aliguori376253e2009-03-05 23:01:23 +00001176static void do_ioport_read(Monitor *mon, int count, int format, int size,
1177 int addr, int has_index, int index)
bellard34405572004-06-08 00:55:58 +00001178{
1179 uint32_t val;
1180 int suffix;
1181
1182 if (has_index) {
Isaku Yamahatad56dd6c2009-07-02 19:32:07 +09001183 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001184 addr++;
1185 }
1186 addr &= 0xffff;
1187
1188 switch(size) {
1189 default:
1190 case 1:
1191 val = cpu_inb(NULL, addr);
1192 suffix = 'b';
1193 break;
1194 case 2:
1195 val = cpu_inw(NULL, addr);
1196 suffix = 'w';
1197 break;
1198 case 4:
1199 val = cpu_inl(NULL, addr);
1200 suffix = 'l';
1201 break;
1202 }
aliguori376253e2009-03-05 23:01:23 +00001203 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1204 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001205}
bellarda3a91a32004-06-04 11:06:21 +00001206
Jan Kiszkaf1147842009-07-14 10:20:11 +02001207static void do_ioport_write(Monitor *mon, int count, int format, int size,
1208 int addr, int val)
1209{
1210 addr &= IOPORTS_MASK;
1211
1212 switch (size) {
1213 default:
1214 case 1:
1215 cpu_outb(NULL, addr, val);
1216 break;
1217 case 2:
1218 cpu_outw(NULL, addr, val);
1219 break;
1220 case 4:
1221 cpu_outl(NULL, addr, val);
1222 break;
1223 }
1224}
1225
aliguori376253e2009-03-05 23:01:23 +00001226static void do_boot_set(Monitor *mon, const char *bootdevice)
aurel320ecdffb2008-05-04 20:11:34 +00001227{
1228 int res;
1229
Jan Kiszka76e30d02009-07-02 00:19:02 +02001230 res = qemu_boot_set(bootdevice);
1231 if (res == 0) {
1232 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1233 } else if (res > 0) {
1234 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001235 } else {
aliguori376253e2009-03-05 23:01:23 +00001236 monitor_printf(mon, "no function defined to set boot device list for "
1237 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001238 }
1239}
1240
aliguori376253e2009-03-05 23:01:23 +00001241static void do_system_reset(Monitor *mon)
bellarde4f90822004-06-20 12:35:44 +00001242{
1243 qemu_system_reset_request();
1244}
1245
aliguori376253e2009-03-05 23:01:23 +00001246static void do_system_powerdown(Monitor *mon)
bellard34751872005-07-02 14:31:34 +00001247{
1248 qemu_system_powerdown_request();
1249}
1250
bellardb86bda52004-09-18 19:32:46 +00001251#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001252static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001253{
aliguori376253e2009-03-05 23:01:23 +00001254 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1255 addr,
1256 pte & mask,
1257 pte & PG_GLOBAL_MASK ? 'G' : '-',
1258 pte & PG_PSE_MASK ? 'P' : '-',
1259 pte & PG_DIRTY_MASK ? 'D' : '-',
1260 pte & PG_ACCESSED_MASK ? 'A' : '-',
1261 pte & PG_PCD_MASK ? 'C' : '-',
1262 pte & PG_PWT_MASK ? 'T' : '-',
1263 pte & PG_USER_MASK ? 'U' : '-',
1264 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001265}
1266
aliguori376253e2009-03-05 23:01:23 +00001267static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001268{
bellard6a00d602005-11-21 23:25:50 +00001269 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001270 int l1, l2;
1271 uint32_t pgd, pde, pte;
1272
bellard6a00d602005-11-21 23:25:50 +00001273 env = mon_get_cpu();
1274 if (!env)
1275 return;
1276
bellardb86bda52004-09-18 19:32:46 +00001277 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001278 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001279 return;
1280 }
1281 pgd = env->cr[3] & ~0xfff;
1282 for(l1 = 0; l1 < 1024; l1++) {
1283 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1284 pde = le32_to_cpu(pde);
1285 if (pde & PG_PRESENT_MASK) {
1286 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001287 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001288 } else {
1289 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001290 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001291 (uint8_t *)&pte, 4);
1292 pte = le32_to_cpu(pte);
1293 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001294 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001295 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001296 ~0xfff);
1297 }
1298 }
1299 }
1300 }
1301 }
1302}
1303
aliguori376253e2009-03-05 23:01:23 +00001304static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001305 uint32_t end, int prot)
1306{
bellard9746b152004-11-11 18:30:24 +00001307 int prot1;
1308 prot1 = *plast_prot;
1309 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001310 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001311 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1312 *pstart, end, end - *pstart,
1313 prot1 & PG_USER_MASK ? 'u' : '-',
1314 'r',
1315 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001316 }
1317 if (prot != 0)
1318 *pstart = end;
1319 else
1320 *pstart = -1;
1321 *plast_prot = prot;
1322 }
1323}
1324
aliguori376253e2009-03-05 23:01:23 +00001325static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001326{
bellard6a00d602005-11-21 23:25:50 +00001327 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001328 int l1, l2, prot, last_prot;
1329 uint32_t pgd, pde, pte, start, end;
1330
bellard6a00d602005-11-21 23:25:50 +00001331 env = mon_get_cpu();
1332 if (!env)
1333 return;
1334
bellardb86bda52004-09-18 19:32:46 +00001335 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001336 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001337 return;
1338 }
1339 pgd = env->cr[3] & ~0xfff;
1340 last_prot = 0;
1341 start = -1;
1342 for(l1 = 0; l1 < 1024; l1++) {
1343 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1344 pde = le32_to_cpu(pde);
1345 end = l1 << 22;
1346 if (pde & PG_PRESENT_MASK) {
1347 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1348 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001349 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001350 } else {
1351 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001352 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001353 (uint8_t *)&pte, 4);
1354 pte = le32_to_cpu(pte);
1355 end = (l1 << 22) + (l2 << 12);
1356 if (pte & PG_PRESENT_MASK) {
1357 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1358 } else {
1359 prot = 0;
1360 }
aliguori376253e2009-03-05 23:01:23 +00001361 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001362 }
1363 }
1364 } else {
1365 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001366 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001367 }
1368 }
1369}
1370#endif
1371
aurel327c664e22009-03-03 06:12:22 +00001372#if defined(TARGET_SH4)
1373
aliguori376253e2009-03-05 23:01:23 +00001374static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001375{
aliguori376253e2009-03-05 23:01:23 +00001376 monitor_printf(mon, " tlb%i:\t"
1377 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1378 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1379 "dirty=%hhu writethrough=%hhu\n",
1380 idx,
1381 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1382 tlb->v, tlb->sh, tlb->c, tlb->pr,
1383 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001384}
1385
aliguori376253e2009-03-05 23:01:23 +00001386static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001387{
1388 CPUState *env = mon_get_cpu();
1389 int i;
1390
aliguori376253e2009-03-05 23:01:23 +00001391 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001392 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001393 print_tlb (mon, i, &env->itlb[i]);
1394 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001395 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001396 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001397}
1398
1399#endif
1400
aliguori376253e2009-03-05 23:01:23 +00001401static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001402{
1403#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001404 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001405 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001406 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001407 else
aliguori376253e2009-03-05 23:01:23 +00001408 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001409#else
aliguori376253e2009-03-05 23:01:23 +00001410 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001411#endif
1412}
1413
aliguori030ea372009-04-21 22:30:47 +00001414static void do_info_numa(Monitor *mon)
1415{
aliguorib28b6232009-04-22 20:20:29 +00001416 int i;
aliguori030ea372009-04-21 22:30:47 +00001417 CPUState *env;
1418
1419 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1420 for (i = 0; i < nb_numa_nodes; i++) {
1421 monitor_printf(mon, "node %d cpus:", i);
1422 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1423 if (env->numa_node == i) {
1424 monitor_printf(mon, " %d", env->cpu_index);
1425 }
1426 }
1427 monitor_printf(mon, "\n");
1428 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1429 node_mem[i] >> 20);
1430 }
1431}
1432
bellard5f1ce942006-02-08 22:40:15 +00001433#ifdef CONFIG_PROFILER
1434
aliguori376253e2009-03-05 23:01:23 +00001435static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001436{
1437 int64_t total;
1438 total = qemu_time;
1439 if (total == 0)
1440 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001441 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1442 dev_time, dev_time / (double)ticks_per_sec);
1443 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1444 qemu_time, qemu_time / (double)ticks_per_sec);
bellard5f1ce942006-02-08 22:40:15 +00001445 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001446 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001447}
1448#else
aliguori376253e2009-03-05 23:01:23 +00001449static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001450{
aliguori376253e2009-03-05 23:01:23 +00001451 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001452}
1453#endif
1454
bellardec36b692006-07-16 18:57:03 +00001455/* Capture support */
1456static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1457
aliguori376253e2009-03-05 23:01:23 +00001458static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001459{
1460 int i;
1461 CaptureState *s;
1462
1463 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001464 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001465 s->ops.info (s->opaque);
1466 }
1467}
1468
Blue Swirl23130862009-06-06 08:22:04 +00001469#ifdef HAS_AUDIO
aliguori376253e2009-03-05 23:01:23 +00001470static void do_stop_capture(Monitor *mon, int n)
bellardec36b692006-07-16 18:57:03 +00001471{
1472 int i;
1473 CaptureState *s;
1474
1475 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1476 if (i == n) {
1477 s->ops.destroy (s->opaque);
1478 LIST_REMOVE (s, entries);
1479 qemu_free (s);
1480 return;
1481 }
1482 }
1483}
1484
aliguori376253e2009-03-05 23:01:23 +00001485static void do_wav_capture(Monitor *mon, const char *path,
1486 int has_freq, int freq,
1487 int has_bits, int bits,
1488 int has_channels, int nchannels)
bellardec36b692006-07-16 18:57:03 +00001489{
1490 CaptureState *s;
1491
1492 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001493
1494 freq = has_freq ? freq : 44100;
1495 bits = has_bits ? bits : 16;
1496 nchannels = has_channels ? nchannels : 2;
1497
1498 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001499 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001500 qemu_free (s);
1501 }
1502 LIST_INSERT_HEAD (&capture_head, s, entries);
1503}
1504#endif
1505
aurel32dc1c0b72008-04-27 23:52:12 +00001506#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001507static void do_inject_nmi(Monitor *mon, int cpu_index)
aurel32dc1c0b72008-04-27 23:52:12 +00001508{
1509 CPUState *env;
1510
1511 for (env = first_cpu; env != NULL; env = env->next_cpu)
1512 if (env->cpu_index == cpu_index) {
1513 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1514 break;
1515 }
1516}
1517#endif
1518
aliguori376253e2009-03-05 23:01:23 +00001519static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001520{
aurel321b530a62009-04-05 20:08:59 +00001521 if (vm_running) {
1522 if (singlestep) {
1523 monitor_printf(mon, "VM status: running (single step mode)\n");
1524 } else {
1525 monitor_printf(mon, "VM status: running\n");
1526 }
1527 } else
aliguori376253e2009-03-05 23:01:23 +00001528 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001529}
1530
1531
aliguori376253e2009-03-05 23:01:23 +00001532static void do_balloon(Monitor *mon, int value)
aliguoridf751fa2008-12-04 20:19:35 +00001533{
1534 ram_addr_t target = value;
1535 qemu_balloon(target << 20);
1536}
1537
aliguori376253e2009-03-05 23:01:23 +00001538static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001539{
1540 ram_addr_t actual;
1541
1542 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001543 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001544 monitor_printf(mon, "Using KVM without synchronous MMU, "
1545 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001546 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001547 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001548 else
aliguori376253e2009-03-05 23:01:23 +00001549 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001550}
1551
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001552static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001553{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001554 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001555
aliguori76655d62009-03-06 20:27:37 +00001556 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001557 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001558 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001559 return acl;
1560}
aliguori76655d62009-03-06 20:27:37 +00001561
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001562static void do_acl_show(Monitor *mon, const char *aclname)
1563{
1564 qemu_acl *acl = find_acl(mon, aclname);
1565 qemu_acl_entry *entry;
1566 int i = 0;
1567
1568 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001569 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001570 acl->defaultDeny ? "deny" : "allow");
aliguori28a76be2009-03-06 20:27:40 +00001571 TAILQ_FOREACH(entry, &acl->entries, next) {
1572 i++;
1573 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001574 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001575 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001576 }
1577}
1578
1579static void do_acl_reset(Monitor *mon, const char *aclname)
1580{
1581 qemu_acl *acl = find_acl(mon, aclname);
1582
1583 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001584 qemu_acl_reset(acl);
1585 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001586 }
1587}
aliguori76655d62009-03-06 20:27:37 +00001588
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001589static void do_acl_policy(Monitor *mon, const char *aclname,
1590 const char *policy)
1591{
1592 qemu_acl *acl = find_acl(mon, aclname);
1593
1594 if (acl) {
1595 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001596 acl->defaultDeny = 0;
1597 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001598 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001599 acl->defaultDeny = 1;
1600 monitor_printf(mon, "acl: policy set to 'deny'\n");
1601 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001602 monitor_printf(mon, "acl: unknown policy '%s', "
1603 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001604 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001605 }
1606}
aliguori76655d62009-03-06 20:27:37 +00001607
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001608static void do_acl_add(Monitor *mon, const char *aclname,
1609 const char *match, const char *policy,
1610 int has_index, int index)
1611{
1612 qemu_acl *acl = find_acl(mon, aclname);
1613 int deny, ret;
1614
1615 if (acl) {
1616 if (strcmp(policy, "allow") == 0) {
1617 deny = 0;
1618 } else if (strcmp(policy, "deny") == 0) {
1619 deny = 1;
1620 } else {
1621 monitor_printf(mon, "acl: unknown policy '%s', "
1622 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001623 return;
1624 }
aliguori28a76be2009-03-06 20:27:40 +00001625 if (has_index)
1626 ret = qemu_acl_insert(acl, deny, match, index);
1627 else
1628 ret = qemu_acl_append(acl, deny, match);
1629 if (ret < 0)
1630 monitor_printf(mon, "acl: unable to add acl entry\n");
1631 else
1632 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001633 }
1634}
aliguori76655d62009-03-06 20:27:37 +00001635
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001636static void do_acl_remove(Monitor *mon, const char *aclname, const char *match)
1637{
1638 qemu_acl *acl = find_acl(mon, aclname);
1639 int ret;
aliguori76655d62009-03-06 20:27:37 +00001640
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001641 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001642 ret = qemu_acl_remove(acl, match);
1643 if (ret < 0)
1644 monitor_printf(mon, "acl: no matching acl entry\n");
1645 else
1646 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001647 }
1648}
1649
Huang Ying79c4f6b2009-06-23 10:05:14 +08001650#if defined(TARGET_I386)
1651static void do_inject_mce(Monitor *mon,
1652 int cpu_index, int bank,
1653 unsigned status_hi, unsigned status_lo,
1654 unsigned mcg_status_hi, unsigned mcg_status_lo,
1655 unsigned addr_hi, unsigned addr_lo,
1656 unsigned misc_hi, unsigned misc_lo)
1657{
1658 CPUState *cenv;
1659 uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
1660 uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
1661 uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
1662 uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
1663
1664 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1665 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1666 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1667 break;
1668 }
1669}
1670#endif
1671
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001672static void do_getfd(Monitor *mon, const char *fdname)
1673{
1674 mon_fd_t *monfd;
1675 int fd;
1676
1677 fd = qemu_chr_get_msgfd(mon->chr);
1678 if (fd == -1) {
1679 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1680 return;
1681 }
1682
1683 if (qemu_isdigit(fdname[0])) {
1684 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1685 return;
1686 }
1687
1688 fd = dup(fd);
1689 if (fd == -1) {
1690 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1691 strerror(errno));
1692 return;
1693 }
1694
1695 LIST_FOREACH(monfd, &mon->fds, next) {
1696 if (strcmp(monfd->name, fdname) != 0) {
1697 continue;
1698 }
1699
1700 close(monfd->fd);
1701 monfd->fd = fd;
1702 return;
1703 }
1704
1705 monfd = qemu_mallocz(sizeof(mon_fd_t));
1706 monfd->name = qemu_strdup(fdname);
1707 monfd->fd = fd;
1708
1709 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1710}
1711
1712static void do_closefd(Monitor *mon, const char *fdname)
1713{
1714 mon_fd_t *monfd;
1715
1716 LIST_FOREACH(monfd, &mon->fds, next) {
1717 if (strcmp(monfd->name, fdname) != 0) {
1718 continue;
1719 }
1720
1721 LIST_REMOVE(monfd, next);
1722 close(monfd->fd);
1723 qemu_free(monfd->name);
1724 qemu_free(monfd);
1725 return;
1726 }
1727
1728 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1729 fdname);
1730}
1731
Juan Quintelac8d41b22009-08-20 19:42:21 +02001732static void do_loadvm(Monitor *mon, const char *name)
1733{
1734 int saved_vm_running = vm_running;
1735
1736 vm_stop(0);
1737
Juan Quintela05f24012009-08-20 19:42:22 +02001738 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001739 vm_start();
1740}
1741
Mark McLoughlin7768e042009-07-22 09:11:41 +01001742int monitor_get_fd(Monitor *mon, const char *fdname)
1743{
1744 mon_fd_t *monfd;
1745
1746 LIST_FOREACH(monfd, &mon->fds, next) {
1747 int fd;
1748
1749 if (strcmp(monfd->name, fdname) != 0) {
1750 continue;
1751 }
1752
1753 fd = monfd->fd;
1754
1755 /* caller takes ownership of fd */
1756 LIST_REMOVE(monfd, next);
1757 qemu_free(monfd->name);
1758 qemu_free(monfd);
1759
1760 return fd;
1761 }
1762
1763 return -1;
1764}
1765
aliguori376253e2009-03-05 23:01:23 +00001766static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001767#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001768 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001769};
1770
Blue Swirl23130862009-06-06 08:22:04 +00001771/* Please update qemu-monitor.hx when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001772static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001773 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001774 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001775 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001776 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001777 { "chardev", "", qemu_chr_info,
1778 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001779 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001780 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001781 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001782 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001783 { "registers", "", do_info_registers,
1784 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001785 { "cpus", "", do_info_cpus,
1786 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001787 { "history", "", do_info_history,
1788 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001789 { "irq", "", irq_info,
1790 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001791 { "pic", "", pic_info,
1792 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001793 { "pci", "", pci_info,
1794 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001795#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001796 { "tlb", "", tlb_info,
1797 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001798#endif
1799#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001800 { "mem", "", mem_info,
1801 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001802 { "hpet", "", do_info_hpet,
1803 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001804#endif
bellarde3db7222005-01-26 22:00:47 +00001805 { "jit", "", do_info_jit,
1806 "", "show dynamic compiler info", },
aliguori7ba1e612008-11-05 16:04:33 +00001807 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001808 "", "show KVM information", },
aliguori030ea372009-04-21 22:30:47 +00001809 { "numa", "", do_info_numa,
1810 "", "show NUMA information", },
bellarda594cfb2005-11-06 16:13:29 +00001811 { "usb", "", usb_info,
1812 "", "show guest USB devices", },
1813 { "usbhost", "", usb_host_info,
1814 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001815 { "profile", "", do_info_profile,
1816 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001817 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001818 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001819 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001820 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001821 { "status", "", do_info_status,
1822 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001823 { "pcmcia", "", pcmcia_info,
1824 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001825 { "mice", "", do_info_mice,
1826 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001827 { "vnc", "", do_info_vnc,
1828 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001829 { "name", "", do_info_name,
1830 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001831 { "uuid", "", do_info_uuid,
1832 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001833#if defined(TARGET_PPC)
1834 { "cpustats", "", do_info_cpu_stats,
1835 "", "show CPU statistics", },
1836#endif
blueswir131a60e22007-10-26 18:42:59 +00001837#if defined(CONFIG_SLIRP)
Jan Kiszka6dbe5532009-06-24 14:42:29 +02001838 { "usernet", "", do_info_usernet,
1839 "", "show user network stack connection states", },
blueswir131a60e22007-10-26 18:42:59 +00001840#endif
aliguori5bb79102008-10-13 03:12:02 +00001841 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001842 { "balloon", "", do_info_balloon,
1843 "", "show balloon information" },
Gerd Hoffmanncae49562009-06-05 15:53:17 +01001844 { "qtree", "", do_info_qtree,
1845 "", "show device tree" },
Gerd Hoffmannf6c64e02009-08-03 15:03:09 +02001846 { "qdm", "", do_info_qdm,
1847 "", "show qdev device model list" },
bellard9dc39cb2004-03-14 21:38:27 +00001848 { NULL, NULL, },
1849};
1850
bellard9307c4c2004-04-04 12:57:25 +00001851/*******************************************************************/
1852
1853static const char *pch;
1854static jmp_buf expr_env;
1855
bellard92a31b12005-02-10 22:00:52 +00001856#define MD_TLONG 0
1857#define MD_I32 1
1858
bellard9307c4c2004-04-04 12:57:25 +00001859typedef struct MonitorDef {
1860 const char *name;
1861 int offset;
blueswir18662d652008-10-02 18:32:44 +00001862 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001863 int type;
bellard9307c4c2004-04-04 12:57:25 +00001864} MonitorDef;
1865
bellard57206fd2004-04-25 18:54:52 +00001866#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001867static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001868{
bellard6a00d602005-11-21 23:25:50 +00001869 CPUState *env = mon_get_cpu();
1870 if (!env)
1871 return 0;
1872 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001873}
1874#endif
1875
bellarda541f292004-04-12 20:39:29 +00001876#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001877static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001878{
bellard6a00d602005-11-21 23:25:50 +00001879 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001880 unsigned int u;
1881 int i;
1882
bellard6a00d602005-11-21 23:25:50 +00001883 if (!env)
1884 return 0;
1885
bellarda541f292004-04-12 20:39:29 +00001886 u = 0;
1887 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00001888 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001889
1890 return u;
1891}
1892
blueswir18662d652008-10-02 18:32:44 +00001893static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001894{
bellard6a00d602005-11-21 23:25:50 +00001895 CPUState *env = mon_get_cpu();
1896 if (!env)
1897 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001898 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001899}
1900
blueswir18662d652008-10-02 18:32:44 +00001901static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001902{
bellard6a00d602005-11-21 23:25:50 +00001903 CPUState *env = mon_get_cpu();
1904 if (!env)
1905 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001906 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001907}
bellard9fddaa02004-05-21 12:59:32 +00001908
blueswir18662d652008-10-02 18:32:44 +00001909static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001910{
bellard6a00d602005-11-21 23:25:50 +00001911 CPUState *env = mon_get_cpu();
1912 if (!env)
1913 return 0;
1914 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001915}
1916
blueswir18662d652008-10-02 18:32:44 +00001917static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001918{
bellard6a00d602005-11-21 23:25:50 +00001919 CPUState *env = mon_get_cpu();
1920 if (!env)
1921 return 0;
1922 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001923}
1924
blueswir18662d652008-10-02 18:32:44 +00001925static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001926{
bellard6a00d602005-11-21 23:25:50 +00001927 CPUState *env = mon_get_cpu();
1928 if (!env)
1929 return 0;
1930 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001931}
bellarda541f292004-04-12 20:39:29 +00001932#endif
1933
bellarde95c8d52004-09-30 22:22:08 +00001934#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001935#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001936static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001937{
bellard6a00d602005-11-21 23:25:50 +00001938 CPUState *env = mon_get_cpu();
1939 if (!env)
1940 return 0;
1941 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001942}
bellard7b936c02005-10-30 17:05:13 +00001943#endif
bellarde95c8d52004-09-30 22:22:08 +00001944
blueswir18662d652008-10-02 18:32:44 +00001945static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001946{
bellard6a00d602005-11-21 23:25:50 +00001947 CPUState *env = mon_get_cpu();
1948 if (!env)
1949 return 0;
1950 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001951}
1952#endif
1953
blueswir18662d652008-10-02 18:32:44 +00001954static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001955#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001956
1957#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001958 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001959 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001960 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001961
bellard9307c4c2004-04-04 12:57:25 +00001962 { "eax", offsetof(CPUState, regs[0]) },
1963 { "ecx", offsetof(CPUState, regs[1]) },
1964 { "edx", offsetof(CPUState, regs[2]) },
1965 { "ebx", offsetof(CPUState, regs[3]) },
1966 { "esp|sp", offsetof(CPUState, regs[4]) },
1967 { "ebp|fp", offsetof(CPUState, regs[5]) },
1968 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001969 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001970#ifdef TARGET_X86_64
1971 { "r8", offsetof(CPUState, regs[8]) },
1972 { "r9", offsetof(CPUState, regs[9]) },
1973 { "r10", offsetof(CPUState, regs[10]) },
1974 { "r11", offsetof(CPUState, regs[11]) },
1975 { "r12", offsetof(CPUState, regs[12]) },
1976 { "r13", offsetof(CPUState, regs[13]) },
1977 { "r14", offsetof(CPUState, regs[14]) },
1978 { "r15", offsetof(CPUState, regs[15]) },
1979#endif
bellard9307c4c2004-04-04 12:57:25 +00001980 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001981 { "eip", offsetof(CPUState, eip) },
1982 SEG("cs", R_CS)
1983 SEG("ds", R_DS)
1984 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001985 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001986 SEG("fs", R_FS)
1987 SEG("gs", R_GS)
1988 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00001989#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00001990 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00001991 { "r0", offsetof(CPUState, gpr[0]) },
1992 { "r1", offsetof(CPUState, gpr[1]) },
1993 { "r2", offsetof(CPUState, gpr[2]) },
1994 { "r3", offsetof(CPUState, gpr[3]) },
1995 { "r4", offsetof(CPUState, gpr[4]) },
1996 { "r5", offsetof(CPUState, gpr[5]) },
1997 { "r6", offsetof(CPUState, gpr[6]) },
1998 { "r7", offsetof(CPUState, gpr[7]) },
1999 { "r8", offsetof(CPUState, gpr[8]) },
2000 { "r9", offsetof(CPUState, gpr[9]) },
2001 { "r10", offsetof(CPUState, gpr[10]) },
2002 { "r11", offsetof(CPUState, gpr[11]) },
2003 { "r12", offsetof(CPUState, gpr[12]) },
2004 { "r13", offsetof(CPUState, gpr[13]) },
2005 { "r14", offsetof(CPUState, gpr[14]) },
2006 { "r15", offsetof(CPUState, gpr[15]) },
2007 { "r16", offsetof(CPUState, gpr[16]) },
2008 { "r17", offsetof(CPUState, gpr[17]) },
2009 { "r18", offsetof(CPUState, gpr[18]) },
2010 { "r19", offsetof(CPUState, gpr[19]) },
2011 { "r20", offsetof(CPUState, gpr[20]) },
2012 { "r21", offsetof(CPUState, gpr[21]) },
2013 { "r22", offsetof(CPUState, gpr[22]) },
2014 { "r23", offsetof(CPUState, gpr[23]) },
2015 { "r24", offsetof(CPUState, gpr[24]) },
2016 { "r25", offsetof(CPUState, gpr[25]) },
2017 { "r26", offsetof(CPUState, gpr[26]) },
2018 { "r27", offsetof(CPUState, gpr[27]) },
2019 { "r28", offsetof(CPUState, gpr[28]) },
2020 { "r29", offsetof(CPUState, gpr[29]) },
2021 { "r30", offsetof(CPUState, gpr[30]) },
2022 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002023 /* Floating point registers */
2024 { "f0", offsetof(CPUState, fpr[0]) },
2025 { "f1", offsetof(CPUState, fpr[1]) },
2026 { "f2", offsetof(CPUState, fpr[2]) },
2027 { "f3", offsetof(CPUState, fpr[3]) },
2028 { "f4", offsetof(CPUState, fpr[4]) },
2029 { "f5", offsetof(CPUState, fpr[5]) },
2030 { "f6", offsetof(CPUState, fpr[6]) },
2031 { "f7", offsetof(CPUState, fpr[7]) },
2032 { "f8", offsetof(CPUState, fpr[8]) },
2033 { "f9", offsetof(CPUState, fpr[9]) },
2034 { "f10", offsetof(CPUState, fpr[10]) },
2035 { "f11", offsetof(CPUState, fpr[11]) },
2036 { "f12", offsetof(CPUState, fpr[12]) },
2037 { "f13", offsetof(CPUState, fpr[13]) },
2038 { "f14", offsetof(CPUState, fpr[14]) },
2039 { "f15", offsetof(CPUState, fpr[15]) },
2040 { "f16", offsetof(CPUState, fpr[16]) },
2041 { "f17", offsetof(CPUState, fpr[17]) },
2042 { "f18", offsetof(CPUState, fpr[18]) },
2043 { "f19", offsetof(CPUState, fpr[19]) },
2044 { "f20", offsetof(CPUState, fpr[20]) },
2045 { "f21", offsetof(CPUState, fpr[21]) },
2046 { "f22", offsetof(CPUState, fpr[22]) },
2047 { "f23", offsetof(CPUState, fpr[23]) },
2048 { "f24", offsetof(CPUState, fpr[24]) },
2049 { "f25", offsetof(CPUState, fpr[25]) },
2050 { "f26", offsetof(CPUState, fpr[26]) },
2051 { "f27", offsetof(CPUState, fpr[27]) },
2052 { "f28", offsetof(CPUState, fpr[28]) },
2053 { "f29", offsetof(CPUState, fpr[29]) },
2054 { "f30", offsetof(CPUState, fpr[30]) },
2055 { "f31", offsetof(CPUState, fpr[31]) },
2056 { "fpscr", offsetof(CPUState, fpscr) },
2057 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002058 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002059 { "lr", offsetof(CPUState, lr) },
2060 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002061 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002062 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002063 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002064 { "msr", 0, &monitor_get_msr, },
2065 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002066 { "tbu", 0, &monitor_get_tbu, },
2067 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002068#if defined(TARGET_PPC64)
2069 /* Address space register */
2070 { "asr", offsetof(CPUState, asr) },
2071#endif
2072 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002073 { "sdr1", offsetof(CPUState, sdr1) },
2074 { "sr0", offsetof(CPUState, sr[0]) },
2075 { "sr1", offsetof(CPUState, sr[1]) },
2076 { "sr2", offsetof(CPUState, sr[2]) },
2077 { "sr3", offsetof(CPUState, sr[3]) },
2078 { "sr4", offsetof(CPUState, sr[4]) },
2079 { "sr5", offsetof(CPUState, sr[5]) },
2080 { "sr6", offsetof(CPUState, sr[6]) },
2081 { "sr7", offsetof(CPUState, sr[7]) },
2082 { "sr8", offsetof(CPUState, sr[8]) },
2083 { "sr9", offsetof(CPUState, sr[9]) },
2084 { "sr10", offsetof(CPUState, sr[10]) },
2085 { "sr11", offsetof(CPUState, sr[11]) },
2086 { "sr12", offsetof(CPUState, sr[12]) },
2087 { "sr13", offsetof(CPUState, sr[13]) },
2088 { "sr14", offsetof(CPUState, sr[14]) },
2089 { "sr15", offsetof(CPUState, sr[15]) },
2090 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002091#elif defined(TARGET_SPARC)
2092 { "g0", offsetof(CPUState, gregs[0]) },
2093 { "g1", offsetof(CPUState, gregs[1]) },
2094 { "g2", offsetof(CPUState, gregs[2]) },
2095 { "g3", offsetof(CPUState, gregs[3]) },
2096 { "g4", offsetof(CPUState, gregs[4]) },
2097 { "g5", offsetof(CPUState, gregs[5]) },
2098 { "g6", offsetof(CPUState, gregs[6]) },
2099 { "g7", offsetof(CPUState, gregs[7]) },
2100 { "o0", 0, monitor_get_reg },
2101 { "o1", 1, monitor_get_reg },
2102 { "o2", 2, monitor_get_reg },
2103 { "o3", 3, monitor_get_reg },
2104 { "o4", 4, monitor_get_reg },
2105 { "o5", 5, monitor_get_reg },
2106 { "o6", 6, monitor_get_reg },
2107 { "o7", 7, monitor_get_reg },
2108 { "l0", 8, monitor_get_reg },
2109 { "l1", 9, monitor_get_reg },
2110 { "l2", 10, monitor_get_reg },
2111 { "l3", 11, monitor_get_reg },
2112 { "l4", 12, monitor_get_reg },
2113 { "l5", 13, monitor_get_reg },
2114 { "l6", 14, monitor_get_reg },
2115 { "l7", 15, monitor_get_reg },
2116 { "i0", 16, monitor_get_reg },
2117 { "i1", 17, monitor_get_reg },
2118 { "i2", 18, monitor_get_reg },
2119 { "i3", 19, monitor_get_reg },
2120 { "i4", 20, monitor_get_reg },
2121 { "i5", 21, monitor_get_reg },
2122 { "i6", 22, monitor_get_reg },
2123 { "i7", 23, monitor_get_reg },
2124 { "pc", offsetof(CPUState, pc) },
2125 { "npc", offsetof(CPUState, npc) },
2126 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002127#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002128 { "psr", 0, &monitor_get_psr, },
2129 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002130#endif
bellarde95c8d52004-09-30 22:22:08 +00002131 { "tbr", offsetof(CPUState, tbr) },
2132 { "fsr", offsetof(CPUState, fsr) },
2133 { "f0", offsetof(CPUState, fpr[0]) },
2134 { "f1", offsetof(CPUState, fpr[1]) },
2135 { "f2", offsetof(CPUState, fpr[2]) },
2136 { "f3", offsetof(CPUState, fpr[3]) },
2137 { "f4", offsetof(CPUState, fpr[4]) },
2138 { "f5", offsetof(CPUState, fpr[5]) },
2139 { "f6", offsetof(CPUState, fpr[6]) },
2140 { "f7", offsetof(CPUState, fpr[7]) },
2141 { "f8", offsetof(CPUState, fpr[8]) },
2142 { "f9", offsetof(CPUState, fpr[9]) },
2143 { "f10", offsetof(CPUState, fpr[10]) },
2144 { "f11", offsetof(CPUState, fpr[11]) },
2145 { "f12", offsetof(CPUState, fpr[12]) },
2146 { "f13", offsetof(CPUState, fpr[13]) },
2147 { "f14", offsetof(CPUState, fpr[14]) },
2148 { "f15", offsetof(CPUState, fpr[15]) },
2149 { "f16", offsetof(CPUState, fpr[16]) },
2150 { "f17", offsetof(CPUState, fpr[17]) },
2151 { "f18", offsetof(CPUState, fpr[18]) },
2152 { "f19", offsetof(CPUState, fpr[19]) },
2153 { "f20", offsetof(CPUState, fpr[20]) },
2154 { "f21", offsetof(CPUState, fpr[21]) },
2155 { "f22", offsetof(CPUState, fpr[22]) },
2156 { "f23", offsetof(CPUState, fpr[23]) },
2157 { "f24", offsetof(CPUState, fpr[24]) },
2158 { "f25", offsetof(CPUState, fpr[25]) },
2159 { "f26", offsetof(CPUState, fpr[26]) },
2160 { "f27", offsetof(CPUState, fpr[27]) },
2161 { "f28", offsetof(CPUState, fpr[28]) },
2162 { "f29", offsetof(CPUState, fpr[29]) },
2163 { "f30", offsetof(CPUState, fpr[30]) },
2164 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002165#ifdef TARGET_SPARC64
2166 { "f32", offsetof(CPUState, fpr[32]) },
2167 { "f34", offsetof(CPUState, fpr[34]) },
2168 { "f36", offsetof(CPUState, fpr[36]) },
2169 { "f38", offsetof(CPUState, fpr[38]) },
2170 { "f40", offsetof(CPUState, fpr[40]) },
2171 { "f42", offsetof(CPUState, fpr[42]) },
2172 { "f44", offsetof(CPUState, fpr[44]) },
2173 { "f46", offsetof(CPUState, fpr[46]) },
2174 { "f48", offsetof(CPUState, fpr[48]) },
2175 { "f50", offsetof(CPUState, fpr[50]) },
2176 { "f52", offsetof(CPUState, fpr[52]) },
2177 { "f54", offsetof(CPUState, fpr[54]) },
2178 { "f56", offsetof(CPUState, fpr[56]) },
2179 { "f58", offsetof(CPUState, fpr[58]) },
2180 { "f60", offsetof(CPUState, fpr[60]) },
2181 { "f62", offsetof(CPUState, fpr[62]) },
2182 { "asi", offsetof(CPUState, asi) },
2183 { "pstate", offsetof(CPUState, pstate) },
2184 { "cansave", offsetof(CPUState, cansave) },
2185 { "canrestore", offsetof(CPUState, canrestore) },
2186 { "otherwin", offsetof(CPUState, otherwin) },
2187 { "wstate", offsetof(CPUState, wstate) },
2188 { "cleanwin", offsetof(CPUState, cleanwin) },
2189 { "fprs", offsetof(CPUState, fprs) },
2190#endif
bellard9307c4c2004-04-04 12:57:25 +00002191#endif
2192 { NULL },
2193};
2194
aliguori376253e2009-03-05 23:01:23 +00002195static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002196{
aliguori376253e2009-03-05 23:01:23 +00002197 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002198 longjmp(expr_env, 1);
2199}
2200
bellard6a00d602005-11-21 23:25:50 +00002201/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002202static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002203{
blueswir18662d652008-10-02 18:32:44 +00002204 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002205 void *ptr;
2206
bellard9307c4c2004-04-04 12:57:25 +00002207 for(md = monitor_defs; md->name != NULL; md++) {
2208 if (compare_cmd(name, md->name)) {
2209 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002210 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002211 } else {
bellard6a00d602005-11-21 23:25:50 +00002212 CPUState *env = mon_get_cpu();
2213 if (!env)
2214 return -2;
2215 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002216 switch(md->type) {
2217 case MD_I32:
2218 *pval = *(int32_t *)ptr;
2219 break;
2220 case MD_TLONG:
2221 *pval = *(target_long *)ptr;
2222 break;
2223 default:
2224 *pval = 0;
2225 break;
2226 }
bellard9307c4c2004-04-04 12:57:25 +00002227 }
2228 return 0;
2229 }
2230 }
2231 return -1;
2232}
2233
2234static void next(void)
2235{
Blue Swirl660f11b2009-07-31 21:16:51 +00002236 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002237 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002238 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002239 pch++;
2240 }
2241}
2242
aliguori376253e2009-03-05 23:01:23 +00002243static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002244
aliguori376253e2009-03-05 23:01:23 +00002245static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002246{
blueswir1c2efc952007-09-25 17:28:42 +00002247 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002248 char *p;
bellard6a00d602005-11-21 23:25:50 +00002249 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002250
2251 switch(*pch) {
2252 case '+':
2253 next();
aliguori376253e2009-03-05 23:01:23 +00002254 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002255 break;
2256 case '-':
2257 next();
aliguori376253e2009-03-05 23:01:23 +00002258 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002259 break;
2260 case '~':
2261 next();
aliguori376253e2009-03-05 23:01:23 +00002262 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002263 break;
2264 case '(':
2265 next();
aliguori376253e2009-03-05 23:01:23 +00002266 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002267 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002268 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002269 }
2270 next();
2271 break;
bellard81d09122004-07-14 17:21:37 +00002272 case '\'':
2273 pch++;
2274 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002275 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002276 n = *pch;
2277 pch++;
2278 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002279 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002280 next();
2281 break;
bellard9307c4c2004-04-04 12:57:25 +00002282 case '$':
2283 {
2284 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002285 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002286
bellard9307c4c2004-04-04 12:57:25 +00002287 pch++;
2288 q = buf;
2289 while ((*pch >= 'a' && *pch <= 'z') ||
2290 (*pch >= 'A' && *pch <= 'Z') ||
2291 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002292 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002293 if ((q - buf) < sizeof(buf) - 1)
2294 *q++ = *pch;
2295 pch++;
2296 }
blueswir1cd390082008-11-16 13:53:32 +00002297 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002298 pch++;
2299 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002300 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002301 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002302 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002303 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002304 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002305 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002306 }
2307 break;
2308 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002309 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002310 n = 0;
2311 break;
2312 default:
blueswir17743e582007-09-24 18:39:04 +00002313#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002314 n = strtoull(pch, &p, 0);
2315#else
bellard9307c4c2004-04-04 12:57:25 +00002316 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002317#endif
bellard9307c4c2004-04-04 12:57:25 +00002318 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002319 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002320 }
2321 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002322 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002323 pch++;
2324 break;
2325 }
2326 return n;
2327}
2328
2329
aliguori376253e2009-03-05 23:01:23 +00002330static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002331{
blueswir1c2efc952007-09-25 17:28:42 +00002332 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002333 int op;
ths3b46e622007-09-17 08:09:54 +00002334
aliguori376253e2009-03-05 23:01:23 +00002335 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002336 for(;;) {
2337 op = *pch;
2338 if (op != '*' && op != '/' && op != '%')
2339 break;
2340 next();
aliguori376253e2009-03-05 23:01:23 +00002341 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002342 switch(op) {
2343 default:
2344 case '*':
2345 val *= val2;
2346 break;
2347 case '/':
2348 case '%':
ths5fafdf22007-09-16 21:08:06 +00002349 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002350 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002351 if (op == '/')
2352 val /= val2;
2353 else
2354 val %= val2;
2355 break;
2356 }
2357 }
2358 return val;
2359}
2360
aliguori376253e2009-03-05 23:01:23 +00002361static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002362{
blueswir1c2efc952007-09-25 17:28:42 +00002363 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002364 int op;
bellard9307c4c2004-04-04 12:57:25 +00002365
aliguori376253e2009-03-05 23:01:23 +00002366 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002367 for(;;) {
2368 op = *pch;
2369 if (op != '&' && op != '|' && op != '^')
2370 break;
2371 next();
aliguori376253e2009-03-05 23:01:23 +00002372 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002373 switch(op) {
2374 default:
2375 case '&':
2376 val &= val2;
2377 break;
2378 case '|':
2379 val |= val2;
2380 break;
2381 case '^':
2382 val ^= val2;
2383 break;
2384 }
2385 }
2386 return val;
2387}
2388
aliguori376253e2009-03-05 23:01:23 +00002389static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002390{
blueswir1c2efc952007-09-25 17:28:42 +00002391 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002392 int op;
bellard9307c4c2004-04-04 12:57:25 +00002393
aliguori376253e2009-03-05 23:01:23 +00002394 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002395 for(;;) {
2396 op = *pch;
2397 if (op != '+' && op != '-')
2398 break;
2399 next();
aliguori376253e2009-03-05 23:01:23 +00002400 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002401 if (op == '+')
2402 val += val2;
2403 else
2404 val -= val2;
2405 }
2406 return val;
2407}
2408
aliguori376253e2009-03-05 23:01:23 +00002409static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002410{
2411 pch = *pp;
2412 if (setjmp(expr_env)) {
2413 *pp = pch;
2414 return -1;
2415 }
blueswir1cd390082008-11-16 13:53:32 +00002416 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002417 pch++;
aliguori376253e2009-03-05 23:01:23 +00002418 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002419 *pp = pch;
2420 return 0;
2421}
2422
2423static int get_str(char *buf, int buf_size, const char **pp)
2424{
2425 const char *p;
2426 char *q;
2427 int c;
2428
bellard81d09122004-07-14 17:21:37 +00002429 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002430 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002431 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002432 p++;
2433 if (*p == '\0') {
2434 fail:
bellard81d09122004-07-14 17:21:37 +00002435 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002436 *pp = p;
2437 return -1;
2438 }
bellard9307c4c2004-04-04 12:57:25 +00002439 if (*p == '\"') {
2440 p++;
2441 while (*p != '\0' && *p != '\"') {
2442 if (*p == '\\') {
2443 p++;
2444 c = *p++;
2445 switch(c) {
2446 case 'n':
2447 c = '\n';
2448 break;
2449 case 'r':
2450 c = '\r';
2451 break;
2452 case '\\':
2453 case '\'':
2454 case '\"':
2455 break;
2456 default:
2457 qemu_printf("unsupported escape code: '\\%c'\n", c);
2458 goto fail;
2459 }
2460 if ((q - buf) < buf_size - 1) {
2461 *q++ = c;
2462 }
2463 } else {
2464 if ((q - buf) < buf_size - 1) {
2465 *q++ = *p;
2466 }
2467 p++;
2468 }
2469 }
2470 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002471 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002472 goto fail;
2473 }
2474 p++;
2475 } else {
blueswir1cd390082008-11-16 13:53:32 +00002476 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002477 if ((q - buf) < buf_size - 1) {
2478 *q++ = *p;
2479 }
2480 p++;
2481 }
bellard9307c4c2004-04-04 12:57:25 +00002482 }
bellard81d09122004-07-14 17:21:37 +00002483 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002484 *pp = p;
2485 return 0;
2486}
2487
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002488/*
2489 * Store the command-name in cmdname, and return a pointer to
2490 * the remaining of the command string.
2491 */
2492static const char *get_command_name(const char *cmdline,
2493 char *cmdname, size_t nlen)
2494{
2495 size_t len;
2496 const char *p, *pstart;
2497
2498 p = cmdline;
2499 while (qemu_isspace(*p))
2500 p++;
2501 if (*p == '\0')
2502 return NULL;
2503 pstart = p;
2504 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2505 p++;
2506 len = p - pstart;
2507 if (len > nlen - 1)
2508 len = nlen - 1;
2509 memcpy(cmdname, pstart, len);
2510 cmdname[len] = '\0';
2511 return p;
2512}
2513
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002514/**
2515 * Read key of 'type' into 'key' and return the current
2516 * 'type' pointer.
2517 */
2518static char *key_get_info(const char *type, char **key)
2519{
2520 size_t len;
2521 char *p, *str;
2522
2523 if (*type == ',')
2524 type++;
2525
2526 p = strchr(type, ':');
2527 if (!p) {
2528 *key = NULL;
2529 return NULL;
2530 }
2531 len = p - type;
2532
2533 str = qemu_malloc(len + 1);
2534 memcpy(str, type, len);
2535 str[len] = '\0';
2536
2537 *key = str;
2538 return ++p;
2539}
2540
bellard9307c4c2004-04-04 12:57:25 +00002541static int default_fmt_format = 'x';
2542static int default_fmt_size = 4;
2543
2544#define MAX_ARGS 16
2545
aliguori376253e2009-03-05 23:01:23 +00002546static void monitor_handle_command(Monitor *mon, const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002547{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002548 const char *p, *typestr;
2549 int c, nb_args, i, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002550 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002551 char cmdname[256];
2552 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002553 char *key;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002554 QDict *qdict;
bellard9307c4c2004-04-04 12:57:25 +00002555 void *str_allocated[MAX_ARGS];
2556 void *args[MAX_ARGS];
aliguori376253e2009-03-05 23:01:23 +00002557 void (*handler_0)(Monitor *mon);
2558 void (*handler_1)(Monitor *mon, void *arg0);
2559 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2560 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2561 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2562 void *arg3);
2563 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2564 void *arg3, void *arg4);
2565 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2566 void *arg3, void *arg4, void *arg5);
2567 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2568 void *arg3, void *arg4, void *arg5, void *arg6);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002569 void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2570 void *arg3, void *arg4, void *arg5, void *arg6,
2571 void *arg7);
2572 void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2573 void *arg3, void *arg4, void *arg5, void *arg6,
2574 void *arg7, void *arg8);
2575 void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2576 void *arg3, void *arg4, void *arg5, void *arg6,
2577 void *arg7, void *arg8, void *arg9);
bellard9dc39cb2004-03-14 21:38:27 +00002578
2579#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002580 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002581#endif
ths3b46e622007-09-17 08:09:54 +00002582
bellard9307c4c2004-04-04 12:57:25 +00002583 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002584 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2585 if (!p)
bellard9dc39cb2004-03-14 21:38:27 +00002586 return;
ths3b46e622007-09-17 08:09:54 +00002587
bellard9307c4c2004-04-04 12:57:25 +00002588 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002589 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002590 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002591 break;
bellard9dc39cb2004-03-14 21:38:27 +00002592 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002593
2594 if (cmd->name == NULL) {
2595 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2596 return;
2597 }
bellard9307c4c2004-04-04 12:57:25 +00002598
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002599 qdict = qdict_new();
2600
bellard9307c4c2004-04-04 12:57:25 +00002601 for(i = 0; i < MAX_ARGS; i++)
2602 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002603
bellard9307c4c2004-04-04 12:57:25 +00002604 /* parse the parameters */
2605 typestr = cmd->args_type;
2606 nb_args = 0;
2607 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002608 typestr = key_get_info(typestr, &key);
2609 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002610 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002611 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002612 typestr++;
2613 switch(c) {
2614 case 'F':
bellard81d09122004-07-14 17:21:37 +00002615 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002616 case 's':
2617 {
2618 int ret;
2619 char *str;
ths3b46e622007-09-17 08:09:54 +00002620
blueswir1cd390082008-11-16 13:53:32 +00002621 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002622 p++;
2623 if (*typestr == '?') {
2624 typestr++;
2625 if (*p == '\0') {
2626 /* no optional string: NULL argument */
2627 str = NULL;
2628 goto add_str;
2629 }
2630 }
2631 ret = get_str(buf, sizeof(buf), &p);
2632 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002633 switch(c) {
2634 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002635 monitor_printf(mon, "%s: filename expected\n",
2636 cmdname);
bellard81d09122004-07-14 17:21:37 +00002637 break;
2638 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002639 monitor_printf(mon, "%s: block device name expected\n",
2640 cmdname);
bellard81d09122004-07-14 17:21:37 +00002641 break;
2642 default:
aliguori376253e2009-03-05 23:01:23 +00002643 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002644 break;
2645 }
bellard9307c4c2004-04-04 12:57:25 +00002646 goto fail;
2647 }
2648 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002649 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002650 str_allocated[nb_args] = str;
2651 add_str:
2652 if (nb_args >= MAX_ARGS) {
2653 error_args:
aliguori376253e2009-03-05 23:01:23 +00002654 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002655 goto fail;
2656 }
2657 args[nb_args++] = str;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002658 if (str)
2659 qdict_put(qdict, key, qstring_from_str(str));
bellard9307c4c2004-04-04 12:57:25 +00002660 }
2661 break;
2662 case '/':
2663 {
2664 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002665
blueswir1cd390082008-11-16 13:53:32 +00002666 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002667 p++;
2668 if (*p == '/') {
2669 /* format found */
2670 p++;
2671 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002672 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002673 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002674 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002675 count = count * 10 + (*p - '0');
2676 p++;
2677 }
2678 }
2679 size = -1;
2680 format = -1;
2681 for(;;) {
2682 switch(*p) {
2683 case 'o':
2684 case 'd':
2685 case 'u':
2686 case 'x':
2687 case 'i':
2688 case 'c':
2689 format = *p++;
2690 break;
2691 case 'b':
2692 size = 1;
2693 p++;
2694 break;
2695 case 'h':
2696 size = 2;
2697 p++;
2698 break;
2699 case 'w':
2700 size = 4;
2701 p++;
2702 break;
2703 case 'g':
2704 case 'L':
2705 size = 8;
2706 p++;
2707 break;
2708 default:
2709 goto next;
2710 }
2711 }
2712 next:
blueswir1cd390082008-11-16 13:53:32 +00002713 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002714 monitor_printf(mon, "invalid char in format: '%c'\n",
2715 *p);
bellard9307c4c2004-04-04 12:57:25 +00002716 goto fail;
2717 }
bellard9307c4c2004-04-04 12:57:25 +00002718 if (format < 0)
2719 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002720 if (format != 'i') {
2721 /* for 'i', not specifying a size gives -1 as size */
2722 if (size < 0)
2723 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002724 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002725 }
bellard9307c4c2004-04-04 12:57:25 +00002726 default_fmt_format = format;
2727 } else {
2728 count = 1;
2729 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002730 if (format != 'i') {
2731 size = default_fmt_size;
2732 } else {
2733 size = -1;
2734 }
bellard9307c4c2004-04-04 12:57:25 +00002735 }
2736 if (nb_args + 3 > MAX_ARGS)
2737 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002738 args[nb_args++] = (void*)(long)count;
2739 args[nb_args++] = (void*)(long)format;
2740 args[nb_args++] = (void*)(long)size;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002741 qdict_put(qdict, "count", qint_from_int(count));
2742 qdict_put(qdict, "format", qint_from_int(format));
2743 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002744 }
2745 break;
2746 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002747 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002748 {
blueswir1c2efc952007-09-25 17:28:42 +00002749 int64_t val;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002750 int dict_add = 1;
blueswir17743e582007-09-24 18:39:04 +00002751
blueswir1cd390082008-11-16 13:53:32 +00002752 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002753 p++;
bellard34405572004-06-08 00:55:58 +00002754 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002755 if (*typestr == '?') {
2756 if (*p == '\0')
2757 has_arg = 0;
2758 else
2759 has_arg = 1;
2760 } else {
2761 if (*p == '.') {
2762 p++;
blueswir1cd390082008-11-16 13:53:32 +00002763 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002764 p++;
2765 has_arg = 1;
2766 } else {
2767 has_arg = 0;
2768 }
2769 }
bellard13224a82006-07-14 22:03:35 +00002770 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002771 if (nb_args >= MAX_ARGS)
2772 goto error_args;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002773 dict_add = has_arg;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002774 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002775 if (!has_arg) {
2776 if (nb_args >= MAX_ARGS)
2777 goto error_args;
2778 val = -1;
2779 goto add_num;
2780 }
2781 }
aliguori376253e2009-03-05 23:01:23 +00002782 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002783 goto fail;
2784 add_num:
bellard92a31b12005-02-10 22:00:52 +00002785 if (c == 'i') {
2786 if (nb_args >= MAX_ARGS)
2787 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002788 args[nb_args++] = (void *)(long)val;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002789 if (dict_add)
2790 qdict_put(qdict, key, qint_from_int(val));
bellard92a31b12005-02-10 22:00:52 +00002791 } else {
2792 if ((nb_args + 1) >= MAX_ARGS)
2793 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002794#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002795 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002796#else
2797 args[nb_args++] = (void *)0;
2798#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002799 args[nb_args++] = (void *)(long)(val & 0xffffffff);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002800 qdict_put(qdict, key, qint_from_int(val));
bellard92a31b12005-02-10 22:00:52 +00002801 }
bellard9307c4c2004-04-04 12:57:25 +00002802 }
2803 break;
2804 case '-':
2805 {
2806 int has_option;
2807 /* option */
ths3b46e622007-09-17 08:09:54 +00002808
bellard9307c4c2004-04-04 12:57:25 +00002809 c = *typestr++;
2810 if (c == '\0')
2811 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002812 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002813 p++;
2814 has_option = 0;
2815 if (*p == '-') {
2816 p++;
2817 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002818 monitor_printf(mon, "%s: unsupported option -%c\n",
2819 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002820 goto fail;
2821 }
2822 p++;
2823 has_option = 1;
2824 }
2825 if (nb_args >= MAX_ARGS)
2826 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002827 args[nb_args++] = (void *)(long)has_option;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002828 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00002829 }
2830 break;
2831 default:
2832 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002833 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002834 goto fail;
2835 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002836 qemu_free(key);
2837 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002838 }
2839 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002840 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002841 p++;
2842 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002843 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2844 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002845 goto fail;
2846 }
2847
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002848 qemu_errors_to_mon(mon);
bellard9307c4c2004-04-04 12:57:25 +00002849 switch(nb_args) {
2850 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002851 handler_0 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002852 handler_0(mon);
bellard9307c4c2004-04-04 12:57:25 +00002853 break;
2854 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002855 handler_1 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002856 handler_1(mon, args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002857 break;
2858 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002859 handler_2 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002860 handler_2(mon, args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002861 break;
2862 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002863 handler_3 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002864 handler_3(mon, args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002865 break;
2866 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002867 handler_4 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002868 handler_4(mon, args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002869 break;
2870 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002871 handler_5 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002872 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002873 break;
bellard34405572004-06-08 00:55:58 +00002874 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002875 handler_6 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002876 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002877 break;
bellardec36b692006-07-16 18:57:03 +00002878 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002879 handler_7 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002880 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2881 args[6]);
bellardec36b692006-07-16 18:57:03 +00002882 break;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002883 case 8:
2884 handler_8 = cmd->handler;
2885 handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2886 args[6], args[7]);
2887 break;
2888 case 9:
2889 handler_9 = cmd->handler;
2890 handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2891 args[6], args[7], args[8]);
2892 break;
2893 case 10:
2894 handler_10 = cmd->handler;
2895 handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2896 args[6], args[7], args[8], args[9]);
2897 break;
bellard9307c4c2004-04-04 12:57:25 +00002898 default:
aliguori376253e2009-03-05 23:01:23 +00002899 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002900 break;
bellard9307c4c2004-04-04 12:57:25 +00002901 }
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002902 qemu_errors_to_previous();
2903
bellard9307c4c2004-04-04 12:57:25 +00002904 fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002905 qemu_free(key);
bellard9307c4c2004-04-04 12:57:25 +00002906 for(i = 0; i < MAX_ARGS; i++)
2907 qemu_free(str_allocated[i]);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002908 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002909}
2910
bellard81d09122004-07-14 17:21:37 +00002911static void cmd_completion(const char *name, const char *list)
2912{
2913 const char *p, *pstart;
2914 char cmd[128];
2915 int len;
2916
2917 p = list;
2918 for(;;) {
2919 pstart = p;
2920 p = strchr(p, '|');
2921 if (!p)
2922 p = pstart + strlen(pstart);
2923 len = p - pstart;
2924 if (len > sizeof(cmd) - 2)
2925 len = sizeof(cmd) - 2;
2926 memcpy(cmd, pstart, len);
2927 cmd[len] = '\0';
2928 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002929 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002930 }
2931 if (*p == '\0')
2932 break;
2933 p++;
2934 }
2935}
2936
2937static void file_completion(const char *input)
2938{
2939 DIR *ffs;
2940 struct dirent *d;
2941 char path[1024];
2942 char file[1024], file_prefix[1024];
2943 int input_path_len;
2944 const char *p;
2945
ths5fafdf22007-09-16 21:08:06 +00002946 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002947 if (!p) {
2948 input_path_len = 0;
2949 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002950 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002951 } else {
2952 input_path_len = p - input + 1;
2953 memcpy(path, input, input_path_len);
2954 if (input_path_len > sizeof(path) - 1)
2955 input_path_len = sizeof(path) - 1;
2956 path[input_path_len] = '\0';
2957 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2958 }
2959#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002960 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2961 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002962#endif
2963 ffs = opendir(path);
2964 if (!ffs)
2965 return;
2966 for(;;) {
2967 struct stat sb;
2968 d = readdir(ffs);
2969 if (!d)
2970 break;
2971 if (strstart(d->d_name, file_prefix, NULL)) {
2972 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002973 if (input_path_len < sizeof(file))
2974 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2975 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002976 /* stat the file to find out if it's a directory.
2977 * In that case add a slash to speed up typing long paths
2978 */
2979 stat(file, &sb);
2980 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002981 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00002982 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002983 }
2984 }
2985 closedir(ffs);
2986}
2987
aliguori51de9762009-03-05 23:00:43 +00002988static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002989{
aliguori51de9762009-03-05 23:00:43 +00002990 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002991 const char *input = opaque;
2992
2993 if (input[0] == '\0' ||
2994 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00002995 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00002996 }
2997}
2998
2999/* NOTE: this parser is an approximate form of the real command parser */
3000static void parse_cmdline(const char *cmdline,
3001 int *pnb_args, char **args)
3002{
3003 const char *p;
3004 int nb_args, ret;
3005 char buf[1024];
3006
3007 p = cmdline;
3008 nb_args = 0;
3009 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003010 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003011 p++;
3012 if (*p == '\0')
3013 break;
3014 if (nb_args >= MAX_ARGS)
3015 break;
3016 ret = get_str(buf, sizeof(buf), &p);
3017 args[nb_args] = qemu_strdup(buf);
3018 nb_args++;
3019 if (ret < 0)
3020 break;
3021 }
3022 *pnb_args = nb_args;
3023}
3024
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003025static const char *next_arg_type(const char *typestr)
3026{
3027 const char *p = strchr(typestr, ':');
3028 return (p != NULL ? ++p : typestr);
3029}
3030
aliguori4c36ba32009-03-05 23:01:37 +00003031static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003032{
3033 const char *cmdname;
3034 char *args[MAX_ARGS];
3035 int nb_args, i, len;
3036 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00003037 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003038 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003039
3040 parse_cmdline(cmdline, &nb_args, args);
3041#ifdef DEBUG_COMPLETION
3042 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003043 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003044 }
3045#endif
3046
3047 /* if the line ends with a space, it means we want to complete the
3048 next arg */
3049 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003050 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003051 if (nb_args >= MAX_ARGS)
3052 return;
3053 args[nb_args++] = qemu_strdup("");
3054 }
3055 if (nb_args <= 1) {
3056 /* command completion */
3057 if (nb_args == 0)
3058 cmdname = "";
3059 else
3060 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003061 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003062 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003063 cmd_completion(cmdname, cmd->name);
3064 }
3065 } else {
3066 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003067 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003068 if (compare_cmd(args[0], cmd->name))
3069 goto found;
3070 }
3071 return;
3072 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003073 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003074 for(i = 0; i < nb_args - 2; i++) {
3075 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003076 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003077 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003078 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003079 }
3080 }
3081 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003082 if (*ptype == '-' && ptype[1] != '\0') {
3083 ptype += 2;
3084 }
bellard81d09122004-07-14 17:21:37 +00003085 switch(*ptype) {
3086 case 'F':
3087 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003088 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003089 file_completion(str);
3090 break;
3091 case 'B':
3092 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003093 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003094 bdrv_iterate(block_completion_it, (void *)str);
3095 break;
bellard7fe48482004-10-09 18:08:01 +00003096 case 's':
3097 /* XXX: more generic ? */
3098 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003099 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003100 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3101 cmd_completion(str, cmd->name);
3102 }
bellard64866c32006-05-07 18:03:31 +00003103 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003104 char *sep = strrchr(str, '-');
3105 if (sep)
3106 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003107 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003108 for(key = key_defs; key->name != NULL; key++) {
3109 cmd_completion(str, key->name);
3110 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003111 } else if (!strcmp(cmd->name, "help|?")) {
3112 readline_set_completion_index(cur_mon->rs, strlen(str));
3113 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3114 cmd_completion(str, cmd->name);
3115 }
bellard7fe48482004-10-09 18:08:01 +00003116 }
3117 break;
bellard81d09122004-07-14 17:21:37 +00003118 default:
3119 break;
3120 }
3121 }
3122 for(i = 0; i < nb_args; i++)
3123 qemu_free(args[i]);
3124}
3125
aliguori731b0362009-03-05 23:01:42 +00003126static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003127{
aliguori731b0362009-03-05 23:01:42 +00003128 Monitor *mon = opaque;
3129
3130 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003131}
3132
aliguori731b0362009-03-05 23:01:42 +00003133static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003134{
aliguori731b0362009-03-05 23:01:42 +00003135 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003136 int i;
aliguori376253e2009-03-05 23:01:23 +00003137
aliguori731b0362009-03-05 23:01:42 +00003138 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003139
aliguoricde76ee2009-03-05 23:01:51 +00003140 if (cur_mon->rs) {
3141 for (i = 0; i < size; i++)
3142 readline_handle_byte(cur_mon->rs, buf[i]);
3143 } else {
3144 if (size == 0 || buf[size - 1] != 0)
3145 monitor_printf(cur_mon, "corrupted command\n");
3146 else
3147 monitor_handle_command(cur_mon, (char *)buf);
3148 }
aliguori731b0362009-03-05 23:01:42 +00003149
3150 cur_mon = old_mon;
3151}
aliguorid8f44602008-10-06 13:52:44 +00003152
aliguori376253e2009-03-05 23:01:23 +00003153static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003154{
aliguori731b0362009-03-05 23:01:42 +00003155 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003156 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003157 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003158}
3159
aliguoricde76ee2009-03-05 23:01:51 +00003160int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003161{
aliguoricde76ee2009-03-05 23:01:51 +00003162 if (!mon->rs)
3163 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003164 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003165 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003166}
3167
aliguori376253e2009-03-05 23:01:23 +00003168void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003169{
aliguoricde76ee2009-03-05 23:01:51 +00003170 if (!mon->rs)
3171 return;
aliguori731b0362009-03-05 23:01:42 +00003172 if (--mon->suspend_cnt == 0)
3173 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003174}
3175
aliguori731b0362009-03-05 23:01:42 +00003176static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003177{
aliguori376253e2009-03-05 23:01:23 +00003178 Monitor *mon = opaque;
3179
aliguori2724b182009-03-05 23:01:47 +00003180 switch (event) {
3181 case CHR_EVENT_MUX_IN:
3182 readline_restart(mon->rs);
3183 monitor_resume(mon);
3184 monitor_flush(mon);
3185 break;
ths86e94de2007-01-05 22:01:59 +00003186
aliguori2724b182009-03-05 23:01:47 +00003187 case CHR_EVENT_MUX_OUT:
3188 if (mon->suspend_cnt == 0)
3189 monitor_printf(mon, "\n");
3190 monitor_flush(mon);
3191 monitor_suspend(mon);
3192 break;
3193
3194 case CHR_EVENT_RESET:
3195 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3196 "information\n", QEMU_VERSION);
3197 if (mon->chr->focus == 0)
3198 readline_show_prompt(mon->rs);
3199 break;
3200 }
ths86e94de2007-01-05 22:01:59 +00003201}
3202
aliguori76655d62009-03-06 20:27:37 +00003203
3204/*
3205 * Local variables:
3206 * c-indent-level: 4
3207 * c-basic-offset: 4
3208 * tab-width: 8
3209 * End:
3210 */
3211
aliguori731b0362009-03-05 23:01:42 +00003212void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003213{
aliguori731b0362009-03-05 23:01:42 +00003214 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003215 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003216
3217 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003218 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003219 is_first_init = 0;
3220 }
aliguori87127162009-03-05 23:01:29 +00003221
3222 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003223
aliguori87127162009-03-05 23:01:29 +00003224 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003225 mon->flags = flags;
aliguori2724b182009-03-05 23:01:47 +00003226 if (mon->chr->focus != 0)
3227 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
aliguoricde76ee2009-03-05 23:01:51 +00003228 if (flags & MONITOR_USE_READLINE) {
3229 mon->rs = readline_init(mon, monitor_find_completion);
3230 monitor_read_command(mon, 0);
3231 }
aliguori87127162009-03-05 23:01:29 +00003232
aliguori731b0362009-03-05 23:01:42 +00003233 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3234 mon);
aliguori87127162009-03-05 23:01:29 +00003235
3236 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003237 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003238 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003239}
3240
aliguori376253e2009-03-05 23:01:23 +00003241static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003242{
aliguoribb5fc202009-03-05 23:01:15 +00003243 BlockDriverState *bs = opaque;
3244 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003245
aliguoribb5fc202009-03-05 23:01:15 +00003246 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003247 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003248 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003249 }
aliguori731b0362009-03-05 23:01:42 +00003250 if (mon->password_completion_cb)
3251 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003252
aliguori731b0362009-03-05 23:01:42 +00003253 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003254}
aliguoric0f4ce72009-03-05 23:01:01 +00003255
aliguori376253e2009-03-05 23:01:23 +00003256void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003257 BlockDriverCompletionFunc *completion_cb,
3258 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003259{
aliguoricde76ee2009-03-05 23:01:51 +00003260 int err;
3261
aliguoribb5fc202009-03-05 23:01:15 +00003262 if (!bdrv_key_required(bs)) {
3263 if (completion_cb)
3264 completion_cb(opaque, 0);
3265 return;
3266 }
aliguoric0f4ce72009-03-05 23:01:01 +00003267
aliguori376253e2009-03-05 23:01:23 +00003268 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3269 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003270
aliguori731b0362009-03-05 23:01:42 +00003271 mon->password_completion_cb = completion_cb;
3272 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003273
aliguoricde76ee2009-03-05 23:01:51 +00003274 err = monitor_read_password(mon, bdrv_password_cb, bs);
3275
3276 if (err && completion_cb)
3277 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003278}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003279
3280typedef struct QemuErrorSink QemuErrorSink;
3281struct QemuErrorSink {
3282 enum {
3283 ERR_SINK_FILE,
3284 ERR_SINK_MONITOR,
3285 } dest;
3286 union {
3287 FILE *fp;
3288 Monitor *mon;
3289 };
3290 QemuErrorSink *previous;
3291};
3292
Blue Swirl528e93a2009-08-31 15:14:40 +00003293static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003294
3295void qemu_errors_to_file(FILE *fp)
3296{
3297 QemuErrorSink *sink;
3298
3299 sink = qemu_mallocz(sizeof(*sink));
3300 sink->dest = ERR_SINK_FILE;
3301 sink->fp = fp;
3302 sink->previous = qemu_error_sink;
3303 qemu_error_sink = sink;
3304}
3305
3306void qemu_errors_to_mon(Monitor *mon)
3307{
3308 QemuErrorSink *sink;
3309
3310 sink = qemu_mallocz(sizeof(*sink));
3311 sink->dest = ERR_SINK_MONITOR;
3312 sink->mon = mon;
3313 sink->previous = qemu_error_sink;
3314 qemu_error_sink = sink;
3315}
3316
3317void qemu_errors_to_previous(void)
3318{
3319 QemuErrorSink *sink;
3320
3321 assert(qemu_error_sink != NULL);
3322 sink = qemu_error_sink;
3323 qemu_error_sink = sink->previous;
3324 qemu_free(sink);
3325}
3326
3327void qemu_error(const char *fmt, ...)
3328{
3329 va_list args;
3330
3331 assert(qemu_error_sink != NULL);
3332 switch (qemu_error_sink->dest) {
3333 case ERR_SINK_FILE:
3334 va_start(args, fmt);
3335 vfprintf(qemu_error_sink->fp, fmt, args);
3336 va_end(args);
3337 break;
3338 case ERR_SINK_MONITOR:
3339 va_start(args, fmt);
3340 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3341 va_end(args);
3342 break;
3343 }
3344}