blob: b0aaadaaf40d263e170c9fb3980ca7e2097d520a [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
aliguori376253e2009-03-05 23:01:23 +0000455static void do_eject(Monitor *mon, int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000456{
457 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000458
bellard9307c4c2004-04-04 12:57:25 +0000459 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000460 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000461 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000462 return;
463 }
aliguori376253e2009-03-05 23:01:23 +0000464 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000465}
466
aliguori376253e2009-03-05 23:01:23 +0000467static void do_change_block(Monitor *mon, const char *device,
468 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000469{
470 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000471 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000472
bellard9307c4c2004-04-04 12:57:25 +0000473 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000474 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000475 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000476 return;
477 }
aurel322ecea9b2008-06-18 22:10:01 +0000478 if (fmt) {
479 drv = bdrv_find_format(fmt);
480 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000481 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000482 return;
483 }
484 }
aliguori376253e2009-03-05 23:01:23 +0000485 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000486 return;
aurel322ecea9b2008-06-18 22:10:01 +0000487 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000488 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000489}
490
aliguori376253e2009-03-05 23:01:23 +0000491static void change_vnc_password_cb(Monitor *mon, const char *password,
492 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000493{
494 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000495 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000496
aliguori731b0362009-03-05 23:01:42 +0000497 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000498}
499
aliguori376253e2009-03-05 23:01:23 +0000500static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000501{
ths70848512007-08-25 01:37:05 +0000502 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000503 strcmp(target, "password") == 0) {
504 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000505 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000506 strncpy(password, arg, sizeof(password));
507 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000508 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000509 } else {
aliguori376253e2009-03-05 23:01:23 +0000510 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000511 }
ths70848512007-08-25 01:37:05 +0000512 } else {
aliguori28a76be2009-03-06 20:27:40 +0000513 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000514 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000515 }
thse25a5822007-08-25 01:36:20 +0000516}
517
aliguori376253e2009-03-05 23:01:23 +0000518static void do_change(Monitor *mon, const char *device, const char *target,
519 const char *arg)
thse25a5822007-08-25 01:36:20 +0000520{
521 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000522 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000523 } else {
aliguori28a76be2009-03-06 20:27:40 +0000524 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000525 }
526}
527
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300528static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000529{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300530 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000531}
532
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300533static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000534{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300535 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000536}
537
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300538static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000539{
540 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300541 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000542
bellard9307c4c2004-04-04 12:57:25 +0000543 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000544 mask = 0;
545 } else {
bellard9307c4c2004-04-04 12:57:25 +0000546 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000547 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000548 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000549 return;
550 }
551 }
552 cpu_set_log(mask);
553}
554
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300555static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000556{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300557 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000558 if (!option || !strcmp(option, "on")) {
559 singlestep = 1;
560 } else if (!strcmp(option, "off")) {
561 singlestep = 0;
562 } else {
563 monitor_printf(mon, "unexpected option %s\n", option);
564 }
565}
566
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300567static void do_stop(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000568{
569 vm_stop(EXCP_INTERRUPT);
570}
571
aliguoribb5fc202009-03-05 23:01:15 +0000572static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000573
aliguori376253e2009-03-05 23:01:23 +0000574struct bdrv_iterate_context {
575 Monitor *mon;
576 int err;
577};
aliguoric0f4ce72009-03-05 23:01:01 +0000578
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300579static void do_cont(Monitor *mon, const QDict *qdict)
aliguori376253e2009-03-05 23:01:23 +0000580{
581 struct bdrv_iterate_context context = { mon, 0 };
582
583 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000584 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000585 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000586 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000587}
588
aliguoribb5fc202009-03-05 23:01:15 +0000589static void bdrv_key_cb(void *opaque, int err)
590{
aliguori376253e2009-03-05 23:01:23 +0000591 Monitor *mon = opaque;
592
aliguoribb5fc202009-03-05 23:01:15 +0000593 /* another key was set successfully, retry to continue */
594 if (!err)
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300595 do_cont(mon, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000596}
597
598static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
599{
aliguori376253e2009-03-05 23:01:23 +0000600 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000601
aliguori376253e2009-03-05 23:01:23 +0000602 if (!context->err && bdrv_key_required(bs)) {
603 context->err = -EBUSY;
604 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
605 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000606 }
607}
608
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300609static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000610{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300611 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000612 if (!device)
613 device = "tcp::" DEFAULT_GDBSTUB_PORT;
614 if (gdbserver_start(device) < 0) {
615 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
616 device);
617 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000618 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000619 } else {
aliguori59030a82009-04-05 18:43:41 +0000620 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
621 device);
bellard8a7ddc32004-03-31 19:00:16 +0000622 }
623}
624
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300625static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100626{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300627 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100628 if (select_watchdog_action(action) == -1) {
629 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
630 }
631}
632
aliguori376253e2009-03-05 23:01:23 +0000633static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000634{
aliguori376253e2009-03-05 23:01:23 +0000635 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000636 switch(c) {
637 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000638 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000639 break;
640 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000641 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000642 break;
643 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000644 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000645 break;
646 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000647 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000648 break;
649 default:
650 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000651 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000652 } else {
aliguori376253e2009-03-05 23:01:23 +0000653 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000654 }
655 break;
656 }
aliguori376253e2009-03-05 23:01:23 +0000657 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000658}
659
aliguori376253e2009-03-05 23:01:23 +0000660static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000661 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000662{
bellard6a00d602005-11-21 23:25:50 +0000663 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000664 int nb_per_line, l, line_size, i, max_digits, len;
665 uint8_t buf[16];
666 uint64_t v;
667
668 if (format == 'i') {
669 int flags;
670 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000671 env = mon_get_cpu();
672 if (!env && !is_physical)
673 return;
bellard9307c4c2004-04-04 12:57:25 +0000674#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000675 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000676 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000677 } else if (wsize == 4) {
678 flags = 0;
679 } else {
bellard6a15fd12006-04-12 21:07:07 +0000680 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000681 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000682 if (env) {
683#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000684 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000685 (env->segs[R_CS].flags & DESC_L_MASK))
686 flags = 2;
687 else
688#endif
689 if (!(env->segs[R_CS].flags & DESC_B_MASK))
690 flags = 1;
691 }
bellard4c27ba22004-04-25 18:05:08 +0000692 }
693#endif
aliguori376253e2009-03-05 23:01:23 +0000694 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000695 return;
696 }
697
698 len = wsize * count;
699 if (wsize == 1)
700 line_size = 8;
701 else
702 line_size = 16;
703 nb_per_line = line_size / wsize;
704 max_digits = 0;
705
706 switch(format) {
707 case 'o':
708 max_digits = (wsize * 8 + 2) / 3;
709 break;
710 default:
711 case 'x':
712 max_digits = (wsize * 8) / 4;
713 break;
714 case 'u':
715 case 'd':
716 max_digits = (wsize * 8 * 10 + 32) / 33;
717 break;
718 case 'c':
719 wsize = 1;
720 break;
721 }
722
723 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000724 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000725 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000726 else
aliguori376253e2009-03-05 23:01:23 +0000727 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000728 l = len;
729 if (l > line_size)
730 l = line_size;
731 if (is_physical) {
732 cpu_physical_memory_rw(addr, buf, l, 0);
733 } else {
bellard6a00d602005-11-21 23:25:50 +0000734 env = mon_get_cpu();
735 if (!env)
736 break;
aliguoric8f79b62008-08-18 14:00:20 +0000737 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000738 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000739 break;
740 }
bellard9307c4c2004-04-04 12:57:25 +0000741 }
ths5fafdf22007-09-16 21:08:06 +0000742 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000743 while (i < l) {
744 switch(wsize) {
745 default:
746 case 1:
747 v = ldub_raw(buf + i);
748 break;
749 case 2:
750 v = lduw_raw(buf + i);
751 break;
752 case 4:
bellard92a31b12005-02-10 22:00:52 +0000753 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000754 break;
755 case 8:
756 v = ldq_raw(buf + i);
757 break;
758 }
aliguori376253e2009-03-05 23:01:23 +0000759 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000760 switch(format) {
761 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000762 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000763 break;
764 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000765 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000766 break;
767 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000768 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000769 break;
770 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000771 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000772 break;
773 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000774 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000775 break;
776 }
777 i += wsize;
778 }
aliguori376253e2009-03-05 23:01:23 +0000779 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000780 addr += l;
781 len -= l;
782 }
783}
784
bellard92a31b12005-02-10 22:00:52 +0000785#if TARGET_LONG_BITS == 64
786#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
787#else
788#define GET_TLONG(h, l) (l)
789#endif
790
aliguori376253e2009-03-05 23:01:23 +0000791static void do_memory_dump(Monitor *mon, int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000792 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000793{
bellard92a31b12005-02-10 22:00:52 +0000794 target_long addr = GET_TLONG(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000795 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000796}
797
blueswir17743e582007-09-24 18:39:04 +0000798#if TARGET_PHYS_ADDR_BITS > 32
799#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
800#else
801#define GET_TPHYSADDR(h, l) (l)
802#endif
803
aliguori376253e2009-03-05 23:01:23 +0000804static void do_physical_memory_dump(Monitor *mon, int count, int format,
805 int size, uint32_t addrh, uint32_t addrl)
bellard92a31b12005-02-10 22:00:52 +0000806
bellard9307c4c2004-04-04 12:57:25 +0000807{
blueswir17743e582007-09-24 18:39:04 +0000808 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000809 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000810}
811
aliguori376253e2009-03-05 23:01:23 +0000812static void do_print(Monitor *mon, int count, int format, int size,
813 unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000814{
blueswir17743e582007-09-24 18:39:04 +0000815 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
816#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000817 switch(format) {
818 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000819 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000820 break;
821 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000822 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000823 break;
824 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000825 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000826 break;
827 default:
828 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000829 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000830 break;
831 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000832 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000833 break;
834 }
bellard92a31b12005-02-10 22:00:52 +0000835#else
836 switch(format) {
837 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000838 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000839 break;
840 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000841 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000842 break;
843 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000844 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000845 break;
846 default:
847 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000848 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000849 break;
850 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000851 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000852 break;
853 }
854#endif
aliguori376253e2009-03-05 23:01:23 +0000855 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000856}
857
aliguori376253e2009-03-05 23:01:23 +0000858static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000859 uint32_t size, const char *filename)
860{
861 FILE *f;
862 target_long addr = GET_TLONG(valh, vall);
863 uint32_t l;
864 CPUState *env;
865 uint8_t buf[1024];
866
867 env = mon_get_cpu();
868 if (!env)
869 return;
870
871 f = fopen(filename, "wb");
872 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000873 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000874 return;
875 }
876 while (size != 0) {
877 l = sizeof(buf);
878 if (l > size)
879 l = size;
880 cpu_memory_rw_debug(env, addr, buf, l, 0);
881 fwrite(buf, 1, l, f);
882 addr += l;
883 size -= l;
884 }
885 fclose(f);
886}
887
aliguori376253e2009-03-05 23:01:23 +0000888static void do_physical_memory_save(Monitor *mon, unsigned int valh,
889 unsigned int vall, uint32_t size,
890 const char *filename)
aurel32a8bdf7a2008-04-11 21:36:14 +0000891{
892 FILE *f;
893 uint32_t l;
894 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000895 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000896
897 f = fopen(filename, "wb");
898 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000899 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000900 return;
901 }
902 while (size != 0) {
903 l = sizeof(buf);
904 if (l > size)
905 l = size;
906 cpu_physical_memory_rw(addr, buf, l, 0);
907 fwrite(buf, 1, l, f);
908 fflush(f);
909 addr += l;
910 size -= l;
911 }
912 fclose(f);
913}
914
aliguori376253e2009-03-05 23:01:23 +0000915static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
bellarde4cf1ad2005-06-04 20:15:57 +0000916{
917 uint32_t addr;
918 uint8_t buf[1];
919 uint16_t sum;
920
921 sum = 0;
922 for(addr = start; addr < (start + size); addr++) {
923 cpu_physical_memory_rw(addr, buf, 1, 0);
924 /* BSD sum algorithm ('sum' Unix command) */
925 sum = (sum >> 1) | (sum << 15);
926 sum += buf[0];
927 }
aliguori376253e2009-03-05 23:01:23 +0000928 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000929}
930
bellarda3a91a32004-06-04 11:06:21 +0000931typedef struct {
932 int keycode;
933 const char *name;
934} KeyDef;
935
936static const KeyDef key_defs[] = {
937 { 0x2a, "shift" },
938 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000939
bellarda3a91a32004-06-04 11:06:21 +0000940 { 0x38, "alt" },
941 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000942 { 0x64, "altgr" },
943 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000944 { 0x1d, "ctrl" },
945 { 0x9d, "ctrl_r" },
946
947 { 0xdd, "menu" },
948
949 { 0x01, "esc" },
950
951 { 0x02, "1" },
952 { 0x03, "2" },
953 { 0x04, "3" },
954 { 0x05, "4" },
955 { 0x06, "5" },
956 { 0x07, "6" },
957 { 0x08, "7" },
958 { 0x09, "8" },
959 { 0x0a, "9" },
960 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000961 { 0x0c, "minus" },
962 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000963 { 0x0e, "backspace" },
964
965 { 0x0f, "tab" },
966 { 0x10, "q" },
967 { 0x11, "w" },
968 { 0x12, "e" },
969 { 0x13, "r" },
970 { 0x14, "t" },
971 { 0x15, "y" },
972 { 0x16, "u" },
973 { 0x17, "i" },
974 { 0x18, "o" },
975 { 0x19, "p" },
976
977 { 0x1c, "ret" },
978
979 { 0x1e, "a" },
980 { 0x1f, "s" },
981 { 0x20, "d" },
982 { 0x21, "f" },
983 { 0x22, "g" },
984 { 0x23, "h" },
985 { 0x24, "j" },
986 { 0x25, "k" },
987 { 0x26, "l" },
988
989 { 0x2c, "z" },
990 { 0x2d, "x" },
991 { 0x2e, "c" },
992 { 0x2f, "v" },
993 { 0x30, "b" },
994 { 0x31, "n" },
995 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000996 { 0x33, "comma" },
997 { 0x34, "dot" },
998 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000999
balrog4d3b6f62008-02-10 16:33:14 +00001000 { 0x37, "asterisk" },
1001
bellarda3a91a32004-06-04 11:06:21 +00001002 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001003 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001004 { 0x3b, "f1" },
1005 { 0x3c, "f2" },
1006 { 0x3d, "f3" },
1007 { 0x3e, "f4" },
1008 { 0x3f, "f5" },
1009 { 0x40, "f6" },
1010 { 0x41, "f7" },
1011 { 0x42, "f8" },
1012 { 0x43, "f9" },
1013 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001014 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001015 { 0x46, "scroll_lock" },
1016
bellard64866c32006-05-07 18:03:31 +00001017 { 0xb5, "kp_divide" },
1018 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001019 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001020 { 0x4e, "kp_add" },
1021 { 0x9c, "kp_enter" },
1022 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001023 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001024
1025 { 0x52, "kp_0" },
1026 { 0x4f, "kp_1" },
1027 { 0x50, "kp_2" },
1028 { 0x51, "kp_3" },
1029 { 0x4b, "kp_4" },
1030 { 0x4c, "kp_5" },
1031 { 0x4d, "kp_6" },
1032 { 0x47, "kp_7" },
1033 { 0x48, "kp_8" },
1034 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001035
bellarda3a91a32004-06-04 11:06:21 +00001036 { 0x56, "<" },
1037
1038 { 0x57, "f11" },
1039 { 0x58, "f12" },
1040
1041 { 0xb7, "print" },
1042
1043 { 0xc7, "home" },
1044 { 0xc9, "pgup" },
1045 { 0xd1, "pgdn" },
1046 { 0xcf, "end" },
1047
1048 { 0xcb, "left" },
1049 { 0xc8, "up" },
1050 { 0xd0, "down" },
1051 { 0xcd, "right" },
1052
1053 { 0xd2, "insert" },
1054 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001055#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1056 { 0xf0, "stop" },
1057 { 0xf1, "again" },
1058 { 0xf2, "props" },
1059 { 0xf3, "undo" },
1060 { 0xf4, "front" },
1061 { 0xf5, "copy" },
1062 { 0xf6, "open" },
1063 { 0xf7, "paste" },
1064 { 0xf8, "find" },
1065 { 0xf9, "cut" },
1066 { 0xfa, "lf" },
1067 { 0xfb, "help" },
1068 { 0xfc, "meta_l" },
1069 { 0xfd, "meta_r" },
1070 { 0xfe, "compose" },
1071#endif
bellarda3a91a32004-06-04 11:06:21 +00001072 { 0, NULL },
1073};
1074
1075static int get_keycode(const char *key)
1076{
1077 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001078 char *endp;
1079 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001080
1081 for(p = key_defs; p->name != NULL; p++) {
1082 if (!strcmp(key, p->name))
1083 return p->keycode;
1084 }
bellard64866c32006-05-07 18:03:31 +00001085 if (strstart(key, "0x", NULL)) {
1086 ret = strtoul(key, &endp, 0);
1087 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1088 return ret;
1089 }
bellarda3a91a32004-06-04 11:06:21 +00001090 return -1;
1091}
1092
balrogc8256f92008-06-08 22:45:01 +00001093#define MAX_KEYCODES 16
1094static uint8_t keycodes[MAX_KEYCODES];
1095static int nb_pending_keycodes;
1096static QEMUTimer *key_timer;
1097
1098static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001099{
balrogc8256f92008-06-08 22:45:01 +00001100 int keycode;
1101
1102 while (nb_pending_keycodes > 0) {
1103 nb_pending_keycodes--;
1104 keycode = keycodes[nb_pending_keycodes];
1105 if (keycode & 0x80)
1106 kbd_put_keycode(0xe0);
1107 kbd_put_keycode(keycode | 0x80);
1108 }
1109}
1110
aliguori376253e2009-03-05 23:01:23 +00001111static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1112 int hold_time)
balrogc8256f92008-06-08 22:45:01 +00001113{
balrog3401c0d2008-06-04 10:05:59 +00001114 char keyname_buf[16];
1115 char *separator;
1116 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +00001117
balrogc8256f92008-06-08 22:45:01 +00001118 if (nb_pending_keycodes > 0) {
1119 qemu_del_timer(key_timer);
1120 release_keys(NULL);
1121 }
1122 if (!has_hold_time)
1123 hold_time = 100;
1124 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001125 while (1) {
1126 separator = strchr(string, '-');
1127 keyname_len = separator ? separator - string : strlen(string);
1128 if (keyname_len > 0) {
1129 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1130 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001131 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001132 return;
bellarda3a91a32004-06-04 11:06:21 +00001133 }
balrogc8256f92008-06-08 22:45:01 +00001134 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001135 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001136 return;
1137 }
1138 keyname_buf[keyname_len] = 0;
1139 keycode = get_keycode(keyname_buf);
1140 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001141 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001142 return;
1143 }
balrogc8256f92008-06-08 22:45:01 +00001144 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001145 }
balrog3401c0d2008-06-04 10:05:59 +00001146 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001147 break;
balrog3401c0d2008-06-04 10:05:59 +00001148 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001149 }
balrogc8256f92008-06-08 22:45:01 +00001150 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001151 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001152 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001153 keycode = keycodes[i];
1154 if (keycode & 0x80)
1155 kbd_put_keycode(0xe0);
1156 kbd_put_keycode(keycode & 0x7f);
1157 }
balrogc8256f92008-06-08 22:45:01 +00001158 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001159 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1160 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001161}
1162
bellard13224a82006-07-14 22:03:35 +00001163static int mouse_button_state;
1164
aliguori376253e2009-03-05 23:01:23 +00001165static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001166 const char *dz_str)
1167{
1168 int dx, dy, dz;
1169 dx = strtol(dx_str, NULL, 0);
1170 dy = strtol(dy_str, NULL, 0);
1171 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001172 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001173 dz = strtol(dz_str, NULL, 0);
1174 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1175}
1176
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001177static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001178{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001179 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001180 mouse_button_state = button_state;
1181 kbd_mouse_event(0, 0, 0, mouse_button_state);
1182}
1183
aliguori376253e2009-03-05 23:01:23 +00001184static void do_ioport_read(Monitor *mon, int count, int format, int size,
1185 int addr, int has_index, int index)
bellard34405572004-06-08 00:55:58 +00001186{
1187 uint32_t val;
1188 int suffix;
1189
1190 if (has_index) {
Isaku Yamahatad56dd6c2009-07-02 19:32:07 +09001191 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001192 addr++;
1193 }
1194 addr &= 0xffff;
1195
1196 switch(size) {
1197 default:
1198 case 1:
1199 val = cpu_inb(NULL, addr);
1200 suffix = 'b';
1201 break;
1202 case 2:
1203 val = cpu_inw(NULL, addr);
1204 suffix = 'w';
1205 break;
1206 case 4:
1207 val = cpu_inl(NULL, addr);
1208 suffix = 'l';
1209 break;
1210 }
aliguori376253e2009-03-05 23:01:23 +00001211 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1212 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001213}
bellarda3a91a32004-06-04 11:06:21 +00001214
Jan Kiszkaf1147842009-07-14 10:20:11 +02001215static void do_ioport_write(Monitor *mon, int count, int format, int size,
1216 int addr, int val)
1217{
1218 addr &= IOPORTS_MASK;
1219
1220 switch (size) {
1221 default:
1222 case 1:
1223 cpu_outb(NULL, addr, val);
1224 break;
1225 case 2:
1226 cpu_outw(NULL, addr, val);
1227 break;
1228 case 4:
1229 cpu_outl(NULL, addr, val);
1230 break;
1231 }
1232}
1233
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001234static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001235{
1236 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001237 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001238
Jan Kiszka76e30d02009-07-02 00:19:02 +02001239 res = qemu_boot_set(bootdevice);
1240 if (res == 0) {
1241 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1242 } else if (res > 0) {
1243 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001244 } else {
aliguori376253e2009-03-05 23:01:23 +00001245 monitor_printf(mon, "no function defined to set boot device list for "
1246 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001247 }
1248}
1249
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001250static void do_system_reset(Monitor *mon, const QDict *qdict)
bellarde4f90822004-06-20 12:35:44 +00001251{
1252 qemu_system_reset_request();
1253}
1254
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001255static void do_system_powerdown(Monitor *mon, const QDict *qdict)
bellard34751872005-07-02 14:31:34 +00001256{
1257 qemu_system_powerdown_request();
1258}
1259
bellardb86bda52004-09-18 19:32:46 +00001260#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001261static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001262{
aliguori376253e2009-03-05 23:01:23 +00001263 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1264 addr,
1265 pte & mask,
1266 pte & PG_GLOBAL_MASK ? 'G' : '-',
1267 pte & PG_PSE_MASK ? 'P' : '-',
1268 pte & PG_DIRTY_MASK ? 'D' : '-',
1269 pte & PG_ACCESSED_MASK ? 'A' : '-',
1270 pte & PG_PCD_MASK ? 'C' : '-',
1271 pte & PG_PWT_MASK ? 'T' : '-',
1272 pte & PG_USER_MASK ? 'U' : '-',
1273 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001274}
1275
aliguori376253e2009-03-05 23:01:23 +00001276static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001277{
bellard6a00d602005-11-21 23:25:50 +00001278 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001279 int l1, l2;
1280 uint32_t pgd, pde, pte;
1281
bellard6a00d602005-11-21 23:25:50 +00001282 env = mon_get_cpu();
1283 if (!env)
1284 return;
1285
bellardb86bda52004-09-18 19:32:46 +00001286 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001287 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001288 return;
1289 }
1290 pgd = env->cr[3] & ~0xfff;
1291 for(l1 = 0; l1 < 1024; l1++) {
1292 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1293 pde = le32_to_cpu(pde);
1294 if (pde & PG_PRESENT_MASK) {
1295 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001296 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001297 } else {
1298 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001299 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001300 (uint8_t *)&pte, 4);
1301 pte = le32_to_cpu(pte);
1302 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001303 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001304 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001305 ~0xfff);
1306 }
1307 }
1308 }
1309 }
1310 }
1311}
1312
aliguori376253e2009-03-05 23:01:23 +00001313static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001314 uint32_t end, int prot)
1315{
bellard9746b152004-11-11 18:30:24 +00001316 int prot1;
1317 prot1 = *plast_prot;
1318 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001319 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001320 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1321 *pstart, end, end - *pstart,
1322 prot1 & PG_USER_MASK ? 'u' : '-',
1323 'r',
1324 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001325 }
1326 if (prot != 0)
1327 *pstart = end;
1328 else
1329 *pstart = -1;
1330 *plast_prot = prot;
1331 }
1332}
1333
aliguori376253e2009-03-05 23:01:23 +00001334static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001335{
bellard6a00d602005-11-21 23:25:50 +00001336 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001337 int l1, l2, prot, last_prot;
1338 uint32_t pgd, pde, pte, start, end;
1339
bellard6a00d602005-11-21 23:25:50 +00001340 env = mon_get_cpu();
1341 if (!env)
1342 return;
1343
bellardb86bda52004-09-18 19:32:46 +00001344 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001345 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001346 return;
1347 }
1348 pgd = env->cr[3] & ~0xfff;
1349 last_prot = 0;
1350 start = -1;
1351 for(l1 = 0; l1 < 1024; l1++) {
1352 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1353 pde = le32_to_cpu(pde);
1354 end = l1 << 22;
1355 if (pde & PG_PRESENT_MASK) {
1356 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1357 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001358 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001359 } else {
1360 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001361 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001362 (uint8_t *)&pte, 4);
1363 pte = le32_to_cpu(pte);
1364 end = (l1 << 22) + (l2 << 12);
1365 if (pte & PG_PRESENT_MASK) {
1366 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1367 } else {
1368 prot = 0;
1369 }
aliguori376253e2009-03-05 23:01:23 +00001370 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001371 }
1372 }
1373 } else {
1374 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001375 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001376 }
1377 }
1378}
1379#endif
1380
aurel327c664e22009-03-03 06:12:22 +00001381#if defined(TARGET_SH4)
1382
aliguori376253e2009-03-05 23:01:23 +00001383static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001384{
aliguori376253e2009-03-05 23:01:23 +00001385 monitor_printf(mon, " tlb%i:\t"
1386 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1387 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1388 "dirty=%hhu writethrough=%hhu\n",
1389 idx,
1390 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1391 tlb->v, tlb->sh, tlb->c, tlb->pr,
1392 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001393}
1394
aliguori376253e2009-03-05 23:01:23 +00001395static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001396{
1397 CPUState *env = mon_get_cpu();
1398 int i;
1399
aliguori376253e2009-03-05 23:01:23 +00001400 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001401 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001402 print_tlb (mon, i, &env->itlb[i]);
1403 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001404 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001405 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001406}
1407
1408#endif
1409
aliguori376253e2009-03-05 23:01:23 +00001410static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001411{
1412#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001413 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001414 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001415 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001416 else
aliguori376253e2009-03-05 23:01:23 +00001417 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001418#else
aliguori376253e2009-03-05 23:01:23 +00001419 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001420#endif
1421}
1422
aliguori030ea372009-04-21 22:30:47 +00001423static void do_info_numa(Monitor *mon)
1424{
aliguorib28b6232009-04-22 20:20:29 +00001425 int i;
aliguori030ea372009-04-21 22:30:47 +00001426 CPUState *env;
1427
1428 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1429 for (i = 0; i < nb_numa_nodes; i++) {
1430 monitor_printf(mon, "node %d cpus:", i);
1431 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1432 if (env->numa_node == i) {
1433 monitor_printf(mon, " %d", env->cpu_index);
1434 }
1435 }
1436 monitor_printf(mon, "\n");
1437 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1438 node_mem[i] >> 20);
1439 }
1440}
1441
bellard5f1ce942006-02-08 22:40:15 +00001442#ifdef CONFIG_PROFILER
1443
aliguori376253e2009-03-05 23:01:23 +00001444static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001445{
1446 int64_t total;
1447 total = qemu_time;
1448 if (total == 0)
1449 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001450 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1451 dev_time, dev_time / (double)ticks_per_sec);
1452 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1453 qemu_time, qemu_time / (double)ticks_per_sec);
bellard5f1ce942006-02-08 22:40:15 +00001454 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001455 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001456}
1457#else
aliguori376253e2009-03-05 23:01:23 +00001458static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001459{
aliguori376253e2009-03-05 23:01:23 +00001460 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001461}
1462#endif
1463
bellardec36b692006-07-16 18:57:03 +00001464/* Capture support */
1465static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1466
aliguori376253e2009-03-05 23:01:23 +00001467static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001468{
1469 int i;
1470 CaptureState *s;
1471
1472 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001473 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001474 s->ops.info (s->opaque);
1475 }
1476}
1477
Blue Swirl23130862009-06-06 08:22:04 +00001478#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001479static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001480{
1481 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001482 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001483 CaptureState *s;
1484
1485 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1486 if (i == n) {
1487 s->ops.destroy (s->opaque);
1488 LIST_REMOVE (s, entries);
1489 qemu_free (s);
1490 return;
1491 }
1492 }
1493}
1494
aliguori376253e2009-03-05 23:01:23 +00001495static void do_wav_capture(Monitor *mon, const char *path,
1496 int has_freq, int freq,
1497 int has_bits, int bits,
1498 int has_channels, int nchannels)
bellardec36b692006-07-16 18:57:03 +00001499{
1500 CaptureState *s;
1501
1502 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001503
1504 freq = has_freq ? freq : 44100;
1505 bits = has_bits ? bits : 16;
1506 nchannels = has_channels ? nchannels : 2;
1507
1508 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001509 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001510 qemu_free (s);
1511 }
1512 LIST_INSERT_HEAD (&capture_head, s, entries);
1513}
1514#endif
1515
aurel32dc1c0b72008-04-27 23:52:12 +00001516#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001517static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001518{
1519 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001520 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001521
1522 for (env = first_cpu; env != NULL; env = env->next_cpu)
1523 if (env->cpu_index == cpu_index) {
1524 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1525 break;
1526 }
1527}
1528#endif
1529
aliguori376253e2009-03-05 23:01:23 +00001530static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001531{
aurel321b530a62009-04-05 20:08:59 +00001532 if (vm_running) {
1533 if (singlestep) {
1534 monitor_printf(mon, "VM status: running (single step mode)\n");
1535 } else {
1536 monitor_printf(mon, "VM status: running\n");
1537 }
1538 } else
aliguori376253e2009-03-05 23:01:23 +00001539 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001540}
1541
1542
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001543static void do_balloon(Monitor *mon, const QDict *qdict)
aliguoridf751fa2008-12-04 20:19:35 +00001544{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001545 int value = qdict_get_int(qdict, "value");
aliguoridf751fa2008-12-04 20:19:35 +00001546 ram_addr_t target = value;
1547 qemu_balloon(target << 20);
1548}
1549
aliguori376253e2009-03-05 23:01:23 +00001550static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001551{
1552 ram_addr_t actual;
1553
1554 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001555 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001556 monitor_printf(mon, "Using KVM without synchronous MMU, "
1557 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001558 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001559 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001560 else
aliguori376253e2009-03-05 23:01:23 +00001561 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001562}
1563
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001564static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001565{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001566 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001567
aliguori76655d62009-03-06 20:27:37 +00001568 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001569 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001570 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001571 return acl;
1572}
aliguori76655d62009-03-06 20:27:37 +00001573
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001574static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001575{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001576 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001577 qemu_acl *acl = find_acl(mon, aclname);
1578 qemu_acl_entry *entry;
1579 int i = 0;
1580
1581 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001582 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001583 acl->defaultDeny ? "deny" : "allow");
aliguori28a76be2009-03-06 20:27:40 +00001584 TAILQ_FOREACH(entry, &acl->entries, next) {
1585 i++;
1586 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001587 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001588 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001589 }
1590}
1591
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001592static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001593{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001594 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001595 qemu_acl *acl = find_acl(mon, aclname);
1596
1597 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001598 qemu_acl_reset(acl);
1599 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001600 }
1601}
aliguori76655d62009-03-06 20:27:37 +00001602
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001603static void do_acl_policy(Monitor *mon, const char *aclname,
1604 const char *policy)
1605{
1606 qemu_acl *acl = find_acl(mon, aclname);
1607
1608 if (acl) {
1609 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001610 acl->defaultDeny = 0;
1611 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001612 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001613 acl->defaultDeny = 1;
1614 monitor_printf(mon, "acl: policy set to 'deny'\n");
1615 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001616 monitor_printf(mon, "acl: unknown policy '%s', "
1617 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001618 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001619 }
1620}
aliguori76655d62009-03-06 20:27:37 +00001621
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001622static void do_acl_add(Monitor *mon, const char *aclname,
1623 const char *match, const char *policy,
1624 int has_index, int index)
1625{
1626 qemu_acl *acl = find_acl(mon, aclname);
1627 int deny, ret;
1628
1629 if (acl) {
1630 if (strcmp(policy, "allow") == 0) {
1631 deny = 0;
1632 } else if (strcmp(policy, "deny") == 0) {
1633 deny = 1;
1634 } else {
1635 monitor_printf(mon, "acl: unknown policy '%s', "
1636 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001637 return;
1638 }
aliguori28a76be2009-03-06 20:27:40 +00001639 if (has_index)
1640 ret = qemu_acl_insert(acl, deny, match, index);
1641 else
1642 ret = qemu_acl_append(acl, deny, match);
1643 if (ret < 0)
1644 monitor_printf(mon, "acl: unable to add acl entry\n");
1645 else
1646 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001647 }
1648}
aliguori76655d62009-03-06 20:27:37 +00001649
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001650static void do_acl_remove(Monitor *mon, const char *aclname, const char *match)
1651{
1652 qemu_acl *acl = find_acl(mon, aclname);
1653 int ret;
aliguori76655d62009-03-06 20:27:37 +00001654
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001655 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001656 ret = qemu_acl_remove(acl, match);
1657 if (ret < 0)
1658 monitor_printf(mon, "acl: no matching acl entry\n");
1659 else
1660 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001661 }
1662}
1663
Huang Ying79c4f6b2009-06-23 10:05:14 +08001664#if defined(TARGET_I386)
1665static void do_inject_mce(Monitor *mon,
1666 int cpu_index, int bank,
1667 unsigned status_hi, unsigned status_lo,
1668 unsigned mcg_status_hi, unsigned mcg_status_lo,
1669 unsigned addr_hi, unsigned addr_lo,
1670 unsigned misc_hi, unsigned misc_lo)
1671{
1672 CPUState *cenv;
1673 uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
1674 uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
1675 uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
1676 uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
1677
1678 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1679 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1680 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1681 break;
1682 }
1683}
1684#endif
1685
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001686static void do_getfd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001687{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001688 const char *fdname = qdict_get_str(qdict, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001689 mon_fd_t *monfd;
1690 int fd;
1691
1692 fd = qemu_chr_get_msgfd(mon->chr);
1693 if (fd == -1) {
1694 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1695 return;
1696 }
1697
1698 if (qemu_isdigit(fdname[0])) {
1699 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1700 return;
1701 }
1702
1703 fd = dup(fd);
1704 if (fd == -1) {
1705 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1706 strerror(errno));
1707 return;
1708 }
1709
1710 LIST_FOREACH(monfd, &mon->fds, next) {
1711 if (strcmp(monfd->name, fdname) != 0) {
1712 continue;
1713 }
1714
1715 close(monfd->fd);
1716 monfd->fd = fd;
1717 return;
1718 }
1719
1720 monfd = qemu_mallocz(sizeof(mon_fd_t));
1721 monfd->name = qemu_strdup(fdname);
1722 monfd->fd = fd;
1723
1724 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1725}
1726
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001727static void do_closefd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001728{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001729 const char *fdname = qdict_get_str(qdict, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001730 mon_fd_t *monfd;
1731
1732 LIST_FOREACH(monfd, &mon->fds, next) {
1733 if (strcmp(monfd->name, fdname) != 0) {
1734 continue;
1735 }
1736
1737 LIST_REMOVE(monfd, next);
1738 close(monfd->fd);
1739 qemu_free(monfd->name);
1740 qemu_free(monfd);
1741 return;
1742 }
1743
1744 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1745 fdname);
1746}
1747
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001748static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001749{
1750 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001751 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001752
1753 vm_stop(0);
1754
Juan Quintela05f24012009-08-20 19:42:22 +02001755 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001756 vm_start();
1757}
1758
Mark McLoughlin7768e042009-07-22 09:11:41 +01001759int monitor_get_fd(Monitor *mon, const char *fdname)
1760{
1761 mon_fd_t *monfd;
1762
1763 LIST_FOREACH(monfd, &mon->fds, next) {
1764 int fd;
1765
1766 if (strcmp(monfd->name, fdname) != 0) {
1767 continue;
1768 }
1769
1770 fd = monfd->fd;
1771
1772 /* caller takes ownership of fd */
1773 LIST_REMOVE(monfd, next);
1774 qemu_free(monfd->name);
1775 qemu_free(monfd);
1776
1777 return fd;
1778 }
1779
1780 return -1;
1781}
1782
aliguori376253e2009-03-05 23:01:23 +00001783static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001784#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001785 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001786};
1787
Blue Swirl23130862009-06-06 08:22:04 +00001788/* Please update qemu-monitor.hx when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001789static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001790 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001791 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001792 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001793 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001794 { "chardev", "", qemu_chr_info,
1795 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001796 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001797 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001798 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001799 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001800 { "registers", "", do_info_registers,
1801 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001802 { "cpus", "", do_info_cpus,
1803 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001804 { "history", "", do_info_history,
1805 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001806 { "irq", "", irq_info,
1807 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001808 { "pic", "", pic_info,
1809 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001810 { "pci", "", pci_info,
1811 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001812#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001813 { "tlb", "", tlb_info,
1814 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001815#endif
1816#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001817 { "mem", "", mem_info,
1818 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001819 { "hpet", "", do_info_hpet,
1820 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001821#endif
bellarde3db7222005-01-26 22:00:47 +00001822 { "jit", "", do_info_jit,
1823 "", "show dynamic compiler info", },
aliguori7ba1e612008-11-05 16:04:33 +00001824 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001825 "", "show KVM information", },
aliguori030ea372009-04-21 22:30:47 +00001826 { "numa", "", do_info_numa,
1827 "", "show NUMA information", },
bellarda594cfb2005-11-06 16:13:29 +00001828 { "usb", "", usb_info,
1829 "", "show guest USB devices", },
1830 { "usbhost", "", usb_host_info,
1831 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001832 { "profile", "", do_info_profile,
1833 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001834 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001835 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001836 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001837 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001838 { "status", "", do_info_status,
1839 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001840 { "pcmcia", "", pcmcia_info,
1841 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001842 { "mice", "", do_info_mice,
1843 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001844 { "vnc", "", do_info_vnc,
1845 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001846 { "name", "", do_info_name,
1847 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001848 { "uuid", "", do_info_uuid,
1849 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001850#if defined(TARGET_PPC)
1851 { "cpustats", "", do_info_cpu_stats,
1852 "", "show CPU statistics", },
1853#endif
blueswir131a60e22007-10-26 18:42:59 +00001854#if defined(CONFIG_SLIRP)
Jan Kiszka6dbe5532009-06-24 14:42:29 +02001855 { "usernet", "", do_info_usernet,
1856 "", "show user network stack connection states", },
blueswir131a60e22007-10-26 18:42:59 +00001857#endif
aliguori5bb79102008-10-13 03:12:02 +00001858 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001859 { "balloon", "", do_info_balloon,
1860 "", "show balloon information" },
Gerd Hoffmanncae49562009-06-05 15:53:17 +01001861 { "qtree", "", do_info_qtree,
1862 "", "show device tree" },
Gerd Hoffmannf6c64e02009-08-03 15:03:09 +02001863 { "qdm", "", do_info_qdm,
1864 "", "show qdev device model list" },
bellard9dc39cb2004-03-14 21:38:27 +00001865 { NULL, NULL, },
1866};
1867
bellard9307c4c2004-04-04 12:57:25 +00001868/*******************************************************************/
1869
1870static const char *pch;
1871static jmp_buf expr_env;
1872
bellard92a31b12005-02-10 22:00:52 +00001873#define MD_TLONG 0
1874#define MD_I32 1
1875
bellard9307c4c2004-04-04 12:57:25 +00001876typedef struct MonitorDef {
1877 const char *name;
1878 int offset;
blueswir18662d652008-10-02 18:32:44 +00001879 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001880 int type;
bellard9307c4c2004-04-04 12:57:25 +00001881} MonitorDef;
1882
bellard57206fd2004-04-25 18:54:52 +00001883#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001884static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001885{
bellard6a00d602005-11-21 23:25:50 +00001886 CPUState *env = mon_get_cpu();
1887 if (!env)
1888 return 0;
1889 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001890}
1891#endif
1892
bellarda541f292004-04-12 20:39:29 +00001893#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001894static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001895{
bellard6a00d602005-11-21 23:25:50 +00001896 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001897 unsigned int u;
1898 int i;
1899
bellard6a00d602005-11-21 23:25:50 +00001900 if (!env)
1901 return 0;
1902
bellarda541f292004-04-12 20:39:29 +00001903 u = 0;
1904 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00001905 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001906
1907 return u;
1908}
1909
blueswir18662d652008-10-02 18:32:44 +00001910static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001911{
bellard6a00d602005-11-21 23:25:50 +00001912 CPUState *env = mon_get_cpu();
1913 if (!env)
1914 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001915 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001916}
1917
blueswir18662d652008-10-02 18:32:44 +00001918static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001919{
bellard6a00d602005-11-21 23:25:50 +00001920 CPUState *env = mon_get_cpu();
1921 if (!env)
1922 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001923 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001924}
bellard9fddaa02004-05-21 12:59:32 +00001925
blueswir18662d652008-10-02 18:32:44 +00001926static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001927{
bellard6a00d602005-11-21 23:25:50 +00001928 CPUState *env = mon_get_cpu();
1929 if (!env)
1930 return 0;
1931 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001932}
1933
blueswir18662d652008-10-02 18:32:44 +00001934static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001935{
bellard6a00d602005-11-21 23:25:50 +00001936 CPUState *env = mon_get_cpu();
1937 if (!env)
1938 return 0;
1939 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001940}
1941
blueswir18662d652008-10-02 18:32:44 +00001942static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001943{
bellard6a00d602005-11-21 23:25:50 +00001944 CPUState *env = mon_get_cpu();
1945 if (!env)
1946 return 0;
1947 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001948}
bellarda541f292004-04-12 20:39:29 +00001949#endif
1950
bellarde95c8d52004-09-30 22:22:08 +00001951#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001952#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001953static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001954{
bellard6a00d602005-11-21 23:25:50 +00001955 CPUState *env = mon_get_cpu();
1956 if (!env)
1957 return 0;
1958 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001959}
bellard7b936c02005-10-30 17:05:13 +00001960#endif
bellarde95c8d52004-09-30 22:22:08 +00001961
blueswir18662d652008-10-02 18:32:44 +00001962static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001963{
bellard6a00d602005-11-21 23:25:50 +00001964 CPUState *env = mon_get_cpu();
1965 if (!env)
1966 return 0;
1967 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001968}
1969#endif
1970
blueswir18662d652008-10-02 18:32:44 +00001971static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001972#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001973
1974#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001975 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001976 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001977 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001978
bellard9307c4c2004-04-04 12:57:25 +00001979 { "eax", offsetof(CPUState, regs[0]) },
1980 { "ecx", offsetof(CPUState, regs[1]) },
1981 { "edx", offsetof(CPUState, regs[2]) },
1982 { "ebx", offsetof(CPUState, regs[3]) },
1983 { "esp|sp", offsetof(CPUState, regs[4]) },
1984 { "ebp|fp", offsetof(CPUState, regs[5]) },
1985 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001986 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001987#ifdef TARGET_X86_64
1988 { "r8", offsetof(CPUState, regs[8]) },
1989 { "r9", offsetof(CPUState, regs[9]) },
1990 { "r10", offsetof(CPUState, regs[10]) },
1991 { "r11", offsetof(CPUState, regs[11]) },
1992 { "r12", offsetof(CPUState, regs[12]) },
1993 { "r13", offsetof(CPUState, regs[13]) },
1994 { "r14", offsetof(CPUState, regs[14]) },
1995 { "r15", offsetof(CPUState, regs[15]) },
1996#endif
bellard9307c4c2004-04-04 12:57:25 +00001997 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001998 { "eip", offsetof(CPUState, eip) },
1999 SEG("cs", R_CS)
2000 SEG("ds", R_DS)
2001 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002002 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002003 SEG("fs", R_FS)
2004 SEG("gs", R_GS)
2005 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002006#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002007 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002008 { "r0", offsetof(CPUState, gpr[0]) },
2009 { "r1", offsetof(CPUState, gpr[1]) },
2010 { "r2", offsetof(CPUState, gpr[2]) },
2011 { "r3", offsetof(CPUState, gpr[3]) },
2012 { "r4", offsetof(CPUState, gpr[4]) },
2013 { "r5", offsetof(CPUState, gpr[5]) },
2014 { "r6", offsetof(CPUState, gpr[6]) },
2015 { "r7", offsetof(CPUState, gpr[7]) },
2016 { "r8", offsetof(CPUState, gpr[8]) },
2017 { "r9", offsetof(CPUState, gpr[9]) },
2018 { "r10", offsetof(CPUState, gpr[10]) },
2019 { "r11", offsetof(CPUState, gpr[11]) },
2020 { "r12", offsetof(CPUState, gpr[12]) },
2021 { "r13", offsetof(CPUState, gpr[13]) },
2022 { "r14", offsetof(CPUState, gpr[14]) },
2023 { "r15", offsetof(CPUState, gpr[15]) },
2024 { "r16", offsetof(CPUState, gpr[16]) },
2025 { "r17", offsetof(CPUState, gpr[17]) },
2026 { "r18", offsetof(CPUState, gpr[18]) },
2027 { "r19", offsetof(CPUState, gpr[19]) },
2028 { "r20", offsetof(CPUState, gpr[20]) },
2029 { "r21", offsetof(CPUState, gpr[21]) },
2030 { "r22", offsetof(CPUState, gpr[22]) },
2031 { "r23", offsetof(CPUState, gpr[23]) },
2032 { "r24", offsetof(CPUState, gpr[24]) },
2033 { "r25", offsetof(CPUState, gpr[25]) },
2034 { "r26", offsetof(CPUState, gpr[26]) },
2035 { "r27", offsetof(CPUState, gpr[27]) },
2036 { "r28", offsetof(CPUState, gpr[28]) },
2037 { "r29", offsetof(CPUState, gpr[29]) },
2038 { "r30", offsetof(CPUState, gpr[30]) },
2039 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002040 /* Floating point registers */
2041 { "f0", offsetof(CPUState, fpr[0]) },
2042 { "f1", offsetof(CPUState, fpr[1]) },
2043 { "f2", offsetof(CPUState, fpr[2]) },
2044 { "f3", offsetof(CPUState, fpr[3]) },
2045 { "f4", offsetof(CPUState, fpr[4]) },
2046 { "f5", offsetof(CPUState, fpr[5]) },
2047 { "f6", offsetof(CPUState, fpr[6]) },
2048 { "f7", offsetof(CPUState, fpr[7]) },
2049 { "f8", offsetof(CPUState, fpr[8]) },
2050 { "f9", offsetof(CPUState, fpr[9]) },
2051 { "f10", offsetof(CPUState, fpr[10]) },
2052 { "f11", offsetof(CPUState, fpr[11]) },
2053 { "f12", offsetof(CPUState, fpr[12]) },
2054 { "f13", offsetof(CPUState, fpr[13]) },
2055 { "f14", offsetof(CPUState, fpr[14]) },
2056 { "f15", offsetof(CPUState, fpr[15]) },
2057 { "f16", offsetof(CPUState, fpr[16]) },
2058 { "f17", offsetof(CPUState, fpr[17]) },
2059 { "f18", offsetof(CPUState, fpr[18]) },
2060 { "f19", offsetof(CPUState, fpr[19]) },
2061 { "f20", offsetof(CPUState, fpr[20]) },
2062 { "f21", offsetof(CPUState, fpr[21]) },
2063 { "f22", offsetof(CPUState, fpr[22]) },
2064 { "f23", offsetof(CPUState, fpr[23]) },
2065 { "f24", offsetof(CPUState, fpr[24]) },
2066 { "f25", offsetof(CPUState, fpr[25]) },
2067 { "f26", offsetof(CPUState, fpr[26]) },
2068 { "f27", offsetof(CPUState, fpr[27]) },
2069 { "f28", offsetof(CPUState, fpr[28]) },
2070 { "f29", offsetof(CPUState, fpr[29]) },
2071 { "f30", offsetof(CPUState, fpr[30]) },
2072 { "f31", offsetof(CPUState, fpr[31]) },
2073 { "fpscr", offsetof(CPUState, fpscr) },
2074 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002075 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002076 { "lr", offsetof(CPUState, lr) },
2077 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002078 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002079 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002080 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002081 { "msr", 0, &monitor_get_msr, },
2082 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002083 { "tbu", 0, &monitor_get_tbu, },
2084 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002085#if defined(TARGET_PPC64)
2086 /* Address space register */
2087 { "asr", offsetof(CPUState, asr) },
2088#endif
2089 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002090 { "sdr1", offsetof(CPUState, sdr1) },
2091 { "sr0", offsetof(CPUState, sr[0]) },
2092 { "sr1", offsetof(CPUState, sr[1]) },
2093 { "sr2", offsetof(CPUState, sr[2]) },
2094 { "sr3", offsetof(CPUState, sr[3]) },
2095 { "sr4", offsetof(CPUState, sr[4]) },
2096 { "sr5", offsetof(CPUState, sr[5]) },
2097 { "sr6", offsetof(CPUState, sr[6]) },
2098 { "sr7", offsetof(CPUState, sr[7]) },
2099 { "sr8", offsetof(CPUState, sr[8]) },
2100 { "sr9", offsetof(CPUState, sr[9]) },
2101 { "sr10", offsetof(CPUState, sr[10]) },
2102 { "sr11", offsetof(CPUState, sr[11]) },
2103 { "sr12", offsetof(CPUState, sr[12]) },
2104 { "sr13", offsetof(CPUState, sr[13]) },
2105 { "sr14", offsetof(CPUState, sr[14]) },
2106 { "sr15", offsetof(CPUState, sr[15]) },
2107 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002108#elif defined(TARGET_SPARC)
2109 { "g0", offsetof(CPUState, gregs[0]) },
2110 { "g1", offsetof(CPUState, gregs[1]) },
2111 { "g2", offsetof(CPUState, gregs[2]) },
2112 { "g3", offsetof(CPUState, gregs[3]) },
2113 { "g4", offsetof(CPUState, gregs[4]) },
2114 { "g5", offsetof(CPUState, gregs[5]) },
2115 { "g6", offsetof(CPUState, gregs[6]) },
2116 { "g7", offsetof(CPUState, gregs[7]) },
2117 { "o0", 0, monitor_get_reg },
2118 { "o1", 1, monitor_get_reg },
2119 { "o2", 2, monitor_get_reg },
2120 { "o3", 3, monitor_get_reg },
2121 { "o4", 4, monitor_get_reg },
2122 { "o5", 5, monitor_get_reg },
2123 { "o6", 6, monitor_get_reg },
2124 { "o7", 7, monitor_get_reg },
2125 { "l0", 8, monitor_get_reg },
2126 { "l1", 9, monitor_get_reg },
2127 { "l2", 10, monitor_get_reg },
2128 { "l3", 11, monitor_get_reg },
2129 { "l4", 12, monitor_get_reg },
2130 { "l5", 13, monitor_get_reg },
2131 { "l6", 14, monitor_get_reg },
2132 { "l7", 15, monitor_get_reg },
2133 { "i0", 16, monitor_get_reg },
2134 { "i1", 17, monitor_get_reg },
2135 { "i2", 18, monitor_get_reg },
2136 { "i3", 19, monitor_get_reg },
2137 { "i4", 20, monitor_get_reg },
2138 { "i5", 21, monitor_get_reg },
2139 { "i6", 22, monitor_get_reg },
2140 { "i7", 23, monitor_get_reg },
2141 { "pc", offsetof(CPUState, pc) },
2142 { "npc", offsetof(CPUState, npc) },
2143 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002144#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002145 { "psr", 0, &monitor_get_psr, },
2146 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002147#endif
bellarde95c8d52004-09-30 22:22:08 +00002148 { "tbr", offsetof(CPUState, tbr) },
2149 { "fsr", offsetof(CPUState, fsr) },
2150 { "f0", offsetof(CPUState, fpr[0]) },
2151 { "f1", offsetof(CPUState, fpr[1]) },
2152 { "f2", offsetof(CPUState, fpr[2]) },
2153 { "f3", offsetof(CPUState, fpr[3]) },
2154 { "f4", offsetof(CPUState, fpr[4]) },
2155 { "f5", offsetof(CPUState, fpr[5]) },
2156 { "f6", offsetof(CPUState, fpr[6]) },
2157 { "f7", offsetof(CPUState, fpr[7]) },
2158 { "f8", offsetof(CPUState, fpr[8]) },
2159 { "f9", offsetof(CPUState, fpr[9]) },
2160 { "f10", offsetof(CPUState, fpr[10]) },
2161 { "f11", offsetof(CPUState, fpr[11]) },
2162 { "f12", offsetof(CPUState, fpr[12]) },
2163 { "f13", offsetof(CPUState, fpr[13]) },
2164 { "f14", offsetof(CPUState, fpr[14]) },
2165 { "f15", offsetof(CPUState, fpr[15]) },
2166 { "f16", offsetof(CPUState, fpr[16]) },
2167 { "f17", offsetof(CPUState, fpr[17]) },
2168 { "f18", offsetof(CPUState, fpr[18]) },
2169 { "f19", offsetof(CPUState, fpr[19]) },
2170 { "f20", offsetof(CPUState, fpr[20]) },
2171 { "f21", offsetof(CPUState, fpr[21]) },
2172 { "f22", offsetof(CPUState, fpr[22]) },
2173 { "f23", offsetof(CPUState, fpr[23]) },
2174 { "f24", offsetof(CPUState, fpr[24]) },
2175 { "f25", offsetof(CPUState, fpr[25]) },
2176 { "f26", offsetof(CPUState, fpr[26]) },
2177 { "f27", offsetof(CPUState, fpr[27]) },
2178 { "f28", offsetof(CPUState, fpr[28]) },
2179 { "f29", offsetof(CPUState, fpr[29]) },
2180 { "f30", offsetof(CPUState, fpr[30]) },
2181 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002182#ifdef TARGET_SPARC64
2183 { "f32", offsetof(CPUState, fpr[32]) },
2184 { "f34", offsetof(CPUState, fpr[34]) },
2185 { "f36", offsetof(CPUState, fpr[36]) },
2186 { "f38", offsetof(CPUState, fpr[38]) },
2187 { "f40", offsetof(CPUState, fpr[40]) },
2188 { "f42", offsetof(CPUState, fpr[42]) },
2189 { "f44", offsetof(CPUState, fpr[44]) },
2190 { "f46", offsetof(CPUState, fpr[46]) },
2191 { "f48", offsetof(CPUState, fpr[48]) },
2192 { "f50", offsetof(CPUState, fpr[50]) },
2193 { "f52", offsetof(CPUState, fpr[52]) },
2194 { "f54", offsetof(CPUState, fpr[54]) },
2195 { "f56", offsetof(CPUState, fpr[56]) },
2196 { "f58", offsetof(CPUState, fpr[58]) },
2197 { "f60", offsetof(CPUState, fpr[60]) },
2198 { "f62", offsetof(CPUState, fpr[62]) },
2199 { "asi", offsetof(CPUState, asi) },
2200 { "pstate", offsetof(CPUState, pstate) },
2201 { "cansave", offsetof(CPUState, cansave) },
2202 { "canrestore", offsetof(CPUState, canrestore) },
2203 { "otherwin", offsetof(CPUState, otherwin) },
2204 { "wstate", offsetof(CPUState, wstate) },
2205 { "cleanwin", offsetof(CPUState, cleanwin) },
2206 { "fprs", offsetof(CPUState, fprs) },
2207#endif
bellard9307c4c2004-04-04 12:57:25 +00002208#endif
2209 { NULL },
2210};
2211
aliguori376253e2009-03-05 23:01:23 +00002212static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002213{
aliguori376253e2009-03-05 23:01:23 +00002214 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002215 longjmp(expr_env, 1);
2216}
2217
bellard6a00d602005-11-21 23:25:50 +00002218/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002219static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002220{
blueswir18662d652008-10-02 18:32:44 +00002221 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002222 void *ptr;
2223
bellard9307c4c2004-04-04 12:57:25 +00002224 for(md = monitor_defs; md->name != NULL; md++) {
2225 if (compare_cmd(name, md->name)) {
2226 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002227 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002228 } else {
bellard6a00d602005-11-21 23:25:50 +00002229 CPUState *env = mon_get_cpu();
2230 if (!env)
2231 return -2;
2232 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002233 switch(md->type) {
2234 case MD_I32:
2235 *pval = *(int32_t *)ptr;
2236 break;
2237 case MD_TLONG:
2238 *pval = *(target_long *)ptr;
2239 break;
2240 default:
2241 *pval = 0;
2242 break;
2243 }
bellard9307c4c2004-04-04 12:57:25 +00002244 }
2245 return 0;
2246 }
2247 }
2248 return -1;
2249}
2250
2251static void next(void)
2252{
Blue Swirl660f11b2009-07-31 21:16:51 +00002253 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002254 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002255 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002256 pch++;
2257 }
2258}
2259
aliguori376253e2009-03-05 23:01:23 +00002260static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002261
aliguori376253e2009-03-05 23:01:23 +00002262static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002263{
blueswir1c2efc952007-09-25 17:28:42 +00002264 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002265 char *p;
bellard6a00d602005-11-21 23:25:50 +00002266 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002267
2268 switch(*pch) {
2269 case '+':
2270 next();
aliguori376253e2009-03-05 23:01:23 +00002271 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002272 break;
2273 case '-':
2274 next();
aliguori376253e2009-03-05 23:01:23 +00002275 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002276 break;
2277 case '~':
2278 next();
aliguori376253e2009-03-05 23:01:23 +00002279 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002280 break;
2281 case '(':
2282 next();
aliguori376253e2009-03-05 23:01:23 +00002283 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002284 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002285 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002286 }
2287 next();
2288 break;
bellard81d09122004-07-14 17:21:37 +00002289 case '\'':
2290 pch++;
2291 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002292 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002293 n = *pch;
2294 pch++;
2295 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002296 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002297 next();
2298 break;
bellard9307c4c2004-04-04 12:57:25 +00002299 case '$':
2300 {
2301 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002302 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002303
bellard9307c4c2004-04-04 12:57:25 +00002304 pch++;
2305 q = buf;
2306 while ((*pch >= 'a' && *pch <= 'z') ||
2307 (*pch >= 'A' && *pch <= 'Z') ||
2308 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002309 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002310 if ((q - buf) < sizeof(buf) - 1)
2311 *q++ = *pch;
2312 pch++;
2313 }
blueswir1cd390082008-11-16 13:53:32 +00002314 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002315 pch++;
2316 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002317 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002318 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002319 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002320 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002321 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002322 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002323 }
2324 break;
2325 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002326 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002327 n = 0;
2328 break;
2329 default:
blueswir17743e582007-09-24 18:39:04 +00002330#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002331 n = strtoull(pch, &p, 0);
2332#else
bellard9307c4c2004-04-04 12:57:25 +00002333 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002334#endif
bellard9307c4c2004-04-04 12:57:25 +00002335 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002336 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002337 }
2338 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002339 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002340 pch++;
2341 break;
2342 }
2343 return n;
2344}
2345
2346
aliguori376253e2009-03-05 23:01:23 +00002347static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002348{
blueswir1c2efc952007-09-25 17:28:42 +00002349 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002350 int op;
ths3b46e622007-09-17 08:09:54 +00002351
aliguori376253e2009-03-05 23:01:23 +00002352 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002353 for(;;) {
2354 op = *pch;
2355 if (op != '*' && op != '/' && op != '%')
2356 break;
2357 next();
aliguori376253e2009-03-05 23:01:23 +00002358 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002359 switch(op) {
2360 default:
2361 case '*':
2362 val *= val2;
2363 break;
2364 case '/':
2365 case '%':
ths5fafdf22007-09-16 21:08:06 +00002366 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002367 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002368 if (op == '/')
2369 val /= val2;
2370 else
2371 val %= val2;
2372 break;
2373 }
2374 }
2375 return val;
2376}
2377
aliguori376253e2009-03-05 23:01:23 +00002378static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002379{
blueswir1c2efc952007-09-25 17:28:42 +00002380 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002381 int op;
bellard9307c4c2004-04-04 12:57:25 +00002382
aliguori376253e2009-03-05 23:01:23 +00002383 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002384 for(;;) {
2385 op = *pch;
2386 if (op != '&' && op != '|' && op != '^')
2387 break;
2388 next();
aliguori376253e2009-03-05 23:01:23 +00002389 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002390 switch(op) {
2391 default:
2392 case '&':
2393 val &= val2;
2394 break;
2395 case '|':
2396 val |= val2;
2397 break;
2398 case '^':
2399 val ^= val2;
2400 break;
2401 }
2402 }
2403 return val;
2404}
2405
aliguori376253e2009-03-05 23:01:23 +00002406static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002407{
blueswir1c2efc952007-09-25 17:28:42 +00002408 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002409 int op;
bellard9307c4c2004-04-04 12:57:25 +00002410
aliguori376253e2009-03-05 23:01:23 +00002411 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002412 for(;;) {
2413 op = *pch;
2414 if (op != '+' && op != '-')
2415 break;
2416 next();
aliguori376253e2009-03-05 23:01:23 +00002417 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002418 if (op == '+')
2419 val += val2;
2420 else
2421 val -= val2;
2422 }
2423 return val;
2424}
2425
aliguori376253e2009-03-05 23:01:23 +00002426static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002427{
2428 pch = *pp;
2429 if (setjmp(expr_env)) {
2430 *pp = pch;
2431 return -1;
2432 }
blueswir1cd390082008-11-16 13:53:32 +00002433 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002434 pch++;
aliguori376253e2009-03-05 23:01:23 +00002435 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002436 *pp = pch;
2437 return 0;
2438}
2439
2440static int get_str(char *buf, int buf_size, const char **pp)
2441{
2442 const char *p;
2443 char *q;
2444 int c;
2445
bellard81d09122004-07-14 17:21:37 +00002446 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002447 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002448 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002449 p++;
2450 if (*p == '\0') {
2451 fail:
bellard81d09122004-07-14 17:21:37 +00002452 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002453 *pp = p;
2454 return -1;
2455 }
bellard9307c4c2004-04-04 12:57:25 +00002456 if (*p == '\"') {
2457 p++;
2458 while (*p != '\0' && *p != '\"') {
2459 if (*p == '\\') {
2460 p++;
2461 c = *p++;
2462 switch(c) {
2463 case 'n':
2464 c = '\n';
2465 break;
2466 case 'r':
2467 c = '\r';
2468 break;
2469 case '\\':
2470 case '\'':
2471 case '\"':
2472 break;
2473 default:
2474 qemu_printf("unsupported escape code: '\\%c'\n", c);
2475 goto fail;
2476 }
2477 if ((q - buf) < buf_size - 1) {
2478 *q++ = c;
2479 }
2480 } else {
2481 if ((q - buf) < buf_size - 1) {
2482 *q++ = *p;
2483 }
2484 p++;
2485 }
2486 }
2487 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002488 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002489 goto fail;
2490 }
2491 p++;
2492 } else {
blueswir1cd390082008-11-16 13:53:32 +00002493 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002494 if ((q - buf) < buf_size - 1) {
2495 *q++ = *p;
2496 }
2497 p++;
2498 }
bellard9307c4c2004-04-04 12:57:25 +00002499 }
bellard81d09122004-07-14 17:21:37 +00002500 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002501 *pp = p;
2502 return 0;
2503}
2504
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002505/*
2506 * Store the command-name in cmdname, and return a pointer to
2507 * the remaining of the command string.
2508 */
2509static const char *get_command_name(const char *cmdline,
2510 char *cmdname, size_t nlen)
2511{
2512 size_t len;
2513 const char *p, *pstart;
2514
2515 p = cmdline;
2516 while (qemu_isspace(*p))
2517 p++;
2518 if (*p == '\0')
2519 return NULL;
2520 pstart = p;
2521 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2522 p++;
2523 len = p - pstart;
2524 if (len > nlen - 1)
2525 len = nlen - 1;
2526 memcpy(cmdname, pstart, len);
2527 cmdname[len] = '\0';
2528 return p;
2529}
2530
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002531/**
2532 * Read key of 'type' into 'key' and return the current
2533 * 'type' pointer.
2534 */
2535static char *key_get_info(const char *type, char **key)
2536{
2537 size_t len;
2538 char *p, *str;
2539
2540 if (*type == ',')
2541 type++;
2542
2543 p = strchr(type, ':');
2544 if (!p) {
2545 *key = NULL;
2546 return NULL;
2547 }
2548 len = p - type;
2549
2550 str = qemu_malloc(len + 1);
2551 memcpy(str, type, len);
2552 str[len] = '\0';
2553
2554 *key = str;
2555 return ++p;
2556}
2557
bellard9307c4c2004-04-04 12:57:25 +00002558static int default_fmt_format = 'x';
2559static int default_fmt_size = 4;
2560
2561#define MAX_ARGS 16
2562
aliguori376253e2009-03-05 23:01:23 +00002563static void monitor_handle_command(Monitor *mon, const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002564{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002565 const char *p, *typestr;
2566 int c, nb_args, i, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002567 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002568 char cmdname[256];
2569 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002570 char *key;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002571 QDict *qdict;
bellard9307c4c2004-04-04 12:57:25 +00002572 void *str_allocated[MAX_ARGS];
2573 void *args[MAX_ARGS];
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03002574 void (*handler_d)(Monitor *mon, const QDict *qdict);
aliguori376253e2009-03-05 23:01:23 +00002575 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2576 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2577 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2578 void *arg3);
2579 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2580 void *arg3, void *arg4);
2581 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2582 void *arg3, void *arg4, void *arg5);
2583 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2584 void *arg3, void *arg4, void *arg5, void *arg6);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002585 void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2586 void *arg3, void *arg4, void *arg5, void *arg6,
2587 void *arg7);
2588 void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2589 void *arg3, void *arg4, void *arg5, void *arg6,
2590 void *arg7, void *arg8);
2591 void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2592 void *arg3, void *arg4, void *arg5, void *arg6,
2593 void *arg7, void *arg8, void *arg9);
bellard9dc39cb2004-03-14 21:38:27 +00002594
2595#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002596 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002597#endif
ths3b46e622007-09-17 08:09:54 +00002598
bellard9307c4c2004-04-04 12:57:25 +00002599 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002600 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2601 if (!p)
bellard9dc39cb2004-03-14 21:38:27 +00002602 return;
ths3b46e622007-09-17 08:09:54 +00002603
bellard9307c4c2004-04-04 12:57:25 +00002604 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002605 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002606 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002607 break;
bellard9dc39cb2004-03-14 21:38:27 +00002608 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002609
2610 if (cmd->name == NULL) {
2611 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2612 return;
2613 }
bellard9307c4c2004-04-04 12:57:25 +00002614
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002615 qdict = qdict_new();
2616
bellard9307c4c2004-04-04 12:57:25 +00002617 for(i = 0; i < MAX_ARGS; i++)
2618 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002619
bellard9307c4c2004-04-04 12:57:25 +00002620 /* parse the parameters */
2621 typestr = cmd->args_type;
2622 nb_args = 0;
2623 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002624 typestr = key_get_info(typestr, &key);
2625 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002626 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002627 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002628 typestr++;
2629 switch(c) {
2630 case 'F':
bellard81d09122004-07-14 17:21:37 +00002631 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002632 case 's':
2633 {
2634 int ret;
2635 char *str;
ths3b46e622007-09-17 08:09:54 +00002636
blueswir1cd390082008-11-16 13:53:32 +00002637 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002638 p++;
2639 if (*typestr == '?') {
2640 typestr++;
2641 if (*p == '\0') {
2642 /* no optional string: NULL argument */
2643 str = NULL;
2644 goto add_str;
2645 }
2646 }
2647 ret = get_str(buf, sizeof(buf), &p);
2648 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002649 switch(c) {
2650 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002651 monitor_printf(mon, "%s: filename expected\n",
2652 cmdname);
bellard81d09122004-07-14 17:21:37 +00002653 break;
2654 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002655 monitor_printf(mon, "%s: block device name expected\n",
2656 cmdname);
bellard81d09122004-07-14 17:21:37 +00002657 break;
2658 default:
aliguori376253e2009-03-05 23:01:23 +00002659 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002660 break;
2661 }
bellard9307c4c2004-04-04 12:57:25 +00002662 goto fail;
2663 }
2664 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002665 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002666 str_allocated[nb_args] = str;
2667 add_str:
2668 if (nb_args >= MAX_ARGS) {
2669 error_args:
aliguori376253e2009-03-05 23:01:23 +00002670 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002671 goto fail;
2672 }
2673 args[nb_args++] = str;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002674 if (str)
2675 qdict_put(qdict, key, qstring_from_str(str));
bellard9307c4c2004-04-04 12:57:25 +00002676 }
2677 break;
2678 case '/':
2679 {
2680 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002681
blueswir1cd390082008-11-16 13:53:32 +00002682 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002683 p++;
2684 if (*p == '/') {
2685 /* format found */
2686 p++;
2687 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002688 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002689 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002690 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002691 count = count * 10 + (*p - '0');
2692 p++;
2693 }
2694 }
2695 size = -1;
2696 format = -1;
2697 for(;;) {
2698 switch(*p) {
2699 case 'o':
2700 case 'd':
2701 case 'u':
2702 case 'x':
2703 case 'i':
2704 case 'c':
2705 format = *p++;
2706 break;
2707 case 'b':
2708 size = 1;
2709 p++;
2710 break;
2711 case 'h':
2712 size = 2;
2713 p++;
2714 break;
2715 case 'w':
2716 size = 4;
2717 p++;
2718 break;
2719 case 'g':
2720 case 'L':
2721 size = 8;
2722 p++;
2723 break;
2724 default:
2725 goto next;
2726 }
2727 }
2728 next:
blueswir1cd390082008-11-16 13:53:32 +00002729 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002730 monitor_printf(mon, "invalid char in format: '%c'\n",
2731 *p);
bellard9307c4c2004-04-04 12:57:25 +00002732 goto fail;
2733 }
bellard9307c4c2004-04-04 12:57:25 +00002734 if (format < 0)
2735 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002736 if (format != 'i') {
2737 /* for 'i', not specifying a size gives -1 as size */
2738 if (size < 0)
2739 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002740 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002741 }
bellard9307c4c2004-04-04 12:57:25 +00002742 default_fmt_format = format;
2743 } else {
2744 count = 1;
2745 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002746 if (format != 'i') {
2747 size = default_fmt_size;
2748 } else {
2749 size = -1;
2750 }
bellard9307c4c2004-04-04 12:57:25 +00002751 }
2752 if (nb_args + 3 > MAX_ARGS)
2753 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002754 args[nb_args++] = (void*)(long)count;
2755 args[nb_args++] = (void*)(long)format;
2756 args[nb_args++] = (void*)(long)size;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002757 qdict_put(qdict, "count", qint_from_int(count));
2758 qdict_put(qdict, "format", qint_from_int(format));
2759 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002760 }
2761 break;
2762 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002763 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002764 {
blueswir1c2efc952007-09-25 17:28:42 +00002765 int64_t val;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002766 int dict_add = 1;
blueswir17743e582007-09-24 18:39:04 +00002767
blueswir1cd390082008-11-16 13:53:32 +00002768 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002769 p++;
bellard34405572004-06-08 00:55:58 +00002770 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002771 if (*typestr == '?') {
2772 if (*p == '\0')
2773 has_arg = 0;
2774 else
2775 has_arg = 1;
2776 } else {
2777 if (*p == '.') {
2778 p++;
blueswir1cd390082008-11-16 13:53:32 +00002779 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002780 p++;
2781 has_arg = 1;
2782 } else {
2783 has_arg = 0;
2784 }
2785 }
bellard13224a82006-07-14 22:03:35 +00002786 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002787 if (nb_args >= MAX_ARGS)
2788 goto error_args;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002789 dict_add = has_arg;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002790 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002791 if (!has_arg) {
2792 if (nb_args >= MAX_ARGS)
2793 goto error_args;
2794 val = -1;
2795 goto add_num;
2796 }
2797 }
aliguori376253e2009-03-05 23:01:23 +00002798 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002799 goto fail;
2800 add_num:
bellard92a31b12005-02-10 22:00:52 +00002801 if (c == 'i') {
2802 if (nb_args >= MAX_ARGS)
2803 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002804 args[nb_args++] = (void *)(long)val;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002805 if (dict_add)
2806 qdict_put(qdict, key, qint_from_int(val));
bellard92a31b12005-02-10 22:00:52 +00002807 } else {
2808 if ((nb_args + 1) >= MAX_ARGS)
2809 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002810#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002811 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002812#else
2813 args[nb_args++] = (void *)0;
2814#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002815 args[nb_args++] = (void *)(long)(val & 0xffffffff);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002816 qdict_put(qdict, key, qint_from_int(val));
bellard92a31b12005-02-10 22:00:52 +00002817 }
bellard9307c4c2004-04-04 12:57:25 +00002818 }
2819 break;
2820 case '-':
2821 {
2822 int has_option;
2823 /* option */
ths3b46e622007-09-17 08:09:54 +00002824
bellard9307c4c2004-04-04 12:57:25 +00002825 c = *typestr++;
2826 if (c == '\0')
2827 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002828 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002829 p++;
2830 has_option = 0;
2831 if (*p == '-') {
2832 p++;
2833 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002834 monitor_printf(mon, "%s: unsupported option -%c\n",
2835 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002836 goto fail;
2837 }
2838 p++;
2839 has_option = 1;
2840 }
2841 if (nb_args >= MAX_ARGS)
2842 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002843 args[nb_args++] = (void *)(long)has_option;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002844 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00002845 }
2846 break;
2847 default:
2848 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002849 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002850 goto fail;
2851 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002852 qemu_free(key);
2853 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002854 }
2855 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002856 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002857 p++;
2858 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002859 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2860 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002861 goto fail;
2862 }
2863
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002864 qemu_errors_to_mon(mon);
bellard9307c4c2004-04-04 12:57:25 +00002865 switch(nb_args) {
2866 case 0:
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002867 case 1:
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03002868 handler_d = cmd->handler;
2869 handler_d(mon, qdict);
bellard9307c4c2004-04-04 12:57:25 +00002870 break;
bellard9307c4c2004-04-04 12:57:25 +00002871 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002872 handler_2 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002873 handler_2(mon, args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002874 break;
2875 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002876 handler_3 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002877 handler_3(mon, args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002878 break;
2879 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002880 handler_4 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002881 handler_4(mon, args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002882 break;
2883 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002884 handler_5 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002885 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002886 break;
bellard34405572004-06-08 00:55:58 +00002887 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002888 handler_6 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002889 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002890 break;
bellardec36b692006-07-16 18:57:03 +00002891 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002892 handler_7 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002893 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2894 args[6]);
bellardec36b692006-07-16 18:57:03 +00002895 break;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002896 case 8:
2897 handler_8 = cmd->handler;
2898 handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2899 args[6], args[7]);
2900 break;
2901 case 9:
2902 handler_9 = cmd->handler;
2903 handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2904 args[6], args[7], args[8]);
2905 break;
2906 case 10:
2907 handler_10 = cmd->handler;
2908 handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2909 args[6], args[7], args[8], args[9]);
2910 break;
bellard9307c4c2004-04-04 12:57:25 +00002911 default:
aliguori376253e2009-03-05 23:01:23 +00002912 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002913 break;
bellard9307c4c2004-04-04 12:57:25 +00002914 }
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002915 qemu_errors_to_previous();
2916
bellard9307c4c2004-04-04 12:57:25 +00002917 fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002918 qemu_free(key);
bellard9307c4c2004-04-04 12:57:25 +00002919 for(i = 0; i < MAX_ARGS; i++)
2920 qemu_free(str_allocated[i]);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002921 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002922}
2923
bellard81d09122004-07-14 17:21:37 +00002924static void cmd_completion(const char *name, const char *list)
2925{
2926 const char *p, *pstart;
2927 char cmd[128];
2928 int len;
2929
2930 p = list;
2931 for(;;) {
2932 pstart = p;
2933 p = strchr(p, '|');
2934 if (!p)
2935 p = pstart + strlen(pstart);
2936 len = p - pstart;
2937 if (len > sizeof(cmd) - 2)
2938 len = sizeof(cmd) - 2;
2939 memcpy(cmd, pstart, len);
2940 cmd[len] = '\0';
2941 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002942 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002943 }
2944 if (*p == '\0')
2945 break;
2946 p++;
2947 }
2948}
2949
2950static void file_completion(const char *input)
2951{
2952 DIR *ffs;
2953 struct dirent *d;
2954 char path[1024];
2955 char file[1024], file_prefix[1024];
2956 int input_path_len;
2957 const char *p;
2958
ths5fafdf22007-09-16 21:08:06 +00002959 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002960 if (!p) {
2961 input_path_len = 0;
2962 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002963 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002964 } else {
2965 input_path_len = p - input + 1;
2966 memcpy(path, input, input_path_len);
2967 if (input_path_len > sizeof(path) - 1)
2968 input_path_len = sizeof(path) - 1;
2969 path[input_path_len] = '\0';
2970 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2971 }
2972#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002973 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2974 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002975#endif
2976 ffs = opendir(path);
2977 if (!ffs)
2978 return;
2979 for(;;) {
2980 struct stat sb;
2981 d = readdir(ffs);
2982 if (!d)
2983 break;
2984 if (strstart(d->d_name, file_prefix, NULL)) {
2985 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002986 if (input_path_len < sizeof(file))
2987 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2988 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002989 /* stat the file to find out if it's a directory.
2990 * In that case add a slash to speed up typing long paths
2991 */
2992 stat(file, &sb);
2993 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002994 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00002995 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002996 }
2997 }
2998 closedir(ffs);
2999}
3000
aliguori51de9762009-03-05 23:00:43 +00003001static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003002{
aliguori51de9762009-03-05 23:00:43 +00003003 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003004 const char *input = opaque;
3005
3006 if (input[0] == '\0' ||
3007 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003008 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003009 }
3010}
3011
3012/* NOTE: this parser is an approximate form of the real command parser */
3013static void parse_cmdline(const char *cmdline,
3014 int *pnb_args, char **args)
3015{
3016 const char *p;
3017 int nb_args, ret;
3018 char buf[1024];
3019
3020 p = cmdline;
3021 nb_args = 0;
3022 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003023 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003024 p++;
3025 if (*p == '\0')
3026 break;
3027 if (nb_args >= MAX_ARGS)
3028 break;
3029 ret = get_str(buf, sizeof(buf), &p);
3030 args[nb_args] = qemu_strdup(buf);
3031 nb_args++;
3032 if (ret < 0)
3033 break;
3034 }
3035 *pnb_args = nb_args;
3036}
3037
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003038static const char *next_arg_type(const char *typestr)
3039{
3040 const char *p = strchr(typestr, ':');
3041 return (p != NULL ? ++p : typestr);
3042}
3043
aliguori4c36ba32009-03-05 23:01:37 +00003044static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003045{
3046 const char *cmdname;
3047 char *args[MAX_ARGS];
3048 int nb_args, i, len;
3049 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00003050 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003051 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003052
3053 parse_cmdline(cmdline, &nb_args, args);
3054#ifdef DEBUG_COMPLETION
3055 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003056 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003057 }
3058#endif
3059
3060 /* if the line ends with a space, it means we want to complete the
3061 next arg */
3062 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003063 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003064 if (nb_args >= MAX_ARGS)
3065 return;
3066 args[nb_args++] = qemu_strdup("");
3067 }
3068 if (nb_args <= 1) {
3069 /* command completion */
3070 if (nb_args == 0)
3071 cmdname = "";
3072 else
3073 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003074 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003075 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003076 cmd_completion(cmdname, cmd->name);
3077 }
3078 } else {
3079 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003080 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003081 if (compare_cmd(args[0], cmd->name))
3082 goto found;
3083 }
3084 return;
3085 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003086 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003087 for(i = 0; i < nb_args - 2; i++) {
3088 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003089 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003090 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003091 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003092 }
3093 }
3094 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003095 if (*ptype == '-' && ptype[1] != '\0') {
3096 ptype += 2;
3097 }
bellard81d09122004-07-14 17:21:37 +00003098 switch(*ptype) {
3099 case 'F':
3100 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003101 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003102 file_completion(str);
3103 break;
3104 case 'B':
3105 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003106 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003107 bdrv_iterate(block_completion_it, (void *)str);
3108 break;
bellard7fe48482004-10-09 18:08:01 +00003109 case 's':
3110 /* XXX: more generic ? */
3111 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003112 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003113 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3114 cmd_completion(str, cmd->name);
3115 }
bellard64866c32006-05-07 18:03:31 +00003116 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003117 char *sep = strrchr(str, '-');
3118 if (sep)
3119 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003120 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003121 for(key = key_defs; key->name != NULL; key++) {
3122 cmd_completion(str, key->name);
3123 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003124 } else if (!strcmp(cmd->name, "help|?")) {
3125 readline_set_completion_index(cur_mon->rs, strlen(str));
3126 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3127 cmd_completion(str, cmd->name);
3128 }
bellard7fe48482004-10-09 18:08:01 +00003129 }
3130 break;
bellard81d09122004-07-14 17:21:37 +00003131 default:
3132 break;
3133 }
3134 }
3135 for(i = 0; i < nb_args; i++)
3136 qemu_free(args[i]);
3137}
3138
aliguori731b0362009-03-05 23:01:42 +00003139static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003140{
aliguori731b0362009-03-05 23:01:42 +00003141 Monitor *mon = opaque;
3142
3143 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003144}
3145
aliguori731b0362009-03-05 23:01:42 +00003146static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003147{
aliguori731b0362009-03-05 23:01:42 +00003148 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003149 int i;
aliguori376253e2009-03-05 23:01:23 +00003150
aliguori731b0362009-03-05 23:01:42 +00003151 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003152
aliguoricde76ee2009-03-05 23:01:51 +00003153 if (cur_mon->rs) {
3154 for (i = 0; i < size; i++)
3155 readline_handle_byte(cur_mon->rs, buf[i]);
3156 } else {
3157 if (size == 0 || buf[size - 1] != 0)
3158 monitor_printf(cur_mon, "corrupted command\n");
3159 else
3160 monitor_handle_command(cur_mon, (char *)buf);
3161 }
aliguori731b0362009-03-05 23:01:42 +00003162
3163 cur_mon = old_mon;
3164}
aliguorid8f44602008-10-06 13:52:44 +00003165
aliguori376253e2009-03-05 23:01:23 +00003166static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003167{
aliguori731b0362009-03-05 23:01:42 +00003168 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003169 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003170 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003171}
3172
aliguoricde76ee2009-03-05 23:01:51 +00003173int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003174{
aliguoricde76ee2009-03-05 23:01:51 +00003175 if (!mon->rs)
3176 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003177 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003178 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003179}
3180
aliguori376253e2009-03-05 23:01:23 +00003181void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003182{
aliguoricde76ee2009-03-05 23:01:51 +00003183 if (!mon->rs)
3184 return;
aliguori731b0362009-03-05 23:01:42 +00003185 if (--mon->suspend_cnt == 0)
3186 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003187}
3188
aliguori731b0362009-03-05 23:01:42 +00003189static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003190{
aliguori376253e2009-03-05 23:01:23 +00003191 Monitor *mon = opaque;
3192
aliguori2724b182009-03-05 23:01:47 +00003193 switch (event) {
3194 case CHR_EVENT_MUX_IN:
3195 readline_restart(mon->rs);
3196 monitor_resume(mon);
3197 monitor_flush(mon);
3198 break;
ths86e94de2007-01-05 22:01:59 +00003199
aliguori2724b182009-03-05 23:01:47 +00003200 case CHR_EVENT_MUX_OUT:
3201 if (mon->suspend_cnt == 0)
3202 monitor_printf(mon, "\n");
3203 monitor_flush(mon);
3204 monitor_suspend(mon);
3205 break;
3206
3207 case CHR_EVENT_RESET:
3208 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3209 "information\n", QEMU_VERSION);
3210 if (mon->chr->focus == 0)
3211 readline_show_prompt(mon->rs);
3212 break;
3213 }
ths86e94de2007-01-05 22:01:59 +00003214}
3215
aliguori76655d62009-03-06 20:27:37 +00003216
3217/*
3218 * Local variables:
3219 * c-indent-level: 4
3220 * c-basic-offset: 4
3221 * tab-width: 8
3222 * End:
3223 */
3224
aliguori731b0362009-03-05 23:01:42 +00003225void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003226{
aliguori731b0362009-03-05 23:01:42 +00003227 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003228 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003229
3230 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003231 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003232 is_first_init = 0;
3233 }
aliguori87127162009-03-05 23:01:29 +00003234
3235 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003236
aliguori87127162009-03-05 23:01:29 +00003237 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003238 mon->flags = flags;
aliguori2724b182009-03-05 23:01:47 +00003239 if (mon->chr->focus != 0)
3240 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
aliguoricde76ee2009-03-05 23:01:51 +00003241 if (flags & MONITOR_USE_READLINE) {
3242 mon->rs = readline_init(mon, monitor_find_completion);
3243 monitor_read_command(mon, 0);
3244 }
aliguori87127162009-03-05 23:01:29 +00003245
aliguori731b0362009-03-05 23:01:42 +00003246 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3247 mon);
aliguori87127162009-03-05 23:01:29 +00003248
3249 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003250 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003251 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003252}
3253
aliguori376253e2009-03-05 23:01:23 +00003254static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003255{
aliguoribb5fc202009-03-05 23:01:15 +00003256 BlockDriverState *bs = opaque;
3257 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003258
aliguoribb5fc202009-03-05 23:01:15 +00003259 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003260 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003261 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003262 }
aliguori731b0362009-03-05 23:01:42 +00003263 if (mon->password_completion_cb)
3264 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003265
aliguori731b0362009-03-05 23:01:42 +00003266 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003267}
aliguoric0f4ce72009-03-05 23:01:01 +00003268
aliguori376253e2009-03-05 23:01:23 +00003269void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003270 BlockDriverCompletionFunc *completion_cb,
3271 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003272{
aliguoricde76ee2009-03-05 23:01:51 +00003273 int err;
3274
aliguoribb5fc202009-03-05 23:01:15 +00003275 if (!bdrv_key_required(bs)) {
3276 if (completion_cb)
3277 completion_cb(opaque, 0);
3278 return;
3279 }
aliguoric0f4ce72009-03-05 23:01:01 +00003280
aliguori376253e2009-03-05 23:01:23 +00003281 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3282 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003283
aliguori731b0362009-03-05 23:01:42 +00003284 mon->password_completion_cb = completion_cb;
3285 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003286
aliguoricde76ee2009-03-05 23:01:51 +00003287 err = monitor_read_password(mon, bdrv_password_cb, bs);
3288
3289 if (err && completion_cb)
3290 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003291}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003292
3293typedef struct QemuErrorSink QemuErrorSink;
3294struct QemuErrorSink {
3295 enum {
3296 ERR_SINK_FILE,
3297 ERR_SINK_MONITOR,
3298 } dest;
3299 union {
3300 FILE *fp;
3301 Monitor *mon;
3302 };
3303 QemuErrorSink *previous;
3304};
3305
Blue Swirl528e93a2009-08-31 15:14:40 +00003306static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003307
3308void qemu_errors_to_file(FILE *fp)
3309{
3310 QemuErrorSink *sink;
3311
3312 sink = qemu_mallocz(sizeof(*sink));
3313 sink->dest = ERR_SINK_FILE;
3314 sink->fp = fp;
3315 sink->previous = qemu_error_sink;
3316 qemu_error_sink = sink;
3317}
3318
3319void qemu_errors_to_mon(Monitor *mon)
3320{
3321 QemuErrorSink *sink;
3322
3323 sink = qemu_mallocz(sizeof(*sink));
3324 sink->dest = ERR_SINK_MONITOR;
3325 sink->mon = mon;
3326 sink->previous = qemu_error_sink;
3327 qemu_error_sink = sink;
3328}
3329
3330void qemu_errors_to_previous(void)
3331{
3332 QemuErrorSink *sink;
3333
3334 assert(qemu_error_sink != NULL);
3335 sink = qemu_error_sink;
3336 qemu_error_sink = sink->previous;
3337 qemu_free(sink);
3338}
3339
3340void qemu_error(const char *fmt, ...)
3341{
3342 va_list args;
3343
3344 assert(qemu_error_sink != NULL);
3345 switch (qemu_error_sink->dest) {
3346 case ERR_SINK_FILE:
3347 va_start(args, fmt);
3348 vfprintf(qemu_error_sink->fp, fmt, args);
3349 va_end(args);
3350 break;
3351 case ERR_SINK_MONITOR:
3352 va_start(args, fmt);
3353 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3354 va_end(args);
3355 break;
3356 }
3357}