blob: 672afee4a7bd9876ff346a1712586b26c7ceb88b [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
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300795static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000796{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300797 int count = qdict_get_int(qdict, "count");
798 int format = qdict_get_int(qdict, "format");
799 int size = qdict_get_int(qdict, "size");
800 target_long addr = qdict_get_int(qdict, "addr");
801
aliguori376253e2009-03-05 23:01:23 +0000802 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000803}
804
blueswir17743e582007-09-24 18:39:04 +0000805#if TARGET_PHYS_ADDR_BITS > 32
806#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
807#else
808#define GET_TPHYSADDR(h, l) (l)
809#endif
810
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300811static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000812{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300813 int count = qdict_get_int(qdict, "count");
814 int format = qdict_get_int(qdict, "format");
815 int size = qdict_get_int(qdict, "size");
816 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
817
aliguori376253e2009-03-05 23:01:23 +0000818 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000819}
820
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300821static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000822{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300823 int format = qdict_get_int(qdict, "format");
824 target_phys_addr_t val = qdict_get_int(qdict, "val");
825
blueswir17743e582007-09-24 18:39:04 +0000826#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000827 switch(format) {
828 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000829 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000830 break;
831 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000832 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000833 break;
834 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000835 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000836 break;
837 default:
838 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000839 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000840 break;
841 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000842 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000843 break;
844 }
bellard92a31b12005-02-10 22:00:52 +0000845#else
846 switch(format) {
847 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000848 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000849 break;
850 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000851 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000852 break;
853 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000854 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000855 break;
856 default:
857 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000858 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000859 break;
860 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000861 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000862 break;
863 }
864#endif
aliguori376253e2009-03-05 23:01:23 +0000865 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000866}
867
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300868static void do_memory_save(Monitor *mon, const QDict *qdict)
bellardb371dc52007-01-03 15:20:39 +0000869{
870 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300871 uint32_t size = qdict_get_int(qdict, "size");
872 const char *filename = qdict_get_str(qdict, "filename");
873 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +0000874 uint32_t l;
875 CPUState *env;
876 uint8_t buf[1024];
877
878 env = mon_get_cpu();
879 if (!env)
880 return;
881
882 f = fopen(filename, "wb");
883 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000884 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000885 return;
886 }
887 while (size != 0) {
888 l = sizeof(buf);
889 if (l > size)
890 l = size;
891 cpu_memory_rw_debug(env, addr, buf, l, 0);
892 fwrite(buf, 1, l, f);
893 addr += l;
894 size -= l;
895 }
896 fclose(f);
897}
898
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300899static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
aurel32a8bdf7a2008-04-11 21:36:14 +0000900{
901 FILE *f;
902 uint32_t l;
903 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300904 uint32_t size = qdict_get_int(qdict, "size");
905 const char *filename = qdict_get_str(qdict, "filename");
906 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +0000907
908 f = fopen(filename, "wb");
909 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000910 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000911 return;
912 }
913 while (size != 0) {
914 l = sizeof(buf);
915 if (l > size)
916 l = size;
917 cpu_physical_memory_rw(addr, buf, l, 0);
918 fwrite(buf, 1, l, f);
919 fflush(f);
920 addr += l;
921 size -= l;
922 }
923 fclose(f);
924}
925
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300926static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000927{
928 uint32_t addr;
929 uint8_t buf[1];
930 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300931 uint32_t start = qdict_get_int(qdict, "start");
932 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000933
934 sum = 0;
935 for(addr = start; addr < (start + size); addr++) {
936 cpu_physical_memory_rw(addr, buf, 1, 0);
937 /* BSD sum algorithm ('sum' Unix command) */
938 sum = (sum >> 1) | (sum << 15);
939 sum += buf[0];
940 }
aliguori376253e2009-03-05 23:01:23 +0000941 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000942}
943
bellarda3a91a32004-06-04 11:06:21 +0000944typedef struct {
945 int keycode;
946 const char *name;
947} KeyDef;
948
949static const KeyDef key_defs[] = {
950 { 0x2a, "shift" },
951 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000952
bellarda3a91a32004-06-04 11:06:21 +0000953 { 0x38, "alt" },
954 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000955 { 0x64, "altgr" },
956 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000957 { 0x1d, "ctrl" },
958 { 0x9d, "ctrl_r" },
959
960 { 0xdd, "menu" },
961
962 { 0x01, "esc" },
963
964 { 0x02, "1" },
965 { 0x03, "2" },
966 { 0x04, "3" },
967 { 0x05, "4" },
968 { 0x06, "5" },
969 { 0x07, "6" },
970 { 0x08, "7" },
971 { 0x09, "8" },
972 { 0x0a, "9" },
973 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000974 { 0x0c, "minus" },
975 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000976 { 0x0e, "backspace" },
977
978 { 0x0f, "tab" },
979 { 0x10, "q" },
980 { 0x11, "w" },
981 { 0x12, "e" },
982 { 0x13, "r" },
983 { 0x14, "t" },
984 { 0x15, "y" },
985 { 0x16, "u" },
986 { 0x17, "i" },
987 { 0x18, "o" },
988 { 0x19, "p" },
989
990 { 0x1c, "ret" },
991
992 { 0x1e, "a" },
993 { 0x1f, "s" },
994 { 0x20, "d" },
995 { 0x21, "f" },
996 { 0x22, "g" },
997 { 0x23, "h" },
998 { 0x24, "j" },
999 { 0x25, "k" },
1000 { 0x26, "l" },
1001
1002 { 0x2c, "z" },
1003 { 0x2d, "x" },
1004 { 0x2e, "c" },
1005 { 0x2f, "v" },
1006 { 0x30, "b" },
1007 { 0x31, "n" },
1008 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001009 { 0x33, "comma" },
1010 { 0x34, "dot" },
1011 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001012
balrog4d3b6f62008-02-10 16:33:14 +00001013 { 0x37, "asterisk" },
1014
bellarda3a91a32004-06-04 11:06:21 +00001015 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001016 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001017 { 0x3b, "f1" },
1018 { 0x3c, "f2" },
1019 { 0x3d, "f3" },
1020 { 0x3e, "f4" },
1021 { 0x3f, "f5" },
1022 { 0x40, "f6" },
1023 { 0x41, "f7" },
1024 { 0x42, "f8" },
1025 { 0x43, "f9" },
1026 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001027 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001028 { 0x46, "scroll_lock" },
1029
bellard64866c32006-05-07 18:03:31 +00001030 { 0xb5, "kp_divide" },
1031 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001032 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001033 { 0x4e, "kp_add" },
1034 { 0x9c, "kp_enter" },
1035 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001036 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001037
1038 { 0x52, "kp_0" },
1039 { 0x4f, "kp_1" },
1040 { 0x50, "kp_2" },
1041 { 0x51, "kp_3" },
1042 { 0x4b, "kp_4" },
1043 { 0x4c, "kp_5" },
1044 { 0x4d, "kp_6" },
1045 { 0x47, "kp_7" },
1046 { 0x48, "kp_8" },
1047 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001048
bellarda3a91a32004-06-04 11:06:21 +00001049 { 0x56, "<" },
1050
1051 { 0x57, "f11" },
1052 { 0x58, "f12" },
1053
1054 { 0xb7, "print" },
1055
1056 { 0xc7, "home" },
1057 { 0xc9, "pgup" },
1058 { 0xd1, "pgdn" },
1059 { 0xcf, "end" },
1060
1061 { 0xcb, "left" },
1062 { 0xc8, "up" },
1063 { 0xd0, "down" },
1064 { 0xcd, "right" },
1065
1066 { 0xd2, "insert" },
1067 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001068#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1069 { 0xf0, "stop" },
1070 { 0xf1, "again" },
1071 { 0xf2, "props" },
1072 { 0xf3, "undo" },
1073 { 0xf4, "front" },
1074 { 0xf5, "copy" },
1075 { 0xf6, "open" },
1076 { 0xf7, "paste" },
1077 { 0xf8, "find" },
1078 { 0xf9, "cut" },
1079 { 0xfa, "lf" },
1080 { 0xfb, "help" },
1081 { 0xfc, "meta_l" },
1082 { 0xfd, "meta_r" },
1083 { 0xfe, "compose" },
1084#endif
bellarda3a91a32004-06-04 11:06:21 +00001085 { 0, NULL },
1086};
1087
1088static int get_keycode(const char *key)
1089{
1090 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001091 char *endp;
1092 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001093
1094 for(p = key_defs; p->name != NULL; p++) {
1095 if (!strcmp(key, p->name))
1096 return p->keycode;
1097 }
bellard64866c32006-05-07 18:03:31 +00001098 if (strstart(key, "0x", NULL)) {
1099 ret = strtoul(key, &endp, 0);
1100 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1101 return ret;
1102 }
bellarda3a91a32004-06-04 11:06:21 +00001103 return -1;
1104}
1105
balrogc8256f92008-06-08 22:45:01 +00001106#define MAX_KEYCODES 16
1107static uint8_t keycodes[MAX_KEYCODES];
1108static int nb_pending_keycodes;
1109static QEMUTimer *key_timer;
1110
1111static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001112{
balrogc8256f92008-06-08 22:45:01 +00001113 int keycode;
1114
1115 while (nb_pending_keycodes > 0) {
1116 nb_pending_keycodes--;
1117 keycode = keycodes[nb_pending_keycodes];
1118 if (keycode & 0x80)
1119 kbd_put_keycode(0xe0);
1120 kbd_put_keycode(keycode | 0x80);
1121 }
1122}
1123
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001124static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001125{
balrog3401c0d2008-06-04 10:05:59 +00001126 char keyname_buf[16];
1127 char *separator;
1128 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001129 const char *string = qdict_get_str(qdict, "string");
1130 int has_hold_time = qdict_haskey(qdict, "hold_time");
1131 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001132
balrogc8256f92008-06-08 22:45:01 +00001133 if (nb_pending_keycodes > 0) {
1134 qemu_del_timer(key_timer);
1135 release_keys(NULL);
1136 }
1137 if (!has_hold_time)
1138 hold_time = 100;
1139 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001140 while (1) {
1141 separator = strchr(string, '-');
1142 keyname_len = separator ? separator - string : strlen(string);
1143 if (keyname_len > 0) {
1144 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1145 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001146 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001147 return;
bellarda3a91a32004-06-04 11:06:21 +00001148 }
balrogc8256f92008-06-08 22:45:01 +00001149 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001150 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001151 return;
1152 }
1153 keyname_buf[keyname_len] = 0;
1154 keycode = get_keycode(keyname_buf);
1155 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001156 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001157 return;
1158 }
balrogc8256f92008-06-08 22:45:01 +00001159 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001160 }
balrog3401c0d2008-06-04 10:05:59 +00001161 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001162 break;
balrog3401c0d2008-06-04 10:05:59 +00001163 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001164 }
balrogc8256f92008-06-08 22:45:01 +00001165 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001166 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001167 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001168 keycode = keycodes[i];
1169 if (keycode & 0x80)
1170 kbd_put_keycode(0xe0);
1171 kbd_put_keycode(keycode & 0x7f);
1172 }
balrogc8256f92008-06-08 22:45:01 +00001173 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001174 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1175 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001176}
1177
bellard13224a82006-07-14 22:03:35 +00001178static int mouse_button_state;
1179
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001180static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001181{
1182 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001183 const char *dx_str = qdict_get_str(qdict, "dx_str");
1184 const char *dy_str = qdict_get_str(qdict, "dy_str");
1185 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001186 dx = strtol(dx_str, NULL, 0);
1187 dy = strtol(dy_str, NULL, 0);
1188 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001189 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001190 dz = strtol(dz_str, NULL, 0);
1191 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1192}
1193
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001194static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001195{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001196 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001197 mouse_button_state = button_state;
1198 kbd_mouse_event(0, 0, 0, mouse_button_state);
1199}
1200
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001201static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001202{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001203 int size = qdict_get_int(qdict, "size");
1204 int addr = qdict_get_int(qdict, "addr");
1205 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001206 uint32_t val;
1207 int suffix;
1208
1209 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001210 int index = qdict_get_int(qdict, "index");
Isaku Yamahatad56dd6c2009-07-02 19:32:07 +09001211 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001212 addr++;
1213 }
1214 addr &= 0xffff;
1215
1216 switch(size) {
1217 default:
1218 case 1:
1219 val = cpu_inb(NULL, addr);
1220 suffix = 'b';
1221 break;
1222 case 2:
1223 val = cpu_inw(NULL, addr);
1224 suffix = 'w';
1225 break;
1226 case 4:
1227 val = cpu_inl(NULL, addr);
1228 suffix = 'l';
1229 break;
1230 }
aliguori376253e2009-03-05 23:01:23 +00001231 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1232 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001233}
bellarda3a91a32004-06-04 11:06:21 +00001234
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001235static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001236{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001237 int size = qdict_get_int(qdict, "size");
1238 int addr = qdict_get_int(qdict, "addr");
1239 int val = qdict_get_int(qdict, "val");
1240
Jan Kiszkaf1147842009-07-14 10:20:11 +02001241 addr &= IOPORTS_MASK;
1242
1243 switch (size) {
1244 default:
1245 case 1:
1246 cpu_outb(NULL, addr, val);
1247 break;
1248 case 2:
1249 cpu_outw(NULL, addr, val);
1250 break;
1251 case 4:
1252 cpu_outl(NULL, addr, val);
1253 break;
1254 }
1255}
1256
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001257static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001258{
1259 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001260 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001261
Jan Kiszka76e30d02009-07-02 00:19:02 +02001262 res = qemu_boot_set(bootdevice);
1263 if (res == 0) {
1264 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1265 } else if (res > 0) {
1266 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001267 } else {
aliguori376253e2009-03-05 23:01:23 +00001268 monitor_printf(mon, "no function defined to set boot device list for "
1269 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001270 }
1271}
1272
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001273static void do_system_reset(Monitor *mon, const QDict *qdict)
bellarde4f90822004-06-20 12:35:44 +00001274{
1275 qemu_system_reset_request();
1276}
1277
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001278static void do_system_powerdown(Monitor *mon, const QDict *qdict)
bellard34751872005-07-02 14:31:34 +00001279{
1280 qemu_system_powerdown_request();
1281}
1282
bellardb86bda52004-09-18 19:32:46 +00001283#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001284static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001285{
aliguori376253e2009-03-05 23:01:23 +00001286 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1287 addr,
1288 pte & mask,
1289 pte & PG_GLOBAL_MASK ? 'G' : '-',
1290 pte & PG_PSE_MASK ? 'P' : '-',
1291 pte & PG_DIRTY_MASK ? 'D' : '-',
1292 pte & PG_ACCESSED_MASK ? 'A' : '-',
1293 pte & PG_PCD_MASK ? 'C' : '-',
1294 pte & PG_PWT_MASK ? 'T' : '-',
1295 pte & PG_USER_MASK ? 'U' : '-',
1296 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001297}
1298
aliguori376253e2009-03-05 23:01:23 +00001299static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001300{
bellard6a00d602005-11-21 23:25:50 +00001301 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001302 int l1, l2;
1303 uint32_t pgd, pde, pte;
1304
bellard6a00d602005-11-21 23:25:50 +00001305 env = mon_get_cpu();
1306 if (!env)
1307 return;
1308
bellardb86bda52004-09-18 19:32:46 +00001309 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001310 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001311 return;
1312 }
1313 pgd = env->cr[3] & ~0xfff;
1314 for(l1 = 0; l1 < 1024; l1++) {
1315 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1316 pde = le32_to_cpu(pde);
1317 if (pde & PG_PRESENT_MASK) {
1318 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001319 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001320 } else {
1321 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001322 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001323 (uint8_t *)&pte, 4);
1324 pte = le32_to_cpu(pte);
1325 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001326 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001327 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001328 ~0xfff);
1329 }
1330 }
1331 }
1332 }
1333 }
1334}
1335
aliguori376253e2009-03-05 23:01:23 +00001336static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001337 uint32_t end, int prot)
1338{
bellard9746b152004-11-11 18:30:24 +00001339 int prot1;
1340 prot1 = *plast_prot;
1341 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001342 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001343 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1344 *pstart, end, end - *pstart,
1345 prot1 & PG_USER_MASK ? 'u' : '-',
1346 'r',
1347 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001348 }
1349 if (prot != 0)
1350 *pstart = end;
1351 else
1352 *pstart = -1;
1353 *plast_prot = prot;
1354 }
1355}
1356
aliguori376253e2009-03-05 23:01:23 +00001357static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001358{
bellard6a00d602005-11-21 23:25:50 +00001359 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001360 int l1, l2, prot, last_prot;
1361 uint32_t pgd, pde, pte, start, end;
1362
bellard6a00d602005-11-21 23:25:50 +00001363 env = mon_get_cpu();
1364 if (!env)
1365 return;
1366
bellardb86bda52004-09-18 19:32:46 +00001367 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001368 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001369 return;
1370 }
1371 pgd = env->cr[3] & ~0xfff;
1372 last_prot = 0;
1373 start = -1;
1374 for(l1 = 0; l1 < 1024; l1++) {
1375 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1376 pde = le32_to_cpu(pde);
1377 end = l1 << 22;
1378 if (pde & PG_PRESENT_MASK) {
1379 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1380 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001381 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001382 } else {
1383 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001384 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001385 (uint8_t *)&pte, 4);
1386 pte = le32_to_cpu(pte);
1387 end = (l1 << 22) + (l2 << 12);
1388 if (pte & PG_PRESENT_MASK) {
1389 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1390 } else {
1391 prot = 0;
1392 }
aliguori376253e2009-03-05 23:01:23 +00001393 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001394 }
1395 }
1396 } else {
1397 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001398 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001399 }
1400 }
1401}
1402#endif
1403
aurel327c664e22009-03-03 06:12:22 +00001404#if defined(TARGET_SH4)
1405
aliguori376253e2009-03-05 23:01:23 +00001406static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001407{
aliguori376253e2009-03-05 23:01:23 +00001408 monitor_printf(mon, " tlb%i:\t"
1409 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1410 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1411 "dirty=%hhu writethrough=%hhu\n",
1412 idx,
1413 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1414 tlb->v, tlb->sh, tlb->c, tlb->pr,
1415 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001416}
1417
aliguori376253e2009-03-05 23:01:23 +00001418static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001419{
1420 CPUState *env = mon_get_cpu();
1421 int i;
1422
aliguori376253e2009-03-05 23:01:23 +00001423 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001424 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001425 print_tlb (mon, i, &env->itlb[i]);
1426 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001427 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001428 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001429}
1430
1431#endif
1432
aliguori376253e2009-03-05 23:01:23 +00001433static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001434{
1435#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001436 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001437 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001438 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001439 else
aliguori376253e2009-03-05 23:01:23 +00001440 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001441#else
aliguori376253e2009-03-05 23:01:23 +00001442 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001443#endif
1444}
1445
aliguori030ea372009-04-21 22:30:47 +00001446static void do_info_numa(Monitor *mon)
1447{
aliguorib28b6232009-04-22 20:20:29 +00001448 int i;
aliguori030ea372009-04-21 22:30:47 +00001449 CPUState *env;
1450
1451 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1452 for (i = 0; i < nb_numa_nodes; i++) {
1453 monitor_printf(mon, "node %d cpus:", i);
1454 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1455 if (env->numa_node == i) {
1456 monitor_printf(mon, " %d", env->cpu_index);
1457 }
1458 }
1459 monitor_printf(mon, "\n");
1460 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1461 node_mem[i] >> 20);
1462 }
1463}
1464
bellard5f1ce942006-02-08 22:40:15 +00001465#ifdef CONFIG_PROFILER
1466
aliguori376253e2009-03-05 23:01:23 +00001467static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001468{
1469 int64_t total;
1470 total = qemu_time;
1471 if (total == 0)
1472 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001473 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1474 dev_time, dev_time / (double)ticks_per_sec);
1475 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1476 qemu_time, qemu_time / (double)ticks_per_sec);
bellard5f1ce942006-02-08 22:40:15 +00001477 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001478 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001479}
1480#else
aliguori376253e2009-03-05 23:01:23 +00001481static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001482{
aliguori376253e2009-03-05 23:01:23 +00001483 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001484}
1485#endif
1486
bellardec36b692006-07-16 18:57:03 +00001487/* Capture support */
1488static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1489
aliguori376253e2009-03-05 23:01:23 +00001490static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001491{
1492 int i;
1493 CaptureState *s;
1494
1495 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001496 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001497 s->ops.info (s->opaque);
1498 }
1499}
1500
Blue Swirl23130862009-06-06 08:22:04 +00001501#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001502static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001503{
1504 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001505 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001506 CaptureState *s;
1507
1508 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1509 if (i == n) {
1510 s->ops.destroy (s->opaque);
1511 LIST_REMOVE (s, entries);
1512 qemu_free (s);
1513 return;
1514 }
1515 }
1516}
1517
aliguori376253e2009-03-05 23:01:23 +00001518static void do_wav_capture(Monitor *mon, const char *path,
1519 int has_freq, int freq,
1520 int has_bits, int bits,
1521 int has_channels, int nchannels)
bellardec36b692006-07-16 18:57:03 +00001522{
1523 CaptureState *s;
1524
1525 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001526
1527 freq = has_freq ? freq : 44100;
1528 bits = has_bits ? bits : 16;
1529 nchannels = has_channels ? nchannels : 2;
1530
1531 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001532 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001533 qemu_free (s);
1534 }
1535 LIST_INSERT_HEAD (&capture_head, s, entries);
1536}
1537#endif
1538
aurel32dc1c0b72008-04-27 23:52:12 +00001539#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001540static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001541{
1542 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001543 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001544
1545 for (env = first_cpu; env != NULL; env = env->next_cpu)
1546 if (env->cpu_index == cpu_index) {
1547 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1548 break;
1549 }
1550}
1551#endif
1552
aliguori376253e2009-03-05 23:01:23 +00001553static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001554{
aurel321b530a62009-04-05 20:08:59 +00001555 if (vm_running) {
1556 if (singlestep) {
1557 monitor_printf(mon, "VM status: running (single step mode)\n");
1558 } else {
1559 monitor_printf(mon, "VM status: running\n");
1560 }
1561 } else
aliguori376253e2009-03-05 23:01:23 +00001562 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001563}
1564
1565
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001566static void do_balloon(Monitor *mon, const QDict *qdict)
aliguoridf751fa2008-12-04 20:19:35 +00001567{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001568 int value = qdict_get_int(qdict, "value");
aliguoridf751fa2008-12-04 20:19:35 +00001569 ram_addr_t target = value;
1570 qemu_balloon(target << 20);
1571}
1572
aliguori376253e2009-03-05 23:01:23 +00001573static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001574{
1575 ram_addr_t actual;
1576
1577 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001578 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001579 monitor_printf(mon, "Using KVM without synchronous MMU, "
1580 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001581 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001582 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001583 else
aliguori376253e2009-03-05 23:01:23 +00001584 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001585}
1586
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001587static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001588{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001589 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001590
aliguori76655d62009-03-06 20:27:37 +00001591 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001592 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001593 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001594 return acl;
1595}
aliguori76655d62009-03-06 20:27:37 +00001596
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001597static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001598{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001599 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001600 qemu_acl *acl = find_acl(mon, aclname);
1601 qemu_acl_entry *entry;
1602 int i = 0;
1603
1604 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001605 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001606 acl->defaultDeny ? "deny" : "allow");
aliguori28a76be2009-03-06 20:27:40 +00001607 TAILQ_FOREACH(entry, &acl->entries, next) {
1608 i++;
1609 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001610 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001611 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001612 }
1613}
1614
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001615static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001616{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001617 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001618 qemu_acl *acl = find_acl(mon, aclname);
1619
1620 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001621 qemu_acl_reset(acl);
1622 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001623 }
1624}
aliguori76655d62009-03-06 20:27:37 +00001625
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001626static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001627{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001628 const char *aclname = qdict_get_str(qdict, "aclname");
1629 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001630 qemu_acl *acl = find_acl(mon, aclname);
1631
1632 if (acl) {
1633 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001634 acl->defaultDeny = 0;
1635 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001636 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001637 acl->defaultDeny = 1;
1638 monitor_printf(mon, "acl: policy set to 'deny'\n");
1639 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001640 monitor_printf(mon, "acl: unknown policy '%s', "
1641 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001642 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001643 }
1644}
aliguori76655d62009-03-06 20:27:37 +00001645
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001646static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001647{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001648 const char *aclname = qdict_get_str(qdict, "aclname");
1649 const char *match = qdict_get_str(qdict, "match");
1650 const char *policy = qdict_get_str(qdict, "policy");
1651 int has_index = qdict_haskey(qdict, "index");
1652 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001653 qemu_acl *acl = find_acl(mon, aclname);
1654 int deny, ret;
1655
1656 if (acl) {
1657 if (strcmp(policy, "allow") == 0) {
1658 deny = 0;
1659 } else if (strcmp(policy, "deny") == 0) {
1660 deny = 1;
1661 } else {
1662 monitor_printf(mon, "acl: unknown policy '%s', "
1663 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001664 return;
1665 }
aliguori28a76be2009-03-06 20:27:40 +00001666 if (has_index)
1667 ret = qemu_acl_insert(acl, deny, match, index);
1668 else
1669 ret = qemu_acl_append(acl, deny, match);
1670 if (ret < 0)
1671 monitor_printf(mon, "acl: unable to add acl entry\n");
1672 else
1673 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001674 }
1675}
aliguori76655d62009-03-06 20:27:37 +00001676
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001677static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001678{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001679 const char *aclname = qdict_get_str(qdict, "aclname");
1680 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001681 qemu_acl *acl = find_acl(mon, aclname);
1682 int ret;
aliguori76655d62009-03-06 20:27:37 +00001683
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001684 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001685 ret = qemu_acl_remove(acl, match);
1686 if (ret < 0)
1687 monitor_printf(mon, "acl: no matching acl entry\n");
1688 else
1689 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001690 }
1691}
1692
Huang Ying79c4f6b2009-06-23 10:05:14 +08001693#if defined(TARGET_I386)
1694static void do_inject_mce(Monitor *mon,
1695 int cpu_index, int bank,
1696 unsigned status_hi, unsigned status_lo,
1697 unsigned mcg_status_hi, unsigned mcg_status_lo,
1698 unsigned addr_hi, unsigned addr_lo,
1699 unsigned misc_hi, unsigned misc_lo)
1700{
1701 CPUState *cenv;
1702 uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
1703 uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
1704 uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
1705 uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
1706
1707 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1708 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1709 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1710 break;
1711 }
1712}
1713#endif
1714
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001715static void do_getfd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001716{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001717 const char *fdname = qdict_get_str(qdict, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001718 mon_fd_t *monfd;
1719 int fd;
1720
1721 fd = qemu_chr_get_msgfd(mon->chr);
1722 if (fd == -1) {
1723 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1724 return;
1725 }
1726
1727 if (qemu_isdigit(fdname[0])) {
1728 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1729 return;
1730 }
1731
1732 fd = dup(fd);
1733 if (fd == -1) {
1734 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1735 strerror(errno));
1736 return;
1737 }
1738
1739 LIST_FOREACH(monfd, &mon->fds, next) {
1740 if (strcmp(monfd->name, fdname) != 0) {
1741 continue;
1742 }
1743
1744 close(monfd->fd);
1745 monfd->fd = fd;
1746 return;
1747 }
1748
1749 monfd = qemu_mallocz(sizeof(mon_fd_t));
1750 monfd->name = qemu_strdup(fdname);
1751 monfd->fd = fd;
1752
1753 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1754}
1755
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001756static void do_closefd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001757{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001758 const char *fdname = qdict_get_str(qdict, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001759 mon_fd_t *monfd;
1760
1761 LIST_FOREACH(monfd, &mon->fds, next) {
1762 if (strcmp(monfd->name, fdname) != 0) {
1763 continue;
1764 }
1765
1766 LIST_REMOVE(monfd, next);
1767 close(monfd->fd);
1768 qemu_free(monfd->name);
1769 qemu_free(monfd);
1770 return;
1771 }
1772
1773 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1774 fdname);
1775}
1776
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001777static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001778{
1779 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001780 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001781
1782 vm_stop(0);
1783
Juan Quintela05f24012009-08-20 19:42:22 +02001784 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001785 vm_start();
1786}
1787
Mark McLoughlin7768e042009-07-22 09:11:41 +01001788int monitor_get_fd(Monitor *mon, const char *fdname)
1789{
1790 mon_fd_t *monfd;
1791
1792 LIST_FOREACH(monfd, &mon->fds, next) {
1793 int fd;
1794
1795 if (strcmp(monfd->name, fdname) != 0) {
1796 continue;
1797 }
1798
1799 fd = monfd->fd;
1800
1801 /* caller takes ownership of fd */
1802 LIST_REMOVE(monfd, next);
1803 qemu_free(monfd->name);
1804 qemu_free(monfd);
1805
1806 return fd;
1807 }
1808
1809 return -1;
1810}
1811
aliguori376253e2009-03-05 23:01:23 +00001812static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001813#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001814 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001815};
1816
Blue Swirl23130862009-06-06 08:22:04 +00001817/* Please update qemu-monitor.hx when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001818static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001819 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001820 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001821 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001822 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001823 { "chardev", "", qemu_chr_info,
1824 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001825 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001826 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001827 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001828 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001829 { "registers", "", do_info_registers,
1830 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001831 { "cpus", "", do_info_cpus,
1832 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001833 { "history", "", do_info_history,
1834 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001835 { "irq", "", irq_info,
1836 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001837 { "pic", "", pic_info,
1838 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001839 { "pci", "", pci_info,
1840 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001841#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001842 { "tlb", "", tlb_info,
1843 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001844#endif
1845#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001846 { "mem", "", mem_info,
1847 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001848 { "hpet", "", do_info_hpet,
1849 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001850#endif
bellarde3db7222005-01-26 22:00:47 +00001851 { "jit", "", do_info_jit,
1852 "", "show dynamic compiler info", },
aliguori7ba1e612008-11-05 16:04:33 +00001853 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001854 "", "show KVM information", },
aliguori030ea372009-04-21 22:30:47 +00001855 { "numa", "", do_info_numa,
1856 "", "show NUMA information", },
bellarda594cfb2005-11-06 16:13:29 +00001857 { "usb", "", usb_info,
1858 "", "show guest USB devices", },
1859 { "usbhost", "", usb_host_info,
1860 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001861 { "profile", "", do_info_profile,
1862 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001863 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001864 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001865 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001866 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001867 { "status", "", do_info_status,
1868 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001869 { "pcmcia", "", pcmcia_info,
1870 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001871 { "mice", "", do_info_mice,
1872 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001873 { "vnc", "", do_info_vnc,
1874 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001875 { "name", "", do_info_name,
1876 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001877 { "uuid", "", do_info_uuid,
1878 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001879#if defined(TARGET_PPC)
1880 { "cpustats", "", do_info_cpu_stats,
1881 "", "show CPU statistics", },
1882#endif
blueswir131a60e22007-10-26 18:42:59 +00001883#if defined(CONFIG_SLIRP)
Jan Kiszka6dbe5532009-06-24 14:42:29 +02001884 { "usernet", "", do_info_usernet,
1885 "", "show user network stack connection states", },
blueswir131a60e22007-10-26 18:42:59 +00001886#endif
aliguori5bb79102008-10-13 03:12:02 +00001887 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001888 { "balloon", "", do_info_balloon,
1889 "", "show balloon information" },
Gerd Hoffmanncae49562009-06-05 15:53:17 +01001890 { "qtree", "", do_info_qtree,
1891 "", "show device tree" },
Gerd Hoffmannf6c64e02009-08-03 15:03:09 +02001892 { "qdm", "", do_info_qdm,
1893 "", "show qdev device model list" },
bellard9dc39cb2004-03-14 21:38:27 +00001894 { NULL, NULL, },
1895};
1896
bellard9307c4c2004-04-04 12:57:25 +00001897/*******************************************************************/
1898
1899static const char *pch;
1900static jmp_buf expr_env;
1901
bellard92a31b12005-02-10 22:00:52 +00001902#define MD_TLONG 0
1903#define MD_I32 1
1904
bellard9307c4c2004-04-04 12:57:25 +00001905typedef struct MonitorDef {
1906 const char *name;
1907 int offset;
blueswir18662d652008-10-02 18:32:44 +00001908 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001909 int type;
bellard9307c4c2004-04-04 12:57:25 +00001910} MonitorDef;
1911
bellard57206fd2004-04-25 18:54:52 +00001912#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001913static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001914{
bellard6a00d602005-11-21 23:25:50 +00001915 CPUState *env = mon_get_cpu();
1916 if (!env)
1917 return 0;
1918 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001919}
1920#endif
1921
bellarda541f292004-04-12 20:39:29 +00001922#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001923static target_long monitor_get_ccr (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();
bellarda541f292004-04-12 20:39:29 +00001926 unsigned int u;
1927 int i;
1928
bellard6a00d602005-11-21 23:25:50 +00001929 if (!env)
1930 return 0;
1931
bellarda541f292004-04-12 20:39:29 +00001932 u = 0;
1933 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00001934 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001935
1936 return u;
1937}
1938
blueswir18662d652008-10-02 18:32:44 +00001939static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001940{
bellard6a00d602005-11-21 23:25:50 +00001941 CPUState *env = mon_get_cpu();
1942 if (!env)
1943 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001944 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001945}
1946
blueswir18662d652008-10-02 18:32:44 +00001947static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001948{
bellard6a00d602005-11-21 23:25:50 +00001949 CPUState *env = mon_get_cpu();
1950 if (!env)
1951 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001952 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001953}
bellard9fddaa02004-05-21 12:59:32 +00001954
blueswir18662d652008-10-02 18:32:44 +00001955static target_long monitor_get_decr (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_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001961}
1962
blueswir18662d652008-10-02 18:32:44 +00001963static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001964{
bellard6a00d602005-11-21 23:25:50 +00001965 CPUState *env = mon_get_cpu();
1966 if (!env)
1967 return 0;
1968 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001969}
1970
blueswir18662d652008-10-02 18:32:44 +00001971static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001972{
bellard6a00d602005-11-21 23:25:50 +00001973 CPUState *env = mon_get_cpu();
1974 if (!env)
1975 return 0;
1976 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001977}
bellarda541f292004-04-12 20:39:29 +00001978#endif
1979
bellarde95c8d52004-09-30 22:22:08 +00001980#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001981#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001982static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001983{
bellard6a00d602005-11-21 23:25:50 +00001984 CPUState *env = mon_get_cpu();
1985 if (!env)
1986 return 0;
1987 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001988}
bellard7b936c02005-10-30 17:05:13 +00001989#endif
bellarde95c8d52004-09-30 22:22:08 +00001990
blueswir18662d652008-10-02 18:32:44 +00001991static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001992{
bellard6a00d602005-11-21 23:25:50 +00001993 CPUState *env = mon_get_cpu();
1994 if (!env)
1995 return 0;
1996 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001997}
1998#endif
1999
blueswir18662d652008-10-02 18:32:44 +00002000static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002001#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002002
2003#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002004 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002005 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002006 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002007
bellard9307c4c2004-04-04 12:57:25 +00002008 { "eax", offsetof(CPUState, regs[0]) },
2009 { "ecx", offsetof(CPUState, regs[1]) },
2010 { "edx", offsetof(CPUState, regs[2]) },
2011 { "ebx", offsetof(CPUState, regs[3]) },
2012 { "esp|sp", offsetof(CPUState, regs[4]) },
2013 { "ebp|fp", offsetof(CPUState, regs[5]) },
2014 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002015 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002016#ifdef TARGET_X86_64
2017 { "r8", offsetof(CPUState, regs[8]) },
2018 { "r9", offsetof(CPUState, regs[9]) },
2019 { "r10", offsetof(CPUState, regs[10]) },
2020 { "r11", offsetof(CPUState, regs[11]) },
2021 { "r12", offsetof(CPUState, regs[12]) },
2022 { "r13", offsetof(CPUState, regs[13]) },
2023 { "r14", offsetof(CPUState, regs[14]) },
2024 { "r15", offsetof(CPUState, regs[15]) },
2025#endif
bellard9307c4c2004-04-04 12:57:25 +00002026 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002027 { "eip", offsetof(CPUState, eip) },
2028 SEG("cs", R_CS)
2029 SEG("ds", R_DS)
2030 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002031 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002032 SEG("fs", R_FS)
2033 SEG("gs", R_GS)
2034 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002035#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002036 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002037 { "r0", offsetof(CPUState, gpr[0]) },
2038 { "r1", offsetof(CPUState, gpr[1]) },
2039 { "r2", offsetof(CPUState, gpr[2]) },
2040 { "r3", offsetof(CPUState, gpr[3]) },
2041 { "r4", offsetof(CPUState, gpr[4]) },
2042 { "r5", offsetof(CPUState, gpr[5]) },
2043 { "r6", offsetof(CPUState, gpr[6]) },
2044 { "r7", offsetof(CPUState, gpr[7]) },
2045 { "r8", offsetof(CPUState, gpr[8]) },
2046 { "r9", offsetof(CPUState, gpr[9]) },
2047 { "r10", offsetof(CPUState, gpr[10]) },
2048 { "r11", offsetof(CPUState, gpr[11]) },
2049 { "r12", offsetof(CPUState, gpr[12]) },
2050 { "r13", offsetof(CPUState, gpr[13]) },
2051 { "r14", offsetof(CPUState, gpr[14]) },
2052 { "r15", offsetof(CPUState, gpr[15]) },
2053 { "r16", offsetof(CPUState, gpr[16]) },
2054 { "r17", offsetof(CPUState, gpr[17]) },
2055 { "r18", offsetof(CPUState, gpr[18]) },
2056 { "r19", offsetof(CPUState, gpr[19]) },
2057 { "r20", offsetof(CPUState, gpr[20]) },
2058 { "r21", offsetof(CPUState, gpr[21]) },
2059 { "r22", offsetof(CPUState, gpr[22]) },
2060 { "r23", offsetof(CPUState, gpr[23]) },
2061 { "r24", offsetof(CPUState, gpr[24]) },
2062 { "r25", offsetof(CPUState, gpr[25]) },
2063 { "r26", offsetof(CPUState, gpr[26]) },
2064 { "r27", offsetof(CPUState, gpr[27]) },
2065 { "r28", offsetof(CPUState, gpr[28]) },
2066 { "r29", offsetof(CPUState, gpr[29]) },
2067 { "r30", offsetof(CPUState, gpr[30]) },
2068 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002069 /* Floating point registers */
2070 { "f0", offsetof(CPUState, fpr[0]) },
2071 { "f1", offsetof(CPUState, fpr[1]) },
2072 { "f2", offsetof(CPUState, fpr[2]) },
2073 { "f3", offsetof(CPUState, fpr[3]) },
2074 { "f4", offsetof(CPUState, fpr[4]) },
2075 { "f5", offsetof(CPUState, fpr[5]) },
2076 { "f6", offsetof(CPUState, fpr[6]) },
2077 { "f7", offsetof(CPUState, fpr[7]) },
2078 { "f8", offsetof(CPUState, fpr[8]) },
2079 { "f9", offsetof(CPUState, fpr[9]) },
2080 { "f10", offsetof(CPUState, fpr[10]) },
2081 { "f11", offsetof(CPUState, fpr[11]) },
2082 { "f12", offsetof(CPUState, fpr[12]) },
2083 { "f13", offsetof(CPUState, fpr[13]) },
2084 { "f14", offsetof(CPUState, fpr[14]) },
2085 { "f15", offsetof(CPUState, fpr[15]) },
2086 { "f16", offsetof(CPUState, fpr[16]) },
2087 { "f17", offsetof(CPUState, fpr[17]) },
2088 { "f18", offsetof(CPUState, fpr[18]) },
2089 { "f19", offsetof(CPUState, fpr[19]) },
2090 { "f20", offsetof(CPUState, fpr[20]) },
2091 { "f21", offsetof(CPUState, fpr[21]) },
2092 { "f22", offsetof(CPUState, fpr[22]) },
2093 { "f23", offsetof(CPUState, fpr[23]) },
2094 { "f24", offsetof(CPUState, fpr[24]) },
2095 { "f25", offsetof(CPUState, fpr[25]) },
2096 { "f26", offsetof(CPUState, fpr[26]) },
2097 { "f27", offsetof(CPUState, fpr[27]) },
2098 { "f28", offsetof(CPUState, fpr[28]) },
2099 { "f29", offsetof(CPUState, fpr[29]) },
2100 { "f30", offsetof(CPUState, fpr[30]) },
2101 { "f31", offsetof(CPUState, fpr[31]) },
2102 { "fpscr", offsetof(CPUState, fpscr) },
2103 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002104 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002105 { "lr", offsetof(CPUState, lr) },
2106 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002107 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002108 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002109 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002110 { "msr", 0, &monitor_get_msr, },
2111 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002112 { "tbu", 0, &monitor_get_tbu, },
2113 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002114#if defined(TARGET_PPC64)
2115 /* Address space register */
2116 { "asr", offsetof(CPUState, asr) },
2117#endif
2118 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002119 { "sdr1", offsetof(CPUState, sdr1) },
2120 { "sr0", offsetof(CPUState, sr[0]) },
2121 { "sr1", offsetof(CPUState, sr[1]) },
2122 { "sr2", offsetof(CPUState, sr[2]) },
2123 { "sr3", offsetof(CPUState, sr[3]) },
2124 { "sr4", offsetof(CPUState, sr[4]) },
2125 { "sr5", offsetof(CPUState, sr[5]) },
2126 { "sr6", offsetof(CPUState, sr[6]) },
2127 { "sr7", offsetof(CPUState, sr[7]) },
2128 { "sr8", offsetof(CPUState, sr[8]) },
2129 { "sr9", offsetof(CPUState, sr[9]) },
2130 { "sr10", offsetof(CPUState, sr[10]) },
2131 { "sr11", offsetof(CPUState, sr[11]) },
2132 { "sr12", offsetof(CPUState, sr[12]) },
2133 { "sr13", offsetof(CPUState, sr[13]) },
2134 { "sr14", offsetof(CPUState, sr[14]) },
2135 { "sr15", offsetof(CPUState, sr[15]) },
2136 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002137#elif defined(TARGET_SPARC)
2138 { "g0", offsetof(CPUState, gregs[0]) },
2139 { "g1", offsetof(CPUState, gregs[1]) },
2140 { "g2", offsetof(CPUState, gregs[2]) },
2141 { "g3", offsetof(CPUState, gregs[3]) },
2142 { "g4", offsetof(CPUState, gregs[4]) },
2143 { "g5", offsetof(CPUState, gregs[5]) },
2144 { "g6", offsetof(CPUState, gregs[6]) },
2145 { "g7", offsetof(CPUState, gregs[7]) },
2146 { "o0", 0, monitor_get_reg },
2147 { "o1", 1, monitor_get_reg },
2148 { "o2", 2, monitor_get_reg },
2149 { "o3", 3, monitor_get_reg },
2150 { "o4", 4, monitor_get_reg },
2151 { "o5", 5, monitor_get_reg },
2152 { "o6", 6, monitor_get_reg },
2153 { "o7", 7, monitor_get_reg },
2154 { "l0", 8, monitor_get_reg },
2155 { "l1", 9, monitor_get_reg },
2156 { "l2", 10, monitor_get_reg },
2157 { "l3", 11, monitor_get_reg },
2158 { "l4", 12, monitor_get_reg },
2159 { "l5", 13, monitor_get_reg },
2160 { "l6", 14, monitor_get_reg },
2161 { "l7", 15, monitor_get_reg },
2162 { "i0", 16, monitor_get_reg },
2163 { "i1", 17, monitor_get_reg },
2164 { "i2", 18, monitor_get_reg },
2165 { "i3", 19, monitor_get_reg },
2166 { "i4", 20, monitor_get_reg },
2167 { "i5", 21, monitor_get_reg },
2168 { "i6", 22, monitor_get_reg },
2169 { "i7", 23, monitor_get_reg },
2170 { "pc", offsetof(CPUState, pc) },
2171 { "npc", offsetof(CPUState, npc) },
2172 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002173#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002174 { "psr", 0, &monitor_get_psr, },
2175 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002176#endif
bellarde95c8d52004-09-30 22:22:08 +00002177 { "tbr", offsetof(CPUState, tbr) },
2178 { "fsr", offsetof(CPUState, fsr) },
2179 { "f0", offsetof(CPUState, fpr[0]) },
2180 { "f1", offsetof(CPUState, fpr[1]) },
2181 { "f2", offsetof(CPUState, fpr[2]) },
2182 { "f3", offsetof(CPUState, fpr[3]) },
2183 { "f4", offsetof(CPUState, fpr[4]) },
2184 { "f5", offsetof(CPUState, fpr[5]) },
2185 { "f6", offsetof(CPUState, fpr[6]) },
2186 { "f7", offsetof(CPUState, fpr[7]) },
2187 { "f8", offsetof(CPUState, fpr[8]) },
2188 { "f9", offsetof(CPUState, fpr[9]) },
2189 { "f10", offsetof(CPUState, fpr[10]) },
2190 { "f11", offsetof(CPUState, fpr[11]) },
2191 { "f12", offsetof(CPUState, fpr[12]) },
2192 { "f13", offsetof(CPUState, fpr[13]) },
2193 { "f14", offsetof(CPUState, fpr[14]) },
2194 { "f15", offsetof(CPUState, fpr[15]) },
2195 { "f16", offsetof(CPUState, fpr[16]) },
2196 { "f17", offsetof(CPUState, fpr[17]) },
2197 { "f18", offsetof(CPUState, fpr[18]) },
2198 { "f19", offsetof(CPUState, fpr[19]) },
2199 { "f20", offsetof(CPUState, fpr[20]) },
2200 { "f21", offsetof(CPUState, fpr[21]) },
2201 { "f22", offsetof(CPUState, fpr[22]) },
2202 { "f23", offsetof(CPUState, fpr[23]) },
2203 { "f24", offsetof(CPUState, fpr[24]) },
2204 { "f25", offsetof(CPUState, fpr[25]) },
2205 { "f26", offsetof(CPUState, fpr[26]) },
2206 { "f27", offsetof(CPUState, fpr[27]) },
2207 { "f28", offsetof(CPUState, fpr[28]) },
2208 { "f29", offsetof(CPUState, fpr[29]) },
2209 { "f30", offsetof(CPUState, fpr[30]) },
2210 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002211#ifdef TARGET_SPARC64
2212 { "f32", offsetof(CPUState, fpr[32]) },
2213 { "f34", offsetof(CPUState, fpr[34]) },
2214 { "f36", offsetof(CPUState, fpr[36]) },
2215 { "f38", offsetof(CPUState, fpr[38]) },
2216 { "f40", offsetof(CPUState, fpr[40]) },
2217 { "f42", offsetof(CPUState, fpr[42]) },
2218 { "f44", offsetof(CPUState, fpr[44]) },
2219 { "f46", offsetof(CPUState, fpr[46]) },
2220 { "f48", offsetof(CPUState, fpr[48]) },
2221 { "f50", offsetof(CPUState, fpr[50]) },
2222 { "f52", offsetof(CPUState, fpr[52]) },
2223 { "f54", offsetof(CPUState, fpr[54]) },
2224 { "f56", offsetof(CPUState, fpr[56]) },
2225 { "f58", offsetof(CPUState, fpr[58]) },
2226 { "f60", offsetof(CPUState, fpr[60]) },
2227 { "f62", offsetof(CPUState, fpr[62]) },
2228 { "asi", offsetof(CPUState, asi) },
2229 { "pstate", offsetof(CPUState, pstate) },
2230 { "cansave", offsetof(CPUState, cansave) },
2231 { "canrestore", offsetof(CPUState, canrestore) },
2232 { "otherwin", offsetof(CPUState, otherwin) },
2233 { "wstate", offsetof(CPUState, wstate) },
2234 { "cleanwin", offsetof(CPUState, cleanwin) },
2235 { "fprs", offsetof(CPUState, fprs) },
2236#endif
bellard9307c4c2004-04-04 12:57:25 +00002237#endif
2238 { NULL },
2239};
2240
aliguori376253e2009-03-05 23:01:23 +00002241static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002242{
aliguori376253e2009-03-05 23:01:23 +00002243 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002244 longjmp(expr_env, 1);
2245}
2246
bellard6a00d602005-11-21 23:25:50 +00002247/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002248static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002249{
blueswir18662d652008-10-02 18:32:44 +00002250 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002251 void *ptr;
2252
bellard9307c4c2004-04-04 12:57:25 +00002253 for(md = monitor_defs; md->name != NULL; md++) {
2254 if (compare_cmd(name, md->name)) {
2255 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002256 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002257 } else {
bellard6a00d602005-11-21 23:25:50 +00002258 CPUState *env = mon_get_cpu();
2259 if (!env)
2260 return -2;
2261 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002262 switch(md->type) {
2263 case MD_I32:
2264 *pval = *(int32_t *)ptr;
2265 break;
2266 case MD_TLONG:
2267 *pval = *(target_long *)ptr;
2268 break;
2269 default:
2270 *pval = 0;
2271 break;
2272 }
bellard9307c4c2004-04-04 12:57:25 +00002273 }
2274 return 0;
2275 }
2276 }
2277 return -1;
2278}
2279
2280static void next(void)
2281{
Blue Swirl660f11b2009-07-31 21:16:51 +00002282 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002283 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002284 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002285 pch++;
2286 }
2287}
2288
aliguori376253e2009-03-05 23:01:23 +00002289static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002290
aliguori376253e2009-03-05 23:01:23 +00002291static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002292{
blueswir1c2efc952007-09-25 17:28:42 +00002293 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002294 char *p;
bellard6a00d602005-11-21 23:25:50 +00002295 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002296
2297 switch(*pch) {
2298 case '+':
2299 next();
aliguori376253e2009-03-05 23:01:23 +00002300 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002301 break;
2302 case '-':
2303 next();
aliguori376253e2009-03-05 23:01:23 +00002304 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002305 break;
2306 case '~':
2307 next();
aliguori376253e2009-03-05 23:01:23 +00002308 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002309 break;
2310 case '(':
2311 next();
aliguori376253e2009-03-05 23:01:23 +00002312 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002313 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002314 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002315 }
2316 next();
2317 break;
bellard81d09122004-07-14 17:21:37 +00002318 case '\'':
2319 pch++;
2320 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002321 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002322 n = *pch;
2323 pch++;
2324 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002325 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002326 next();
2327 break;
bellard9307c4c2004-04-04 12:57:25 +00002328 case '$':
2329 {
2330 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002331 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002332
bellard9307c4c2004-04-04 12:57:25 +00002333 pch++;
2334 q = buf;
2335 while ((*pch >= 'a' && *pch <= 'z') ||
2336 (*pch >= 'A' && *pch <= 'Z') ||
2337 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002338 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002339 if ((q - buf) < sizeof(buf) - 1)
2340 *q++ = *pch;
2341 pch++;
2342 }
blueswir1cd390082008-11-16 13:53:32 +00002343 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002344 pch++;
2345 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002346 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002347 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002348 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002349 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002350 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002351 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002352 }
2353 break;
2354 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002355 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002356 n = 0;
2357 break;
2358 default:
blueswir17743e582007-09-24 18:39:04 +00002359#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002360 n = strtoull(pch, &p, 0);
2361#else
bellard9307c4c2004-04-04 12:57:25 +00002362 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002363#endif
bellard9307c4c2004-04-04 12:57:25 +00002364 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002365 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002366 }
2367 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002368 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002369 pch++;
2370 break;
2371 }
2372 return n;
2373}
2374
2375
aliguori376253e2009-03-05 23:01:23 +00002376static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002377{
blueswir1c2efc952007-09-25 17:28:42 +00002378 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002379 int op;
ths3b46e622007-09-17 08:09:54 +00002380
aliguori376253e2009-03-05 23:01:23 +00002381 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002382 for(;;) {
2383 op = *pch;
2384 if (op != '*' && op != '/' && op != '%')
2385 break;
2386 next();
aliguori376253e2009-03-05 23:01:23 +00002387 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002388 switch(op) {
2389 default:
2390 case '*':
2391 val *= val2;
2392 break;
2393 case '/':
2394 case '%':
ths5fafdf22007-09-16 21:08:06 +00002395 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002396 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002397 if (op == '/')
2398 val /= val2;
2399 else
2400 val %= val2;
2401 break;
2402 }
2403 }
2404 return val;
2405}
2406
aliguori376253e2009-03-05 23:01:23 +00002407static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002408{
blueswir1c2efc952007-09-25 17:28:42 +00002409 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002410 int op;
bellard9307c4c2004-04-04 12:57:25 +00002411
aliguori376253e2009-03-05 23:01:23 +00002412 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002413 for(;;) {
2414 op = *pch;
2415 if (op != '&' && op != '|' && op != '^')
2416 break;
2417 next();
aliguori376253e2009-03-05 23:01:23 +00002418 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002419 switch(op) {
2420 default:
2421 case '&':
2422 val &= val2;
2423 break;
2424 case '|':
2425 val |= val2;
2426 break;
2427 case '^':
2428 val ^= val2;
2429 break;
2430 }
2431 }
2432 return val;
2433}
2434
aliguori376253e2009-03-05 23:01:23 +00002435static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002436{
blueswir1c2efc952007-09-25 17:28:42 +00002437 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002438 int op;
bellard9307c4c2004-04-04 12:57:25 +00002439
aliguori376253e2009-03-05 23:01:23 +00002440 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002441 for(;;) {
2442 op = *pch;
2443 if (op != '+' && op != '-')
2444 break;
2445 next();
aliguori376253e2009-03-05 23:01:23 +00002446 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002447 if (op == '+')
2448 val += val2;
2449 else
2450 val -= val2;
2451 }
2452 return val;
2453}
2454
aliguori376253e2009-03-05 23:01:23 +00002455static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002456{
2457 pch = *pp;
2458 if (setjmp(expr_env)) {
2459 *pp = pch;
2460 return -1;
2461 }
blueswir1cd390082008-11-16 13:53:32 +00002462 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002463 pch++;
aliguori376253e2009-03-05 23:01:23 +00002464 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002465 *pp = pch;
2466 return 0;
2467}
2468
2469static int get_str(char *buf, int buf_size, const char **pp)
2470{
2471 const char *p;
2472 char *q;
2473 int c;
2474
bellard81d09122004-07-14 17:21:37 +00002475 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002476 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002477 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002478 p++;
2479 if (*p == '\0') {
2480 fail:
bellard81d09122004-07-14 17:21:37 +00002481 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002482 *pp = p;
2483 return -1;
2484 }
bellard9307c4c2004-04-04 12:57:25 +00002485 if (*p == '\"') {
2486 p++;
2487 while (*p != '\0' && *p != '\"') {
2488 if (*p == '\\') {
2489 p++;
2490 c = *p++;
2491 switch(c) {
2492 case 'n':
2493 c = '\n';
2494 break;
2495 case 'r':
2496 c = '\r';
2497 break;
2498 case '\\':
2499 case '\'':
2500 case '\"':
2501 break;
2502 default:
2503 qemu_printf("unsupported escape code: '\\%c'\n", c);
2504 goto fail;
2505 }
2506 if ((q - buf) < buf_size - 1) {
2507 *q++ = c;
2508 }
2509 } else {
2510 if ((q - buf) < buf_size - 1) {
2511 *q++ = *p;
2512 }
2513 p++;
2514 }
2515 }
2516 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002517 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002518 goto fail;
2519 }
2520 p++;
2521 } else {
blueswir1cd390082008-11-16 13:53:32 +00002522 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002523 if ((q - buf) < buf_size - 1) {
2524 *q++ = *p;
2525 }
2526 p++;
2527 }
bellard9307c4c2004-04-04 12:57:25 +00002528 }
bellard81d09122004-07-14 17:21:37 +00002529 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002530 *pp = p;
2531 return 0;
2532}
2533
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002534/*
2535 * Store the command-name in cmdname, and return a pointer to
2536 * the remaining of the command string.
2537 */
2538static const char *get_command_name(const char *cmdline,
2539 char *cmdname, size_t nlen)
2540{
2541 size_t len;
2542 const char *p, *pstart;
2543
2544 p = cmdline;
2545 while (qemu_isspace(*p))
2546 p++;
2547 if (*p == '\0')
2548 return NULL;
2549 pstart = p;
2550 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2551 p++;
2552 len = p - pstart;
2553 if (len > nlen - 1)
2554 len = nlen - 1;
2555 memcpy(cmdname, pstart, len);
2556 cmdname[len] = '\0';
2557 return p;
2558}
2559
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002560/**
2561 * Read key of 'type' into 'key' and return the current
2562 * 'type' pointer.
2563 */
2564static char *key_get_info(const char *type, char **key)
2565{
2566 size_t len;
2567 char *p, *str;
2568
2569 if (*type == ',')
2570 type++;
2571
2572 p = strchr(type, ':');
2573 if (!p) {
2574 *key = NULL;
2575 return NULL;
2576 }
2577 len = p - type;
2578
2579 str = qemu_malloc(len + 1);
2580 memcpy(str, type, len);
2581 str[len] = '\0';
2582
2583 *key = str;
2584 return ++p;
2585}
2586
bellard9307c4c2004-04-04 12:57:25 +00002587static int default_fmt_format = 'x';
2588static int default_fmt_size = 4;
2589
2590#define MAX_ARGS 16
2591
aliguori376253e2009-03-05 23:01:23 +00002592static void monitor_handle_command(Monitor *mon, const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002593{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002594 const char *p, *typestr;
2595 int c, nb_args, i, has_arg;
aliguori376253e2009-03-05 23:01:23 +00002596 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002597 char cmdname[256];
2598 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002599 char *key;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002600 QDict *qdict;
bellard9307c4c2004-04-04 12:57:25 +00002601 void *str_allocated[MAX_ARGS];
2602 void *args[MAX_ARGS];
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03002603 void (*handler_d)(Monitor *mon, const QDict *qdict);
aliguori376253e2009-03-05 23:01:23 +00002604 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2605 void *arg3, void *arg4, void *arg5, void *arg6);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002606 void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2607 void *arg3, void *arg4, void *arg5, void *arg6,
2608 void *arg7);
2609 void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2610 void *arg3, void *arg4, void *arg5, void *arg6,
2611 void *arg7, void *arg8);
2612 void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2613 void *arg3, void *arg4, void *arg5, void *arg6,
2614 void *arg7, void *arg8, void *arg9);
bellard9dc39cb2004-03-14 21:38:27 +00002615
2616#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002617 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002618#endif
ths3b46e622007-09-17 08:09:54 +00002619
bellard9307c4c2004-04-04 12:57:25 +00002620 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002621 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2622 if (!p)
bellard9dc39cb2004-03-14 21:38:27 +00002623 return;
ths3b46e622007-09-17 08:09:54 +00002624
bellard9307c4c2004-04-04 12:57:25 +00002625 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002626 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002627 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002628 break;
bellard9dc39cb2004-03-14 21:38:27 +00002629 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002630
2631 if (cmd->name == NULL) {
2632 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2633 return;
2634 }
bellard9307c4c2004-04-04 12:57:25 +00002635
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002636 qdict = qdict_new();
2637
bellard9307c4c2004-04-04 12:57:25 +00002638 for(i = 0; i < MAX_ARGS; i++)
2639 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002640
bellard9307c4c2004-04-04 12:57:25 +00002641 /* parse the parameters */
2642 typestr = cmd->args_type;
2643 nb_args = 0;
2644 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002645 typestr = key_get_info(typestr, &key);
2646 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002647 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002648 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002649 typestr++;
2650 switch(c) {
2651 case 'F':
bellard81d09122004-07-14 17:21:37 +00002652 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002653 case 's':
2654 {
2655 int ret;
2656 char *str;
ths3b46e622007-09-17 08:09:54 +00002657
blueswir1cd390082008-11-16 13:53:32 +00002658 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002659 p++;
2660 if (*typestr == '?') {
2661 typestr++;
2662 if (*p == '\0') {
2663 /* no optional string: NULL argument */
2664 str = NULL;
2665 goto add_str;
2666 }
2667 }
2668 ret = get_str(buf, sizeof(buf), &p);
2669 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002670 switch(c) {
2671 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002672 monitor_printf(mon, "%s: filename expected\n",
2673 cmdname);
bellard81d09122004-07-14 17:21:37 +00002674 break;
2675 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002676 monitor_printf(mon, "%s: block device name expected\n",
2677 cmdname);
bellard81d09122004-07-14 17:21:37 +00002678 break;
2679 default:
aliguori376253e2009-03-05 23:01:23 +00002680 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002681 break;
2682 }
bellard9307c4c2004-04-04 12:57:25 +00002683 goto fail;
2684 }
2685 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002686 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002687 str_allocated[nb_args] = str;
2688 add_str:
2689 if (nb_args >= MAX_ARGS) {
2690 error_args:
aliguori376253e2009-03-05 23:01:23 +00002691 monitor_printf(mon, "%s: too many arguments\n", cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002692 goto fail;
2693 }
2694 args[nb_args++] = str;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002695 if (str)
2696 qdict_put(qdict, key, qstring_from_str(str));
bellard9307c4c2004-04-04 12:57:25 +00002697 }
2698 break;
2699 case '/':
2700 {
2701 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002702
blueswir1cd390082008-11-16 13:53:32 +00002703 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002704 p++;
2705 if (*p == '/') {
2706 /* format found */
2707 p++;
2708 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002709 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002710 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002711 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002712 count = count * 10 + (*p - '0');
2713 p++;
2714 }
2715 }
2716 size = -1;
2717 format = -1;
2718 for(;;) {
2719 switch(*p) {
2720 case 'o':
2721 case 'd':
2722 case 'u':
2723 case 'x':
2724 case 'i':
2725 case 'c':
2726 format = *p++;
2727 break;
2728 case 'b':
2729 size = 1;
2730 p++;
2731 break;
2732 case 'h':
2733 size = 2;
2734 p++;
2735 break;
2736 case 'w':
2737 size = 4;
2738 p++;
2739 break;
2740 case 'g':
2741 case 'L':
2742 size = 8;
2743 p++;
2744 break;
2745 default:
2746 goto next;
2747 }
2748 }
2749 next:
blueswir1cd390082008-11-16 13:53:32 +00002750 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002751 monitor_printf(mon, "invalid char in format: '%c'\n",
2752 *p);
bellard9307c4c2004-04-04 12:57:25 +00002753 goto fail;
2754 }
bellard9307c4c2004-04-04 12:57:25 +00002755 if (format < 0)
2756 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002757 if (format != 'i') {
2758 /* for 'i', not specifying a size gives -1 as size */
2759 if (size < 0)
2760 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002761 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002762 }
bellard9307c4c2004-04-04 12:57:25 +00002763 default_fmt_format = format;
2764 } else {
2765 count = 1;
2766 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002767 if (format != 'i') {
2768 size = default_fmt_size;
2769 } else {
2770 size = -1;
2771 }
bellard9307c4c2004-04-04 12:57:25 +00002772 }
2773 if (nb_args + 3 > MAX_ARGS)
2774 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002775 args[nb_args++] = (void*)(long)count;
2776 args[nb_args++] = (void*)(long)format;
2777 args[nb_args++] = (void*)(long)size;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002778 qdict_put(qdict, "count", qint_from_int(count));
2779 qdict_put(qdict, "format", qint_from_int(format));
2780 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002781 }
2782 break;
2783 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002784 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002785 {
blueswir1c2efc952007-09-25 17:28:42 +00002786 int64_t val;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002787 int dict_add = 1;
blueswir17743e582007-09-24 18:39:04 +00002788
blueswir1cd390082008-11-16 13:53:32 +00002789 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002790 p++;
bellard34405572004-06-08 00:55:58 +00002791 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002792 if (*typestr == '?') {
2793 if (*p == '\0')
2794 has_arg = 0;
2795 else
2796 has_arg = 1;
2797 } else {
2798 if (*p == '.') {
2799 p++;
blueswir1cd390082008-11-16 13:53:32 +00002800 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002801 p++;
2802 has_arg = 1;
2803 } else {
2804 has_arg = 0;
2805 }
2806 }
bellard13224a82006-07-14 22:03:35 +00002807 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002808 if (nb_args >= MAX_ARGS)
2809 goto error_args;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002810 dict_add = has_arg;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002811 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002812 if (!has_arg) {
2813 if (nb_args >= MAX_ARGS)
2814 goto error_args;
2815 val = -1;
2816 goto add_num;
2817 }
2818 }
aliguori376253e2009-03-05 23:01:23 +00002819 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002820 goto fail;
2821 add_num:
bellard92a31b12005-02-10 22:00:52 +00002822 if (c == 'i') {
2823 if (nb_args >= MAX_ARGS)
2824 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002825 args[nb_args++] = (void *)(long)val;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002826 if (dict_add)
2827 qdict_put(qdict, key, qint_from_int(val));
bellard92a31b12005-02-10 22:00:52 +00002828 } else {
2829 if ((nb_args + 1) >= MAX_ARGS)
2830 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002831#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002832 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002833#else
2834 args[nb_args++] = (void *)0;
2835#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002836 args[nb_args++] = (void *)(long)(val & 0xffffffff);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002837 qdict_put(qdict, key, qint_from_int(val));
bellard92a31b12005-02-10 22:00:52 +00002838 }
bellard9307c4c2004-04-04 12:57:25 +00002839 }
2840 break;
2841 case '-':
2842 {
2843 int has_option;
2844 /* option */
ths3b46e622007-09-17 08:09:54 +00002845
bellard9307c4c2004-04-04 12:57:25 +00002846 c = *typestr++;
2847 if (c == '\0')
2848 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002849 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002850 p++;
2851 has_option = 0;
2852 if (*p == '-') {
2853 p++;
2854 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002855 monitor_printf(mon, "%s: unsupported option -%c\n",
2856 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002857 goto fail;
2858 }
2859 p++;
2860 has_option = 1;
2861 }
2862 if (nb_args >= MAX_ARGS)
2863 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002864 args[nb_args++] = (void *)(long)has_option;
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002865 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00002866 }
2867 break;
2868 default:
2869 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002870 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002871 goto fail;
2872 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002873 qemu_free(key);
2874 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002875 }
2876 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002877 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002878 p++;
2879 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002880 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2881 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002882 goto fail;
2883 }
2884
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002885 qemu_errors_to_mon(mon);
bellard9307c4c2004-04-04 12:57:25 +00002886 switch(nb_args) {
2887 case 0:
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002888 case 1:
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002889 case 2:
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03002890 case 3:
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03002891 case 4:
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002892 case 5:
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03002893 case 6:
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03002894 handler_d = cmd->handler;
2895 handler_d(mon, qdict);
bellard9307c4c2004-04-04 12:57:25 +00002896 break;
bellardec36b692006-07-16 18:57:03 +00002897 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002898 handler_7 = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +00002899 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2900 args[6]);
bellardec36b692006-07-16 18:57:03 +00002901 break;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002902 case 8:
2903 handler_8 = cmd->handler;
2904 handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2905 args[6], args[7]);
2906 break;
2907 case 9:
2908 handler_9 = cmd->handler;
2909 handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2910 args[6], args[7], args[8]);
2911 break;
2912 case 10:
2913 handler_10 = cmd->handler;
2914 handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2915 args[6], args[7], args[8], args[9]);
2916 break;
bellard9307c4c2004-04-04 12:57:25 +00002917 default:
aliguori376253e2009-03-05 23:01:23 +00002918 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002919 break;
bellard9307c4c2004-04-04 12:57:25 +00002920 }
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002921 qemu_errors_to_previous();
2922
bellard9307c4c2004-04-04 12:57:25 +00002923 fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002924 qemu_free(key);
bellard9307c4c2004-04-04 12:57:25 +00002925 for(i = 0; i < MAX_ARGS; i++)
2926 qemu_free(str_allocated[i]);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002927 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002928}
2929
bellard81d09122004-07-14 17:21:37 +00002930static void cmd_completion(const char *name, const char *list)
2931{
2932 const char *p, *pstart;
2933 char cmd[128];
2934 int len;
2935
2936 p = list;
2937 for(;;) {
2938 pstart = p;
2939 p = strchr(p, '|');
2940 if (!p)
2941 p = pstart + strlen(pstart);
2942 len = p - pstart;
2943 if (len > sizeof(cmd) - 2)
2944 len = sizeof(cmd) - 2;
2945 memcpy(cmd, pstart, len);
2946 cmd[len] = '\0';
2947 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002948 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002949 }
2950 if (*p == '\0')
2951 break;
2952 p++;
2953 }
2954}
2955
2956static void file_completion(const char *input)
2957{
2958 DIR *ffs;
2959 struct dirent *d;
2960 char path[1024];
2961 char file[1024], file_prefix[1024];
2962 int input_path_len;
2963 const char *p;
2964
ths5fafdf22007-09-16 21:08:06 +00002965 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002966 if (!p) {
2967 input_path_len = 0;
2968 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002969 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002970 } else {
2971 input_path_len = p - input + 1;
2972 memcpy(path, input, input_path_len);
2973 if (input_path_len > sizeof(path) - 1)
2974 input_path_len = sizeof(path) - 1;
2975 path[input_path_len] = '\0';
2976 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2977 }
2978#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002979 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2980 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002981#endif
2982 ffs = opendir(path);
2983 if (!ffs)
2984 return;
2985 for(;;) {
2986 struct stat sb;
2987 d = readdir(ffs);
2988 if (!d)
2989 break;
2990 if (strstart(d->d_name, file_prefix, NULL)) {
2991 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002992 if (input_path_len < sizeof(file))
2993 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2994 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002995 /* stat the file to find out if it's a directory.
2996 * In that case add a slash to speed up typing long paths
2997 */
2998 stat(file, &sb);
2999 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003000 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003001 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003002 }
3003 }
3004 closedir(ffs);
3005}
3006
aliguori51de9762009-03-05 23:00:43 +00003007static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003008{
aliguori51de9762009-03-05 23:00:43 +00003009 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003010 const char *input = opaque;
3011
3012 if (input[0] == '\0' ||
3013 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003014 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003015 }
3016}
3017
3018/* NOTE: this parser is an approximate form of the real command parser */
3019static void parse_cmdline(const char *cmdline,
3020 int *pnb_args, char **args)
3021{
3022 const char *p;
3023 int nb_args, ret;
3024 char buf[1024];
3025
3026 p = cmdline;
3027 nb_args = 0;
3028 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003029 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003030 p++;
3031 if (*p == '\0')
3032 break;
3033 if (nb_args >= MAX_ARGS)
3034 break;
3035 ret = get_str(buf, sizeof(buf), &p);
3036 args[nb_args] = qemu_strdup(buf);
3037 nb_args++;
3038 if (ret < 0)
3039 break;
3040 }
3041 *pnb_args = nb_args;
3042}
3043
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003044static const char *next_arg_type(const char *typestr)
3045{
3046 const char *p = strchr(typestr, ':');
3047 return (p != NULL ? ++p : typestr);
3048}
3049
aliguori4c36ba32009-03-05 23:01:37 +00003050static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003051{
3052 const char *cmdname;
3053 char *args[MAX_ARGS];
3054 int nb_args, i, len;
3055 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00003056 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003057 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003058
3059 parse_cmdline(cmdline, &nb_args, args);
3060#ifdef DEBUG_COMPLETION
3061 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003062 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003063 }
3064#endif
3065
3066 /* if the line ends with a space, it means we want to complete the
3067 next arg */
3068 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003069 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003070 if (nb_args >= MAX_ARGS)
3071 return;
3072 args[nb_args++] = qemu_strdup("");
3073 }
3074 if (nb_args <= 1) {
3075 /* command completion */
3076 if (nb_args == 0)
3077 cmdname = "";
3078 else
3079 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003080 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003081 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003082 cmd_completion(cmdname, cmd->name);
3083 }
3084 } else {
3085 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003086 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003087 if (compare_cmd(args[0], cmd->name))
3088 goto found;
3089 }
3090 return;
3091 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003092 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003093 for(i = 0; i < nb_args - 2; i++) {
3094 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003095 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003096 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003097 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003098 }
3099 }
3100 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003101 if (*ptype == '-' && ptype[1] != '\0') {
3102 ptype += 2;
3103 }
bellard81d09122004-07-14 17:21:37 +00003104 switch(*ptype) {
3105 case 'F':
3106 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003107 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003108 file_completion(str);
3109 break;
3110 case 'B':
3111 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003112 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003113 bdrv_iterate(block_completion_it, (void *)str);
3114 break;
bellard7fe48482004-10-09 18:08:01 +00003115 case 's':
3116 /* XXX: more generic ? */
3117 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003118 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003119 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3120 cmd_completion(str, cmd->name);
3121 }
bellard64866c32006-05-07 18:03:31 +00003122 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003123 char *sep = strrchr(str, '-');
3124 if (sep)
3125 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003126 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003127 for(key = key_defs; key->name != NULL; key++) {
3128 cmd_completion(str, key->name);
3129 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003130 } else if (!strcmp(cmd->name, "help|?")) {
3131 readline_set_completion_index(cur_mon->rs, strlen(str));
3132 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3133 cmd_completion(str, cmd->name);
3134 }
bellard7fe48482004-10-09 18:08:01 +00003135 }
3136 break;
bellard81d09122004-07-14 17:21:37 +00003137 default:
3138 break;
3139 }
3140 }
3141 for(i = 0; i < nb_args; i++)
3142 qemu_free(args[i]);
3143}
3144
aliguori731b0362009-03-05 23:01:42 +00003145static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003146{
aliguori731b0362009-03-05 23:01:42 +00003147 Monitor *mon = opaque;
3148
3149 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003150}
3151
aliguori731b0362009-03-05 23:01:42 +00003152static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003153{
aliguori731b0362009-03-05 23:01:42 +00003154 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003155 int i;
aliguori376253e2009-03-05 23:01:23 +00003156
aliguori731b0362009-03-05 23:01:42 +00003157 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003158
aliguoricde76ee2009-03-05 23:01:51 +00003159 if (cur_mon->rs) {
3160 for (i = 0; i < size; i++)
3161 readline_handle_byte(cur_mon->rs, buf[i]);
3162 } else {
3163 if (size == 0 || buf[size - 1] != 0)
3164 monitor_printf(cur_mon, "corrupted command\n");
3165 else
3166 monitor_handle_command(cur_mon, (char *)buf);
3167 }
aliguori731b0362009-03-05 23:01:42 +00003168
3169 cur_mon = old_mon;
3170}
aliguorid8f44602008-10-06 13:52:44 +00003171
aliguori376253e2009-03-05 23:01:23 +00003172static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003173{
aliguori731b0362009-03-05 23:01:42 +00003174 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003175 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003176 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003177}
3178
aliguoricde76ee2009-03-05 23:01:51 +00003179int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003180{
aliguoricde76ee2009-03-05 23:01:51 +00003181 if (!mon->rs)
3182 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003183 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003184 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003185}
3186
aliguori376253e2009-03-05 23:01:23 +00003187void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003188{
aliguoricde76ee2009-03-05 23:01:51 +00003189 if (!mon->rs)
3190 return;
aliguori731b0362009-03-05 23:01:42 +00003191 if (--mon->suspend_cnt == 0)
3192 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003193}
3194
aliguori731b0362009-03-05 23:01:42 +00003195static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003196{
aliguori376253e2009-03-05 23:01:23 +00003197 Monitor *mon = opaque;
3198
aliguori2724b182009-03-05 23:01:47 +00003199 switch (event) {
3200 case CHR_EVENT_MUX_IN:
3201 readline_restart(mon->rs);
3202 monitor_resume(mon);
3203 monitor_flush(mon);
3204 break;
ths86e94de2007-01-05 22:01:59 +00003205
aliguori2724b182009-03-05 23:01:47 +00003206 case CHR_EVENT_MUX_OUT:
3207 if (mon->suspend_cnt == 0)
3208 monitor_printf(mon, "\n");
3209 monitor_flush(mon);
3210 monitor_suspend(mon);
3211 break;
3212
3213 case CHR_EVENT_RESET:
3214 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3215 "information\n", QEMU_VERSION);
3216 if (mon->chr->focus == 0)
3217 readline_show_prompt(mon->rs);
3218 break;
3219 }
ths86e94de2007-01-05 22:01:59 +00003220}
3221
aliguori76655d62009-03-06 20:27:37 +00003222
3223/*
3224 * Local variables:
3225 * c-indent-level: 4
3226 * c-basic-offset: 4
3227 * tab-width: 8
3228 * End:
3229 */
3230
aliguori731b0362009-03-05 23:01:42 +00003231void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003232{
aliguori731b0362009-03-05 23:01:42 +00003233 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003234 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003235
3236 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003237 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003238 is_first_init = 0;
3239 }
aliguori87127162009-03-05 23:01:29 +00003240
3241 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003242
aliguori87127162009-03-05 23:01:29 +00003243 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003244 mon->flags = flags;
aliguori2724b182009-03-05 23:01:47 +00003245 if (mon->chr->focus != 0)
3246 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
aliguoricde76ee2009-03-05 23:01:51 +00003247 if (flags & MONITOR_USE_READLINE) {
3248 mon->rs = readline_init(mon, monitor_find_completion);
3249 monitor_read_command(mon, 0);
3250 }
aliguori87127162009-03-05 23:01:29 +00003251
aliguori731b0362009-03-05 23:01:42 +00003252 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3253 mon);
aliguori87127162009-03-05 23:01:29 +00003254
3255 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003256 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003257 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003258}
3259
aliguori376253e2009-03-05 23:01:23 +00003260static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003261{
aliguoribb5fc202009-03-05 23:01:15 +00003262 BlockDriverState *bs = opaque;
3263 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003264
aliguoribb5fc202009-03-05 23:01:15 +00003265 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003266 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003267 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003268 }
aliguori731b0362009-03-05 23:01:42 +00003269 if (mon->password_completion_cb)
3270 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003271
aliguori731b0362009-03-05 23:01:42 +00003272 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003273}
aliguoric0f4ce72009-03-05 23:01:01 +00003274
aliguori376253e2009-03-05 23:01:23 +00003275void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003276 BlockDriverCompletionFunc *completion_cb,
3277 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003278{
aliguoricde76ee2009-03-05 23:01:51 +00003279 int err;
3280
aliguoribb5fc202009-03-05 23:01:15 +00003281 if (!bdrv_key_required(bs)) {
3282 if (completion_cb)
3283 completion_cb(opaque, 0);
3284 return;
3285 }
aliguoric0f4ce72009-03-05 23:01:01 +00003286
aliguori376253e2009-03-05 23:01:23 +00003287 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3288 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003289
aliguori731b0362009-03-05 23:01:42 +00003290 mon->password_completion_cb = completion_cb;
3291 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003292
aliguoricde76ee2009-03-05 23:01:51 +00003293 err = monitor_read_password(mon, bdrv_password_cb, bs);
3294
3295 if (err && completion_cb)
3296 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003297}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003298
3299typedef struct QemuErrorSink QemuErrorSink;
3300struct QemuErrorSink {
3301 enum {
3302 ERR_SINK_FILE,
3303 ERR_SINK_MONITOR,
3304 } dest;
3305 union {
3306 FILE *fp;
3307 Monitor *mon;
3308 };
3309 QemuErrorSink *previous;
3310};
3311
Blue Swirl528e93a2009-08-31 15:14:40 +00003312static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003313
3314void qemu_errors_to_file(FILE *fp)
3315{
3316 QemuErrorSink *sink;
3317
3318 sink = qemu_mallocz(sizeof(*sink));
3319 sink->dest = ERR_SINK_FILE;
3320 sink->fp = fp;
3321 sink->previous = qemu_error_sink;
3322 qemu_error_sink = sink;
3323}
3324
3325void qemu_errors_to_mon(Monitor *mon)
3326{
3327 QemuErrorSink *sink;
3328
3329 sink = qemu_mallocz(sizeof(*sink));
3330 sink->dest = ERR_SINK_MONITOR;
3331 sink->mon = mon;
3332 sink->previous = qemu_error_sink;
3333 qemu_error_sink = sink;
3334}
3335
3336void qemu_errors_to_previous(void)
3337{
3338 QemuErrorSink *sink;
3339
3340 assert(qemu_error_sink != NULL);
3341 sink = qemu_error_sink;
3342 qemu_error_sink = sink->previous;
3343 qemu_free(sink);
3344}
3345
3346void qemu_error(const char *fmt, ...)
3347{
3348 va_list args;
3349
3350 assert(qemu_error_sink != NULL);
3351 switch (qemu_error_sink->dest) {
3352 case ERR_SINK_FILE:
3353 va_start(args, fmt);
3354 vfprintf(qemu_error_sink->fp, fmt, args);
3355 va_end(args);
3356 break;
3357 case ERR_SINK_MONITOR:
3358 va_start(args, fmt);
3359 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3360 va_end(args);
3361 break;
3362 }
3363}