blob: 19411b28d41eb31e913f750561f35d31b3872f59 [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 Capitulinod54908a2009-08-28 15:27:13 -0300257static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300258{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300259 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300260}
261
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300262static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000263{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200264 int all_devices;
265 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300266 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000267
bellard7954c732006-08-01 15:52:40 +0000268 all_devices = !strcmp(device, "all");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200269 TAILQ_FOREACH(dinfo, &drives, next) {
270 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300271 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200272 continue;
273 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000274 }
275}
276
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300277static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000278{
aliguori376253e2009-03-05 23:01:23 +0000279 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300280 const char *item = qdict_get_try_str(qdict, "item");
aliguori376253e2009-03-05 23:01:23 +0000281 void (*handler)(Monitor *);
bellard9dc39cb2004-03-14 21:38:27 +0000282
bellard9307c4c2004-04-04 12:57:25 +0000283 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000284 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000285 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000286 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000287 goto found;
288 }
289 help:
aliguori376253e2009-03-05 23:01:23 +0000290 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000291 return;
292 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000293 handler = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +0000294 handler(mon);
bellard9dc39cb2004-03-14 21:38:27 +0000295}
296
aliguori376253e2009-03-05 23:01:23 +0000297static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000298{
pbrook4a19f1e2009-04-07 23:17:49 +0000299 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000300}
301
aliguori376253e2009-03-05 23:01:23 +0000302static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000303{
304 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000305 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000306}
307
aurel32bf4f74c2008-12-18 22:42:34 +0000308#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000309static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000310{
aliguori376253e2009-03-05 23:01:23 +0000311 monitor_printf(mon, "HPET is %s by QEMU\n",
312 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000313}
aurel32bf4f74c2008-12-18 22:42:34 +0000314#endif
aliguori16b29ae2008-12-17 23:28:44 +0000315
aliguori376253e2009-03-05 23:01:23 +0000316static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000317{
aliguori376253e2009-03-05 23:01:23 +0000318 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
319 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
320 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
321 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
322 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000323}
324
bellard6a00d602005-11-21 23:25:50 +0000325/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000326static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000327{
328 CPUState *env;
329
330 for(env = first_cpu; env != NULL; env = env->next_cpu) {
331 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000332 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000333 return 0;
334 }
335 }
336 return -1;
337}
338
pbrook9596ebb2007-11-18 01:44:38 +0000339static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000340{
aliguori731b0362009-03-05 23:01:42 +0000341 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000342 mon_set_cpu(0);
343 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300344 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000345 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000346}
347
aliguori376253e2009-03-05 23:01:23 +0000348static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000349{
bellard6a00d602005-11-21 23:25:50 +0000350 CPUState *env;
351 env = mon_get_cpu();
352 if (!env)
353 return;
bellard9307c4c2004-04-04 12:57:25 +0000354#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000355 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000356 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000357#else
aliguori376253e2009-03-05 23:01:23 +0000358 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000359 0);
bellard9307c4c2004-04-04 12:57:25 +0000360#endif
361}
362
aliguori376253e2009-03-05 23:01:23 +0000363static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000364{
365 CPUState *env;
366
367 /* just to set the default cpu if not already done */
368 mon_get_cpu();
369
370 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Avi Kivity4c0960c2009-08-17 23:19:53 +0300371 cpu_synchronize_state(env);
aliguori376253e2009-03-05 23:01:23 +0000372 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000373 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000374 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000375#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000376 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
377 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000378#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000379 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000380#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000381 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
382 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000383#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000384 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000385#endif
thsead93602007-09-06 00:18:15 +0000386 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000387 monitor_printf(mon, " (halted)");
388 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000389 }
390}
391
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300392static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000393{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300394 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000395 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000396 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000397}
398
aliguori376253e2009-03-05 23:01:23 +0000399static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000400{
aliguori376253e2009-03-05 23:01:23 +0000401 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000402}
403
aliguori376253e2009-03-05 23:01:23 +0000404static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000405{
406 int i;
bellard7e2515e2004-08-01 21:52:19 +0000407 const char *str;
ths3b46e622007-09-17 08:09:54 +0000408
aliguoricde76ee2009-03-05 23:01:51 +0000409 if (!mon->rs)
410 return;
bellard7e2515e2004-08-01 21:52:19 +0000411 i = 0;
412 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000413 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000414 if (!str)
415 break;
aliguori376253e2009-03-05 23:01:23 +0000416 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000417 i++;
bellardaa455482004-04-04 13:07:25 +0000418 }
419}
420
j_mayer76a66252007-03-07 08:32:30 +0000421#if defined(TARGET_PPC)
422/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000423static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000424{
425 CPUState *env;
426
427 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000428 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000429}
430#endif
431
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300432static void do_quit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000433{
434 exit(0);
435}
436
aliguori376253e2009-03-05 23:01:23 +0000437static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000438{
439 if (bdrv_is_inserted(bs)) {
440 if (!force) {
441 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000442 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000443 return -1;
444 }
445 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000446 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000447 return -1;
448 }
449 }
450 bdrv_close(bs);
451 }
452 return 0;
453}
454
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300455static void do_eject(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000456{
457 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300458 int force = qdict_get_int(qdict, "force");
459 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000460
bellard9307c4c2004-04-04 12:57:25 +0000461 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000462 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000463 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000464 return;
465 }
aliguori376253e2009-03-05 23:01:23 +0000466 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000467}
468
aliguori376253e2009-03-05 23:01:23 +0000469static void do_change_block(Monitor *mon, const char *device,
470 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000471{
472 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000473 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000474
bellard9307c4c2004-04-04 12:57:25 +0000475 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000476 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000477 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000478 return;
479 }
aurel322ecea9b2008-06-18 22:10:01 +0000480 if (fmt) {
481 drv = bdrv_find_format(fmt);
482 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000483 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000484 return;
485 }
486 }
aliguori376253e2009-03-05 23:01:23 +0000487 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000488 return;
aurel322ecea9b2008-06-18 22:10:01 +0000489 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000490 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000491}
492
aliguori376253e2009-03-05 23:01:23 +0000493static void change_vnc_password_cb(Monitor *mon, const char *password,
494 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000495{
496 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000497 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000498
aliguori731b0362009-03-05 23:01:42 +0000499 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000500}
501
aliguori376253e2009-03-05 23:01:23 +0000502static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000503{
ths70848512007-08-25 01:37:05 +0000504 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000505 strcmp(target, "password") == 0) {
506 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000507 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000508 strncpy(password, arg, sizeof(password));
509 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000510 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000511 } else {
aliguori376253e2009-03-05 23:01:23 +0000512 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000513 }
ths70848512007-08-25 01:37:05 +0000514 } else {
aliguori28a76be2009-03-06 20:27:40 +0000515 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000516 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000517 }
thse25a5822007-08-25 01:36:20 +0000518}
519
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300520static void do_change(Monitor *mon, const QDict *qdict)
thse25a5822007-08-25 01:36:20 +0000521{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300522 const char *device = qdict_get_str(qdict, "device");
523 const char *target = qdict_get_str(qdict, "target");
524 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000525 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000526 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000527 } else {
aliguori28a76be2009-03-06 20:27:40 +0000528 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000529 }
530}
531
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300532static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000533{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300534 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000535}
536
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300537static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000538{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300539 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000540}
541
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300542static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000543{
544 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300545 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000546
bellard9307c4c2004-04-04 12:57:25 +0000547 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000548 mask = 0;
549 } else {
bellard9307c4c2004-04-04 12:57:25 +0000550 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000551 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000552 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000553 return;
554 }
555 }
556 cpu_set_log(mask);
557}
558
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300559static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000560{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300561 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000562 if (!option || !strcmp(option, "on")) {
563 singlestep = 1;
564 } else if (!strcmp(option, "off")) {
565 singlestep = 0;
566 } else {
567 monitor_printf(mon, "unexpected option %s\n", option);
568 }
569}
570
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300571static void do_stop(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000572{
573 vm_stop(EXCP_INTERRUPT);
574}
575
aliguoribb5fc202009-03-05 23:01:15 +0000576static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000577
aliguori376253e2009-03-05 23:01:23 +0000578struct bdrv_iterate_context {
579 Monitor *mon;
580 int err;
581};
aliguoric0f4ce72009-03-05 23:01:01 +0000582
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300583static void do_cont(Monitor *mon, const QDict *qdict)
aliguori376253e2009-03-05 23:01:23 +0000584{
585 struct bdrv_iterate_context context = { mon, 0 };
586
587 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000588 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000589 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000590 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000591}
592
aliguoribb5fc202009-03-05 23:01:15 +0000593static void bdrv_key_cb(void *opaque, int err)
594{
aliguori376253e2009-03-05 23:01:23 +0000595 Monitor *mon = opaque;
596
aliguoribb5fc202009-03-05 23:01:15 +0000597 /* another key was set successfully, retry to continue */
598 if (!err)
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300599 do_cont(mon, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000600}
601
602static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
603{
aliguori376253e2009-03-05 23:01:23 +0000604 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000605
aliguori376253e2009-03-05 23:01:23 +0000606 if (!context->err && bdrv_key_required(bs)) {
607 context->err = -EBUSY;
608 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
609 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000610 }
611}
612
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300613static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000614{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300615 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000616 if (!device)
617 device = "tcp::" DEFAULT_GDBSTUB_PORT;
618 if (gdbserver_start(device) < 0) {
619 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
620 device);
621 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000622 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000623 } else {
aliguori59030a82009-04-05 18:43:41 +0000624 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
625 device);
bellard8a7ddc32004-03-31 19:00:16 +0000626 }
627}
628
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300629static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100630{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300631 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100632 if (select_watchdog_action(action) == -1) {
633 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
634 }
635}
636
aliguori376253e2009-03-05 23:01:23 +0000637static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000638{
aliguori376253e2009-03-05 23:01:23 +0000639 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000640 switch(c) {
641 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000642 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000643 break;
644 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000645 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000646 break;
647 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000648 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000649 break;
650 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000651 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000652 break;
653 default:
654 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000655 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000656 } else {
aliguori376253e2009-03-05 23:01:23 +0000657 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000658 }
659 break;
660 }
aliguori376253e2009-03-05 23:01:23 +0000661 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000662}
663
aliguori376253e2009-03-05 23:01:23 +0000664static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000665 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000666{
bellard6a00d602005-11-21 23:25:50 +0000667 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000668 int nb_per_line, l, line_size, i, max_digits, len;
669 uint8_t buf[16];
670 uint64_t v;
671
672 if (format == 'i') {
673 int flags;
674 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000675 env = mon_get_cpu();
676 if (!env && !is_physical)
677 return;
bellard9307c4c2004-04-04 12:57:25 +0000678#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000679 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000680 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000681 } else if (wsize == 4) {
682 flags = 0;
683 } else {
bellard6a15fd12006-04-12 21:07:07 +0000684 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000685 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000686 if (env) {
687#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000688 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000689 (env->segs[R_CS].flags & DESC_L_MASK))
690 flags = 2;
691 else
692#endif
693 if (!(env->segs[R_CS].flags & DESC_B_MASK))
694 flags = 1;
695 }
bellard4c27ba22004-04-25 18:05:08 +0000696 }
697#endif
aliguori376253e2009-03-05 23:01:23 +0000698 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000699 return;
700 }
701
702 len = wsize * count;
703 if (wsize == 1)
704 line_size = 8;
705 else
706 line_size = 16;
707 nb_per_line = line_size / wsize;
708 max_digits = 0;
709
710 switch(format) {
711 case 'o':
712 max_digits = (wsize * 8 + 2) / 3;
713 break;
714 default:
715 case 'x':
716 max_digits = (wsize * 8) / 4;
717 break;
718 case 'u':
719 case 'd':
720 max_digits = (wsize * 8 * 10 + 32) / 33;
721 break;
722 case 'c':
723 wsize = 1;
724 break;
725 }
726
727 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000728 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000729 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000730 else
aliguori376253e2009-03-05 23:01:23 +0000731 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000732 l = len;
733 if (l > line_size)
734 l = line_size;
735 if (is_physical) {
736 cpu_physical_memory_rw(addr, buf, l, 0);
737 } else {
bellard6a00d602005-11-21 23:25:50 +0000738 env = mon_get_cpu();
739 if (!env)
740 break;
aliguoric8f79b62008-08-18 14:00:20 +0000741 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000742 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000743 break;
744 }
bellard9307c4c2004-04-04 12:57:25 +0000745 }
ths5fafdf22007-09-16 21:08:06 +0000746 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000747 while (i < l) {
748 switch(wsize) {
749 default:
750 case 1:
751 v = ldub_raw(buf + i);
752 break;
753 case 2:
754 v = lduw_raw(buf + i);
755 break;
756 case 4:
bellard92a31b12005-02-10 22:00:52 +0000757 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000758 break;
759 case 8:
760 v = ldq_raw(buf + i);
761 break;
762 }
aliguori376253e2009-03-05 23:01:23 +0000763 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000764 switch(format) {
765 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000766 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000767 break;
768 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000769 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000770 break;
771 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000772 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000773 break;
774 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000775 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000776 break;
777 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000778 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000779 break;
780 }
781 i += wsize;
782 }
aliguori376253e2009-03-05 23:01:23 +0000783 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000784 addr += l;
785 len -= l;
786 }
787}
788
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300789static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000790{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300791 int count = qdict_get_int(qdict, "count");
792 int format = qdict_get_int(qdict, "format");
793 int size = qdict_get_int(qdict, "size");
794 target_long addr = qdict_get_int(qdict, "addr");
795
aliguori376253e2009-03-05 23:01:23 +0000796 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000797}
798
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300799static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000800{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300801 int count = qdict_get_int(qdict, "count");
802 int format = qdict_get_int(qdict, "format");
803 int size = qdict_get_int(qdict, "size");
804 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
805
aliguori376253e2009-03-05 23:01:23 +0000806 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000807}
808
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300809static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000810{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300811 int format = qdict_get_int(qdict, "format");
812 target_phys_addr_t val = qdict_get_int(qdict, "val");
813
blueswir17743e582007-09-24 18:39:04 +0000814#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000815 switch(format) {
816 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000817 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000818 break;
819 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000820 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000821 break;
822 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000823 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000824 break;
825 default:
826 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000827 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000828 break;
829 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000830 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000831 break;
832 }
bellard92a31b12005-02-10 22:00:52 +0000833#else
834 switch(format) {
835 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000836 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000837 break;
838 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000839 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000840 break;
841 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000842 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000843 break;
844 default:
845 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000846 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000847 break;
848 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000849 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000850 break;
851 }
852#endif
aliguori376253e2009-03-05 23:01:23 +0000853 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000854}
855
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300856static void do_memory_save(Monitor *mon, const QDict *qdict)
bellardb371dc52007-01-03 15:20:39 +0000857{
858 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300859 uint32_t size = qdict_get_int(qdict, "size");
860 const char *filename = qdict_get_str(qdict, "filename");
861 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +0000862 uint32_t l;
863 CPUState *env;
864 uint8_t buf[1024];
865
866 env = mon_get_cpu();
867 if (!env)
868 return;
869
870 f = fopen(filename, "wb");
871 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000872 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000873 return;
874 }
875 while (size != 0) {
876 l = sizeof(buf);
877 if (l > size)
878 l = size;
879 cpu_memory_rw_debug(env, addr, buf, l, 0);
880 fwrite(buf, 1, l, f);
881 addr += l;
882 size -= l;
883 }
884 fclose(f);
885}
886
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300887static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
aurel32a8bdf7a2008-04-11 21:36:14 +0000888{
889 FILE *f;
890 uint32_t l;
891 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300892 uint32_t size = qdict_get_int(qdict, "size");
893 const char *filename = qdict_get_str(qdict, "filename");
894 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +0000895
896 f = fopen(filename, "wb");
897 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000898 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000899 return;
900 }
901 while (size != 0) {
902 l = sizeof(buf);
903 if (l > size)
904 l = size;
905 cpu_physical_memory_rw(addr, buf, l, 0);
906 fwrite(buf, 1, l, f);
907 fflush(f);
908 addr += l;
909 size -= l;
910 }
911 fclose(f);
912}
913
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300914static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000915{
916 uint32_t addr;
917 uint8_t buf[1];
918 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300919 uint32_t start = qdict_get_int(qdict, "start");
920 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000921
922 sum = 0;
923 for(addr = start; addr < (start + size); addr++) {
924 cpu_physical_memory_rw(addr, buf, 1, 0);
925 /* BSD sum algorithm ('sum' Unix command) */
926 sum = (sum >> 1) | (sum << 15);
927 sum += buf[0];
928 }
aliguori376253e2009-03-05 23:01:23 +0000929 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000930}
931
bellarda3a91a32004-06-04 11:06:21 +0000932typedef struct {
933 int keycode;
934 const char *name;
935} KeyDef;
936
937static const KeyDef key_defs[] = {
938 { 0x2a, "shift" },
939 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000940
bellarda3a91a32004-06-04 11:06:21 +0000941 { 0x38, "alt" },
942 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000943 { 0x64, "altgr" },
944 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000945 { 0x1d, "ctrl" },
946 { 0x9d, "ctrl_r" },
947
948 { 0xdd, "menu" },
949
950 { 0x01, "esc" },
951
952 { 0x02, "1" },
953 { 0x03, "2" },
954 { 0x04, "3" },
955 { 0x05, "4" },
956 { 0x06, "5" },
957 { 0x07, "6" },
958 { 0x08, "7" },
959 { 0x09, "8" },
960 { 0x0a, "9" },
961 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000962 { 0x0c, "minus" },
963 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000964 { 0x0e, "backspace" },
965
966 { 0x0f, "tab" },
967 { 0x10, "q" },
968 { 0x11, "w" },
969 { 0x12, "e" },
970 { 0x13, "r" },
971 { 0x14, "t" },
972 { 0x15, "y" },
973 { 0x16, "u" },
974 { 0x17, "i" },
975 { 0x18, "o" },
976 { 0x19, "p" },
977
978 { 0x1c, "ret" },
979
980 { 0x1e, "a" },
981 { 0x1f, "s" },
982 { 0x20, "d" },
983 { 0x21, "f" },
984 { 0x22, "g" },
985 { 0x23, "h" },
986 { 0x24, "j" },
987 { 0x25, "k" },
988 { 0x26, "l" },
989
990 { 0x2c, "z" },
991 { 0x2d, "x" },
992 { 0x2e, "c" },
993 { 0x2f, "v" },
994 { 0x30, "b" },
995 { 0x31, "n" },
996 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000997 { 0x33, "comma" },
998 { 0x34, "dot" },
999 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001000
balrog4d3b6f62008-02-10 16:33:14 +00001001 { 0x37, "asterisk" },
1002
bellarda3a91a32004-06-04 11:06:21 +00001003 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001004 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001005 { 0x3b, "f1" },
1006 { 0x3c, "f2" },
1007 { 0x3d, "f3" },
1008 { 0x3e, "f4" },
1009 { 0x3f, "f5" },
1010 { 0x40, "f6" },
1011 { 0x41, "f7" },
1012 { 0x42, "f8" },
1013 { 0x43, "f9" },
1014 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001015 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001016 { 0x46, "scroll_lock" },
1017
bellard64866c32006-05-07 18:03:31 +00001018 { 0xb5, "kp_divide" },
1019 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001020 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001021 { 0x4e, "kp_add" },
1022 { 0x9c, "kp_enter" },
1023 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001024 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001025
1026 { 0x52, "kp_0" },
1027 { 0x4f, "kp_1" },
1028 { 0x50, "kp_2" },
1029 { 0x51, "kp_3" },
1030 { 0x4b, "kp_4" },
1031 { 0x4c, "kp_5" },
1032 { 0x4d, "kp_6" },
1033 { 0x47, "kp_7" },
1034 { 0x48, "kp_8" },
1035 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001036
bellarda3a91a32004-06-04 11:06:21 +00001037 { 0x56, "<" },
1038
1039 { 0x57, "f11" },
1040 { 0x58, "f12" },
1041
1042 { 0xb7, "print" },
1043
1044 { 0xc7, "home" },
1045 { 0xc9, "pgup" },
1046 { 0xd1, "pgdn" },
1047 { 0xcf, "end" },
1048
1049 { 0xcb, "left" },
1050 { 0xc8, "up" },
1051 { 0xd0, "down" },
1052 { 0xcd, "right" },
1053
1054 { 0xd2, "insert" },
1055 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001056#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1057 { 0xf0, "stop" },
1058 { 0xf1, "again" },
1059 { 0xf2, "props" },
1060 { 0xf3, "undo" },
1061 { 0xf4, "front" },
1062 { 0xf5, "copy" },
1063 { 0xf6, "open" },
1064 { 0xf7, "paste" },
1065 { 0xf8, "find" },
1066 { 0xf9, "cut" },
1067 { 0xfa, "lf" },
1068 { 0xfb, "help" },
1069 { 0xfc, "meta_l" },
1070 { 0xfd, "meta_r" },
1071 { 0xfe, "compose" },
1072#endif
bellarda3a91a32004-06-04 11:06:21 +00001073 { 0, NULL },
1074};
1075
1076static int get_keycode(const char *key)
1077{
1078 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001079 char *endp;
1080 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001081
1082 for(p = key_defs; p->name != NULL; p++) {
1083 if (!strcmp(key, p->name))
1084 return p->keycode;
1085 }
bellard64866c32006-05-07 18:03:31 +00001086 if (strstart(key, "0x", NULL)) {
1087 ret = strtoul(key, &endp, 0);
1088 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1089 return ret;
1090 }
bellarda3a91a32004-06-04 11:06:21 +00001091 return -1;
1092}
1093
balrogc8256f92008-06-08 22:45:01 +00001094#define MAX_KEYCODES 16
1095static uint8_t keycodes[MAX_KEYCODES];
1096static int nb_pending_keycodes;
1097static QEMUTimer *key_timer;
1098
1099static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001100{
balrogc8256f92008-06-08 22:45:01 +00001101 int keycode;
1102
1103 while (nb_pending_keycodes > 0) {
1104 nb_pending_keycodes--;
1105 keycode = keycodes[nb_pending_keycodes];
1106 if (keycode & 0x80)
1107 kbd_put_keycode(0xe0);
1108 kbd_put_keycode(keycode | 0x80);
1109 }
1110}
1111
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001112static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001113{
balrog3401c0d2008-06-04 10:05:59 +00001114 char keyname_buf[16];
1115 char *separator;
1116 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001117 const char *string = qdict_get_str(qdict, "string");
1118 int has_hold_time = qdict_haskey(qdict, "hold_time");
1119 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001120
balrogc8256f92008-06-08 22:45:01 +00001121 if (nb_pending_keycodes > 0) {
1122 qemu_del_timer(key_timer);
1123 release_keys(NULL);
1124 }
1125 if (!has_hold_time)
1126 hold_time = 100;
1127 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001128 while (1) {
1129 separator = strchr(string, '-');
1130 keyname_len = separator ? separator - string : strlen(string);
1131 if (keyname_len > 0) {
1132 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1133 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001134 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001135 return;
bellarda3a91a32004-06-04 11:06:21 +00001136 }
balrogc8256f92008-06-08 22:45:01 +00001137 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001138 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001139 return;
1140 }
1141 keyname_buf[keyname_len] = 0;
1142 keycode = get_keycode(keyname_buf);
1143 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001144 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001145 return;
1146 }
balrogc8256f92008-06-08 22:45:01 +00001147 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001148 }
balrog3401c0d2008-06-04 10:05:59 +00001149 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001150 break;
balrog3401c0d2008-06-04 10:05:59 +00001151 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001152 }
balrogc8256f92008-06-08 22:45:01 +00001153 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001154 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001155 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001156 keycode = keycodes[i];
1157 if (keycode & 0x80)
1158 kbd_put_keycode(0xe0);
1159 kbd_put_keycode(keycode & 0x7f);
1160 }
balrogc8256f92008-06-08 22:45:01 +00001161 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001162 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1163 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001164}
1165
bellard13224a82006-07-14 22:03:35 +00001166static int mouse_button_state;
1167
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001168static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001169{
1170 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001171 const char *dx_str = qdict_get_str(qdict, "dx_str");
1172 const char *dy_str = qdict_get_str(qdict, "dy_str");
1173 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001174 dx = strtol(dx_str, NULL, 0);
1175 dy = strtol(dy_str, NULL, 0);
1176 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001177 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001178 dz = strtol(dz_str, NULL, 0);
1179 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1180}
1181
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001182static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001183{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001184 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001185 mouse_button_state = button_state;
1186 kbd_mouse_event(0, 0, 0, mouse_button_state);
1187}
1188
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001189static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001190{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001191 int size = qdict_get_int(qdict, "size");
1192 int addr = qdict_get_int(qdict, "addr");
1193 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001194 uint32_t val;
1195 int suffix;
1196
1197 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001198 int index = qdict_get_int(qdict, "index");
Isaku Yamahatad56dd6c2009-07-02 19:32:07 +09001199 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001200 addr++;
1201 }
1202 addr &= 0xffff;
1203
1204 switch(size) {
1205 default:
1206 case 1:
1207 val = cpu_inb(NULL, addr);
1208 suffix = 'b';
1209 break;
1210 case 2:
1211 val = cpu_inw(NULL, addr);
1212 suffix = 'w';
1213 break;
1214 case 4:
1215 val = cpu_inl(NULL, addr);
1216 suffix = 'l';
1217 break;
1218 }
aliguori376253e2009-03-05 23:01:23 +00001219 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1220 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001221}
bellarda3a91a32004-06-04 11:06:21 +00001222
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001223static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001224{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001225 int size = qdict_get_int(qdict, "size");
1226 int addr = qdict_get_int(qdict, "addr");
1227 int val = qdict_get_int(qdict, "val");
1228
Jan Kiszkaf1147842009-07-14 10:20:11 +02001229 addr &= IOPORTS_MASK;
1230
1231 switch (size) {
1232 default:
1233 case 1:
1234 cpu_outb(NULL, addr, val);
1235 break;
1236 case 2:
1237 cpu_outw(NULL, addr, val);
1238 break;
1239 case 4:
1240 cpu_outl(NULL, addr, val);
1241 break;
1242 }
1243}
1244
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001245static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001246{
1247 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001248 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001249
Jan Kiszka76e30d02009-07-02 00:19:02 +02001250 res = qemu_boot_set(bootdevice);
1251 if (res == 0) {
1252 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1253 } else if (res > 0) {
1254 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001255 } else {
aliguori376253e2009-03-05 23:01:23 +00001256 monitor_printf(mon, "no function defined to set boot device list for "
1257 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001258 }
1259}
1260
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001261static void do_system_reset(Monitor *mon, const QDict *qdict)
bellarde4f90822004-06-20 12:35:44 +00001262{
1263 qemu_system_reset_request();
1264}
1265
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001266static void do_system_powerdown(Monitor *mon, const QDict *qdict)
bellard34751872005-07-02 14:31:34 +00001267{
1268 qemu_system_powerdown_request();
1269}
1270
bellardb86bda52004-09-18 19:32:46 +00001271#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001272static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001273{
aliguori376253e2009-03-05 23:01:23 +00001274 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1275 addr,
1276 pte & mask,
1277 pte & PG_GLOBAL_MASK ? 'G' : '-',
1278 pte & PG_PSE_MASK ? 'P' : '-',
1279 pte & PG_DIRTY_MASK ? 'D' : '-',
1280 pte & PG_ACCESSED_MASK ? 'A' : '-',
1281 pte & PG_PCD_MASK ? 'C' : '-',
1282 pte & PG_PWT_MASK ? 'T' : '-',
1283 pte & PG_USER_MASK ? 'U' : '-',
1284 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001285}
1286
aliguori376253e2009-03-05 23:01:23 +00001287static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001288{
bellard6a00d602005-11-21 23:25:50 +00001289 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001290 int l1, l2;
1291 uint32_t pgd, pde, pte;
1292
bellard6a00d602005-11-21 23:25:50 +00001293 env = mon_get_cpu();
1294 if (!env)
1295 return;
1296
bellardb86bda52004-09-18 19:32:46 +00001297 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001298 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001299 return;
1300 }
1301 pgd = env->cr[3] & ~0xfff;
1302 for(l1 = 0; l1 < 1024; l1++) {
1303 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1304 pde = le32_to_cpu(pde);
1305 if (pde & PG_PRESENT_MASK) {
1306 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001307 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001308 } else {
1309 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001310 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001311 (uint8_t *)&pte, 4);
1312 pte = le32_to_cpu(pte);
1313 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001314 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001315 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001316 ~0xfff);
1317 }
1318 }
1319 }
1320 }
1321 }
1322}
1323
aliguori376253e2009-03-05 23:01:23 +00001324static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001325 uint32_t end, int prot)
1326{
bellard9746b152004-11-11 18:30:24 +00001327 int prot1;
1328 prot1 = *plast_prot;
1329 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001330 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001331 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1332 *pstart, end, end - *pstart,
1333 prot1 & PG_USER_MASK ? 'u' : '-',
1334 'r',
1335 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001336 }
1337 if (prot != 0)
1338 *pstart = end;
1339 else
1340 *pstart = -1;
1341 *plast_prot = prot;
1342 }
1343}
1344
aliguori376253e2009-03-05 23:01:23 +00001345static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001346{
bellard6a00d602005-11-21 23:25:50 +00001347 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001348 int l1, l2, prot, last_prot;
1349 uint32_t pgd, pde, pte, start, end;
1350
bellard6a00d602005-11-21 23:25:50 +00001351 env = mon_get_cpu();
1352 if (!env)
1353 return;
1354
bellardb86bda52004-09-18 19:32:46 +00001355 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001356 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001357 return;
1358 }
1359 pgd = env->cr[3] & ~0xfff;
1360 last_prot = 0;
1361 start = -1;
1362 for(l1 = 0; l1 < 1024; l1++) {
1363 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1364 pde = le32_to_cpu(pde);
1365 end = l1 << 22;
1366 if (pde & PG_PRESENT_MASK) {
1367 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1368 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001369 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001370 } else {
1371 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001372 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001373 (uint8_t *)&pte, 4);
1374 pte = le32_to_cpu(pte);
1375 end = (l1 << 22) + (l2 << 12);
1376 if (pte & PG_PRESENT_MASK) {
1377 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1378 } else {
1379 prot = 0;
1380 }
aliguori376253e2009-03-05 23:01:23 +00001381 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001382 }
1383 }
1384 } else {
1385 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001386 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001387 }
1388 }
1389}
1390#endif
1391
aurel327c664e22009-03-03 06:12:22 +00001392#if defined(TARGET_SH4)
1393
aliguori376253e2009-03-05 23:01:23 +00001394static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001395{
aliguori376253e2009-03-05 23:01:23 +00001396 monitor_printf(mon, " tlb%i:\t"
1397 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1398 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1399 "dirty=%hhu writethrough=%hhu\n",
1400 idx,
1401 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1402 tlb->v, tlb->sh, tlb->c, tlb->pr,
1403 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001404}
1405
aliguori376253e2009-03-05 23:01:23 +00001406static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001407{
1408 CPUState *env = mon_get_cpu();
1409 int i;
1410
aliguori376253e2009-03-05 23:01:23 +00001411 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001412 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001413 print_tlb (mon, i, &env->itlb[i]);
1414 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001415 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001416 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001417}
1418
1419#endif
1420
aliguori376253e2009-03-05 23:01:23 +00001421static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001422{
1423#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001424 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001425 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001426 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001427 else
aliguori376253e2009-03-05 23:01:23 +00001428 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001429#else
aliguori376253e2009-03-05 23:01:23 +00001430 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001431#endif
1432}
1433
aliguori030ea372009-04-21 22:30:47 +00001434static void do_info_numa(Monitor *mon)
1435{
aliguorib28b6232009-04-22 20:20:29 +00001436 int i;
aliguori030ea372009-04-21 22:30:47 +00001437 CPUState *env;
1438
1439 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1440 for (i = 0; i < nb_numa_nodes; i++) {
1441 monitor_printf(mon, "node %d cpus:", i);
1442 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1443 if (env->numa_node == i) {
1444 monitor_printf(mon, " %d", env->cpu_index);
1445 }
1446 }
1447 monitor_printf(mon, "\n");
1448 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1449 node_mem[i] >> 20);
1450 }
1451}
1452
bellard5f1ce942006-02-08 22:40:15 +00001453#ifdef CONFIG_PROFILER
1454
aliguori376253e2009-03-05 23:01:23 +00001455static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001456{
1457 int64_t total;
1458 total = qemu_time;
1459 if (total == 0)
1460 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001461 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1462 dev_time, dev_time / (double)ticks_per_sec);
1463 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1464 qemu_time, qemu_time / (double)ticks_per_sec);
bellard5f1ce942006-02-08 22:40:15 +00001465 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001466 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001467}
1468#else
aliguori376253e2009-03-05 23:01:23 +00001469static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001470{
aliguori376253e2009-03-05 23:01:23 +00001471 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001472}
1473#endif
1474
bellardec36b692006-07-16 18:57:03 +00001475/* Capture support */
1476static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1477
aliguori376253e2009-03-05 23:01:23 +00001478static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001479{
1480 int i;
1481 CaptureState *s;
1482
1483 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001484 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001485 s->ops.info (s->opaque);
1486 }
1487}
1488
Blue Swirl23130862009-06-06 08:22:04 +00001489#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001490static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001491{
1492 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001493 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001494 CaptureState *s;
1495
1496 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1497 if (i == n) {
1498 s->ops.destroy (s->opaque);
1499 LIST_REMOVE (s, entries);
1500 qemu_free (s);
1501 return;
1502 }
1503 }
1504}
1505
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001506static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001507{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001508 const char *path = qdict_get_str(qdict, "path");
1509 int has_freq = qdict_haskey(qdict, "freq");
1510 int freq = qdict_get_try_int(qdict, "freq", -1);
1511 int has_bits = qdict_haskey(qdict, "bits");
1512 int bits = qdict_get_try_int(qdict, "bits", -1);
1513 int has_channels = qdict_haskey(qdict, "nchannels");
1514 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001515 CaptureState *s;
1516
1517 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001518
1519 freq = has_freq ? freq : 44100;
1520 bits = has_bits ? bits : 16;
1521 nchannels = has_channels ? nchannels : 2;
1522
1523 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001524 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001525 qemu_free (s);
1526 }
1527 LIST_INSERT_HEAD (&capture_head, s, entries);
1528}
1529#endif
1530
aurel32dc1c0b72008-04-27 23:52:12 +00001531#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001532static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001533{
1534 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001535 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001536
1537 for (env = first_cpu; env != NULL; env = env->next_cpu)
1538 if (env->cpu_index == cpu_index) {
1539 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1540 break;
1541 }
1542}
1543#endif
1544
aliguori376253e2009-03-05 23:01:23 +00001545static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001546{
aurel321b530a62009-04-05 20:08:59 +00001547 if (vm_running) {
1548 if (singlestep) {
1549 monitor_printf(mon, "VM status: running (single step mode)\n");
1550 } else {
1551 monitor_printf(mon, "VM status: running\n");
1552 }
1553 } else
aliguori376253e2009-03-05 23:01:23 +00001554 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001555}
1556
1557
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001558static void do_balloon(Monitor *mon, const QDict *qdict)
aliguoridf751fa2008-12-04 20:19:35 +00001559{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001560 int value = qdict_get_int(qdict, "value");
aliguoridf751fa2008-12-04 20:19:35 +00001561 ram_addr_t target = value;
1562 qemu_balloon(target << 20);
1563}
1564
aliguori376253e2009-03-05 23:01:23 +00001565static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001566{
1567 ram_addr_t actual;
1568
1569 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001570 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001571 monitor_printf(mon, "Using KVM without synchronous MMU, "
1572 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001573 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001574 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001575 else
aliguori376253e2009-03-05 23:01:23 +00001576 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001577}
1578
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001579static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001580{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001581 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001582
aliguori76655d62009-03-06 20:27:37 +00001583 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001584 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001585 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001586 return acl;
1587}
aliguori76655d62009-03-06 20:27:37 +00001588
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001589static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001590{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001591 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001592 qemu_acl *acl = find_acl(mon, aclname);
1593 qemu_acl_entry *entry;
1594 int i = 0;
1595
1596 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001597 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001598 acl->defaultDeny ? "deny" : "allow");
aliguori28a76be2009-03-06 20:27:40 +00001599 TAILQ_FOREACH(entry, &acl->entries, next) {
1600 i++;
1601 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001602 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001603 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001604 }
1605}
1606
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001607static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001608{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001609 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001610 qemu_acl *acl = find_acl(mon, aclname);
1611
1612 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001613 qemu_acl_reset(acl);
1614 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001615 }
1616}
aliguori76655d62009-03-06 20:27:37 +00001617
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001618static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001619{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001620 const char *aclname = qdict_get_str(qdict, "aclname");
1621 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001622 qemu_acl *acl = find_acl(mon, aclname);
1623
1624 if (acl) {
1625 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001626 acl->defaultDeny = 0;
1627 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001628 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001629 acl->defaultDeny = 1;
1630 monitor_printf(mon, "acl: policy set to 'deny'\n");
1631 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001632 monitor_printf(mon, "acl: unknown policy '%s', "
1633 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001634 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001635 }
1636}
aliguori76655d62009-03-06 20:27:37 +00001637
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001638static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001639{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001640 const char *aclname = qdict_get_str(qdict, "aclname");
1641 const char *match = qdict_get_str(qdict, "match");
1642 const char *policy = qdict_get_str(qdict, "policy");
1643 int has_index = qdict_haskey(qdict, "index");
1644 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001645 qemu_acl *acl = find_acl(mon, aclname);
1646 int deny, ret;
1647
1648 if (acl) {
1649 if (strcmp(policy, "allow") == 0) {
1650 deny = 0;
1651 } else if (strcmp(policy, "deny") == 0) {
1652 deny = 1;
1653 } else {
1654 monitor_printf(mon, "acl: unknown policy '%s', "
1655 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001656 return;
1657 }
aliguori28a76be2009-03-06 20:27:40 +00001658 if (has_index)
1659 ret = qemu_acl_insert(acl, deny, match, index);
1660 else
1661 ret = qemu_acl_append(acl, deny, match);
1662 if (ret < 0)
1663 monitor_printf(mon, "acl: unable to add acl entry\n");
1664 else
1665 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001666 }
1667}
aliguori76655d62009-03-06 20:27:37 +00001668
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001669static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001670{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001671 const char *aclname = qdict_get_str(qdict, "aclname");
1672 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001673 qemu_acl *acl = find_acl(mon, aclname);
1674 int ret;
aliguori76655d62009-03-06 20:27:37 +00001675
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001676 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001677 ret = qemu_acl_remove(acl, match);
1678 if (ret < 0)
1679 monitor_printf(mon, "acl: no matching acl entry\n");
1680 else
1681 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001682 }
1683}
1684
Huang Ying79c4f6b2009-06-23 10:05:14 +08001685#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001686static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001687{
1688 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001689 int cpu_index = qdict_get_int(qdict, "cpu_index");
1690 int bank = qdict_get_int(qdict, "bank");
1691 uint64_t status = qdict_get_int(qdict, "status");
1692 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1693 uint64_t addr = qdict_get_int(qdict, "addr");
1694 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08001695
1696 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1697 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1698 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1699 break;
1700 }
1701}
1702#endif
1703
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001704static void do_getfd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001705{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001706 const char *fdname = qdict_get_str(qdict, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001707 mon_fd_t *monfd;
1708 int fd;
1709
1710 fd = qemu_chr_get_msgfd(mon->chr);
1711 if (fd == -1) {
1712 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1713 return;
1714 }
1715
1716 if (qemu_isdigit(fdname[0])) {
1717 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1718 return;
1719 }
1720
1721 fd = dup(fd);
1722 if (fd == -1) {
1723 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1724 strerror(errno));
1725 return;
1726 }
1727
1728 LIST_FOREACH(monfd, &mon->fds, next) {
1729 if (strcmp(monfd->name, fdname) != 0) {
1730 continue;
1731 }
1732
1733 close(monfd->fd);
1734 monfd->fd = fd;
1735 return;
1736 }
1737
1738 monfd = qemu_mallocz(sizeof(mon_fd_t));
1739 monfd->name = qemu_strdup(fdname);
1740 monfd->fd = fd;
1741
1742 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1743}
1744
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001745static void do_closefd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001746{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001747 const char *fdname = qdict_get_str(qdict, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001748 mon_fd_t *monfd;
1749
1750 LIST_FOREACH(monfd, &mon->fds, next) {
1751 if (strcmp(monfd->name, fdname) != 0) {
1752 continue;
1753 }
1754
1755 LIST_REMOVE(monfd, next);
1756 close(monfd->fd);
1757 qemu_free(monfd->name);
1758 qemu_free(monfd);
1759 return;
1760 }
1761
1762 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1763 fdname);
1764}
1765
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001766static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001767{
1768 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001769 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001770
1771 vm_stop(0);
1772
Juan Quintela05f24012009-08-20 19:42:22 +02001773 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001774 vm_start();
1775}
1776
Mark McLoughlin7768e042009-07-22 09:11:41 +01001777int monitor_get_fd(Monitor *mon, const char *fdname)
1778{
1779 mon_fd_t *monfd;
1780
1781 LIST_FOREACH(monfd, &mon->fds, next) {
1782 int fd;
1783
1784 if (strcmp(monfd->name, fdname) != 0) {
1785 continue;
1786 }
1787
1788 fd = monfd->fd;
1789
1790 /* caller takes ownership of fd */
1791 LIST_REMOVE(monfd, next);
1792 qemu_free(monfd->name);
1793 qemu_free(monfd);
1794
1795 return fd;
1796 }
1797
1798 return -1;
1799}
1800
aliguori376253e2009-03-05 23:01:23 +00001801static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001802#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001803 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001804};
1805
Blue Swirl23130862009-06-06 08:22:04 +00001806/* Please update qemu-monitor.hx when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001807static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001808 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001809 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001810 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001811 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001812 { "chardev", "", qemu_chr_info,
1813 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001814 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001815 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001816 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001817 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001818 { "registers", "", do_info_registers,
1819 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001820 { "cpus", "", do_info_cpus,
1821 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001822 { "history", "", do_info_history,
1823 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001824 { "irq", "", irq_info,
1825 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001826 { "pic", "", pic_info,
1827 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001828 { "pci", "", pci_info,
1829 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001830#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001831 { "tlb", "", tlb_info,
1832 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001833#endif
1834#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001835 { "mem", "", mem_info,
1836 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001837 { "hpet", "", do_info_hpet,
1838 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001839#endif
bellarde3db7222005-01-26 22:00:47 +00001840 { "jit", "", do_info_jit,
1841 "", "show dynamic compiler info", },
aliguori7ba1e612008-11-05 16:04:33 +00001842 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001843 "", "show KVM information", },
aliguori030ea372009-04-21 22:30:47 +00001844 { "numa", "", do_info_numa,
1845 "", "show NUMA information", },
bellarda594cfb2005-11-06 16:13:29 +00001846 { "usb", "", usb_info,
1847 "", "show guest USB devices", },
1848 { "usbhost", "", usb_host_info,
1849 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001850 { "profile", "", do_info_profile,
1851 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001852 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001853 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001854 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001855 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001856 { "status", "", do_info_status,
1857 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001858 { "pcmcia", "", pcmcia_info,
1859 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001860 { "mice", "", do_info_mice,
1861 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001862 { "vnc", "", do_info_vnc,
1863 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001864 { "name", "", do_info_name,
1865 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001866 { "uuid", "", do_info_uuid,
1867 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001868#if defined(TARGET_PPC)
1869 { "cpustats", "", do_info_cpu_stats,
1870 "", "show CPU statistics", },
1871#endif
blueswir131a60e22007-10-26 18:42:59 +00001872#if defined(CONFIG_SLIRP)
Jan Kiszka6dbe5532009-06-24 14:42:29 +02001873 { "usernet", "", do_info_usernet,
1874 "", "show user network stack connection states", },
blueswir131a60e22007-10-26 18:42:59 +00001875#endif
aliguori5bb79102008-10-13 03:12:02 +00001876 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001877 { "balloon", "", do_info_balloon,
1878 "", "show balloon information" },
Gerd Hoffmanncae49562009-06-05 15:53:17 +01001879 { "qtree", "", do_info_qtree,
1880 "", "show device tree" },
Gerd Hoffmannf6c64e02009-08-03 15:03:09 +02001881 { "qdm", "", do_info_qdm,
1882 "", "show qdev device model list" },
bellard9dc39cb2004-03-14 21:38:27 +00001883 { NULL, NULL, },
1884};
1885
bellard9307c4c2004-04-04 12:57:25 +00001886/*******************************************************************/
1887
1888static const char *pch;
1889static jmp_buf expr_env;
1890
bellard92a31b12005-02-10 22:00:52 +00001891#define MD_TLONG 0
1892#define MD_I32 1
1893
bellard9307c4c2004-04-04 12:57:25 +00001894typedef struct MonitorDef {
1895 const char *name;
1896 int offset;
blueswir18662d652008-10-02 18:32:44 +00001897 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001898 int type;
bellard9307c4c2004-04-04 12:57:25 +00001899} MonitorDef;
1900
bellard57206fd2004-04-25 18:54:52 +00001901#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001902static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001903{
bellard6a00d602005-11-21 23:25:50 +00001904 CPUState *env = mon_get_cpu();
1905 if (!env)
1906 return 0;
1907 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001908}
1909#endif
1910
bellarda541f292004-04-12 20:39:29 +00001911#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001912static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001913{
bellard6a00d602005-11-21 23:25:50 +00001914 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001915 unsigned int u;
1916 int i;
1917
bellard6a00d602005-11-21 23:25:50 +00001918 if (!env)
1919 return 0;
1920
bellarda541f292004-04-12 20:39:29 +00001921 u = 0;
1922 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00001923 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001924
1925 return u;
1926}
1927
blueswir18662d652008-10-02 18:32:44 +00001928static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001929{
bellard6a00d602005-11-21 23:25:50 +00001930 CPUState *env = mon_get_cpu();
1931 if (!env)
1932 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001933 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001934}
1935
blueswir18662d652008-10-02 18:32:44 +00001936static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001937{
bellard6a00d602005-11-21 23:25:50 +00001938 CPUState *env = mon_get_cpu();
1939 if (!env)
1940 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001941 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001942}
bellard9fddaa02004-05-21 12:59:32 +00001943
blueswir18662d652008-10-02 18:32:44 +00001944static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001945{
bellard6a00d602005-11-21 23:25:50 +00001946 CPUState *env = mon_get_cpu();
1947 if (!env)
1948 return 0;
1949 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001950}
1951
blueswir18662d652008-10-02 18:32:44 +00001952static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001953{
bellard6a00d602005-11-21 23:25:50 +00001954 CPUState *env = mon_get_cpu();
1955 if (!env)
1956 return 0;
1957 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001958}
1959
blueswir18662d652008-10-02 18:32:44 +00001960static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001961{
bellard6a00d602005-11-21 23:25:50 +00001962 CPUState *env = mon_get_cpu();
1963 if (!env)
1964 return 0;
1965 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001966}
bellarda541f292004-04-12 20:39:29 +00001967#endif
1968
bellarde95c8d52004-09-30 22:22:08 +00001969#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001970#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001971static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001972{
bellard6a00d602005-11-21 23:25:50 +00001973 CPUState *env = mon_get_cpu();
1974 if (!env)
1975 return 0;
1976 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001977}
bellard7b936c02005-10-30 17:05:13 +00001978#endif
bellarde95c8d52004-09-30 22:22:08 +00001979
blueswir18662d652008-10-02 18:32:44 +00001980static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001981{
bellard6a00d602005-11-21 23:25:50 +00001982 CPUState *env = mon_get_cpu();
1983 if (!env)
1984 return 0;
1985 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001986}
1987#endif
1988
blueswir18662d652008-10-02 18:32:44 +00001989static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001990#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001991
1992#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001993 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001994 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001995 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001996
bellard9307c4c2004-04-04 12:57:25 +00001997 { "eax", offsetof(CPUState, regs[0]) },
1998 { "ecx", offsetof(CPUState, regs[1]) },
1999 { "edx", offsetof(CPUState, regs[2]) },
2000 { "ebx", offsetof(CPUState, regs[3]) },
2001 { "esp|sp", offsetof(CPUState, regs[4]) },
2002 { "ebp|fp", offsetof(CPUState, regs[5]) },
2003 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002004 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002005#ifdef TARGET_X86_64
2006 { "r8", offsetof(CPUState, regs[8]) },
2007 { "r9", offsetof(CPUState, regs[9]) },
2008 { "r10", offsetof(CPUState, regs[10]) },
2009 { "r11", offsetof(CPUState, regs[11]) },
2010 { "r12", offsetof(CPUState, regs[12]) },
2011 { "r13", offsetof(CPUState, regs[13]) },
2012 { "r14", offsetof(CPUState, regs[14]) },
2013 { "r15", offsetof(CPUState, regs[15]) },
2014#endif
bellard9307c4c2004-04-04 12:57:25 +00002015 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002016 { "eip", offsetof(CPUState, eip) },
2017 SEG("cs", R_CS)
2018 SEG("ds", R_DS)
2019 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002020 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002021 SEG("fs", R_FS)
2022 SEG("gs", R_GS)
2023 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002024#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002025 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002026 { "r0", offsetof(CPUState, gpr[0]) },
2027 { "r1", offsetof(CPUState, gpr[1]) },
2028 { "r2", offsetof(CPUState, gpr[2]) },
2029 { "r3", offsetof(CPUState, gpr[3]) },
2030 { "r4", offsetof(CPUState, gpr[4]) },
2031 { "r5", offsetof(CPUState, gpr[5]) },
2032 { "r6", offsetof(CPUState, gpr[6]) },
2033 { "r7", offsetof(CPUState, gpr[7]) },
2034 { "r8", offsetof(CPUState, gpr[8]) },
2035 { "r9", offsetof(CPUState, gpr[9]) },
2036 { "r10", offsetof(CPUState, gpr[10]) },
2037 { "r11", offsetof(CPUState, gpr[11]) },
2038 { "r12", offsetof(CPUState, gpr[12]) },
2039 { "r13", offsetof(CPUState, gpr[13]) },
2040 { "r14", offsetof(CPUState, gpr[14]) },
2041 { "r15", offsetof(CPUState, gpr[15]) },
2042 { "r16", offsetof(CPUState, gpr[16]) },
2043 { "r17", offsetof(CPUState, gpr[17]) },
2044 { "r18", offsetof(CPUState, gpr[18]) },
2045 { "r19", offsetof(CPUState, gpr[19]) },
2046 { "r20", offsetof(CPUState, gpr[20]) },
2047 { "r21", offsetof(CPUState, gpr[21]) },
2048 { "r22", offsetof(CPUState, gpr[22]) },
2049 { "r23", offsetof(CPUState, gpr[23]) },
2050 { "r24", offsetof(CPUState, gpr[24]) },
2051 { "r25", offsetof(CPUState, gpr[25]) },
2052 { "r26", offsetof(CPUState, gpr[26]) },
2053 { "r27", offsetof(CPUState, gpr[27]) },
2054 { "r28", offsetof(CPUState, gpr[28]) },
2055 { "r29", offsetof(CPUState, gpr[29]) },
2056 { "r30", offsetof(CPUState, gpr[30]) },
2057 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002058 /* Floating point registers */
2059 { "f0", offsetof(CPUState, fpr[0]) },
2060 { "f1", offsetof(CPUState, fpr[1]) },
2061 { "f2", offsetof(CPUState, fpr[2]) },
2062 { "f3", offsetof(CPUState, fpr[3]) },
2063 { "f4", offsetof(CPUState, fpr[4]) },
2064 { "f5", offsetof(CPUState, fpr[5]) },
2065 { "f6", offsetof(CPUState, fpr[6]) },
2066 { "f7", offsetof(CPUState, fpr[7]) },
2067 { "f8", offsetof(CPUState, fpr[8]) },
2068 { "f9", offsetof(CPUState, fpr[9]) },
2069 { "f10", offsetof(CPUState, fpr[10]) },
2070 { "f11", offsetof(CPUState, fpr[11]) },
2071 { "f12", offsetof(CPUState, fpr[12]) },
2072 { "f13", offsetof(CPUState, fpr[13]) },
2073 { "f14", offsetof(CPUState, fpr[14]) },
2074 { "f15", offsetof(CPUState, fpr[15]) },
2075 { "f16", offsetof(CPUState, fpr[16]) },
2076 { "f17", offsetof(CPUState, fpr[17]) },
2077 { "f18", offsetof(CPUState, fpr[18]) },
2078 { "f19", offsetof(CPUState, fpr[19]) },
2079 { "f20", offsetof(CPUState, fpr[20]) },
2080 { "f21", offsetof(CPUState, fpr[21]) },
2081 { "f22", offsetof(CPUState, fpr[22]) },
2082 { "f23", offsetof(CPUState, fpr[23]) },
2083 { "f24", offsetof(CPUState, fpr[24]) },
2084 { "f25", offsetof(CPUState, fpr[25]) },
2085 { "f26", offsetof(CPUState, fpr[26]) },
2086 { "f27", offsetof(CPUState, fpr[27]) },
2087 { "f28", offsetof(CPUState, fpr[28]) },
2088 { "f29", offsetof(CPUState, fpr[29]) },
2089 { "f30", offsetof(CPUState, fpr[30]) },
2090 { "f31", offsetof(CPUState, fpr[31]) },
2091 { "fpscr", offsetof(CPUState, fpscr) },
2092 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002093 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002094 { "lr", offsetof(CPUState, lr) },
2095 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002096 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002097 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002098 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002099 { "msr", 0, &monitor_get_msr, },
2100 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002101 { "tbu", 0, &monitor_get_tbu, },
2102 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002103#if defined(TARGET_PPC64)
2104 /* Address space register */
2105 { "asr", offsetof(CPUState, asr) },
2106#endif
2107 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002108 { "sdr1", offsetof(CPUState, sdr1) },
2109 { "sr0", offsetof(CPUState, sr[0]) },
2110 { "sr1", offsetof(CPUState, sr[1]) },
2111 { "sr2", offsetof(CPUState, sr[2]) },
2112 { "sr3", offsetof(CPUState, sr[3]) },
2113 { "sr4", offsetof(CPUState, sr[4]) },
2114 { "sr5", offsetof(CPUState, sr[5]) },
2115 { "sr6", offsetof(CPUState, sr[6]) },
2116 { "sr7", offsetof(CPUState, sr[7]) },
2117 { "sr8", offsetof(CPUState, sr[8]) },
2118 { "sr9", offsetof(CPUState, sr[9]) },
2119 { "sr10", offsetof(CPUState, sr[10]) },
2120 { "sr11", offsetof(CPUState, sr[11]) },
2121 { "sr12", offsetof(CPUState, sr[12]) },
2122 { "sr13", offsetof(CPUState, sr[13]) },
2123 { "sr14", offsetof(CPUState, sr[14]) },
2124 { "sr15", offsetof(CPUState, sr[15]) },
2125 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002126#elif defined(TARGET_SPARC)
2127 { "g0", offsetof(CPUState, gregs[0]) },
2128 { "g1", offsetof(CPUState, gregs[1]) },
2129 { "g2", offsetof(CPUState, gregs[2]) },
2130 { "g3", offsetof(CPUState, gregs[3]) },
2131 { "g4", offsetof(CPUState, gregs[4]) },
2132 { "g5", offsetof(CPUState, gregs[5]) },
2133 { "g6", offsetof(CPUState, gregs[6]) },
2134 { "g7", offsetof(CPUState, gregs[7]) },
2135 { "o0", 0, monitor_get_reg },
2136 { "o1", 1, monitor_get_reg },
2137 { "o2", 2, monitor_get_reg },
2138 { "o3", 3, monitor_get_reg },
2139 { "o4", 4, monitor_get_reg },
2140 { "o5", 5, monitor_get_reg },
2141 { "o6", 6, monitor_get_reg },
2142 { "o7", 7, monitor_get_reg },
2143 { "l0", 8, monitor_get_reg },
2144 { "l1", 9, monitor_get_reg },
2145 { "l2", 10, monitor_get_reg },
2146 { "l3", 11, monitor_get_reg },
2147 { "l4", 12, monitor_get_reg },
2148 { "l5", 13, monitor_get_reg },
2149 { "l6", 14, monitor_get_reg },
2150 { "l7", 15, monitor_get_reg },
2151 { "i0", 16, monitor_get_reg },
2152 { "i1", 17, monitor_get_reg },
2153 { "i2", 18, monitor_get_reg },
2154 { "i3", 19, monitor_get_reg },
2155 { "i4", 20, monitor_get_reg },
2156 { "i5", 21, monitor_get_reg },
2157 { "i6", 22, monitor_get_reg },
2158 { "i7", 23, monitor_get_reg },
2159 { "pc", offsetof(CPUState, pc) },
2160 { "npc", offsetof(CPUState, npc) },
2161 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002162#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002163 { "psr", 0, &monitor_get_psr, },
2164 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002165#endif
bellarde95c8d52004-09-30 22:22:08 +00002166 { "tbr", offsetof(CPUState, tbr) },
2167 { "fsr", offsetof(CPUState, fsr) },
2168 { "f0", offsetof(CPUState, fpr[0]) },
2169 { "f1", offsetof(CPUState, fpr[1]) },
2170 { "f2", offsetof(CPUState, fpr[2]) },
2171 { "f3", offsetof(CPUState, fpr[3]) },
2172 { "f4", offsetof(CPUState, fpr[4]) },
2173 { "f5", offsetof(CPUState, fpr[5]) },
2174 { "f6", offsetof(CPUState, fpr[6]) },
2175 { "f7", offsetof(CPUState, fpr[7]) },
2176 { "f8", offsetof(CPUState, fpr[8]) },
2177 { "f9", offsetof(CPUState, fpr[9]) },
2178 { "f10", offsetof(CPUState, fpr[10]) },
2179 { "f11", offsetof(CPUState, fpr[11]) },
2180 { "f12", offsetof(CPUState, fpr[12]) },
2181 { "f13", offsetof(CPUState, fpr[13]) },
2182 { "f14", offsetof(CPUState, fpr[14]) },
2183 { "f15", offsetof(CPUState, fpr[15]) },
2184 { "f16", offsetof(CPUState, fpr[16]) },
2185 { "f17", offsetof(CPUState, fpr[17]) },
2186 { "f18", offsetof(CPUState, fpr[18]) },
2187 { "f19", offsetof(CPUState, fpr[19]) },
2188 { "f20", offsetof(CPUState, fpr[20]) },
2189 { "f21", offsetof(CPUState, fpr[21]) },
2190 { "f22", offsetof(CPUState, fpr[22]) },
2191 { "f23", offsetof(CPUState, fpr[23]) },
2192 { "f24", offsetof(CPUState, fpr[24]) },
2193 { "f25", offsetof(CPUState, fpr[25]) },
2194 { "f26", offsetof(CPUState, fpr[26]) },
2195 { "f27", offsetof(CPUState, fpr[27]) },
2196 { "f28", offsetof(CPUState, fpr[28]) },
2197 { "f29", offsetof(CPUState, fpr[29]) },
2198 { "f30", offsetof(CPUState, fpr[30]) },
2199 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002200#ifdef TARGET_SPARC64
2201 { "f32", offsetof(CPUState, fpr[32]) },
2202 { "f34", offsetof(CPUState, fpr[34]) },
2203 { "f36", offsetof(CPUState, fpr[36]) },
2204 { "f38", offsetof(CPUState, fpr[38]) },
2205 { "f40", offsetof(CPUState, fpr[40]) },
2206 { "f42", offsetof(CPUState, fpr[42]) },
2207 { "f44", offsetof(CPUState, fpr[44]) },
2208 { "f46", offsetof(CPUState, fpr[46]) },
2209 { "f48", offsetof(CPUState, fpr[48]) },
2210 { "f50", offsetof(CPUState, fpr[50]) },
2211 { "f52", offsetof(CPUState, fpr[52]) },
2212 { "f54", offsetof(CPUState, fpr[54]) },
2213 { "f56", offsetof(CPUState, fpr[56]) },
2214 { "f58", offsetof(CPUState, fpr[58]) },
2215 { "f60", offsetof(CPUState, fpr[60]) },
2216 { "f62", offsetof(CPUState, fpr[62]) },
2217 { "asi", offsetof(CPUState, asi) },
2218 { "pstate", offsetof(CPUState, pstate) },
2219 { "cansave", offsetof(CPUState, cansave) },
2220 { "canrestore", offsetof(CPUState, canrestore) },
2221 { "otherwin", offsetof(CPUState, otherwin) },
2222 { "wstate", offsetof(CPUState, wstate) },
2223 { "cleanwin", offsetof(CPUState, cleanwin) },
2224 { "fprs", offsetof(CPUState, fprs) },
2225#endif
bellard9307c4c2004-04-04 12:57:25 +00002226#endif
2227 { NULL },
2228};
2229
aliguori376253e2009-03-05 23:01:23 +00002230static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002231{
aliguori376253e2009-03-05 23:01:23 +00002232 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002233 longjmp(expr_env, 1);
2234}
2235
bellard6a00d602005-11-21 23:25:50 +00002236/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002237static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002238{
blueswir18662d652008-10-02 18:32:44 +00002239 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002240 void *ptr;
2241
bellard9307c4c2004-04-04 12:57:25 +00002242 for(md = monitor_defs; md->name != NULL; md++) {
2243 if (compare_cmd(name, md->name)) {
2244 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002245 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002246 } else {
bellard6a00d602005-11-21 23:25:50 +00002247 CPUState *env = mon_get_cpu();
2248 if (!env)
2249 return -2;
2250 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002251 switch(md->type) {
2252 case MD_I32:
2253 *pval = *(int32_t *)ptr;
2254 break;
2255 case MD_TLONG:
2256 *pval = *(target_long *)ptr;
2257 break;
2258 default:
2259 *pval = 0;
2260 break;
2261 }
bellard9307c4c2004-04-04 12:57:25 +00002262 }
2263 return 0;
2264 }
2265 }
2266 return -1;
2267}
2268
2269static void next(void)
2270{
Blue Swirl660f11b2009-07-31 21:16:51 +00002271 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002272 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002273 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002274 pch++;
2275 }
2276}
2277
aliguori376253e2009-03-05 23:01:23 +00002278static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002279
aliguori376253e2009-03-05 23:01:23 +00002280static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002281{
blueswir1c2efc952007-09-25 17:28:42 +00002282 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002283 char *p;
bellard6a00d602005-11-21 23:25:50 +00002284 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002285
2286 switch(*pch) {
2287 case '+':
2288 next();
aliguori376253e2009-03-05 23:01:23 +00002289 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002290 break;
2291 case '-':
2292 next();
aliguori376253e2009-03-05 23:01:23 +00002293 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002294 break;
2295 case '~':
2296 next();
aliguori376253e2009-03-05 23:01:23 +00002297 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002298 break;
2299 case '(':
2300 next();
aliguori376253e2009-03-05 23:01:23 +00002301 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002302 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002303 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002304 }
2305 next();
2306 break;
bellard81d09122004-07-14 17:21:37 +00002307 case '\'':
2308 pch++;
2309 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002310 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002311 n = *pch;
2312 pch++;
2313 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002314 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002315 next();
2316 break;
bellard9307c4c2004-04-04 12:57:25 +00002317 case '$':
2318 {
2319 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002320 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002321
bellard9307c4c2004-04-04 12:57:25 +00002322 pch++;
2323 q = buf;
2324 while ((*pch >= 'a' && *pch <= 'z') ||
2325 (*pch >= 'A' && *pch <= 'Z') ||
2326 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002327 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002328 if ((q - buf) < sizeof(buf) - 1)
2329 *q++ = *pch;
2330 pch++;
2331 }
blueswir1cd390082008-11-16 13:53:32 +00002332 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002333 pch++;
2334 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002335 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002336 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002337 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002338 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002339 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002340 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002341 }
2342 break;
2343 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002344 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002345 n = 0;
2346 break;
2347 default:
blueswir17743e582007-09-24 18:39:04 +00002348#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002349 n = strtoull(pch, &p, 0);
2350#else
bellard9307c4c2004-04-04 12:57:25 +00002351 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002352#endif
bellard9307c4c2004-04-04 12:57:25 +00002353 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002354 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002355 }
2356 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002357 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002358 pch++;
2359 break;
2360 }
2361 return n;
2362}
2363
2364
aliguori376253e2009-03-05 23:01:23 +00002365static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002366{
blueswir1c2efc952007-09-25 17:28:42 +00002367 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002368 int op;
ths3b46e622007-09-17 08:09:54 +00002369
aliguori376253e2009-03-05 23:01:23 +00002370 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002371 for(;;) {
2372 op = *pch;
2373 if (op != '*' && op != '/' && op != '%')
2374 break;
2375 next();
aliguori376253e2009-03-05 23:01:23 +00002376 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002377 switch(op) {
2378 default:
2379 case '*':
2380 val *= val2;
2381 break;
2382 case '/':
2383 case '%':
ths5fafdf22007-09-16 21:08:06 +00002384 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002385 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002386 if (op == '/')
2387 val /= val2;
2388 else
2389 val %= val2;
2390 break;
2391 }
2392 }
2393 return val;
2394}
2395
aliguori376253e2009-03-05 23:01:23 +00002396static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002397{
blueswir1c2efc952007-09-25 17:28:42 +00002398 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002399 int op;
bellard9307c4c2004-04-04 12:57:25 +00002400
aliguori376253e2009-03-05 23:01:23 +00002401 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002402 for(;;) {
2403 op = *pch;
2404 if (op != '&' && op != '|' && op != '^')
2405 break;
2406 next();
aliguori376253e2009-03-05 23:01:23 +00002407 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002408 switch(op) {
2409 default:
2410 case '&':
2411 val &= val2;
2412 break;
2413 case '|':
2414 val |= val2;
2415 break;
2416 case '^':
2417 val ^= val2;
2418 break;
2419 }
2420 }
2421 return val;
2422}
2423
aliguori376253e2009-03-05 23:01:23 +00002424static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002425{
blueswir1c2efc952007-09-25 17:28:42 +00002426 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002427 int op;
bellard9307c4c2004-04-04 12:57:25 +00002428
aliguori376253e2009-03-05 23:01:23 +00002429 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002430 for(;;) {
2431 op = *pch;
2432 if (op != '+' && op != '-')
2433 break;
2434 next();
aliguori376253e2009-03-05 23:01:23 +00002435 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002436 if (op == '+')
2437 val += val2;
2438 else
2439 val -= val2;
2440 }
2441 return val;
2442}
2443
aliguori376253e2009-03-05 23:01:23 +00002444static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002445{
2446 pch = *pp;
2447 if (setjmp(expr_env)) {
2448 *pp = pch;
2449 return -1;
2450 }
blueswir1cd390082008-11-16 13:53:32 +00002451 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002452 pch++;
aliguori376253e2009-03-05 23:01:23 +00002453 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002454 *pp = pch;
2455 return 0;
2456}
2457
2458static int get_str(char *buf, int buf_size, const char **pp)
2459{
2460 const char *p;
2461 char *q;
2462 int c;
2463
bellard81d09122004-07-14 17:21:37 +00002464 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002465 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002466 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002467 p++;
2468 if (*p == '\0') {
2469 fail:
bellard81d09122004-07-14 17:21:37 +00002470 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002471 *pp = p;
2472 return -1;
2473 }
bellard9307c4c2004-04-04 12:57:25 +00002474 if (*p == '\"') {
2475 p++;
2476 while (*p != '\0' && *p != '\"') {
2477 if (*p == '\\') {
2478 p++;
2479 c = *p++;
2480 switch(c) {
2481 case 'n':
2482 c = '\n';
2483 break;
2484 case 'r':
2485 c = '\r';
2486 break;
2487 case '\\':
2488 case '\'':
2489 case '\"':
2490 break;
2491 default:
2492 qemu_printf("unsupported escape code: '\\%c'\n", c);
2493 goto fail;
2494 }
2495 if ((q - buf) < buf_size - 1) {
2496 *q++ = c;
2497 }
2498 } else {
2499 if ((q - buf) < buf_size - 1) {
2500 *q++ = *p;
2501 }
2502 p++;
2503 }
2504 }
2505 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002506 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002507 goto fail;
2508 }
2509 p++;
2510 } else {
blueswir1cd390082008-11-16 13:53:32 +00002511 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002512 if ((q - buf) < buf_size - 1) {
2513 *q++ = *p;
2514 }
2515 p++;
2516 }
bellard9307c4c2004-04-04 12:57:25 +00002517 }
bellard81d09122004-07-14 17:21:37 +00002518 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002519 *pp = p;
2520 return 0;
2521}
2522
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002523/*
2524 * Store the command-name in cmdname, and return a pointer to
2525 * the remaining of the command string.
2526 */
2527static const char *get_command_name(const char *cmdline,
2528 char *cmdname, size_t nlen)
2529{
2530 size_t len;
2531 const char *p, *pstart;
2532
2533 p = cmdline;
2534 while (qemu_isspace(*p))
2535 p++;
2536 if (*p == '\0')
2537 return NULL;
2538 pstart = p;
2539 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2540 p++;
2541 len = p - pstart;
2542 if (len > nlen - 1)
2543 len = nlen - 1;
2544 memcpy(cmdname, pstart, len);
2545 cmdname[len] = '\0';
2546 return p;
2547}
2548
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002549/**
2550 * Read key of 'type' into 'key' and return the current
2551 * 'type' pointer.
2552 */
2553static char *key_get_info(const char *type, char **key)
2554{
2555 size_t len;
2556 char *p, *str;
2557
2558 if (*type == ',')
2559 type++;
2560
2561 p = strchr(type, ':');
2562 if (!p) {
2563 *key = NULL;
2564 return NULL;
2565 }
2566 len = p - type;
2567
2568 str = qemu_malloc(len + 1);
2569 memcpy(str, type, len);
2570 str[len] = '\0';
2571
2572 *key = str;
2573 return ++p;
2574}
2575
bellard9307c4c2004-04-04 12:57:25 +00002576static int default_fmt_format = 'x';
2577static int default_fmt_size = 4;
2578
2579#define MAX_ARGS 16
2580
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002581static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2582 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002583 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00002584{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002585 const char *p, *typestr;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002586 int c, nb_args, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002587 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002588 char cmdname[256];
2589 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002590 char *key;
bellard9307c4c2004-04-04 12:57:25 +00002591 void *args[MAX_ARGS];
bellard9dc39cb2004-03-14 21:38:27 +00002592
2593#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002594 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002595#endif
ths3b46e622007-09-17 08:09:54 +00002596
bellard9307c4c2004-04-04 12:57:25 +00002597 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002598 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2599 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002600 return NULL;
ths3b46e622007-09-17 08:09:54 +00002601
bellard9307c4c2004-04-04 12:57:25 +00002602 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002603 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002604 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002605 break;
bellard9dc39cb2004-03-14 21:38:27 +00002606 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002607
2608 if (cmd->name == NULL) {
2609 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002610 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002611 }
bellard9307c4c2004-04-04 12:57:25 +00002612
bellard9307c4c2004-04-04 12:57:25 +00002613 /* parse the parameters */
2614 typestr = cmd->args_type;
2615 nb_args = 0;
2616 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002617 typestr = key_get_info(typestr, &key);
2618 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002619 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002620 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002621 typestr++;
2622 switch(c) {
2623 case 'F':
bellard81d09122004-07-14 17:21:37 +00002624 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002625 case 's':
2626 {
2627 int ret;
2628 char *str;
ths3b46e622007-09-17 08:09:54 +00002629
blueswir1cd390082008-11-16 13:53:32 +00002630 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002631 p++;
2632 if (*typestr == '?') {
2633 typestr++;
2634 if (*p == '\0') {
2635 /* no optional string: NULL argument */
2636 str = NULL;
2637 goto add_str;
2638 }
2639 }
2640 ret = get_str(buf, sizeof(buf), &p);
2641 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002642 switch(c) {
2643 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002644 monitor_printf(mon, "%s: filename expected\n",
2645 cmdname);
bellard81d09122004-07-14 17:21:37 +00002646 break;
2647 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002648 monitor_printf(mon, "%s: block device name expected\n",
2649 cmdname);
bellard81d09122004-07-14 17:21:37 +00002650 break;
2651 default:
aliguori376253e2009-03-05 23:01:23 +00002652 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002653 break;
2654 }
bellard9307c4c2004-04-04 12:57:25 +00002655 goto fail;
2656 }
2657 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002658 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002659 add_str:
2660 if (nb_args >= MAX_ARGS) {
2661 error_args:
aliguori376253e2009-03-05 23:01:23 +00002662 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002663 goto fail;
2664 }
2665 args[nb_args++] = str;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002666 if (str)
2667 qdict_put(qdict, key, qstring_from_str(str));
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03002668 qemu_free(str);
bellard9307c4c2004-04-04 12:57:25 +00002669 }
2670 break;
2671 case '/':
2672 {
2673 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002674
blueswir1cd390082008-11-16 13:53:32 +00002675 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002676 p++;
2677 if (*p == '/') {
2678 /* format found */
2679 p++;
2680 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002681 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002682 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002683 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002684 count = count * 10 + (*p - '0');
2685 p++;
2686 }
2687 }
2688 size = -1;
2689 format = -1;
2690 for(;;) {
2691 switch(*p) {
2692 case 'o':
2693 case 'd':
2694 case 'u':
2695 case 'x':
2696 case 'i':
2697 case 'c':
2698 format = *p++;
2699 break;
2700 case 'b':
2701 size = 1;
2702 p++;
2703 break;
2704 case 'h':
2705 size = 2;
2706 p++;
2707 break;
2708 case 'w':
2709 size = 4;
2710 p++;
2711 break;
2712 case 'g':
2713 case 'L':
2714 size = 8;
2715 p++;
2716 break;
2717 default:
2718 goto next;
2719 }
2720 }
2721 next:
blueswir1cd390082008-11-16 13:53:32 +00002722 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002723 monitor_printf(mon, "invalid char in format: '%c'\n",
2724 *p);
bellard9307c4c2004-04-04 12:57:25 +00002725 goto fail;
2726 }
bellard9307c4c2004-04-04 12:57:25 +00002727 if (format < 0)
2728 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002729 if (format != 'i') {
2730 /* for 'i', not specifying a size gives -1 as size */
2731 if (size < 0)
2732 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002733 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002734 }
bellard9307c4c2004-04-04 12:57:25 +00002735 default_fmt_format = format;
2736 } else {
2737 count = 1;
2738 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002739 if (format != 'i') {
2740 size = default_fmt_size;
2741 } else {
2742 size = -1;
2743 }
bellard9307c4c2004-04-04 12:57:25 +00002744 }
2745 if (nb_args + 3 > MAX_ARGS)
2746 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002747 args[nb_args++] = (void*)(long)count;
2748 args[nb_args++] = (void*)(long)format;
2749 args[nb_args++] = (void*)(long)size;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002750 qdict_put(qdict, "count", qint_from_int(count));
2751 qdict_put(qdict, "format", qint_from_int(format));
2752 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002753 }
2754 break;
2755 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002756 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002757 {
blueswir1c2efc952007-09-25 17:28:42 +00002758 int64_t val;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002759 int dict_add = 1;
blueswir17743e582007-09-24 18:39:04 +00002760
blueswir1cd390082008-11-16 13:53:32 +00002761 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002762 p++;
bellard34405572004-06-08 00:55:58 +00002763 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002764 if (*typestr == '?') {
2765 if (*p == '\0')
2766 has_arg = 0;
2767 else
2768 has_arg = 1;
2769 } else {
2770 if (*p == '.') {
2771 p++;
blueswir1cd390082008-11-16 13:53:32 +00002772 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002773 p++;
2774 has_arg = 1;
2775 } else {
2776 has_arg = 0;
2777 }
2778 }
bellard13224a82006-07-14 22:03:35 +00002779 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002780 if (nb_args >= MAX_ARGS)
2781 goto error_args;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002782 dict_add = has_arg;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002783 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002784 if (!has_arg) {
2785 if (nb_args >= MAX_ARGS)
2786 goto error_args;
2787 val = -1;
2788 goto add_num;
2789 }
2790 }
aliguori376253e2009-03-05 23:01:23 +00002791 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002792 goto fail;
2793 add_num:
bellard92a31b12005-02-10 22:00:52 +00002794 if (c == 'i') {
2795 if (nb_args >= MAX_ARGS)
2796 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002797 args[nb_args++] = (void *)(long)val;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002798 if (dict_add)
2799 qdict_put(qdict, key, qint_from_int(val));
bellard92a31b12005-02-10 22:00:52 +00002800 } else {
2801 if ((nb_args + 1) >= MAX_ARGS)
2802 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002803#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002804 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002805#else
2806 args[nb_args++] = (void *)0;
2807#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002808 args[nb_args++] = (void *)(long)(val & 0xffffffff);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002809 qdict_put(qdict, key, qint_from_int(val));
bellard92a31b12005-02-10 22:00:52 +00002810 }
bellard9307c4c2004-04-04 12:57:25 +00002811 }
2812 break;
2813 case '-':
2814 {
2815 int has_option;
2816 /* option */
ths3b46e622007-09-17 08:09:54 +00002817
bellard9307c4c2004-04-04 12:57:25 +00002818 c = *typestr++;
2819 if (c == '\0')
2820 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002821 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002822 p++;
2823 has_option = 0;
2824 if (*p == '-') {
2825 p++;
2826 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002827 monitor_printf(mon, "%s: unsupported option -%c\n",
2828 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002829 goto fail;
2830 }
2831 p++;
2832 has_option = 1;
2833 }
2834 if (nb_args >= MAX_ARGS)
2835 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002836 args[nb_args++] = (void *)(long)has_option;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002837 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00002838 }
2839 break;
2840 default:
2841 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002842 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002843 goto fail;
2844 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002845 qemu_free(key);
2846 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002847 }
2848 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002849 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002850 p++;
2851 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002852 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2853 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002854 goto fail;
2855 }
2856
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002857 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002858
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002859fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002860 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002861 return NULL;
2862}
2863
2864static void monitor_handle_command(Monitor *mon, const char *cmdline)
2865{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002866 QDict *qdict;
2867 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002868
2869 qdict = qdict_new();
2870
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03002871 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002872 if (cmd) {
2873 void (*handler)(Monitor *mon, const QDict *qdict);
2874
2875 qemu_errors_to_mon(mon);
2876
2877 handler = cmd->handler;
2878 handler(mon, qdict);
2879
2880 qemu_errors_to_previous();
2881 }
2882
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002883 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002884}
2885
bellard81d09122004-07-14 17:21:37 +00002886static void cmd_completion(const char *name, const char *list)
2887{
2888 const char *p, *pstart;
2889 char cmd[128];
2890 int len;
2891
2892 p = list;
2893 for(;;) {
2894 pstart = p;
2895 p = strchr(p, '|');
2896 if (!p)
2897 p = pstart + strlen(pstart);
2898 len = p - pstart;
2899 if (len > sizeof(cmd) - 2)
2900 len = sizeof(cmd) - 2;
2901 memcpy(cmd, pstart, len);
2902 cmd[len] = '\0';
2903 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002904 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002905 }
2906 if (*p == '\0')
2907 break;
2908 p++;
2909 }
2910}
2911
2912static void file_completion(const char *input)
2913{
2914 DIR *ffs;
2915 struct dirent *d;
2916 char path[1024];
2917 char file[1024], file_prefix[1024];
2918 int input_path_len;
2919 const char *p;
2920
ths5fafdf22007-09-16 21:08:06 +00002921 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002922 if (!p) {
2923 input_path_len = 0;
2924 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002925 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002926 } else {
2927 input_path_len = p - input + 1;
2928 memcpy(path, input, input_path_len);
2929 if (input_path_len > sizeof(path) - 1)
2930 input_path_len = sizeof(path) - 1;
2931 path[input_path_len] = '\0';
2932 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2933 }
2934#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002935 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2936 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002937#endif
2938 ffs = opendir(path);
2939 if (!ffs)
2940 return;
2941 for(;;) {
2942 struct stat sb;
2943 d = readdir(ffs);
2944 if (!d)
2945 break;
2946 if (strstart(d->d_name, file_prefix, NULL)) {
2947 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002948 if (input_path_len < sizeof(file))
2949 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2950 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002951 /* stat the file to find out if it's a directory.
2952 * In that case add a slash to speed up typing long paths
2953 */
2954 stat(file, &sb);
2955 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002956 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00002957 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002958 }
2959 }
2960 closedir(ffs);
2961}
2962
aliguori51de9762009-03-05 23:00:43 +00002963static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002964{
aliguori51de9762009-03-05 23:00:43 +00002965 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002966 const char *input = opaque;
2967
2968 if (input[0] == '\0' ||
2969 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00002970 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00002971 }
2972}
2973
2974/* NOTE: this parser is an approximate form of the real command parser */
2975static void parse_cmdline(const char *cmdline,
2976 int *pnb_args, char **args)
2977{
2978 const char *p;
2979 int nb_args, ret;
2980 char buf[1024];
2981
2982 p = cmdline;
2983 nb_args = 0;
2984 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002985 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002986 p++;
2987 if (*p == '\0')
2988 break;
2989 if (nb_args >= MAX_ARGS)
2990 break;
2991 ret = get_str(buf, sizeof(buf), &p);
2992 args[nb_args] = qemu_strdup(buf);
2993 nb_args++;
2994 if (ret < 0)
2995 break;
2996 }
2997 *pnb_args = nb_args;
2998}
2999
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003000static const char *next_arg_type(const char *typestr)
3001{
3002 const char *p = strchr(typestr, ':');
3003 return (p != NULL ? ++p : typestr);
3004}
3005
aliguori4c36ba32009-03-05 23:01:37 +00003006static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003007{
3008 const char *cmdname;
3009 char *args[MAX_ARGS];
3010 int nb_args, i, len;
3011 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00003012 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003013 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003014
3015 parse_cmdline(cmdline, &nb_args, args);
3016#ifdef DEBUG_COMPLETION
3017 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003018 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003019 }
3020#endif
3021
3022 /* if the line ends with a space, it means we want to complete the
3023 next arg */
3024 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003025 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003026 if (nb_args >= MAX_ARGS)
3027 return;
3028 args[nb_args++] = qemu_strdup("");
3029 }
3030 if (nb_args <= 1) {
3031 /* command completion */
3032 if (nb_args == 0)
3033 cmdname = "";
3034 else
3035 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003036 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003037 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003038 cmd_completion(cmdname, cmd->name);
3039 }
3040 } else {
3041 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003042 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003043 if (compare_cmd(args[0], cmd->name))
3044 goto found;
3045 }
3046 return;
3047 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003048 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003049 for(i = 0; i < nb_args - 2; i++) {
3050 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003051 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003052 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003053 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003054 }
3055 }
3056 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003057 if (*ptype == '-' && ptype[1] != '\0') {
3058 ptype += 2;
3059 }
bellard81d09122004-07-14 17:21:37 +00003060 switch(*ptype) {
3061 case 'F':
3062 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003063 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003064 file_completion(str);
3065 break;
3066 case 'B':
3067 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003068 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003069 bdrv_iterate(block_completion_it, (void *)str);
3070 break;
bellard7fe48482004-10-09 18:08:01 +00003071 case 's':
3072 /* XXX: more generic ? */
3073 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003074 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003075 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3076 cmd_completion(str, cmd->name);
3077 }
bellard64866c32006-05-07 18:03:31 +00003078 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003079 char *sep = strrchr(str, '-');
3080 if (sep)
3081 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003082 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003083 for(key = key_defs; key->name != NULL; key++) {
3084 cmd_completion(str, key->name);
3085 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003086 } else if (!strcmp(cmd->name, "help|?")) {
3087 readline_set_completion_index(cur_mon->rs, strlen(str));
3088 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3089 cmd_completion(str, cmd->name);
3090 }
bellard7fe48482004-10-09 18:08:01 +00003091 }
3092 break;
bellard81d09122004-07-14 17:21:37 +00003093 default:
3094 break;
3095 }
3096 }
3097 for(i = 0; i < nb_args; i++)
3098 qemu_free(args[i]);
3099}
3100
aliguori731b0362009-03-05 23:01:42 +00003101static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003102{
aliguori731b0362009-03-05 23:01:42 +00003103 Monitor *mon = opaque;
3104
3105 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003106}
3107
aliguori731b0362009-03-05 23:01:42 +00003108static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003109{
aliguori731b0362009-03-05 23:01:42 +00003110 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003111 int i;
aliguori376253e2009-03-05 23:01:23 +00003112
aliguori731b0362009-03-05 23:01:42 +00003113 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003114
aliguoricde76ee2009-03-05 23:01:51 +00003115 if (cur_mon->rs) {
3116 for (i = 0; i < size; i++)
3117 readline_handle_byte(cur_mon->rs, buf[i]);
3118 } else {
3119 if (size == 0 || buf[size - 1] != 0)
3120 monitor_printf(cur_mon, "corrupted command\n");
3121 else
3122 monitor_handle_command(cur_mon, (char *)buf);
3123 }
aliguori731b0362009-03-05 23:01:42 +00003124
3125 cur_mon = old_mon;
3126}
aliguorid8f44602008-10-06 13:52:44 +00003127
aliguori376253e2009-03-05 23:01:23 +00003128static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003129{
aliguori731b0362009-03-05 23:01:42 +00003130 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003131 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003132 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003133}
3134
aliguoricde76ee2009-03-05 23:01:51 +00003135int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003136{
aliguoricde76ee2009-03-05 23:01:51 +00003137 if (!mon->rs)
3138 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003139 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003140 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003141}
3142
aliguori376253e2009-03-05 23:01:23 +00003143void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003144{
aliguoricde76ee2009-03-05 23:01:51 +00003145 if (!mon->rs)
3146 return;
aliguori731b0362009-03-05 23:01:42 +00003147 if (--mon->suspend_cnt == 0)
3148 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003149}
3150
aliguori731b0362009-03-05 23:01:42 +00003151static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003152{
aliguori376253e2009-03-05 23:01:23 +00003153 Monitor *mon = opaque;
3154
aliguori2724b182009-03-05 23:01:47 +00003155 switch (event) {
3156 case CHR_EVENT_MUX_IN:
3157 readline_restart(mon->rs);
3158 monitor_resume(mon);
3159 monitor_flush(mon);
3160 break;
ths86e94de2007-01-05 22:01:59 +00003161
aliguori2724b182009-03-05 23:01:47 +00003162 case CHR_EVENT_MUX_OUT:
3163 if (mon->suspend_cnt == 0)
3164 monitor_printf(mon, "\n");
3165 monitor_flush(mon);
3166 monitor_suspend(mon);
3167 break;
3168
3169 case CHR_EVENT_RESET:
3170 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3171 "information\n", QEMU_VERSION);
3172 if (mon->chr->focus == 0)
3173 readline_show_prompt(mon->rs);
3174 break;
3175 }
ths86e94de2007-01-05 22:01:59 +00003176}
3177
aliguori76655d62009-03-06 20:27:37 +00003178
3179/*
3180 * Local variables:
3181 * c-indent-level: 4
3182 * c-basic-offset: 4
3183 * tab-width: 8
3184 * End:
3185 */
3186
aliguori731b0362009-03-05 23:01:42 +00003187void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003188{
aliguori731b0362009-03-05 23:01:42 +00003189 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003190 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003191
3192 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003193 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003194 is_first_init = 0;
3195 }
aliguori87127162009-03-05 23:01:29 +00003196
3197 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003198
aliguori87127162009-03-05 23:01:29 +00003199 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003200 mon->flags = flags;
aliguori2724b182009-03-05 23:01:47 +00003201 if (mon->chr->focus != 0)
3202 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
aliguoricde76ee2009-03-05 23:01:51 +00003203 if (flags & MONITOR_USE_READLINE) {
3204 mon->rs = readline_init(mon, monitor_find_completion);
3205 monitor_read_command(mon, 0);
3206 }
aliguori87127162009-03-05 23:01:29 +00003207
aliguori731b0362009-03-05 23:01:42 +00003208 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3209 mon);
aliguori87127162009-03-05 23:01:29 +00003210
3211 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003212 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003213 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003214}
3215
aliguori376253e2009-03-05 23:01:23 +00003216static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003217{
aliguoribb5fc202009-03-05 23:01:15 +00003218 BlockDriverState *bs = opaque;
3219 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003220
aliguoribb5fc202009-03-05 23:01:15 +00003221 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003222 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003223 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003224 }
aliguori731b0362009-03-05 23:01:42 +00003225 if (mon->password_completion_cb)
3226 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003227
aliguori731b0362009-03-05 23:01:42 +00003228 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003229}
aliguoric0f4ce72009-03-05 23:01:01 +00003230
aliguori376253e2009-03-05 23:01:23 +00003231void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003232 BlockDriverCompletionFunc *completion_cb,
3233 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003234{
aliguoricde76ee2009-03-05 23:01:51 +00003235 int err;
3236
aliguoribb5fc202009-03-05 23:01:15 +00003237 if (!bdrv_key_required(bs)) {
3238 if (completion_cb)
3239 completion_cb(opaque, 0);
3240 return;
3241 }
aliguoric0f4ce72009-03-05 23:01:01 +00003242
aliguori376253e2009-03-05 23:01:23 +00003243 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3244 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003245
aliguori731b0362009-03-05 23:01:42 +00003246 mon->password_completion_cb = completion_cb;
3247 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003248
aliguoricde76ee2009-03-05 23:01:51 +00003249 err = monitor_read_password(mon, bdrv_password_cb, bs);
3250
3251 if (err && completion_cb)
3252 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003253}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003254
3255typedef struct QemuErrorSink QemuErrorSink;
3256struct QemuErrorSink {
3257 enum {
3258 ERR_SINK_FILE,
3259 ERR_SINK_MONITOR,
3260 } dest;
3261 union {
3262 FILE *fp;
3263 Monitor *mon;
3264 };
3265 QemuErrorSink *previous;
3266};
3267
Blue Swirl528e93a2009-08-31 15:14:40 +00003268static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003269
3270void qemu_errors_to_file(FILE *fp)
3271{
3272 QemuErrorSink *sink;
3273
3274 sink = qemu_mallocz(sizeof(*sink));
3275 sink->dest = ERR_SINK_FILE;
3276 sink->fp = fp;
3277 sink->previous = qemu_error_sink;
3278 qemu_error_sink = sink;
3279}
3280
3281void qemu_errors_to_mon(Monitor *mon)
3282{
3283 QemuErrorSink *sink;
3284
3285 sink = qemu_mallocz(sizeof(*sink));
3286 sink->dest = ERR_SINK_MONITOR;
3287 sink->mon = mon;
3288 sink->previous = qemu_error_sink;
3289 qemu_error_sink = sink;
3290}
3291
3292void qemu_errors_to_previous(void)
3293{
3294 QemuErrorSink *sink;
3295
3296 assert(qemu_error_sink != NULL);
3297 sink = qemu_error_sink;
3298 qemu_error_sink = sink->previous;
3299 qemu_free(sink);
3300}
3301
3302void qemu_error(const char *fmt, ...)
3303{
3304 va_list args;
3305
3306 assert(qemu_error_sink != NULL);
3307 switch (qemu_error_sink->dest) {
3308 case ERR_SINK_FILE:
3309 va_start(args, fmt);
3310 vfprintf(qemu_error_sink->fp, fmt, args);
3311 va_end(args);
3312 break;
3313 case ERR_SINK_MONITOR:
3314 va_start(args, fmt);
3315 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3316 va_end(args);
3317 break;
3318 }
3319}