blob: d6b1d6463e925d070957b1890f02892e89d35d9e [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
aliguori376253e2009-03-05 23:01:23 +0000520static void do_change(Monitor *mon, const char *device, const char *target,
521 const char *arg)
thse25a5822007-08-25 01:36:20 +0000522{
523 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000524 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000525 } else {
aliguori28a76be2009-03-06 20:27:40 +0000526 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000527 }
528}
529
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300530static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000531{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300532 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000533}
534
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300535static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000536{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300537 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000538}
539
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300540static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000541{
542 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300543 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000544
bellard9307c4c2004-04-04 12:57:25 +0000545 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000546 mask = 0;
547 } else {
bellard9307c4c2004-04-04 12:57:25 +0000548 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000549 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000550 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000551 return;
552 }
553 }
554 cpu_set_log(mask);
555}
556
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300557static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000558{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300559 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000560 if (!option || !strcmp(option, "on")) {
561 singlestep = 1;
562 } else if (!strcmp(option, "off")) {
563 singlestep = 0;
564 } else {
565 monitor_printf(mon, "unexpected option %s\n", option);
566 }
567}
568
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300569static void do_stop(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000570{
571 vm_stop(EXCP_INTERRUPT);
572}
573
aliguoribb5fc202009-03-05 23:01:15 +0000574static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000575
aliguori376253e2009-03-05 23:01:23 +0000576struct bdrv_iterate_context {
577 Monitor *mon;
578 int err;
579};
aliguoric0f4ce72009-03-05 23:01:01 +0000580
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300581static void do_cont(Monitor *mon, const QDict *qdict)
aliguori376253e2009-03-05 23:01:23 +0000582{
583 struct bdrv_iterate_context context = { mon, 0 };
584
585 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000586 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000587 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000588 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000589}
590
aliguoribb5fc202009-03-05 23:01:15 +0000591static void bdrv_key_cb(void *opaque, int err)
592{
aliguori376253e2009-03-05 23:01:23 +0000593 Monitor *mon = opaque;
594
aliguoribb5fc202009-03-05 23:01:15 +0000595 /* another key was set successfully, retry to continue */
596 if (!err)
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300597 do_cont(mon, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000598}
599
600static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
601{
aliguori376253e2009-03-05 23:01:23 +0000602 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000603
aliguori376253e2009-03-05 23:01:23 +0000604 if (!context->err && bdrv_key_required(bs)) {
605 context->err = -EBUSY;
606 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
607 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000608 }
609}
610
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300611static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000612{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300613 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000614 if (!device)
615 device = "tcp::" DEFAULT_GDBSTUB_PORT;
616 if (gdbserver_start(device) < 0) {
617 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
618 device);
619 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000620 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000621 } else {
aliguori59030a82009-04-05 18:43:41 +0000622 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
623 device);
bellard8a7ddc32004-03-31 19:00:16 +0000624 }
625}
626
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300627static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100628{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300629 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100630 if (select_watchdog_action(action) == -1) {
631 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
632 }
633}
634
aliguori376253e2009-03-05 23:01:23 +0000635static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000636{
aliguori376253e2009-03-05 23:01:23 +0000637 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000638 switch(c) {
639 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000640 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000641 break;
642 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000643 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000644 break;
645 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000646 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000647 break;
648 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000649 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000650 break;
651 default:
652 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000653 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000654 } else {
aliguori376253e2009-03-05 23:01:23 +0000655 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000656 }
657 break;
658 }
aliguori376253e2009-03-05 23:01:23 +0000659 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000660}
661
aliguori376253e2009-03-05 23:01:23 +0000662static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000663 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000664{
bellard6a00d602005-11-21 23:25:50 +0000665 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000666 int nb_per_line, l, line_size, i, max_digits, len;
667 uint8_t buf[16];
668 uint64_t v;
669
670 if (format == 'i') {
671 int flags;
672 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000673 env = mon_get_cpu();
674 if (!env && !is_physical)
675 return;
bellard9307c4c2004-04-04 12:57:25 +0000676#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000677 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000678 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000679 } else if (wsize == 4) {
680 flags = 0;
681 } else {
bellard6a15fd12006-04-12 21:07:07 +0000682 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000683 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000684 if (env) {
685#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000686 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000687 (env->segs[R_CS].flags & DESC_L_MASK))
688 flags = 2;
689 else
690#endif
691 if (!(env->segs[R_CS].flags & DESC_B_MASK))
692 flags = 1;
693 }
bellard4c27ba22004-04-25 18:05:08 +0000694 }
695#endif
aliguori376253e2009-03-05 23:01:23 +0000696 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000697 return;
698 }
699
700 len = wsize * count;
701 if (wsize == 1)
702 line_size = 8;
703 else
704 line_size = 16;
705 nb_per_line = line_size / wsize;
706 max_digits = 0;
707
708 switch(format) {
709 case 'o':
710 max_digits = (wsize * 8 + 2) / 3;
711 break;
712 default:
713 case 'x':
714 max_digits = (wsize * 8) / 4;
715 break;
716 case 'u':
717 case 'd':
718 max_digits = (wsize * 8 * 10 + 32) / 33;
719 break;
720 case 'c':
721 wsize = 1;
722 break;
723 }
724
725 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000726 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000727 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000728 else
aliguori376253e2009-03-05 23:01:23 +0000729 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000730 l = len;
731 if (l > line_size)
732 l = line_size;
733 if (is_physical) {
734 cpu_physical_memory_rw(addr, buf, l, 0);
735 } else {
bellard6a00d602005-11-21 23:25:50 +0000736 env = mon_get_cpu();
737 if (!env)
738 break;
aliguoric8f79b62008-08-18 14:00:20 +0000739 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000740 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000741 break;
742 }
bellard9307c4c2004-04-04 12:57:25 +0000743 }
ths5fafdf22007-09-16 21:08:06 +0000744 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000745 while (i < l) {
746 switch(wsize) {
747 default:
748 case 1:
749 v = ldub_raw(buf + i);
750 break;
751 case 2:
752 v = lduw_raw(buf + i);
753 break;
754 case 4:
bellard92a31b12005-02-10 22:00:52 +0000755 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000756 break;
757 case 8:
758 v = ldq_raw(buf + i);
759 break;
760 }
aliguori376253e2009-03-05 23:01:23 +0000761 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000762 switch(format) {
763 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000764 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000765 break;
766 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000767 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000768 break;
769 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000770 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000771 break;
772 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000773 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000774 break;
775 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000776 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000777 break;
778 }
779 i += wsize;
780 }
aliguori376253e2009-03-05 23:01:23 +0000781 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000782 addr += l;
783 len -= l;
784 }
785}
786
bellard92a31b12005-02-10 22:00:52 +0000787#if TARGET_LONG_BITS == 64
788#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
789#else
790#define GET_TLONG(h, l) (l)
791#endif
792
aliguori376253e2009-03-05 23:01:23 +0000793static void do_memory_dump(Monitor *mon, int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000794 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000795{
bellard92a31b12005-02-10 22:00:52 +0000796 target_long addr = GET_TLONG(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000797 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000798}
799
blueswir17743e582007-09-24 18:39:04 +0000800#if TARGET_PHYS_ADDR_BITS > 32
801#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
802#else
803#define GET_TPHYSADDR(h, l) (l)
804#endif
805
aliguori376253e2009-03-05 23:01:23 +0000806static void do_physical_memory_dump(Monitor *mon, int count, int format,
807 int size, uint32_t addrh, uint32_t addrl)
bellard92a31b12005-02-10 22:00:52 +0000808
bellard9307c4c2004-04-04 12:57:25 +0000809{
blueswir17743e582007-09-24 18:39:04 +0000810 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000811 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000812}
813
aliguori376253e2009-03-05 23:01:23 +0000814static void do_print(Monitor *mon, int count, int format, int size,
815 unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000816{
blueswir17743e582007-09-24 18:39:04 +0000817 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
818#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000819 switch(format) {
820 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000821 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000822 break;
823 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000824 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000825 break;
826 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000827 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000828 break;
829 default:
830 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000831 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000832 break;
833 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000834 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000835 break;
836 }
bellard92a31b12005-02-10 22:00:52 +0000837#else
838 switch(format) {
839 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000840 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000841 break;
842 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000843 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000844 break;
845 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000846 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000847 break;
848 default:
849 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000850 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000851 break;
852 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000853 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000854 break;
855 }
856#endif
aliguori376253e2009-03-05 23:01:23 +0000857 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000858}
859
aliguori376253e2009-03-05 23:01:23 +0000860static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000861 uint32_t size, const char *filename)
862{
863 FILE *f;
864 target_long addr = GET_TLONG(valh, vall);
865 uint32_t l;
866 CPUState *env;
867 uint8_t buf[1024];
868
869 env = mon_get_cpu();
870 if (!env)
871 return;
872
873 f = fopen(filename, "wb");
874 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000875 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000876 return;
877 }
878 while (size != 0) {
879 l = sizeof(buf);
880 if (l > size)
881 l = size;
882 cpu_memory_rw_debug(env, addr, buf, l, 0);
883 fwrite(buf, 1, l, f);
884 addr += l;
885 size -= l;
886 }
887 fclose(f);
888}
889
aliguori376253e2009-03-05 23:01:23 +0000890static void do_physical_memory_save(Monitor *mon, unsigned int valh,
891 unsigned int vall, uint32_t size,
892 const char *filename)
aurel32a8bdf7a2008-04-11 21:36:14 +0000893{
894 FILE *f;
895 uint32_t l;
896 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000897 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000898
899 f = fopen(filename, "wb");
900 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000901 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000902 return;
903 }
904 while (size != 0) {
905 l = sizeof(buf);
906 if (l > size)
907 l = size;
908 cpu_physical_memory_rw(addr, buf, l, 0);
909 fwrite(buf, 1, l, f);
910 fflush(f);
911 addr += l;
912 size -= l;
913 }
914 fclose(f);
915}
916
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300917static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000918{
919 uint32_t addr;
920 uint8_t buf[1];
921 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300922 uint32_t start = qdict_get_int(qdict, "start");
923 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000924
925 sum = 0;
926 for(addr = start; addr < (start + size); addr++) {
927 cpu_physical_memory_rw(addr, buf, 1, 0);
928 /* BSD sum algorithm ('sum' Unix command) */
929 sum = (sum >> 1) | (sum << 15);
930 sum += buf[0];
931 }
aliguori376253e2009-03-05 23:01:23 +0000932 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000933}
934
bellarda3a91a32004-06-04 11:06:21 +0000935typedef struct {
936 int keycode;
937 const char *name;
938} KeyDef;
939
940static const KeyDef key_defs[] = {
941 { 0x2a, "shift" },
942 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000943
bellarda3a91a32004-06-04 11:06:21 +0000944 { 0x38, "alt" },
945 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000946 { 0x64, "altgr" },
947 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000948 { 0x1d, "ctrl" },
949 { 0x9d, "ctrl_r" },
950
951 { 0xdd, "menu" },
952
953 { 0x01, "esc" },
954
955 { 0x02, "1" },
956 { 0x03, "2" },
957 { 0x04, "3" },
958 { 0x05, "4" },
959 { 0x06, "5" },
960 { 0x07, "6" },
961 { 0x08, "7" },
962 { 0x09, "8" },
963 { 0x0a, "9" },
964 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000965 { 0x0c, "minus" },
966 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000967 { 0x0e, "backspace" },
968
969 { 0x0f, "tab" },
970 { 0x10, "q" },
971 { 0x11, "w" },
972 { 0x12, "e" },
973 { 0x13, "r" },
974 { 0x14, "t" },
975 { 0x15, "y" },
976 { 0x16, "u" },
977 { 0x17, "i" },
978 { 0x18, "o" },
979 { 0x19, "p" },
980
981 { 0x1c, "ret" },
982
983 { 0x1e, "a" },
984 { 0x1f, "s" },
985 { 0x20, "d" },
986 { 0x21, "f" },
987 { 0x22, "g" },
988 { 0x23, "h" },
989 { 0x24, "j" },
990 { 0x25, "k" },
991 { 0x26, "l" },
992
993 { 0x2c, "z" },
994 { 0x2d, "x" },
995 { 0x2e, "c" },
996 { 0x2f, "v" },
997 { 0x30, "b" },
998 { 0x31, "n" },
999 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001000 { 0x33, "comma" },
1001 { 0x34, "dot" },
1002 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001003
balrog4d3b6f62008-02-10 16:33:14 +00001004 { 0x37, "asterisk" },
1005
bellarda3a91a32004-06-04 11:06:21 +00001006 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001007 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001008 { 0x3b, "f1" },
1009 { 0x3c, "f2" },
1010 { 0x3d, "f3" },
1011 { 0x3e, "f4" },
1012 { 0x3f, "f5" },
1013 { 0x40, "f6" },
1014 { 0x41, "f7" },
1015 { 0x42, "f8" },
1016 { 0x43, "f9" },
1017 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001018 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001019 { 0x46, "scroll_lock" },
1020
bellard64866c32006-05-07 18:03:31 +00001021 { 0xb5, "kp_divide" },
1022 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001023 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001024 { 0x4e, "kp_add" },
1025 { 0x9c, "kp_enter" },
1026 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001027 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001028
1029 { 0x52, "kp_0" },
1030 { 0x4f, "kp_1" },
1031 { 0x50, "kp_2" },
1032 { 0x51, "kp_3" },
1033 { 0x4b, "kp_4" },
1034 { 0x4c, "kp_5" },
1035 { 0x4d, "kp_6" },
1036 { 0x47, "kp_7" },
1037 { 0x48, "kp_8" },
1038 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001039
bellarda3a91a32004-06-04 11:06:21 +00001040 { 0x56, "<" },
1041
1042 { 0x57, "f11" },
1043 { 0x58, "f12" },
1044
1045 { 0xb7, "print" },
1046
1047 { 0xc7, "home" },
1048 { 0xc9, "pgup" },
1049 { 0xd1, "pgdn" },
1050 { 0xcf, "end" },
1051
1052 { 0xcb, "left" },
1053 { 0xc8, "up" },
1054 { 0xd0, "down" },
1055 { 0xcd, "right" },
1056
1057 { 0xd2, "insert" },
1058 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001059#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1060 { 0xf0, "stop" },
1061 { 0xf1, "again" },
1062 { 0xf2, "props" },
1063 { 0xf3, "undo" },
1064 { 0xf4, "front" },
1065 { 0xf5, "copy" },
1066 { 0xf6, "open" },
1067 { 0xf7, "paste" },
1068 { 0xf8, "find" },
1069 { 0xf9, "cut" },
1070 { 0xfa, "lf" },
1071 { 0xfb, "help" },
1072 { 0xfc, "meta_l" },
1073 { 0xfd, "meta_r" },
1074 { 0xfe, "compose" },
1075#endif
bellarda3a91a32004-06-04 11:06:21 +00001076 { 0, NULL },
1077};
1078
1079static int get_keycode(const char *key)
1080{
1081 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001082 char *endp;
1083 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001084
1085 for(p = key_defs; p->name != NULL; p++) {
1086 if (!strcmp(key, p->name))
1087 return p->keycode;
1088 }
bellard64866c32006-05-07 18:03:31 +00001089 if (strstart(key, "0x", NULL)) {
1090 ret = strtoul(key, &endp, 0);
1091 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1092 return ret;
1093 }
bellarda3a91a32004-06-04 11:06:21 +00001094 return -1;
1095}
1096
balrogc8256f92008-06-08 22:45:01 +00001097#define MAX_KEYCODES 16
1098static uint8_t keycodes[MAX_KEYCODES];
1099static int nb_pending_keycodes;
1100static QEMUTimer *key_timer;
1101
1102static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001103{
balrogc8256f92008-06-08 22:45:01 +00001104 int keycode;
1105
1106 while (nb_pending_keycodes > 0) {
1107 nb_pending_keycodes--;
1108 keycode = keycodes[nb_pending_keycodes];
1109 if (keycode & 0x80)
1110 kbd_put_keycode(0xe0);
1111 kbd_put_keycode(keycode | 0x80);
1112 }
1113}
1114
aliguori376253e2009-03-05 23:01:23 +00001115static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1116 int hold_time)
balrogc8256f92008-06-08 22:45:01 +00001117{
balrog3401c0d2008-06-04 10:05:59 +00001118 char keyname_buf[16];
1119 char *separator;
1120 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +00001121
balrogc8256f92008-06-08 22:45:01 +00001122 if (nb_pending_keycodes > 0) {
1123 qemu_del_timer(key_timer);
1124 release_keys(NULL);
1125 }
1126 if (!has_hold_time)
1127 hold_time = 100;
1128 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001129 while (1) {
1130 separator = strchr(string, '-');
1131 keyname_len = separator ? separator - string : strlen(string);
1132 if (keyname_len > 0) {
1133 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1134 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001135 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001136 return;
bellarda3a91a32004-06-04 11:06:21 +00001137 }
balrogc8256f92008-06-08 22:45:01 +00001138 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001139 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001140 return;
1141 }
1142 keyname_buf[keyname_len] = 0;
1143 keycode = get_keycode(keyname_buf);
1144 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001145 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001146 return;
1147 }
balrogc8256f92008-06-08 22:45:01 +00001148 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001149 }
balrog3401c0d2008-06-04 10:05:59 +00001150 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001151 break;
balrog3401c0d2008-06-04 10:05:59 +00001152 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001153 }
balrogc8256f92008-06-08 22:45:01 +00001154 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001155 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001156 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001157 keycode = keycodes[i];
1158 if (keycode & 0x80)
1159 kbd_put_keycode(0xe0);
1160 kbd_put_keycode(keycode & 0x7f);
1161 }
balrogc8256f92008-06-08 22:45:01 +00001162 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001163 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1164 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001165}
1166
bellard13224a82006-07-14 22:03:35 +00001167static int mouse_button_state;
1168
aliguori376253e2009-03-05 23:01:23 +00001169static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001170 const char *dz_str)
1171{
1172 int dx, dy, dz;
1173 dx = strtol(dx_str, NULL, 0);
1174 dy = strtol(dy_str, NULL, 0);
1175 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001176 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001177 dz = strtol(dz_str, NULL, 0);
1178 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1179}
1180
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001181static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001182{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001183 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001184 mouse_button_state = button_state;
1185 kbd_mouse_event(0, 0, 0, mouse_button_state);
1186}
1187
aliguori376253e2009-03-05 23:01:23 +00001188static void do_ioport_read(Monitor *mon, int count, int format, int size,
1189 int addr, int has_index, int index)
bellard34405572004-06-08 00:55:58 +00001190{
1191 uint32_t val;
1192 int suffix;
1193
1194 if (has_index) {
Isaku Yamahatad56dd6c2009-07-02 19:32:07 +09001195 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001196 addr++;
1197 }
1198 addr &= 0xffff;
1199
1200 switch(size) {
1201 default:
1202 case 1:
1203 val = cpu_inb(NULL, addr);
1204 suffix = 'b';
1205 break;
1206 case 2:
1207 val = cpu_inw(NULL, addr);
1208 suffix = 'w';
1209 break;
1210 case 4:
1211 val = cpu_inl(NULL, addr);
1212 suffix = 'l';
1213 break;
1214 }
aliguori376253e2009-03-05 23:01:23 +00001215 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1216 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001217}
bellarda3a91a32004-06-04 11:06:21 +00001218
Jan Kiszkaf1147842009-07-14 10:20:11 +02001219static void do_ioport_write(Monitor *mon, int count, int format, int size,
1220 int addr, int val)
1221{
1222 addr &= IOPORTS_MASK;
1223
1224 switch (size) {
1225 default:
1226 case 1:
1227 cpu_outb(NULL, addr, val);
1228 break;
1229 case 2:
1230 cpu_outw(NULL, addr, val);
1231 break;
1232 case 4:
1233 cpu_outl(NULL, addr, val);
1234 break;
1235 }
1236}
1237
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001238static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001239{
1240 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001241 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001242
Jan Kiszka76e30d02009-07-02 00:19:02 +02001243 res = qemu_boot_set(bootdevice);
1244 if (res == 0) {
1245 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1246 } else if (res > 0) {
1247 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001248 } else {
aliguori376253e2009-03-05 23:01:23 +00001249 monitor_printf(mon, "no function defined to set boot device list for "
1250 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001251 }
1252}
1253
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001254static void do_system_reset(Monitor *mon, const QDict *qdict)
bellarde4f90822004-06-20 12:35:44 +00001255{
1256 qemu_system_reset_request();
1257}
1258
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001259static void do_system_powerdown(Monitor *mon, const QDict *qdict)
bellard34751872005-07-02 14:31:34 +00001260{
1261 qemu_system_powerdown_request();
1262}
1263
bellardb86bda52004-09-18 19:32:46 +00001264#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001265static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001266{
aliguori376253e2009-03-05 23:01:23 +00001267 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1268 addr,
1269 pte & mask,
1270 pte & PG_GLOBAL_MASK ? 'G' : '-',
1271 pte & PG_PSE_MASK ? 'P' : '-',
1272 pte & PG_DIRTY_MASK ? 'D' : '-',
1273 pte & PG_ACCESSED_MASK ? 'A' : '-',
1274 pte & PG_PCD_MASK ? 'C' : '-',
1275 pte & PG_PWT_MASK ? 'T' : '-',
1276 pte & PG_USER_MASK ? 'U' : '-',
1277 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001278}
1279
aliguori376253e2009-03-05 23:01:23 +00001280static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001281{
bellard6a00d602005-11-21 23:25:50 +00001282 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001283 int l1, l2;
1284 uint32_t pgd, pde, pte;
1285
bellard6a00d602005-11-21 23:25:50 +00001286 env = mon_get_cpu();
1287 if (!env)
1288 return;
1289
bellardb86bda52004-09-18 19:32:46 +00001290 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001291 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001292 return;
1293 }
1294 pgd = env->cr[3] & ~0xfff;
1295 for(l1 = 0; l1 < 1024; l1++) {
1296 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1297 pde = le32_to_cpu(pde);
1298 if (pde & PG_PRESENT_MASK) {
1299 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001300 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001301 } else {
1302 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001303 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001304 (uint8_t *)&pte, 4);
1305 pte = le32_to_cpu(pte);
1306 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001307 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001308 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001309 ~0xfff);
1310 }
1311 }
1312 }
1313 }
1314 }
1315}
1316
aliguori376253e2009-03-05 23:01:23 +00001317static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001318 uint32_t end, int prot)
1319{
bellard9746b152004-11-11 18:30:24 +00001320 int prot1;
1321 prot1 = *plast_prot;
1322 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001323 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001324 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1325 *pstart, end, end - *pstart,
1326 prot1 & PG_USER_MASK ? 'u' : '-',
1327 'r',
1328 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001329 }
1330 if (prot != 0)
1331 *pstart = end;
1332 else
1333 *pstart = -1;
1334 *plast_prot = prot;
1335 }
1336}
1337
aliguori376253e2009-03-05 23:01:23 +00001338static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001339{
bellard6a00d602005-11-21 23:25:50 +00001340 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001341 int l1, l2, prot, last_prot;
1342 uint32_t pgd, pde, pte, start, end;
1343
bellard6a00d602005-11-21 23:25:50 +00001344 env = mon_get_cpu();
1345 if (!env)
1346 return;
1347
bellardb86bda52004-09-18 19:32:46 +00001348 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001349 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001350 return;
1351 }
1352 pgd = env->cr[3] & ~0xfff;
1353 last_prot = 0;
1354 start = -1;
1355 for(l1 = 0; l1 < 1024; l1++) {
1356 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1357 pde = le32_to_cpu(pde);
1358 end = l1 << 22;
1359 if (pde & PG_PRESENT_MASK) {
1360 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1361 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001362 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001363 } else {
1364 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001365 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001366 (uint8_t *)&pte, 4);
1367 pte = le32_to_cpu(pte);
1368 end = (l1 << 22) + (l2 << 12);
1369 if (pte & PG_PRESENT_MASK) {
1370 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1371 } else {
1372 prot = 0;
1373 }
aliguori376253e2009-03-05 23:01:23 +00001374 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001375 }
1376 }
1377 } else {
1378 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001379 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001380 }
1381 }
1382}
1383#endif
1384
aurel327c664e22009-03-03 06:12:22 +00001385#if defined(TARGET_SH4)
1386
aliguori376253e2009-03-05 23:01:23 +00001387static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001388{
aliguori376253e2009-03-05 23:01:23 +00001389 monitor_printf(mon, " tlb%i:\t"
1390 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1391 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1392 "dirty=%hhu writethrough=%hhu\n",
1393 idx,
1394 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1395 tlb->v, tlb->sh, tlb->c, tlb->pr,
1396 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001397}
1398
aliguori376253e2009-03-05 23:01:23 +00001399static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001400{
1401 CPUState *env = mon_get_cpu();
1402 int i;
1403
aliguori376253e2009-03-05 23:01:23 +00001404 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001405 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001406 print_tlb (mon, i, &env->itlb[i]);
1407 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001408 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001409 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001410}
1411
1412#endif
1413
aliguori376253e2009-03-05 23:01:23 +00001414static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001415{
1416#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001417 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001418 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001419 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001420 else
aliguori376253e2009-03-05 23:01:23 +00001421 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001422#else
aliguori376253e2009-03-05 23:01:23 +00001423 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001424#endif
1425}
1426
aliguori030ea372009-04-21 22:30:47 +00001427static void do_info_numa(Monitor *mon)
1428{
aliguorib28b6232009-04-22 20:20:29 +00001429 int i;
aliguori030ea372009-04-21 22:30:47 +00001430 CPUState *env;
1431
1432 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1433 for (i = 0; i < nb_numa_nodes; i++) {
1434 monitor_printf(mon, "node %d cpus:", i);
1435 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1436 if (env->numa_node == i) {
1437 monitor_printf(mon, " %d", env->cpu_index);
1438 }
1439 }
1440 monitor_printf(mon, "\n");
1441 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1442 node_mem[i] >> 20);
1443 }
1444}
1445
bellard5f1ce942006-02-08 22:40:15 +00001446#ifdef CONFIG_PROFILER
1447
aliguori376253e2009-03-05 23:01:23 +00001448static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001449{
1450 int64_t total;
1451 total = qemu_time;
1452 if (total == 0)
1453 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001454 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1455 dev_time, dev_time / (double)ticks_per_sec);
1456 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1457 qemu_time, qemu_time / (double)ticks_per_sec);
bellard5f1ce942006-02-08 22:40:15 +00001458 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001459 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001460}
1461#else
aliguori376253e2009-03-05 23:01:23 +00001462static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001463{
aliguori376253e2009-03-05 23:01:23 +00001464 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001465}
1466#endif
1467
bellardec36b692006-07-16 18:57:03 +00001468/* Capture support */
1469static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1470
aliguori376253e2009-03-05 23:01:23 +00001471static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001472{
1473 int i;
1474 CaptureState *s;
1475
1476 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001477 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001478 s->ops.info (s->opaque);
1479 }
1480}
1481
Blue Swirl23130862009-06-06 08:22:04 +00001482#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001483static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001484{
1485 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001486 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001487 CaptureState *s;
1488
1489 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1490 if (i == n) {
1491 s->ops.destroy (s->opaque);
1492 LIST_REMOVE (s, entries);
1493 qemu_free (s);
1494 return;
1495 }
1496 }
1497}
1498
aliguori376253e2009-03-05 23:01:23 +00001499static void do_wav_capture(Monitor *mon, const char *path,
1500 int has_freq, int freq,
1501 int has_bits, int bits,
1502 int has_channels, int nchannels)
bellardec36b692006-07-16 18:57:03 +00001503{
1504 CaptureState *s;
1505
1506 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001507
1508 freq = has_freq ? freq : 44100;
1509 bits = has_bits ? bits : 16;
1510 nchannels = has_channels ? nchannels : 2;
1511
1512 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001513 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001514 qemu_free (s);
1515 }
1516 LIST_INSERT_HEAD (&capture_head, s, entries);
1517}
1518#endif
1519
aurel32dc1c0b72008-04-27 23:52:12 +00001520#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001521static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001522{
1523 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001524 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001525
1526 for (env = first_cpu; env != NULL; env = env->next_cpu)
1527 if (env->cpu_index == cpu_index) {
1528 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1529 break;
1530 }
1531}
1532#endif
1533
aliguori376253e2009-03-05 23:01:23 +00001534static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001535{
aurel321b530a62009-04-05 20:08:59 +00001536 if (vm_running) {
1537 if (singlestep) {
1538 monitor_printf(mon, "VM status: running (single step mode)\n");
1539 } else {
1540 monitor_printf(mon, "VM status: running\n");
1541 }
1542 } else
aliguori376253e2009-03-05 23:01:23 +00001543 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001544}
1545
1546
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001547static void do_balloon(Monitor *mon, const QDict *qdict)
aliguoridf751fa2008-12-04 20:19:35 +00001548{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001549 int value = qdict_get_int(qdict, "value");
aliguoridf751fa2008-12-04 20:19:35 +00001550 ram_addr_t target = value;
1551 qemu_balloon(target << 20);
1552}
1553
aliguori376253e2009-03-05 23:01:23 +00001554static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001555{
1556 ram_addr_t actual;
1557
1558 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001559 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001560 monitor_printf(mon, "Using KVM without synchronous MMU, "
1561 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001562 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001563 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001564 else
aliguori376253e2009-03-05 23:01:23 +00001565 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001566}
1567
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001568static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001569{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001570 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001571
aliguori76655d62009-03-06 20:27:37 +00001572 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001573 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001574 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001575 return acl;
1576}
aliguori76655d62009-03-06 20:27:37 +00001577
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001578static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001579{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001580 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001581 qemu_acl *acl = find_acl(mon, aclname);
1582 qemu_acl_entry *entry;
1583 int i = 0;
1584
1585 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001586 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001587 acl->defaultDeny ? "deny" : "allow");
aliguori28a76be2009-03-06 20:27:40 +00001588 TAILQ_FOREACH(entry, &acl->entries, next) {
1589 i++;
1590 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001591 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001592 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001593 }
1594}
1595
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001596static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001597{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001598 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001599 qemu_acl *acl = find_acl(mon, aclname);
1600
1601 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001602 qemu_acl_reset(acl);
1603 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001604 }
1605}
aliguori76655d62009-03-06 20:27:37 +00001606
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001607static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001608{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001609 const char *aclname = qdict_get_str(qdict, "aclname");
1610 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001611 qemu_acl *acl = find_acl(mon, aclname);
1612
1613 if (acl) {
1614 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001615 acl->defaultDeny = 0;
1616 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001617 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001618 acl->defaultDeny = 1;
1619 monitor_printf(mon, "acl: policy set to 'deny'\n");
1620 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001621 monitor_printf(mon, "acl: unknown policy '%s', "
1622 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001623 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001624 }
1625}
aliguori76655d62009-03-06 20:27:37 +00001626
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001627static void do_acl_add(Monitor *mon, const char *aclname,
1628 const char *match, const char *policy,
1629 int has_index, int index)
1630{
1631 qemu_acl *acl = find_acl(mon, aclname);
1632 int deny, ret;
1633
1634 if (acl) {
1635 if (strcmp(policy, "allow") == 0) {
1636 deny = 0;
1637 } else if (strcmp(policy, "deny") == 0) {
1638 deny = 1;
1639 } else {
1640 monitor_printf(mon, "acl: unknown policy '%s', "
1641 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001642 return;
1643 }
aliguori28a76be2009-03-06 20:27:40 +00001644 if (has_index)
1645 ret = qemu_acl_insert(acl, deny, match, index);
1646 else
1647 ret = qemu_acl_append(acl, deny, match);
1648 if (ret < 0)
1649 monitor_printf(mon, "acl: unable to add acl entry\n");
1650 else
1651 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001652 }
1653}
aliguori76655d62009-03-06 20:27:37 +00001654
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001655static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001656{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001657 const char *aclname = qdict_get_str(qdict, "aclname");
1658 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001659 qemu_acl *acl = find_acl(mon, aclname);
1660 int ret;
aliguori76655d62009-03-06 20:27:37 +00001661
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001662 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001663 ret = qemu_acl_remove(acl, match);
1664 if (ret < 0)
1665 monitor_printf(mon, "acl: no matching acl entry\n");
1666 else
1667 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001668 }
1669}
1670
Huang Ying79c4f6b2009-06-23 10:05:14 +08001671#if defined(TARGET_I386)
1672static void do_inject_mce(Monitor *mon,
1673 int cpu_index, int bank,
1674 unsigned status_hi, unsigned status_lo,
1675 unsigned mcg_status_hi, unsigned mcg_status_lo,
1676 unsigned addr_hi, unsigned addr_lo,
1677 unsigned misc_hi, unsigned misc_lo)
1678{
1679 CPUState *cenv;
1680 uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
1681 uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
1682 uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
1683 uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
1684
1685 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1686 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1687 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1688 break;
1689 }
1690}
1691#endif
1692
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001693static void do_getfd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001694{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001695 const char *fdname = qdict_get_str(qdict, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001696 mon_fd_t *monfd;
1697 int fd;
1698
1699 fd = qemu_chr_get_msgfd(mon->chr);
1700 if (fd == -1) {
1701 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1702 return;
1703 }
1704
1705 if (qemu_isdigit(fdname[0])) {
1706 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1707 return;
1708 }
1709
1710 fd = dup(fd);
1711 if (fd == -1) {
1712 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1713 strerror(errno));
1714 return;
1715 }
1716
1717 LIST_FOREACH(monfd, &mon->fds, next) {
1718 if (strcmp(monfd->name, fdname) != 0) {
1719 continue;
1720 }
1721
1722 close(monfd->fd);
1723 monfd->fd = fd;
1724 return;
1725 }
1726
1727 monfd = qemu_mallocz(sizeof(mon_fd_t));
1728 monfd->name = qemu_strdup(fdname);
1729 monfd->fd = fd;
1730
1731 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1732}
1733
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001734static void do_closefd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001735{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001736 const char *fdname = qdict_get_str(qdict, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001737 mon_fd_t *monfd;
1738
1739 LIST_FOREACH(monfd, &mon->fds, next) {
1740 if (strcmp(monfd->name, fdname) != 0) {
1741 continue;
1742 }
1743
1744 LIST_REMOVE(monfd, next);
1745 close(monfd->fd);
1746 qemu_free(monfd->name);
1747 qemu_free(monfd);
1748 return;
1749 }
1750
1751 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1752 fdname);
1753}
1754
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001755static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001756{
1757 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001758 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001759
1760 vm_stop(0);
1761
Juan Quintela05f24012009-08-20 19:42:22 +02001762 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001763 vm_start();
1764}
1765
Mark McLoughlin7768e042009-07-22 09:11:41 +01001766int monitor_get_fd(Monitor *mon, const char *fdname)
1767{
1768 mon_fd_t *monfd;
1769
1770 LIST_FOREACH(monfd, &mon->fds, next) {
1771 int fd;
1772
1773 if (strcmp(monfd->name, fdname) != 0) {
1774 continue;
1775 }
1776
1777 fd = monfd->fd;
1778
1779 /* caller takes ownership of fd */
1780 LIST_REMOVE(monfd, next);
1781 qemu_free(monfd->name);
1782 qemu_free(monfd);
1783
1784 return fd;
1785 }
1786
1787 return -1;
1788}
1789
aliguori376253e2009-03-05 23:01:23 +00001790static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001791#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001792 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001793};
1794
Blue Swirl23130862009-06-06 08:22:04 +00001795/* Please update qemu-monitor.hx when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001796static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001797 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001798 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001799 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001800 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001801 { "chardev", "", qemu_chr_info,
1802 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001803 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001804 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001805 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001806 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001807 { "registers", "", do_info_registers,
1808 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001809 { "cpus", "", do_info_cpus,
1810 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001811 { "history", "", do_info_history,
1812 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001813 { "irq", "", irq_info,
1814 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001815 { "pic", "", pic_info,
1816 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001817 { "pci", "", pci_info,
1818 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001819#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001820 { "tlb", "", tlb_info,
1821 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001822#endif
1823#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001824 { "mem", "", mem_info,
1825 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001826 { "hpet", "", do_info_hpet,
1827 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001828#endif
bellarde3db7222005-01-26 22:00:47 +00001829 { "jit", "", do_info_jit,
1830 "", "show dynamic compiler info", },
aliguori7ba1e612008-11-05 16:04:33 +00001831 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001832 "", "show KVM information", },
aliguori030ea372009-04-21 22:30:47 +00001833 { "numa", "", do_info_numa,
1834 "", "show NUMA information", },
bellarda594cfb2005-11-06 16:13:29 +00001835 { "usb", "", usb_info,
1836 "", "show guest USB devices", },
1837 { "usbhost", "", usb_host_info,
1838 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001839 { "profile", "", do_info_profile,
1840 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001841 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001842 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001843 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001844 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001845 { "status", "", do_info_status,
1846 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001847 { "pcmcia", "", pcmcia_info,
1848 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001849 { "mice", "", do_info_mice,
1850 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001851 { "vnc", "", do_info_vnc,
1852 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001853 { "name", "", do_info_name,
1854 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001855 { "uuid", "", do_info_uuid,
1856 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001857#if defined(TARGET_PPC)
1858 { "cpustats", "", do_info_cpu_stats,
1859 "", "show CPU statistics", },
1860#endif
blueswir131a60e22007-10-26 18:42:59 +00001861#if defined(CONFIG_SLIRP)
Jan Kiszka6dbe5532009-06-24 14:42:29 +02001862 { "usernet", "", do_info_usernet,
1863 "", "show user network stack connection states", },
blueswir131a60e22007-10-26 18:42:59 +00001864#endif
aliguori5bb79102008-10-13 03:12:02 +00001865 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001866 { "balloon", "", do_info_balloon,
1867 "", "show balloon information" },
Gerd Hoffmanncae49562009-06-05 15:53:17 +01001868 { "qtree", "", do_info_qtree,
1869 "", "show device tree" },
Gerd Hoffmannf6c64e02009-08-03 15:03:09 +02001870 { "qdm", "", do_info_qdm,
1871 "", "show qdev device model list" },
bellard9dc39cb2004-03-14 21:38:27 +00001872 { NULL, NULL, },
1873};
1874
bellard9307c4c2004-04-04 12:57:25 +00001875/*******************************************************************/
1876
1877static const char *pch;
1878static jmp_buf expr_env;
1879
bellard92a31b12005-02-10 22:00:52 +00001880#define MD_TLONG 0
1881#define MD_I32 1
1882
bellard9307c4c2004-04-04 12:57:25 +00001883typedef struct MonitorDef {
1884 const char *name;
1885 int offset;
blueswir18662d652008-10-02 18:32:44 +00001886 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001887 int type;
bellard9307c4c2004-04-04 12:57:25 +00001888} MonitorDef;
1889
bellard57206fd2004-04-25 18:54:52 +00001890#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001891static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001892{
bellard6a00d602005-11-21 23:25:50 +00001893 CPUState *env = mon_get_cpu();
1894 if (!env)
1895 return 0;
1896 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001897}
1898#endif
1899
bellarda541f292004-04-12 20:39:29 +00001900#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001901static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001902{
bellard6a00d602005-11-21 23:25:50 +00001903 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001904 unsigned int u;
1905 int i;
1906
bellard6a00d602005-11-21 23:25:50 +00001907 if (!env)
1908 return 0;
1909
bellarda541f292004-04-12 20:39:29 +00001910 u = 0;
1911 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00001912 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001913
1914 return u;
1915}
1916
blueswir18662d652008-10-02 18:32:44 +00001917static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001918{
bellard6a00d602005-11-21 23:25:50 +00001919 CPUState *env = mon_get_cpu();
1920 if (!env)
1921 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001922 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001923}
1924
blueswir18662d652008-10-02 18:32:44 +00001925static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001926{
bellard6a00d602005-11-21 23:25:50 +00001927 CPUState *env = mon_get_cpu();
1928 if (!env)
1929 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001930 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001931}
bellard9fddaa02004-05-21 12:59:32 +00001932
blueswir18662d652008-10-02 18:32:44 +00001933static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001934{
bellard6a00d602005-11-21 23:25:50 +00001935 CPUState *env = mon_get_cpu();
1936 if (!env)
1937 return 0;
1938 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001939}
1940
blueswir18662d652008-10-02 18:32:44 +00001941static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001942{
bellard6a00d602005-11-21 23:25:50 +00001943 CPUState *env = mon_get_cpu();
1944 if (!env)
1945 return 0;
1946 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001947}
1948
blueswir18662d652008-10-02 18:32:44 +00001949static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001950{
bellard6a00d602005-11-21 23:25:50 +00001951 CPUState *env = mon_get_cpu();
1952 if (!env)
1953 return 0;
1954 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001955}
bellarda541f292004-04-12 20:39:29 +00001956#endif
1957
bellarde95c8d52004-09-30 22:22:08 +00001958#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001959#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001960static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001961{
bellard6a00d602005-11-21 23:25:50 +00001962 CPUState *env = mon_get_cpu();
1963 if (!env)
1964 return 0;
1965 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001966}
bellard7b936c02005-10-30 17:05:13 +00001967#endif
bellarde95c8d52004-09-30 22:22:08 +00001968
blueswir18662d652008-10-02 18:32:44 +00001969static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001970{
bellard6a00d602005-11-21 23:25:50 +00001971 CPUState *env = mon_get_cpu();
1972 if (!env)
1973 return 0;
1974 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001975}
1976#endif
1977
blueswir18662d652008-10-02 18:32:44 +00001978static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001979#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001980
1981#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001982 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001983 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001984 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001985
bellard9307c4c2004-04-04 12:57:25 +00001986 { "eax", offsetof(CPUState, regs[0]) },
1987 { "ecx", offsetof(CPUState, regs[1]) },
1988 { "edx", offsetof(CPUState, regs[2]) },
1989 { "ebx", offsetof(CPUState, regs[3]) },
1990 { "esp|sp", offsetof(CPUState, regs[4]) },
1991 { "ebp|fp", offsetof(CPUState, regs[5]) },
1992 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001993 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001994#ifdef TARGET_X86_64
1995 { "r8", offsetof(CPUState, regs[8]) },
1996 { "r9", offsetof(CPUState, regs[9]) },
1997 { "r10", offsetof(CPUState, regs[10]) },
1998 { "r11", offsetof(CPUState, regs[11]) },
1999 { "r12", offsetof(CPUState, regs[12]) },
2000 { "r13", offsetof(CPUState, regs[13]) },
2001 { "r14", offsetof(CPUState, regs[14]) },
2002 { "r15", offsetof(CPUState, regs[15]) },
2003#endif
bellard9307c4c2004-04-04 12:57:25 +00002004 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002005 { "eip", offsetof(CPUState, eip) },
2006 SEG("cs", R_CS)
2007 SEG("ds", R_DS)
2008 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002009 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002010 SEG("fs", R_FS)
2011 SEG("gs", R_GS)
2012 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002013#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002014 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002015 { "r0", offsetof(CPUState, gpr[0]) },
2016 { "r1", offsetof(CPUState, gpr[1]) },
2017 { "r2", offsetof(CPUState, gpr[2]) },
2018 { "r3", offsetof(CPUState, gpr[3]) },
2019 { "r4", offsetof(CPUState, gpr[4]) },
2020 { "r5", offsetof(CPUState, gpr[5]) },
2021 { "r6", offsetof(CPUState, gpr[6]) },
2022 { "r7", offsetof(CPUState, gpr[7]) },
2023 { "r8", offsetof(CPUState, gpr[8]) },
2024 { "r9", offsetof(CPUState, gpr[9]) },
2025 { "r10", offsetof(CPUState, gpr[10]) },
2026 { "r11", offsetof(CPUState, gpr[11]) },
2027 { "r12", offsetof(CPUState, gpr[12]) },
2028 { "r13", offsetof(CPUState, gpr[13]) },
2029 { "r14", offsetof(CPUState, gpr[14]) },
2030 { "r15", offsetof(CPUState, gpr[15]) },
2031 { "r16", offsetof(CPUState, gpr[16]) },
2032 { "r17", offsetof(CPUState, gpr[17]) },
2033 { "r18", offsetof(CPUState, gpr[18]) },
2034 { "r19", offsetof(CPUState, gpr[19]) },
2035 { "r20", offsetof(CPUState, gpr[20]) },
2036 { "r21", offsetof(CPUState, gpr[21]) },
2037 { "r22", offsetof(CPUState, gpr[22]) },
2038 { "r23", offsetof(CPUState, gpr[23]) },
2039 { "r24", offsetof(CPUState, gpr[24]) },
2040 { "r25", offsetof(CPUState, gpr[25]) },
2041 { "r26", offsetof(CPUState, gpr[26]) },
2042 { "r27", offsetof(CPUState, gpr[27]) },
2043 { "r28", offsetof(CPUState, gpr[28]) },
2044 { "r29", offsetof(CPUState, gpr[29]) },
2045 { "r30", offsetof(CPUState, gpr[30]) },
2046 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002047 /* Floating point registers */
2048 { "f0", offsetof(CPUState, fpr[0]) },
2049 { "f1", offsetof(CPUState, fpr[1]) },
2050 { "f2", offsetof(CPUState, fpr[2]) },
2051 { "f3", offsetof(CPUState, fpr[3]) },
2052 { "f4", offsetof(CPUState, fpr[4]) },
2053 { "f5", offsetof(CPUState, fpr[5]) },
2054 { "f6", offsetof(CPUState, fpr[6]) },
2055 { "f7", offsetof(CPUState, fpr[7]) },
2056 { "f8", offsetof(CPUState, fpr[8]) },
2057 { "f9", offsetof(CPUState, fpr[9]) },
2058 { "f10", offsetof(CPUState, fpr[10]) },
2059 { "f11", offsetof(CPUState, fpr[11]) },
2060 { "f12", offsetof(CPUState, fpr[12]) },
2061 { "f13", offsetof(CPUState, fpr[13]) },
2062 { "f14", offsetof(CPUState, fpr[14]) },
2063 { "f15", offsetof(CPUState, fpr[15]) },
2064 { "f16", offsetof(CPUState, fpr[16]) },
2065 { "f17", offsetof(CPUState, fpr[17]) },
2066 { "f18", offsetof(CPUState, fpr[18]) },
2067 { "f19", offsetof(CPUState, fpr[19]) },
2068 { "f20", offsetof(CPUState, fpr[20]) },
2069 { "f21", offsetof(CPUState, fpr[21]) },
2070 { "f22", offsetof(CPUState, fpr[22]) },
2071 { "f23", offsetof(CPUState, fpr[23]) },
2072 { "f24", offsetof(CPUState, fpr[24]) },
2073 { "f25", offsetof(CPUState, fpr[25]) },
2074 { "f26", offsetof(CPUState, fpr[26]) },
2075 { "f27", offsetof(CPUState, fpr[27]) },
2076 { "f28", offsetof(CPUState, fpr[28]) },
2077 { "f29", offsetof(CPUState, fpr[29]) },
2078 { "f30", offsetof(CPUState, fpr[30]) },
2079 { "f31", offsetof(CPUState, fpr[31]) },
2080 { "fpscr", offsetof(CPUState, fpscr) },
2081 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002082 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002083 { "lr", offsetof(CPUState, lr) },
2084 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002085 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002086 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002087 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002088 { "msr", 0, &monitor_get_msr, },
2089 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002090 { "tbu", 0, &monitor_get_tbu, },
2091 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002092#if defined(TARGET_PPC64)
2093 /* Address space register */
2094 { "asr", offsetof(CPUState, asr) },
2095#endif
2096 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002097 { "sdr1", offsetof(CPUState, sdr1) },
2098 { "sr0", offsetof(CPUState, sr[0]) },
2099 { "sr1", offsetof(CPUState, sr[1]) },
2100 { "sr2", offsetof(CPUState, sr[2]) },
2101 { "sr3", offsetof(CPUState, sr[3]) },
2102 { "sr4", offsetof(CPUState, sr[4]) },
2103 { "sr5", offsetof(CPUState, sr[5]) },
2104 { "sr6", offsetof(CPUState, sr[6]) },
2105 { "sr7", offsetof(CPUState, sr[7]) },
2106 { "sr8", offsetof(CPUState, sr[8]) },
2107 { "sr9", offsetof(CPUState, sr[9]) },
2108 { "sr10", offsetof(CPUState, sr[10]) },
2109 { "sr11", offsetof(CPUState, sr[11]) },
2110 { "sr12", offsetof(CPUState, sr[12]) },
2111 { "sr13", offsetof(CPUState, sr[13]) },
2112 { "sr14", offsetof(CPUState, sr[14]) },
2113 { "sr15", offsetof(CPUState, sr[15]) },
2114 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002115#elif defined(TARGET_SPARC)
2116 { "g0", offsetof(CPUState, gregs[0]) },
2117 { "g1", offsetof(CPUState, gregs[1]) },
2118 { "g2", offsetof(CPUState, gregs[2]) },
2119 { "g3", offsetof(CPUState, gregs[3]) },
2120 { "g4", offsetof(CPUState, gregs[4]) },
2121 { "g5", offsetof(CPUState, gregs[5]) },
2122 { "g6", offsetof(CPUState, gregs[6]) },
2123 { "g7", offsetof(CPUState, gregs[7]) },
2124 { "o0", 0, monitor_get_reg },
2125 { "o1", 1, monitor_get_reg },
2126 { "o2", 2, monitor_get_reg },
2127 { "o3", 3, monitor_get_reg },
2128 { "o4", 4, monitor_get_reg },
2129 { "o5", 5, monitor_get_reg },
2130 { "o6", 6, monitor_get_reg },
2131 { "o7", 7, monitor_get_reg },
2132 { "l0", 8, monitor_get_reg },
2133 { "l1", 9, monitor_get_reg },
2134 { "l2", 10, monitor_get_reg },
2135 { "l3", 11, monitor_get_reg },
2136 { "l4", 12, monitor_get_reg },
2137 { "l5", 13, monitor_get_reg },
2138 { "l6", 14, monitor_get_reg },
2139 { "l7", 15, monitor_get_reg },
2140 { "i0", 16, monitor_get_reg },
2141 { "i1", 17, monitor_get_reg },
2142 { "i2", 18, monitor_get_reg },
2143 { "i3", 19, monitor_get_reg },
2144 { "i4", 20, monitor_get_reg },
2145 { "i5", 21, monitor_get_reg },
2146 { "i6", 22, monitor_get_reg },
2147 { "i7", 23, monitor_get_reg },
2148 { "pc", offsetof(CPUState, pc) },
2149 { "npc", offsetof(CPUState, npc) },
2150 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002151#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002152 { "psr", 0, &monitor_get_psr, },
2153 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002154#endif
bellarde95c8d52004-09-30 22:22:08 +00002155 { "tbr", offsetof(CPUState, tbr) },
2156 { "fsr", offsetof(CPUState, fsr) },
2157 { "f0", offsetof(CPUState, fpr[0]) },
2158 { "f1", offsetof(CPUState, fpr[1]) },
2159 { "f2", offsetof(CPUState, fpr[2]) },
2160 { "f3", offsetof(CPUState, fpr[3]) },
2161 { "f4", offsetof(CPUState, fpr[4]) },
2162 { "f5", offsetof(CPUState, fpr[5]) },
2163 { "f6", offsetof(CPUState, fpr[6]) },
2164 { "f7", offsetof(CPUState, fpr[7]) },
2165 { "f8", offsetof(CPUState, fpr[8]) },
2166 { "f9", offsetof(CPUState, fpr[9]) },
2167 { "f10", offsetof(CPUState, fpr[10]) },
2168 { "f11", offsetof(CPUState, fpr[11]) },
2169 { "f12", offsetof(CPUState, fpr[12]) },
2170 { "f13", offsetof(CPUState, fpr[13]) },
2171 { "f14", offsetof(CPUState, fpr[14]) },
2172 { "f15", offsetof(CPUState, fpr[15]) },
2173 { "f16", offsetof(CPUState, fpr[16]) },
2174 { "f17", offsetof(CPUState, fpr[17]) },
2175 { "f18", offsetof(CPUState, fpr[18]) },
2176 { "f19", offsetof(CPUState, fpr[19]) },
2177 { "f20", offsetof(CPUState, fpr[20]) },
2178 { "f21", offsetof(CPUState, fpr[21]) },
2179 { "f22", offsetof(CPUState, fpr[22]) },
2180 { "f23", offsetof(CPUState, fpr[23]) },
2181 { "f24", offsetof(CPUState, fpr[24]) },
2182 { "f25", offsetof(CPUState, fpr[25]) },
2183 { "f26", offsetof(CPUState, fpr[26]) },
2184 { "f27", offsetof(CPUState, fpr[27]) },
2185 { "f28", offsetof(CPUState, fpr[28]) },
2186 { "f29", offsetof(CPUState, fpr[29]) },
2187 { "f30", offsetof(CPUState, fpr[30]) },
2188 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002189#ifdef TARGET_SPARC64
2190 { "f32", offsetof(CPUState, fpr[32]) },
2191 { "f34", offsetof(CPUState, fpr[34]) },
2192 { "f36", offsetof(CPUState, fpr[36]) },
2193 { "f38", offsetof(CPUState, fpr[38]) },
2194 { "f40", offsetof(CPUState, fpr[40]) },
2195 { "f42", offsetof(CPUState, fpr[42]) },
2196 { "f44", offsetof(CPUState, fpr[44]) },
2197 { "f46", offsetof(CPUState, fpr[46]) },
2198 { "f48", offsetof(CPUState, fpr[48]) },
2199 { "f50", offsetof(CPUState, fpr[50]) },
2200 { "f52", offsetof(CPUState, fpr[52]) },
2201 { "f54", offsetof(CPUState, fpr[54]) },
2202 { "f56", offsetof(CPUState, fpr[56]) },
2203 { "f58", offsetof(CPUState, fpr[58]) },
2204 { "f60", offsetof(CPUState, fpr[60]) },
2205 { "f62", offsetof(CPUState, fpr[62]) },
2206 { "asi", offsetof(CPUState, asi) },
2207 { "pstate", offsetof(CPUState, pstate) },
2208 { "cansave", offsetof(CPUState, cansave) },
2209 { "canrestore", offsetof(CPUState, canrestore) },
2210 { "otherwin", offsetof(CPUState, otherwin) },
2211 { "wstate", offsetof(CPUState, wstate) },
2212 { "cleanwin", offsetof(CPUState, cleanwin) },
2213 { "fprs", offsetof(CPUState, fprs) },
2214#endif
bellard9307c4c2004-04-04 12:57:25 +00002215#endif
2216 { NULL },
2217};
2218
aliguori376253e2009-03-05 23:01:23 +00002219static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002220{
aliguori376253e2009-03-05 23:01:23 +00002221 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002222 longjmp(expr_env, 1);
2223}
2224
bellard6a00d602005-11-21 23:25:50 +00002225/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002226static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002227{
blueswir18662d652008-10-02 18:32:44 +00002228 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002229 void *ptr;
2230
bellard9307c4c2004-04-04 12:57:25 +00002231 for(md = monitor_defs; md->name != NULL; md++) {
2232 if (compare_cmd(name, md->name)) {
2233 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002234 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002235 } else {
bellard6a00d602005-11-21 23:25:50 +00002236 CPUState *env = mon_get_cpu();
2237 if (!env)
2238 return -2;
2239 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002240 switch(md->type) {
2241 case MD_I32:
2242 *pval = *(int32_t *)ptr;
2243 break;
2244 case MD_TLONG:
2245 *pval = *(target_long *)ptr;
2246 break;
2247 default:
2248 *pval = 0;
2249 break;
2250 }
bellard9307c4c2004-04-04 12:57:25 +00002251 }
2252 return 0;
2253 }
2254 }
2255 return -1;
2256}
2257
2258static void next(void)
2259{
Blue Swirl660f11b2009-07-31 21:16:51 +00002260 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002261 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002262 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002263 pch++;
2264 }
2265}
2266
aliguori376253e2009-03-05 23:01:23 +00002267static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002268
aliguori376253e2009-03-05 23:01:23 +00002269static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002270{
blueswir1c2efc952007-09-25 17:28:42 +00002271 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002272 char *p;
bellard6a00d602005-11-21 23:25:50 +00002273 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002274
2275 switch(*pch) {
2276 case '+':
2277 next();
aliguori376253e2009-03-05 23:01:23 +00002278 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002279 break;
2280 case '-':
2281 next();
aliguori376253e2009-03-05 23:01:23 +00002282 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002283 break;
2284 case '~':
2285 next();
aliguori376253e2009-03-05 23:01:23 +00002286 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002287 break;
2288 case '(':
2289 next();
aliguori376253e2009-03-05 23:01:23 +00002290 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002291 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002292 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002293 }
2294 next();
2295 break;
bellard81d09122004-07-14 17:21:37 +00002296 case '\'':
2297 pch++;
2298 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002299 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002300 n = *pch;
2301 pch++;
2302 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002303 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002304 next();
2305 break;
bellard9307c4c2004-04-04 12:57:25 +00002306 case '$':
2307 {
2308 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002309 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002310
bellard9307c4c2004-04-04 12:57:25 +00002311 pch++;
2312 q = buf;
2313 while ((*pch >= 'a' && *pch <= 'z') ||
2314 (*pch >= 'A' && *pch <= 'Z') ||
2315 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002316 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002317 if ((q - buf) < sizeof(buf) - 1)
2318 *q++ = *pch;
2319 pch++;
2320 }
blueswir1cd390082008-11-16 13:53:32 +00002321 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002322 pch++;
2323 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002324 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002325 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002326 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002327 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002328 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002329 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002330 }
2331 break;
2332 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002333 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002334 n = 0;
2335 break;
2336 default:
blueswir17743e582007-09-24 18:39:04 +00002337#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002338 n = strtoull(pch, &p, 0);
2339#else
bellard9307c4c2004-04-04 12:57:25 +00002340 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002341#endif
bellard9307c4c2004-04-04 12:57:25 +00002342 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002343 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002344 }
2345 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002346 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002347 pch++;
2348 break;
2349 }
2350 return n;
2351}
2352
2353
aliguori376253e2009-03-05 23:01:23 +00002354static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002355{
blueswir1c2efc952007-09-25 17:28:42 +00002356 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002357 int op;
ths3b46e622007-09-17 08:09:54 +00002358
aliguori376253e2009-03-05 23:01:23 +00002359 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002360 for(;;) {
2361 op = *pch;
2362 if (op != '*' && op != '/' && op != '%')
2363 break;
2364 next();
aliguori376253e2009-03-05 23:01:23 +00002365 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002366 switch(op) {
2367 default:
2368 case '*':
2369 val *= val2;
2370 break;
2371 case '/':
2372 case '%':
ths5fafdf22007-09-16 21:08:06 +00002373 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002374 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002375 if (op == '/')
2376 val /= val2;
2377 else
2378 val %= val2;
2379 break;
2380 }
2381 }
2382 return val;
2383}
2384
aliguori376253e2009-03-05 23:01:23 +00002385static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002386{
blueswir1c2efc952007-09-25 17:28:42 +00002387 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002388 int op;
bellard9307c4c2004-04-04 12:57:25 +00002389
aliguori376253e2009-03-05 23:01:23 +00002390 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002391 for(;;) {
2392 op = *pch;
2393 if (op != '&' && op != '|' && op != '^')
2394 break;
2395 next();
aliguori376253e2009-03-05 23:01:23 +00002396 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002397 switch(op) {
2398 default:
2399 case '&':
2400 val &= val2;
2401 break;
2402 case '|':
2403 val |= val2;
2404 break;
2405 case '^':
2406 val ^= val2;
2407 break;
2408 }
2409 }
2410 return val;
2411}
2412
aliguori376253e2009-03-05 23:01:23 +00002413static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002414{
blueswir1c2efc952007-09-25 17:28:42 +00002415 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002416 int op;
bellard9307c4c2004-04-04 12:57:25 +00002417
aliguori376253e2009-03-05 23:01:23 +00002418 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002419 for(;;) {
2420 op = *pch;
2421 if (op != '+' && op != '-')
2422 break;
2423 next();
aliguori376253e2009-03-05 23:01:23 +00002424 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002425 if (op == '+')
2426 val += val2;
2427 else
2428 val -= val2;
2429 }
2430 return val;
2431}
2432
aliguori376253e2009-03-05 23:01:23 +00002433static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002434{
2435 pch = *pp;
2436 if (setjmp(expr_env)) {
2437 *pp = pch;
2438 return -1;
2439 }
blueswir1cd390082008-11-16 13:53:32 +00002440 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002441 pch++;
aliguori376253e2009-03-05 23:01:23 +00002442 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002443 *pp = pch;
2444 return 0;
2445}
2446
2447static int get_str(char *buf, int buf_size, const char **pp)
2448{
2449 const char *p;
2450 char *q;
2451 int c;
2452
bellard81d09122004-07-14 17:21:37 +00002453 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002454 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002455 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002456 p++;
2457 if (*p == '\0') {
2458 fail:
bellard81d09122004-07-14 17:21:37 +00002459 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002460 *pp = p;
2461 return -1;
2462 }
bellard9307c4c2004-04-04 12:57:25 +00002463 if (*p == '\"') {
2464 p++;
2465 while (*p != '\0' && *p != '\"') {
2466 if (*p == '\\') {
2467 p++;
2468 c = *p++;
2469 switch(c) {
2470 case 'n':
2471 c = '\n';
2472 break;
2473 case 'r':
2474 c = '\r';
2475 break;
2476 case '\\':
2477 case '\'':
2478 case '\"':
2479 break;
2480 default:
2481 qemu_printf("unsupported escape code: '\\%c'\n", c);
2482 goto fail;
2483 }
2484 if ((q - buf) < buf_size - 1) {
2485 *q++ = c;
2486 }
2487 } else {
2488 if ((q - buf) < buf_size - 1) {
2489 *q++ = *p;
2490 }
2491 p++;
2492 }
2493 }
2494 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002495 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002496 goto fail;
2497 }
2498 p++;
2499 } else {
blueswir1cd390082008-11-16 13:53:32 +00002500 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002501 if ((q - buf) < buf_size - 1) {
2502 *q++ = *p;
2503 }
2504 p++;
2505 }
bellard9307c4c2004-04-04 12:57:25 +00002506 }
bellard81d09122004-07-14 17:21:37 +00002507 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002508 *pp = p;
2509 return 0;
2510}
2511
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002512/*
2513 * Store the command-name in cmdname, and return a pointer to
2514 * the remaining of the command string.
2515 */
2516static const char *get_command_name(const char *cmdline,
2517 char *cmdname, size_t nlen)
2518{
2519 size_t len;
2520 const char *p, *pstart;
2521
2522 p = cmdline;
2523 while (qemu_isspace(*p))
2524 p++;
2525 if (*p == '\0')
2526 return NULL;
2527 pstart = p;
2528 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2529 p++;
2530 len = p - pstart;
2531 if (len > nlen - 1)
2532 len = nlen - 1;
2533 memcpy(cmdname, pstart, len);
2534 cmdname[len] = '\0';
2535 return p;
2536}
2537
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002538/**
2539 * Read key of 'type' into 'key' and return the current
2540 * 'type' pointer.
2541 */
2542static char *key_get_info(const char *type, char **key)
2543{
2544 size_t len;
2545 char *p, *str;
2546
2547 if (*type == ',')
2548 type++;
2549
2550 p = strchr(type, ':');
2551 if (!p) {
2552 *key = NULL;
2553 return NULL;
2554 }
2555 len = p - type;
2556
2557 str = qemu_malloc(len + 1);
2558 memcpy(str, type, len);
2559 str[len] = '\0';
2560
2561 *key = str;
2562 return ++p;
2563}
2564
bellard9307c4c2004-04-04 12:57:25 +00002565static int default_fmt_format = 'x';
2566static int default_fmt_size = 4;
2567
2568#define MAX_ARGS 16
2569
aliguori376253e2009-03-05 23:01:23 +00002570static void monitor_handle_command(Monitor *mon, const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002571{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002572 const char *p, *typestr;
2573 int c, nb_args, i, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002574 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002575 char cmdname[256];
2576 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002577 char *key;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002578 QDict *qdict;
bellard9307c4c2004-04-04 12:57:25 +00002579 void *str_allocated[MAX_ARGS];
2580 void *args[MAX_ARGS];
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03002581 void (*handler_d)(Monitor *mon, const QDict *qdict);
aliguori376253e2009-03-05 23:01:23 +00002582 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2583 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2584 void *arg3);
2585 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2586 void *arg3, void *arg4);
2587 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2588 void *arg3, void *arg4, void *arg5);
2589 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2590 void *arg3, void *arg4, void *arg5, void *arg6);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002591 void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2592 void *arg3, void *arg4, void *arg5, void *arg6,
2593 void *arg7);
2594 void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2595 void *arg3, void *arg4, void *arg5, void *arg6,
2596 void *arg7, void *arg8);
2597 void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2598 void *arg3, void *arg4, void *arg5, void *arg6,
2599 void *arg7, void *arg8, void *arg9);
bellard9dc39cb2004-03-14 21:38:27 +00002600
2601#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002602 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002603#endif
ths3b46e622007-09-17 08:09:54 +00002604
bellard9307c4c2004-04-04 12:57:25 +00002605 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002606 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2607 if (!p)
bellard9dc39cb2004-03-14 21:38:27 +00002608 return;
ths3b46e622007-09-17 08:09:54 +00002609
bellard9307c4c2004-04-04 12:57:25 +00002610 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002611 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002612 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002613 break;
bellard9dc39cb2004-03-14 21:38:27 +00002614 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002615
2616 if (cmd->name == NULL) {
2617 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2618 return;
2619 }
bellard9307c4c2004-04-04 12:57:25 +00002620
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002621 qdict = qdict_new();
2622
bellard9307c4c2004-04-04 12:57:25 +00002623 for(i = 0; i < MAX_ARGS; i++)
2624 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002625
bellard9307c4c2004-04-04 12:57:25 +00002626 /* parse the parameters */
2627 typestr = cmd->args_type;
2628 nb_args = 0;
2629 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002630 typestr = key_get_info(typestr, &key);
2631 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002632 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002633 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002634 typestr++;
2635 switch(c) {
2636 case 'F':
bellard81d09122004-07-14 17:21:37 +00002637 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002638 case 's':
2639 {
2640 int ret;
2641 char *str;
ths3b46e622007-09-17 08:09:54 +00002642
blueswir1cd390082008-11-16 13:53:32 +00002643 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002644 p++;
2645 if (*typestr == '?') {
2646 typestr++;
2647 if (*p == '\0') {
2648 /* no optional string: NULL argument */
2649 str = NULL;
2650 goto add_str;
2651 }
2652 }
2653 ret = get_str(buf, sizeof(buf), &p);
2654 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002655 switch(c) {
2656 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002657 monitor_printf(mon, "%s: filename expected\n",
2658 cmdname);
bellard81d09122004-07-14 17:21:37 +00002659 break;
2660 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002661 monitor_printf(mon, "%s: block device name expected\n",
2662 cmdname);
bellard81d09122004-07-14 17:21:37 +00002663 break;
2664 default:
aliguori376253e2009-03-05 23:01:23 +00002665 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002666 break;
2667 }
bellard9307c4c2004-04-04 12:57:25 +00002668 goto fail;
2669 }
2670 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002671 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002672 str_allocated[nb_args] = str;
2673 add_str:
2674 if (nb_args >= MAX_ARGS) {
2675 error_args:
aliguori376253e2009-03-05 23:01:23 +00002676 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002677 goto fail;
2678 }
2679 args[nb_args++] = str;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002680 if (str)
2681 qdict_put(qdict, key, qstring_from_str(str));
bellard9307c4c2004-04-04 12:57:25 +00002682 }
2683 break;
2684 case '/':
2685 {
2686 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002687
blueswir1cd390082008-11-16 13:53:32 +00002688 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002689 p++;
2690 if (*p == '/') {
2691 /* format found */
2692 p++;
2693 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002694 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002695 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002696 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002697 count = count * 10 + (*p - '0');
2698 p++;
2699 }
2700 }
2701 size = -1;
2702 format = -1;
2703 for(;;) {
2704 switch(*p) {
2705 case 'o':
2706 case 'd':
2707 case 'u':
2708 case 'x':
2709 case 'i':
2710 case 'c':
2711 format = *p++;
2712 break;
2713 case 'b':
2714 size = 1;
2715 p++;
2716 break;
2717 case 'h':
2718 size = 2;
2719 p++;
2720 break;
2721 case 'w':
2722 size = 4;
2723 p++;
2724 break;
2725 case 'g':
2726 case 'L':
2727 size = 8;
2728 p++;
2729 break;
2730 default:
2731 goto next;
2732 }
2733 }
2734 next:
blueswir1cd390082008-11-16 13:53:32 +00002735 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002736 monitor_printf(mon, "invalid char in format: '%c'\n",
2737 *p);
bellard9307c4c2004-04-04 12:57:25 +00002738 goto fail;
2739 }
bellard9307c4c2004-04-04 12:57:25 +00002740 if (format < 0)
2741 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002742 if (format != 'i') {
2743 /* for 'i', not specifying a size gives -1 as size */
2744 if (size < 0)
2745 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002746 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002747 }
bellard9307c4c2004-04-04 12:57:25 +00002748 default_fmt_format = format;
2749 } else {
2750 count = 1;
2751 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002752 if (format != 'i') {
2753 size = default_fmt_size;
2754 } else {
2755 size = -1;
2756 }
bellard9307c4c2004-04-04 12:57:25 +00002757 }
2758 if (nb_args + 3 > MAX_ARGS)
2759 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002760 args[nb_args++] = (void*)(long)count;
2761 args[nb_args++] = (void*)(long)format;
2762 args[nb_args++] = (void*)(long)size;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002763 qdict_put(qdict, "count", qint_from_int(count));
2764 qdict_put(qdict, "format", qint_from_int(format));
2765 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002766 }
2767 break;
2768 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002769 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002770 {
blueswir1c2efc952007-09-25 17:28:42 +00002771 int64_t val;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002772 int dict_add = 1;
blueswir17743e582007-09-24 18:39:04 +00002773
blueswir1cd390082008-11-16 13:53:32 +00002774 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002775 p++;
bellard34405572004-06-08 00:55:58 +00002776 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002777 if (*typestr == '?') {
2778 if (*p == '\0')
2779 has_arg = 0;
2780 else
2781 has_arg = 1;
2782 } else {
2783 if (*p == '.') {
2784 p++;
blueswir1cd390082008-11-16 13:53:32 +00002785 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002786 p++;
2787 has_arg = 1;
2788 } else {
2789 has_arg = 0;
2790 }
2791 }
bellard13224a82006-07-14 22:03:35 +00002792 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002793 if (nb_args >= MAX_ARGS)
2794 goto error_args;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002795 dict_add = has_arg;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002796 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002797 if (!has_arg) {
2798 if (nb_args >= MAX_ARGS)
2799 goto error_args;
2800 val = -1;
2801 goto add_num;
2802 }
2803 }
aliguori376253e2009-03-05 23:01:23 +00002804 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002805 goto fail;
2806 add_num:
bellard92a31b12005-02-10 22:00:52 +00002807 if (c == 'i') {
2808 if (nb_args >= MAX_ARGS)
2809 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002810 args[nb_args++] = (void *)(long)val;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002811 if (dict_add)
2812 qdict_put(qdict, key, qint_from_int(val));
bellard92a31b12005-02-10 22:00:52 +00002813 } else {
2814 if ((nb_args + 1) >= MAX_ARGS)
2815 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002816#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002817 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002818#else
2819 args[nb_args++] = (void *)0;
2820#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002821 args[nb_args++] = (void *)(long)(val & 0xffffffff);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002822 qdict_put(qdict, key, qint_from_int(val));
bellard92a31b12005-02-10 22:00:52 +00002823 }
bellard9307c4c2004-04-04 12:57:25 +00002824 }
2825 break;
2826 case '-':
2827 {
2828 int has_option;
2829 /* option */
ths3b46e622007-09-17 08:09:54 +00002830
bellard9307c4c2004-04-04 12:57:25 +00002831 c = *typestr++;
2832 if (c == '\0')
2833 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002834 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002835 p++;
2836 has_option = 0;
2837 if (*p == '-') {
2838 p++;
2839 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002840 monitor_printf(mon, "%s: unsupported option -%c\n",
2841 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002842 goto fail;
2843 }
2844 p++;
2845 has_option = 1;
2846 }
2847 if (nb_args >= MAX_ARGS)
2848 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002849 args[nb_args++] = (void *)(long)has_option;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002850 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00002851 }
2852 break;
2853 default:
2854 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002855 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002856 goto fail;
2857 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002858 qemu_free(key);
2859 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002860 }
2861 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002862 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002863 p++;
2864 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002865 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2866 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002867 goto fail;
2868 }
2869
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002870 qemu_errors_to_mon(mon);
bellard9307c4c2004-04-04 12:57:25 +00002871 switch(nb_args) {
2872 case 0:
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002873 case 1:
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002874 case 2:
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03002875 handler_d = cmd->handler;
2876 handler_d(mon, qdict);
bellard9307c4c2004-04-04 12:57:25 +00002877 break;
bellard9307c4c2004-04-04 12:57:25 +00002878 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002879 handler_3 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002880 handler_3(mon, args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002881 break;
2882 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002883 handler_4 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002884 handler_4(mon, args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002885 break;
2886 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002887 handler_5 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002888 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002889 break;
bellard34405572004-06-08 00:55:58 +00002890 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002891 handler_6 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002892 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002893 break;
bellardec36b692006-07-16 18:57:03 +00002894 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002895 handler_7 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002896 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2897 args[6]);
bellardec36b692006-07-16 18:57:03 +00002898 break;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002899 case 8:
2900 handler_8 = cmd->handler;
2901 handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2902 args[6], args[7]);
2903 break;
2904 case 9:
2905 handler_9 = cmd->handler;
2906 handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2907 args[6], args[7], args[8]);
2908 break;
2909 case 10:
2910 handler_10 = cmd->handler;
2911 handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2912 args[6], args[7], args[8], args[9]);
2913 break;
bellard9307c4c2004-04-04 12:57:25 +00002914 default:
aliguori376253e2009-03-05 23:01:23 +00002915 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002916 break;
bellard9307c4c2004-04-04 12:57:25 +00002917 }
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002918 qemu_errors_to_previous();
2919
bellard9307c4c2004-04-04 12:57:25 +00002920 fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002921 qemu_free(key);
bellard9307c4c2004-04-04 12:57:25 +00002922 for(i = 0; i < MAX_ARGS; i++)
2923 qemu_free(str_allocated[i]);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002924 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002925}
2926
bellard81d09122004-07-14 17:21:37 +00002927static void cmd_completion(const char *name, const char *list)
2928{
2929 const char *p, *pstart;
2930 char cmd[128];
2931 int len;
2932
2933 p = list;
2934 for(;;) {
2935 pstart = p;
2936 p = strchr(p, '|');
2937 if (!p)
2938 p = pstart + strlen(pstart);
2939 len = p - pstart;
2940 if (len > sizeof(cmd) - 2)
2941 len = sizeof(cmd) - 2;
2942 memcpy(cmd, pstart, len);
2943 cmd[len] = '\0';
2944 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002945 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002946 }
2947 if (*p == '\0')
2948 break;
2949 p++;
2950 }
2951}
2952
2953static void file_completion(const char *input)
2954{
2955 DIR *ffs;
2956 struct dirent *d;
2957 char path[1024];
2958 char file[1024], file_prefix[1024];
2959 int input_path_len;
2960 const char *p;
2961
ths5fafdf22007-09-16 21:08:06 +00002962 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002963 if (!p) {
2964 input_path_len = 0;
2965 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002966 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002967 } else {
2968 input_path_len = p - input + 1;
2969 memcpy(path, input, input_path_len);
2970 if (input_path_len > sizeof(path) - 1)
2971 input_path_len = sizeof(path) - 1;
2972 path[input_path_len] = '\0';
2973 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2974 }
2975#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002976 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2977 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002978#endif
2979 ffs = opendir(path);
2980 if (!ffs)
2981 return;
2982 for(;;) {
2983 struct stat sb;
2984 d = readdir(ffs);
2985 if (!d)
2986 break;
2987 if (strstart(d->d_name, file_prefix, NULL)) {
2988 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002989 if (input_path_len < sizeof(file))
2990 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2991 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002992 /* stat the file to find out if it's a directory.
2993 * In that case add a slash to speed up typing long paths
2994 */
2995 stat(file, &sb);
2996 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002997 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00002998 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002999 }
3000 }
3001 closedir(ffs);
3002}
3003
aliguori51de9762009-03-05 23:00:43 +00003004static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003005{
aliguori51de9762009-03-05 23:00:43 +00003006 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003007 const char *input = opaque;
3008
3009 if (input[0] == '\0' ||
3010 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003011 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003012 }
3013}
3014
3015/* NOTE: this parser is an approximate form of the real command parser */
3016static void parse_cmdline(const char *cmdline,
3017 int *pnb_args, char **args)
3018{
3019 const char *p;
3020 int nb_args, ret;
3021 char buf[1024];
3022
3023 p = cmdline;
3024 nb_args = 0;
3025 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003026 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003027 p++;
3028 if (*p == '\0')
3029 break;
3030 if (nb_args >= MAX_ARGS)
3031 break;
3032 ret = get_str(buf, sizeof(buf), &p);
3033 args[nb_args] = qemu_strdup(buf);
3034 nb_args++;
3035 if (ret < 0)
3036 break;
3037 }
3038 *pnb_args = nb_args;
3039}
3040
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003041static const char *next_arg_type(const char *typestr)
3042{
3043 const char *p = strchr(typestr, ':');
3044 return (p != NULL ? ++p : typestr);
3045}
3046
aliguori4c36ba32009-03-05 23:01:37 +00003047static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003048{
3049 const char *cmdname;
3050 char *args[MAX_ARGS];
3051 int nb_args, i, len;
3052 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00003053 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003054 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003055
3056 parse_cmdline(cmdline, &nb_args, args);
3057#ifdef DEBUG_COMPLETION
3058 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003059 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003060 }
3061#endif
3062
3063 /* if the line ends with a space, it means we want to complete the
3064 next arg */
3065 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003066 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003067 if (nb_args >= MAX_ARGS)
3068 return;
3069 args[nb_args++] = qemu_strdup("");
3070 }
3071 if (nb_args <= 1) {
3072 /* command completion */
3073 if (nb_args == 0)
3074 cmdname = "";
3075 else
3076 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003077 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003078 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003079 cmd_completion(cmdname, cmd->name);
3080 }
3081 } else {
3082 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003083 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003084 if (compare_cmd(args[0], cmd->name))
3085 goto found;
3086 }
3087 return;
3088 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003089 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003090 for(i = 0; i < nb_args - 2; i++) {
3091 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003092 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003093 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003094 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003095 }
3096 }
3097 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003098 if (*ptype == '-' && ptype[1] != '\0') {
3099 ptype += 2;
3100 }
bellard81d09122004-07-14 17:21:37 +00003101 switch(*ptype) {
3102 case 'F':
3103 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003104 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003105 file_completion(str);
3106 break;
3107 case 'B':
3108 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003109 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003110 bdrv_iterate(block_completion_it, (void *)str);
3111 break;
bellard7fe48482004-10-09 18:08:01 +00003112 case 's':
3113 /* XXX: more generic ? */
3114 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003115 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003116 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3117 cmd_completion(str, cmd->name);
3118 }
bellard64866c32006-05-07 18:03:31 +00003119 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003120 char *sep = strrchr(str, '-');
3121 if (sep)
3122 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003123 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003124 for(key = key_defs; key->name != NULL; key++) {
3125 cmd_completion(str, key->name);
3126 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003127 } else if (!strcmp(cmd->name, "help|?")) {
3128 readline_set_completion_index(cur_mon->rs, strlen(str));
3129 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3130 cmd_completion(str, cmd->name);
3131 }
bellard7fe48482004-10-09 18:08:01 +00003132 }
3133 break;
bellard81d09122004-07-14 17:21:37 +00003134 default:
3135 break;
3136 }
3137 }
3138 for(i = 0; i < nb_args; i++)
3139 qemu_free(args[i]);
3140}
3141
aliguori731b0362009-03-05 23:01:42 +00003142static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003143{
aliguori731b0362009-03-05 23:01:42 +00003144 Monitor *mon = opaque;
3145
3146 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003147}
3148
aliguori731b0362009-03-05 23:01:42 +00003149static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003150{
aliguori731b0362009-03-05 23:01:42 +00003151 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003152 int i;
aliguori376253e2009-03-05 23:01:23 +00003153
aliguori731b0362009-03-05 23:01:42 +00003154 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003155
aliguoricde76ee2009-03-05 23:01:51 +00003156 if (cur_mon->rs) {
3157 for (i = 0; i < size; i++)
3158 readline_handle_byte(cur_mon->rs, buf[i]);
3159 } else {
3160 if (size == 0 || buf[size - 1] != 0)
3161 monitor_printf(cur_mon, "corrupted command\n");
3162 else
3163 monitor_handle_command(cur_mon, (char *)buf);
3164 }
aliguori731b0362009-03-05 23:01:42 +00003165
3166 cur_mon = old_mon;
3167}
aliguorid8f44602008-10-06 13:52:44 +00003168
aliguori376253e2009-03-05 23:01:23 +00003169static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003170{
aliguori731b0362009-03-05 23:01:42 +00003171 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003172 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003173 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003174}
3175
aliguoricde76ee2009-03-05 23:01:51 +00003176int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003177{
aliguoricde76ee2009-03-05 23:01:51 +00003178 if (!mon->rs)
3179 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003180 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003181 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003182}
3183
aliguori376253e2009-03-05 23:01:23 +00003184void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003185{
aliguoricde76ee2009-03-05 23:01:51 +00003186 if (!mon->rs)
3187 return;
aliguori731b0362009-03-05 23:01:42 +00003188 if (--mon->suspend_cnt == 0)
3189 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003190}
3191
aliguori731b0362009-03-05 23:01:42 +00003192static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003193{
aliguori376253e2009-03-05 23:01:23 +00003194 Monitor *mon = opaque;
3195
aliguori2724b182009-03-05 23:01:47 +00003196 switch (event) {
3197 case CHR_EVENT_MUX_IN:
3198 readline_restart(mon->rs);
3199 monitor_resume(mon);
3200 monitor_flush(mon);
3201 break;
ths86e94de2007-01-05 22:01:59 +00003202
aliguori2724b182009-03-05 23:01:47 +00003203 case CHR_EVENT_MUX_OUT:
3204 if (mon->suspend_cnt == 0)
3205 monitor_printf(mon, "\n");
3206 monitor_flush(mon);
3207 monitor_suspend(mon);
3208 break;
3209
3210 case CHR_EVENT_RESET:
3211 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3212 "information\n", QEMU_VERSION);
3213 if (mon->chr->focus == 0)
3214 readline_show_prompt(mon->rs);
3215 break;
3216 }
ths86e94de2007-01-05 22:01:59 +00003217}
3218
aliguori76655d62009-03-06 20:27:37 +00003219
3220/*
3221 * Local variables:
3222 * c-indent-level: 4
3223 * c-basic-offset: 4
3224 * tab-width: 8
3225 * End:
3226 */
3227
aliguori731b0362009-03-05 23:01:42 +00003228void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003229{
aliguori731b0362009-03-05 23:01:42 +00003230 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003231 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003232
3233 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003234 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003235 is_first_init = 0;
3236 }
aliguori87127162009-03-05 23:01:29 +00003237
3238 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003239
aliguori87127162009-03-05 23:01:29 +00003240 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003241 mon->flags = flags;
aliguori2724b182009-03-05 23:01:47 +00003242 if (mon->chr->focus != 0)
3243 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
aliguoricde76ee2009-03-05 23:01:51 +00003244 if (flags & MONITOR_USE_READLINE) {
3245 mon->rs = readline_init(mon, monitor_find_completion);
3246 monitor_read_command(mon, 0);
3247 }
aliguori87127162009-03-05 23:01:29 +00003248
aliguori731b0362009-03-05 23:01:42 +00003249 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3250 mon);
aliguori87127162009-03-05 23:01:29 +00003251
3252 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003253 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003254 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003255}
3256
aliguori376253e2009-03-05 23:01:23 +00003257static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003258{
aliguoribb5fc202009-03-05 23:01:15 +00003259 BlockDriverState *bs = opaque;
3260 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003261
aliguoribb5fc202009-03-05 23:01:15 +00003262 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003263 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003264 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003265 }
aliguori731b0362009-03-05 23:01:42 +00003266 if (mon->password_completion_cb)
3267 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003268
aliguori731b0362009-03-05 23:01:42 +00003269 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003270}
aliguoric0f4ce72009-03-05 23:01:01 +00003271
aliguori376253e2009-03-05 23:01:23 +00003272void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003273 BlockDriverCompletionFunc *completion_cb,
3274 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003275{
aliguoricde76ee2009-03-05 23:01:51 +00003276 int err;
3277
aliguoribb5fc202009-03-05 23:01:15 +00003278 if (!bdrv_key_required(bs)) {
3279 if (completion_cb)
3280 completion_cb(opaque, 0);
3281 return;
3282 }
aliguoric0f4ce72009-03-05 23:01:01 +00003283
aliguori376253e2009-03-05 23:01:23 +00003284 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3285 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003286
aliguori731b0362009-03-05 23:01:42 +00003287 mon->password_completion_cb = completion_cb;
3288 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003289
aliguoricde76ee2009-03-05 23:01:51 +00003290 err = monitor_read_password(mon, bdrv_password_cb, bs);
3291
3292 if (err && completion_cb)
3293 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003294}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003295
3296typedef struct QemuErrorSink QemuErrorSink;
3297struct QemuErrorSink {
3298 enum {
3299 ERR_SINK_FILE,
3300 ERR_SINK_MONITOR,
3301 } dest;
3302 union {
3303 FILE *fp;
3304 Monitor *mon;
3305 };
3306 QemuErrorSink *previous;
3307};
3308
Blue Swirl528e93a2009-08-31 15:14:40 +00003309static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003310
3311void qemu_errors_to_file(FILE *fp)
3312{
3313 QemuErrorSink *sink;
3314
3315 sink = qemu_mallocz(sizeof(*sink));
3316 sink->dest = ERR_SINK_FILE;
3317 sink->fp = fp;
3318 sink->previous = qemu_error_sink;
3319 qemu_error_sink = sink;
3320}
3321
3322void qemu_errors_to_mon(Monitor *mon)
3323{
3324 QemuErrorSink *sink;
3325
3326 sink = qemu_mallocz(sizeof(*sink));
3327 sink->dest = ERR_SINK_MONITOR;
3328 sink->mon = mon;
3329 sink->previous = qemu_error_sink;
3330 qemu_error_sink = sink;
3331}
3332
3333void qemu_errors_to_previous(void)
3334{
3335 QemuErrorSink *sink;
3336
3337 assert(qemu_error_sink != NULL);
3338 sink = qemu_error_sink;
3339 qemu_error_sink = sink->previous;
3340 qemu_free(sink);
3341}
3342
3343void qemu_error(const char *fmt, ...)
3344{
3345 va_list args;
3346
3347 assert(qemu_error_sink != NULL);
3348 switch (qemu_error_sink->dest) {
3349 case ERR_SINK_FILE:
3350 va_start(args, fmt);
3351 vfprintf(qemu_error_sink->fp, fmt, args);
3352 va_end(args);
3353 break;
3354 case ERR_SINK_MONITOR:
3355 va_start(args, fmt);
3356 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3357 va_end(args);
3358 break;
3359 }
3360}