blob: 001d350376fe6bb966e2affbabd40e155296b6af [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 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030064 * '?' optional type (for all types, except '/')
65 * '.' other form of optional type (for 'i' and 'l')
66 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000067 *
68 */
69
aliguori376253e2009-03-05 23:01:23 +000070typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000071 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000072 const char *args_type;
blueswir1a5f1b962008-08-17 20:21:51 +000073 void *handler;
bellard9dc39cb2004-03-14 21:38:27 +000074 const char *params;
75 const char *help;
aliguori376253e2009-03-05 23:01:23 +000076} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000077
Mark McLoughlinf07918f2009-07-22 09:11:40 +010078/* file descriptors passed via SCM_RIGHTS */
79typedef struct mon_fd_t mon_fd_t;
80struct mon_fd_t {
81 char *name;
82 int fd;
83 LIST_ENTRY(mon_fd_t) next;
84};
85
aliguori87127162009-03-05 23:01:29 +000086struct Monitor {
87 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +020088 int mux_out;
89 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +000090 int flags;
91 int suspend_cnt;
92 uint8_t outbuf[1024];
93 int outbuf_index;
94 ReadLineState *rs;
95 CPUState *mon_cpu;
96 BlockDriverCompletionFunc *password_completion_cb;
97 void *password_opaque;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010098 LIST_HEAD(,mon_fd_t) fds;
aliguori87127162009-03-05 23:01:29 +000099 LIST_ENTRY(Monitor) entry;
100};
101
102static LIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000103
aliguori376253e2009-03-05 23:01:23 +0000104static const mon_cmd_t mon_cmds[];
105static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000106
aliguori87127162009-03-05 23:01:29 +0000107Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000108
aliguori731b0362009-03-05 23:01:42 +0000109static void monitor_command_cb(Monitor *mon, const char *cmdline,
110 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000111
aliguori731b0362009-03-05 23:01:42 +0000112static void monitor_read_command(Monitor *mon, int show_prompt)
113{
114 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
115 if (show_prompt)
116 readline_show_prompt(mon->rs);
117}
bellard6a00d602005-11-21 23:25:50 +0000118
aliguoricde76ee2009-03-05 23:01:51 +0000119static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
120 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000121{
aliguoricde76ee2009-03-05 23:01:51 +0000122 if (mon->rs) {
123 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
124 /* prompt is printed on return from the command handler */
125 return 0;
126 } else {
127 monitor_printf(mon, "terminal does not support password prompting\n");
128 return -ENOTTY;
129 }
aliguoribb5fc202009-03-05 23:01:15 +0000130}
131
aliguori376253e2009-03-05 23:01:23 +0000132void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000133{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200134 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000135 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
136 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000137 }
138}
139
140/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000141static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000142{
ths60fe76f2007-12-16 03:02:09 +0000143 char c;
aliguori731b0362009-03-05 23:01:42 +0000144
145 if (!mon)
146 return;
147
bellard7e2515e2004-08-01 21:52:19 +0000148 for(;;) {
149 c = *str++;
150 if (c == '\0')
151 break;
bellard7ba12602006-07-14 20:26:42 +0000152 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000153 mon->outbuf[mon->outbuf_index++] = '\r';
154 mon->outbuf[mon->outbuf_index++] = c;
155 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
156 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000157 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000158 }
159}
160
aliguori376253e2009-03-05 23:01:23 +0000161void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000162{
163 char buf[4096];
164 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000165 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000166}
167
aliguori376253e2009-03-05 23:01:23 +0000168void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000169{
170 va_list ap;
171 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000172 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000173 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000174}
175
aliguori376253e2009-03-05 23:01:23 +0000176void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000177{
178 int i;
179
180 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000181 switch (filename[i]) {
182 case ' ':
183 case '"':
184 case '\\':
185 monitor_printf(mon, "\\%c", filename[i]);
186 break;
187 case '\t':
188 monitor_printf(mon, "\\t");
189 break;
190 case '\r':
191 monitor_printf(mon, "\\r");
192 break;
193 case '\n':
194 monitor_printf(mon, "\\n");
195 break;
196 default:
197 monitor_printf(mon, "%c", filename[i]);
198 break;
199 }
thsfef30742006-12-22 14:11:32 +0000200 }
201}
202
bellard7fe48482004-10-09 18:08:01 +0000203static int monitor_fprintf(FILE *stream, const char *fmt, ...)
204{
205 va_list ap;
206 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000207 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000208 va_end(ap);
209 return 0;
210}
211
bellard9dc39cb2004-03-14 21:38:27 +0000212static int compare_cmd(const char *name, const char *list)
213{
214 const char *p, *pstart;
215 int len;
216 len = strlen(name);
217 p = list;
218 for(;;) {
219 pstart = p;
220 p = strchr(p, '|');
221 if (!p)
222 p = pstart + strlen(pstart);
223 if ((p - pstart) == len && !memcmp(pstart, name, len))
224 return 1;
225 if (*p == '\0')
226 break;
227 p++;
228 }
229 return 0;
230}
231
aliguori376253e2009-03-05 23:01:23 +0000232static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
233 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000234{
aliguori376253e2009-03-05 23:01:23 +0000235 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000236
237 for(cmd = cmds; cmd->name != NULL; cmd++) {
238 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000239 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
240 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000241 }
242}
243
aliguori376253e2009-03-05 23:01:23 +0000244static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000245{
246 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000247 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000248 } else {
aliguori376253e2009-03-05 23:01:23 +0000249 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000250 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000251 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000252 monitor_printf(mon, "Log items (comma separated):\n");
253 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000254 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000255 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000256 }
257 }
bellard9dc39cb2004-03-14 21:38:27 +0000258 }
259}
260
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300261static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300262{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300263 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300264}
265
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300266static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000267{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200268 int all_devices;
269 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300270 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000271
bellard7954c732006-08-01 15:52:40 +0000272 all_devices = !strcmp(device, "all");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200273 TAILQ_FOREACH(dinfo, &drives, next) {
274 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300275 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200276 continue;
277 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000278 }
279}
280
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300281static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000282{
aliguori376253e2009-03-05 23:01:23 +0000283 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300284 const char *item = qdict_get_try_str(qdict, "item");
aliguori376253e2009-03-05 23:01:23 +0000285 void (*handler)(Monitor *);
bellard9dc39cb2004-03-14 21:38:27 +0000286
bellard9307c4c2004-04-04 12:57:25 +0000287 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000288 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000289 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000290 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000291 goto found;
292 }
293 help:
aliguori376253e2009-03-05 23:01:23 +0000294 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000295 return;
296 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000297 handler = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +0000298 handler(mon);
bellard9dc39cb2004-03-14 21:38:27 +0000299}
300
aliguori376253e2009-03-05 23:01:23 +0000301static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000302{
pbrook4a19f1e2009-04-07 23:17:49 +0000303 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000304}
305
aliguori376253e2009-03-05 23:01:23 +0000306static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000307{
308 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000309 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000310}
311
aurel32bf4f74c2008-12-18 22:42:34 +0000312#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000313static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000314{
aliguori376253e2009-03-05 23:01:23 +0000315 monitor_printf(mon, "HPET is %s by QEMU\n",
316 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000317}
aurel32bf4f74c2008-12-18 22:42:34 +0000318#endif
aliguori16b29ae2008-12-17 23:28:44 +0000319
aliguori376253e2009-03-05 23:01:23 +0000320static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000321{
aliguori376253e2009-03-05 23:01:23 +0000322 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
323 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
324 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
325 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
326 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000327}
328
bellard6a00d602005-11-21 23:25:50 +0000329/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000330static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000331{
332 CPUState *env;
333
334 for(env = first_cpu; env != NULL; env = env->next_cpu) {
335 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000336 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000337 return 0;
338 }
339 }
340 return -1;
341}
342
pbrook9596ebb2007-11-18 01:44:38 +0000343static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000344{
aliguori731b0362009-03-05 23:01:42 +0000345 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000346 mon_set_cpu(0);
347 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300348 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000349 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000350}
351
aliguori376253e2009-03-05 23:01:23 +0000352static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000353{
bellard6a00d602005-11-21 23:25:50 +0000354 CPUState *env;
355 env = mon_get_cpu();
356 if (!env)
357 return;
bellard9307c4c2004-04-04 12:57:25 +0000358#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000359 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000360 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000361#else
aliguori376253e2009-03-05 23:01:23 +0000362 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000363 0);
bellard9307c4c2004-04-04 12:57:25 +0000364#endif
365}
366
aliguori376253e2009-03-05 23:01:23 +0000367static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000368{
369 CPUState *env;
370
371 /* just to set the default cpu if not already done */
372 mon_get_cpu();
373
374 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Avi Kivity4c0960c2009-08-17 23:19:53 +0300375 cpu_synchronize_state(env);
aliguori376253e2009-03-05 23:01:23 +0000376 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000377 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000378 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000379#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000380 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
381 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000382#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000383 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000384#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000385 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
386 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000387#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000388 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000389#endif
thsead93602007-09-06 00:18:15 +0000390 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000391 monitor_printf(mon, " (halted)");
392 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000393 }
394}
395
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300396static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000397{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300398 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000399 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000400 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000401}
402
aliguori376253e2009-03-05 23:01:23 +0000403static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000404{
aliguori376253e2009-03-05 23:01:23 +0000405 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000406}
407
aliguori376253e2009-03-05 23:01:23 +0000408static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000409{
410 int i;
bellard7e2515e2004-08-01 21:52:19 +0000411 const char *str;
ths3b46e622007-09-17 08:09:54 +0000412
aliguoricde76ee2009-03-05 23:01:51 +0000413 if (!mon->rs)
414 return;
bellard7e2515e2004-08-01 21:52:19 +0000415 i = 0;
416 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000417 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000418 if (!str)
419 break;
aliguori376253e2009-03-05 23:01:23 +0000420 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000421 i++;
bellardaa455482004-04-04 13:07:25 +0000422 }
423}
424
j_mayer76a66252007-03-07 08:32:30 +0000425#if defined(TARGET_PPC)
426/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000427static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000428{
429 CPUState *env;
430
431 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000432 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000433}
434#endif
435
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300436static void do_quit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000437{
438 exit(0);
439}
440
aliguori376253e2009-03-05 23:01:23 +0000441static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000442{
443 if (bdrv_is_inserted(bs)) {
444 if (!force) {
445 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000446 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000447 return -1;
448 }
449 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000450 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000451 return -1;
452 }
453 }
454 bdrv_close(bs);
455 }
456 return 0;
457}
458
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300459static void do_eject(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000460{
461 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300462 int force = qdict_get_int(qdict, "force");
463 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000464
bellard9307c4c2004-04-04 12:57:25 +0000465 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000466 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000467 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000468 return;
469 }
aliguori376253e2009-03-05 23:01:23 +0000470 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000471}
472
aliguori376253e2009-03-05 23:01:23 +0000473static void do_change_block(Monitor *mon, const char *device,
474 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000475{
476 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000477 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000478
bellard9307c4c2004-04-04 12:57:25 +0000479 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000480 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000481 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000482 return;
483 }
aurel322ecea9b2008-06-18 22:10:01 +0000484 if (fmt) {
485 drv = bdrv_find_format(fmt);
486 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000487 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000488 return;
489 }
490 }
aliguori376253e2009-03-05 23:01:23 +0000491 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000492 return;
aurel322ecea9b2008-06-18 22:10:01 +0000493 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000494 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000495}
496
aliguori376253e2009-03-05 23:01:23 +0000497static void change_vnc_password_cb(Monitor *mon, const char *password,
498 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000499{
500 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000501 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000502
aliguori731b0362009-03-05 23:01:42 +0000503 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000504}
505
aliguori376253e2009-03-05 23:01:23 +0000506static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000507{
ths70848512007-08-25 01:37:05 +0000508 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000509 strcmp(target, "password") == 0) {
510 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000511 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000512 strncpy(password, arg, sizeof(password));
513 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000514 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000515 } else {
aliguori376253e2009-03-05 23:01:23 +0000516 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000517 }
ths70848512007-08-25 01:37:05 +0000518 } else {
aliguori28a76be2009-03-06 20:27:40 +0000519 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000520 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000521 }
thse25a5822007-08-25 01:36:20 +0000522}
523
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300524static void do_change(Monitor *mon, const QDict *qdict)
thse25a5822007-08-25 01:36:20 +0000525{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300526 const char *device = qdict_get_str(qdict, "device");
527 const char *target = qdict_get_str(qdict, "target");
528 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000529 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000530 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000531 } else {
aliguori28a76be2009-03-06 20:27:40 +0000532 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000533 }
534}
535
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300536static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000537{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300538 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000539}
540
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300541static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000542{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300543 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000544}
545
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300546static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000547{
548 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300549 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000550
bellard9307c4c2004-04-04 12:57:25 +0000551 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000552 mask = 0;
553 } else {
bellard9307c4c2004-04-04 12:57:25 +0000554 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000555 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000556 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000557 return;
558 }
559 }
560 cpu_set_log(mask);
561}
562
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300563static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000564{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300565 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000566 if (!option || !strcmp(option, "on")) {
567 singlestep = 1;
568 } else if (!strcmp(option, "off")) {
569 singlestep = 0;
570 } else {
571 monitor_printf(mon, "unexpected option %s\n", option);
572 }
573}
574
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300575static void do_stop(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000576{
577 vm_stop(EXCP_INTERRUPT);
578}
579
aliguoribb5fc202009-03-05 23:01:15 +0000580static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000581
aliguori376253e2009-03-05 23:01:23 +0000582struct bdrv_iterate_context {
583 Monitor *mon;
584 int err;
585};
aliguoric0f4ce72009-03-05 23:01:01 +0000586
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300587static void do_cont(Monitor *mon, const QDict *qdict)
aliguori376253e2009-03-05 23:01:23 +0000588{
589 struct bdrv_iterate_context context = { mon, 0 };
590
591 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000592 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000593 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000594 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000595}
596
aliguoribb5fc202009-03-05 23:01:15 +0000597static void bdrv_key_cb(void *opaque, int err)
598{
aliguori376253e2009-03-05 23:01:23 +0000599 Monitor *mon = opaque;
600
aliguoribb5fc202009-03-05 23:01:15 +0000601 /* another key was set successfully, retry to continue */
602 if (!err)
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300603 do_cont(mon, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000604}
605
606static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
607{
aliguori376253e2009-03-05 23:01:23 +0000608 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000609
aliguori376253e2009-03-05 23:01:23 +0000610 if (!context->err && bdrv_key_required(bs)) {
611 context->err = -EBUSY;
612 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
613 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000614 }
615}
616
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300617static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000618{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300619 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000620 if (!device)
621 device = "tcp::" DEFAULT_GDBSTUB_PORT;
622 if (gdbserver_start(device) < 0) {
623 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
624 device);
625 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000626 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000627 } else {
aliguori59030a82009-04-05 18:43:41 +0000628 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
629 device);
bellard8a7ddc32004-03-31 19:00:16 +0000630 }
631}
632
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300633static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100634{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300635 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100636 if (select_watchdog_action(action) == -1) {
637 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
638 }
639}
640
aliguori376253e2009-03-05 23:01:23 +0000641static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000642{
aliguori376253e2009-03-05 23:01:23 +0000643 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000644 switch(c) {
645 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000646 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000647 break;
648 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000649 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000650 break;
651 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000652 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000653 break;
654 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000655 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000656 break;
657 default:
658 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000659 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000660 } else {
aliguori376253e2009-03-05 23:01:23 +0000661 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000662 }
663 break;
664 }
aliguori376253e2009-03-05 23:01:23 +0000665 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000666}
667
aliguori376253e2009-03-05 23:01:23 +0000668static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000669 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000670{
bellard6a00d602005-11-21 23:25:50 +0000671 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000672 int nb_per_line, l, line_size, i, max_digits, len;
673 uint8_t buf[16];
674 uint64_t v;
675
676 if (format == 'i') {
677 int flags;
678 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000679 env = mon_get_cpu();
680 if (!env && !is_physical)
681 return;
bellard9307c4c2004-04-04 12:57:25 +0000682#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000683 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000684 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000685 } else if (wsize == 4) {
686 flags = 0;
687 } else {
bellard6a15fd12006-04-12 21:07:07 +0000688 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000689 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000690 if (env) {
691#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000692 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000693 (env->segs[R_CS].flags & DESC_L_MASK))
694 flags = 2;
695 else
696#endif
697 if (!(env->segs[R_CS].flags & DESC_B_MASK))
698 flags = 1;
699 }
bellard4c27ba22004-04-25 18:05:08 +0000700 }
701#endif
aliguori376253e2009-03-05 23:01:23 +0000702 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000703 return;
704 }
705
706 len = wsize * count;
707 if (wsize == 1)
708 line_size = 8;
709 else
710 line_size = 16;
711 nb_per_line = line_size / wsize;
712 max_digits = 0;
713
714 switch(format) {
715 case 'o':
716 max_digits = (wsize * 8 + 2) / 3;
717 break;
718 default:
719 case 'x':
720 max_digits = (wsize * 8) / 4;
721 break;
722 case 'u':
723 case 'd':
724 max_digits = (wsize * 8 * 10 + 32) / 33;
725 break;
726 case 'c':
727 wsize = 1;
728 break;
729 }
730
731 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000732 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000733 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000734 else
aliguori376253e2009-03-05 23:01:23 +0000735 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000736 l = len;
737 if (l > line_size)
738 l = line_size;
739 if (is_physical) {
740 cpu_physical_memory_rw(addr, buf, l, 0);
741 } else {
bellard6a00d602005-11-21 23:25:50 +0000742 env = mon_get_cpu();
743 if (!env)
744 break;
aliguoric8f79b62008-08-18 14:00:20 +0000745 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000746 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000747 break;
748 }
bellard9307c4c2004-04-04 12:57:25 +0000749 }
ths5fafdf22007-09-16 21:08:06 +0000750 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000751 while (i < l) {
752 switch(wsize) {
753 default:
754 case 1:
755 v = ldub_raw(buf + i);
756 break;
757 case 2:
758 v = lduw_raw(buf + i);
759 break;
760 case 4:
bellard92a31b12005-02-10 22:00:52 +0000761 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000762 break;
763 case 8:
764 v = ldq_raw(buf + i);
765 break;
766 }
aliguori376253e2009-03-05 23:01:23 +0000767 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000768 switch(format) {
769 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000770 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000771 break;
772 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000773 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000774 break;
775 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000776 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000777 break;
778 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000779 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000780 break;
781 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000782 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000783 break;
784 }
785 i += wsize;
786 }
aliguori376253e2009-03-05 23:01:23 +0000787 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000788 addr += l;
789 len -= l;
790 }
791}
792
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300793static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000794{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300795 int count = qdict_get_int(qdict, "count");
796 int format = qdict_get_int(qdict, "format");
797 int size = qdict_get_int(qdict, "size");
798 target_long addr = qdict_get_int(qdict, "addr");
799
aliguori376253e2009-03-05 23:01:23 +0000800 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000801}
802
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300803static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000804{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300805 int count = qdict_get_int(qdict, "count");
806 int format = qdict_get_int(qdict, "format");
807 int size = qdict_get_int(qdict, "size");
808 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
809
aliguori376253e2009-03-05 23:01:23 +0000810 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000811}
812
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300813static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000814{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300815 int format = qdict_get_int(qdict, "format");
816 target_phys_addr_t val = qdict_get_int(qdict, "val");
817
blueswir17743e582007-09-24 18:39:04 +0000818#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000819 switch(format) {
820 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000821 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000822 break;
823 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000824 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000825 break;
826 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000827 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000828 break;
829 default:
830 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000831 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000832 break;
833 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000834 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000835 break;
836 }
bellard92a31b12005-02-10 22:00:52 +0000837#else
838 switch(format) {
839 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000840 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000841 break;
842 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000843 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000844 break;
845 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000846 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000847 break;
848 default:
849 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000850 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000851 break;
852 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000853 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000854 break;
855 }
856#endif
aliguori376253e2009-03-05 23:01:23 +0000857 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000858}
859
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300860static void do_memory_save(Monitor *mon, const QDict *qdict)
bellardb371dc52007-01-03 15:20:39 +0000861{
862 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300863 uint32_t size = qdict_get_int(qdict, "size");
864 const char *filename = qdict_get_str(qdict, "filename");
865 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +0000866 uint32_t l;
867 CPUState *env;
868 uint8_t buf[1024];
869
870 env = mon_get_cpu();
871 if (!env)
872 return;
873
874 f = fopen(filename, "wb");
875 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000876 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000877 return;
878 }
879 while (size != 0) {
880 l = sizeof(buf);
881 if (l > size)
882 l = size;
883 cpu_memory_rw_debug(env, addr, buf, l, 0);
884 fwrite(buf, 1, l, f);
885 addr += l;
886 size -= l;
887 }
888 fclose(f);
889}
890
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300891static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
aurel32a8bdf7a2008-04-11 21:36:14 +0000892{
893 FILE *f;
894 uint32_t l;
895 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300896 uint32_t size = qdict_get_int(qdict, "size");
897 const char *filename = qdict_get_str(qdict, "filename");
898 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +0000899
900 f = fopen(filename, "wb");
901 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000902 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000903 return;
904 }
905 while (size != 0) {
906 l = sizeof(buf);
907 if (l > size)
908 l = size;
909 cpu_physical_memory_rw(addr, buf, l, 0);
910 fwrite(buf, 1, l, f);
911 fflush(f);
912 addr += l;
913 size -= l;
914 }
915 fclose(f);
916}
917
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300918static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000919{
920 uint32_t addr;
921 uint8_t buf[1];
922 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300923 uint32_t start = qdict_get_int(qdict, "start");
924 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000925
926 sum = 0;
927 for(addr = start; addr < (start + size); addr++) {
928 cpu_physical_memory_rw(addr, buf, 1, 0);
929 /* BSD sum algorithm ('sum' Unix command) */
930 sum = (sum >> 1) | (sum << 15);
931 sum += buf[0];
932 }
aliguori376253e2009-03-05 23:01:23 +0000933 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000934}
935
bellarda3a91a32004-06-04 11:06:21 +0000936typedef struct {
937 int keycode;
938 const char *name;
939} KeyDef;
940
941static const KeyDef key_defs[] = {
942 { 0x2a, "shift" },
943 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000944
bellarda3a91a32004-06-04 11:06:21 +0000945 { 0x38, "alt" },
946 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000947 { 0x64, "altgr" },
948 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000949 { 0x1d, "ctrl" },
950 { 0x9d, "ctrl_r" },
951
952 { 0xdd, "menu" },
953
954 { 0x01, "esc" },
955
956 { 0x02, "1" },
957 { 0x03, "2" },
958 { 0x04, "3" },
959 { 0x05, "4" },
960 { 0x06, "5" },
961 { 0x07, "6" },
962 { 0x08, "7" },
963 { 0x09, "8" },
964 { 0x0a, "9" },
965 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000966 { 0x0c, "minus" },
967 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000968 { 0x0e, "backspace" },
969
970 { 0x0f, "tab" },
971 { 0x10, "q" },
972 { 0x11, "w" },
973 { 0x12, "e" },
974 { 0x13, "r" },
975 { 0x14, "t" },
976 { 0x15, "y" },
977 { 0x16, "u" },
978 { 0x17, "i" },
979 { 0x18, "o" },
980 { 0x19, "p" },
981
982 { 0x1c, "ret" },
983
984 { 0x1e, "a" },
985 { 0x1f, "s" },
986 { 0x20, "d" },
987 { 0x21, "f" },
988 { 0x22, "g" },
989 { 0x23, "h" },
990 { 0x24, "j" },
991 { 0x25, "k" },
992 { 0x26, "l" },
993
994 { 0x2c, "z" },
995 { 0x2d, "x" },
996 { 0x2e, "c" },
997 { 0x2f, "v" },
998 { 0x30, "b" },
999 { 0x31, "n" },
1000 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001001 { 0x33, "comma" },
1002 { 0x34, "dot" },
1003 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001004
balrog4d3b6f62008-02-10 16:33:14 +00001005 { 0x37, "asterisk" },
1006
bellarda3a91a32004-06-04 11:06:21 +00001007 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001008 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001009 { 0x3b, "f1" },
1010 { 0x3c, "f2" },
1011 { 0x3d, "f3" },
1012 { 0x3e, "f4" },
1013 { 0x3f, "f5" },
1014 { 0x40, "f6" },
1015 { 0x41, "f7" },
1016 { 0x42, "f8" },
1017 { 0x43, "f9" },
1018 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001019 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001020 { 0x46, "scroll_lock" },
1021
bellard64866c32006-05-07 18:03:31 +00001022 { 0xb5, "kp_divide" },
1023 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001024 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001025 { 0x4e, "kp_add" },
1026 { 0x9c, "kp_enter" },
1027 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001028 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001029
1030 { 0x52, "kp_0" },
1031 { 0x4f, "kp_1" },
1032 { 0x50, "kp_2" },
1033 { 0x51, "kp_3" },
1034 { 0x4b, "kp_4" },
1035 { 0x4c, "kp_5" },
1036 { 0x4d, "kp_6" },
1037 { 0x47, "kp_7" },
1038 { 0x48, "kp_8" },
1039 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001040
bellarda3a91a32004-06-04 11:06:21 +00001041 { 0x56, "<" },
1042
1043 { 0x57, "f11" },
1044 { 0x58, "f12" },
1045
1046 { 0xb7, "print" },
1047
1048 { 0xc7, "home" },
1049 { 0xc9, "pgup" },
1050 { 0xd1, "pgdn" },
1051 { 0xcf, "end" },
1052
1053 { 0xcb, "left" },
1054 { 0xc8, "up" },
1055 { 0xd0, "down" },
1056 { 0xcd, "right" },
1057
1058 { 0xd2, "insert" },
1059 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001060#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1061 { 0xf0, "stop" },
1062 { 0xf1, "again" },
1063 { 0xf2, "props" },
1064 { 0xf3, "undo" },
1065 { 0xf4, "front" },
1066 { 0xf5, "copy" },
1067 { 0xf6, "open" },
1068 { 0xf7, "paste" },
1069 { 0xf8, "find" },
1070 { 0xf9, "cut" },
1071 { 0xfa, "lf" },
1072 { 0xfb, "help" },
1073 { 0xfc, "meta_l" },
1074 { 0xfd, "meta_r" },
1075 { 0xfe, "compose" },
1076#endif
bellarda3a91a32004-06-04 11:06:21 +00001077 { 0, NULL },
1078};
1079
1080static int get_keycode(const char *key)
1081{
1082 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001083 char *endp;
1084 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001085
1086 for(p = key_defs; p->name != NULL; p++) {
1087 if (!strcmp(key, p->name))
1088 return p->keycode;
1089 }
bellard64866c32006-05-07 18:03:31 +00001090 if (strstart(key, "0x", NULL)) {
1091 ret = strtoul(key, &endp, 0);
1092 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1093 return ret;
1094 }
bellarda3a91a32004-06-04 11:06:21 +00001095 return -1;
1096}
1097
balrogc8256f92008-06-08 22:45:01 +00001098#define MAX_KEYCODES 16
1099static uint8_t keycodes[MAX_KEYCODES];
1100static int nb_pending_keycodes;
1101static QEMUTimer *key_timer;
1102
1103static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001104{
balrogc8256f92008-06-08 22:45:01 +00001105 int keycode;
1106
1107 while (nb_pending_keycodes > 0) {
1108 nb_pending_keycodes--;
1109 keycode = keycodes[nb_pending_keycodes];
1110 if (keycode & 0x80)
1111 kbd_put_keycode(0xe0);
1112 kbd_put_keycode(keycode | 0x80);
1113 }
1114}
1115
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001116static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001117{
balrog3401c0d2008-06-04 10:05:59 +00001118 char keyname_buf[16];
1119 char *separator;
1120 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001121 const char *string = qdict_get_str(qdict, "string");
1122 int has_hold_time = qdict_haskey(qdict, "hold_time");
1123 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001124
balrogc8256f92008-06-08 22:45:01 +00001125 if (nb_pending_keycodes > 0) {
1126 qemu_del_timer(key_timer);
1127 release_keys(NULL);
1128 }
1129 if (!has_hold_time)
1130 hold_time = 100;
1131 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001132 while (1) {
1133 separator = strchr(string, '-');
1134 keyname_len = separator ? separator - string : strlen(string);
1135 if (keyname_len > 0) {
1136 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1137 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001138 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001139 return;
bellarda3a91a32004-06-04 11:06:21 +00001140 }
balrogc8256f92008-06-08 22:45:01 +00001141 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001142 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001143 return;
1144 }
1145 keyname_buf[keyname_len] = 0;
1146 keycode = get_keycode(keyname_buf);
1147 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001148 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001149 return;
1150 }
balrogc8256f92008-06-08 22:45:01 +00001151 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001152 }
balrog3401c0d2008-06-04 10:05:59 +00001153 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001154 break;
balrog3401c0d2008-06-04 10:05:59 +00001155 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001156 }
balrogc8256f92008-06-08 22:45:01 +00001157 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001158 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001159 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001160 keycode = keycodes[i];
1161 if (keycode & 0x80)
1162 kbd_put_keycode(0xe0);
1163 kbd_put_keycode(keycode & 0x7f);
1164 }
balrogc8256f92008-06-08 22:45:01 +00001165 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001166 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1167 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001168}
1169
bellard13224a82006-07-14 22:03:35 +00001170static int mouse_button_state;
1171
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001172static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001173{
1174 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001175 const char *dx_str = qdict_get_str(qdict, "dx_str");
1176 const char *dy_str = qdict_get_str(qdict, "dy_str");
1177 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001178 dx = strtol(dx_str, NULL, 0);
1179 dy = strtol(dy_str, NULL, 0);
1180 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001181 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001182 dz = strtol(dz_str, NULL, 0);
1183 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1184}
1185
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001186static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001187{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001188 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001189 mouse_button_state = button_state;
1190 kbd_mouse_event(0, 0, 0, mouse_button_state);
1191}
1192
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001193static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001194{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001195 int size = qdict_get_int(qdict, "size");
1196 int addr = qdict_get_int(qdict, "addr");
1197 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001198 uint32_t val;
1199 int suffix;
1200
1201 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001202 int index = qdict_get_int(qdict, "index");
Isaku Yamahatad56dd6c2009-07-02 19:32:07 +09001203 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001204 addr++;
1205 }
1206 addr &= 0xffff;
1207
1208 switch(size) {
1209 default:
1210 case 1:
1211 val = cpu_inb(NULL, addr);
1212 suffix = 'b';
1213 break;
1214 case 2:
1215 val = cpu_inw(NULL, addr);
1216 suffix = 'w';
1217 break;
1218 case 4:
1219 val = cpu_inl(NULL, addr);
1220 suffix = 'l';
1221 break;
1222 }
aliguori376253e2009-03-05 23:01:23 +00001223 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1224 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001225}
bellarda3a91a32004-06-04 11:06:21 +00001226
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001227static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001228{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001229 int size = qdict_get_int(qdict, "size");
1230 int addr = qdict_get_int(qdict, "addr");
1231 int val = qdict_get_int(qdict, "val");
1232
Jan Kiszkaf1147842009-07-14 10:20:11 +02001233 addr &= IOPORTS_MASK;
1234
1235 switch (size) {
1236 default:
1237 case 1:
1238 cpu_outb(NULL, addr, val);
1239 break;
1240 case 2:
1241 cpu_outw(NULL, addr, val);
1242 break;
1243 case 4:
1244 cpu_outl(NULL, addr, val);
1245 break;
1246 }
1247}
1248
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001249static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001250{
1251 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001252 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001253
Jan Kiszka76e30d02009-07-02 00:19:02 +02001254 res = qemu_boot_set(bootdevice);
1255 if (res == 0) {
1256 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1257 } else if (res > 0) {
1258 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001259 } else {
aliguori376253e2009-03-05 23:01:23 +00001260 monitor_printf(mon, "no function defined to set boot device list for "
1261 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001262 }
1263}
1264
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001265static void do_system_reset(Monitor *mon, const QDict *qdict)
bellarde4f90822004-06-20 12:35:44 +00001266{
1267 qemu_system_reset_request();
1268}
1269
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001270static void do_system_powerdown(Monitor *mon, const QDict *qdict)
bellard34751872005-07-02 14:31:34 +00001271{
1272 qemu_system_powerdown_request();
1273}
1274
bellardb86bda52004-09-18 19:32:46 +00001275#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001276static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001277{
aliguori376253e2009-03-05 23:01:23 +00001278 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1279 addr,
1280 pte & mask,
1281 pte & PG_GLOBAL_MASK ? 'G' : '-',
1282 pte & PG_PSE_MASK ? 'P' : '-',
1283 pte & PG_DIRTY_MASK ? 'D' : '-',
1284 pte & PG_ACCESSED_MASK ? 'A' : '-',
1285 pte & PG_PCD_MASK ? 'C' : '-',
1286 pte & PG_PWT_MASK ? 'T' : '-',
1287 pte & PG_USER_MASK ? 'U' : '-',
1288 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001289}
1290
aliguori376253e2009-03-05 23:01:23 +00001291static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001292{
bellard6a00d602005-11-21 23:25:50 +00001293 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001294 int l1, l2;
1295 uint32_t pgd, pde, pte;
1296
bellard6a00d602005-11-21 23:25:50 +00001297 env = mon_get_cpu();
1298 if (!env)
1299 return;
1300
bellardb86bda52004-09-18 19:32:46 +00001301 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001302 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001303 return;
1304 }
1305 pgd = env->cr[3] & ~0xfff;
1306 for(l1 = 0; l1 < 1024; l1++) {
1307 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1308 pde = le32_to_cpu(pde);
1309 if (pde & PG_PRESENT_MASK) {
1310 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001311 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001312 } else {
1313 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001314 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001315 (uint8_t *)&pte, 4);
1316 pte = le32_to_cpu(pte);
1317 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001318 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001319 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001320 ~0xfff);
1321 }
1322 }
1323 }
1324 }
1325 }
1326}
1327
aliguori376253e2009-03-05 23:01:23 +00001328static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001329 uint32_t end, int prot)
1330{
bellard9746b152004-11-11 18:30:24 +00001331 int prot1;
1332 prot1 = *plast_prot;
1333 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001334 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001335 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1336 *pstart, end, end - *pstart,
1337 prot1 & PG_USER_MASK ? 'u' : '-',
1338 'r',
1339 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001340 }
1341 if (prot != 0)
1342 *pstart = end;
1343 else
1344 *pstart = -1;
1345 *plast_prot = prot;
1346 }
1347}
1348
aliguori376253e2009-03-05 23:01:23 +00001349static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001350{
bellard6a00d602005-11-21 23:25:50 +00001351 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001352 int l1, l2, prot, last_prot;
1353 uint32_t pgd, pde, pte, start, end;
1354
bellard6a00d602005-11-21 23:25:50 +00001355 env = mon_get_cpu();
1356 if (!env)
1357 return;
1358
bellardb86bda52004-09-18 19:32:46 +00001359 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001360 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001361 return;
1362 }
1363 pgd = env->cr[3] & ~0xfff;
1364 last_prot = 0;
1365 start = -1;
1366 for(l1 = 0; l1 < 1024; l1++) {
1367 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1368 pde = le32_to_cpu(pde);
1369 end = l1 << 22;
1370 if (pde & PG_PRESENT_MASK) {
1371 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1372 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001373 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001374 } else {
1375 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001376 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001377 (uint8_t *)&pte, 4);
1378 pte = le32_to_cpu(pte);
1379 end = (l1 << 22) + (l2 << 12);
1380 if (pte & PG_PRESENT_MASK) {
1381 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1382 } else {
1383 prot = 0;
1384 }
aliguori376253e2009-03-05 23:01:23 +00001385 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001386 }
1387 }
1388 } else {
1389 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001390 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001391 }
1392 }
1393}
1394#endif
1395
aurel327c664e22009-03-03 06:12:22 +00001396#if defined(TARGET_SH4)
1397
aliguori376253e2009-03-05 23:01:23 +00001398static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001399{
aliguori376253e2009-03-05 23:01:23 +00001400 monitor_printf(mon, " tlb%i:\t"
1401 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1402 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1403 "dirty=%hhu writethrough=%hhu\n",
1404 idx,
1405 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1406 tlb->v, tlb->sh, tlb->c, tlb->pr,
1407 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001408}
1409
aliguori376253e2009-03-05 23:01:23 +00001410static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001411{
1412 CPUState *env = mon_get_cpu();
1413 int i;
1414
aliguori376253e2009-03-05 23:01:23 +00001415 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001416 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001417 print_tlb (mon, i, &env->itlb[i]);
1418 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001419 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001420 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001421}
1422
1423#endif
1424
aliguori376253e2009-03-05 23:01:23 +00001425static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001426{
1427#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001428 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001429 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001430 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001431 else
aliguori376253e2009-03-05 23:01:23 +00001432 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001433#else
aliguori376253e2009-03-05 23:01:23 +00001434 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001435#endif
1436}
1437
aliguori030ea372009-04-21 22:30:47 +00001438static void do_info_numa(Monitor *mon)
1439{
aliguorib28b6232009-04-22 20:20:29 +00001440 int i;
aliguori030ea372009-04-21 22:30:47 +00001441 CPUState *env;
1442
1443 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1444 for (i = 0; i < nb_numa_nodes; i++) {
1445 monitor_printf(mon, "node %d cpus:", i);
1446 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1447 if (env->numa_node == i) {
1448 monitor_printf(mon, " %d", env->cpu_index);
1449 }
1450 }
1451 monitor_printf(mon, "\n");
1452 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1453 node_mem[i] >> 20);
1454 }
1455}
1456
bellard5f1ce942006-02-08 22:40:15 +00001457#ifdef CONFIG_PROFILER
1458
aliguori376253e2009-03-05 23:01:23 +00001459static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001460{
1461 int64_t total;
1462 total = qemu_time;
1463 if (total == 0)
1464 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001465 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1466 dev_time, dev_time / (double)ticks_per_sec);
1467 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1468 qemu_time, qemu_time / (double)ticks_per_sec);
bellard5f1ce942006-02-08 22:40:15 +00001469 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001470 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001471}
1472#else
aliguori376253e2009-03-05 23:01:23 +00001473static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001474{
aliguori376253e2009-03-05 23:01:23 +00001475 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001476}
1477#endif
1478
bellardec36b692006-07-16 18:57:03 +00001479/* Capture support */
1480static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1481
aliguori376253e2009-03-05 23:01:23 +00001482static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001483{
1484 int i;
1485 CaptureState *s;
1486
1487 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001488 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001489 s->ops.info (s->opaque);
1490 }
1491}
1492
Blue Swirl23130862009-06-06 08:22:04 +00001493#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001494static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001495{
1496 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001497 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001498 CaptureState *s;
1499
1500 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1501 if (i == n) {
1502 s->ops.destroy (s->opaque);
1503 LIST_REMOVE (s, entries);
1504 qemu_free (s);
1505 return;
1506 }
1507 }
1508}
1509
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001510static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001511{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001512 const char *path = qdict_get_str(qdict, "path");
1513 int has_freq = qdict_haskey(qdict, "freq");
1514 int freq = qdict_get_try_int(qdict, "freq", -1);
1515 int has_bits = qdict_haskey(qdict, "bits");
1516 int bits = qdict_get_try_int(qdict, "bits", -1);
1517 int has_channels = qdict_haskey(qdict, "nchannels");
1518 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001519 CaptureState *s;
1520
1521 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001522
1523 freq = has_freq ? freq : 44100;
1524 bits = has_bits ? bits : 16;
1525 nchannels = has_channels ? nchannels : 2;
1526
1527 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001528 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001529 qemu_free (s);
1530 }
1531 LIST_INSERT_HEAD (&capture_head, s, entries);
1532}
1533#endif
1534
aurel32dc1c0b72008-04-27 23:52:12 +00001535#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001536static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001537{
1538 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001539 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001540
1541 for (env = first_cpu; env != NULL; env = env->next_cpu)
1542 if (env->cpu_index == cpu_index) {
1543 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1544 break;
1545 }
1546}
1547#endif
1548
aliguori376253e2009-03-05 23:01:23 +00001549static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001550{
aurel321b530a62009-04-05 20:08:59 +00001551 if (vm_running) {
1552 if (singlestep) {
1553 monitor_printf(mon, "VM status: running (single step mode)\n");
1554 } else {
1555 monitor_printf(mon, "VM status: running\n");
1556 }
1557 } else
aliguori376253e2009-03-05 23:01:23 +00001558 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001559}
1560
1561
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001562static void do_balloon(Monitor *mon, const QDict *qdict)
aliguoridf751fa2008-12-04 20:19:35 +00001563{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001564 int value = qdict_get_int(qdict, "value");
aliguoridf751fa2008-12-04 20:19:35 +00001565 ram_addr_t target = value;
1566 qemu_balloon(target << 20);
1567}
1568
aliguori376253e2009-03-05 23:01:23 +00001569static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001570{
1571 ram_addr_t actual;
1572
1573 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001574 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001575 monitor_printf(mon, "Using KVM without synchronous MMU, "
1576 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001577 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001578 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001579 else
aliguori376253e2009-03-05 23:01:23 +00001580 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001581}
1582
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001583static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001584{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001585 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001586
aliguori76655d62009-03-06 20:27:37 +00001587 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001588 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001589 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001590 return acl;
1591}
aliguori76655d62009-03-06 20:27:37 +00001592
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001593static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001594{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001595 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001596 qemu_acl *acl = find_acl(mon, aclname);
1597 qemu_acl_entry *entry;
1598 int i = 0;
1599
1600 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001601 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001602 acl->defaultDeny ? "deny" : "allow");
aliguori28a76be2009-03-06 20:27:40 +00001603 TAILQ_FOREACH(entry, &acl->entries, next) {
1604 i++;
1605 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001606 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001607 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001608 }
1609}
1610
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001611static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001612{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001613 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001614 qemu_acl *acl = find_acl(mon, aclname);
1615
1616 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001617 qemu_acl_reset(acl);
1618 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001619 }
1620}
aliguori76655d62009-03-06 20:27:37 +00001621
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001622static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001623{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001624 const char *aclname = qdict_get_str(qdict, "aclname");
1625 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001626 qemu_acl *acl = find_acl(mon, aclname);
1627
1628 if (acl) {
1629 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001630 acl->defaultDeny = 0;
1631 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001632 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001633 acl->defaultDeny = 1;
1634 monitor_printf(mon, "acl: policy set to 'deny'\n");
1635 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001636 monitor_printf(mon, "acl: unknown policy '%s', "
1637 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001638 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001639 }
1640}
aliguori76655d62009-03-06 20:27:37 +00001641
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001642static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001643{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001644 const char *aclname = qdict_get_str(qdict, "aclname");
1645 const char *match = qdict_get_str(qdict, "match");
1646 const char *policy = qdict_get_str(qdict, "policy");
1647 int has_index = qdict_haskey(qdict, "index");
1648 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001649 qemu_acl *acl = find_acl(mon, aclname);
1650 int deny, ret;
1651
1652 if (acl) {
1653 if (strcmp(policy, "allow") == 0) {
1654 deny = 0;
1655 } else if (strcmp(policy, "deny") == 0) {
1656 deny = 1;
1657 } else {
1658 monitor_printf(mon, "acl: unknown policy '%s', "
1659 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001660 return;
1661 }
aliguori28a76be2009-03-06 20:27:40 +00001662 if (has_index)
1663 ret = qemu_acl_insert(acl, deny, match, index);
1664 else
1665 ret = qemu_acl_append(acl, deny, match);
1666 if (ret < 0)
1667 monitor_printf(mon, "acl: unable to add acl entry\n");
1668 else
1669 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001670 }
1671}
aliguori76655d62009-03-06 20:27:37 +00001672
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001673static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001674{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001675 const char *aclname = qdict_get_str(qdict, "aclname");
1676 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001677 qemu_acl *acl = find_acl(mon, aclname);
1678 int ret;
aliguori76655d62009-03-06 20:27:37 +00001679
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001680 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001681 ret = qemu_acl_remove(acl, match);
1682 if (ret < 0)
1683 monitor_printf(mon, "acl: no matching acl entry\n");
1684 else
1685 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001686 }
1687}
1688
Huang Ying79c4f6b2009-06-23 10:05:14 +08001689#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001690static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001691{
1692 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001693 int cpu_index = qdict_get_int(qdict, "cpu_index");
1694 int bank = qdict_get_int(qdict, "bank");
1695 uint64_t status = qdict_get_int(qdict, "status");
1696 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1697 uint64_t addr = qdict_get_int(qdict, "addr");
1698 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08001699
1700 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1701 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1702 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1703 break;
1704 }
1705}
1706#endif
1707
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001708static void do_getfd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001709{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001710 const char *fdname = qdict_get_str(qdict, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001711 mon_fd_t *monfd;
1712 int fd;
1713
1714 fd = qemu_chr_get_msgfd(mon->chr);
1715 if (fd == -1) {
1716 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1717 return;
1718 }
1719
1720 if (qemu_isdigit(fdname[0])) {
1721 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1722 return;
1723 }
1724
1725 fd = dup(fd);
1726 if (fd == -1) {
1727 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1728 strerror(errno));
1729 return;
1730 }
1731
1732 LIST_FOREACH(monfd, &mon->fds, next) {
1733 if (strcmp(monfd->name, fdname) != 0) {
1734 continue;
1735 }
1736
1737 close(monfd->fd);
1738 monfd->fd = fd;
1739 return;
1740 }
1741
1742 monfd = qemu_mallocz(sizeof(mon_fd_t));
1743 monfd->name = qemu_strdup(fdname);
1744 monfd->fd = fd;
1745
1746 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1747}
1748
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001749static void do_closefd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001750{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001751 const char *fdname = qdict_get_str(qdict, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001752 mon_fd_t *monfd;
1753
1754 LIST_FOREACH(monfd, &mon->fds, next) {
1755 if (strcmp(monfd->name, fdname) != 0) {
1756 continue;
1757 }
1758
1759 LIST_REMOVE(monfd, next);
1760 close(monfd->fd);
1761 qemu_free(monfd->name);
1762 qemu_free(monfd);
1763 return;
1764 }
1765
1766 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1767 fdname);
1768}
1769
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001770static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001771{
1772 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001773 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001774
1775 vm_stop(0);
1776
Juan Quintela05f24012009-08-20 19:42:22 +02001777 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001778 vm_start();
1779}
1780
Mark McLoughlin7768e042009-07-22 09:11:41 +01001781int monitor_get_fd(Monitor *mon, const char *fdname)
1782{
1783 mon_fd_t *monfd;
1784
1785 LIST_FOREACH(monfd, &mon->fds, next) {
1786 int fd;
1787
1788 if (strcmp(monfd->name, fdname) != 0) {
1789 continue;
1790 }
1791
1792 fd = monfd->fd;
1793
1794 /* caller takes ownership of fd */
1795 LIST_REMOVE(monfd, next);
1796 qemu_free(monfd->name);
1797 qemu_free(monfd);
1798
1799 return fd;
1800 }
1801
1802 return -1;
1803}
1804
aliguori376253e2009-03-05 23:01:23 +00001805static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001806#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001807 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001808};
1809
Blue Swirl23130862009-06-06 08:22:04 +00001810/* Please update qemu-monitor.hx when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001811static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001812 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001813 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001814 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001815 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001816 { "chardev", "", qemu_chr_info,
1817 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001818 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001819 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001820 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001821 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001822 { "registers", "", do_info_registers,
1823 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001824 { "cpus", "", do_info_cpus,
1825 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001826 { "history", "", do_info_history,
1827 "", "show the command line history", },
bellard4a0fb712004-05-21 11:39:07 +00001828 { "irq", "", irq_info,
1829 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001830 { "pic", "", pic_info,
1831 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001832 { "pci", "", pci_info,
1833 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001834#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001835 { "tlb", "", tlb_info,
1836 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001837#endif
1838#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001839 { "mem", "", mem_info,
1840 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001841 { "hpet", "", do_info_hpet,
1842 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001843#endif
bellarde3db7222005-01-26 22:00:47 +00001844 { "jit", "", do_info_jit,
1845 "", "show dynamic compiler info", },
aliguori7ba1e612008-11-05 16:04:33 +00001846 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001847 "", "show KVM information", },
aliguori030ea372009-04-21 22:30:47 +00001848 { "numa", "", do_info_numa,
1849 "", "show NUMA information", },
bellarda594cfb2005-11-06 16:13:29 +00001850 { "usb", "", usb_info,
1851 "", "show guest USB devices", },
1852 { "usbhost", "", usb_host_info,
1853 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001854 { "profile", "", do_info_profile,
1855 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001856 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001857 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001858 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001859 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001860 { "status", "", do_info_status,
1861 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001862 { "pcmcia", "", pcmcia_info,
1863 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001864 { "mice", "", do_info_mice,
1865 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001866 { "vnc", "", do_info_vnc,
1867 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001868 { "name", "", do_info_name,
1869 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001870 { "uuid", "", do_info_uuid,
1871 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001872#if defined(TARGET_PPC)
1873 { "cpustats", "", do_info_cpu_stats,
1874 "", "show CPU statistics", },
1875#endif
blueswir131a60e22007-10-26 18:42:59 +00001876#if defined(CONFIG_SLIRP)
Jan Kiszka6dbe5532009-06-24 14:42:29 +02001877 { "usernet", "", do_info_usernet,
1878 "", "show user network stack connection states", },
blueswir131a60e22007-10-26 18:42:59 +00001879#endif
aliguori5bb79102008-10-13 03:12:02 +00001880 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001881 { "balloon", "", do_info_balloon,
1882 "", "show balloon information" },
Gerd Hoffmanncae49562009-06-05 15:53:17 +01001883 { "qtree", "", do_info_qtree,
1884 "", "show device tree" },
Gerd Hoffmannf6c64e02009-08-03 15:03:09 +02001885 { "qdm", "", do_info_qdm,
1886 "", "show qdev device model list" },
bellard9dc39cb2004-03-14 21:38:27 +00001887 { NULL, NULL, },
1888};
1889
bellard9307c4c2004-04-04 12:57:25 +00001890/*******************************************************************/
1891
1892static const char *pch;
1893static jmp_buf expr_env;
1894
bellard92a31b12005-02-10 22:00:52 +00001895#define MD_TLONG 0
1896#define MD_I32 1
1897
bellard9307c4c2004-04-04 12:57:25 +00001898typedef struct MonitorDef {
1899 const char *name;
1900 int offset;
blueswir18662d652008-10-02 18:32:44 +00001901 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001902 int type;
bellard9307c4c2004-04-04 12:57:25 +00001903} MonitorDef;
1904
bellard57206fd2004-04-25 18:54:52 +00001905#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001906static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001907{
bellard6a00d602005-11-21 23:25:50 +00001908 CPUState *env = mon_get_cpu();
1909 if (!env)
1910 return 0;
1911 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001912}
1913#endif
1914
bellarda541f292004-04-12 20:39:29 +00001915#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001916static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001917{
bellard6a00d602005-11-21 23:25:50 +00001918 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001919 unsigned int u;
1920 int i;
1921
bellard6a00d602005-11-21 23:25:50 +00001922 if (!env)
1923 return 0;
1924
bellarda541f292004-04-12 20:39:29 +00001925 u = 0;
1926 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00001927 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001928
1929 return u;
1930}
1931
blueswir18662d652008-10-02 18:32:44 +00001932static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001933{
bellard6a00d602005-11-21 23:25:50 +00001934 CPUState *env = mon_get_cpu();
1935 if (!env)
1936 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001937 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001938}
1939
blueswir18662d652008-10-02 18:32:44 +00001940static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001941{
bellard6a00d602005-11-21 23:25:50 +00001942 CPUState *env = mon_get_cpu();
1943 if (!env)
1944 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001945 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001946}
bellard9fddaa02004-05-21 12:59:32 +00001947
blueswir18662d652008-10-02 18:32:44 +00001948static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001949{
bellard6a00d602005-11-21 23:25:50 +00001950 CPUState *env = mon_get_cpu();
1951 if (!env)
1952 return 0;
1953 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001954}
1955
blueswir18662d652008-10-02 18:32:44 +00001956static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001957{
bellard6a00d602005-11-21 23:25:50 +00001958 CPUState *env = mon_get_cpu();
1959 if (!env)
1960 return 0;
1961 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001962}
1963
blueswir18662d652008-10-02 18:32:44 +00001964static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001965{
bellard6a00d602005-11-21 23:25:50 +00001966 CPUState *env = mon_get_cpu();
1967 if (!env)
1968 return 0;
1969 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001970}
bellarda541f292004-04-12 20:39:29 +00001971#endif
1972
bellarde95c8d52004-09-30 22:22:08 +00001973#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001974#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001975static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001976{
bellard6a00d602005-11-21 23:25:50 +00001977 CPUState *env = mon_get_cpu();
1978 if (!env)
1979 return 0;
1980 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001981}
bellard7b936c02005-10-30 17:05:13 +00001982#endif
bellarde95c8d52004-09-30 22:22:08 +00001983
blueswir18662d652008-10-02 18:32:44 +00001984static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001985{
bellard6a00d602005-11-21 23:25:50 +00001986 CPUState *env = mon_get_cpu();
1987 if (!env)
1988 return 0;
1989 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001990}
1991#endif
1992
blueswir18662d652008-10-02 18:32:44 +00001993static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001994#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001995
1996#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001997 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001998 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001999 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002000
bellard9307c4c2004-04-04 12:57:25 +00002001 { "eax", offsetof(CPUState, regs[0]) },
2002 { "ecx", offsetof(CPUState, regs[1]) },
2003 { "edx", offsetof(CPUState, regs[2]) },
2004 { "ebx", offsetof(CPUState, regs[3]) },
2005 { "esp|sp", offsetof(CPUState, regs[4]) },
2006 { "ebp|fp", offsetof(CPUState, regs[5]) },
2007 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002008 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002009#ifdef TARGET_X86_64
2010 { "r8", offsetof(CPUState, regs[8]) },
2011 { "r9", offsetof(CPUState, regs[9]) },
2012 { "r10", offsetof(CPUState, regs[10]) },
2013 { "r11", offsetof(CPUState, regs[11]) },
2014 { "r12", offsetof(CPUState, regs[12]) },
2015 { "r13", offsetof(CPUState, regs[13]) },
2016 { "r14", offsetof(CPUState, regs[14]) },
2017 { "r15", offsetof(CPUState, regs[15]) },
2018#endif
bellard9307c4c2004-04-04 12:57:25 +00002019 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002020 { "eip", offsetof(CPUState, eip) },
2021 SEG("cs", R_CS)
2022 SEG("ds", R_DS)
2023 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002024 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002025 SEG("fs", R_FS)
2026 SEG("gs", R_GS)
2027 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002028#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002029 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002030 { "r0", offsetof(CPUState, gpr[0]) },
2031 { "r1", offsetof(CPUState, gpr[1]) },
2032 { "r2", offsetof(CPUState, gpr[2]) },
2033 { "r3", offsetof(CPUState, gpr[3]) },
2034 { "r4", offsetof(CPUState, gpr[4]) },
2035 { "r5", offsetof(CPUState, gpr[5]) },
2036 { "r6", offsetof(CPUState, gpr[6]) },
2037 { "r7", offsetof(CPUState, gpr[7]) },
2038 { "r8", offsetof(CPUState, gpr[8]) },
2039 { "r9", offsetof(CPUState, gpr[9]) },
2040 { "r10", offsetof(CPUState, gpr[10]) },
2041 { "r11", offsetof(CPUState, gpr[11]) },
2042 { "r12", offsetof(CPUState, gpr[12]) },
2043 { "r13", offsetof(CPUState, gpr[13]) },
2044 { "r14", offsetof(CPUState, gpr[14]) },
2045 { "r15", offsetof(CPUState, gpr[15]) },
2046 { "r16", offsetof(CPUState, gpr[16]) },
2047 { "r17", offsetof(CPUState, gpr[17]) },
2048 { "r18", offsetof(CPUState, gpr[18]) },
2049 { "r19", offsetof(CPUState, gpr[19]) },
2050 { "r20", offsetof(CPUState, gpr[20]) },
2051 { "r21", offsetof(CPUState, gpr[21]) },
2052 { "r22", offsetof(CPUState, gpr[22]) },
2053 { "r23", offsetof(CPUState, gpr[23]) },
2054 { "r24", offsetof(CPUState, gpr[24]) },
2055 { "r25", offsetof(CPUState, gpr[25]) },
2056 { "r26", offsetof(CPUState, gpr[26]) },
2057 { "r27", offsetof(CPUState, gpr[27]) },
2058 { "r28", offsetof(CPUState, gpr[28]) },
2059 { "r29", offsetof(CPUState, gpr[29]) },
2060 { "r30", offsetof(CPUState, gpr[30]) },
2061 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002062 /* Floating point registers */
2063 { "f0", offsetof(CPUState, fpr[0]) },
2064 { "f1", offsetof(CPUState, fpr[1]) },
2065 { "f2", offsetof(CPUState, fpr[2]) },
2066 { "f3", offsetof(CPUState, fpr[3]) },
2067 { "f4", offsetof(CPUState, fpr[4]) },
2068 { "f5", offsetof(CPUState, fpr[5]) },
2069 { "f6", offsetof(CPUState, fpr[6]) },
2070 { "f7", offsetof(CPUState, fpr[7]) },
2071 { "f8", offsetof(CPUState, fpr[8]) },
2072 { "f9", offsetof(CPUState, fpr[9]) },
2073 { "f10", offsetof(CPUState, fpr[10]) },
2074 { "f11", offsetof(CPUState, fpr[11]) },
2075 { "f12", offsetof(CPUState, fpr[12]) },
2076 { "f13", offsetof(CPUState, fpr[13]) },
2077 { "f14", offsetof(CPUState, fpr[14]) },
2078 { "f15", offsetof(CPUState, fpr[15]) },
2079 { "f16", offsetof(CPUState, fpr[16]) },
2080 { "f17", offsetof(CPUState, fpr[17]) },
2081 { "f18", offsetof(CPUState, fpr[18]) },
2082 { "f19", offsetof(CPUState, fpr[19]) },
2083 { "f20", offsetof(CPUState, fpr[20]) },
2084 { "f21", offsetof(CPUState, fpr[21]) },
2085 { "f22", offsetof(CPUState, fpr[22]) },
2086 { "f23", offsetof(CPUState, fpr[23]) },
2087 { "f24", offsetof(CPUState, fpr[24]) },
2088 { "f25", offsetof(CPUState, fpr[25]) },
2089 { "f26", offsetof(CPUState, fpr[26]) },
2090 { "f27", offsetof(CPUState, fpr[27]) },
2091 { "f28", offsetof(CPUState, fpr[28]) },
2092 { "f29", offsetof(CPUState, fpr[29]) },
2093 { "f30", offsetof(CPUState, fpr[30]) },
2094 { "f31", offsetof(CPUState, fpr[31]) },
2095 { "fpscr", offsetof(CPUState, fpscr) },
2096 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002097 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002098 { "lr", offsetof(CPUState, lr) },
2099 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002100 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002101 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002102 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002103 { "msr", 0, &monitor_get_msr, },
2104 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002105 { "tbu", 0, &monitor_get_tbu, },
2106 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002107#if defined(TARGET_PPC64)
2108 /* Address space register */
2109 { "asr", offsetof(CPUState, asr) },
2110#endif
2111 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002112 { "sdr1", offsetof(CPUState, sdr1) },
2113 { "sr0", offsetof(CPUState, sr[0]) },
2114 { "sr1", offsetof(CPUState, sr[1]) },
2115 { "sr2", offsetof(CPUState, sr[2]) },
2116 { "sr3", offsetof(CPUState, sr[3]) },
2117 { "sr4", offsetof(CPUState, sr[4]) },
2118 { "sr5", offsetof(CPUState, sr[5]) },
2119 { "sr6", offsetof(CPUState, sr[6]) },
2120 { "sr7", offsetof(CPUState, sr[7]) },
2121 { "sr8", offsetof(CPUState, sr[8]) },
2122 { "sr9", offsetof(CPUState, sr[9]) },
2123 { "sr10", offsetof(CPUState, sr[10]) },
2124 { "sr11", offsetof(CPUState, sr[11]) },
2125 { "sr12", offsetof(CPUState, sr[12]) },
2126 { "sr13", offsetof(CPUState, sr[13]) },
2127 { "sr14", offsetof(CPUState, sr[14]) },
2128 { "sr15", offsetof(CPUState, sr[15]) },
2129 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002130#elif defined(TARGET_SPARC)
2131 { "g0", offsetof(CPUState, gregs[0]) },
2132 { "g1", offsetof(CPUState, gregs[1]) },
2133 { "g2", offsetof(CPUState, gregs[2]) },
2134 { "g3", offsetof(CPUState, gregs[3]) },
2135 { "g4", offsetof(CPUState, gregs[4]) },
2136 { "g5", offsetof(CPUState, gregs[5]) },
2137 { "g6", offsetof(CPUState, gregs[6]) },
2138 { "g7", offsetof(CPUState, gregs[7]) },
2139 { "o0", 0, monitor_get_reg },
2140 { "o1", 1, monitor_get_reg },
2141 { "o2", 2, monitor_get_reg },
2142 { "o3", 3, monitor_get_reg },
2143 { "o4", 4, monitor_get_reg },
2144 { "o5", 5, monitor_get_reg },
2145 { "o6", 6, monitor_get_reg },
2146 { "o7", 7, monitor_get_reg },
2147 { "l0", 8, monitor_get_reg },
2148 { "l1", 9, monitor_get_reg },
2149 { "l2", 10, monitor_get_reg },
2150 { "l3", 11, monitor_get_reg },
2151 { "l4", 12, monitor_get_reg },
2152 { "l5", 13, monitor_get_reg },
2153 { "l6", 14, monitor_get_reg },
2154 { "l7", 15, monitor_get_reg },
2155 { "i0", 16, monitor_get_reg },
2156 { "i1", 17, monitor_get_reg },
2157 { "i2", 18, monitor_get_reg },
2158 { "i3", 19, monitor_get_reg },
2159 { "i4", 20, monitor_get_reg },
2160 { "i5", 21, monitor_get_reg },
2161 { "i6", 22, monitor_get_reg },
2162 { "i7", 23, monitor_get_reg },
2163 { "pc", offsetof(CPUState, pc) },
2164 { "npc", offsetof(CPUState, npc) },
2165 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002166#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002167 { "psr", 0, &monitor_get_psr, },
2168 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002169#endif
bellarde95c8d52004-09-30 22:22:08 +00002170 { "tbr", offsetof(CPUState, tbr) },
2171 { "fsr", offsetof(CPUState, fsr) },
2172 { "f0", offsetof(CPUState, fpr[0]) },
2173 { "f1", offsetof(CPUState, fpr[1]) },
2174 { "f2", offsetof(CPUState, fpr[2]) },
2175 { "f3", offsetof(CPUState, fpr[3]) },
2176 { "f4", offsetof(CPUState, fpr[4]) },
2177 { "f5", offsetof(CPUState, fpr[5]) },
2178 { "f6", offsetof(CPUState, fpr[6]) },
2179 { "f7", offsetof(CPUState, fpr[7]) },
2180 { "f8", offsetof(CPUState, fpr[8]) },
2181 { "f9", offsetof(CPUState, fpr[9]) },
2182 { "f10", offsetof(CPUState, fpr[10]) },
2183 { "f11", offsetof(CPUState, fpr[11]) },
2184 { "f12", offsetof(CPUState, fpr[12]) },
2185 { "f13", offsetof(CPUState, fpr[13]) },
2186 { "f14", offsetof(CPUState, fpr[14]) },
2187 { "f15", offsetof(CPUState, fpr[15]) },
2188 { "f16", offsetof(CPUState, fpr[16]) },
2189 { "f17", offsetof(CPUState, fpr[17]) },
2190 { "f18", offsetof(CPUState, fpr[18]) },
2191 { "f19", offsetof(CPUState, fpr[19]) },
2192 { "f20", offsetof(CPUState, fpr[20]) },
2193 { "f21", offsetof(CPUState, fpr[21]) },
2194 { "f22", offsetof(CPUState, fpr[22]) },
2195 { "f23", offsetof(CPUState, fpr[23]) },
2196 { "f24", offsetof(CPUState, fpr[24]) },
2197 { "f25", offsetof(CPUState, fpr[25]) },
2198 { "f26", offsetof(CPUState, fpr[26]) },
2199 { "f27", offsetof(CPUState, fpr[27]) },
2200 { "f28", offsetof(CPUState, fpr[28]) },
2201 { "f29", offsetof(CPUState, fpr[29]) },
2202 { "f30", offsetof(CPUState, fpr[30]) },
2203 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002204#ifdef TARGET_SPARC64
2205 { "f32", offsetof(CPUState, fpr[32]) },
2206 { "f34", offsetof(CPUState, fpr[34]) },
2207 { "f36", offsetof(CPUState, fpr[36]) },
2208 { "f38", offsetof(CPUState, fpr[38]) },
2209 { "f40", offsetof(CPUState, fpr[40]) },
2210 { "f42", offsetof(CPUState, fpr[42]) },
2211 { "f44", offsetof(CPUState, fpr[44]) },
2212 { "f46", offsetof(CPUState, fpr[46]) },
2213 { "f48", offsetof(CPUState, fpr[48]) },
2214 { "f50", offsetof(CPUState, fpr[50]) },
2215 { "f52", offsetof(CPUState, fpr[52]) },
2216 { "f54", offsetof(CPUState, fpr[54]) },
2217 { "f56", offsetof(CPUState, fpr[56]) },
2218 { "f58", offsetof(CPUState, fpr[58]) },
2219 { "f60", offsetof(CPUState, fpr[60]) },
2220 { "f62", offsetof(CPUState, fpr[62]) },
2221 { "asi", offsetof(CPUState, asi) },
2222 { "pstate", offsetof(CPUState, pstate) },
2223 { "cansave", offsetof(CPUState, cansave) },
2224 { "canrestore", offsetof(CPUState, canrestore) },
2225 { "otherwin", offsetof(CPUState, otherwin) },
2226 { "wstate", offsetof(CPUState, wstate) },
2227 { "cleanwin", offsetof(CPUState, cleanwin) },
2228 { "fprs", offsetof(CPUState, fprs) },
2229#endif
bellard9307c4c2004-04-04 12:57:25 +00002230#endif
2231 { NULL },
2232};
2233
aliguori376253e2009-03-05 23:01:23 +00002234static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002235{
aliguori376253e2009-03-05 23:01:23 +00002236 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002237 longjmp(expr_env, 1);
2238}
2239
bellard6a00d602005-11-21 23:25:50 +00002240/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002241static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002242{
blueswir18662d652008-10-02 18:32:44 +00002243 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002244 void *ptr;
2245
bellard9307c4c2004-04-04 12:57:25 +00002246 for(md = monitor_defs; md->name != NULL; md++) {
2247 if (compare_cmd(name, md->name)) {
2248 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002249 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002250 } else {
bellard6a00d602005-11-21 23:25:50 +00002251 CPUState *env = mon_get_cpu();
2252 if (!env)
2253 return -2;
2254 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002255 switch(md->type) {
2256 case MD_I32:
2257 *pval = *(int32_t *)ptr;
2258 break;
2259 case MD_TLONG:
2260 *pval = *(target_long *)ptr;
2261 break;
2262 default:
2263 *pval = 0;
2264 break;
2265 }
bellard9307c4c2004-04-04 12:57:25 +00002266 }
2267 return 0;
2268 }
2269 }
2270 return -1;
2271}
2272
2273static void next(void)
2274{
Blue Swirl660f11b2009-07-31 21:16:51 +00002275 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002276 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002277 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002278 pch++;
2279 }
2280}
2281
aliguori376253e2009-03-05 23:01:23 +00002282static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002283
aliguori376253e2009-03-05 23:01:23 +00002284static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002285{
blueswir1c2efc952007-09-25 17:28:42 +00002286 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002287 char *p;
bellard6a00d602005-11-21 23:25:50 +00002288 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002289
2290 switch(*pch) {
2291 case '+':
2292 next();
aliguori376253e2009-03-05 23:01:23 +00002293 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002294 break;
2295 case '-':
2296 next();
aliguori376253e2009-03-05 23:01:23 +00002297 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002298 break;
2299 case '~':
2300 next();
aliguori376253e2009-03-05 23:01:23 +00002301 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002302 break;
2303 case '(':
2304 next();
aliguori376253e2009-03-05 23:01:23 +00002305 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002306 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002307 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002308 }
2309 next();
2310 break;
bellard81d09122004-07-14 17:21:37 +00002311 case '\'':
2312 pch++;
2313 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002314 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002315 n = *pch;
2316 pch++;
2317 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002318 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002319 next();
2320 break;
bellard9307c4c2004-04-04 12:57:25 +00002321 case '$':
2322 {
2323 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002324 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002325
bellard9307c4c2004-04-04 12:57:25 +00002326 pch++;
2327 q = buf;
2328 while ((*pch >= 'a' && *pch <= 'z') ||
2329 (*pch >= 'A' && *pch <= 'Z') ||
2330 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002331 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002332 if ((q - buf) < sizeof(buf) - 1)
2333 *q++ = *pch;
2334 pch++;
2335 }
blueswir1cd390082008-11-16 13:53:32 +00002336 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002337 pch++;
2338 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002339 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002340 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002341 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002342 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002343 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002344 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002345 }
2346 break;
2347 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002348 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002349 n = 0;
2350 break;
2351 default:
blueswir17743e582007-09-24 18:39:04 +00002352#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002353 n = strtoull(pch, &p, 0);
2354#else
bellard9307c4c2004-04-04 12:57:25 +00002355 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002356#endif
bellard9307c4c2004-04-04 12:57:25 +00002357 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002358 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002359 }
2360 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002361 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002362 pch++;
2363 break;
2364 }
2365 return n;
2366}
2367
2368
aliguori376253e2009-03-05 23:01:23 +00002369static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002370{
blueswir1c2efc952007-09-25 17:28:42 +00002371 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002372 int op;
ths3b46e622007-09-17 08:09:54 +00002373
aliguori376253e2009-03-05 23:01:23 +00002374 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002375 for(;;) {
2376 op = *pch;
2377 if (op != '*' && op != '/' && op != '%')
2378 break;
2379 next();
aliguori376253e2009-03-05 23:01:23 +00002380 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002381 switch(op) {
2382 default:
2383 case '*':
2384 val *= val2;
2385 break;
2386 case '/':
2387 case '%':
ths5fafdf22007-09-16 21:08:06 +00002388 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002389 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002390 if (op == '/')
2391 val /= val2;
2392 else
2393 val %= val2;
2394 break;
2395 }
2396 }
2397 return val;
2398}
2399
aliguori376253e2009-03-05 23:01:23 +00002400static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002401{
blueswir1c2efc952007-09-25 17:28:42 +00002402 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002403 int op;
bellard9307c4c2004-04-04 12:57:25 +00002404
aliguori376253e2009-03-05 23:01:23 +00002405 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002406 for(;;) {
2407 op = *pch;
2408 if (op != '&' && op != '|' && op != '^')
2409 break;
2410 next();
aliguori376253e2009-03-05 23:01:23 +00002411 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002412 switch(op) {
2413 default:
2414 case '&':
2415 val &= val2;
2416 break;
2417 case '|':
2418 val |= val2;
2419 break;
2420 case '^':
2421 val ^= val2;
2422 break;
2423 }
2424 }
2425 return val;
2426}
2427
aliguori376253e2009-03-05 23:01:23 +00002428static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002429{
blueswir1c2efc952007-09-25 17:28:42 +00002430 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002431 int op;
bellard9307c4c2004-04-04 12:57:25 +00002432
aliguori376253e2009-03-05 23:01:23 +00002433 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002434 for(;;) {
2435 op = *pch;
2436 if (op != '+' && op != '-')
2437 break;
2438 next();
aliguori376253e2009-03-05 23:01:23 +00002439 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002440 if (op == '+')
2441 val += val2;
2442 else
2443 val -= val2;
2444 }
2445 return val;
2446}
2447
aliguori376253e2009-03-05 23:01:23 +00002448static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002449{
2450 pch = *pp;
2451 if (setjmp(expr_env)) {
2452 *pp = pch;
2453 return -1;
2454 }
blueswir1cd390082008-11-16 13:53:32 +00002455 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002456 pch++;
aliguori376253e2009-03-05 23:01:23 +00002457 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002458 *pp = pch;
2459 return 0;
2460}
2461
2462static int get_str(char *buf, int buf_size, const char **pp)
2463{
2464 const char *p;
2465 char *q;
2466 int c;
2467
bellard81d09122004-07-14 17:21:37 +00002468 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002469 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002470 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002471 p++;
2472 if (*p == '\0') {
2473 fail:
bellard81d09122004-07-14 17:21:37 +00002474 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002475 *pp = p;
2476 return -1;
2477 }
bellard9307c4c2004-04-04 12:57:25 +00002478 if (*p == '\"') {
2479 p++;
2480 while (*p != '\0' && *p != '\"') {
2481 if (*p == '\\') {
2482 p++;
2483 c = *p++;
2484 switch(c) {
2485 case 'n':
2486 c = '\n';
2487 break;
2488 case 'r':
2489 c = '\r';
2490 break;
2491 case '\\':
2492 case '\'':
2493 case '\"':
2494 break;
2495 default:
2496 qemu_printf("unsupported escape code: '\\%c'\n", c);
2497 goto fail;
2498 }
2499 if ((q - buf) < buf_size - 1) {
2500 *q++ = c;
2501 }
2502 } else {
2503 if ((q - buf) < buf_size - 1) {
2504 *q++ = *p;
2505 }
2506 p++;
2507 }
2508 }
2509 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002510 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002511 goto fail;
2512 }
2513 p++;
2514 } else {
blueswir1cd390082008-11-16 13:53:32 +00002515 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002516 if ((q - buf) < buf_size - 1) {
2517 *q++ = *p;
2518 }
2519 p++;
2520 }
bellard9307c4c2004-04-04 12:57:25 +00002521 }
bellard81d09122004-07-14 17:21:37 +00002522 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002523 *pp = p;
2524 return 0;
2525}
2526
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002527/*
2528 * Store the command-name in cmdname, and return a pointer to
2529 * the remaining of the command string.
2530 */
2531static const char *get_command_name(const char *cmdline,
2532 char *cmdname, size_t nlen)
2533{
2534 size_t len;
2535 const char *p, *pstart;
2536
2537 p = cmdline;
2538 while (qemu_isspace(*p))
2539 p++;
2540 if (*p == '\0')
2541 return NULL;
2542 pstart = p;
2543 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2544 p++;
2545 len = p - pstart;
2546 if (len > nlen - 1)
2547 len = nlen - 1;
2548 memcpy(cmdname, pstart, len);
2549 cmdname[len] = '\0';
2550 return p;
2551}
2552
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002553/**
2554 * Read key of 'type' into 'key' and return the current
2555 * 'type' pointer.
2556 */
2557static char *key_get_info(const char *type, char **key)
2558{
2559 size_t len;
2560 char *p, *str;
2561
2562 if (*type == ',')
2563 type++;
2564
2565 p = strchr(type, ':');
2566 if (!p) {
2567 *key = NULL;
2568 return NULL;
2569 }
2570 len = p - type;
2571
2572 str = qemu_malloc(len + 1);
2573 memcpy(str, type, len);
2574 str[len] = '\0';
2575
2576 *key = str;
2577 return ++p;
2578}
2579
bellard9307c4c2004-04-04 12:57:25 +00002580static int default_fmt_format = 'x';
2581static int default_fmt_size = 4;
2582
2583#define MAX_ARGS 16
2584
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002585static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2586 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002587 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00002588{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002589 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03002590 int c;
aliguori376253e2009-03-05 23:01:23 +00002591 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002592 char cmdname[256];
2593 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002594 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00002595
2596#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002597 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002598#endif
ths3b46e622007-09-17 08:09:54 +00002599
bellard9307c4c2004-04-04 12:57:25 +00002600 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002601 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2602 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002603 return NULL;
ths3b46e622007-09-17 08:09:54 +00002604
bellard9307c4c2004-04-04 12:57:25 +00002605 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002606 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002607 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002608 break;
bellard9dc39cb2004-03-14 21:38:27 +00002609 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002610
2611 if (cmd->name == NULL) {
2612 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002613 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002614 }
bellard9307c4c2004-04-04 12:57:25 +00002615
bellard9307c4c2004-04-04 12:57:25 +00002616 /* parse the parameters */
2617 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002618 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002619 typestr = key_get_info(typestr, &key);
2620 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002621 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002622 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002623 typestr++;
2624 switch(c) {
2625 case 'F':
bellard81d09122004-07-14 17:21:37 +00002626 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002627 case 's':
2628 {
2629 int ret;
ths3b46e622007-09-17 08:09:54 +00002630
blueswir1cd390082008-11-16 13:53:32 +00002631 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002632 p++;
2633 if (*typestr == '?') {
2634 typestr++;
2635 if (*p == '\0') {
2636 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002637 break;
bellard9307c4c2004-04-04 12:57:25 +00002638 }
2639 }
2640 ret = get_str(buf, sizeof(buf), &p);
2641 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002642 switch(c) {
2643 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002644 monitor_printf(mon, "%s: filename expected\n",
2645 cmdname);
bellard81d09122004-07-14 17:21:37 +00002646 break;
2647 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002648 monitor_printf(mon, "%s: block device name expected\n",
2649 cmdname);
bellard81d09122004-07-14 17:21:37 +00002650 break;
2651 default:
aliguori376253e2009-03-05 23:01:23 +00002652 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002653 break;
2654 }
bellard9307c4c2004-04-04 12:57:25 +00002655 goto fail;
2656 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002657 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002658 }
2659 break;
2660 case '/':
2661 {
2662 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002663
blueswir1cd390082008-11-16 13:53:32 +00002664 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002665 p++;
2666 if (*p == '/') {
2667 /* format found */
2668 p++;
2669 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002670 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002671 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002672 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002673 count = count * 10 + (*p - '0');
2674 p++;
2675 }
2676 }
2677 size = -1;
2678 format = -1;
2679 for(;;) {
2680 switch(*p) {
2681 case 'o':
2682 case 'd':
2683 case 'u':
2684 case 'x':
2685 case 'i':
2686 case 'c':
2687 format = *p++;
2688 break;
2689 case 'b':
2690 size = 1;
2691 p++;
2692 break;
2693 case 'h':
2694 size = 2;
2695 p++;
2696 break;
2697 case 'w':
2698 size = 4;
2699 p++;
2700 break;
2701 case 'g':
2702 case 'L':
2703 size = 8;
2704 p++;
2705 break;
2706 default:
2707 goto next;
2708 }
2709 }
2710 next:
blueswir1cd390082008-11-16 13:53:32 +00002711 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002712 monitor_printf(mon, "invalid char in format: '%c'\n",
2713 *p);
bellard9307c4c2004-04-04 12:57:25 +00002714 goto fail;
2715 }
bellard9307c4c2004-04-04 12:57:25 +00002716 if (format < 0)
2717 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002718 if (format != 'i') {
2719 /* for 'i', not specifying a size gives -1 as size */
2720 if (size < 0)
2721 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002722 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002723 }
bellard9307c4c2004-04-04 12:57:25 +00002724 default_fmt_format = format;
2725 } else {
2726 count = 1;
2727 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002728 if (format != 'i') {
2729 size = default_fmt_size;
2730 } else {
2731 size = -1;
2732 }
bellard9307c4c2004-04-04 12:57:25 +00002733 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002734 qdict_put(qdict, "count", qint_from_int(count));
2735 qdict_put(qdict, "format", qint_from_int(format));
2736 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002737 }
2738 break;
2739 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002740 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002741 {
blueswir1c2efc952007-09-25 17:28:42 +00002742 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002743
blueswir1cd390082008-11-16 13:53:32 +00002744 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002745 p++;
bellard34405572004-06-08 00:55:58 +00002746 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002747 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002748 if (*p == '\0') {
2749 typestr++;
2750 break;
2751 }
bellard34405572004-06-08 00:55:58 +00002752 } else {
2753 if (*p == '.') {
2754 p++;
blueswir1cd390082008-11-16 13:53:32 +00002755 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002756 p++;
bellard34405572004-06-08 00:55:58 +00002757 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002758 typestr++;
2759 break;
bellard34405572004-06-08 00:55:58 +00002760 }
2761 }
bellard13224a82006-07-14 22:03:35 +00002762 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002763 }
aliguori376253e2009-03-05 23:01:23 +00002764 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002765 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002766 /* Check if 'i' is greater than 32-bit */
2767 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2768 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
2769 monitor_printf(mon, "integer is for 32-bit values\n");
2770 goto fail;
2771 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002772 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002773 }
2774 break;
2775 case '-':
2776 {
2777 int has_option;
2778 /* option */
ths3b46e622007-09-17 08:09:54 +00002779
bellard9307c4c2004-04-04 12:57:25 +00002780 c = *typestr++;
2781 if (c == '\0')
2782 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002783 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002784 p++;
2785 has_option = 0;
2786 if (*p == '-') {
2787 p++;
2788 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002789 monitor_printf(mon, "%s: unsupported option -%c\n",
2790 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002791 goto fail;
2792 }
2793 p++;
2794 has_option = 1;
2795 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002796 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00002797 }
2798 break;
2799 default:
2800 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002801 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002802 goto fail;
2803 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002804 qemu_free(key);
2805 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002806 }
2807 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002808 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002809 p++;
2810 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002811 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2812 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002813 goto fail;
2814 }
2815
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002816 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002817
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002818fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002819 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002820 return NULL;
2821}
2822
2823static void monitor_handle_command(Monitor *mon, const char *cmdline)
2824{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002825 QDict *qdict;
2826 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002827
2828 qdict = qdict_new();
2829
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03002830 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002831 if (cmd) {
2832 void (*handler)(Monitor *mon, const QDict *qdict);
2833
2834 qemu_errors_to_mon(mon);
2835
2836 handler = cmd->handler;
2837 handler(mon, qdict);
2838
2839 qemu_errors_to_previous();
2840 }
2841
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002842 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002843}
2844
bellard81d09122004-07-14 17:21:37 +00002845static void cmd_completion(const char *name, const char *list)
2846{
2847 const char *p, *pstart;
2848 char cmd[128];
2849 int len;
2850
2851 p = list;
2852 for(;;) {
2853 pstart = p;
2854 p = strchr(p, '|');
2855 if (!p)
2856 p = pstart + strlen(pstart);
2857 len = p - pstart;
2858 if (len > sizeof(cmd) - 2)
2859 len = sizeof(cmd) - 2;
2860 memcpy(cmd, pstart, len);
2861 cmd[len] = '\0';
2862 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002863 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002864 }
2865 if (*p == '\0')
2866 break;
2867 p++;
2868 }
2869}
2870
2871static void file_completion(const char *input)
2872{
2873 DIR *ffs;
2874 struct dirent *d;
2875 char path[1024];
2876 char file[1024], file_prefix[1024];
2877 int input_path_len;
2878 const char *p;
2879
ths5fafdf22007-09-16 21:08:06 +00002880 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002881 if (!p) {
2882 input_path_len = 0;
2883 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002884 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002885 } else {
2886 input_path_len = p - input + 1;
2887 memcpy(path, input, input_path_len);
2888 if (input_path_len > sizeof(path) - 1)
2889 input_path_len = sizeof(path) - 1;
2890 path[input_path_len] = '\0';
2891 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2892 }
2893#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002894 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2895 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002896#endif
2897 ffs = opendir(path);
2898 if (!ffs)
2899 return;
2900 for(;;) {
2901 struct stat sb;
2902 d = readdir(ffs);
2903 if (!d)
2904 break;
2905 if (strstart(d->d_name, file_prefix, NULL)) {
2906 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002907 if (input_path_len < sizeof(file))
2908 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2909 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002910 /* stat the file to find out if it's a directory.
2911 * In that case add a slash to speed up typing long paths
2912 */
2913 stat(file, &sb);
2914 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002915 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00002916 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002917 }
2918 }
2919 closedir(ffs);
2920}
2921
aliguori51de9762009-03-05 23:00:43 +00002922static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002923{
aliguori51de9762009-03-05 23:00:43 +00002924 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002925 const char *input = opaque;
2926
2927 if (input[0] == '\0' ||
2928 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00002929 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00002930 }
2931}
2932
2933/* NOTE: this parser is an approximate form of the real command parser */
2934static void parse_cmdline(const char *cmdline,
2935 int *pnb_args, char **args)
2936{
2937 const char *p;
2938 int nb_args, ret;
2939 char buf[1024];
2940
2941 p = cmdline;
2942 nb_args = 0;
2943 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002944 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002945 p++;
2946 if (*p == '\0')
2947 break;
2948 if (nb_args >= MAX_ARGS)
2949 break;
2950 ret = get_str(buf, sizeof(buf), &p);
2951 args[nb_args] = qemu_strdup(buf);
2952 nb_args++;
2953 if (ret < 0)
2954 break;
2955 }
2956 *pnb_args = nb_args;
2957}
2958
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002959static const char *next_arg_type(const char *typestr)
2960{
2961 const char *p = strchr(typestr, ':');
2962 return (p != NULL ? ++p : typestr);
2963}
2964
aliguori4c36ba32009-03-05 23:01:37 +00002965static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002966{
2967 const char *cmdname;
2968 char *args[MAX_ARGS];
2969 int nb_args, i, len;
2970 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00002971 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002972 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002973
2974 parse_cmdline(cmdline, &nb_args, args);
2975#ifdef DEBUG_COMPLETION
2976 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00002977 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00002978 }
2979#endif
2980
2981 /* if the line ends with a space, it means we want to complete the
2982 next arg */
2983 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002984 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002985 if (nb_args >= MAX_ARGS)
2986 return;
2987 args[nb_args++] = qemu_strdup("");
2988 }
2989 if (nb_args <= 1) {
2990 /* command completion */
2991 if (nb_args == 0)
2992 cmdname = "";
2993 else
2994 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00002995 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00002996 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002997 cmd_completion(cmdname, cmd->name);
2998 }
2999 } else {
3000 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003001 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003002 if (compare_cmd(args[0], cmd->name))
3003 goto found;
3004 }
3005 return;
3006 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003007 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003008 for(i = 0; i < nb_args - 2; i++) {
3009 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003010 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003011 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003012 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003013 }
3014 }
3015 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003016 if (*ptype == '-' && ptype[1] != '\0') {
3017 ptype += 2;
3018 }
bellard81d09122004-07-14 17:21:37 +00003019 switch(*ptype) {
3020 case 'F':
3021 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003022 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003023 file_completion(str);
3024 break;
3025 case 'B':
3026 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003027 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003028 bdrv_iterate(block_completion_it, (void *)str);
3029 break;
bellard7fe48482004-10-09 18:08:01 +00003030 case 's':
3031 /* XXX: more generic ? */
3032 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003033 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003034 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3035 cmd_completion(str, cmd->name);
3036 }
bellard64866c32006-05-07 18:03:31 +00003037 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003038 char *sep = strrchr(str, '-');
3039 if (sep)
3040 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003041 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003042 for(key = key_defs; key->name != NULL; key++) {
3043 cmd_completion(str, key->name);
3044 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003045 } else if (!strcmp(cmd->name, "help|?")) {
3046 readline_set_completion_index(cur_mon->rs, strlen(str));
3047 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3048 cmd_completion(str, cmd->name);
3049 }
bellard7fe48482004-10-09 18:08:01 +00003050 }
3051 break;
bellard81d09122004-07-14 17:21:37 +00003052 default:
3053 break;
3054 }
3055 }
3056 for(i = 0; i < nb_args; i++)
3057 qemu_free(args[i]);
3058}
3059
aliguori731b0362009-03-05 23:01:42 +00003060static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003061{
aliguori731b0362009-03-05 23:01:42 +00003062 Monitor *mon = opaque;
3063
3064 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003065}
3066
aliguori731b0362009-03-05 23:01:42 +00003067static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003068{
aliguori731b0362009-03-05 23:01:42 +00003069 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003070 int i;
aliguori376253e2009-03-05 23:01:23 +00003071
aliguori731b0362009-03-05 23:01:42 +00003072 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003073
aliguoricde76ee2009-03-05 23:01:51 +00003074 if (cur_mon->rs) {
3075 for (i = 0; i < size; i++)
3076 readline_handle_byte(cur_mon->rs, buf[i]);
3077 } else {
3078 if (size == 0 || buf[size - 1] != 0)
3079 monitor_printf(cur_mon, "corrupted command\n");
3080 else
3081 monitor_handle_command(cur_mon, (char *)buf);
3082 }
aliguori731b0362009-03-05 23:01:42 +00003083
3084 cur_mon = old_mon;
3085}
aliguorid8f44602008-10-06 13:52:44 +00003086
aliguori376253e2009-03-05 23:01:23 +00003087static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003088{
aliguori731b0362009-03-05 23:01:42 +00003089 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003090 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003091 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003092}
3093
aliguoricde76ee2009-03-05 23:01:51 +00003094int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003095{
aliguoricde76ee2009-03-05 23:01:51 +00003096 if (!mon->rs)
3097 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003098 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003099 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003100}
3101
aliguori376253e2009-03-05 23:01:23 +00003102void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003103{
aliguoricde76ee2009-03-05 23:01:51 +00003104 if (!mon->rs)
3105 return;
aliguori731b0362009-03-05 23:01:42 +00003106 if (--mon->suspend_cnt == 0)
3107 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003108}
3109
aliguori731b0362009-03-05 23:01:42 +00003110static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003111{
aliguori376253e2009-03-05 23:01:23 +00003112 Monitor *mon = opaque;
3113
aliguori2724b182009-03-05 23:01:47 +00003114 switch (event) {
3115 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003116 mon->mux_out = 0;
3117 if (mon->reset_seen) {
3118 readline_restart(mon->rs);
3119 monitor_resume(mon);
3120 monitor_flush(mon);
3121 } else {
3122 mon->suspend_cnt = 0;
3123 }
aliguori2724b182009-03-05 23:01:47 +00003124 break;
ths86e94de2007-01-05 22:01:59 +00003125
aliguori2724b182009-03-05 23:01:47 +00003126 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003127 if (mon->reset_seen) {
3128 if (mon->suspend_cnt == 0) {
3129 monitor_printf(mon, "\n");
3130 }
3131 monitor_flush(mon);
3132 monitor_suspend(mon);
3133 } else {
3134 mon->suspend_cnt++;
3135 }
3136 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00003137 break;
3138
3139 case CHR_EVENT_RESET:
3140 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3141 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003142 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00003143 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003144 }
3145 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00003146 break;
3147 }
ths86e94de2007-01-05 22:01:59 +00003148}
3149
aliguori76655d62009-03-06 20:27:37 +00003150
3151/*
3152 * Local variables:
3153 * c-indent-level: 4
3154 * c-basic-offset: 4
3155 * tab-width: 8
3156 * End:
3157 */
3158
aliguori731b0362009-03-05 23:01:42 +00003159void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003160{
aliguori731b0362009-03-05 23:01:42 +00003161 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003162 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003163
3164 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003165 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003166 is_first_init = 0;
3167 }
aliguori87127162009-03-05 23:01:29 +00003168
3169 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003170
aliguori87127162009-03-05 23:01:29 +00003171 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003172 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00003173 if (flags & MONITOR_USE_READLINE) {
3174 mon->rs = readline_init(mon, monitor_find_completion);
3175 monitor_read_command(mon, 0);
3176 }
aliguori87127162009-03-05 23:01:29 +00003177
aliguori731b0362009-03-05 23:01:42 +00003178 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3179 mon);
aliguori87127162009-03-05 23:01:29 +00003180
3181 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003182 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003183 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003184}
3185
aliguori376253e2009-03-05 23:01:23 +00003186static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003187{
aliguoribb5fc202009-03-05 23:01:15 +00003188 BlockDriverState *bs = opaque;
3189 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003190
aliguoribb5fc202009-03-05 23:01:15 +00003191 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003192 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003193 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003194 }
aliguori731b0362009-03-05 23:01:42 +00003195 if (mon->password_completion_cb)
3196 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003197
aliguori731b0362009-03-05 23:01:42 +00003198 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003199}
aliguoric0f4ce72009-03-05 23:01:01 +00003200
aliguori376253e2009-03-05 23:01:23 +00003201void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003202 BlockDriverCompletionFunc *completion_cb,
3203 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003204{
aliguoricde76ee2009-03-05 23:01:51 +00003205 int err;
3206
aliguoribb5fc202009-03-05 23:01:15 +00003207 if (!bdrv_key_required(bs)) {
3208 if (completion_cb)
3209 completion_cb(opaque, 0);
3210 return;
3211 }
aliguoric0f4ce72009-03-05 23:01:01 +00003212
aliguori376253e2009-03-05 23:01:23 +00003213 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3214 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003215
aliguori731b0362009-03-05 23:01:42 +00003216 mon->password_completion_cb = completion_cb;
3217 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003218
aliguoricde76ee2009-03-05 23:01:51 +00003219 err = monitor_read_password(mon, bdrv_password_cb, bs);
3220
3221 if (err && completion_cb)
3222 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003223}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003224
3225typedef struct QemuErrorSink QemuErrorSink;
3226struct QemuErrorSink {
3227 enum {
3228 ERR_SINK_FILE,
3229 ERR_SINK_MONITOR,
3230 } dest;
3231 union {
3232 FILE *fp;
3233 Monitor *mon;
3234 };
3235 QemuErrorSink *previous;
3236};
3237
Blue Swirl528e93a2009-08-31 15:14:40 +00003238static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003239
3240void qemu_errors_to_file(FILE *fp)
3241{
3242 QemuErrorSink *sink;
3243
3244 sink = qemu_mallocz(sizeof(*sink));
3245 sink->dest = ERR_SINK_FILE;
3246 sink->fp = fp;
3247 sink->previous = qemu_error_sink;
3248 qemu_error_sink = sink;
3249}
3250
3251void qemu_errors_to_mon(Monitor *mon)
3252{
3253 QemuErrorSink *sink;
3254
3255 sink = qemu_mallocz(sizeof(*sink));
3256 sink->dest = ERR_SINK_MONITOR;
3257 sink->mon = mon;
3258 sink->previous = qemu_error_sink;
3259 qemu_error_sink = sink;
3260}
3261
3262void qemu_errors_to_previous(void)
3263{
3264 QemuErrorSink *sink;
3265
3266 assert(qemu_error_sink != NULL);
3267 sink = qemu_error_sink;
3268 qemu_error_sink = sink->previous;
3269 qemu_free(sink);
3270}
3271
3272void qemu_error(const char *fmt, ...)
3273{
3274 va_list args;
3275
3276 assert(qemu_error_sink != NULL);
3277 switch (qemu_error_sink->dest) {
3278 case ERR_SINK_FILE:
3279 va_start(args, fmt);
3280 vfprintf(qemu_error_sink->fp, fmt, args);
3281 va_end(args);
3282 break;
3283 case ERR_SINK_MONITOR:
3284 va_start(args, fmt);
3285 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3286 va_end(args);
3287 break;
3288 }
3289}