blob: f5f4d0e084c722e385f2bff2a388446d0c81973c [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;
aliguori731b0362009-03-05 23:01:42 +000088 int flags;
89 int suspend_cnt;
90 uint8_t outbuf[1024];
91 int outbuf_index;
92 ReadLineState *rs;
93 CPUState *mon_cpu;
94 BlockDriverCompletionFunc *password_completion_cb;
95 void *password_opaque;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010096 LIST_HEAD(,mon_fd_t) fds;
aliguori87127162009-03-05 23:01:29 +000097 LIST_ENTRY(Monitor) entry;
98};
99
100static LIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000101
aliguori376253e2009-03-05 23:01:23 +0000102static const mon_cmd_t mon_cmds[];
103static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000104
aliguori87127162009-03-05 23:01:29 +0000105Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000106
aliguori731b0362009-03-05 23:01:42 +0000107static void monitor_command_cb(Monitor *mon, const char *cmdline,
108 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000109
aliguori731b0362009-03-05 23:01:42 +0000110static void monitor_read_command(Monitor *mon, int show_prompt)
111{
112 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
113 if (show_prompt)
114 readline_show_prompt(mon->rs);
115}
bellard6a00d602005-11-21 23:25:50 +0000116
aliguoricde76ee2009-03-05 23:01:51 +0000117static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
118 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000119{
aliguoricde76ee2009-03-05 23:01:51 +0000120 if (mon->rs) {
121 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
122 /* prompt is printed on return from the command handler */
123 return 0;
124 } else {
125 monitor_printf(mon, "terminal does not support password prompting\n");
126 return -ENOTTY;
127 }
aliguoribb5fc202009-03-05 23:01:15 +0000128}
129
aliguori376253e2009-03-05 23:01:23 +0000130void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000131{
aliguori731b0362009-03-05 23:01:42 +0000132 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
133 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
134 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000135 }
136}
137
138/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000139static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000140{
ths60fe76f2007-12-16 03:02:09 +0000141 char c;
aliguori731b0362009-03-05 23:01:42 +0000142
143 if (!mon)
144 return;
145
bellard7e2515e2004-08-01 21:52:19 +0000146 for(;;) {
147 c = *str++;
148 if (c == '\0')
149 break;
bellard7ba12602006-07-14 20:26:42 +0000150 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000151 mon->outbuf[mon->outbuf_index++] = '\r';
152 mon->outbuf[mon->outbuf_index++] = c;
153 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
154 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000155 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000156 }
157}
158
aliguori376253e2009-03-05 23:01:23 +0000159void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000160{
161 char buf[4096];
162 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000163 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000164}
165
aliguori376253e2009-03-05 23:01:23 +0000166void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000167{
168 va_list ap;
169 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000170 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000171 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000172}
173
aliguori376253e2009-03-05 23:01:23 +0000174void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000175{
176 int i;
177
178 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000179 switch (filename[i]) {
180 case ' ':
181 case '"':
182 case '\\':
183 monitor_printf(mon, "\\%c", filename[i]);
184 break;
185 case '\t':
186 monitor_printf(mon, "\\t");
187 break;
188 case '\r':
189 monitor_printf(mon, "\\r");
190 break;
191 case '\n':
192 monitor_printf(mon, "\\n");
193 break;
194 default:
195 monitor_printf(mon, "%c", filename[i]);
196 break;
197 }
thsfef30742006-12-22 14:11:32 +0000198 }
199}
200
bellard7fe48482004-10-09 18:08:01 +0000201static int monitor_fprintf(FILE *stream, const char *fmt, ...)
202{
203 va_list ap;
204 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000205 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000206 va_end(ap);
207 return 0;
208}
209
bellard9dc39cb2004-03-14 21:38:27 +0000210static int compare_cmd(const char *name, const char *list)
211{
212 const char *p, *pstart;
213 int len;
214 len = strlen(name);
215 p = list;
216 for(;;) {
217 pstart = p;
218 p = strchr(p, '|');
219 if (!p)
220 p = pstart + strlen(pstart);
221 if ((p - pstart) == len && !memcmp(pstart, name, len))
222 return 1;
223 if (*p == '\0')
224 break;
225 p++;
226 }
227 return 0;
228}
229
aliguori376253e2009-03-05 23:01:23 +0000230static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
231 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000232{
aliguori376253e2009-03-05 23:01:23 +0000233 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000234
235 for(cmd = cmds; cmd->name != NULL; cmd++) {
236 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000237 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
238 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000239 }
240}
241
aliguori376253e2009-03-05 23:01:23 +0000242static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000243{
244 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000245 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000246 } else {
aliguori376253e2009-03-05 23:01:23 +0000247 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000248 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000249 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000250 monitor_printf(mon, "Log items (comma separated):\n");
251 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000252 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000253 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000254 }
255 }
bellard9dc39cb2004-03-14 21:38:27 +0000256 }
257}
258
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300259static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300260{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300261 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300262}
263
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300264static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000265{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200266 int all_devices;
267 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300268 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000269
bellard7954c732006-08-01 15:52:40 +0000270 all_devices = !strcmp(device, "all");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200271 TAILQ_FOREACH(dinfo, &drives, next) {
272 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300273 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200274 continue;
275 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000276 }
277}
278
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300279static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000280{
aliguori376253e2009-03-05 23:01:23 +0000281 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300282 const char *item = qdict_get_try_str(qdict, "item");
aliguori376253e2009-03-05 23:01:23 +0000283 void (*handler)(Monitor *);
bellard9dc39cb2004-03-14 21:38:27 +0000284
bellard9307c4c2004-04-04 12:57:25 +0000285 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000286 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000287 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000288 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000289 goto found;
290 }
291 help:
aliguori376253e2009-03-05 23:01:23 +0000292 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000293 return;
294 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000295 handler = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +0000296 handler(mon);
bellard9dc39cb2004-03-14 21:38:27 +0000297}
298
aliguori376253e2009-03-05 23:01:23 +0000299static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000300{
pbrook4a19f1e2009-04-07 23:17:49 +0000301 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000302}
303
aliguori376253e2009-03-05 23:01:23 +0000304static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000305{
306 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000307 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000308}
309
aurel32bf4f74c2008-12-18 22:42:34 +0000310#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000311static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000312{
aliguori376253e2009-03-05 23:01:23 +0000313 monitor_printf(mon, "HPET is %s by QEMU\n",
314 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000315}
aurel32bf4f74c2008-12-18 22:42:34 +0000316#endif
aliguori16b29ae2008-12-17 23:28:44 +0000317
aliguori376253e2009-03-05 23:01:23 +0000318static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000319{
aliguori376253e2009-03-05 23:01:23 +0000320 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
321 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
322 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
323 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
324 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000325}
326
bellard6a00d602005-11-21 23:25:50 +0000327/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000328static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000329{
330 CPUState *env;
331
332 for(env = first_cpu; env != NULL; env = env->next_cpu) {
333 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000334 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000335 return 0;
336 }
337 }
338 return -1;
339}
340
pbrook9596ebb2007-11-18 01:44:38 +0000341static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000342{
aliguori731b0362009-03-05 23:01:42 +0000343 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000344 mon_set_cpu(0);
345 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300346 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000347 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000348}
349
aliguori376253e2009-03-05 23:01:23 +0000350static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000351{
bellard6a00d602005-11-21 23:25:50 +0000352 CPUState *env;
353 env = mon_get_cpu();
354 if (!env)
355 return;
bellard9307c4c2004-04-04 12:57:25 +0000356#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000357 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000358 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000359#else
aliguori376253e2009-03-05 23:01:23 +0000360 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000361 0);
bellard9307c4c2004-04-04 12:57:25 +0000362#endif
363}
364
aliguori376253e2009-03-05 23:01:23 +0000365static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000366{
367 CPUState *env;
368
369 /* just to set the default cpu if not already done */
370 mon_get_cpu();
371
372 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Avi Kivity4c0960c2009-08-17 23:19:53 +0300373 cpu_synchronize_state(env);
aliguori376253e2009-03-05 23:01:23 +0000374 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000375 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000376 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000377#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000378 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
379 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000380#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000381 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000382#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000383 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
384 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000385#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000386 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000387#endif
thsead93602007-09-06 00:18:15 +0000388 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000389 monitor_printf(mon, " (halted)");
390 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000391 }
392}
393
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300394static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000395{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300396 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000397 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000398 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000399}
400
aliguori376253e2009-03-05 23:01:23 +0000401static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000402{
aliguori376253e2009-03-05 23:01:23 +0000403 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000404}
405
aliguori376253e2009-03-05 23:01:23 +0000406static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000407{
408 int i;
bellard7e2515e2004-08-01 21:52:19 +0000409 const char *str;
ths3b46e622007-09-17 08:09:54 +0000410
aliguoricde76ee2009-03-05 23:01:51 +0000411 if (!mon->rs)
412 return;
bellard7e2515e2004-08-01 21:52:19 +0000413 i = 0;
414 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000415 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000416 if (!str)
417 break;
aliguori376253e2009-03-05 23:01:23 +0000418 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000419 i++;
bellardaa455482004-04-04 13:07:25 +0000420 }
421}
422
j_mayer76a66252007-03-07 08:32:30 +0000423#if defined(TARGET_PPC)
424/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000425static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000426{
427 CPUState *env;
428
429 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000430 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000431}
432#endif
433
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300434static void do_quit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000435{
436 exit(0);
437}
438
aliguori376253e2009-03-05 23:01:23 +0000439static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000440{
441 if (bdrv_is_inserted(bs)) {
442 if (!force) {
443 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000444 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000445 return -1;
446 }
447 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000448 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000449 return -1;
450 }
451 }
452 bdrv_close(bs);
453 }
454 return 0;
455}
456
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300457static void do_eject(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000458{
459 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300460 int force = qdict_get_int(qdict, "force");
461 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000462
bellard9307c4c2004-04-04 12:57:25 +0000463 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000464 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000465 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000466 return;
467 }
aliguori376253e2009-03-05 23:01:23 +0000468 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000469}
470
aliguori376253e2009-03-05 23:01:23 +0000471static void do_change_block(Monitor *mon, const char *device,
472 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000473{
474 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000475 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000476
bellard9307c4c2004-04-04 12:57:25 +0000477 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000478 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000479 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000480 return;
481 }
aurel322ecea9b2008-06-18 22:10:01 +0000482 if (fmt) {
483 drv = bdrv_find_format(fmt);
484 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000485 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000486 return;
487 }
488 }
aliguori376253e2009-03-05 23:01:23 +0000489 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000490 return;
aurel322ecea9b2008-06-18 22:10:01 +0000491 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000492 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000493}
494
aliguori376253e2009-03-05 23:01:23 +0000495static void change_vnc_password_cb(Monitor *mon, const char *password,
496 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000497{
498 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000499 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000500
aliguori731b0362009-03-05 23:01:42 +0000501 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000502}
503
aliguori376253e2009-03-05 23:01:23 +0000504static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000505{
ths70848512007-08-25 01:37:05 +0000506 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000507 strcmp(target, "password") == 0) {
508 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000509 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000510 strncpy(password, arg, sizeof(password));
511 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000512 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000513 } else {
aliguori376253e2009-03-05 23:01:23 +0000514 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000515 }
ths70848512007-08-25 01:37:05 +0000516 } else {
aliguori28a76be2009-03-06 20:27:40 +0000517 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000518 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000519 }
thse25a5822007-08-25 01:36:20 +0000520}
521
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300522static void do_change(Monitor *mon, const QDict *qdict)
thse25a5822007-08-25 01:36:20 +0000523{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300524 const char *device = qdict_get_str(qdict, "device");
525 const char *target = qdict_get_str(qdict, "target");
526 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000527 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000528 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000529 } else {
aliguori28a76be2009-03-06 20:27:40 +0000530 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000531 }
532}
533
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300534static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000535{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300536 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000537}
538
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300539static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000540{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300541 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000542}
543
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300544static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000545{
546 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300547 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000548
bellard9307c4c2004-04-04 12:57:25 +0000549 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000550 mask = 0;
551 } else {
bellard9307c4c2004-04-04 12:57:25 +0000552 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000553 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000554 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000555 return;
556 }
557 }
558 cpu_set_log(mask);
559}
560
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300561static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000562{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300563 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000564 if (!option || !strcmp(option, "on")) {
565 singlestep = 1;
566 } else if (!strcmp(option, "off")) {
567 singlestep = 0;
568 } else {
569 monitor_printf(mon, "unexpected option %s\n", option);
570 }
571}
572
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300573static void do_stop(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000574{
575 vm_stop(EXCP_INTERRUPT);
576}
577
aliguoribb5fc202009-03-05 23:01:15 +0000578static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000579
aliguori376253e2009-03-05 23:01:23 +0000580struct bdrv_iterate_context {
581 Monitor *mon;
582 int err;
583};
aliguoric0f4ce72009-03-05 23:01:01 +0000584
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300585static void do_cont(Monitor *mon, const QDict *qdict)
aliguori376253e2009-03-05 23:01:23 +0000586{
587 struct bdrv_iterate_context context = { mon, 0 };
588
589 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000590 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000591 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000592 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000593}
594
aliguoribb5fc202009-03-05 23:01:15 +0000595static void bdrv_key_cb(void *opaque, int err)
596{
aliguori376253e2009-03-05 23:01:23 +0000597 Monitor *mon = opaque;
598
aliguoribb5fc202009-03-05 23:01:15 +0000599 /* another key was set successfully, retry to continue */
600 if (!err)
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300601 do_cont(mon, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000602}
603
604static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
605{
aliguori376253e2009-03-05 23:01:23 +0000606 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000607
aliguori376253e2009-03-05 23:01:23 +0000608 if (!context->err && bdrv_key_required(bs)) {
609 context->err = -EBUSY;
610 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
611 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000612 }
613}
614
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300615static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000616{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300617 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000618 if (!device)
619 device = "tcp::" DEFAULT_GDBSTUB_PORT;
620 if (gdbserver_start(device) < 0) {
621 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
622 device);
623 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000624 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000625 } else {
aliguori59030a82009-04-05 18:43:41 +0000626 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
627 device);
bellard8a7ddc32004-03-31 19:00:16 +0000628 }
629}
630
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300631static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100632{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300633 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100634 if (select_watchdog_action(action) == -1) {
635 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
636 }
637}
638
aliguori376253e2009-03-05 23:01:23 +0000639static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000640{
aliguori376253e2009-03-05 23:01:23 +0000641 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000642 switch(c) {
643 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000644 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000645 break;
646 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000647 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000648 break;
649 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000650 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000651 break;
652 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000653 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000654 break;
655 default:
656 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000657 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000658 } else {
aliguori376253e2009-03-05 23:01:23 +0000659 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000660 }
661 break;
662 }
aliguori376253e2009-03-05 23:01:23 +0000663 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000664}
665
aliguori376253e2009-03-05 23:01:23 +0000666static void memory_dump(Monitor *mon, int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000667 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000668{
bellard6a00d602005-11-21 23:25:50 +0000669 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000670 int nb_per_line, l, line_size, i, max_digits, len;
671 uint8_t buf[16];
672 uint64_t v;
673
674 if (format == 'i') {
675 int flags;
676 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000677 env = mon_get_cpu();
678 if (!env && !is_physical)
679 return;
bellard9307c4c2004-04-04 12:57:25 +0000680#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000681 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000682 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000683 } else if (wsize == 4) {
684 flags = 0;
685 } else {
bellard6a15fd12006-04-12 21:07:07 +0000686 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000687 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000688 if (env) {
689#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000690 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000691 (env->segs[R_CS].flags & DESC_L_MASK))
692 flags = 2;
693 else
694#endif
695 if (!(env->segs[R_CS].flags & DESC_B_MASK))
696 flags = 1;
697 }
bellard4c27ba22004-04-25 18:05:08 +0000698 }
699#endif
aliguori376253e2009-03-05 23:01:23 +0000700 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000701 return;
702 }
703
704 len = wsize * count;
705 if (wsize == 1)
706 line_size = 8;
707 else
708 line_size = 16;
709 nb_per_line = line_size / wsize;
710 max_digits = 0;
711
712 switch(format) {
713 case 'o':
714 max_digits = (wsize * 8 + 2) / 3;
715 break;
716 default:
717 case 'x':
718 max_digits = (wsize * 8) / 4;
719 break;
720 case 'u':
721 case 'd':
722 max_digits = (wsize * 8 * 10 + 32) / 33;
723 break;
724 case 'c':
725 wsize = 1;
726 break;
727 }
728
729 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000730 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000731 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000732 else
aliguori376253e2009-03-05 23:01:23 +0000733 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000734 l = len;
735 if (l > line_size)
736 l = line_size;
737 if (is_physical) {
738 cpu_physical_memory_rw(addr, buf, l, 0);
739 } else {
bellard6a00d602005-11-21 23:25:50 +0000740 env = mon_get_cpu();
741 if (!env)
742 break;
aliguoric8f79b62008-08-18 14:00:20 +0000743 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000744 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000745 break;
746 }
bellard9307c4c2004-04-04 12:57:25 +0000747 }
ths5fafdf22007-09-16 21:08:06 +0000748 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000749 while (i < l) {
750 switch(wsize) {
751 default:
752 case 1:
753 v = ldub_raw(buf + i);
754 break;
755 case 2:
756 v = lduw_raw(buf + i);
757 break;
758 case 4:
bellard92a31b12005-02-10 22:00:52 +0000759 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000760 break;
761 case 8:
762 v = ldq_raw(buf + i);
763 break;
764 }
aliguori376253e2009-03-05 23:01:23 +0000765 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000766 switch(format) {
767 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000768 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000769 break;
770 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000771 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000772 break;
773 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000774 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000775 break;
776 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000777 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000778 break;
779 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000780 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000781 break;
782 }
783 i += wsize;
784 }
aliguori376253e2009-03-05 23:01:23 +0000785 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000786 addr += l;
787 len -= l;
788 }
789}
790
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300791static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000792{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300793 int count = qdict_get_int(qdict, "count");
794 int format = qdict_get_int(qdict, "format");
795 int size = qdict_get_int(qdict, "size");
796 target_long addr = qdict_get_int(qdict, "addr");
797
aliguori376253e2009-03-05 23:01:23 +0000798 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000799}
800
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300801static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000802{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300803 int count = qdict_get_int(qdict, "count");
804 int format = qdict_get_int(qdict, "format");
805 int size = qdict_get_int(qdict, "size");
806 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
807
aliguori376253e2009-03-05 23:01:23 +0000808 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000809}
810
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300811static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000812{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300813 int format = qdict_get_int(qdict, "format");
814 target_phys_addr_t val = qdict_get_int(qdict, "val");
815
blueswir17743e582007-09-24 18:39:04 +0000816#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000817 switch(format) {
818 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000819 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000820 break;
821 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000822 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000823 break;
824 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000825 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000826 break;
827 default:
828 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000829 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000830 break;
831 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000832 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000833 break;
834 }
bellard92a31b12005-02-10 22:00:52 +0000835#else
836 switch(format) {
837 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000838 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000839 break;
840 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000841 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000842 break;
843 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000844 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000845 break;
846 default:
847 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000848 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000849 break;
850 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000851 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000852 break;
853 }
854#endif
aliguori376253e2009-03-05 23:01:23 +0000855 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000856}
857
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300858static void do_memory_save(Monitor *mon, const QDict *qdict)
bellardb371dc52007-01-03 15:20:39 +0000859{
860 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300861 uint32_t size = qdict_get_int(qdict, "size");
862 const char *filename = qdict_get_str(qdict, "filename");
863 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +0000864 uint32_t l;
865 CPUState *env;
866 uint8_t buf[1024];
867
868 env = mon_get_cpu();
869 if (!env)
870 return;
871
872 f = fopen(filename, "wb");
873 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000874 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000875 return;
876 }
877 while (size != 0) {
878 l = sizeof(buf);
879 if (l > size)
880 l = size;
881 cpu_memory_rw_debug(env, addr, buf, l, 0);
882 fwrite(buf, 1, l, f);
883 addr += l;
884 size -= l;
885 }
886 fclose(f);
887}
888
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300889static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
aurel32a8bdf7a2008-04-11 21:36:14 +0000890{
891 FILE *f;
892 uint32_t l;
893 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300894 uint32_t size = qdict_get_int(qdict, "size");
895 const char *filename = qdict_get_str(qdict, "filename");
896 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +0000897
898 f = fopen(filename, "wb");
899 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000900 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000901 return;
902 }
903 while (size != 0) {
904 l = sizeof(buf);
905 if (l > size)
906 l = size;
907 cpu_physical_memory_rw(addr, buf, l, 0);
908 fwrite(buf, 1, l, f);
909 fflush(f);
910 addr += l;
911 size -= l;
912 }
913 fclose(f);
914}
915
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300916static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000917{
918 uint32_t addr;
919 uint8_t buf[1];
920 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300921 uint32_t start = qdict_get_int(qdict, "start");
922 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000923
924 sum = 0;
925 for(addr = start; addr < (start + size); addr++) {
926 cpu_physical_memory_rw(addr, buf, 1, 0);
927 /* BSD sum algorithm ('sum' Unix command) */
928 sum = (sum >> 1) | (sum << 15);
929 sum += buf[0];
930 }
aliguori376253e2009-03-05 23:01:23 +0000931 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000932}
933
bellarda3a91a32004-06-04 11:06:21 +0000934typedef struct {
935 int keycode;
936 const char *name;
937} KeyDef;
938
939static const KeyDef key_defs[] = {
940 { 0x2a, "shift" },
941 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000942
bellarda3a91a32004-06-04 11:06:21 +0000943 { 0x38, "alt" },
944 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000945 { 0x64, "altgr" },
946 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000947 { 0x1d, "ctrl" },
948 { 0x9d, "ctrl_r" },
949
950 { 0xdd, "menu" },
951
952 { 0x01, "esc" },
953
954 { 0x02, "1" },
955 { 0x03, "2" },
956 { 0x04, "3" },
957 { 0x05, "4" },
958 { 0x06, "5" },
959 { 0x07, "6" },
960 { 0x08, "7" },
961 { 0x09, "8" },
962 { 0x0a, "9" },
963 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000964 { 0x0c, "minus" },
965 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000966 { 0x0e, "backspace" },
967
968 { 0x0f, "tab" },
969 { 0x10, "q" },
970 { 0x11, "w" },
971 { 0x12, "e" },
972 { 0x13, "r" },
973 { 0x14, "t" },
974 { 0x15, "y" },
975 { 0x16, "u" },
976 { 0x17, "i" },
977 { 0x18, "o" },
978 { 0x19, "p" },
979
980 { 0x1c, "ret" },
981
982 { 0x1e, "a" },
983 { 0x1f, "s" },
984 { 0x20, "d" },
985 { 0x21, "f" },
986 { 0x22, "g" },
987 { 0x23, "h" },
988 { 0x24, "j" },
989 { 0x25, "k" },
990 { 0x26, "l" },
991
992 { 0x2c, "z" },
993 { 0x2d, "x" },
994 { 0x2e, "c" },
995 { 0x2f, "v" },
996 { 0x30, "b" },
997 { 0x31, "n" },
998 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000999 { 0x33, "comma" },
1000 { 0x34, "dot" },
1001 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001002
balrog4d3b6f62008-02-10 16:33:14 +00001003 { 0x37, "asterisk" },
1004
bellarda3a91a32004-06-04 11:06:21 +00001005 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001006 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001007 { 0x3b, "f1" },
1008 { 0x3c, "f2" },
1009 { 0x3d, "f3" },
1010 { 0x3e, "f4" },
1011 { 0x3f, "f5" },
1012 { 0x40, "f6" },
1013 { 0x41, "f7" },
1014 { 0x42, "f8" },
1015 { 0x43, "f9" },
1016 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001017 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001018 { 0x46, "scroll_lock" },
1019
bellard64866c32006-05-07 18:03:31 +00001020 { 0xb5, "kp_divide" },
1021 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001022 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001023 { 0x4e, "kp_add" },
1024 { 0x9c, "kp_enter" },
1025 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001026 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001027
1028 { 0x52, "kp_0" },
1029 { 0x4f, "kp_1" },
1030 { 0x50, "kp_2" },
1031 { 0x51, "kp_3" },
1032 { 0x4b, "kp_4" },
1033 { 0x4c, "kp_5" },
1034 { 0x4d, "kp_6" },
1035 { 0x47, "kp_7" },
1036 { 0x48, "kp_8" },
1037 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001038
bellarda3a91a32004-06-04 11:06:21 +00001039 { 0x56, "<" },
1040
1041 { 0x57, "f11" },
1042 { 0x58, "f12" },
1043
1044 { 0xb7, "print" },
1045
1046 { 0xc7, "home" },
1047 { 0xc9, "pgup" },
1048 { 0xd1, "pgdn" },
1049 { 0xcf, "end" },
1050
1051 { 0xcb, "left" },
1052 { 0xc8, "up" },
1053 { 0xd0, "down" },
1054 { 0xcd, "right" },
1055
1056 { 0xd2, "insert" },
1057 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001058#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1059 { 0xf0, "stop" },
1060 { 0xf1, "again" },
1061 { 0xf2, "props" },
1062 { 0xf3, "undo" },
1063 { 0xf4, "front" },
1064 { 0xf5, "copy" },
1065 { 0xf6, "open" },
1066 { 0xf7, "paste" },
1067 { 0xf8, "find" },
1068 { 0xf9, "cut" },
1069 { 0xfa, "lf" },
1070 { 0xfb, "help" },
1071 { 0xfc, "meta_l" },
1072 { 0xfd, "meta_r" },
1073 { 0xfe, "compose" },
1074#endif
bellarda3a91a32004-06-04 11:06:21 +00001075 { 0, NULL },
1076};
1077
1078static int get_keycode(const char *key)
1079{
1080 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001081 char *endp;
1082 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001083
1084 for(p = key_defs; p->name != NULL; p++) {
1085 if (!strcmp(key, p->name))
1086 return p->keycode;
1087 }
bellard64866c32006-05-07 18:03:31 +00001088 if (strstart(key, "0x", NULL)) {
1089 ret = strtoul(key, &endp, 0);
1090 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1091 return ret;
1092 }
bellarda3a91a32004-06-04 11:06:21 +00001093 return -1;
1094}
1095
balrogc8256f92008-06-08 22:45:01 +00001096#define MAX_KEYCODES 16
1097static uint8_t keycodes[MAX_KEYCODES];
1098static int nb_pending_keycodes;
1099static QEMUTimer *key_timer;
1100
1101static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001102{
balrogc8256f92008-06-08 22:45:01 +00001103 int keycode;
1104
1105 while (nb_pending_keycodes > 0) {
1106 nb_pending_keycodes--;
1107 keycode = keycodes[nb_pending_keycodes];
1108 if (keycode & 0x80)
1109 kbd_put_keycode(0xe0);
1110 kbd_put_keycode(keycode | 0x80);
1111 }
1112}
1113
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001114static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001115{
balrog3401c0d2008-06-04 10:05:59 +00001116 char keyname_buf[16];
1117 char *separator;
1118 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001119 const char *string = qdict_get_str(qdict, "string");
1120 int has_hold_time = qdict_haskey(qdict, "hold_time");
1121 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001122
balrogc8256f92008-06-08 22:45:01 +00001123 if (nb_pending_keycodes > 0) {
1124 qemu_del_timer(key_timer);
1125 release_keys(NULL);
1126 }
1127 if (!has_hold_time)
1128 hold_time = 100;
1129 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001130 while (1) {
1131 separator = strchr(string, '-');
1132 keyname_len = separator ? separator - string : strlen(string);
1133 if (keyname_len > 0) {
1134 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1135 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001136 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001137 return;
bellarda3a91a32004-06-04 11:06:21 +00001138 }
balrogc8256f92008-06-08 22:45:01 +00001139 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001140 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001141 return;
1142 }
1143 keyname_buf[keyname_len] = 0;
1144 keycode = get_keycode(keyname_buf);
1145 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001146 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001147 return;
1148 }
balrogc8256f92008-06-08 22:45:01 +00001149 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001150 }
balrog3401c0d2008-06-04 10:05:59 +00001151 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001152 break;
balrog3401c0d2008-06-04 10:05:59 +00001153 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001154 }
balrogc8256f92008-06-08 22:45:01 +00001155 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001156 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001157 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001158 keycode = keycodes[i];
1159 if (keycode & 0x80)
1160 kbd_put_keycode(0xe0);
1161 kbd_put_keycode(keycode & 0x7f);
1162 }
balrogc8256f92008-06-08 22:45:01 +00001163 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001164 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1165 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001166}
1167
bellard13224a82006-07-14 22:03:35 +00001168static int mouse_button_state;
1169
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001170static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001171{
1172 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001173 const char *dx_str = qdict_get_str(qdict, "dx_str");
1174 const char *dy_str = qdict_get_str(qdict, "dy_str");
1175 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001176 dx = strtol(dx_str, NULL, 0);
1177 dy = strtol(dy_str, NULL, 0);
1178 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001179 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001180 dz = strtol(dz_str, NULL, 0);
1181 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1182}
1183
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001184static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001185{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001186 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001187 mouse_button_state = button_state;
1188 kbd_mouse_event(0, 0, 0, mouse_button_state);
1189}
1190
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001191static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001192{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001193 int size = qdict_get_int(qdict, "size");
1194 int addr = qdict_get_int(qdict, "addr");
1195 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001196 uint32_t val;
1197 int suffix;
1198
1199 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001200 int index = qdict_get_int(qdict, "index");
Isaku Yamahatad56dd6c2009-07-02 19:32:07 +09001201 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001202 addr++;
1203 }
1204 addr &= 0xffff;
1205
1206 switch(size) {
1207 default:
1208 case 1:
1209 val = cpu_inb(NULL, addr);
1210 suffix = 'b';
1211 break;
1212 case 2:
1213 val = cpu_inw(NULL, addr);
1214 suffix = 'w';
1215 break;
1216 case 4:
1217 val = cpu_inl(NULL, addr);
1218 suffix = 'l';
1219 break;
1220 }
aliguori376253e2009-03-05 23:01:23 +00001221 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1222 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001223}
bellarda3a91a32004-06-04 11:06:21 +00001224
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001225static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001226{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001227 int size = qdict_get_int(qdict, "size");
1228 int addr = qdict_get_int(qdict, "addr");
1229 int val = qdict_get_int(qdict, "val");
1230
Jan Kiszkaf1147842009-07-14 10:20:11 +02001231 addr &= IOPORTS_MASK;
1232
1233 switch (size) {
1234 default:
1235 case 1:
1236 cpu_outb(NULL, addr, val);
1237 break;
1238 case 2:
1239 cpu_outw(NULL, addr, val);
1240 break;
1241 case 4:
1242 cpu_outl(NULL, addr, val);
1243 break;
1244 }
1245}
1246
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001247static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001248{
1249 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001250 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001251
Jan Kiszka76e30d02009-07-02 00:19:02 +02001252 res = qemu_boot_set(bootdevice);
1253 if (res == 0) {
1254 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1255 } else if (res > 0) {
1256 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001257 } else {
aliguori376253e2009-03-05 23:01:23 +00001258 monitor_printf(mon, "no function defined to set boot device list for "
1259 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001260 }
1261}
1262
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001263static void do_system_reset(Monitor *mon, const QDict *qdict)
bellarde4f90822004-06-20 12:35:44 +00001264{
1265 qemu_system_reset_request();
1266}
1267
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001268static void do_system_powerdown(Monitor *mon, const QDict *qdict)
bellard34751872005-07-02 14:31:34 +00001269{
1270 qemu_system_powerdown_request();
1271}
1272
bellardb86bda52004-09-18 19:32:46 +00001273#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001274static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001275{
aliguori376253e2009-03-05 23:01:23 +00001276 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1277 addr,
1278 pte & mask,
1279 pte & PG_GLOBAL_MASK ? 'G' : '-',
1280 pte & PG_PSE_MASK ? 'P' : '-',
1281 pte & PG_DIRTY_MASK ? 'D' : '-',
1282 pte & PG_ACCESSED_MASK ? 'A' : '-',
1283 pte & PG_PCD_MASK ? 'C' : '-',
1284 pte & PG_PWT_MASK ? 'T' : '-',
1285 pte & PG_USER_MASK ? 'U' : '-',
1286 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001287}
1288
aliguori376253e2009-03-05 23:01:23 +00001289static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001290{
bellard6a00d602005-11-21 23:25:50 +00001291 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001292 int l1, l2;
1293 uint32_t pgd, pde, pte;
1294
bellard6a00d602005-11-21 23:25:50 +00001295 env = mon_get_cpu();
1296 if (!env)
1297 return;
1298
bellardb86bda52004-09-18 19:32:46 +00001299 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001300 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001301 return;
1302 }
1303 pgd = env->cr[3] & ~0xfff;
1304 for(l1 = 0; l1 < 1024; l1++) {
1305 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1306 pde = le32_to_cpu(pde);
1307 if (pde & PG_PRESENT_MASK) {
1308 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001309 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001310 } else {
1311 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001312 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001313 (uint8_t *)&pte, 4);
1314 pte = le32_to_cpu(pte);
1315 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001316 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001317 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001318 ~0xfff);
1319 }
1320 }
1321 }
1322 }
1323 }
1324}
1325
aliguori376253e2009-03-05 23:01:23 +00001326static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001327 uint32_t end, int prot)
1328{
bellard9746b152004-11-11 18:30:24 +00001329 int prot1;
1330 prot1 = *plast_prot;
1331 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001332 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001333 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1334 *pstart, end, end - *pstart,
1335 prot1 & PG_USER_MASK ? 'u' : '-',
1336 'r',
1337 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001338 }
1339 if (prot != 0)
1340 *pstart = end;
1341 else
1342 *pstart = -1;
1343 *plast_prot = prot;
1344 }
1345}
1346
aliguori376253e2009-03-05 23:01:23 +00001347static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001348{
bellard6a00d602005-11-21 23:25:50 +00001349 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001350 int l1, l2, prot, last_prot;
1351 uint32_t pgd, pde, pte, start, end;
1352
bellard6a00d602005-11-21 23:25:50 +00001353 env = mon_get_cpu();
1354 if (!env)
1355 return;
1356
bellardb86bda52004-09-18 19:32:46 +00001357 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001358 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001359 return;
1360 }
1361 pgd = env->cr[3] & ~0xfff;
1362 last_prot = 0;
1363 start = -1;
1364 for(l1 = 0; l1 < 1024; l1++) {
1365 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1366 pde = le32_to_cpu(pde);
1367 end = l1 << 22;
1368 if (pde & PG_PRESENT_MASK) {
1369 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1370 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001371 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001372 } else {
1373 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001374 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001375 (uint8_t *)&pte, 4);
1376 pte = le32_to_cpu(pte);
1377 end = (l1 << 22) + (l2 << 12);
1378 if (pte & PG_PRESENT_MASK) {
1379 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1380 } else {
1381 prot = 0;
1382 }
aliguori376253e2009-03-05 23:01:23 +00001383 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001384 }
1385 }
1386 } else {
1387 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001388 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001389 }
1390 }
1391}
1392#endif
1393
aurel327c664e22009-03-03 06:12:22 +00001394#if defined(TARGET_SH4)
1395
aliguori376253e2009-03-05 23:01:23 +00001396static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001397{
aliguori376253e2009-03-05 23:01:23 +00001398 monitor_printf(mon, " tlb%i:\t"
1399 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1400 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1401 "dirty=%hhu writethrough=%hhu\n",
1402 idx,
1403 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1404 tlb->v, tlb->sh, tlb->c, tlb->pr,
1405 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001406}
1407
aliguori376253e2009-03-05 23:01:23 +00001408static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001409{
1410 CPUState *env = mon_get_cpu();
1411 int i;
1412
aliguori376253e2009-03-05 23:01:23 +00001413 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001414 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001415 print_tlb (mon, i, &env->itlb[i]);
1416 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001417 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001418 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001419}
1420
1421#endif
1422
aliguori376253e2009-03-05 23:01:23 +00001423static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001424{
1425#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001426 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001427 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001428 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001429 else
aliguori376253e2009-03-05 23:01:23 +00001430 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001431#else
aliguori376253e2009-03-05 23:01:23 +00001432 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001433#endif
1434}
1435
aliguori030ea372009-04-21 22:30:47 +00001436static void do_info_numa(Monitor *mon)
1437{
aliguorib28b6232009-04-22 20:20:29 +00001438 int i;
aliguori030ea372009-04-21 22:30:47 +00001439 CPUState *env;
1440
1441 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1442 for (i = 0; i < nb_numa_nodes; i++) {
1443 monitor_printf(mon, "node %d cpus:", i);
1444 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1445 if (env->numa_node == i) {
1446 monitor_printf(mon, " %d", env->cpu_index);
1447 }
1448 }
1449 monitor_printf(mon, "\n");
1450 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1451 node_mem[i] >> 20);
1452 }
1453}
1454
bellard5f1ce942006-02-08 22:40:15 +00001455#ifdef CONFIG_PROFILER
1456
aliguori376253e2009-03-05 23:01:23 +00001457static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001458{
1459 int64_t total;
1460 total = qemu_time;
1461 if (total == 0)
1462 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001463 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1464 dev_time, dev_time / (double)ticks_per_sec);
1465 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1466 qemu_time, qemu_time / (double)ticks_per_sec);
bellard5f1ce942006-02-08 22:40:15 +00001467 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001468 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001469}
1470#else
aliguori376253e2009-03-05 23:01:23 +00001471static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001472{
aliguori376253e2009-03-05 23:01:23 +00001473 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001474}
1475#endif
1476
bellardec36b692006-07-16 18:57:03 +00001477/* Capture support */
1478static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1479
aliguori376253e2009-03-05 23:01:23 +00001480static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001481{
1482 int i;
1483 CaptureState *s;
1484
1485 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001486 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001487 s->ops.info (s->opaque);
1488 }
1489}
1490
Blue Swirl23130862009-06-06 08:22:04 +00001491#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001492static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001493{
1494 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001495 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001496 CaptureState *s;
1497
1498 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1499 if (i == n) {
1500 s->ops.destroy (s->opaque);
1501 LIST_REMOVE (s, entries);
1502 qemu_free (s);
1503 return;
1504 }
1505 }
1506}
1507
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001508static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001509{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001510 const char *path = qdict_get_str(qdict, "path");
1511 int has_freq = qdict_haskey(qdict, "freq");
1512 int freq = qdict_get_try_int(qdict, "freq", -1);
1513 int has_bits = qdict_haskey(qdict, "bits");
1514 int bits = qdict_get_try_int(qdict, "bits", -1);
1515 int has_channels = qdict_haskey(qdict, "nchannels");
1516 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001517 CaptureState *s;
1518
1519 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001520
1521 freq = has_freq ? freq : 44100;
1522 bits = has_bits ? bits : 16;
1523 nchannels = has_channels ? nchannels : 2;
1524
1525 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001526 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001527 qemu_free (s);
1528 }
1529 LIST_INSERT_HEAD (&capture_head, s, entries);
1530}
1531#endif
1532
aurel32dc1c0b72008-04-27 23:52:12 +00001533#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001534static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001535{
1536 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001537 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001538
1539 for (env = first_cpu; env != NULL; env = env->next_cpu)
1540 if (env->cpu_index == cpu_index) {
1541 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1542 break;
1543 }
1544}
1545#endif
1546
aliguori376253e2009-03-05 23:01:23 +00001547static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001548{
aurel321b530a62009-04-05 20:08:59 +00001549 if (vm_running) {
1550 if (singlestep) {
1551 monitor_printf(mon, "VM status: running (single step mode)\n");
1552 } else {
1553 monitor_printf(mon, "VM status: running\n");
1554 }
1555 } else
aliguori376253e2009-03-05 23:01:23 +00001556 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001557}
1558
1559
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001560static void do_balloon(Monitor *mon, const QDict *qdict)
aliguoridf751fa2008-12-04 20:19:35 +00001561{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001562 int value = qdict_get_int(qdict, "value");
aliguoridf751fa2008-12-04 20:19:35 +00001563 ram_addr_t target = value;
1564 qemu_balloon(target << 20);
1565}
1566
aliguori376253e2009-03-05 23:01:23 +00001567static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001568{
1569 ram_addr_t actual;
1570
1571 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001572 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001573 monitor_printf(mon, "Using KVM without synchronous MMU, "
1574 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001575 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001576 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001577 else
aliguori376253e2009-03-05 23:01:23 +00001578 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001579}
1580
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001581static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001582{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001583 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001584
aliguori76655d62009-03-06 20:27:37 +00001585 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001586 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001587 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001588 return acl;
1589}
aliguori76655d62009-03-06 20:27:37 +00001590
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001591static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001592{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001593 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001594 qemu_acl *acl = find_acl(mon, aclname);
1595 qemu_acl_entry *entry;
1596 int i = 0;
1597
1598 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001599 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001600 acl->defaultDeny ? "deny" : "allow");
aliguori28a76be2009-03-06 20:27:40 +00001601 TAILQ_FOREACH(entry, &acl->entries, next) {
1602 i++;
1603 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001604 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001605 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001606 }
1607}
1608
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001609static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001610{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001611 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001612 qemu_acl *acl = find_acl(mon, aclname);
1613
1614 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001615 qemu_acl_reset(acl);
1616 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001617 }
1618}
aliguori76655d62009-03-06 20:27:37 +00001619
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001620static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001621{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001622 const char *aclname = qdict_get_str(qdict, "aclname");
1623 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001624 qemu_acl *acl = find_acl(mon, aclname);
1625
1626 if (acl) {
1627 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001628 acl->defaultDeny = 0;
1629 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001630 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001631 acl->defaultDeny = 1;
1632 monitor_printf(mon, "acl: policy set to 'deny'\n");
1633 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001634 monitor_printf(mon, "acl: unknown policy '%s', "
1635 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001636 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001637 }
1638}
aliguori76655d62009-03-06 20:27:37 +00001639
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001640static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001641{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001642 const char *aclname = qdict_get_str(qdict, "aclname");
1643 const char *match = qdict_get_str(qdict, "match");
1644 const char *policy = qdict_get_str(qdict, "policy");
1645 int has_index = qdict_haskey(qdict, "index");
1646 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001647 qemu_acl *acl = find_acl(mon, aclname);
1648 int deny, ret;
1649
1650 if (acl) {
1651 if (strcmp(policy, "allow") == 0) {
1652 deny = 0;
1653 } else if (strcmp(policy, "deny") == 0) {
1654 deny = 1;
1655 } else {
1656 monitor_printf(mon, "acl: unknown policy '%s', "
1657 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001658 return;
1659 }
aliguori28a76be2009-03-06 20:27:40 +00001660 if (has_index)
1661 ret = qemu_acl_insert(acl, deny, match, index);
1662 else
1663 ret = qemu_acl_append(acl, deny, match);
1664 if (ret < 0)
1665 monitor_printf(mon, "acl: unable to add acl entry\n");
1666 else
1667 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001668 }
1669}
aliguori76655d62009-03-06 20:27:37 +00001670
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001671static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001672{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001673 const char *aclname = qdict_get_str(qdict, "aclname");
1674 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001675 qemu_acl *acl = find_acl(mon, aclname);
1676 int ret;
aliguori76655d62009-03-06 20:27:37 +00001677
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001678 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001679 ret = qemu_acl_remove(acl, match);
1680 if (ret < 0)
1681 monitor_printf(mon, "acl: no matching acl entry\n");
1682 else
1683 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001684 }
1685}
1686
Huang Ying79c4f6b2009-06-23 10:05:14 +08001687#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001688static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001689{
1690 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001691 int cpu_index = qdict_get_int(qdict, "cpu_index");
1692 int bank = qdict_get_int(qdict, "bank");
1693 uint64_t status = qdict_get_int(qdict, "status");
1694 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1695 uint64_t addr = qdict_get_int(qdict, "addr");
1696 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08001697
1698 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1699 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1700 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1701 break;
1702 }
1703}
1704#endif
1705
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001706static void do_getfd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001707{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001708 const char *fdname = qdict_get_str(qdict, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001709 mon_fd_t *monfd;
1710 int fd;
1711
1712 fd = qemu_chr_get_msgfd(mon->chr);
1713 if (fd == -1) {
1714 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1715 return;
1716 }
1717
1718 if (qemu_isdigit(fdname[0])) {
1719 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1720 return;
1721 }
1722
1723 fd = dup(fd);
1724 if (fd == -1) {
1725 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1726 strerror(errno));
1727 return;
1728 }
1729
1730 LIST_FOREACH(monfd, &mon->fds, next) {
1731 if (strcmp(monfd->name, fdname) != 0) {
1732 continue;
1733 }
1734
1735 close(monfd->fd);
1736 monfd->fd = fd;
1737 return;
1738 }
1739
1740 monfd = qemu_mallocz(sizeof(mon_fd_t));
1741 monfd->name = qemu_strdup(fdname);
1742 monfd->fd = fd;
1743
1744 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1745}
1746
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001747static void do_closefd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001748{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001749 const char *fdname = qdict_get_str(qdict, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001750 mon_fd_t *monfd;
1751
1752 LIST_FOREACH(monfd, &mon->fds, next) {
1753 if (strcmp(monfd->name, fdname) != 0) {
1754 continue;
1755 }
1756
1757 LIST_REMOVE(monfd, next);
1758 close(monfd->fd);
1759 qemu_free(monfd->name);
1760 qemu_free(monfd);
1761 return;
1762 }
1763
1764 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1765 fdname);
1766}
1767
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001768static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001769{
1770 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001771 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001772
1773 vm_stop(0);
1774
Juan Quintela05f24012009-08-20 19:42:22 +02001775 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001776 vm_start();
1777}
1778
Mark McLoughlin7768e042009-07-22 09:11:41 +01001779int monitor_get_fd(Monitor *mon, const char *fdname)
1780{
1781 mon_fd_t *monfd;
1782
1783 LIST_FOREACH(monfd, &mon->fds, next) {
1784 int fd;
1785
1786 if (strcmp(monfd->name, fdname) != 0) {
1787 continue;
1788 }
1789
1790 fd = monfd->fd;
1791
1792 /* caller takes ownership of fd */
1793 LIST_REMOVE(monfd, next);
1794 qemu_free(monfd->name);
1795 qemu_free(monfd);
1796
1797 return fd;
1798 }
1799
1800 return -1;
1801}
1802
aliguori376253e2009-03-05 23:01:23 +00001803static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001804#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001805 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001806};
1807
Blue Swirl23130862009-06-06 08:22:04 +00001808/* Please update qemu-monitor.hx when adding or changing commands */
aliguori376253e2009-03-05 23:01:23 +00001809static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001810 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001811 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001812 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001813 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001814 { "chardev", "", qemu_chr_info,
1815 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001816 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001817 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001818 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001819 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001820 { "registers", "", do_info_registers,
1821 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001822 { "cpus", "", do_info_cpus,
1823 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001824 { "history", "", do_info_history,
1825 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001826 { "irq", "", irq_info,
1827 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001828 { "pic", "", pic_info,
1829 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001830 { "pci", "", pci_info,
1831 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001832#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001833 { "tlb", "", tlb_info,
1834 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001835#endif
1836#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001837 { "mem", "", mem_info,
1838 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001839 { "hpet", "", do_info_hpet,
1840 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001841#endif
bellarde3db7222005-01-26 22:00:47 +00001842 { "jit", "", do_info_jit,
1843 "", "show dynamic compiler info", },
aliguori7ba1e612008-11-05 16:04:33 +00001844 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001845 "", "show KVM information", },
aliguori030ea372009-04-21 22:30:47 +00001846 { "numa", "", do_info_numa,
1847 "", "show NUMA information", },
bellarda594cfb2005-11-06 16:13:29 +00001848 { "usb", "", usb_info,
1849 "", "show guest USB devices", },
1850 { "usbhost", "", usb_host_info,
1851 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001852 { "profile", "", do_info_profile,
1853 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001854 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001855 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001856 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001857 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001858 { "status", "", do_info_status,
1859 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001860 { "pcmcia", "", pcmcia_info,
1861 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001862 { "mice", "", do_info_mice,
1863 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001864 { "vnc", "", do_info_vnc,
1865 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001866 { "name", "", do_info_name,
1867 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001868 { "uuid", "", do_info_uuid,
1869 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001870#if defined(TARGET_PPC)
1871 { "cpustats", "", do_info_cpu_stats,
1872 "", "show CPU statistics", },
1873#endif
blueswir131a60e22007-10-26 18:42:59 +00001874#if defined(CONFIG_SLIRP)
Jan Kiszka6dbe5532009-06-24 14:42:29 +02001875 { "usernet", "", do_info_usernet,
1876 "", "show user network stack connection states", },
blueswir131a60e22007-10-26 18:42:59 +00001877#endif
aliguori5bb79102008-10-13 03:12:02 +00001878 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001879 { "balloon", "", do_info_balloon,
1880 "", "show balloon information" },
Gerd Hoffmanncae49562009-06-05 15:53:17 +01001881 { "qtree", "", do_info_qtree,
1882 "", "show device tree" },
Gerd Hoffmannf6c64e02009-08-03 15:03:09 +02001883 { "qdm", "", do_info_qdm,
1884 "", "show qdev device model list" },
bellard9dc39cb2004-03-14 21:38:27 +00001885 { NULL, NULL, },
1886};
1887
bellard9307c4c2004-04-04 12:57:25 +00001888/*******************************************************************/
1889
1890static const char *pch;
1891static jmp_buf expr_env;
1892
bellard92a31b12005-02-10 22:00:52 +00001893#define MD_TLONG 0
1894#define MD_I32 1
1895
bellard9307c4c2004-04-04 12:57:25 +00001896typedef struct MonitorDef {
1897 const char *name;
1898 int offset;
blueswir18662d652008-10-02 18:32:44 +00001899 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001900 int type;
bellard9307c4c2004-04-04 12:57:25 +00001901} MonitorDef;
1902
bellard57206fd2004-04-25 18:54:52 +00001903#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001904static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001905{
bellard6a00d602005-11-21 23:25:50 +00001906 CPUState *env = mon_get_cpu();
1907 if (!env)
1908 return 0;
1909 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001910}
1911#endif
1912
bellarda541f292004-04-12 20:39:29 +00001913#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001914static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001915{
bellard6a00d602005-11-21 23:25:50 +00001916 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001917 unsigned int u;
1918 int i;
1919
bellard6a00d602005-11-21 23:25:50 +00001920 if (!env)
1921 return 0;
1922
bellarda541f292004-04-12 20:39:29 +00001923 u = 0;
1924 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00001925 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001926
1927 return u;
1928}
1929
blueswir18662d652008-10-02 18:32:44 +00001930static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001931{
bellard6a00d602005-11-21 23:25:50 +00001932 CPUState *env = mon_get_cpu();
1933 if (!env)
1934 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001935 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001936}
1937
blueswir18662d652008-10-02 18:32:44 +00001938static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001939{
bellard6a00d602005-11-21 23:25:50 +00001940 CPUState *env = mon_get_cpu();
1941 if (!env)
1942 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001943 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001944}
bellard9fddaa02004-05-21 12:59:32 +00001945
blueswir18662d652008-10-02 18:32:44 +00001946static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001947{
bellard6a00d602005-11-21 23:25:50 +00001948 CPUState *env = mon_get_cpu();
1949 if (!env)
1950 return 0;
1951 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001952}
1953
blueswir18662d652008-10-02 18:32:44 +00001954static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001955{
bellard6a00d602005-11-21 23:25:50 +00001956 CPUState *env = mon_get_cpu();
1957 if (!env)
1958 return 0;
1959 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001960}
1961
blueswir18662d652008-10-02 18:32:44 +00001962static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001963{
bellard6a00d602005-11-21 23:25:50 +00001964 CPUState *env = mon_get_cpu();
1965 if (!env)
1966 return 0;
1967 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001968}
bellarda541f292004-04-12 20:39:29 +00001969#endif
1970
bellarde95c8d52004-09-30 22:22:08 +00001971#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001972#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001973static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001974{
bellard6a00d602005-11-21 23:25:50 +00001975 CPUState *env = mon_get_cpu();
1976 if (!env)
1977 return 0;
1978 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001979}
bellard7b936c02005-10-30 17:05:13 +00001980#endif
bellarde95c8d52004-09-30 22:22:08 +00001981
blueswir18662d652008-10-02 18:32:44 +00001982static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001983{
bellard6a00d602005-11-21 23:25:50 +00001984 CPUState *env = mon_get_cpu();
1985 if (!env)
1986 return 0;
1987 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001988}
1989#endif
1990
blueswir18662d652008-10-02 18:32:44 +00001991static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001992#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001993
1994#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001995 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001996 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001997 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001998
bellard9307c4c2004-04-04 12:57:25 +00001999 { "eax", offsetof(CPUState, regs[0]) },
2000 { "ecx", offsetof(CPUState, regs[1]) },
2001 { "edx", offsetof(CPUState, regs[2]) },
2002 { "ebx", offsetof(CPUState, regs[3]) },
2003 { "esp|sp", offsetof(CPUState, regs[4]) },
2004 { "ebp|fp", offsetof(CPUState, regs[5]) },
2005 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002006 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002007#ifdef TARGET_X86_64
2008 { "r8", offsetof(CPUState, regs[8]) },
2009 { "r9", offsetof(CPUState, regs[9]) },
2010 { "r10", offsetof(CPUState, regs[10]) },
2011 { "r11", offsetof(CPUState, regs[11]) },
2012 { "r12", offsetof(CPUState, regs[12]) },
2013 { "r13", offsetof(CPUState, regs[13]) },
2014 { "r14", offsetof(CPUState, regs[14]) },
2015 { "r15", offsetof(CPUState, regs[15]) },
2016#endif
bellard9307c4c2004-04-04 12:57:25 +00002017 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002018 { "eip", offsetof(CPUState, eip) },
2019 SEG("cs", R_CS)
2020 SEG("ds", R_DS)
2021 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002022 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002023 SEG("fs", R_FS)
2024 SEG("gs", R_GS)
2025 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002026#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002027 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002028 { "r0", offsetof(CPUState, gpr[0]) },
2029 { "r1", offsetof(CPUState, gpr[1]) },
2030 { "r2", offsetof(CPUState, gpr[2]) },
2031 { "r3", offsetof(CPUState, gpr[3]) },
2032 { "r4", offsetof(CPUState, gpr[4]) },
2033 { "r5", offsetof(CPUState, gpr[5]) },
2034 { "r6", offsetof(CPUState, gpr[6]) },
2035 { "r7", offsetof(CPUState, gpr[7]) },
2036 { "r8", offsetof(CPUState, gpr[8]) },
2037 { "r9", offsetof(CPUState, gpr[9]) },
2038 { "r10", offsetof(CPUState, gpr[10]) },
2039 { "r11", offsetof(CPUState, gpr[11]) },
2040 { "r12", offsetof(CPUState, gpr[12]) },
2041 { "r13", offsetof(CPUState, gpr[13]) },
2042 { "r14", offsetof(CPUState, gpr[14]) },
2043 { "r15", offsetof(CPUState, gpr[15]) },
2044 { "r16", offsetof(CPUState, gpr[16]) },
2045 { "r17", offsetof(CPUState, gpr[17]) },
2046 { "r18", offsetof(CPUState, gpr[18]) },
2047 { "r19", offsetof(CPUState, gpr[19]) },
2048 { "r20", offsetof(CPUState, gpr[20]) },
2049 { "r21", offsetof(CPUState, gpr[21]) },
2050 { "r22", offsetof(CPUState, gpr[22]) },
2051 { "r23", offsetof(CPUState, gpr[23]) },
2052 { "r24", offsetof(CPUState, gpr[24]) },
2053 { "r25", offsetof(CPUState, gpr[25]) },
2054 { "r26", offsetof(CPUState, gpr[26]) },
2055 { "r27", offsetof(CPUState, gpr[27]) },
2056 { "r28", offsetof(CPUState, gpr[28]) },
2057 { "r29", offsetof(CPUState, gpr[29]) },
2058 { "r30", offsetof(CPUState, gpr[30]) },
2059 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002060 /* Floating point registers */
2061 { "f0", offsetof(CPUState, fpr[0]) },
2062 { "f1", offsetof(CPUState, fpr[1]) },
2063 { "f2", offsetof(CPUState, fpr[2]) },
2064 { "f3", offsetof(CPUState, fpr[3]) },
2065 { "f4", offsetof(CPUState, fpr[4]) },
2066 { "f5", offsetof(CPUState, fpr[5]) },
2067 { "f6", offsetof(CPUState, fpr[6]) },
2068 { "f7", offsetof(CPUState, fpr[7]) },
2069 { "f8", offsetof(CPUState, fpr[8]) },
2070 { "f9", offsetof(CPUState, fpr[9]) },
2071 { "f10", offsetof(CPUState, fpr[10]) },
2072 { "f11", offsetof(CPUState, fpr[11]) },
2073 { "f12", offsetof(CPUState, fpr[12]) },
2074 { "f13", offsetof(CPUState, fpr[13]) },
2075 { "f14", offsetof(CPUState, fpr[14]) },
2076 { "f15", offsetof(CPUState, fpr[15]) },
2077 { "f16", offsetof(CPUState, fpr[16]) },
2078 { "f17", offsetof(CPUState, fpr[17]) },
2079 { "f18", offsetof(CPUState, fpr[18]) },
2080 { "f19", offsetof(CPUState, fpr[19]) },
2081 { "f20", offsetof(CPUState, fpr[20]) },
2082 { "f21", offsetof(CPUState, fpr[21]) },
2083 { "f22", offsetof(CPUState, fpr[22]) },
2084 { "f23", offsetof(CPUState, fpr[23]) },
2085 { "f24", offsetof(CPUState, fpr[24]) },
2086 { "f25", offsetof(CPUState, fpr[25]) },
2087 { "f26", offsetof(CPUState, fpr[26]) },
2088 { "f27", offsetof(CPUState, fpr[27]) },
2089 { "f28", offsetof(CPUState, fpr[28]) },
2090 { "f29", offsetof(CPUState, fpr[29]) },
2091 { "f30", offsetof(CPUState, fpr[30]) },
2092 { "f31", offsetof(CPUState, fpr[31]) },
2093 { "fpscr", offsetof(CPUState, fpscr) },
2094 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002095 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002096 { "lr", offsetof(CPUState, lr) },
2097 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002098 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002099 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002100 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002101 { "msr", 0, &monitor_get_msr, },
2102 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002103 { "tbu", 0, &monitor_get_tbu, },
2104 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002105#if defined(TARGET_PPC64)
2106 /* Address space register */
2107 { "asr", offsetof(CPUState, asr) },
2108#endif
2109 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002110 { "sdr1", offsetof(CPUState, sdr1) },
2111 { "sr0", offsetof(CPUState, sr[0]) },
2112 { "sr1", offsetof(CPUState, sr[1]) },
2113 { "sr2", offsetof(CPUState, sr[2]) },
2114 { "sr3", offsetof(CPUState, sr[3]) },
2115 { "sr4", offsetof(CPUState, sr[4]) },
2116 { "sr5", offsetof(CPUState, sr[5]) },
2117 { "sr6", offsetof(CPUState, sr[6]) },
2118 { "sr7", offsetof(CPUState, sr[7]) },
2119 { "sr8", offsetof(CPUState, sr[8]) },
2120 { "sr9", offsetof(CPUState, sr[9]) },
2121 { "sr10", offsetof(CPUState, sr[10]) },
2122 { "sr11", offsetof(CPUState, sr[11]) },
2123 { "sr12", offsetof(CPUState, sr[12]) },
2124 { "sr13", offsetof(CPUState, sr[13]) },
2125 { "sr14", offsetof(CPUState, sr[14]) },
2126 { "sr15", offsetof(CPUState, sr[15]) },
2127 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002128#elif defined(TARGET_SPARC)
2129 { "g0", offsetof(CPUState, gregs[0]) },
2130 { "g1", offsetof(CPUState, gregs[1]) },
2131 { "g2", offsetof(CPUState, gregs[2]) },
2132 { "g3", offsetof(CPUState, gregs[3]) },
2133 { "g4", offsetof(CPUState, gregs[4]) },
2134 { "g5", offsetof(CPUState, gregs[5]) },
2135 { "g6", offsetof(CPUState, gregs[6]) },
2136 { "g7", offsetof(CPUState, gregs[7]) },
2137 { "o0", 0, monitor_get_reg },
2138 { "o1", 1, monitor_get_reg },
2139 { "o2", 2, monitor_get_reg },
2140 { "o3", 3, monitor_get_reg },
2141 { "o4", 4, monitor_get_reg },
2142 { "o5", 5, monitor_get_reg },
2143 { "o6", 6, monitor_get_reg },
2144 { "o7", 7, monitor_get_reg },
2145 { "l0", 8, monitor_get_reg },
2146 { "l1", 9, monitor_get_reg },
2147 { "l2", 10, monitor_get_reg },
2148 { "l3", 11, monitor_get_reg },
2149 { "l4", 12, monitor_get_reg },
2150 { "l5", 13, monitor_get_reg },
2151 { "l6", 14, monitor_get_reg },
2152 { "l7", 15, monitor_get_reg },
2153 { "i0", 16, monitor_get_reg },
2154 { "i1", 17, monitor_get_reg },
2155 { "i2", 18, monitor_get_reg },
2156 { "i3", 19, monitor_get_reg },
2157 { "i4", 20, monitor_get_reg },
2158 { "i5", 21, monitor_get_reg },
2159 { "i6", 22, monitor_get_reg },
2160 { "i7", 23, monitor_get_reg },
2161 { "pc", offsetof(CPUState, pc) },
2162 { "npc", offsetof(CPUState, npc) },
2163 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002164#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002165 { "psr", 0, &monitor_get_psr, },
2166 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002167#endif
bellarde95c8d52004-09-30 22:22:08 +00002168 { "tbr", offsetof(CPUState, tbr) },
2169 { "fsr", offsetof(CPUState, fsr) },
2170 { "f0", offsetof(CPUState, fpr[0]) },
2171 { "f1", offsetof(CPUState, fpr[1]) },
2172 { "f2", offsetof(CPUState, fpr[2]) },
2173 { "f3", offsetof(CPUState, fpr[3]) },
2174 { "f4", offsetof(CPUState, fpr[4]) },
2175 { "f5", offsetof(CPUState, fpr[5]) },
2176 { "f6", offsetof(CPUState, fpr[6]) },
2177 { "f7", offsetof(CPUState, fpr[7]) },
2178 { "f8", offsetof(CPUState, fpr[8]) },
2179 { "f9", offsetof(CPUState, fpr[9]) },
2180 { "f10", offsetof(CPUState, fpr[10]) },
2181 { "f11", offsetof(CPUState, fpr[11]) },
2182 { "f12", offsetof(CPUState, fpr[12]) },
2183 { "f13", offsetof(CPUState, fpr[13]) },
2184 { "f14", offsetof(CPUState, fpr[14]) },
2185 { "f15", offsetof(CPUState, fpr[15]) },
2186 { "f16", offsetof(CPUState, fpr[16]) },
2187 { "f17", offsetof(CPUState, fpr[17]) },
2188 { "f18", offsetof(CPUState, fpr[18]) },
2189 { "f19", offsetof(CPUState, fpr[19]) },
2190 { "f20", offsetof(CPUState, fpr[20]) },
2191 { "f21", offsetof(CPUState, fpr[21]) },
2192 { "f22", offsetof(CPUState, fpr[22]) },
2193 { "f23", offsetof(CPUState, fpr[23]) },
2194 { "f24", offsetof(CPUState, fpr[24]) },
2195 { "f25", offsetof(CPUState, fpr[25]) },
2196 { "f26", offsetof(CPUState, fpr[26]) },
2197 { "f27", offsetof(CPUState, fpr[27]) },
2198 { "f28", offsetof(CPUState, fpr[28]) },
2199 { "f29", offsetof(CPUState, fpr[29]) },
2200 { "f30", offsetof(CPUState, fpr[30]) },
2201 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002202#ifdef TARGET_SPARC64
2203 { "f32", offsetof(CPUState, fpr[32]) },
2204 { "f34", offsetof(CPUState, fpr[34]) },
2205 { "f36", offsetof(CPUState, fpr[36]) },
2206 { "f38", offsetof(CPUState, fpr[38]) },
2207 { "f40", offsetof(CPUState, fpr[40]) },
2208 { "f42", offsetof(CPUState, fpr[42]) },
2209 { "f44", offsetof(CPUState, fpr[44]) },
2210 { "f46", offsetof(CPUState, fpr[46]) },
2211 { "f48", offsetof(CPUState, fpr[48]) },
2212 { "f50", offsetof(CPUState, fpr[50]) },
2213 { "f52", offsetof(CPUState, fpr[52]) },
2214 { "f54", offsetof(CPUState, fpr[54]) },
2215 { "f56", offsetof(CPUState, fpr[56]) },
2216 { "f58", offsetof(CPUState, fpr[58]) },
2217 { "f60", offsetof(CPUState, fpr[60]) },
2218 { "f62", offsetof(CPUState, fpr[62]) },
2219 { "asi", offsetof(CPUState, asi) },
2220 { "pstate", offsetof(CPUState, pstate) },
2221 { "cansave", offsetof(CPUState, cansave) },
2222 { "canrestore", offsetof(CPUState, canrestore) },
2223 { "otherwin", offsetof(CPUState, otherwin) },
2224 { "wstate", offsetof(CPUState, wstate) },
2225 { "cleanwin", offsetof(CPUState, cleanwin) },
2226 { "fprs", offsetof(CPUState, fprs) },
2227#endif
bellard9307c4c2004-04-04 12:57:25 +00002228#endif
2229 { NULL },
2230};
2231
aliguori376253e2009-03-05 23:01:23 +00002232static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002233{
aliguori376253e2009-03-05 23:01:23 +00002234 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002235 longjmp(expr_env, 1);
2236}
2237
bellard6a00d602005-11-21 23:25:50 +00002238/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002239static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002240{
blueswir18662d652008-10-02 18:32:44 +00002241 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002242 void *ptr;
2243
bellard9307c4c2004-04-04 12:57:25 +00002244 for(md = monitor_defs; md->name != NULL; md++) {
2245 if (compare_cmd(name, md->name)) {
2246 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002247 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002248 } else {
bellard6a00d602005-11-21 23:25:50 +00002249 CPUState *env = mon_get_cpu();
2250 if (!env)
2251 return -2;
2252 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002253 switch(md->type) {
2254 case MD_I32:
2255 *pval = *(int32_t *)ptr;
2256 break;
2257 case MD_TLONG:
2258 *pval = *(target_long *)ptr;
2259 break;
2260 default:
2261 *pval = 0;
2262 break;
2263 }
bellard9307c4c2004-04-04 12:57:25 +00002264 }
2265 return 0;
2266 }
2267 }
2268 return -1;
2269}
2270
2271static void next(void)
2272{
Blue Swirl660f11b2009-07-31 21:16:51 +00002273 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002274 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002275 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002276 pch++;
2277 }
2278}
2279
aliguori376253e2009-03-05 23:01:23 +00002280static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002281
aliguori376253e2009-03-05 23:01:23 +00002282static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002283{
blueswir1c2efc952007-09-25 17:28:42 +00002284 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002285 char *p;
bellard6a00d602005-11-21 23:25:50 +00002286 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002287
2288 switch(*pch) {
2289 case '+':
2290 next();
aliguori376253e2009-03-05 23:01:23 +00002291 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002292 break;
2293 case '-':
2294 next();
aliguori376253e2009-03-05 23:01:23 +00002295 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002296 break;
2297 case '~':
2298 next();
aliguori376253e2009-03-05 23:01:23 +00002299 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002300 break;
2301 case '(':
2302 next();
aliguori376253e2009-03-05 23:01:23 +00002303 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002304 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002305 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002306 }
2307 next();
2308 break;
bellard81d09122004-07-14 17:21:37 +00002309 case '\'':
2310 pch++;
2311 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002312 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002313 n = *pch;
2314 pch++;
2315 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002316 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002317 next();
2318 break;
bellard9307c4c2004-04-04 12:57:25 +00002319 case '$':
2320 {
2321 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002322 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002323
bellard9307c4c2004-04-04 12:57:25 +00002324 pch++;
2325 q = buf;
2326 while ((*pch >= 'a' && *pch <= 'z') ||
2327 (*pch >= 'A' && *pch <= 'Z') ||
2328 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002329 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002330 if ((q - buf) < sizeof(buf) - 1)
2331 *q++ = *pch;
2332 pch++;
2333 }
blueswir1cd390082008-11-16 13:53:32 +00002334 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002335 pch++;
2336 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002337 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002338 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002339 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002340 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002341 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002342 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002343 }
2344 break;
2345 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002346 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002347 n = 0;
2348 break;
2349 default:
blueswir17743e582007-09-24 18:39:04 +00002350#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002351 n = strtoull(pch, &p, 0);
2352#else
bellard9307c4c2004-04-04 12:57:25 +00002353 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002354#endif
bellard9307c4c2004-04-04 12:57:25 +00002355 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002356 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002357 }
2358 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002359 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002360 pch++;
2361 break;
2362 }
2363 return n;
2364}
2365
2366
aliguori376253e2009-03-05 23:01:23 +00002367static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002368{
blueswir1c2efc952007-09-25 17:28:42 +00002369 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002370 int op;
ths3b46e622007-09-17 08:09:54 +00002371
aliguori376253e2009-03-05 23:01:23 +00002372 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002373 for(;;) {
2374 op = *pch;
2375 if (op != '*' && op != '/' && op != '%')
2376 break;
2377 next();
aliguori376253e2009-03-05 23:01:23 +00002378 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002379 switch(op) {
2380 default:
2381 case '*':
2382 val *= val2;
2383 break;
2384 case '/':
2385 case '%':
ths5fafdf22007-09-16 21:08:06 +00002386 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002387 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002388 if (op == '/')
2389 val /= val2;
2390 else
2391 val %= val2;
2392 break;
2393 }
2394 }
2395 return val;
2396}
2397
aliguori376253e2009-03-05 23:01:23 +00002398static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002399{
blueswir1c2efc952007-09-25 17:28:42 +00002400 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002401 int op;
bellard9307c4c2004-04-04 12:57:25 +00002402
aliguori376253e2009-03-05 23:01:23 +00002403 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002404 for(;;) {
2405 op = *pch;
2406 if (op != '&' && op != '|' && op != '^')
2407 break;
2408 next();
aliguori376253e2009-03-05 23:01:23 +00002409 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002410 switch(op) {
2411 default:
2412 case '&':
2413 val &= val2;
2414 break;
2415 case '|':
2416 val |= val2;
2417 break;
2418 case '^':
2419 val ^= val2;
2420 break;
2421 }
2422 }
2423 return val;
2424}
2425
aliguori376253e2009-03-05 23:01:23 +00002426static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002427{
blueswir1c2efc952007-09-25 17:28:42 +00002428 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002429 int op;
bellard9307c4c2004-04-04 12:57:25 +00002430
aliguori376253e2009-03-05 23:01:23 +00002431 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002432 for(;;) {
2433 op = *pch;
2434 if (op != '+' && op != '-')
2435 break;
2436 next();
aliguori376253e2009-03-05 23:01:23 +00002437 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002438 if (op == '+')
2439 val += val2;
2440 else
2441 val -= val2;
2442 }
2443 return val;
2444}
2445
aliguori376253e2009-03-05 23:01:23 +00002446static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002447{
2448 pch = *pp;
2449 if (setjmp(expr_env)) {
2450 *pp = pch;
2451 return -1;
2452 }
blueswir1cd390082008-11-16 13:53:32 +00002453 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002454 pch++;
aliguori376253e2009-03-05 23:01:23 +00002455 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002456 *pp = pch;
2457 return 0;
2458}
2459
2460static int get_str(char *buf, int buf_size, const char **pp)
2461{
2462 const char *p;
2463 char *q;
2464 int c;
2465
bellard81d09122004-07-14 17:21:37 +00002466 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002467 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002468 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002469 p++;
2470 if (*p == '\0') {
2471 fail:
bellard81d09122004-07-14 17:21:37 +00002472 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002473 *pp = p;
2474 return -1;
2475 }
bellard9307c4c2004-04-04 12:57:25 +00002476 if (*p == '\"') {
2477 p++;
2478 while (*p != '\0' && *p != '\"') {
2479 if (*p == '\\') {
2480 p++;
2481 c = *p++;
2482 switch(c) {
2483 case 'n':
2484 c = '\n';
2485 break;
2486 case 'r':
2487 c = '\r';
2488 break;
2489 case '\\':
2490 case '\'':
2491 case '\"':
2492 break;
2493 default:
2494 qemu_printf("unsupported escape code: '\\%c'\n", c);
2495 goto fail;
2496 }
2497 if ((q - buf) < buf_size - 1) {
2498 *q++ = c;
2499 }
2500 } else {
2501 if ((q - buf) < buf_size - 1) {
2502 *q++ = *p;
2503 }
2504 p++;
2505 }
2506 }
2507 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002508 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002509 goto fail;
2510 }
2511 p++;
2512 } else {
blueswir1cd390082008-11-16 13:53:32 +00002513 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002514 if ((q - buf) < buf_size - 1) {
2515 *q++ = *p;
2516 }
2517 p++;
2518 }
bellard9307c4c2004-04-04 12:57:25 +00002519 }
bellard81d09122004-07-14 17:21:37 +00002520 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002521 *pp = p;
2522 return 0;
2523}
2524
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002525/*
2526 * Store the command-name in cmdname, and return a pointer to
2527 * the remaining of the command string.
2528 */
2529static const char *get_command_name(const char *cmdline,
2530 char *cmdname, size_t nlen)
2531{
2532 size_t len;
2533 const char *p, *pstart;
2534
2535 p = cmdline;
2536 while (qemu_isspace(*p))
2537 p++;
2538 if (*p == '\0')
2539 return NULL;
2540 pstart = p;
2541 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2542 p++;
2543 len = p - pstart;
2544 if (len > nlen - 1)
2545 len = nlen - 1;
2546 memcpy(cmdname, pstart, len);
2547 cmdname[len] = '\0';
2548 return p;
2549}
2550
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002551/**
2552 * Read key of 'type' into 'key' and return the current
2553 * 'type' pointer.
2554 */
2555static char *key_get_info(const char *type, char **key)
2556{
2557 size_t len;
2558 char *p, *str;
2559
2560 if (*type == ',')
2561 type++;
2562
2563 p = strchr(type, ':');
2564 if (!p) {
2565 *key = NULL;
2566 return NULL;
2567 }
2568 len = p - type;
2569
2570 str = qemu_malloc(len + 1);
2571 memcpy(str, type, len);
2572 str[len] = '\0';
2573
2574 *key = str;
2575 return ++p;
2576}
2577
bellard9307c4c2004-04-04 12:57:25 +00002578static int default_fmt_format = 'x';
2579static int default_fmt_size = 4;
2580
2581#define MAX_ARGS 16
2582
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002583static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2584 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002585 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00002586{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002587 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03002588 int c;
aliguori376253e2009-03-05 23:01:23 +00002589 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002590 char cmdname[256];
2591 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002592 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00002593
2594#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002595 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002596#endif
ths3b46e622007-09-17 08:09:54 +00002597
bellard9307c4c2004-04-04 12:57:25 +00002598 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002599 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2600 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002601 return NULL;
ths3b46e622007-09-17 08:09:54 +00002602
bellard9307c4c2004-04-04 12:57:25 +00002603 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002604 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002605 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002606 break;
bellard9dc39cb2004-03-14 21:38:27 +00002607 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002608
2609 if (cmd->name == NULL) {
2610 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002611 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002612 }
bellard9307c4c2004-04-04 12:57:25 +00002613
bellard9307c4c2004-04-04 12:57:25 +00002614 /* parse the parameters */
2615 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002616 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002617 typestr = key_get_info(typestr, &key);
2618 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002619 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002620 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002621 typestr++;
2622 switch(c) {
2623 case 'F':
bellard81d09122004-07-14 17:21:37 +00002624 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002625 case 's':
2626 {
2627 int ret;
ths3b46e622007-09-17 08:09:54 +00002628
blueswir1cd390082008-11-16 13:53:32 +00002629 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002630 p++;
2631 if (*typestr == '?') {
2632 typestr++;
2633 if (*p == '\0') {
2634 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002635 break;
bellard9307c4c2004-04-04 12:57:25 +00002636 }
2637 }
2638 ret = get_str(buf, sizeof(buf), &p);
2639 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002640 switch(c) {
2641 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002642 monitor_printf(mon, "%s: filename expected\n",
2643 cmdname);
bellard81d09122004-07-14 17:21:37 +00002644 break;
2645 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002646 monitor_printf(mon, "%s: block device name expected\n",
2647 cmdname);
bellard81d09122004-07-14 17:21:37 +00002648 break;
2649 default:
aliguori376253e2009-03-05 23:01:23 +00002650 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002651 break;
2652 }
bellard9307c4c2004-04-04 12:57:25 +00002653 goto fail;
2654 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002655 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002656 }
2657 break;
2658 case '/':
2659 {
2660 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002661
blueswir1cd390082008-11-16 13:53:32 +00002662 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002663 p++;
2664 if (*p == '/') {
2665 /* format found */
2666 p++;
2667 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002668 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002669 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002670 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002671 count = count * 10 + (*p - '0');
2672 p++;
2673 }
2674 }
2675 size = -1;
2676 format = -1;
2677 for(;;) {
2678 switch(*p) {
2679 case 'o':
2680 case 'd':
2681 case 'u':
2682 case 'x':
2683 case 'i':
2684 case 'c':
2685 format = *p++;
2686 break;
2687 case 'b':
2688 size = 1;
2689 p++;
2690 break;
2691 case 'h':
2692 size = 2;
2693 p++;
2694 break;
2695 case 'w':
2696 size = 4;
2697 p++;
2698 break;
2699 case 'g':
2700 case 'L':
2701 size = 8;
2702 p++;
2703 break;
2704 default:
2705 goto next;
2706 }
2707 }
2708 next:
blueswir1cd390082008-11-16 13:53:32 +00002709 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002710 monitor_printf(mon, "invalid char in format: '%c'\n",
2711 *p);
bellard9307c4c2004-04-04 12:57:25 +00002712 goto fail;
2713 }
bellard9307c4c2004-04-04 12:57:25 +00002714 if (format < 0)
2715 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002716 if (format != 'i') {
2717 /* for 'i', not specifying a size gives -1 as size */
2718 if (size < 0)
2719 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002720 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002721 }
bellard9307c4c2004-04-04 12:57:25 +00002722 default_fmt_format = format;
2723 } else {
2724 count = 1;
2725 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002726 if (format != 'i') {
2727 size = default_fmt_size;
2728 } else {
2729 size = -1;
2730 }
bellard9307c4c2004-04-04 12:57:25 +00002731 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002732 qdict_put(qdict, "count", qint_from_int(count));
2733 qdict_put(qdict, "format", qint_from_int(format));
2734 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002735 }
2736 break;
2737 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002738 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002739 {
blueswir1c2efc952007-09-25 17:28:42 +00002740 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002741
blueswir1cd390082008-11-16 13:53:32 +00002742 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002743 p++;
bellard34405572004-06-08 00:55:58 +00002744 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002745 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002746 if (*p == '\0') {
2747 typestr++;
2748 break;
2749 }
bellard34405572004-06-08 00:55:58 +00002750 } else {
2751 if (*p == '.') {
2752 p++;
blueswir1cd390082008-11-16 13:53:32 +00002753 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002754 p++;
bellard34405572004-06-08 00:55:58 +00002755 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002756 typestr++;
2757 break;
bellard34405572004-06-08 00:55:58 +00002758 }
2759 }
bellard13224a82006-07-14 22:03:35 +00002760 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002761 }
aliguori376253e2009-03-05 23:01:23 +00002762 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002763 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002764 /* Check if 'i' is greater than 32-bit */
2765 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2766 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
2767 monitor_printf(mon, "integer is for 32-bit values\n");
2768 goto fail;
2769 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002770 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002771 }
2772 break;
2773 case '-':
2774 {
2775 int has_option;
2776 /* option */
ths3b46e622007-09-17 08:09:54 +00002777
bellard9307c4c2004-04-04 12:57:25 +00002778 c = *typestr++;
2779 if (c == '\0')
2780 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002781 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002782 p++;
2783 has_option = 0;
2784 if (*p == '-') {
2785 p++;
2786 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002787 monitor_printf(mon, "%s: unsupported option -%c\n",
2788 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002789 goto fail;
2790 }
2791 p++;
2792 has_option = 1;
2793 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002794 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00002795 }
2796 break;
2797 default:
2798 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002799 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002800 goto fail;
2801 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002802 qemu_free(key);
2803 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002804 }
2805 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002806 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002807 p++;
2808 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002809 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2810 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002811 goto fail;
2812 }
2813
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002814 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002815
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002816fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002817 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002818 return NULL;
2819}
2820
2821static void monitor_handle_command(Monitor *mon, const char *cmdline)
2822{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002823 QDict *qdict;
2824 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002825
2826 qdict = qdict_new();
2827
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03002828 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002829 if (cmd) {
2830 void (*handler)(Monitor *mon, const QDict *qdict);
2831
2832 qemu_errors_to_mon(mon);
2833
2834 handler = cmd->handler;
2835 handler(mon, qdict);
2836
2837 qemu_errors_to_previous();
2838 }
2839
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002840 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002841}
2842
bellard81d09122004-07-14 17:21:37 +00002843static void cmd_completion(const char *name, const char *list)
2844{
2845 const char *p, *pstart;
2846 char cmd[128];
2847 int len;
2848
2849 p = list;
2850 for(;;) {
2851 pstart = p;
2852 p = strchr(p, '|');
2853 if (!p)
2854 p = pstart + strlen(pstart);
2855 len = p - pstart;
2856 if (len > sizeof(cmd) - 2)
2857 len = sizeof(cmd) - 2;
2858 memcpy(cmd, pstart, len);
2859 cmd[len] = '\0';
2860 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002861 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002862 }
2863 if (*p == '\0')
2864 break;
2865 p++;
2866 }
2867}
2868
2869static void file_completion(const char *input)
2870{
2871 DIR *ffs;
2872 struct dirent *d;
2873 char path[1024];
2874 char file[1024], file_prefix[1024];
2875 int input_path_len;
2876 const char *p;
2877
ths5fafdf22007-09-16 21:08:06 +00002878 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002879 if (!p) {
2880 input_path_len = 0;
2881 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002882 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002883 } else {
2884 input_path_len = p - input + 1;
2885 memcpy(path, input, input_path_len);
2886 if (input_path_len > sizeof(path) - 1)
2887 input_path_len = sizeof(path) - 1;
2888 path[input_path_len] = '\0';
2889 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2890 }
2891#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002892 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2893 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002894#endif
2895 ffs = opendir(path);
2896 if (!ffs)
2897 return;
2898 for(;;) {
2899 struct stat sb;
2900 d = readdir(ffs);
2901 if (!d)
2902 break;
2903 if (strstart(d->d_name, file_prefix, NULL)) {
2904 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002905 if (input_path_len < sizeof(file))
2906 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2907 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002908 /* stat the file to find out if it's a directory.
2909 * In that case add a slash to speed up typing long paths
2910 */
2911 stat(file, &sb);
2912 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002913 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00002914 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002915 }
2916 }
2917 closedir(ffs);
2918}
2919
aliguori51de9762009-03-05 23:00:43 +00002920static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002921{
aliguori51de9762009-03-05 23:00:43 +00002922 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002923 const char *input = opaque;
2924
2925 if (input[0] == '\0' ||
2926 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00002927 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00002928 }
2929}
2930
2931/* NOTE: this parser is an approximate form of the real command parser */
2932static void parse_cmdline(const char *cmdline,
2933 int *pnb_args, char **args)
2934{
2935 const char *p;
2936 int nb_args, ret;
2937 char buf[1024];
2938
2939 p = cmdline;
2940 nb_args = 0;
2941 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002942 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002943 p++;
2944 if (*p == '\0')
2945 break;
2946 if (nb_args >= MAX_ARGS)
2947 break;
2948 ret = get_str(buf, sizeof(buf), &p);
2949 args[nb_args] = qemu_strdup(buf);
2950 nb_args++;
2951 if (ret < 0)
2952 break;
2953 }
2954 *pnb_args = nb_args;
2955}
2956
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002957static const char *next_arg_type(const char *typestr)
2958{
2959 const char *p = strchr(typestr, ':');
2960 return (p != NULL ? ++p : typestr);
2961}
2962
aliguori4c36ba32009-03-05 23:01:37 +00002963static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002964{
2965 const char *cmdname;
2966 char *args[MAX_ARGS];
2967 int nb_args, i, len;
2968 const char *ptype, *str;
aliguori376253e2009-03-05 23:01:23 +00002969 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002970 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002971
2972 parse_cmdline(cmdline, &nb_args, args);
2973#ifdef DEBUG_COMPLETION
2974 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00002975 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00002976 }
2977#endif
2978
2979 /* if the line ends with a space, it means we want to complete the
2980 next arg */
2981 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002982 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002983 if (nb_args >= MAX_ARGS)
2984 return;
2985 args[nb_args++] = qemu_strdup("");
2986 }
2987 if (nb_args <= 1) {
2988 /* command completion */
2989 if (nb_args == 0)
2990 cmdname = "";
2991 else
2992 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00002993 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00002994 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00002995 cmd_completion(cmdname, cmd->name);
2996 }
2997 } else {
2998 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002999 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003000 if (compare_cmd(args[0], cmd->name))
3001 goto found;
3002 }
3003 return;
3004 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003005 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003006 for(i = 0; i < nb_args - 2; i++) {
3007 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003008 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003009 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003010 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003011 }
3012 }
3013 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003014 if (*ptype == '-' && ptype[1] != '\0') {
3015 ptype += 2;
3016 }
bellard81d09122004-07-14 17:21:37 +00003017 switch(*ptype) {
3018 case 'F':
3019 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003020 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003021 file_completion(str);
3022 break;
3023 case 'B':
3024 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003025 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003026 bdrv_iterate(block_completion_it, (void *)str);
3027 break;
bellard7fe48482004-10-09 18:08:01 +00003028 case 's':
3029 /* XXX: more generic ? */
3030 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003031 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003032 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3033 cmd_completion(str, cmd->name);
3034 }
bellard64866c32006-05-07 18:03:31 +00003035 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003036 char *sep = strrchr(str, '-');
3037 if (sep)
3038 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003039 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003040 for(key = key_defs; key->name != NULL; key++) {
3041 cmd_completion(str, key->name);
3042 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003043 } else if (!strcmp(cmd->name, "help|?")) {
3044 readline_set_completion_index(cur_mon->rs, strlen(str));
3045 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3046 cmd_completion(str, cmd->name);
3047 }
bellard7fe48482004-10-09 18:08:01 +00003048 }
3049 break;
bellard81d09122004-07-14 17:21:37 +00003050 default:
3051 break;
3052 }
3053 }
3054 for(i = 0; i < nb_args; i++)
3055 qemu_free(args[i]);
3056}
3057
aliguori731b0362009-03-05 23:01:42 +00003058static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003059{
aliguori731b0362009-03-05 23:01:42 +00003060 Monitor *mon = opaque;
3061
3062 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003063}
3064
aliguori731b0362009-03-05 23:01:42 +00003065static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003066{
aliguori731b0362009-03-05 23:01:42 +00003067 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003068 int i;
aliguori376253e2009-03-05 23:01:23 +00003069
aliguori731b0362009-03-05 23:01:42 +00003070 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003071
aliguoricde76ee2009-03-05 23:01:51 +00003072 if (cur_mon->rs) {
3073 for (i = 0; i < size; i++)
3074 readline_handle_byte(cur_mon->rs, buf[i]);
3075 } else {
3076 if (size == 0 || buf[size - 1] != 0)
3077 monitor_printf(cur_mon, "corrupted command\n");
3078 else
3079 monitor_handle_command(cur_mon, (char *)buf);
3080 }
aliguori731b0362009-03-05 23:01:42 +00003081
3082 cur_mon = old_mon;
3083}
aliguorid8f44602008-10-06 13:52:44 +00003084
aliguori376253e2009-03-05 23:01:23 +00003085static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003086{
aliguori731b0362009-03-05 23:01:42 +00003087 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003088 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003089 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003090}
3091
aliguoricde76ee2009-03-05 23:01:51 +00003092int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003093{
aliguoricde76ee2009-03-05 23:01:51 +00003094 if (!mon->rs)
3095 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003096 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003097 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003098}
3099
aliguori376253e2009-03-05 23:01:23 +00003100void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003101{
aliguoricde76ee2009-03-05 23:01:51 +00003102 if (!mon->rs)
3103 return;
aliguori731b0362009-03-05 23:01:42 +00003104 if (--mon->suspend_cnt == 0)
3105 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003106}
3107
aliguori731b0362009-03-05 23:01:42 +00003108static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003109{
aliguori376253e2009-03-05 23:01:23 +00003110 Monitor *mon = opaque;
3111
aliguori2724b182009-03-05 23:01:47 +00003112 switch (event) {
3113 case CHR_EVENT_MUX_IN:
3114 readline_restart(mon->rs);
3115 monitor_resume(mon);
3116 monitor_flush(mon);
3117 break;
ths86e94de2007-01-05 22:01:59 +00003118
aliguori2724b182009-03-05 23:01:47 +00003119 case CHR_EVENT_MUX_OUT:
3120 if (mon->suspend_cnt == 0)
3121 monitor_printf(mon, "\n");
3122 monitor_flush(mon);
3123 monitor_suspend(mon);
3124 break;
3125
3126 case CHR_EVENT_RESET:
3127 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3128 "information\n", QEMU_VERSION);
3129 if (mon->chr->focus == 0)
3130 readline_show_prompt(mon->rs);
3131 break;
3132 }
ths86e94de2007-01-05 22:01:59 +00003133}
3134
aliguori76655d62009-03-06 20:27:37 +00003135
3136/*
3137 * Local variables:
3138 * c-indent-level: 4
3139 * c-basic-offset: 4
3140 * tab-width: 8
3141 * End:
3142 */
3143
aliguori731b0362009-03-05 23:01:42 +00003144void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003145{
aliguori731b0362009-03-05 23:01:42 +00003146 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003147 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003148
3149 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003150 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003151 is_first_init = 0;
3152 }
aliguori87127162009-03-05 23:01:29 +00003153
3154 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003155
aliguori87127162009-03-05 23:01:29 +00003156 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003157 mon->flags = flags;
aliguori2724b182009-03-05 23:01:47 +00003158 if (mon->chr->focus != 0)
3159 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
aliguoricde76ee2009-03-05 23:01:51 +00003160 if (flags & MONITOR_USE_READLINE) {
3161 mon->rs = readline_init(mon, monitor_find_completion);
3162 monitor_read_command(mon, 0);
3163 }
aliguori87127162009-03-05 23:01:29 +00003164
aliguori731b0362009-03-05 23:01:42 +00003165 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3166 mon);
aliguori87127162009-03-05 23:01:29 +00003167
3168 LIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003169 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003170 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003171}
3172
aliguori376253e2009-03-05 23:01:23 +00003173static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003174{
aliguoribb5fc202009-03-05 23:01:15 +00003175 BlockDriverState *bs = opaque;
3176 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003177
aliguoribb5fc202009-03-05 23:01:15 +00003178 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003179 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003180 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003181 }
aliguori731b0362009-03-05 23:01:42 +00003182 if (mon->password_completion_cb)
3183 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003184
aliguori731b0362009-03-05 23:01:42 +00003185 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003186}
aliguoric0f4ce72009-03-05 23:01:01 +00003187
aliguori376253e2009-03-05 23:01:23 +00003188void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003189 BlockDriverCompletionFunc *completion_cb,
3190 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003191{
aliguoricde76ee2009-03-05 23:01:51 +00003192 int err;
3193
aliguoribb5fc202009-03-05 23:01:15 +00003194 if (!bdrv_key_required(bs)) {
3195 if (completion_cb)
3196 completion_cb(opaque, 0);
3197 return;
3198 }
aliguoric0f4ce72009-03-05 23:01:01 +00003199
aliguori376253e2009-03-05 23:01:23 +00003200 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3201 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003202
aliguori731b0362009-03-05 23:01:42 +00003203 mon->password_completion_cb = completion_cb;
3204 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003205
aliguoricde76ee2009-03-05 23:01:51 +00003206 err = monitor_read_password(mon, bdrv_password_cb, bs);
3207
3208 if (err && completion_cb)
3209 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003210}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003211
3212typedef struct QemuErrorSink QemuErrorSink;
3213struct QemuErrorSink {
3214 enum {
3215 ERR_SINK_FILE,
3216 ERR_SINK_MONITOR,
3217 } dest;
3218 union {
3219 FILE *fp;
3220 Monitor *mon;
3221 };
3222 QemuErrorSink *previous;
3223};
3224
Blue Swirl528e93a2009-08-31 15:14:40 +00003225static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003226
3227void qemu_errors_to_file(FILE *fp)
3228{
3229 QemuErrorSink *sink;
3230
3231 sink = qemu_mallocz(sizeof(*sink));
3232 sink->dest = ERR_SINK_FILE;
3233 sink->fp = fp;
3234 sink->previous = qemu_error_sink;
3235 qemu_error_sink = sink;
3236}
3237
3238void qemu_errors_to_mon(Monitor *mon)
3239{
3240 QemuErrorSink *sink;
3241
3242 sink = qemu_mallocz(sizeof(*sink));
3243 sink->dest = ERR_SINK_MONITOR;
3244 sink->mon = mon;
3245 sink->previous = qemu_error_sink;
3246 qemu_error_sink = sink;
3247}
3248
3249void qemu_errors_to_previous(void)
3250{
3251 QemuErrorSink *sink;
3252
3253 assert(qemu_error_sink != NULL);
3254 sink = qemu_error_sink;
3255 qemu_error_sink = sink->previous;
3256 qemu_free(sink);
3257}
3258
3259void qemu_error(const char *fmt, ...)
3260{
3261 va_list args;
3262
3263 assert(qemu_error_sink != NULL);
3264 switch (qemu_error_sink->dest) {
3265 case ERR_SINK_FILE:
3266 va_start(args, fmt);
3267 vfprintf(qemu_error_sink->fp, fmt, args);
3268 va_end(args);
3269 break;
3270 case ERR_SINK_MONITOR:
3271 va_start(args, fmt);
3272 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3273 va_end(args);
3274 break;
3275 }
3276}