blob: 8607d6c2f444c52c3cabb35f9dc9e206b1a92307 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Gerd Hoffmanncae49562009-06-05 15:53:17 +010026#include "hw/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
28#include "hw/pcmcia.h"
29#include "hw/pc.h"
30#include "hw/pci.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010031#include "hw/watchdog.h"
pbrook87ecb682007-11-17 17:14:51 +000032#include "gdbstub.h"
33#include "net.h"
34#include "qemu-char.h"
35#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000036#include "monitor.h"
37#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000038#include "console.h"
39#include "block.h"
40#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000041#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000042#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000043#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000044#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000045#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000046#include "acl.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030047#include "qint.h"
48#include "qdict.h"
49#include "qstring.h"
ths6a5bd302007-12-03 17:05:38 +000050
bellard9dc39cb2004-03-14 21:38:27 +000051//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000052//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000053
bellard9307c4c2004-04-04 12:57:25 +000054/*
55 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000056 *
bellard9307c4c2004-04-04 12:57:25 +000057 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000058 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000059 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000060 * 'i' 32 bit integer
61 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000062 * '/' optional gdb-like print format (like "/10x")
63 *
64 * '?' optional type (for 'F', 's' and 'i')
65 *
66 */
67
aliguori376253e2009-03-05 23:01:23 +000068typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000069 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000070 const char *args_type;
blueswir1a5f1b962008-08-17 20:21:51 +000071 void *handler;
bellard9dc39cb2004-03-14 21:38:27 +000072 const char *params;
73 const char *help;
aliguori376253e2009-03-05 23:01:23 +000074} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000075
Mark McLoughlinf07918f2009-07-22 09:11:40 +010076/* file descriptors passed via SCM_RIGHTS */
77typedef struct mon_fd_t mon_fd_t;
78struct mon_fd_t {
79 char *name;
80 int fd;
81 LIST_ENTRY(mon_fd_t) next;
82};
83
aliguori87127162009-03-05 23:01:29 +000084struct Monitor {
85 CharDriverState *chr;
aliguori731b0362009-03-05 23:01:42 +000086 int flags;
87 int suspend_cnt;
88 uint8_t outbuf[1024];
89 int outbuf_index;
90 ReadLineState *rs;
91 CPUState *mon_cpu;
92 BlockDriverCompletionFunc *password_completion_cb;
93 void *password_opaque;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010094 LIST_HEAD(,mon_fd_t) fds;
aliguori87127162009-03-05 23:01:29 +000095 LIST_ENTRY(Monitor) entry;
96};
97
98static LIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +000099
aliguori376253e2009-03-05 23:01:23 +0000100static const mon_cmd_t mon_cmds[];
101static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000102
aliguori87127162009-03-05 23:01:29 +0000103Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000104
aliguori731b0362009-03-05 23:01:42 +0000105static void monitor_command_cb(Monitor *mon, const char *cmdline,
106 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000107
aliguori731b0362009-03-05 23:01:42 +0000108static void monitor_read_command(Monitor *mon, int show_prompt)
109{
110 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
111 if (show_prompt)
112 readline_show_prompt(mon->rs);
113}
bellard6a00d602005-11-21 23:25:50 +0000114
aliguoricde76ee2009-03-05 23:01:51 +0000115static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
116 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000117{
aliguoricde76ee2009-03-05 23:01:51 +0000118 if (mon->rs) {
119 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
120 /* prompt is printed on return from the command handler */
121 return 0;
122 } else {
123 monitor_printf(mon, "terminal does not support password prompting\n");
124 return -ENOTTY;
125 }
aliguoribb5fc202009-03-05 23:01:15 +0000126}
127
aliguori376253e2009-03-05 23:01:23 +0000128void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000129{
aliguori731b0362009-03-05 23:01:42 +0000130 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
131 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
132 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000133 }
134}
135
136/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000137static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000138{
ths60fe76f2007-12-16 03:02:09 +0000139 char c;
aliguori731b0362009-03-05 23:01:42 +0000140
141 if (!mon)
142 return;
143
bellard7e2515e2004-08-01 21:52:19 +0000144 for(;;) {
145 c = *str++;
146 if (c == '\0')
147 break;
bellard7ba12602006-07-14 20:26:42 +0000148 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000149 mon->outbuf[mon->outbuf_index++] = '\r';
150 mon->outbuf[mon->outbuf_index++] = c;
151 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
152 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000153 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000154 }
155}
156
aliguori376253e2009-03-05 23:01:23 +0000157void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000158{
159 char buf[4096];
160 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000161 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000162}
163
aliguori376253e2009-03-05 23:01:23 +0000164void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000165{
166 va_list ap;
167 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000168 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000169 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000170}
171
aliguori376253e2009-03-05 23:01:23 +0000172void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000173{
174 int i;
175
176 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000177 switch (filename[i]) {
178 case ' ':
179 case '"':
180 case '\\':
181 monitor_printf(mon, "\\%c", filename[i]);
182 break;
183 case '\t':
184 monitor_printf(mon, "\\t");
185 break;
186 case '\r':
187 monitor_printf(mon, "\\r");
188 break;
189 case '\n':
190 monitor_printf(mon, "\\n");
191 break;
192 default:
193 monitor_printf(mon, "%c", filename[i]);
194 break;
195 }
thsfef30742006-12-22 14:11:32 +0000196 }
197}
198
bellard7fe48482004-10-09 18:08:01 +0000199static int monitor_fprintf(FILE *stream, const char *fmt, ...)
200{
201 va_list ap;
202 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000203 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000204 va_end(ap);
205 return 0;
206}
207
bellard9dc39cb2004-03-14 21:38:27 +0000208static int compare_cmd(const char *name, const char *list)
209{
210 const char *p, *pstart;
211 int len;
212 len = strlen(name);
213 p = list;
214 for(;;) {
215 pstart = p;
216 p = strchr(p, '|');
217 if (!p)
218 p = pstart + strlen(pstart);
219 if ((p - pstart) == len && !memcmp(pstart, name, len))
220 return 1;
221 if (*p == '\0')
222 break;
223 p++;
224 }
225 return 0;
226}
227
aliguori376253e2009-03-05 23:01:23 +0000228static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
229 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000230{
aliguori376253e2009-03-05 23:01:23 +0000231 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000232
233 for(cmd = cmds; cmd->name != NULL; cmd++) {
234 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000235 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
236 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000237 }
238}
239
aliguori376253e2009-03-05 23:01:23 +0000240static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000241{
242 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000243 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000244 } else {
aliguori376253e2009-03-05 23:01:23 +0000245 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000246 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000247 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000248 monitor_printf(mon, "Log items (comma separated):\n");
249 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000250 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000251 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000252 }
253 }
bellard9dc39cb2004-03-14 21:38:27 +0000254 }
255}
256
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300257static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300258{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300259 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300260}
261
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300262static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000263{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200264 int all_devices;
265 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300266 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000267
bellard7954c732006-08-01 15:52:40 +0000268 all_devices = !strcmp(device, "all");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200269 TAILQ_FOREACH(dinfo, &drives, next) {
270 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300271 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200272 continue;
273 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000274 }
275}
276
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300277static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000278{
aliguori376253e2009-03-05 23:01:23 +0000279 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300280 const char *item = qdict_get_try_str(qdict, "item");
aliguori376253e2009-03-05 23:01:23 +0000281 void (*handler)(Monitor *);
bellard9dc39cb2004-03-14 21:38:27 +0000282
bellard9307c4c2004-04-04 12:57:25 +0000283 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000284 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000285 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000286 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000287 goto found;
288 }
289 help:
aliguori376253e2009-03-05 23:01:23 +0000290 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000291 return;
292 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000293 handler = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +0000294 handler(mon);
bellard9dc39cb2004-03-14 21:38:27 +0000295}
296
aliguori376253e2009-03-05 23:01:23 +0000297static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000298{
pbrook4a19f1e2009-04-07 23:17:49 +0000299 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000300}
301
aliguori376253e2009-03-05 23:01:23 +0000302static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000303{
304 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000305 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000306}
307
aurel32bf4f74c2008-12-18 22:42:34 +0000308#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000309static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000310{
aliguori376253e2009-03-05 23:01:23 +0000311 monitor_printf(mon, "HPET is %s by QEMU\n",
312 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000313}
aurel32bf4f74c2008-12-18 22:42:34 +0000314#endif
aliguori16b29ae2008-12-17 23:28:44 +0000315
aliguori376253e2009-03-05 23:01:23 +0000316static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000317{
aliguori376253e2009-03-05 23:01:23 +0000318 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
319 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
320 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
321 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
322 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000323}
324
bellard6a00d602005-11-21 23:25:50 +0000325/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000326static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000327{
328 CPUState *env;
329
330 for(env = first_cpu; env != NULL; env = env->next_cpu) {
331 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000332 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000333 return 0;
334 }
335 }
336 return -1;
337}
338
pbrook9596ebb2007-11-18 01:44:38 +0000339static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000340{
aliguori731b0362009-03-05 23:01:42 +0000341 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000342 mon_set_cpu(0);
343 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300344 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000345 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000346}
347
aliguori376253e2009-03-05 23:01:23 +0000348static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000349{
bellard6a00d602005-11-21 23:25:50 +0000350 CPUState *env;
351 env = mon_get_cpu();
352 if (!env)
353 return;
bellard9307c4c2004-04-04 12:57:25 +0000354#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000355 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000356 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000357#else
aliguori376253e2009-03-05 23:01:23 +0000358 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000359 0);
bellard9307c4c2004-04-04 12:57:25 +0000360#endif
361}
362
aliguori376253e2009-03-05 23:01:23 +0000363static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000364{
365 CPUState *env;
366
367 /* just to set the default cpu if not already done */
368 mon_get_cpu();
369
370 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Avi Kivity4c0960c2009-08-17 23:19:53 +0300371 cpu_synchronize_state(env);
aliguori376253e2009-03-05 23:01:23 +0000372 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000373 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000374 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000375#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000376 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
377 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000378#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000379 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000380#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000381 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
382 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000383#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000384 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000385#endif
thsead93602007-09-06 00:18:15 +0000386 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000387 monitor_printf(mon, " (halted)");
388 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000389 }
390}
391
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300392static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000393{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300394 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000395 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000396 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000397}
398
aliguori376253e2009-03-05 23:01:23 +0000399static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000400{
aliguori376253e2009-03-05 23:01:23 +0000401 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000402}
403
aliguori376253e2009-03-05 23:01:23 +0000404static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000405{
406 int i;
bellard7e2515e2004-08-01 21:52:19 +0000407 const char *str;
ths3b46e622007-09-17 08:09:54 +0000408
aliguoricde76ee2009-03-05 23:01:51 +0000409 if (!mon->rs)
410 return;
bellard7e2515e2004-08-01 21:52:19 +0000411 i = 0;
412 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000413 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000414 if (!str)
415 break;
aliguori376253e2009-03-05 23:01:23 +0000416 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000417 i++;
bellardaa455482004-04-04 13:07:25 +0000418 }
419}
420
j_mayer76a66252007-03-07 08:32:30 +0000421#if defined(TARGET_PPC)
422/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000423static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000424{
425 CPUState *env;
426
427 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000428 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000429}
430#endif
431
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300432static void do_quit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000433{
434 exit(0);
435}
436
aliguori376253e2009-03-05 23:01:23 +0000437static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000438{
439 if (bdrv_is_inserted(bs)) {
440 if (!force) {
441 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000442 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000443 return -1;
444 }
445 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000446 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000447 return -1;
448 }
449 }
450 bdrv_close(bs);
451 }
452 return 0;
453}
454
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300455static void do_eject(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000456{
457 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300458 int force = qdict_get_int(qdict, "force");
459 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000460
bellard9307c4c2004-04-04 12:57:25 +0000461 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000462 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000463 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000464 return;
465 }
aliguori376253e2009-03-05 23:01:23 +0000466 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000467}
468
aliguori376253e2009-03-05 23:01:23 +0000469static void do_change_block(Monitor *mon, const char *device,
470 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000471{
472 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000473 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000474
bellard9307c4c2004-04-04 12:57:25 +0000475 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000476 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000477 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000478 return;
479 }
aurel322ecea9b2008-06-18 22:10:01 +0000480 if (fmt) {
481 drv = bdrv_find_format(fmt);
482 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000483 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000484 return;
485 }
486 }
aliguori376253e2009-03-05 23:01:23 +0000487 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000488 return;
aurel322ecea9b2008-06-18 22:10:01 +0000489 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000490 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000491}
492
aliguori376253e2009-03-05 23:01:23 +0000493static void change_vnc_password_cb(Monitor *mon, const char *password,
494 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000495{
496 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000497 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000498
aliguori731b0362009-03-05 23:01:42 +0000499 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000500}
501
aliguori376253e2009-03-05 23:01:23 +0000502static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000503{
ths70848512007-08-25 01:37:05 +0000504 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000505 strcmp(target, "password") == 0) {
506 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000507 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000508 strncpy(password, arg, sizeof(password));
509 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000510 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000511 } else {
aliguori376253e2009-03-05 23:01:23 +0000512 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000513 }
ths70848512007-08-25 01:37:05 +0000514 } else {
aliguori28a76be2009-03-06 20:27:40 +0000515 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000516 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000517 }
thse25a5822007-08-25 01:36:20 +0000518}
519
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300520static void do_change(Monitor *mon, const QDict *qdict)
thse25a5822007-08-25 01:36:20 +0000521{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300522 const char *device = qdict_get_str(qdict, "device");
523 const char *target = qdict_get_str(qdict, "target");
524 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000525 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000526 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000527 } else {
aliguori28a76be2009-03-06 20:27:40 +0000528 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000529 }
530}
531
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300532static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000533{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300534 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000535}
536
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300537static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000538{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300539 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000540}
541
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300542static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000543{
544 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300545 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000546
bellard9307c4c2004-04-04 12:57:25 +0000547 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000548 mask = 0;
549 } else {
bellard9307c4c2004-04-04 12:57:25 +0000550 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000551 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000552 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000553 return;
554 }
555 }
556 cpu_set_log(mask);
557}
558
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300559static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000560{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300561 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000562 if (!option || !strcmp(option, "on")) {
563 singlestep = 1;
564 } else if (!strcmp(option, "off")) {
565 singlestep = 0;
566 } else {
567 monitor_printf(mon, "unexpected option %s\n", option);
568 }
569}
570
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300571static void do_stop(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000572{
573 vm_stop(EXCP_INTERRUPT);
574}
575
aliguoribb5fc202009-03-05 23:01:15 +0000576static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000577
aliguori376253e2009-03-05 23:01:23 +0000578struct bdrv_iterate_context {
579 Monitor *mon;
580 int err;
581};
aliguoric0f4ce72009-03-05 23:01:01 +0000582
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300583static void do_cont(Monitor *mon, const QDict *qdict)
aliguori376253e2009-03-05 23:01:23 +0000584{
585 struct bdrv_iterate_context context = { mon, 0 };
586
587 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000588 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000589 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000590 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000591}
592
aliguoribb5fc202009-03-05 23:01:15 +0000593static void bdrv_key_cb(void *opaque, int err)
594{
aliguori376253e2009-03-05 23:01:23 +0000595 Monitor *mon = opaque;
596
aliguoribb5fc202009-03-05 23:01:15 +0000597 /* another key was set successfully, retry to continue */
598 if (!err)
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300599 do_cont(mon, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000600}
601
602static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
603{
aliguori376253e2009-03-05 23:01:23 +0000604 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000605
aliguori376253e2009-03-05 23:01:23 +0000606 if (!context->err && bdrv_key_required(bs)) {
607 context->err = -EBUSY;
608 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
609 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000610 }
611}
612
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300613static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000614{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300615 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000616 if (!device)
617 device = "tcp::" DEFAULT_GDBSTUB_PORT;
618 if (gdbserver_start(device) < 0) {
619 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
620 device);
621 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000622 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000623 } else {
aliguori59030a82009-04-05 18:43:41 +0000624 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
625 device);
bellard8a7ddc32004-03-31 19:00:16 +0000626 }
627}
628
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300629static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100630{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300631 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100632 if (select_watchdog_action(action) == -1) {
633 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
634 }
635}
636
aliguori376253e2009-03-05 23:01:23 +0000637static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000638{
aliguori376253e2009-03-05 23:01:23 +0000639 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000640 switch(c) {
641 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000642 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000643 break;
644 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000645 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000646 break;
647 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000648 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000649 break;
650 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000651 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000652 break;
653 default:
654 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000655 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000656 } else {
aliguori376253e2009-03-05 23:01:23 +0000657 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000658 }
659 break;
660 }
aliguori376253e2009-03-05 23:01:23 +0000661 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000662}
663
aliguori376253e2009-03-05 23:01:23 +0000664static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000665 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000666{
bellard6a00d602005-11-21 23:25:50 +0000667 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000668 int nb_per_line, l, line_size, i, max_digits, len;
669 uint8_t buf[16];
670 uint64_t v;
671
672 if (format == 'i') {
673 int flags;
674 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000675 env = mon_get_cpu();
676 if (!env && !is_physical)
677 return;
bellard9307c4c2004-04-04 12:57:25 +0000678#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000679 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000680 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000681 } else if (wsize == 4) {
682 flags = 0;
683 } else {
bellard6a15fd12006-04-12 21:07:07 +0000684 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000685 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000686 if (env) {
687#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000688 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000689 (env->segs[R_CS].flags & DESC_L_MASK))
690 flags = 2;
691 else
692#endif
693 if (!(env->segs[R_CS].flags & DESC_B_MASK))
694 flags = 1;
695 }
bellard4c27ba22004-04-25 18:05:08 +0000696 }
697#endif
aliguori376253e2009-03-05 23:01:23 +0000698 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000699 return;
700 }
701
702 len = wsize * count;
703 if (wsize == 1)
704 line_size = 8;
705 else
706 line_size = 16;
707 nb_per_line = line_size / wsize;
708 max_digits = 0;
709
710 switch(format) {
711 case 'o':
712 max_digits = (wsize * 8 + 2) / 3;
713 break;
714 default:
715 case 'x':
716 max_digits = (wsize * 8) / 4;
717 break;
718 case 'u':
719 case 'd':
720 max_digits = (wsize * 8 * 10 + 32) / 33;
721 break;
722 case 'c':
723 wsize = 1;
724 break;
725 }
726
727 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000728 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000729 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000730 else
aliguori376253e2009-03-05 23:01:23 +0000731 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000732 l = len;
733 if (l > line_size)
734 l = line_size;
735 if (is_physical) {
736 cpu_physical_memory_rw(addr, buf, l, 0);
737 } else {
bellard6a00d602005-11-21 23:25:50 +0000738 env = mon_get_cpu();
739 if (!env)
740 break;
aliguoric8f79b62008-08-18 14:00:20 +0000741 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000742 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000743 break;
744 }
bellard9307c4c2004-04-04 12:57:25 +0000745 }
ths5fafdf22007-09-16 21:08:06 +0000746 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000747 while (i < l) {
748 switch(wsize) {
749 default:
750 case 1:
751 v = ldub_raw(buf + i);
752 break;
753 case 2:
754 v = lduw_raw(buf + i);
755 break;
756 case 4:
bellard92a31b12005-02-10 22:00:52 +0000757 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000758 break;
759 case 8:
760 v = ldq_raw(buf + i);
761 break;
762 }
aliguori376253e2009-03-05 23:01:23 +0000763 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000764 switch(format) {
765 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000766 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000767 break;
768 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000769 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000770 break;
771 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000772 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000773 break;
774 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000775 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000776 break;
777 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000778 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000779 break;
780 }
781 i += wsize;
782 }
aliguori376253e2009-03-05 23:01:23 +0000783 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000784 addr += l;
785 len -= l;
786 }
787}
788
bellard92a31b12005-02-10 22:00:52 +0000789#if TARGET_LONG_BITS == 64
790#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
791#else
792#define GET_TLONG(h, l) (l)
793#endif
794
aliguori376253e2009-03-05 23:01:23 +0000795static void do_memory_dump(Monitor *mon, int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000796 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000797{
bellard92a31b12005-02-10 22:00:52 +0000798 target_long addr = GET_TLONG(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000799 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000800}
801
blueswir17743e582007-09-24 18:39:04 +0000802#if TARGET_PHYS_ADDR_BITS > 32
803#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
804#else
805#define GET_TPHYSADDR(h, l) (l)
806#endif
807
aliguori376253e2009-03-05 23:01:23 +0000808static void do_physical_memory_dump(Monitor *mon, int count, int format,
809 int size, uint32_t addrh, uint32_t addrl)
bellard92a31b12005-02-10 22:00:52 +0000810
bellard9307c4c2004-04-04 12:57:25 +0000811{
blueswir17743e582007-09-24 18:39:04 +0000812 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
aliguori376253e2009-03-05 23:01:23 +0000813 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000814}
815
aliguori376253e2009-03-05 23:01:23 +0000816static void do_print(Monitor *mon, int count, int format, int size,
817 unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000818{
blueswir17743e582007-09-24 18:39:04 +0000819 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
820#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000821 switch(format) {
822 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000823 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000824 break;
825 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000826 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000827 break;
828 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000829 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000830 break;
831 default:
832 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000833 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000834 break;
835 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000836 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000837 break;
838 }
bellard92a31b12005-02-10 22:00:52 +0000839#else
840 switch(format) {
841 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000842 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000843 break;
844 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000845 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000846 break;
847 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000848 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000849 break;
850 default:
851 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000852 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000853 break;
854 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000855 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000856 break;
857 }
858#endif
aliguori376253e2009-03-05 23:01:23 +0000859 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000860}
861
aliguori376253e2009-03-05 23:01:23 +0000862static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000863 uint32_t size, const char *filename)
864{
865 FILE *f;
866 target_long addr = GET_TLONG(valh, vall);
867 uint32_t l;
868 CPUState *env;
869 uint8_t buf[1024];
870
871 env = mon_get_cpu();
872 if (!env)
873 return;
874
875 f = fopen(filename, "wb");
876 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000877 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000878 return;
879 }
880 while (size != 0) {
881 l = sizeof(buf);
882 if (l > size)
883 l = size;
884 cpu_memory_rw_debug(env, addr, buf, l, 0);
885 fwrite(buf, 1, l, f);
886 addr += l;
887 size -= l;
888 }
889 fclose(f);
890}
891
aliguori376253e2009-03-05 23:01:23 +0000892static void do_physical_memory_save(Monitor *mon, unsigned int valh,
893 unsigned int vall, uint32_t size,
894 const char *filename)
aurel32a8bdf7a2008-04-11 21:36:14 +0000895{
896 FILE *f;
897 uint32_t l;
898 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000899 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000900
901 f = fopen(filename, "wb");
902 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000903 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000904 return;
905 }
906 while (size != 0) {
907 l = sizeof(buf);
908 if (l > size)
909 l = size;
910 cpu_physical_memory_rw(addr, buf, l, 0);
911 fwrite(buf, 1, l, f);
912 fflush(f);
913 addr += l;
914 size -= l;
915 }
916 fclose(f);
917}
918
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300919static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000920{
921 uint32_t addr;
922 uint8_t buf[1];
923 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300924 uint32_t start = qdict_get_int(qdict, "start");
925 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000926
927 sum = 0;
928 for(addr = start; addr < (start + size); addr++) {
929 cpu_physical_memory_rw(addr, buf, 1, 0);
930 /* BSD sum algorithm ('sum' Unix command) */
931 sum = (sum >> 1) | (sum << 15);
932 sum += buf[0];
933 }
aliguori376253e2009-03-05 23:01:23 +0000934 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000935}
936
bellarda3a91a32004-06-04 11:06:21 +0000937typedef struct {
938 int keycode;
939 const char *name;
940} KeyDef;
941
942static const KeyDef key_defs[] = {
943 { 0x2a, "shift" },
944 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000945
bellarda3a91a32004-06-04 11:06:21 +0000946 { 0x38, "alt" },
947 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000948 { 0x64, "altgr" },
949 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000950 { 0x1d, "ctrl" },
951 { 0x9d, "ctrl_r" },
952
953 { 0xdd, "menu" },
954
955 { 0x01, "esc" },
956
957 { 0x02, "1" },
958 { 0x03, "2" },
959 { 0x04, "3" },
960 { 0x05, "4" },
961 { 0x06, "5" },
962 { 0x07, "6" },
963 { 0x08, "7" },
964 { 0x09, "8" },
965 { 0x0a, "9" },
966 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000967 { 0x0c, "minus" },
968 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000969 { 0x0e, "backspace" },
970
971 { 0x0f, "tab" },
972 { 0x10, "q" },
973 { 0x11, "w" },
974 { 0x12, "e" },
975 { 0x13, "r" },
976 { 0x14, "t" },
977 { 0x15, "y" },
978 { 0x16, "u" },
979 { 0x17, "i" },
980 { 0x18, "o" },
981 { 0x19, "p" },
982
983 { 0x1c, "ret" },
984
985 { 0x1e, "a" },
986 { 0x1f, "s" },
987 { 0x20, "d" },
988 { 0x21, "f" },
989 { 0x22, "g" },
990 { 0x23, "h" },
991 { 0x24, "j" },
992 { 0x25, "k" },
993 { 0x26, "l" },
994
995 { 0x2c, "z" },
996 { 0x2d, "x" },
997 { 0x2e, "c" },
998 { 0x2f, "v" },
999 { 0x30, "b" },
1000 { 0x31, "n" },
1001 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001002 { 0x33, "comma" },
1003 { 0x34, "dot" },
1004 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001005
balrog4d3b6f62008-02-10 16:33:14 +00001006 { 0x37, "asterisk" },
1007
bellarda3a91a32004-06-04 11:06:21 +00001008 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001009 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001010 { 0x3b, "f1" },
1011 { 0x3c, "f2" },
1012 { 0x3d, "f3" },
1013 { 0x3e, "f4" },
1014 { 0x3f, "f5" },
1015 { 0x40, "f6" },
1016 { 0x41, "f7" },
1017 { 0x42, "f8" },
1018 { 0x43, "f9" },
1019 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001020 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001021 { 0x46, "scroll_lock" },
1022
bellard64866c32006-05-07 18:03:31 +00001023 { 0xb5, "kp_divide" },
1024 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001025 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001026 { 0x4e, "kp_add" },
1027 { 0x9c, "kp_enter" },
1028 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001029 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001030
1031 { 0x52, "kp_0" },
1032 { 0x4f, "kp_1" },
1033 { 0x50, "kp_2" },
1034 { 0x51, "kp_3" },
1035 { 0x4b, "kp_4" },
1036 { 0x4c, "kp_5" },
1037 { 0x4d, "kp_6" },
1038 { 0x47, "kp_7" },
1039 { 0x48, "kp_8" },
1040 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001041
bellarda3a91a32004-06-04 11:06:21 +00001042 { 0x56, "<" },
1043
1044 { 0x57, "f11" },
1045 { 0x58, "f12" },
1046
1047 { 0xb7, "print" },
1048
1049 { 0xc7, "home" },
1050 { 0xc9, "pgup" },
1051 { 0xd1, "pgdn" },
1052 { 0xcf, "end" },
1053
1054 { 0xcb, "left" },
1055 { 0xc8, "up" },
1056 { 0xd0, "down" },
1057 { 0xcd, "right" },
1058
1059 { 0xd2, "insert" },
1060 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001061#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1062 { 0xf0, "stop" },
1063 { 0xf1, "again" },
1064 { 0xf2, "props" },
1065 { 0xf3, "undo" },
1066 { 0xf4, "front" },
1067 { 0xf5, "copy" },
1068 { 0xf6, "open" },
1069 { 0xf7, "paste" },
1070 { 0xf8, "find" },
1071 { 0xf9, "cut" },
1072 { 0xfa, "lf" },
1073 { 0xfb, "help" },
1074 { 0xfc, "meta_l" },
1075 { 0xfd, "meta_r" },
1076 { 0xfe, "compose" },
1077#endif
bellarda3a91a32004-06-04 11:06:21 +00001078 { 0, NULL },
1079};
1080
1081static int get_keycode(const char *key)
1082{
1083 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001084 char *endp;
1085 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001086
1087 for(p = key_defs; p->name != NULL; p++) {
1088 if (!strcmp(key, p->name))
1089 return p->keycode;
1090 }
bellard64866c32006-05-07 18:03:31 +00001091 if (strstart(key, "0x", NULL)) {
1092 ret = strtoul(key, &endp, 0);
1093 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1094 return ret;
1095 }
bellarda3a91a32004-06-04 11:06:21 +00001096 return -1;
1097}
1098
balrogc8256f92008-06-08 22:45:01 +00001099#define MAX_KEYCODES 16
1100static uint8_t keycodes[MAX_KEYCODES];
1101static int nb_pending_keycodes;
1102static QEMUTimer *key_timer;
1103
1104static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001105{
balrogc8256f92008-06-08 22:45:01 +00001106 int keycode;
1107
1108 while (nb_pending_keycodes > 0) {
1109 nb_pending_keycodes--;
1110 keycode = keycodes[nb_pending_keycodes];
1111 if (keycode & 0x80)
1112 kbd_put_keycode(0xe0);
1113 kbd_put_keycode(keycode | 0x80);
1114 }
1115}
1116
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001117static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001118{
balrog3401c0d2008-06-04 10:05:59 +00001119 char keyname_buf[16];
1120 char *separator;
1121 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001122 const char *string = qdict_get_str(qdict, "string");
1123 int has_hold_time = qdict_haskey(qdict, "hold_time");
1124 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001125
balrogc8256f92008-06-08 22:45:01 +00001126 if (nb_pending_keycodes > 0) {
1127 qemu_del_timer(key_timer);
1128 release_keys(NULL);
1129 }
1130 if (!has_hold_time)
1131 hold_time = 100;
1132 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001133 while (1) {
1134 separator = strchr(string, '-');
1135 keyname_len = separator ? separator - string : strlen(string);
1136 if (keyname_len > 0) {
1137 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1138 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001139 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001140 return;
bellarda3a91a32004-06-04 11:06:21 +00001141 }
balrogc8256f92008-06-08 22:45:01 +00001142 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001143 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001144 return;
1145 }
1146 keyname_buf[keyname_len] = 0;
1147 keycode = get_keycode(keyname_buf);
1148 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001149 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001150 return;
1151 }
balrogc8256f92008-06-08 22:45:01 +00001152 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001153 }
balrog3401c0d2008-06-04 10:05:59 +00001154 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001155 break;
balrog3401c0d2008-06-04 10:05:59 +00001156 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001157 }
balrogc8256f92008-06-08 22:45:01 +00001158 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001159 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001160 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001161 keycode = keycodes[i];
1162 if (keycode & 0x80)
1163 kbd_put_keycode(0xe0);
1164 kbd_put_keycode(keycode & 0x7f);
1165 }
balrogc8256f92008-06-08 22:45:01 +00001166 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001167 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1168 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001169}
1170
bellard13224a82006-07-14 22:03:35 +00001171static int mouse_button_state;
1172
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001173static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001174{
1175 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001176 const char *dx_str = qdict_get_str(qdict, "dx_str");
1177 const char *dy_str = qdict_get_str(qdict, "dy_str");
1178 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001179 dx = strtol(dx_str, NULL, 0);
1180 dy = strtol(dy_str, NULL, 0);
1181 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001182 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001183 dz = strtol(dz_str, NULL, 0);
1184 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1185}
1186
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001187static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001188{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001189 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001190 mouse_button_state = button_state;
1191 kbd_mouse_event(0, 0, 0, mouse_button_state);
1192}
1193
aliguori376253e2009-03-05 23:01:23 +00001194static void do_ioport_read(Monitor *mon, int count, int format, int size,
1195 int addr, int has_index, int index)
bellard34405572004-06-08 00:55:58 +00001196{
1197 uint32_t val;
1198 int suffix;
1199
1200 if (has_index) {
Isaku Yamahatad56dd6c2009-07-02 19:32:07 +09001201 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001202 addr++;
1203 }
1204 addr &= 0xffff;
1205
1206 switch(size) {
1207 default:
1208 case 1:
1209 val = cpu_inb(NULL, addr);
1210 suffix = 'b';
1211 break;
1212 case 2:
1213 val = cpu_inw(NULL, addr);
1214 suffix = 'w';
1215 break;
1216 case 4:
1217 val = cpu_inl(NULL, addr);
1218 suffix = 'l';
1219 break;
1220 }
aliguori376253e2009-03-05 23:01:23 +00001221 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1222 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001223}
bellarda3a91a32004-06-04 11:06:21 +00001224
Jan Kiszkaf1147842009-07-14 10:20:11 +02001225static void do_ioport_write(Monitor *mon, int count, int format, int size,
1226 int addr, int val)
1227{
1228 addr &= IOPORTS_MASK;
1229
1230 switch (size) {
1231 default:
1232 case 1:
1233 cpu_outb(NULL, addr, val);
1234 break;
1235 case 2:
1236 cpu_outw(NULL, addr, val);
1237 break;
1238 case 4:
1239 cpu_outl(NULL, addr, val);
1240 break;
1241 }
1242}
1243
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001244static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001245{
1246 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001247 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001248
Jan Kiszka76e30d02009-07-02 00:19:02 +02001249 res = qemu_boot_set(bootdevice);
1250 if (res == 0) {
1251 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1252 } else if (res > 0) {
1253 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001254 } else {
aliguori376253e2009-03-05 23:01:23 +00001255 monitor_printf(mon, "no function defined to set boot device list for "
1256 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001257 }
1258}
1259
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001260static void do_system_reset(Monitor *mon, const QDict *qdict)
bellarde4f90822004-06-20 12:35:44 +00001261{
1262 qemu_system_reset_request();
1263}
1264
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001265static void do_system_powerdown(Monitor *mon, const QDict *qdict)
bellard34751872005-07-02 14:31:34 +00001266{
1267 qemu_system_powerdown_request();
1268}
1269
bellardb86bda52004-09-18 19:32:46 +00001270#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001271static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001272{
aliguori376253e2009-03-05 23:01:23 +00001273 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1274 addr,
1275 pte & mask,
1276 pte & PG_GLOBAL_MASK ? 'G' : '-',
1277 pte & PG_PSE_MASK ? 'P' : '-',
1278 pte & PG_DIRTY_MASK ? 'D' : '-',
1279 pte & PG_ACCESSED_MASK ? 'A' : '-',
1280 pte & PG_PCD_MASK ? 'C' : '-',
1281 pte & PG_PWT_MASK ? 'T' : '-',
1282 pte & PG_USER_MASK ? 'U' : '-',
1283 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001284}
1285
aliguori376253e2009-03-05 23:01:23 +00001286static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001287{
bellard6a00d602005-11-21 23:25:50 +00001288 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001289 int l1, l2;
1290 uint32_t pgd, pde, pte;
1291
bellard6a00d602005-11-21 23:25:50 +00001292 env = mon_get_cpu();
1293 if (!env)
1294 return;
1295
bellardb86bda52004-09-18 19:32:46 +00001296 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001297 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001298 return;
1299 }
1300 pgd = env->cr[3] & ~0xfff;
1301 for(l1 = 0; l1 < 1024; l1++) {
1302 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1303 pde = le32_to_cpu(pde);
1304 if (pde & PG_PRESENT_MASK) {
1305 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001306 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001307 } else {
1308 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001309 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001310 (uint8_t *)&pte, 4);
1311 pte = le32_to_cpu(pte);
1312 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001313 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001314 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001315 ~0xfff);
1316 }
1317 }
1318 }
1319 }
1320 }
1321}
1322
aliguori376253e2009-03-05 23:01:23 +00001323static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001324 uint32_t end, int prot)
1325{
bellard9746b152004-11-11 18:30:24 +00001326 int prot1;
1327 prot1 = *plast_prot;
1328 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001329 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001330 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1331 *pstart, end, end - *pstart,
1332 prot1 & PG_USER_MASK ? 'u' : '-',
1333 'r',
1334 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001335 }
1336 if (prot != 0)
1337 *pstart = end;
1338 else
1339 *pstart = -1;
1340 *plast_prot = prot;
1341 }
1342}
1343
aliguori376253e2009-03-05 23:01:23 +00001344static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001345{
bellard6a00d602005-11-21 23:25:50 +00001346 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001347 int l1, l2, prot, last_prot;
1348 uint32_t pgd, pde, pte, start, end;
1349
bellard6a00d602005-11-21 23:25:50 +00001350 env = mon_get_cpu();
1351 if (!env)
1352 return;
1353
bellardb86bda52004-09-18 19:32:46 +00001354 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001355 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001356 return;
1357 }
1358 pgd = env->cr[3] & ~0xfff;
1359 last_prot = 0;
1360 start = -1;
1361 for(l1 = 0; l1 < 1024; l1++) {
1362 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1363 pde = le32_to_cpu(pde);
1364 end = l1 << 22;
1365 if (pde & PG_PRESENT_MASK) {
1366 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1367 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001368 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001369 } else {
1370 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001371 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001372 (uint8_t *)&pte, 4);
1373 pte = le32_to_cpu(pte);
1374 end = (l1 << 22) + (l2 << 12);
1375 if (pte & PG_PRESENT_MASK) {
1376 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1377 } else {
1378 prot = 0;
1379 }
aliguori376253e2009-03-05 23:01:23 +00001380 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001381 }
1382 }
1383 } else {
1384 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001385 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001386 }
1387 }
1388}
1389#endif
1390
aurel327c664e22009-03-03 06:12:22 +00001391#if defined(TARGET_SH4)
1392
aliguori376253e2009-03-05 23:01:23 +00001393static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001394{
aliguori376253e2009-03-05 23:01:23 +00001395 monitor_printf(mon, " tlb%i:\t"
1396 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1397 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1398 "dirty=%hhu writethrough=%hhu\n",
1399 idx,
1400 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1401 tlb->v, tlb->sh, tlb->c, tlb->pr,
1402 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001403}
1404
aliguori376253e2009-03-05 23:01:23 +00001405static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001406{
1407 CPUState *env = mon_get_cpu();
1408 int i;
1409
aliguori376253e2009-03-05 23:01:23 +00001410 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001411 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001412 print_tlb (mon, i, &env->itlb[i]);
1413 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001414 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001415 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001416}
1417
1418#endif
1419
aliguori376253e2009-03-05 23:01:23 +00001420static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001421{
1422#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001423 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001424 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001425 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001426 else
aliguori376253e2009-03-05 23:01:23 +00001427 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001428#else
aliguori376253e2009-03-05 23:01:23 +00001429 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001430#endif
1431}
1432
aliguori030ea372009-04-21 22:30:47 +00001433static void do_info_numa(Monitor *mon)
1434{
aliguorib28b6232009-04-22 20:20:29 +00001435 int i;
aliguori030ea372009-04-21 22:30:47 +00001436 CPUState *env;
1437
1438 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1439 for (i = 0; i < nb_numa_nodes; i++) {
1440 monitor_printf(mon, "node %d cpus:", i);
1441 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1442 if (env->numa_node == i) {
1443 monitor_printf(mon, " %d", env->cpu_index);
1444 }
1445 }
1446 monitor_printf(mon, "\n");
1447 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1448 node_mem[i] >> 20);
1449 }
1450}
1451
bellard5f1ce942006-02-08 22:40:15 +00001452#ifdef CONFIG_PROFILER
1453
aliguori376253e2009-03-05 23:01:23 +00001454static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001455{
1456 int64_t total;
1457 total = qemu_time;
1458 if (total == 0)
1459 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001460 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1461 dev_time, dev_time / (double)ticks_per_sec);
1462 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1463 qemu_time, qemu_time / (double)ticks_per_sec);
bellard5f1ce942006-02-08 22:40:15 +00001464 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001465 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001466}
1467#else
aliguori376253e2009-03-05 23:01:23 +00001468static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001469{
aliguori376253e2009-03-05 23:01:23 +00001470 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001471}
1472#endif
1473
bellardec36b692006-07-16 18:57:03 +00001474/* Capture support */
1475static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1476
aliguori376253e2009-03-05 23:01:23 +00001477static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001478{
1479 int i;
1480 CaptureState *s;
1481
1482 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001483 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001484 s->ops.info (s->opaque);
1485 }
1486}
1487
Blue Swirl23130862009-06-06 08:22:04 +00001488#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001489static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001490{
1491 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001492 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001493 CaptureState *s;
1494
1495 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1496 if (i == n) {
1497 s->ops.destroy (s->opaque);
1498 LIST_REMOVE (s, entries);
1499 qemu_free (s);
1500 return;
1501 }
1502 }
1503}
1504
aliguori376253e2009-03-05 23:01:23 +00001505static void do_wav_capture(Monitor *mon, const char *path,
1506 int has_freq, int freq,
1507 int has_bits, int bits,
1508 int has_channels, int nchannels)
bellardec36b692006-07-16 18:57:03 +00001509{
1510 CaptureState *s;
1511
1512 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001513
1514 freq = has_freq ? freq : 44100;
1515 bits = has_bits ? bits : 16;
1516 nchannels = has_channels ? nchannels : 2;
1517
1518 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001519 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001520 qemu_free (s);
1521 }
1522 LIST_INSERT_HEAD (&capture_head, s, entries);
1523}
1524#endif
1525
aurel32dc1c0b72008-04-27 23:52:12 +00001526#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001527static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001528{
1529 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001530 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001531
1532 for (env = first_cpu; env != NULL; env = env->next_cpu)
1533 if (env->cpu_index == cpu_index) {
1534 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1535 break;
1536 }
1537}
1538#endif
1539
aliguori376253e2009-03-05 23:01:23 +00001540static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001541{
aurel321b530a62009-04-05 20:08:59 +00001542 if (vm_running) {
1543 if (singlestep) {
1544 monitor_printf(mon, "VM status: running (single step mode)\n");
1545 } else {
1546 monitor_printf(mon, "VM status: running\n");
1547 }
1548 } else
aliguori376253e2009-03-05 23:01:23 +00001549 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001550}
1551
1552
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001553static void do_balloon(Monitor *mon, const QDict *qdict)
aliguoridf751fa2008-12-04 20:19:35 +00001554{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001555 int value = qdict_get_int(qdict, "value");
aliguoridf751fa2008-12-04 20:19:35 +00001556 ram_addr_t target = value;
1557 qemu_balloon(target << 20);
1558}
1559
aliguori376253e2009-03-05 23:01:23 +00001560static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001561{
1562 ram_addr_t actual;
1563
1564 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001565 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001566 monitor_printf(mon, "Using KVM without synchronous MMU, "
1567 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001568 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001569 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001570 else
aliguori376253e2009-03-05 23:01:23 +00001571 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001572}
1573
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001574static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001575{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001576 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001577
aliguori76655d62009-03-06 20:27:37 +00001578 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001579 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001580 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001581 return acl;
1582}
aliguori76655d62009-03-06 20:27:37 +00001583
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001584static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001585{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001586 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001587 qemu_acl *acl = find_acl(mon, aclname);
1588 qemu_acl_entry *entry;
1589 int i = 0;
1590
1591 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001592 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001593 acl->defaultDeny ? "deny" : "allow");
aliguori28a76be2009-03-06 20:27:40 +00001594 TAILQ_FOREACH(entry, &acl->entries, next) {
1595 i++;
1596 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001597 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001598 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001599 }
1600}
1601
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001602static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001603{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001604 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001605 qemu_acl *acl = find_acl(mon, aclname);
1606
1607 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001608 qemu_acl_reset(acl);
1609 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001610 }
1611}
aliguori76655d62009-03-06 20:27:37 +00001612
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001613static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001614{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001615 const char *aclname = qdict_get_str(qdict, "aclname");
1616 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001617 qemu_acl *acl = find_acl(mon, aclname);
1618
1619 if (acl) {
1620 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001621 acl->defaultDeny = 0;
1622 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001623 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001624 acl->defaultDeny = 1;
1625 monitor_printf(mon, "acl: policy set to 'deny'\n");
1626 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001627 monitor_printf(mon, "acl: unknown policy '%s', "
1628 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001629 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001630 }
1631}
aliguori76655d62009-03-06 20:27:37 +00001632
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001633static void do_acl_add(Monitor *mon, const char *aclname,
1634 const char *match, const char *policy,
1635 int has_index, int index)
1636{
1637 qemu_acl *acl = find_acl(mon, aclname);
1638 int deny, ret;
1639
1640 if (acl) {
1641 if (strcmp(policy, "allow") == 0) {
1642 deny = 0;
1643 } else if (strcmp(policy, "deny") == 0) {
1644 deny = 1;
1645 } else {
1646 monitor_printf(mon, "acl: unknown policy '%s', "
1647 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001648 return;
1649 }
aliguori28a76be2009-03-06 20:27:40 +00001650 if (has_index)
1651 ret = qemu_acl_insert(acl, deny, match, index);
1652 else
1653 ret = qemu_acl_append(acl, deny, match);
1654 if (ret < 0)
1655 monitor_printf(mon, "acl: unable to add acl entry\n");
1656 else
1657 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001658 }
1659}
aliguori76655d62009-03-06 20:27:37 +00001660
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001661static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001662{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001663 const char *aclname = qdict_get_str(qdict, "aclname");
1664 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001665 qemu_acl *acl = find_acl(mon, aclname);
1666 int ret;
aliguori76655d62009-03-06 20:27:37 +00001667
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001668 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001669 ret = qemu_acl_remove(acl, match);
1670 if (ret < 0)
1671 monitor_printf(mon, "acl: no matching acl entry\n");
1672 else
1673 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001674 }
1675}
1676
Huang Ying79c4f6b2009-06-23 10:05:14 +08001677#if defined(TARGET_I386)
1678static void do_inject_mce(Monitor *mon,
1679 int cpu_index, int bank,
1680 unsigned status_hi, unsigned status_lo,
1681 unsigned mcg_status_hi, unsigned mcg_status_lo,
1682 unsigned addr_hi, unsigned addr_lo,
1683 unsigned misc_hi, unsigned misc_lo)
1684{
1685 CPUState *cenv;
1686 uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
1687 uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
1688 uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
1689 uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
1690
1691 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1692 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1693 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1694 break;
1695 }
1696}
1697#endif
1698
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001699static void do_getfd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001700{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001701 const char *fdname = qdict_get_str(qdict, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001702 mon_fd_t *monfd;
1703 int fd;
1704
1705 fd = qemu_chr_get_msgfd(mon->chr);
1706 if (fd == -1) {
1707 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1708 return;
1709 }
1710
1711 if (qemu_isdigit(fdname[0])) {
1712 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1713 return;
1714 }
1715
1716 fd = dup(fd);
1717 if (fd == -1) {
1718 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1719 strerror(errno));
1720 return;
1721 }
1722
1723 LIST_FOREACH(monfd, &mon->fds, next) {
1724 if (strcmp(monfd->name, fdname) != 0) {
1725 continue;
1726 }
1727
1728 close(monfd->fd);
1729 monfd->fd = fd;
1730 return;
1731 }
1732
1733 monfd = qemu_mallocz(sizeof(mon_fd_t));
1734 monfd->name = qemu_strdup(fdname);
1735 monfd->fd = fd;
1736
1737 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1738}
1739
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001740static void do_closefd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001741{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001742 const char *fdname = qdict_get_str(qdict, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001743 mon_fd_t *monfd;
1744
1745 LIST_FOREACH(monfd, &mon->fds, next) {
1746 if (strcmp(monfd->name, fdname) != 0) {
1747 continue;
1748 }
1749
1750 LIST_REMOVE(monfd, next);
1751 close(monfd->fd);
1752 qemu_free(monfd->name);
1753 qemu_free(monfd);
1754 return;
1755 }
1756
1757 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1758 fdname);
1759}
1760
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001761static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001762{
1763 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001764 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001765
1766 vm_stop(0);
1767
Juan Quintela05f24012009-08-20 19:42:22 +02001768 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001769 vm_start();
1770}
1771
Mark McLoughlin7768e042009-07-22 09:11:41 +01001772int monitor_get_fd(Monitor *mon, const char *fdname)
1773{
1774 mon_fd_t *monfd;
1775
1776 LIST_FOREACH(monfd, &mon->fds, next) {
1777 int fd;
1778
1779 if (strcmp(monfd->name, fdname) != 0) {
1780 continue;
1781 }
1782
1783 fd = monfd->fd;
1784
1785 /* caller takes ownership of fd */
1786 LIST_REMOVE(monfd, next);
1787 qemu_free(monfd->name);
1788 qemu_free(monfd);
1789
1790 return fd;
1791 }
1792
1793 return -1;
1794}
1795
aliguori376253e2009-03-05 23:01:23 +00001796static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001797#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001798 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001799};
1800
Blue Swirl23130862009-06-06 08:22:04 +00001801/* Please update qemu-monitor.hx when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001802static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001803 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001804 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001805 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001806 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001807 { "chardev", "", qemu_chr_info,
1808 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001809 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001810 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001811 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001812 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001813 { "registers", "", do_info_registers,
1814 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001815 { "cpus", "", do_info_cpus,
1816 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001817 { "history", "", do_info_history,
1818 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001819 { "irq", "", irq_info,
1820 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001821 { "pic", "", pic_info,
1822 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001823 { "pci", "", pci_info,
1824 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001825#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001826 { "tlb", "", tlb_info,
1827 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001828#endif
1829#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001830 { "mem", "", mem_info,
1831 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001832 { "hpet", "", do_info_hpet,
1833 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001834#endif
bellarde3db7222005-01-26 22:00:47 +00001835 { "jit", "", do_info_jit,
1836 "", "show dynamic compiler info", },
aliguori7ba1e612008-11-05 16:04:33 +00001837 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001838 "", "show KVM information", },
aliguori030ea372009-04-21 22:30:47 +00001839 { "numa", "", do_info_numa,
1840 "", "show NUMA information", },
bellarda594cfb2005-11-06 16:13:29 +00001841 { "usb", "", usb_info,
1842 "", "show guest USB devices", },
1843 { "usbhost", "", usb_host_info,
1844 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001845 { "profile", "", do_info_profile,
1846 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001847 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001848 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001849 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001850 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001851 { "status", "", do_info_status,
1852 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001853 { "pcmcia", "", pcmcia_info,
1854 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001855 { "mice", "", do_info_mice,
1856 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001857 { "vnc", "", do_info_vnc,
1858 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001859 { "name", "", do_info_name,
1860 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001861 { "uuid", "", do_info_uuid,
1862 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001863#if defined(TARGET_PPC)
1864 { "cpustats", "", do_info_cpu_stats,
1865 "", "show CPU statistics", },
1866#endif
blueswir131a60e22007-10-26 18:42:59 +00001867#if defined(CONFIG_SLIRP)
Jan Kiszka6dbe5532009-06-24 14:42:29 +02001868 { "usernet", "", do_info_usernet,
1869 "", "show user network stack connection states", },
blueswir131a60e22007-10-26 18:42:59 +00001870#endif
aliguori5bb79102008-10-13 03:12:02 +00001871 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001872 { "balloon", "", do_info_balloon,
1873 "", "show balloon information" },
Gerd Hoffmanncae49562009-06-05 15:53:17 +01001874 { "qtree", "", do_info_qtree,
1875 "", "show device tree" },
Gerd Hoffmannf6c64e02009-08-03 15:03:09 +02001876 { "qdm", "", do_info_qdm,
1877 "", "show qdev device model list" },
bellard9dc39cb2004-03-14 21:38:27 +00001878 { NULL, NULL, },
1879};
1880
bellard9307c4c2004-04-04 12:57:25 +00001881/*******************************************************************/
1882
1883static const char *pch;
1884static jmp_buf expr_env;
1885
bellard92a31b12005-02-10 22:00:52 +00001886#define MD_TLONG 0
1887#define MD_I32 1
1888
bellard9307c4c2004-04-04 12:57:25 +00001889typedef struct MonitorDef {
1890 const char *name;
1891 int offset;
blueswir18662d652008-10-02 18:32:44 +00001892 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001893 int type;
bellard9307c4c2004-04-04 12:57:25 +00001894} MonitorDef;
1895
bellard57206fd2004-04-25 18:54:52 +00001896#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001897static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001898{
bellard6a00d602005-11-21 23:25:50 +00001899 CPUState *env = mon_get_cpu();
1900 if (!env)
1901 return 0;
1902 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001903}
1904#endif
1905
bellarda541f292004-04-12 20:39:29 +00001906#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001907static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001908{
bellard6a00d602005-11-21 23:25:50 +00001909 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001910 unsigned int u;
1911 int i;
1912
bellard6a00d602005-11-21 23:25:50 +00001913 if (!env)
1914 return 0;
1915
bellarda541f292004-04-12 20:39:29 +00001916 u = 0;
1917 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00001918 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001919
1920 return u;
1921}
1922
blueswir18662d652008-10-02 18:32:44 +00001923static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001924{
bellard6a00d602005-11-21 23:25:50 +00001925 CPUState *env = mon_get_cpu();
1926 if (!env)
1927 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001928 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001929}
1930
blueswir18662d652008-10-02 18:32:44 +00001931static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001932{
bellard6a00d602005-11-21 23:25:50 +00001933 CPUState *env = mon_get_cpu();
1934 if (!env)
1935 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001936 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001937}
bellard9fddaa02004-05-21 12:59:32 +00001938
blueswir18662d652008-10-02 18:32:44 +00001939static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001940{
bellard6a00d602005-11-21 23:25:50 +00001941 CPUState *env = mon_get_cpu();
1942 if (!env)
1943 return 0;
1944 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001945}
1946
blueswir18662d652008-10-02 18:32:44 +00001947static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001948{
bellard6a00d602005-11-21 23:25:50 +00001949 CPUState *env = mon_get_cpu();
1950 if (!env)
1951 return 0;
1952 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001953}
1954
blueswir18662d652008-10-02 18:32:44 +00001955static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001956{
bellard6a00d602005-11-21 23:25:50 +00001957 CPUState *env = mon_get_cpu();
1958 if (!env)
1959 return 0;
1960 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001961}
bellarda541f292004-04-12 20:39:29 +00001962#endif
1963
bellarde95c8d52004-09-30 22:22:08 +00001964#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001965#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001966static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001967{
bellard6a00d602005-11-21 23:25:50 +00001968 CPUState *env = mon_get_cpu();
1969 if (!env)
1970 return 0;
1971 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001972}
bellard7b936c02005-10-30 17:05:13 +00001973#endif
bellarde95c8d52004-09-30 22:22:08 +00001974
blueswir18662d652008-10-02 18:32:44 +00001975static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001976{
bellard6a00d602005-11-21 23:25:50 +00001977 CPUState *env = mon_get_cpu();
1978 if (!env)
1979 return 0;
1980 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001981}
1982#endif
1983
blueswir18662d652008-10-02 18:32:44 +00001984static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001985#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001986
1987#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001988 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001989 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001990 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001991
bellard9307c4c2004-04-04 12:57:25 +00001992 { "eax", offsetof(CPUState, regs[0]) },
1993 { "ecx", offsetof(CPUState, regs[1]) },
1994 { "edx", offsetof(CPUState, regs[2]) },
1995 { "ebx", offsetof(CPUState, regs[3]) },
1996 { "esp|sp", offsetof(CPUState, regs[4]) },
1997 { "ebp|fp", offsetof(CPUState, regs[5]) },
1998 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001999 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002000#ifdef TARGET_X86_64
2001 { "r8", offsetof(CPUState, regs[8]) },
2002 { "r9", offsetof(CPUState, regs[9]) },
2003 { "r10", offsetof(CPUState, regs[10]) },
2004 { "r11", offsetof(CPUState, regs[11]) },
2005 { "r12", offsetof(CPUState, regs[12]) },
2006 { "r13", offsetof(CPUState, regs[13]) },
2007 { "r14", offsetof(CPUState, regs[14]) },
2008 { "r15", offsetof(CPUState, regs[15]) },
2009#endif
bellard9307c4c2004-04-04 12:57:25 +00002010 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002011 { "eip", offsetof(CPUState, eip) },
2012 SEG("cs", R_CS)
2013 SEG("ds", R_DS)
2014 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002015 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002016 SEG("fs", R_FS)
2017 SEG("gs", R_GS)
2018 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002019#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002020 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002021 { "r0", offsetof(CPUState, gpr[0]) },
2022 { "r1", offsetof(CPUState, gpr[1]) },
2023 { "r2", offsetof(CPUState, gpr[2]) },
2024 { "r3", offsetof(CPUState, gpr[3]) },
2025 { "r4", offsetof(CPUState, gpr[4]) },
2026 { "r5", offsetof(CPUState, gpr[5]) },
2027 { "r6", offsetof(CPUState, gpr[6]) },
2028 { "r7", offsetof(CPUState, gpr[7]) },
2029 { "r8", offsetof(CPUState, gpr[8]) },
2030 { "r9", offsetof(CPUState, gpr[9]) },
2031 { "r10", offsetof(CPUState, gpr[10]) },
2032 { "r11", offsetof(CPUState, gpr[11]) },
2033 { "r12", offsetof(CPUState, gpr[12]) },
2034 { "r13", offsetof(CPUState, gpr[13]) },
2035 { "r14", offsetof(CPUState, gpr[14]) },
2036 { "r15", offsetof(CPUState, gpr[15]) },
2037 { "r16", offsetof(CPUState, gpr[16]) },
2038 { "r17", offsetof(CPUState, gpr[17]) },
2039 { "r18", offsetof(CPUState, gpr[18]) },
2040 { "r19", offsetof(CPUState, gpr[19]) },
2041 { "r20", offsetof(CPUState, gpr[20]) },
2042 { "r21", offsetof(CPUState, gpr[21]) },
2043 { "r22", offsetof(CPUState, gpr[22]) },
2044 { "r23", offsetof(CPUState, gpr[23]) },
2045 { "r24", offsetof(CPUState, gpr[24]) },
2046 { "r25", offsetof(CPUState, gpr[25]) },
2047 { "r26", offsetof(CPUState, gpr[26]) },
2048 { "r27", offsetof(CPUState, gpr[27]) },
2049 { "r28", offsetof(CPUState, gpr[28]) },
2050 { "r29", offsetof(CPUState, gpr[29]) },
2051 { "r30", offsetof(CPUState, gpr[30]) },
2052 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002053 /* Floating point registers */
2054 { "f0", offsetof(CPUState, fpr[0]) },
2055 { "f1", offsetof(CPUState, fpr[1]) },
2056 { "f2", offsetof(CPUState, fpr[2]) },
2057 { "f3", offsetof(CPUState, fpr[3]) },
2058 { "f4", offsetof(CPUState, fpr[4]) },
2059 { "f5", offsetof(CPUState, fpr[5]) },
2060 { "f6", offsetof(CPUState, fpr[6]) },
2061 { "f7", offsetof(CPUState, fpr[7]) },
2062 { "f8", offsetof(CPUState, fpr[8]) },
2063 { "f9", offsetof(CPUState, fpr[9]) },
2064 { "f10", offsetof(CPUState, fpr[10]) },
2065 { "f11", offsetof(CPUState, fpr[11]) },
2066 { "f12", offsetof(CPUState, fpr[12]) },
2067 { "f13", offsetof(CPUState, fpr[13]) },
2068 { "f14", offsetof(CPUState, fpr[14]) },
2069 { "f15", offsetof(CPUState, fpr[15]) },
2070 { "f16", offsetof(CPUState, fpr[16]) },
2071 { "f17", offsetof(CPUState, fpr[17]) },
2072 { "f18", offsetof(CPUState, fpr[18]) },
2073 { "f19", offsetof(CPUState, fpr[19]) },
2074 { "f20", offsetof(CPUState, fpr[20]) },
2075 { "f21", offsetof(CPUState, fpr[21]) },
2076 { "f22", offsetof(CPUState, fpr[22]) },
2077 { "f23", offsetof(CPUState, fpr[23]) },
2078 { "f24", offsetof(CPUState, fpr[24]) },
2079 { "f25", offsetof(CPUState, fpr[25]) },
2080 { "f26", offsetof(CPUState, fpr[26]) },
2081 { "f27", offsetof(CPUState, fpr[27]) },
2082 { "f28", offsetof(CPUState, fpr[28]) },
2083 { "f29", offsetof(CPUState, fpr[29]) },
2084 { "f30", offsetof(CPUState, fpr[30]) },
2085 { "f31", offsetof(CPUState, fpr[31]) },
2086 { "fpscr", offsetof(CPUState, fpscr) },
2087 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002088 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002089 { "lr", offsetof(CPUState, lr) },
2090 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002091 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002092 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002093 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002094 { "msr", 0, &monitor_get_msr, },
2095 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002096 { "tbu", 0, &monitor_get_tbu, },
2097 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002098#if defined(TARGET_PPC64)
2099 /* Address space register */
2100 { "asr", offsetof(CPUState, asr) },
2101#endif
2102 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002103 { "sdr1", offsetof(CPUState, sdr1) },
2104 { "sr0", offsetof(CPUState, sr[0]) },
2105 { "sr1", offsetof(CPUState, sr[1]) },
2106 { "sr2", offsetof(CPUState, sr[2]) },
2107 { "sr3", offsetof(CPUState, sr[3]) },
2108 { "sr4", offsetof(CPUState, sr[4]) },
2109 { "sr5", offsetof(CPUState, sr[5]) },
2110 { "sr6", offsetof(CPUState, sr[6]) },
2111 { "sr7", offsetof(CPUState, sr[7]) },
2112 { "sr8", offsetof(CPUState, sr[8]) },
2113 { "sr9", offsetof(CPUState, sr[9]) },
2114 { "sr10", offsetof(CPUState, sr[10]) },
2115 { "sr11", offsetof(CPUState, sr[11]) },
2116 { "sr12", offsetof(CPUState, sr[12]) },
2117 { "sr13", offsetof(CPUState, sr[13]) },
2118 { "sr14", offsetof(CPUState, sr[14]) },
2119 { "sr15", offsetof(CPUState, sr[15]) },
2120 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002121#elif defined(TARGET_SPARC)
2122 { "g0", offsetof(CPUState, gregs[0]) },
2123 { "g1", offsetof(CPUState, gregs[1]) },
2124 { "g2", offsetof(CPUState, gregs[2]) },
2125 { "g3", offsetof(CPUState, gregs[3]) },
2126 { "g4", offsetof(CPUState, gregs[4]) },
2127 { "g5", offsetof(CPUState, gregs[5]) },
2128 { "g6", offsetof(CPUState, gregs[6]) },
2129 { "g7", offsetof(CPUState, gregs[7]) },
2130 { "o0", 0, monitor_get_reg },
2131 { "o1", 1, monitor_get_reg },
2132 { "o2", 2, monitor_get_reg },
2133 { "o3", 3, monitor_get_reg },
2134 { "o4", 4, monitor_get_reg },
2135 { "o5", 5, monitor_get_reg },
2136 { "o6", 6, monitor_get_reg },
2137 { "o7", 7, monitor_get_reg },
2138 { "l0", 8, monitor_get_reg },
2139 { "l1", 9, monitor_get_reg },
2140 { "l2", 10, monitor_get_reg },
2141 { "l3", 11, monitor_get_reg },
2142 { "l4", 12, monitor_get_reg },
2143 { "l5", 13, monitor_get_reg },
2144 { "l6", 14, monitor_get_reg },
2145 { "l7", 15, monitor_get_reg },
2146 { "i0", 16, monitor_get_reg },
2147 { "i1", 17, monitor_get_reg },
2148 { "i2", 18, monitor_get_reg },
2149 { "i3", 19, monitor_get_reg },
2150 { "i4", 20, monitor_get_reg },
2151 { "i5", 21, monitor_get_reg },
2152 { "i6", 22, monitor_get_reg },
2153 { "i7", 23, monitor_get_reg },
2154 { "pc", offsetof(CPUState, pc) },
2155 { "npc", offsetof(CPUState, npc) },
2156 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002157#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002158 { "psr", 0, &monitor_get_psr, },
2159 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002160#endif
bellarde95c8d52004-09-30 22:22:08 +00002161 { "tbr", offsetof(CPUState, tbr) },
2162 { "fsr", offsetof(CPUState, fsr) },
2163 { "f0", offsetof(CPUState, fpr[0]) },
2164 { "f1", offsetof(CPUState, fpr[1]) },
2165 { "f2", offsetof(CPUState, fpr[2]) },
2166 { "f3", offsetof(CPUState, fpr[3]) },
2167 { "f4", offsetof(CPUState, fpr[4]) },
2168 { "f5", offsetof(CPUState, fpr[5]) },
2169 { "f6", offsetof(CPUState, fpr[6]) },
2170 { "f7", offsetof(CPUState, fpr[7]) },
2171 { "f8", offsetof(CPUState, fpr[8]) },
2172 { "f9", offsetof(CPUState, fpr[9]) },
2173 { "f10", offsetof(CPUState, fpr[10]) },
2174 { "f11", offsetof(CPUState, fpr[11]) },
2175 { "f12", offsetof(CPUState, fpr[12]) },
2176 { "f13", offsetof(CPUState, fpr[13]) },
2177 { "f14", offsetof(CPUState, fpr[14]) },
2178 { "f15", offsetof(CPUState, fpr[15]) },
2179 { "f16", offsetof(CPUState, fpr[16]) },
2180 { "f17", offsetof(CPUState, fpr[17]) },
2181 { "f18", offsetof(CPUState, fpr[18]) },
2182 { "f19", offsetof(CPUState, fpr[19]) },
2183 { "f20", offsetof(CPUState, fpr[20]) },
2184 { "f21", offsetof(CPUState, fpr[21]) },
2185 { "f22", offsetof(CPUState, fpr[22]) },
2186 { "f23", offsetof(CPUState, fpr[23]) },
2187 { "f24", offsetof(CPUState, fpr[24]) },
2188 { "f25", offsetof(CPUState, fpr[25]) },
2189 { "f26", offsetof(CPUState, fpr[26]) },
2190 { "f27", offsetof(CPUState, fpr[27]) },
2191 { "f28", offsetof(CPUState, fpr[28]) },
2192 { "f29", offsetof(CPUState, fpr[29]) },
2193 { "f30", offsetof(CPUState, fpr[30]) },
2194 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002195#ifdef TARGET_SPARC64
2196 { "f32", offsetof(CPUState, fpr[32]) },
2197 { "f34", offsetof(CPUState, fpr[34]) },
2198 { "f36", offsetof(CPUState, fpr[36]) },
2199 { "f38", offsetof(CPUState, fpr[38]) },
2200 { "f40", offsetof(CPUState, fpr[40]) },
2201 { "f42", offsetof(CPUState, fpr[42]) },
2202 { "f44", offsetof(CPUState, fpr[44]) },
2203 { "f46", offsetof(CPUState, fpr[46]) },
2204 { "f48", offsetof(CPUState, fpr[48]) },
2205 { "f50", offsetof(CPUState, fpr[50]) },
2206 { "f52", offsetof(CPUState, fpr[52]) },
2207 { "f54", offsetof(CPUState, fpr[54]) },
2208 { "f56", offsetof(CPUState, fpr[56]) },
2209 { "f58", offsetof(CPUState, fpr[58]) },
2210 { "f60", offsetof(CPUState, fpr[60]) },
2211 { "f62", offsetof(CPUState, fpr[62]) },
2212 { "asi", offsetof(CPUState, asi) },
2213 { "pstate", offsetof(CPUState, pstate) },
2214 { "cansave", offsetof(CPUState, cansave) },
2215 { "canrestore", offsetof(CPUState, canrestore) },
2216 { "otherwin", offsetof(CPUState, otherwin) },
2217 { "wstate", offsetof(CPUState, wstate) },
2218 { "cleanwin", offsetof(CPUState, cleanwin) },
2219 { "fprs", offsetof(CPUState, fprs) },
2220#endif
bellard9307c4c2004-04-04 12:57:25 +00002221#endif
2222 { NULL },
2223};
2224
aliguori376253e2009-03-05 23:01:23 +00002225static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002226{
aliguori376253e2009-03-05 23:01:23 +00002227 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002228 longjmp(expr_env, 1);
2229}
2230
bellard6a00d602005-11-21 23:25:50 +00002231/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002232static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002233{
blueswir18662d652008-10-02 18:32:44 +00002234 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002235 void *ptr;
2236
bellard9307c4c2004-04-04 12:57:25 +00002237 for(md = monitor_defs; md->name != NULL; md++) {
2238 if (compare_cmd(name, md->name)) {
2239 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002240 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002241 } else {
bellard6a00d602005-11-21 23:25:50 +00002242 CPUState *env = mon_get_cpu();
2243 if (!env)
2244 return -2;
2245 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002246 switch(md->type) {
2247 case MD_I32:
2248 *pval = *(int32_t *)ptr;
2249 break;
2250 case MD_TLONG:
2251 *pval = *(target_long *)ptr;
2252 break;
2253 default:
2254 *pval = 0;
2255 break;
2256 }
bellard9307c4c2004-04-04 12:57:25 +00002257 }
2258 return 0;
2259 }
2260 }
2261 return -1;
2262}
2263
2264static void next(void)
2265{
Blue Swirl660f11b2009-07-31 21:16:51 +00002266 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002267 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002268 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002269 pch++;
2270 }
2271}
2272
aliguori376253e2009-03-05 23:01:23 +00002273static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002274
aliguori376253e2009-03-05 23:01:23 +00002275static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002276{
blueswir1c2efc952007-09-25 17:28:42 +00002277 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002278 char *p;
bellard6a00d602005-11-21 23:25:50 +00002279 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002280
2281 switch(*pch) {
2282 case '+':
2283 next();
aliguori376253e2009-03-05 23:01:23 +00002284 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002285 break;
2286 case '-':
2287 next();
aliguori376253e2009-03-05 23:01:23 +00002288 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002289 break;
2290 case '~':
2291 next();
aliguori376253e2009-03-05 23:01:23 +00002292 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002293 break;
2294 case '(':
2295 next();
aliguori376253e2009-03-05 23:01:23 +00002296 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002297 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002298 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002299 }
2300 next();
2301 break;
bellard81d09122004-07-14 17:21:37 +00002302 case '\'':
2303 pch++;
2304 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002305 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002306 n = *pch;
2307 pch++;
2308 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002309 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002310 next();
2311 break;
bellard9307c4c2004-04-04 12:57:25 +00002312 case '$':
2313 {
2314 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002315 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002316
bellard9307c4c2004-04-04 12:57:25 +00002317 pch++;
2318 q = buf;
2319 while ((*pch >= 'a' && *pch <= 'z') ||
2320 (*pch >= 'A' && *pch <= 'Z') ||
2321 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002322 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002323 if ((q - buf) < sizeof(buf) - 1)
2324 *q++ = *pch;
2325 pch++;
2326 }
blueswir1cd390082008-11-16 13:53:32 +00002327 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002328 pch++;
2329 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002330 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002331 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002332 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002333 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002334 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002335 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002336 }
2337 break;
2338 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002339 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002340 n = 0;
2341 break;
2342 default:
blueswir17743e582007-09-24 18:39:04 +00002343#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002344 n = strtoull(pch, &p, 0);
2345#else
bellard9307c4c2004-04-04 12:57:25 +00002346 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002347#endif
bellard9307c4c2004-04-04 12:57:25 +00002348 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002349 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002350 }
2351 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002352 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002353 pch++;
2354 break;
2355 }
2356 return n;
2357}
2358
2359
aliguori376253e2009-03-05 23:01:23 +00002360static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002361{
blueswir1c2efc952007-09-25 17:28:42 +00002362 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002363 int op;
ths3b46e622007-09-17 08:09:54 +00002364
aliguori376253e2009-03-05 23:01:23 +00002365 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002366 for(;;) {
2367 op = *pch;
2368 if (op != '*' && op != '/' && op != '%')
2369 break;
2370 next();
aliguori376253e2009-03-05 23:01:23 +00002371 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002372 switch(op) {
2373 default:
2374 case '*':
2375 val *= val2;
2376 break;
2377 case '/':
2378 case '%':
ths5fafdf22007-09-16 21:08:06 +00002379 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002380 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002381 if (op == '/')
2382 val /= val2;
2383 else
2384 val %= val2;
2385 break;
2386 }
2387 }
2388 return val;
2389}
2390
aliguori376253e2009-03-05 23:01:23 +00002391static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002392{
blueswir1c2efc952007-09-25 17:28:42 +00002393 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002394 int op;
bellard9307c4c2004-04-04 12:57:25 +00002395
aliguori376253e2009-03-05 23:01:23 +00002396 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002397 for(;;) {
2398 op = *pch;
2399 if (op != '&' && op != '|' && op != '^')
2400 break;
2401 next();
aliguori376253e2009-03-05 23:01:23 +00002402 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002403 switch(op) {
2404 default:
2405 case '&':
2406 val &= val2;
2407 break;
2408 case '|':
2409 val |= val2;
2410 break;
2411 case '^':
2412 val ^= val2;
2413 break;
2414 }
2415 }
2416 return val;
2417}
2418
aliguori376253e2009-03-05 23:01:23 +00002419static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002420{
blueswir1c2efc952007-09-25 17:28:42 +00002421 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002422 int op;
bellard9307c4c2004-04-04 12:57:25 +00002423
aliguori376253e2009-03-05 23:01:23 +00002424 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002425 for(;;) {
2426 op = *pch;
2427 if (op != '+' && op != '-')
2428 break;
2429 next();
aliguori376253e2009-03-05 23:01:23 +00002430 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002431 if (op == '+')
2432 val += val2;
2433 else
2434 val -= val2;
2435 }
2436 return val;
2437}
2438
aliguori376253e2009-03-05 23:01:23 +00002439static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002440{
2441 pch = *pp;
2442 if (setjmp(expr_env)) {
2443 *pp = pch;
2444 return -1;
2445 }
blueswir1cd390082008-11-16 13:53:32 +00002446 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002447 pch++;
aliguori376253e2009-03-05 23:01:23 +00002448 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002449 *pp = pch;
2450 return 0;
2451}
2452
2453static int get_str(char *buf, int buf_size, const char **pp)
2454{
2455 const char *p;
2456 char *q;
2457 int c;
2458
bellard81d09122004-07-14 17:21:37 +00002459 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002460 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002461 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002462 p++;
2463 if (*p == '\0') {
2464 fail:
bellard81d09122004-07-14 17:21:37 +00002465 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002466 *pp = p;
2467 return -1;
2468 }
bellard9307c4c2004-04-04 12:57:25 +00002469 if (*p == '\"') {
2470 p++;
2471 while (*p != '\0' && *p != '\"') {
2472 if (*p == '\\') {
2473 p++;
2474 c = *p++;
2475 switch(c) {
2476 case 'n':
2477 c = '\n';
2478 break;
2479 case 'r':
2480 c = '\r';
2481 break;
2482 case '\\':
2483 case '\'':
2484 case '\"':
2485 break;
2486 default:
2487 qemu_printf("unsupported escape code: '\\%c'\n", c);
2488 goto fail;
2489 }
2490 if ((q - buf) < buf_size - 1) {
2491 *q++ = c;
2492 }
2493 } else {
2494 if ((q - buf) < buf_size - 1) {
2495 *q++ = *p;
2496 }
2497 p++;
2498 }
2499 }
2500 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002501 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002502 goto fail;
2503 }
2504 p++;
2505 } else {
blueswir1cd390082008-11-16 13:53:32 +00002506 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002507 if ((q - buf) < buf_size - 1) {
2508 *q++ = *p;
2509 }
2510 p++;
2511 }
bellard9307c4c2004-04-04 12:57:25 +00002512 }
bellard81d09122004-07-14 17:21:37 +00002513 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002514 *pp = p;
2515 return 0;
2516}
2517
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002518/*
2519 * Store the command-name in cmdname, and return a pointer to
2520 * the remaining of the command string.
2521 */
2522static const char *get_command_name(const char *cmdline,
2523 char *cmdname, size_t nlen)
2524{
2525 size_t len;
2526 const char *p, *pstart;
2527
2528 p = cmdline;
2529 while (qemu_isspace(*p))
2530 p++;
2531 if (*p == '\0')
2532 return NULL;
2533 pstart = p;
2534 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2535 p++;
2536 len = p - pstart;
2537 if (len > nlen - 1)
2538 len = nlen - 1;
2539 memcpy(cmdname, pstart, len);
2540 cmdname[len] = '\0';
2541 return p;
2542}
2543
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002544/**
2545 * Read key of 'type' into 'key' and return the current
2546 * 'type' pointer.
2547 */
2548static char *key_get_info(const char *type, char **key)
2549{
2550 size_t len;
2551 char *p, *str;
2552
2553 if (*type == ',')
2554 type++;
2555
2556 p = strchr(type, ':');
2557 if (!p) {
2558 *key = NULL;
2559 return NULL;
2560 }
2561 len = p - type;
2562
2563 str = qemu_malloc(len + 1);
2564 memcpy(str, type, len);
2565 str[len] = '\0';
2566
2567 *key = str;
2568 return ++p;
2569}
2570
bellard9307c4c2004-04-04 12:57:25 +00002571static int default_fmt_format = 'x';
2572static int default_fmt_size = 4;
2573
2574#define MAX_ARGS 16
2575
aliguori376253e2009-03-05 23:01:23 +00002576static void monitor_handle_command(Monitor *mon, const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002577{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002578 const char *p, *typestr;
2579 int c, nb_args, i, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002580 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002581 char cmdname[256];
2582 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002583 char *key;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002584 QDict *qdict;
bellard9307c4c2004-04-04 12:57:25 +00002585 void *str_allocated[MAX_ARGS];
2586 void *args[MAX_ARGS];
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03002587 void (*handler_d)(Monitor *mon, const QDict *qdict);
aliguori376253e2009-03-05 23:01:23 +00002588 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2589 void *arg3);
2590 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2591 void *arg3, void *arg4);
2592 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2593 void *arg3, void *arg4, void *arg5);
2594 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2595 void *arg3, void *arg4, void *arg5, void *arg6);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002596 void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2597 void *arg3, void *arg4, void *arg5, void *arg6,
2598 void *arg7);
2599 void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2600 void *arg3, void *arg4, void *arg5, void *arg6,
2601 void *arg7, void *arg8);
2602 void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2603 void *arg3, void *arg4, void *arg5, void *arg6,
2604 void *arg7, void *arg8, void *arg9);
bellard9dc39cb2004-03-14 21:38:27 +00002605
2606#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002607 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002608#endif
ths3b46e622007-09-17 08:09:54 +00002609
bellard9307c4c2004-04-04 12:57:25 +00002610 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002611 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2612 if (!p)
bellard9dc39cb2004-03-14 21:38:27 +00002613 return;
ths3b46e622007-09-17 08:09:54 +00002614
bellard9307c4c2004-04-04 12:57:25 +00002615 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002616 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002617 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002618 break;
bellard9dc39cb2004-03-14 21:38:27 +00002619 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002620
2621 if (cmd->name == NULL) {
2622 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2623 return;
2624 }
bellard9307c4c2004-04-04 12:57:25 +00002625
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002626 qdict = qdict_new();
2627
bellard9307c4c2004-04-04 12:57:25 +00002628 for(i = 0; i < MAX_ARGS; i++)
2629 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002630
bellard9307c4c2004-04-04 12:57:25 +00002631 /* parse the parameters */
2632 typestr = cmd->args_type;
2633 nb_args = 0;
2634 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002635 typestr = key_get_info(typestr, &key);
2636 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002637 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002638 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002639 typestr++;
2640 switch(c) {
2641 case 'F':
bellard81d09122004-07-14 17:21:37 +00002642 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002643 case 's':
2644 {
2645 int ret;
2646 char *str;
ths3b46e622007-09-17 08:09:54 +00002647
blueswir1cd390082008-11-16 13:53:32 +00002648 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002649 p++;
2650 if (*typestr == '?') {
2651 typestr++;
2652 if (*p == '\0') {
2653 /* no optional string: NULL argument */
2654 str = NULL;
2655 goto add_str;
2656 }
2657 }
2658 ret = get_str(buf, sizeof(buf), &p);
2659 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002660 switch(c) {
2661 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002662 monitor_printf(mon, "%s: filename expected\n",
2663 cmdname);
bellard81d09122004-07-14 17:21:37 +00002664 break;
2665 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002666 monitor_printf(mon, "%s: block device name expected\n",
2667 cmdname);
bellard81d09122004-07-14 17:21:37 +00002668 break;
2669 default:
aliguori376253e2009-03-05 23:01:23 +00002670 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002671 break;
2672 }
bellard9307c4c2004-04-04 12:57:25 +00002673 goto fail;
2674 }
2675 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002676 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002677 str_allocated[nb_args] = str;
2678 add_str:
2679 if (nb_args >= MAX_ARGS) {
2680 error_args:
aliguori376253e2009-03-05 23:01:23 +00002681 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002682 goto fail;
2683 }
2684 args[nb_args++] = str;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002685 if (str)
2686 qdict_put(qdict, key, qstring_from_str(str));
bellard9307c4c2004-04-04 12:57:25 +00002687 }
2688 break;
2689 case '/':
2690 {
2691 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002692
blueswir1cd390082008-11-16 13:53:32 +00002693 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002694 p++;
2695 if (*p == '/') {
2696 /* format found */
2697 p++;
2698 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002699 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002700 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002701 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002702 count = count * 10 + (*p - '0');
2703 p++;
2704 }
2705 }
2706 size = -1;
2707 format = -1;
2708 for(;;) {
2709 switch(*p) {
2710 case 'o':
2711 case 'd':
2712 case 'u':
2713 case 'x':
2714 case 'i':
2715 case 'c':
2716 format = *p++;
2717 break;
2718 case 'b':
2719 size = 1;
2720 p++;
2721 break;
2722 case 'h':
2723 size = 2;
2724 p++;
2725 break;
2726 case 'w':
2727 size = 4;
2728 p++;
2729 break;
2730 case 'g':
2731 case 'L':
2732 size = 8;
2733 p++;
2734 break;
2735 default:
2736 goto next;
2737 }
2738 }
2739 next:
blueswir1cd390082008-11-16 13:53:32 +00002740 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002741 monitor_printf(mon, "invalid char in format: '%c'\n",
2742 *p);
bellard9307c4c2004-04-04 12:57:25 +00002743 goto fail;
2744 }
bellard9307c4c2004-04-04 12:57:25 +00002745 if (format < 0)
2746 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002747 if (format != 'i') {
2748 /* for 'i', not specifying a size gives -1 as size */
2749 if (size < 0)
2750 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002751 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002752 }
bellard9307c4c2004-04-04 12:57:25 +00002753 default_fmt_format = format;
2754 } else {
2755 count = 1;
2756 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002757 if (format != 'i') {
2758 size = default_fmt_size;
2759 } else {
2760 size = -1;
2761 }
bellard9307c4c2004-04-04 12:57:25 +00002762 }
2763 if (nb_args + 3 > MAX_ARGS)
2764 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002765 args[nb_args++] = (void*)(long)count;
2766 args[nb_args++] = (void*)(long)format;
2767 args[nb_args++] = (void*)(long)size;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002768 qdict_put(qdict, "count", qint_from_int(count));
2769 qdict_put(qdict, "format", qint_from_int(format));
2770 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002771 }
2772 break;
2773 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002774 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002775 {
blueswir1c2efc952007-09-25 17:28:42 +00002776 int64_t val;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002777 int dict_add = 1;
blueswir17743e582007-09-24 18:39:04 +00002778
blueswir1cd390082008-11-16 13:53:32 +00002779 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002780 p++;
bellard34405572004-06-08 00:55:58 +00002781 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002782 if (*typestr == '?') {
2783 if (*p == '\0')
2784 has_arg = 0;
2785 else
2786 has_arg = 1;
2787 } else {
2788 if (*p == '.') {
2789 p++;
blueswir1cd390082008-11-16 13:53:32 +00002790 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002791 p++;
2792 has_arg = 1;
2793 } else {
2794 has_arg = 0;
2795 }
2796 }
bellard13224a82006-07-14 22:03:35 +00002797 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002798 if (nb_args >= MAX_ARGS)
2799 goto error_args;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002800 dict_add = has_arg;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002801 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002802 if (!has_arg) {
2803 if (nb_args >= MAX_ARGS)
2804 goto error_args;
2805 val = -1;
2806 goto add_num;
2807 }
2808 }
aliguori376253e2009-03-05 23:01:23 +00002809 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002810 goto fail;
2811 add_num:
bellard92a31b12005-02-10 22:00:52 +00002812 if (c == 'i') {
2813 if (nb_args >= MAX_ARGS)
2814 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002815 args[nb_args++] = (void *)(long)val;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002816 if (dict_add)
2817 qdict_put(qdict, key, qint_from_int(val));
bellard92a31b12005-02-10 22:00:52 +00002818 } else {
2819 if ((nb_args + 1) >= MAX_ARGS)
2820 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002821#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002822 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002823#else
2824 args[nb_args++] = (void *)0;
2825#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002826 args[nb_args++] = (void *)(long)(val & 0xffffffff);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002827 qdict_put(qdict, key, qint_from_int(val));
bellard92a31b12005-02-10 22:00:52 +00002828 }
bellard9307c4c2004-04-04 12:57:25 +00002829 }
2830 break;
2831 case '-':
2832 {
2833 int has_option;
2834 /* option */
ths3b46e622007-09-17 08:09:54 +00002835
bellard9307c4c2004-04-04 12:57:25 +00002836 c = *typestr++;
2837 if (c == '\0')
2838 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002839 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002840 p++;
2841 has_option = 0;
2842 if (*p == '-') {
2843 p++;
2844 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002845 monitor_printf(mon, "%s: unsupported option -%c\n",
2846 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002847 goto fail;
2848 }
2849 p++;
2850 has_option = 1;
2851 }
2852 if (nb_args >= MAX_ARGS)
2853 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002854 args[nb_args++] = (void *)(long)has_option;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002855 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00002856 }
2857 break;
2858 default:
2859 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002860 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002861 goto fail;
2862 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002863 qemu_free(key);
2864 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002865 }
2866 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002867 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002868 p++;
2869 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002870 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2871 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002872 goto fail;
2873 }
2874
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002875 qemu_errors_to_mon(mon);
bellard9307c4c2004-04-04 12:57:25 +00002876 switch(nb_args) {
2877 case 0:
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002878 case 1:
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002879 case 2:
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03002880 case 3:
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03002881 handler_d = cmd->handler;
2882 handler_d(mon, qdict);
bellard9307c4c2004-04-04 12:57:25 +00002883 break;
bellard9307c4c2004-04-04 12:57:25 +00002884 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002885 handler_4 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002886 handler_4(mon, args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002887 break;
2888 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002889 handler_5 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002890 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002891 break;
bellard34405572004-06-08 00:55:58 +00002892 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002893 handler_6 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002894 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002895 break;
bellardec36b692006-07-16 18:57:03 +00002896 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002897 handler_7 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002898 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2899 args[6]);
bellardec36b692006-07-16 18:57:03 +00002900 break;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002901 case 8:
2902 handler_8 = cmd->handler;
2903 handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2904 args[6], args[7]);
2905 break;
2906 case 9:
2907 handler_9 = cmd->handler;
2908 handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2909 args[6], args[7], args[8]);
2910 break;
2911 case 10:
2912 handler_10 = cmd->handler;
2913 handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2914 args[6], args[7], args[8], args[9]);
2915 break;
bellard9307c4c2004-04-04 12:57:25 +00002916 default:
aliguori376253e2009-03-05 23:01:23 +00002917 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002918 break;
bellard9307c4c2004-04-04 12:57:25 +00002919 }
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002920 qemu_errors_to_previous();
2921
bellard9307c4c2004-04-04 12:57:25 +00002922 fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002923 qemu_free(key);
bellard9307c4c2004-04-04 12:57:25 +00002924 for(i = 0; i < MAX_ARGS; i++)
2925 qemu_free(str_allocated[i]);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002926 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002927}
2928
bellard81d09122004-07-14 17:21:37 +00002929static void cmd_completion(const char *name, const char *list)
2930{
2931 const char *p, *pstart;
2932 char cmd[128];
2933 int len;
2934
2935 p = list;
2936 for(;;) {
2937 pstart = p;
2938 p = strchr(p, '|');
2939 if (!p)
2940 p = pstart + strlen(pstart);
2941 len = p - pstart;
2942 if (len > sizeof(cmd) - 2)
2943 len = sizeof(cmd) - 2;
2944 memcpy(cmd, pstart, len);
2945 cmd[len] = '\0';
2946 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002947 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002948 }
2949 if (*p == '\0')
2950 break;
2951 p++;
2952 }
2953}
2954
2955static void file_completion(const char *input)
2956{
2957 DIR *ffs;
2958 struct dirent *d;
2959 char path[1024];
2960 char file[1024], file_prefix[1024];
2961 int input_path_len;
2962 const char *p;
2963
ths5fafdf22007-09-16 21:08:06 +00002964 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002965 if (!p) {
2966 input_path_len = 0;
2967 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002968 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002969 } else {
2970 input_path_len = p - input + 1;
2971 memcpy(path, input, input_path_len);
2972 if (input_path_len > sizeof(path) - 1)
2973 input_path_len = sizeof(path) - 1;
2974 path[input_path_len] = '\0';
2975 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2976 }
2977#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002978 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2979 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002980#endif
2981 ffs = opendir(path);
2982 if (!ffs)
2983 return;
2984 for(;;) {
2985 struct stat sb;
2986 d = readdir(ffs);
2987 if (!d)
2988 break;
2989 if (strstart(d->d_name, file_prefix, NULL)) {
2990 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002991 if (input_path_len < sizeof(file))
2992 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2993 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002994 /* stat the file to find out if it's a directory.
2995 * In that case add a slash to speed up typing long paths
2996 */
2997 stat(file, &sb);
2998 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002999 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003000 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003001 }
3002 }
3003 closedir(ffs);
3004}
3005
aliguori51de9762009-03-05 23:00:43 +00003006static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003007{
aliguori51de9762009-03-05 23:00:43 +00003008 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003009 const char *input = opaque;
3010
3011 if (input[0] == '\0' ||
3012 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003013 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003014 }
3015}
3016
3017/* NOTE: this parser is an approximate form of the real command parser */
3018static void parse_cmdline(const char *cmdline,
3019 int *pnb_args, char **args)
3020{
3021 const char *p;
3022 int nb_args, ret;
3023 char buf[1024];
3024
3025 p = cmdline;
3026 nb_args = 0;
3027 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003028 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003029 p++;
3030 if (*p == '\0')
3031 break;
3032 if (nb_args >= MAX_ARGS)
3033 break;
3034 ret = get_str(buf, sizeof(buf), &p);
3035 args[nb_args] = qemu_strdup(buf);
3036 nb_args++;
3037 if (ret < 0)
3038 break;
3039 }
3040 *pnb_args = nb_args;
3041}
3042
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003043static const char *next_arg_type(const char *typestr)
3044{
3045 const char *p = strchr(typestr, ':');
3046 return (p != NULL ? ++p : typestr);
3047}
3048
aliguori4c36ba32009-03-05 23:01:37 +00003049static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003050{
3051 const char *cmdname;
3052 char *args[MAX_ARGS];
3053 int nb_args, i, len;
3054 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00003055 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003056 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003057
3058 parse_cmdline(cmdline, &nb_args, args);
3059#ifdef DEBUG_COMPLETION
3060 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003061 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003062 }
3063#endif
3064
3065 /* if the line ends with a space, it means we want to complete the
3066 next arg */
3067 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003068 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003069 if (nb_args >= MAX_ARGS)
3070 return;
3071 args[nb_args++] = qemu_strdup("");
3072 }
3073 if (nb_args <= 1) {
3074 /* command completion */
3075 if (nb_args == 0)
3076 cmdname = "";
3077 else
3078 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003079 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003080 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003081 cmd_completion(cmdname, cmd->name);
3082 }
3083 } else {
3084 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003085 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003086 if (compare_cmd(args[0], cmd->name))
3087 goto found;
3088 }
3089 return;
3090 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003091 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003092 for(i = 0; i < nb_args - 2; i++) {
3093 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003094 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003095 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003096 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003097 }
3098 }
3099 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003100 if (*ptype == '-' && ptype[1] != '\0') {
3101 ptype += 2;
3102 }
bellard81d09122004-07-14 17:21:37 +00003103 switch(*ptype) {
3104 case 'F':
3105 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003106 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003107 file_completion(str);
3108 break;
3109 case 'B':
3110 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003111 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003112 bdrv_iterate(block_completion_it, (void *)str);
3113 break;
bellard7fe48482004-10-09 18:08:01 +00003114 case 's':
3115 /* XXX: more generic ? */
3116 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003117 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003118 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3119 cmd_completion(str, cmd->name);
3120 }
bellard64866c32006-05-07 18:03:31 +00003121 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003122 char *sep = strrchr(str, '-');
3123 if (sep)
3124 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003125 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003126 for(key = key_defs; key->name != NULL; key++) {
3127 cmd_completion(str, key->name);
3128 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003129 } else if (!strcmp(cmd->name, "help|?")) {
3130 readline_set_completion_index(cur_mon->rs, strlen(str));
3131 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3132 cmd_completion(str, cmd->name);
3133 }
bellard7fe48482004-10-09 18:08:01 +00003134 }
3135 break;
bellard81d09122004-07-14 17:21:37 +00003136 default:
3137 break;
3138 }
3139 }
3140 for(i = 0; i < nb_args; i++)
3141 qemu_free(args[i]);
3142}
3143
aliguori731b0362009-03-05 23:01:42 +00003144static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003145{
aliguori731b0362009-03-05 23:01:42 +00003146 Monitor *mon = opaque;
3147
3148 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003149}
3150
aliguori731b0362009-03-05 23:01:42 +00003151static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003152{
aliguori731b0362009-03-05 23:01:42 +00003153 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003154 int i;
aliguori376253e2009-03-05 23:01:23 +00003155
aliguori731b0362009-03-05 23:01:42 +00003156 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003157
aliguoricde76ee2009-03-05 23:01:51 +00003158 if (cur_mon->rs) {
3159 for (i = 0; i < size; i++)
3160 readline_handle_byte(cur_mon->rs, buf[i]);
3161 } else {
3162 if (size == 0 || buf[size - 1] != 0)
3163 monitor_printf(cur_mon, "corrupted command\n");
3164 else
3165 monitor_handle_command(cur_mon, (char *)buf);
3166 }
aliguori731b0362009-03-05 23:01:42 +00003167
3168 cur_mon = old_mon;
3169}
aliguorid8f44602008-10-06 13:52:44 +00003170
aliguori376253e2009-03-05 23:01:23 +00003171static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003172{
aliguori731b0362009-03-05 23:01:42 +00003173 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003174 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003175 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003176}
3177
aliguoricde76ee2009-03-05 23:01:51 +00003178int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003179{
aliguoricde76ee2009-03-05 23:01:51 +00003180 if (!mon->rs)
3181 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003182 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003183 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003184}
3185
aliguori376253e2009-03-05 23:01:23 +00003186void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003187{
aliguoricde76ee2009-03-05 23:01:51 +00003188 if (!mon->rs)
3189 return;
aliguori731b0362009-03-05 23:01:42 +00003190 if (--mon->suspend_cnt == 0)
3191 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003192}
3193
aliguori731b0362009-03-05 23:01:42 +00003194static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003195{
aliguori376253e2009-03-05 23:01:23 +00003196 Monitor *mon = opaque;
3197
aliguori2724b182009-03-05 23:01:47 +00003198 switch (event) {
3199 case CHR_EVENT_MUX_IN:
3200 readline_restart(mon->rs);
3201 monitor_resume(mon);
3202 monitor_flush(mon);
3203 break;
ths86e94de2007-01-05 22:01:59 +00003204
aliguori2724b182009-03-05 23:01:47 +00003205 case CHR_EVENT_MUX_OUT:
3206 if (mon->suspend_cnt == 0)
3207 monitor_printf(mon, "\n");
3208 monitor_flush(mon);
3209 monitor_suspend(mon);
3210 break;
3211
3212 case CHR_EVENT_RESET:
3213 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3214 "information\n", QEMU_VERSION);
3215 if (mon->chr->focus == 0)
3216 readline_show_prompt(mon->rs);
3217 break;
3218 }
ths86e94de2007-01-05 22:01:59 +00003219}
3220
aliguori76655d62009-03-06 20:27:37 +00003221
3222/*
3223 * Local variables:
3224 * c-indent-level: 4
3225 * c-basic-offset: 4
3226 * tab-width: 8
3227 * End:
3228 */
3229
aliguori731b0362009-03-05 23:01:42 +00003230void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003231{
aliguori731b0362009-03-05 23:01:42 +00003232 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003233 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003234
3235 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003236 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003237 is_first_init = 0;
3238 }
aliguori87127162009-03-05 23:01:29 +00003239
3240 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003241
aliguori87127162009-03-05 23:01:29 +00003242 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003243 mon->flags = flags;
aliguori2724b182009-03-05 23:01:47 +00003244 if (mon->chr->focus != 0)
3245 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
aliguoricde76ee2009-03-05 23:01:51 +00003246 if (flags & MONITOR_USE_READLINE) {
3247 mon->rs = readline_init(mon, monitor_find_completion);
3248 monitor_read_command(mon, 0);
3249 }
aliguori87127162009-03-05 23:01:29 +00003250
aliguori731b0362009-03-05 23:01:42 +00003251 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3252 mon);
aliguori87127162009-03-05 23:01:29 +00003253
3254 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003255 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003256 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003257}
3258
aliguori376253e2009-03-05 23:01:23 +00003259static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003260{
aliguoribb5fc202009-03-05 23:01:15 +00003261 BlockDriverState *bs = opaque;
3262 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003263
aliguoribb5fc202009-03-05 23:01:15 +00003264 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003265 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003266 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003267 }
aliguori731b0362009-03-05 23:01:42 +00003268 if (mon->password_completion_cb)
3269 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003270
aliguori731b0362009-03-05 23:01:42 +00003271 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003272}
aliguoric0f4ce72009-03-05 23:01:01 +00003273
aliguori376253e2009-03-05 23:01:23 +00003274void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003275 BlockDriverCompletionFunc *completion_cb,
3276 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003277{
aliguoricde76ee2009-03-05 23:01:51 +00003278 int err;
3279
aliguoribb5fc202009-03-05 23:01:15 +00003280 if (!bdrv_key_required(bs)) {
3281 if (completion_cb)
3282 completion_cb(opaque, 0);
3283 return;
3284 }
aliguoric0f4ce72009-03-05 23:01:01 +00003285
aliguori376253e2009-03-05 23:01:23 +00003286 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3287 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003288
aliguori731b0362009-03-05 23:01:42 +00003289 mon->password_completion_cb = completion_cb;
3290 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003291
aliguoricde76ee2009-03-05 23:01:51 +00003292 err = monitor_read_password(mon, bdrv_password_cb, bs);
3293
3294 if (err && completion_cb)
3295 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003296}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003297
3298typedef struct QemuErrorSink QemuErrorSink;
3299struct QemuErrorSink {
3300 enum {
3301 ERR_SINK_FILE,
3302 ERR_SINK_MONITOR,
3303 } dest;
3304 union {
3305 FILE *fp;
3306 Monitor *mon;
3307 };
3308 QemuErrorSink *previous;
3309};
3310
Blue Swirl528e93a2009-08-31 15:14:40 +00003311static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003312
3313void qemu_errors_to_file(FILE *fp)
3314{
3315 QemuErrorSink *sink;
3316
3317 sink = qemu_mallocz(sizeof(*sink));
3318 sink->dest = ERR_SINK_FILE;
3319 sink->fp = fp;
3320 sink->previous = qemu_error_sink;
3321 qemu_error_sink = sink;
3322}
3323
3324void qemu_errors_to_mon(Monitor *mon)
3325{
3326 QemuErrorSink *sink;
3327
3328 sink = qemu_mallocz(sizeof(*sink));
3329 sink->dest = ERR_SINK_MONITOR;
3330 sink->mon = mon;
3331 sink->previous = qemu_error_sink;
3332 qemu_error_sink = sink;
3333}
3334
3335void qemu_errors_to_previous(void)
3336{
3337 QemuErrorSink *sink;
3338
3339 assert(qemu_error_sink != NULL);
3340 sink = qemu_error_sink;
3341 qemu_error_sink = sink->previous;
3342 qemu_free(sink);
3343}
3344
3345void qemu_error(const char *fmt, ...)
3346{
3347 va_list args;
3348
3349 assert(qemu_error_sink != NULL);
3350 switch (qemu_error_sink->dest) {
3351 case ERR_SINK_FILE:
3352 va_start(args, fmt);
3353 vfprintf(qemu_error_sink->fp, fmt, args);
3354 va_end(args);
3355 break;
3356 case ERR_SINK_MONITOR:
3357 va_start(args, fmt);
3358 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3359 va_end(args);
3360 break;
3361 }
3362}