blob: f79b83355aa60bc8039faa7a28a8d4f2d6370814 [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"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020032#include "hw/loader.h"
pbrook87ecb682007-11-17 17:14:51 +000033#include "gdbstub.h"
34#include "net.h"
35#include "qemu-char.h"
36#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000037#include "monitor.h"
38#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000039#include "console.h"
40#include "block.h"
41#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000042#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000043#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000044#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000045#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000046#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000047#include "acl.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030048#include "qint.h"
49#include "qdict.h"
50#include "qstring.h"
ths6a5bd302007-12-03 17:05:38 +000051
bellard9dc39cb2004-03-14 21:38:27 +000052//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000053//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000054
bellard9307c4c2004-04-04 12:57:25 +000055/*
56 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000057 *
bellard9307c4c2004-04-04 12:57:25 +000058 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000059 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000060 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000061 * 'i' 32 bit integer
62 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000063 * '/' optional gdb-like print format (like "/10x")
64 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030065 * '?' optional type (for all types, except '/')
66 * '.' other form of optional type (for 'i' and 'l')
67 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000068 *
69 */
70
Anthony Liguoric227f092009-10-01 16:12:16 -050071typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000072 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000073 const char *args_type;
blueswir1a5f1b962008-08-17 20:21:51 +000074 void *handler;
bellard9dc39cb2004-03-14 21:38:27 +000075 const char *params;
76 const char *help;
Anthony Liguoric227f092009-10-01 16:12:16 -050077} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000078
Mark McLoughlinf07918f2009-07-22 09:11:40 +010079/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -050080typedef struct mon_fd_t mon_fd_t;
81struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +010082 char *name;
83 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -050084 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010085};
86
aliguori87127162009-03-05 23:01:29 +000087struct Monitor {
88 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +020089 int mux_out;
90 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +000091 int flags;
92 int suspend_cnt;
93 uint8_t outbuf[1024];
94 int outbuf_index;
95 ReadLineState *rs;
96 CPUState *mon_cpu;
97 BlockDriverCompletionFunc *password_completion_cb;
98 void *password_opaque;
Anthony Liguoric227f092009-10-01 16:12:16 -050099 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000100 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000101};
102
Blue Swirl72cf2d42009-09-12 07:36:22 +0000103static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000104
Anthony Liguoric227f092009-10-01 16:12:16 -0500105static const mon_cmd_t mon_cmds[];
106static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000107
aliguori87127162009-03-05 23:01:29 +0000108Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000109
aliguori731b0362009-03-05 23:01:42 +0000110static void monitor_command_cb(Monitor *mon, const char *cmdline,
111 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000112
aliguori731b0362009-03-05 23:01:42 +0000113static void monitor_read_command(Monitor *mon, int show_prompt)
114{
115 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
116 if (show_prompt)
117 readline_show_prompt(mon->rs);
118}
bellard6a00d602005-11-21 23:25:50 +0000119
aliguoricde76ee2009-03-05 23:01:51 +0000120static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
121 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000122{
aliguoricde76ee2009-03-05 23:01:51 +0000123 if (mon->rs) {
124 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
125 /* prompt is printed on return from the command handler */
126 return 0;
127 } else {
128 monitor_printf(mon, "terminal does not support password prompting\n");
129 return -ENOTTY;
130 }
aliguoribb5fc202009-03-05 23:01:15 +0000131}
132
aliguori376253e2009-03-05 23:01:23 +0000133void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000134{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200135 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000136 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
137 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000138 }
139}
140
141/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000142static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000143{
ths60fe76f2007-12-16 03:02:09 +0000144 char c;
aliguori731b0362009-03-05 23:01:42 +0000145
146 if (!mon)
147 return;
148
bellard7e2515e2004-08-01 21:52:19 +0000149 for(;;) {
150 c = *str++;
151 if (c == '\0')
152 break;
bellard7ba12602006-07-14 20:26:42 +0000153 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000154 mon->outbuf[mon->outbuf_index++] = '\r';
155 mon->outbuf[mon->outbuf_index++] = c;
156 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
157 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000158 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000159 }
160}
161
aliguori376253e2009-03-05 23:01:23 +0000162void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000163{
164 char buf[4096];
165 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000166 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000167}
168
aliguori376253e2009-03-05 23:01:23 +0000169void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000170{
171 va_list ap;
172 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000173 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000174 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000175}
176
aliguori376253e2009-03-05 23:01:23 +0000177void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000178{
179 int i;
180
181 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000182 switch (filename[i]) {
183 case ' ':
184 case '"':
185 case '\\':
186 monitor_printf(mon, "\\%c", filename[i]);
187 break;
188 case '\t':
189 monitor_printf(mon, "\\t");
190 break;
191 case '\r':
192 monitor_printf(mon, "\\r");
193 break;
194 case '\n':
195 monitor_printf(mon, "\\n");
196 break;
197 default:
198 monitor_printf(mon, "%c", filename[i]);
199 break;
200 }
thsfef30742006-12-22 14:11:32 +0000201 }
202}
203
bellard7fe48482004-10-09 18:08:01 +0000204static int monitor_fprintf(FILE *stream, const char *fmt, ...)
205{
206 va_list ap;
207 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000208 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000209 va_end(ap);
210 return 0;
211}
212
bellard9dc39cb2004-03-14 21:38:27 +0000213static int compare_cmd(const char *name, const char *list)
214{
215 const char *p, *pstart;
216 int len;
217 len = strlen(name);
218 p = list;
219 for(;;) {
220 pstart = p;
221 p = strchr(p, '|');
222 if (!p)
223 p = pstart + strlen(pstart);
224 if ((p - pstart) == len && !memcmp(pstart, name, len))
225 return 1;
226 if (*p == '\0')
227 break;
228 p++;
229 }
230 return 0;
231}
232
Anthony Liguoric227f092009-10-01 16:12:16 -0500233static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000234 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000235{
Anthony Liguoric227f092009-10-01 16:12:16 -0500236 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000237
238 for(cmd = cmds; cmd->name != NULL; cmd++) {
239 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000240 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
241 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000242 }
243}
244
aliguori376253e2009-03-05 23:01:23 +0000245static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000246{
247 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000248 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000249 } else {
aliguori376253e2009-03-05 23:01:23 +0000250 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000251 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000252 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000253 monitor_printf(mon, "Log items (comma separated):\n");
254 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000255 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000256 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000257 }
258 }
bellard9dc39cb2004-03-14 21:38:27 +0000259 }
260}
261
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300262static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300263{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300264 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300265}
266
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300267static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000268{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200269 int all_devices;
270 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300271 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000272
bellard7954c732006-08-01 15:52:40 +0000273 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000274 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200275 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300276 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200277 continue;
278 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000279 }
280}
281
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300282static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000283{
Anthony Liguoric227f092009-10-01 16:12:16 -0500284 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300285 const char *item = qdict_get_try_str(qdict, "item");
aliguori376253e2009-03-05 23:01:23 +0000286 void (*handler)(Monitor *);
bellard9dc39cb2004-03-14 21:38:27 +0000287
bellard9307c4c2004-04-04 12:57:25 +0000288 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000289 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000290 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000291 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000292 goto found;
293 }
294 help:
aliguori376253e2009-03-05 23:01:23 +0000295 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000296 return;
297 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000298 handler = cmd->handler;
aliguori376253e2009-03-05 23:01:23 +0000299 handler(mon);
bellard9dc39cb2004-03-14 21:38:27 +0000300}
301
aliguori376253e2009-03-05 23:01:23 +0000302static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000303{
pbrook4a19f1e2009-04-07 23:17:49 +0000304 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000305}
306
aliguori376253e2009-03-05 23:01:23 +0000307static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000308{
309 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000310 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000311}
312
aurel32bf4f74c2008-12-18 22:42:34 +0000313#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000314static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000315{
aliguori376253e2009-03-05 23:01:23 +0000316 monitor_printf(mon, "HPET is %s by QEMU\n",
317 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000318}
aurel32bf4f74c2008-12-18 22:42:34 +0000319#endif
aliguori16b29ae2008-12-17 23:28:44 +0000320
aliguori376253e2009-03-05 23:01:23 +0000321static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000322{
aliguori376253e2009-03-05 23:01:23 +0000323 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
324 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
325 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
326 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
327 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000328}
329
bellard6a00d602005-11-21 23:25:50 +0000330/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000331static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000332{
333 CPUState *env;
334
335 for(env = first_cpu; env != NULL; env = env->next_cpu) {
336 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000337 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000338 return 0;
339 }
340 }
341 return -1;
342}
343
pbrook9596ebb2007-11-18 01:44:38 +0000344static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000345{
aliguori731b0362009-03-05 23:01:42 +0000346 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000347 mon_set_cpu(0);
348 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300349 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000350 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000351}
352
aliguori376253e2009-03-05 23:01:23 +0000353static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000354{
bellard6a00d602005-11-21 23:25:50 +0000355 CPUState *env;
356 env = mon_get_cpu();
357 if (!env)
358 return;
bellard9307c4c2004-04-04 12:57:25 +0000359#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000360 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000361 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000362#else
aliguori376253e2009-03-05 23:01:23 +0000363 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000364 0);
bellard9307c4c2004-04-04 12:57:25 +0000365#endif
366}
367
aliguori376253e2009-03-05 23:01:23 +0000368static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000369{
370 CPUState *env;
371
372 /* just to set the default cpu if not already done */
373 mon_get_cpu();
374
375 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Avi Kivity4c0960c2009-08-17 23:19:53 +0300376 cpu_synchronize_state(env);
aliguori376253e2009-03-05 23:01:23 +0000377 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000378 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000379 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000380#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000381 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
382 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000383#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000384 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000385#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000386 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
387 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000388#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000389 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000390#endif
thsead93602007-09-06 00:18:15 +0000391 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000392 monitor_printf(mon, " (halted)");
393 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000394 }
395}
396
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300397static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000398{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300399 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000400 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000401 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000402}
403
aliguori376253e2009-03-05 23:01:23 +0000404static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000405{
aliguori376253e2009-03-05 23:01:23 +0000406 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000407}
408
aliguori376253e2009-03-05 23:01:23 +0000409static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000410{
411 int i;
bellard7e2515e2004-08-01 21:52:19 +0000412 const char *str;
ths3b46e622007-09-17 08:09:54 +0000413
aliguoricde76ee2009-03-05 23:01:51 +0000414 if (!mon->rs)
415 return;
bellard7e2515e2004-08-01 21:52:19 +0000416 i = 0;
417 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000418 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000419 if (!str)
420 break;
aliguori376253e2009-03-05 23:01:23 +0000421 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000422 i++;
bellardaa455482004-04-04 13:07:25 +0000423 }
424}
425
j_mayer76a66252007-03-07 08:32:30 +0000426#if defined(TARGET_PPC)
427/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000428static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000429{
430 CPUState *env;
431
432 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000433 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000434}
435#endif
436
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300437static void do_quit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000438{
439 exit(0);
440}
441
aliguori376253e2009-03-05 23:01:23 +0000442static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000443{
444 if (bdrv_is_inserted(bs)) {
445 if (!force) {
446 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000447 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000448 return -1;
449 }
450 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000451 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000452 return -1;
453 }
454 }
455 bdrv_close(bs);
456 }
457 return 0;
458}
459
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300460static void do_eject(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000461{
462 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300463 int force = qdict_get_int(qdict, "force");
464 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000465
bellard9307c4c2004-04-04 12:57:25 +0000466 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000467 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000468 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000469 return;
470 }
aliguori376253e2009-03-05 23:01:23 +0000471 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000472}
473
aliguori376253e2009-03-05 23:01:23 +0000474static void do_change_block(Monitor *mon, const char *device,
475 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000476{
477 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000478 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000479
bellard9307c4c2004-04-04 12:57:25 +0000480 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000481 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000482 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000483 return;
484 }
aurel322ecea9b2008-06-18 22:10:01 +0000485 if (fmt) {
486 drv = bdrv_find_format(fmt);
487 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000488 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000489 return;
490 }
491 }
aliguori376253e2009-03-05 23:01:23 +0000492 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000493 return;
aurel322ecea9b2008-06-18 22:10:01 +0000494 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000495 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000496}
497
aliguori376253e2009-03-05 23:01:23 +0000498static void change_vnc_password_cb(Monitor *mon, const char *password,
499 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000500{
501 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000502 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000503
aliguori731b0362009-03-05 23:01:42 +0000504 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000505}
506
aliguori376253e2009-03-05 23:01:23 +0000507static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000508{
ths70848512007-08-25 01:37:05 +0000509 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000510 strcmp(target, "password") == 0) {
511 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000512 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000513 strncpy(password, arg, sizeof(password));
514 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000515 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000516 } else {
aliguori376253e2009-03-05 23:01:23 +0000517 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000518 }
ths70848512007-08-25 01:37:05 +0000519 } else {
aliguori28a76be2009-03-06 20:27:40 +0000520 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000521 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000522 }
thse25a5822007-08-25 01:36:20 +0000523}
524
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300525static void do_change(Monitor *mon, const QDict *qdict)
thse25a5822007-08-25 01:36:20 +0000526{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300527 const char *device = qdict_get_str(qdict, "device");
528 const char *target = qdict_get_str(qdict, "target");
529 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000530 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000531 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000532 } else {
aliguori28a76be2009-03-06 20:27:40 +0000533 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000534 }
535}
536
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300537static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000538{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300539 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000540}
541
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300542static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000543{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300544 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000545}
546
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300547static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000548{
549 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300550 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000551
bellard9307c4c2004-04-04 12:57:25 +0000552 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000553 mask = 0;
554 } else {
bellard9307c4c2004-04-04 12:57:25 +0000555 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000556 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000557 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000558 return;
559 }
560 }
561 cpu_set_log(mask);
562}
563
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300564static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000565{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300566 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000567 if (!option || !strcmp(option, "on")) {
568 singlestep = 1;
569 } else if (!strcmp(option, "off")) {
570 singlestep = 0;
571 } else {
572 monitor_printf(mon, "unexpected option %s\n", option);
573 }
574}
575
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300576static void do_stop(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000577{
578 vm_stop(EXCP_INTERRUPT);
579}
580
aliguoribb5fc202009-03-05 23:01:15 +0000581static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000582
aliguori376253e2009-03-05 23:01:23 +0000583struct bdrv_iterate_context {
584 Monitor *mon;
585 int err;
586};
aliguoric0f4ce72009-03-05 23:01:01 +0000587
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300588static void do_cont(Monitor *mon, const QDict *qdict)
aliguori376253e2009-03-05 23:01:23 +0000589{
590 struct bdrv_iterate_context context = { mon, 0 };
591
592 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000593 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000594 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000595 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000596}
597
aliguoribb5fc202009-03-05 23:01:15 +0000598static void bdrv_key_cb(void *opaque, int err)
599{
aliguori376253e2009-03-05 23:01:23 +0000600 Monitor *mon = opaque;
601
aliguoribb5fc202009-03-05 23:01:15 +0000602 /* another key was set successfully, retry to continue */
603 if (!err)
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300604 do_cont(mon, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000605}
606
607static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
608{
aliguori376253e2009-03-05 23:01:23 +0000609 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000610
aliguori376253e2009-03-05 23:01:23 +0000611 if (!context->err && bdrv_key_required(bs)) {
612 context->err = -EBUSY;
613 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
614 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000615 }
616}
617
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300618static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000619{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300620 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000621 if (!device)
622 device = "tcp::" DEFAULT_GDBSTUB_PORT;
623 if (gdbserver_start(device) < 0) {
624 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
625 device);
626 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000627 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000628 } else {
aliguori59030a82009-04-05 18:43:41 +0000629 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
630 device);
bellard8a7ddc32004-03-31 19:00:16 +0000631 }
632}
633
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300634static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100635{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300636 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100637 if (select_watchdog_action(action) == -1) {
638 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
639 }
640}
641
aliguori376253e2009-03-05 23:01:23 +0000642static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000643{
aliguori376253e2009-03-05 23:01:23 +0000644 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000645 switch(c) {
646 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000647 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000648 break;
649 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000650 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000651 break;
652 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000653 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000654 break;
655 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000656 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000657 break;
658 default:
659 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000660 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000661 } else {
aliguori376253e2009-03-05 23:01:23 +0000662 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000663 }
664 break;
665 }
aliguori376253e2009-03-05 23:01:23 +0000666 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000667}
668
aliguori376253e2009-03-05 23:01:23 +0000669static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -0500670 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000671{
bellard6a00d602005-11-21 23:25:50 +0000672 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000673 int nb_per_line, l, line_size, i, max_digits, len;
674 uint8_t buf[16];
675 uint64_t v;
676
677 if (format == 'i') {
678 int flags;
679 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000680 env = mon_get_cpu();
681 if (!env && !is_physical)
682 return;
bellard9307c4c2004-04-04 12:57:25 +0000683#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000684 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000685 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000686 } else if (wsize == 4) {
687 flags = 0;
688 } else {
bellard6a15fd12006-04-12 21:07:07 +0000689 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000690 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000691 if (env) {
692#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000693 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000694 (env->segs[R_CS].flags & DESC_L_MASK))
695 flags = 2;
696 else
697#endif
698 if (!(env->segs[R_CS].flags & DESC_B_MASK))
699 flags = 1;
700 }
bellard4c27ba22004-04-25 18:05:08 +0000701 }
702#endif
aliguori376253e2009-03-05 23:01:23 +0000703 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000704 return;
705 }
706
707 len = wsize * count;
708 if (wsize == 1)
709 line_size = 8;
710 else
711 line_size = 16;
712 nb_per_line = line_size / wsize;
713 max_digits = 0;
714
715 switch(format) {
716 case 'o':
717 max_digits = (wsize * 8 + 2) / 3;
718 break;
719 default:
720 case 'x':
721 max_digits = (wsize * 8) / 4;
722 break;
723 case 'u':
724 case 'd':
725 max_digits = (wsize * 8 * 10 + 32) / 33;
726 break;
727 case 'c':
728 wsize = 1;
729 break;
730 }
731
732 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000733 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000734 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000735 else
aliguori376253e2009-03-05 23:01:23 +0000736 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000737 l = len;
738 if (l > line_size)
739 l = line_size;
740 if (is_physical) {
741 cpu_physical_memory_rw(addr, buf, l, 0);
742 } else {
bellard6a00d602005-11-21 23:25:50 +0000743 env = mon_get_cpu();
744 if (!env)
745 break;
aliguoric8f79b62008-08-18 14:00:20 +0000746 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000747 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000748 break;
749 }
bellard9307c4c2004-04-04 12:57:25 +0000750 }
ths5fafdf22007-09-16 21:08:06 +0000751 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000752 while (i < l) {
753 switch(wsize) {
754 default:
755 case 1:
756 v = ldub_raw(buf + i);
757 break;
758 case 2:
759 v = lduw_raw(buf + i);
760 break;
761 case 4:
bellard92a31b12005-02-10 22:00:52 +0000762 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000763 break;
764 case 8:
765 v = ldq_raw(buf + i);
766 break;
767 }
aliguori376253e2009-03-05 23:01:23 +0000768 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000769 switch(format) {
770 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000771 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000772 break;
773 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000774 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000775 break;
776 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000777 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000778 break;
779 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000780 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000781 break;
782 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000783 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000784 break;
785 }
786 i += wsize;
787 }
aliguori376253e2009-03-05 23:01:23 +0000788 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000789 addr += l;
790 len -= l;
791 }
792}
793
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300794static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000795{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300796 int count = qdict_get_int(qdict, "count");
797 int format = qdict_get_int(qdict, "format");
798 int size = qdict_get_int(qdict, "size");
799 target_long addr = qdict_get_int(qdict, "addr");
800
aliguori376253e2009-03-05 23:01:23 +0000801 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000802}
803
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300804static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000805{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300806 int count = qdict_get_int(qdict, "count");
807 int format = qdict_get_int(qdict, "format");
808 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -0500809 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300810
aliguori376253e2009-03-05 23:01:23 +0000811 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000812}
813
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300814static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000815{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300816 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -0500817 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300818
blueswir17743e582007-09-24 18:39:04 +0000819#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000820 switch(format) {
821 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000822 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000823 break;
824 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000825 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000826 break;
827 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000828 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000829 break;
830 default:
831 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000832 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000833 break;
834 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000835 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000836 break;
837 }
bellard92a31b12005-02-10 22:00:52 +0000838#else
839 switch(format) {
840 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000841 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000842 break;
843 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000844 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000845 break;
846 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000847 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000848 break;
849 default:
850 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000851 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000852 break;
853 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000854 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000855 break;
856 }
857#endif
aliguori376253e2009-03-05 23:01:23 +0000858 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000859}
860
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300861static void do_memory_save(Monitor *mon, const QDict *qdict)
bellardb371dc52007-01-03 15:20:39 +0000862{
863 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300864 uint32_t size = qdict_get_int(qdict, "size");
865 const char *filename = qdict_get_str(qdict, "filename");
866 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +0000867 uint32_t l;
868 CPUState *env;
869 uint8_t buf[1024];
870
871 env = mon_get_cpu();
872 if (!env)
873 return;
874
875 f = fopen(filename, "wb");
876 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000877 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000878 return;
879 }
880 while (size != 0) {
881 l = sizeof(buf);
882 if (l > size)
883 l = size;
884 cpu_memory_rw_debug(env, addr, buf, l, 0);
885 fwrite(buf, 1, l, f);
886 addr += l;
887 size -= l;
888 }
889 fclose(f);
890}
891
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300892static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
aurel32a8bdf7a2008-04-11 21:36:14 +0000893{
894 FILE *f;
895 uint32_t l;
896 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300897 uint32_t size = qdict_get_int(qdict, "size");
898 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -0500899 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +0000900
901 f = fopen(filename, "wb");
902 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000903 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000904 return;
905 }
906 while (size != 0) {
907 l = sizeof(buf);
908 if (l > size)
909 l = size;
910 cpu_physical_memory_rw(addr, buf, l, 0);
911 fwrite(buf, 1, l, f);
912 fflush(f);
913 addr += l;
914 size -= l;
915 }
916 fclose(f);
917}
918
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300919static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000920{
921 uint32_t addr;
922 uint8_t buf[1];
923 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300924 uint32_t start = qdict_get_int(qdict, "start");
925 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000926
927 sum = 0;
928 for(addr = start; addr < (start + size); addr++) {
929 cpu_physical_memory_rw(addr, buf, 1, 0);
930 /* BSD sum algorithm ('sum' Unix command) */
931 sum = (sum >> 1) | (sum << 15);
932 sum += buf[0];
933 }
aliguori376253e2009-03-05 23:01:23 +0000934 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000935}
936
bellarda3a91a32004-06-04 11:06:21 +0000937typedef struct {
938 int keycode;
939 const char *name;
940} KeyDef;
941
942static const KeyDef key_defs[] = {
943 { 0x2a, "shift" },
944 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000945
bellarda3a91a32004-06-04 11:06:21 +0000946 { 0x38, "alt" },
947 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000948 { 0x64, "altgr" },
949 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000950 { 0x1d, "ctrl" },
951 { 0x9d, "ctrl_r" },
952
953 { 0xdd, "menu" },
954
955 { 0x01, "esc" },
956
957 { 0x02, "1" },
958 { 0x03, "2" },
959 { 0x04, "3" },
960 { 0x05, "4" },
961 { 0x06, "5" },
962 { 0x07, "6" },
963 { 0x08, "7" },
964 { 0x09, "8" },
965 { 0x0a, "9" },
966 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000967 { 0x0c, "minus" },
968 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000969 { 0x0e, "backspace" },
970
971 { 0x0f, "tab" },
972 { 0x10, "q" },
973 { 0x11, "w" },
974 { 0x12, "e" },
975 { 0x13, "r" },
976 { 0x14, "t" },
977 { 0x15, "y" },
978 { 0x16, "u" },
979 { 0x17, "i" },
980 { 0x18, "o" },
981 { 0x19, "p" },
982
983 { 0x1c, "ret" },
984
985 { 0x1e, "a" },
986 { 0x1f, "s" },
987 { 0x20, "d" },
988 { 0x21, "f" },
989 { 0x22, "g" },
990 { 0x23, "h" },
991 { 0x24, "j" },
992 { 0x25, "k" },
993 { 0x26, "l" },
994
995 { 0x2c, "z" },
996 { 0x2d, "x" },
997 { 0x2e, "c" },
998 { 0x2f, "v" },
999 { 0x30, "b" },
1000 { 0x31, "n" },
1001 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001002 { 0x33, "comma" },
1003 { 0x34, "dot" },
1004 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001005
balrog4d3b6f62008-02-10 16:33:14 +00001006 { 0x37, "asterisk" },
1007
bellarda3a91a32004-06-04 11:06:21 +00001008 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001009 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001010 { 0x3b, "f1" },
1011 { 0x3c, "f2" },
1012 { 0x3d, "f3" },
1013 { 0x3e, "f4" },
1014 { 0x3f, "f5" },
1015 { 0x40, "f6" },
1016 { 0x41, "f7" },
1017 { 0x42, "f8" },
1018 { 0x43, "f9" },
1019 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001020 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001021 { 0x46, "scroll_lock" },
1022
bellard64866c32006-05-07 18:03:31 +00001023 { 0xb5, "kp_divide" },
1024 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001025 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001026 { 0x4e, "kp_add" },
1027 { 0x9c, "kp_enter" },
1028 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001029 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001030
1031 { 0x52, "kp_0" },
1032 { 0x4f, "kp_1" },
1033 { 0x50, "kp_2" },
1034 { 0x51, "kp_3" },
1035 { 0x4b, "kp_4" },
1036 { 0x4c, "kp_5" },
1037 { 0x4d, "kp_6" },
1038 { 0x47, "kp_7" },
1039 { 0x48, "kp_8" },
1040 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001041
bellarda3a91a32004-06-04 11:06:21 +00001042 { 0x56, "<" },
1043
1044 { 0x57, "f11" },
1045 { 0x58, "f12" },
1046
1047 { 0xb7, "print" },
1048
1049 { 0xc7, "home" },
1050 { 0xc9, "pgup" },
1051 { 0xd1, "pgdn" },
1052 { 0xcf, "end" },
1053
1054 { 0xcb, "left" },
1055 { 0xc8, "up" },
1056 { 0xd0, "down" },
1057 { 0xcd, "right" },
1058
1059 { 0xd2, "insert" },
1060 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001061#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1062 { 0xf0, "stop" },
1063 { 0xf1, "again" },
1064 { 0xf2, "props" },
1065 { 0xf3, "undo" },
1066 { 0xf4, "front" },
1067 { 0xf5, "copy" },
1068 { 0xf6, "open" },
1069 { 0xf7, "paste" },
1070 { 0xf8, "find" },
1071 { 0xf9, "cut" },
1072 { 0xfa, "lf" },
1073 { 0xfb, "help" },
1074 { 0xfc, "meta_l" },
1075 { 0xfd, "meta_r" },
1076 { 0xfe, "compose" },
1077#endif
bellarda3a91a32004-06-04 11:06:21 +00001078 { 0, NULL },
1079};
1080
1081static int get_keycode(const char *key)
1082{
1083 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001084 char *endp;
1085 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001086
1087 for(p = key_defs; p->name != NULL; p++) {
1088 if (!strcmp(key, p->name))
1089 return p->keycode;
1090 }
bellard64866c32006-05-07 18:03:31 +00001091 if (strstart(key, "0x", NULL)) {
1092 ret = strtoul(key, &endp, 0);
1093 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1094 return ret;
1095 }
bellarda3a91a32004-06-04 11:06:21 +00001096 return -1;
1097}
1098
balrogc8256f92008-06-08 22:45:01 +00001099#define MAX_KEYCODES 16
1100static uint8_t keycodes[MAX_KEYCODES];
1101static int nb_pending_keycodes;
1102static QEMUTimer *key_timer;
1103
1104static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001105{
balrogc8256f92008-06-08 22:45:01 +00001106 int keycode;
1107
1108 while (nb_pending_keycodes > 0) {
1109 nb_pending_keycodes--;
1110 keycode = keycodes[nb_pending_keycodes];
1111 if (keycode & 0x80)
1112 kbd_put_keycode(0xe0);
1113 kbd_put_keycode(keycode | 0x80);
1114 }
1115}
1116
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001117static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001118{
balrog3401c0d2008-06-04 10:05:59 +00001119 char keyname_buf[16];
1120 char *separator;
1121 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001122 const char *string = qdict_get_str(qdict, "string");
1123 int has_hold_time = qdict_haskey(qdict, "hold_time");
1124 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001125
balrogc8256f92008-06-08 22:45:01 +00001126 if (nb_pending_keycodes > 0) {
1127 qemu_del_timer(key_timer);
1128 release_keys(NULL);
1129 }
1130 if (!has_hold_time)
1131 hold_time = 100;
1132 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001133 while (1) {
1134 separator = strchr(string, '-');
1135 keyname_len = separator ? separator - string : strlen(string);
1136 if (keyname_len > 0) {
1137 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1138 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001139 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001140 return;
bellarda3a91a32004-06-04 11:06:21 +00001141 }
balrogc8256f92008-06-08 22:45:01 +00001142 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001143 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001144 return;
1145 }
1146 keyname_buf[keyname_len] = 0;
1147 keycode = get_keycode(keyname_buf);
1148 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001149 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001150 return;
1151 }
balrogc8256f92008-06-08 22:45:01 +00001152 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001153 }
balrog3401c0d2008-06-04 10:05:59 +00001154 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001155 break;
balrog3401c0d2008-06-04 10:05:59 +00001156 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001157 }
balrogc8256f92008-06-08 22:45:01 +00001158 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001159 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001160 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001161 keycode = keycodes[i];
1162 if (keycode & 0x80)
1163 kbd_put_keycode(0xe0);
1164 kbd_put_keycode(keycode & 0x7f);
1165 }
balrogc8256f92008-06-08 22:45:01 +00001166 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001167 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001168 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001169}
1170
bellard13224a82006-07-14 22:03:35 +00001171static int mouse_button_state;
1172
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001173static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001174{
1175 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001176 const char *dx_str = qdict_get_str(qdict, "dx_str");
1177 const char *dy_str = qdict_get_str(qdict, "dy_str");
1178 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001179 dx = strtol(dx_str, NULL, 0);
1180 dy = strtol(dy_str, NULL, 0);
1181 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001182 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001183 dz = strtol(dz_str, NULL, 0);
1184 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1185}
1186
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001187static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001188{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001189 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001190 mouse_button_state = button_state;
1191 kbd_mouse_event(0, 0, 0, mouse_button_state);
1192}
1193
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001194static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001195{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001196 int size = qdict_get_int(qdict, "size");
1197 int addr = qdict_get_int(qdict, "addr");
1198 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001199 uint32_t val;
1200 int suffix;
1201
1202 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001203 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001204 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001205 addr++;
1206 }
1207 addr &= 0xffff;
1208
1209 switch(size) {
1210 default:
1211 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001212 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001213 suffix = 'b';
1214 break;
1215 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001216 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001217 suffix = 'w';
1218 break;
1219 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001220 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001221 suffix = 'l';
1222 break;
1223 }
aliguori376253e2009-03-05 23:01:23 +00001224 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1225 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001226}
bellarda3a91a32004-06-04 11:06:21 +00001227
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001228static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001229{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001230 int size = qdict_get_int(qdict, "size");
1231 int addr = qdict_get_int(qdict, "addr");
1232 int val = qdict_get_int(qdict, "val");
1233
Jan Kiszkaf1147842009-07-14 10:20:11 +02001234 addr &= IOPORTS_MASK;
1235
1236 switch (size) {
1237 default:
1238 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001239 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001240 break;
1241 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001242 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001243 break;
1244 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001245 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001246 break;
1247 }
1248}
1249
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001250static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001251{
1252 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001253 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001254
Jan Kiszka76e30d02009-07-02 00:19:02 +02001255 res = qemu_boot_set(bootdevice);
1256 if (res == 0) {
1257 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1258 } else if (res > 0) {
1259 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001260 } else {
aliguori376253e2009-03-05 23:01:23 +00001261 monitor_printf(mon, "no function defined to set boot device list for "
1262 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001263 }
1264}
1265
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001266static void do_system_reset(Monitor *mon, const QDict *qdict)
bellarde4f90822004-06-20 12:35:44 +00001267{
1268 qemu_system_reset_request();
1269}
1270
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001271static void do_system_powerdown(Monitor *mon, const QDict *qdict)
bellard34751872005-07-02 14:31:34 +00001272{
1273 qemu_system_powerdown_request();
1274}
1275
bellardb86bda52004-09-18 19:32:46 +00001276#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001277static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001278{
aliguori376253e2009-03-05 23:01:23 +00001279 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1280 addr,
1281 pte & mask,
1282 pte & PG_GLOBAL_MASK ? 'G' : '-',
1283 pte & PG_PSE_MASK ? 'P' : '-',
1284 pte & PG_DIRTY_MASK ? 'D' : '-',
1285 pte & PG_ACCESSED_MASK ? 'A' : '-',
1286 pte & PG_PCD_MASK ? 'C' : '-',
1287 pte & PG_PWT_MASK ? 'T' : '-',
1288 pte & PG_USER_MASK ? 'U' : '-',
1289 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001290}
1291
aliguori376253e2009-03-05 23:01:23 +00001292static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001293{
bellard6a00d602005-11-21 23:25:50 +00001294 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001295 int l1, l2;
1296 uint32_t pgd, pde, pte;
1297
bellard6a00d602005-11-21 23:25:50 +00001298 env = mon_get_cpu();
1299 if (!env)
1300 return;
1301
bellardb86bda52004-09-18 19:32:46 +00001302 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001303 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001304 return;
1305 }
1306 pgd = env->cr[3] & ~0xfff;
1307 for(l1 = 0; l1 < 1024; l1++) {
1308 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1309 pde = le32_to_cpu(pde);
1310 if (pde & PG_PRESENT_MASK) {
1311 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001312 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001313 } else {
1314 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001315 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001316 (uint8_t *)&pte, 4);
1317 pte = le32_to_cpu(pte);
1318 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001319 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001320 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001321 ~0xfff);
1322 }
1323 }
1324 }
1325 }
1326 }
1327}
1328
aliguori376253e2009-03-05 23:01:23 +00001329static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001330 uint32_t end, int prot)
1331{
bellard9746b152004-11-11 18:30:24 +00001332 int prot1;
1333 prot1 = *plast_prot;
1334 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001335 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001336 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1337 *pstart, end, end - *pstart,
1338 prot1 & PG_USER_MASK ? 'u' : '-',
1339 'r',
1340 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001341 }
1342 if (prot != 0)
1343 *pstart = end;
1344 else
1345 *pstart = -1;
1346 *plast_prot = prot;
1347 }
1348}
1349
aliguori376253e2009-03-05 23:01:23 +00001350static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001351{
bellard6a00d602005-11-21 23:25:50 +00001352 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001353 int l1, l2, prot, last_prot;
1354 uint32_t pgd, pde, pte, start, end;
1355
bellard6a00d602005-11-21 23:25:50 +00001356 env = mon_get_cpu();
1357 if (!env)
1358 return;
1359
bellardb86bda52004-09-18 19:32:46 +00001360 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001361 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001362 return;
1363 }
1364 pgd = env->cr[3] & ~0xfff;
1365 last_prot = 0;
1366 start = -1;
1367 for(l1 = 0; l1 < 1024; l1++) {
1368 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1369 pde = le32_to_cpu(pde);
1370 end = l1 << 22;
1371 if (pde & PG_PRESENT_MASK) {
1372 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1373 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001374 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001375 } else {
1376 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001377 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001378 (uint8_t *)&pte, 4);
1379 pte = le32_to_cpu(pte);
1380 end = (l1 << 22) + (l2 << 12);
1381 if (pte & PG_PRESENT_MASK) {
1382 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1383 } else {
1384 prot = 0;
1385 }
aliguori376253e2009-03-05 23:01:23 +00001386 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001387 }
1388 }
1389 } else {
1390 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001391 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001392 }
1393 }
1394}
1395#endif
1396
aurel327c664e22009-03-03 06:12:22 +00001397#if defined(TARGET_SH4)
1398
aliguori376253e2009-03-05 23:01:23 +00001399static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001400{
aliguori376253e2009-03-05 23:01:23 +00001401 monitor_printf(mon, " tlb%i:\t"
1402 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1403 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1404 "dirty=%hhu writethrough=%hhu\n",
1405 idx,
1406 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1407 tlb->v, tlb->sh, tlb->c, tlb->pr,
1408 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001409}
1410
aliguori376253e2009-03-05 23:01:23 +00001411static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001412{
1413 CPUState *env = mon_get_cpu();
1414 int i;
1415
aliguori376253e2009-03-05 23:01:23 +00001416 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001417 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001418 print_tlb (mon, i, &env->itlb[i]);
1419 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001420 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001421 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001422}
1423
1424#endif
1425
aliguori376253e2009-03-05 23:01:23 +00001426static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001427{
1428#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001429 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001430 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001431 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001432 else
aliguori376253e2009-03-05 23:01:23 +00001433 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001434#else
aliguori376253e2009-03-05 23:01:23 +00001435 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001436#endif
1437}
1438
aliguori030ea372009-04-21 22:30:47 +00001439static void do_info_numa(Monitor *mon)
1440{
aliguorib28b6232009-04-22 20:20:29 +00001441 int i;
aliguori030ea372009-04-21 22:30:47 +00001442 CPUState *env;
1443
1444 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1445 for (i = 0; i < nb_numa_nodes; i++) {
1446 monitor_printf(mon, "node %d cpus:", i);
1447 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1448 if (env->numa_node == i) {
1449 monitor_printf(mon, " %d", env->cpu_index);
1450 }
1451 }
1452 monitor_printf(mon, "\n");
1453 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1454 node_mem[i] >> 20);
1455 }
1456}
1457
bellard5f1ce942006-02-08 22:40:15 +00001458#ifdef CONFIG_PROFILER
1459
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001460int64_t qemu_time;
1461int64_t dev_time;
1462
aliguori376253e2009-03-05 23:01:23 +00001463static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001464{
1465 int64_t total;
1466 total = qemu_time;
1467 if (total == 0)
1468 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001469 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001470 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001471 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001472 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001473 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001474 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001475}
1476#else
aliguori376253e2009-03-05 23:01:23 +00001477static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001478{
aliguori376253e2009-03-05 23:01:23 +00001479 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001480}
1481#endif
1482
bellardec36b692006-07-16 18:57:03 +00001483/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001484static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001485
aliguori376253e2009-03-05 23:01:23 +00001486static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001487{
1488 int i;
1489 CaptureState *s;
1490
1491 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001492 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001493 s->ops.info (s->opaque);
1494 }
1495}
1496
Blue Swirl23130862009-06-06 08:22:04 +00001497#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001498static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001499{
1500 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001501 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001502 CaptureState *s;
1503
1504 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1505 if (i == n) {
1506 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001507 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00001508 qemu_free (s);
1509 return;
1510 }
1511 }
1512}
1513
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001514static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001515{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001516 const char *path = qdict_get_str(qdict, "path");
1517 int has_freq = qdict_haskey(qdict, "freq");
1518 int freq = qdict_get_try_int(qdict, "freq", -1);
1519 int has_bits = qdict_haskey(qdict, "bits");
1520 int bits = qdict_get_try_int(qdict, "bits", -1);
1521 int has_channels = qdict_haskey(qdict, "nchannels");
1522 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001523 CaptureState *s;
1524
1525 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001526
1527 freq = has_freq ? freq : 44100;
1528 bits = has_bits ? bits : 16;
1529 nchannels = has_channels ? nchannels : 2;
1530
1531 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001532 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001533 qemu_free (s);
1534 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001535 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001536}
1537#endif
1538
aurel32dc1c0b72008-04-27 23:52:12 +00001539#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001540static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001541{
1542 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001543 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001544
1545 for (env = first_cpu; env != NULL; env = env->next_cpu)
1546 if (env->cpu_index == cpu_index) {
1547 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1548 break;
1549 }
1550}
1551#endif
1552
aliguori376253e2009-03-05 23:01:23 +00001553static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001554{
aurel321b530a62009-04-05 20:08:59 +00001555 if (vm_running) {
1556 if (singlestep) {
1557 monitor_printf(mon, "VM status: running (single step mode)\n");
1558 } else {
1559 monitor_printf(mon, "VM status: running\n");
1560 }
1561 } else
aliguori376253e2009-03-05 23:01:23 +00001562 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001563}
1564
1565
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001566static void do_balloon(Monitor *mon, const QDict *qdict)
aliguoridf751fa2008-12-04 20:19:35 +00001567{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001568 int value = qdict_get_int(qdict, "value");
Anthony Liguoric227f092009-10-01 16:12:16 -05001569 ram_addr_t target = value;
aliguoridf751fa2008-12-04 20:19:35 +00001570 qemu_balloon(target << 20);
1571}
1572
aliguori376253e2009-03-05 23:01:23 +00001573static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001574{
Anthony Liguoric227f092009-10-01 16:12:16 -05001575 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00001576
1577 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001578 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001579 monitor_printf(mon, "Using KVM without synchronous MMU, "
1580 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001581 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001582 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001583 else
aliguori376253e2009-03-05 23:01:23 +00001584 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001585}
1586
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001587static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001588{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001589 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001590
aliguori76655d62009-03-06 20:27:37 +00001591 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001592 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001593 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001594 return acl;
1595}
aliguori76655d62009-03-06 20:27:37 +00001596
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001597static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001598{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001599 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001600 qemu_acl *acl = find_acl(mon, aclname);
1601 qemu_acl_entry *entry;
1602 int i = 0;
1603
1604 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001605 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001606 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001607 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001608 i++;
1609 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001610 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001611 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001612 }
1613}
1614
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001615static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001616{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001617 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001618 qemu_acl *acl = find_acl(mon, aclname);
1619
1620 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001621 qemu_acl_reset(acl);
1622 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001623 }
1624}
aliguori76655d62009-03-06 20:27:37 +00001625
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001626static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001627{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001628 const char *aclname = qdict_get_str(qdict, "aclname");
1629 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001630 qemu_acl *acl = find_acl(mon, aclname);
1631
1632 if (acl) {
1633 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001634 acl->defaultDeny = 0;
1635 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001636 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001637 acl->defaultDeny = 1;
1638 monitor_printf(mon, "acl: policy set to 'deny'\n");
1639 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001640 monitor_printf(mon, "acl: unknown policy '%s', "
1641 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001642 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001643 }
1644}
aliguori76655d62009-03-06 20:27:37 +00001645
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001646static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001647{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001648 const char *aclname = qdict_get_str(qdict, "aclname");
1649 const char *match = qdict_get_str(qdict, "match");
1650 const char *policy = qdict_get_str(qdict, "policy");
1651 int has_index = qdict_haskey(qdict, "index");
1652 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001653 qemu_acl *acl = find_acl(mon, aclname);
1654 int deny, ret;
1655
1656 if (acl) {
1657 if (strcmp(policy, "allow") == 0) {
1658 deny = 0;
1659 } else if (strcmp(policy, "deny") == 0) {
1660 deny = 1;
1661 } else {
1662 monitor_printf(mon, "acl: unknown policy '%s', "
1663 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001664 return;
1665 }
aliguori28a76be2009-03-06 20:27:40 +00001666 if (has_index)
1667 ret = qemu_acl_insert(acl, deny, match, index);
1668 else
1669 ret = qemu_acl_append(acl, deny, match);
1670 if (ret < 0)
1671 monitor_printf(mon, "acl: unable to add acl entry\n");
1672 else
1673 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001674 }
1675}
aliguori76655d62009-03-06 20:27:37 +00001676
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001677static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001678{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001679 const char *aclname = qdict_get_str(qdict, "aclname");
1680 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001681 qemu_acl *acl = find_acl(mon, aclname);
1682 int ret;
aliguori76655d62009-03-06 20:27:37 +00001683
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001684 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001685 ret = qemu_acl_remove(acl, match);
1686 if (ret < 0)
1687 monitor_printf(mon, "acl: no matching acl entry\n");
1688 else
1689 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001690 }
1691}
1692
Huang Ying79c4f6b2009-06-23 10:05:14 +08001693#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001694static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001695{
1696 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001697 int cpu_index = qdict_get_int(qdict, "cpu_index");
1698 int bank = qdict_get_int(qdict, "bank");
1699 uint64_t status = qdict_get_int(qdict, "status");
1700 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1701 uint64_t addr = qdict_get_int(qdict, "addr");
1702 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08001703
1704 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1705 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1706 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1707 break;
1708 }
1709}
1710#endif
1711
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001712static void do_getfd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001713{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001714 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001715 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001716 int fd;
1717
1718 fd = qemu_chr_get_msgfd(mon->chr);
1719 if (fd == -1) {
1720 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1721 return;
1722 }
1723
1724 if (qemu_isdigit(fdname[0])) {
1725 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1726 return;
1727 }
1728
1729 fd = dup(fd);
1730 if (fd == -1) {
1731 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1732 strerror(errno));
1733 return;
1734 }
1735
Blue Swirl72cf2d42009-09-12 07:36:22 +00001736 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001737 if (strcmp(monfd->name, fdname) != 0) {
1738 continue;
1739 }
1740
1741 close(monfd->fd);
1742 monfd->fd = fd;
1743 return;
1744 }
1745
Anthony Liguoric227f092009-10-01 16:12:16 -05001746 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001747 monfd->name = qemu_strdup(fdname);
1748 monfd->fd = fd;
1749
Blue Swirl72cf2d42009-09-12 07:36:22 +00001750 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001751}
1752
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001753static void do_closefd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001754{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001755 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001756 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001757
Blue Swirl72cf2d42009-09-12 07:36:22 +00001758 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001759 if (strcmp(monfd->name, fdname) != 0) {
1760 continue;
1761 }
1762
Blue Swirl72cf2d42009-09-12 07:36:22 +00001763 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001764 close(monfd->fd);
1765 qemu_free(monfd->name);
1766 qemu_free(monfd);
1767 return;
1768 }
1769
1770 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1771 fdname);
1772}
1773
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001774static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001775{
1776 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001777 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001778
1779 vm_stop(0);
1780
Juan Quintela05f24012009-08-20 19:42:22 +02001781 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001782 vm_start();
1783}
1784
Mark McLoughlin7768e042009-07-22 09:11:41 +01001785int monitor_get_fd(Monitor *mon, const char *fdname)
1786{
Anthony Liguoric227f092009-10-01 16:12:16 -05001787 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001788
Blue Swirl72cf2d42009-09-12 07:36:22 +00001789 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001790 int fd;
1791
1792 if (strcmp(monfd->name, fdname) != 0) {
1793 continue;
1794 }
1795
1796 fd = monfd->fd;
1797
1798 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001799 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001800 qemu_free(monfd->name);
1801 qemu_free(monfd);
1802
1803 return fd;
1804 }
1805
1806 return -1;
1807}
1808
Anthony Liguoric227f092009-10-01 16:12:16 -05001809static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001810#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001811 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001812};
1813
Blue Swirl23130862009-06-06 08:22:04 +00001814/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05001815static const mon_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001816 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001817 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001818 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001819 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001820 { "chardev", "", qemu_chr_info,
1821 "", "show the character devices" },
aliguori376253e2009-03-05 23:01:23 +00001822 { "block", "", bdrv_info,
bellard9dc39cb2004-03-14 21:38:27 +00001823 "", "show the block devices" },
aliguori376253e2009-03-05 23:01:23 +00001824 { "blockstats", "", bdrv_info_stats,
thsa36e69d2007-12-02 05:18:19 +00001825 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001826 { "registers", "", do_info_registers,
1827 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001828 { "cpus", "", do_info_cpus,
1829 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001830 { "history", "", do_info_history,
1831 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001832 { "irq", "", irq_info,
1833 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001834 { "pic", "", pic_info,
1835 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001836 { "pci", "", pci_info,
1837 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001838#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001839 { "tlb", "", tlb_info,
1840 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001841#endif
1842#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001843 { "mem", "", mem_info,
1844 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001845 { "hpet", "", do_info_hpet,
1846 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001847#endif
bellarde3db7222005-01-26 22:00:47 +00001848 { "jit", "", do_info_jit,
1849 "", "show dynamic compiler info", },
aliguori7ba1e612008-11-05 16:04:33 +00001850 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001851 "", "show KVM information", },
aliguori030ea372009-04-21 22:30:47 +00001852 { "numa", "", do_info_numa,
1853 "", "show NUMA information", },
bellarda594cfb2005-11-06 16:13:29 +00001854 { "usb", "", usb_info,
1855 "", "show guest USB devices", },
1856 { "usbhost", "", usb_host_info,
1857 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001858 { "profile", "", do_info_profile,
1859 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001860 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001861 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001862 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001863 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001864 { "status", "", do_info_status,
1865 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001866 { "pcmcia", "", pcmcia_info,
1867 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001868 { "mice", "", do_info_mice,
1869 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001870 { "vnc", "", do_info_vnc,
1871 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001872 { "name", "", do_info_name,
1873 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001874 { "uuid", "", do_info_uuid,
1875 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001876#if defined(TARGET_PPC)
1877 { "cpustats", "", do_info_cpu_stats,
1878 "", "show CPU statistics", },
1879#endif
blueswir131a60e22007-10-26 18:42:59 +00001880#if defined(CONFIG_SLIRP)
Jan Kiszka6dbe5532009-06-24 14:42:29 +02001881 { "usernet", "", do_info_usernet,
1882 "", "show user network stack connection states", },
blueswir131a60e22007-10-26 18:42:59 +00001883#endif
aliguori5bb79102008-10-13 03:12:02 +00001884 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001885 { "balloon", "", do_info_balloon,
1886 "", "show balloon information" },
Gerd Hoffmanncae49562009-06-05 15:53:17 +01001887 { "qtree", "", do_info_qtree,
1888 "", "show device tree" },
Gerd Hoffmannf6c64e02009-08-03 15:03:09 +02001889 { "qdm", "", do_info_qdm,
1890 "", "show qdev device model list" },
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02001891 { "roms", "", do_info_roms,
1892 "", "show roms" },
bellard9dc39cb2004-03-14 21:38:27 +00001893 { NULL, NULL, },
1894};
1895
bellard9307c4c2004-04-04 12:57:25 +00001896/*******************************************************************/
1897
1898static const char *pch;
1899static jmp_buf expr_env;
1900
bellard92a31b12005-02-10 22:00:52 +00001901#define MD_TLONG 0
1902#define MD_I32 1
1903
bellard9307c4c2004-04-04 12:57:25 +00001904typedef struct MonitorDef {
1905 const char *name;
1906 int offset;
blueswir18662d652008-10-02 18:32:44 +00001907 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001908 int type;
bellard9307c4c2004-04-04 12:57:25 +00001909} MonitorDef;
1910
bellard57206fd2004-04-25 18:54:52 +00001911#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001912static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001913{
bellard6a00d602005-11-21 23:25:50 +00001914 CPUState *env = mon_get_cpu();
1915 if (!env)
1916 return 0;
1917 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001918}
1919#endif
1920
bellarda541f292004-04-12 20:39:29 +00001921#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001922static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001923{
bellard6a00d602005-11-21 23:25:50 +00001924 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001925 unsigned int u;
1926 int i;
1927
bellard6a00d602005-11-21 23:25:50 +00001928 if (!env)
1929 return 0;
1930
bellarda541f292004-04-12 20:39:29 +00001931 u = 0;
1932 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00001933 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001934
1935 return u;
1936}
1937
blueswir18662d652008-10-02 18:32:44 +00001938static target_long monitor_get_msr (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;
j_mayer0411a972007-10-25 21:35:50 +00001943 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001944}
1945
blueswir18662d652008-10-02 18:32:44 +00001946static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001947{
bellard6a00d602005-11-21 23:25:50 +00001948 CPUState *env = mon_get_cpu();
1949 if (!env)
1950 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001951 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001952}
bellard9fddaa02004-05-21 12:59:32 +00001953
blueswir18662d652008-10-02 18:32:44 +00001954static target_long monitor_get_decr (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_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001960}
1961
blueswir18662d652008-10-02 18:32:44 +00001962static target_long monitor_get_tbu (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_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001968}
1969
blueswir18662d652008-10-02 18:32:44 +00001970static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001971{
bellard6a00d602005-11-21 23:25:50 +00001972 CPUState *env = mon_get_cpu();
1973 if (!env)
1974 return 0;
1975 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001976}
bellarda541f292004-04-12 20:39:29 +00001977#endif
1978
bellarde95c8d52004-09-30 22:22:08 +00001979#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001980#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001981static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001982{
bellard6a00d602005-11-21 23:25:50 +00001983 CPUState *env = mon_get_cpu();
1984 if (!env)
1985 return 0;
1986 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001987}
bellard7b936c02005-10-30 17:05:13 +00001988#endif
bellarde95c8d52004-09-30 22:22:08 +00001989
blueswir18662d652008-10-02 18:32:44 +00001990static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001991{
bellard6a00d602005-11-21 23:25:50 +00001992 CPUState *env = mon_get_cpu();
1993 if (!env)
1994 return 0;
1995 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001996}
1997#endif
1998
blueswir18662d652008-10-02 18:32:44 +00001999static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002000#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002001
2002#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002003 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002004 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002005 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002006
bellard9307c4c2004-04-04 12:57:25 +00002007 { "eax", offsetof(CPUState, regs[0]) },
2008 { "ecx", offsetof(CPUState, regs[1]) },
2009 { "edx", offsetof(CPUState, regs[2]) },
2010 { "ebx", offsetof(CPUState, regs[3]) },
2011 { "esp|sp", offsetof(CPUState, regs[4]) },
2012 { "ebp|fp", offsetof(CPUState, regs[5]) },
2013 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002014 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002015#ifdef TARGET_X86_64
2016 { "r8", offsetof(CPUState, regs[8]) },
2017 { "r9", offsetof(CPUState, regs[9]) },
2018 { "r10", offsetof(CPUState, regs[10]) },
2019 { "r11", offsetof(CPUState, regs[11]) },
2020 { "r12", offsetof(CPUState, regs[12]) },
2021 { "r13", offsetof(CPUState, regs[13]) },
2022 { "r14", offsetof(CPUState, regs[14]) },
2023 { "r15", offsetof(CPUState, regs[15]) },
2024#endif
bellard9307c4c2004-04-04 12:57:25 +00002025 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002026 { "eip", offsetof(CPUState, eip) },
2027 SEG("cs", R_CS)
2028 SEG("ds", R_DS)
2029 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002030 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002031 SEG("fs", R_FS)
2032 SEG("gs", R_GS)
2033 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002034#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002035 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002036 { "r0", offsetof(CPUState, gpr[0]) },
2037 { "r1", offsetof(CPUState, gpr[1]) },
2038 { "r2", offsetof(CPUState, gpr[2]) },
2039 { "r3", offsetof(CPUState, gpr[3]) },
2040 { "r4", offsetof(CPUState, gpr[4]) },
2041 { "r5", offsetof(CPUState, gpr[5]) },
2042 { "r6", offsetof(CPUState, gpr[6]) },
2043 { "r7", offsetof(CPUState, gpr[7]) },
2044 { "r8", offsetof(CPUState, gpr[8]) },
2045 { "r9", offsetof(CPUState, gpr[9]) },
2046 { "r10", offsetof(CPUState, gpr[10]) },
2047 { "r11", offsetof(CPUState, gpr[11]) },
2048 { "r12", offsetof(CPUState, gpr[12]) },
2049 { "r13", offsetof(CPUState, gpr[13]) },
2050 { "r14", offsetof(CPUState, gpr[14]) },
2051 { "r15", offsetof(CPUState, gpr[15]) },
2052 { "r16", offsetof(CPUState, gpr[16]) },
2053 { "r17", offsetof(CPUState, gpr[17]) },
2054 { "r18", offsetof(CPUState, gpr[18]) },
2055 { "r19", offsetof(CPUState, gpr[19]) },
2056 { "r20", offsetof(CPUState, gpr[20]) },
2057 { "r21", offsetof(CPUState, gpr[21]) },
2058 { "r22", offsetof(CPUState, gpr[22]) },
2059 { "r23", offsetof(CPUState, gpr[23]) },
2060 { "r24", offsetof(CPUState, gpr[24]) },
2061 { "r25", offsetof(CPUState, gpr[25]) },
2062 { "r26", offsetof(CPUState, gpr[26]) },
2063 { "r27", offsetof(CPUState, gpr[27]) },
2064 { "r28", offsetof(CPUState, gpr[28]) },
2065 { "r29", offsetof(CPUState, gpr[29]) },
2066 { "r30", offsetof(CPUState, gpr[30]) },
2067 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002068 /* Floating point registers */
2069 { "f0", offsetof(CPUState, fpr[0]) },
2070 { "f1", offsetof(CPUState, fpr[1]) },
2071 { "f2", offsetof(CPUState, fpr[2]) },
2072 { "f3", offsetof(CPUState, fpr[3]) },
2073 { "f4", offsetof(CPUState, fpr[4]) },
2074 { "f5", offsetof(CPUState, fpr[5]) },
2075 { "f6", offsetof(CPUState, fpr[6]) },
2076 { "f7", offsetof(CPUState, fpr[7]) },
2077 { "f8", offsetof(CPUState, fpr[8]) },
2078 { "f9", offsetof(CPUState, fpr[9]) },
2079 { "f10", offsetof(CPUState, fpr[10]) },
2080 { "f11", offsetof(CPUState, fpr[11]) },
2081 { "f12", offsetof(CPUState, fpr[12]) },
2082 { "f13", offsetof(CPUState, fpr[13]) },
2083 { "f14", offsetof(CPUState, fpr[14]) },
2084 { "f15", offsetof(CPUState, fpr[15]) },
2085 { "f16", offsetof(CPUState, fpr[16]) },
2086 { "f17", offsetof(CPUState, fpr[17]) },
2087 { "f18", offsetof(CPUState, fpr[18]) },
2088 { "f19", offsetof(CPUState, fpr[19]) },
2089 { "f20", offsetof(CPUState, fpr[20]) },
2090 { "f21", offsetof(CPUState, fpr[21]) },
2091 { "f22", offsetof(CPUState, fpr[22]) },
2092 { "f23", offsetof(CPUState, fpr[23]) },
2093 { "f24", offsetof(CPUState, fpr[24]) },
2094 { "f25", offsetof(CPUState, fpr[25]) },
2095 { "f26", offsetof(CPUState, fpr[26]) },
2096 { "f27", offsetof(CPUState, fpr[27]) },
2097 { "f28", offsetof(CPUState, fpr[28]) },
2098 { "f29", offsetof(CPUState, fpr[29]) },
2099 { "f30", offsetof(CPUState, fpr[30]) },
2100 { "f31", offsetof(CPUState, fpr[31]) },
2101 { "fpscr", offsetof(CPUState, fpscr) },
2102 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002103 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002104 { "lr", offsetof(CPUState, lr) },
2105 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002106 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002107 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002108 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002109 { "msr", 0, &monitor_get_msr, },
2110 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002111 { "tbu", 0, &monitor_get_tbu, },
2112 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002113#if defined(TARGET_PPC64)
2114 /* Address space register */
2115 { "asr", offsetof(CPUState, asr) },
2116#endif
2117 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002118 { "sdr1", offsetof(CPUState, sdr1) },
2119 { "sr0", offsetof(CPUState, sr[0]) },
2120 { "sr1", offsetof(CPUState, sr[1]) },
2121 { "sr2", offsetof(CPUState, sr[2]) },
2122 { "sr3", offsetof(CPUState, sr[3]) },
2123 { "sr4", offsetof(CPUState, sr[4]) },
2124 { "sr5", offsetof(CPUState, sr[5]) },
2125 { "sr6", offsetof(CPUState, sr[6]) },
2126 { "sr7", offsetof(CPUState, sr[7]) },
2127 { "sr8", offsetof(CPUState, sr[8]) },
2128 { "sr9", offsetof(CPUState, sr[9]) },
2129 { "sr10", offsetof(CPUState, sr[10]) },
2130 { "sr11", offsetof(CPUState, sr[11]) },
2131 { "sr12", offsetof(CPUState, sr[12]) },
2132 { "sr13", offsetof(CPUState, sr[13]) },
2133 { "sr14", offsetof(CPUState, sr[14]) },
2134 { "sr15", offsetof(CPUState, sr[15]) },
2135 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002136#elif defined(TARGET_SPARC)
2137 { "g0", offsetof(CPUState, gregs[0]) },
2138 { "g1", offsetof(CPUState, gregs[1]) },
2139 { "g2", offsetof(CPUState, gregs[2]) },
2140 { "g3", offsetof(CPUState, gregs[3]) },
2141 { "g4", offsetof(CPUState, gregs[4]) },
2142 { "g5", offsetof(CPUState, gregs[5]) },
2143 { "g6", offsetof(CPUState, gregs[6]) },
2144 { "g7", offsetof(CPUState, gregs[7]) },
2145 { "o0", 0, monitor_get_reg },
2146 { "o1", 1, monitor_get_reg },
2147 { "o2", 2, monitor_get_reg },
2148 { "o3", 3, monitor_get_reg },
2149 { "o4", 4, monitor_get_reg },
2150 { "o5", 5, monitor_get_reg },
2151 { "o6", 6, monitor_get_reg },
2152 { "o7", 7, monitor_get_reg },
2153 { "l0", 8, monitor_get_reg },
2154 { "l1", 9, monitor_get_reg },
2155 { "l2", 10, monitor_get_reg },
2156 { "l3", 11, monitor_get_reg },
2157 { "l4", 12, monitor_get_reg },
2158 { "l5", 13, monitor_get_reg },
2159 { "l6", 14, monitor_get_reg },
2160 { "l7", 15, monitor_get_reg },
2161 { "i0", 16, monitor_get_reg },
2162 { "i1", 17, monitor_get_reg },
2163 { "i2", 18, monitor_get_reg },
2164 { "i3", 19, monitor_get_reg },
2165 { "i4", 20, monitor_get_reg },
2166 { "i5", 21, monitor_get_reg },
2167 { "i6", 22, monitor_get_reg },
2168 { "i7", 23, monitor_get_reg },
2169 { "pc", offsetof(CPUState, pc) },
2170 { "npc", offsetof(CPUState, npc) },
2171 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002172#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002173 { "psr", 0, &monitor_get_psr, },
2174 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002175#endif
bellarde95c8d52004-09-30 22:22:08 +00002176 { "tbr", offsetof(CPUState, tbr) },
2177 { "fsr", offsetof(CPUState, fsr) },
2178 { "f0", offsetof(CPUState, fpr[0]) },
2179 { "f1", offsetof(CPUState, fpr[1]) },
2180 { "f2", offsetof(CPUState, fpr[2]) },
2181 { "f3", offsetof(CPUState, fpr[3]) },
2182 { "f4", offsetof(CPUState, fpr[4]) },
2183 { "f5", offsetof(CPUState, fpr[5]) },
2184 { "f6", offsetof(CPUState, fpr[6]) },
2185 { "f7", offsetof(CPUState, fpr[7]) },
2186 { "f8", offsetof(CPUState, fpr[8]) },
2187 { "f9", offsetof(CPUState, fpr[9]) },
2188 { "f10", offsetof(CPUState, fpr[10]) },
2189 { "f11", offsetof(CPUState, fpr[11]) },
2190 { "f12", offsetof(CPUState, fpr[12]) },
2191 { "f13", offsetof(CPUState, fpr[13]) },
2192 { "f14", offsetof(CPUState, fpr[14]) },
2193 { "f15", offsetof(CPUState, fpr[15]) },
2194 { "f16", offsetof(CPUState, fpr[16]) },
2195 { "f17", offsetof(CPUState, fpr[17]) },
2196 { "f18", offsetof(CPUState, fpr[18]) },
2197 { "f19", offsetof(CPUState, fpr[19]) },
2198 { "f20", offsetof(CPUState, fpr[20]) },
2199 { "f21", offsetof(CPUState, fpr[21]) },
2200 { "f22", offsetof(CPUState, fpr[22]) },
2201 { "f23", offsetof(CPUState, fpr[23]) },
2202 { "f24", offsetof(CPUState, fpr[24]) },
2203 { "f25", offsetof(CPUState, fpr[25]) },
2204 { "f26", offsetof(CPUState, fpr[26]) },
2205 { "f27", offsetof(CPUState, fpr[27]) },
2206 { "f28", offsetof(CPUState, fpr[28]) },
2207 { "f29", offsetof(CPUState, fpr[29]) },
2208 { "f30", offsetof(CPUState, fpr[30]) },
2209 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002210#ifdef TARGET_SPARC64
2211 { "f32", offsetof(CPUState, fpr[32]) },
2212 { "f34", offsetof(CPUState, fpr[34]) },
2213 { "f36", offsetof(CPUState, fpr[36]) },
2214 { "f38", offsetof(CPUState, fpr[38]) },
2215 { "f40", offsetof(CPUState, fpr[40]) },
2216 { "f42", offsetof(CPUState, fpr[42]) },
2217 { "f44", offsetof(CPUState, fpr[44]) },
2218 { "f46", offsetof(CPUState, fpr[46]) },
2219 { "f48", offsetof(CPUState, fpr[48]) },
2220 { "f50", offsetof(CPUState, fpr[50]) },
2221 { "f52", offsetof(CPUState, fpr[52]) },
2222 { "f54", offsetof(CPUState, fpr[54]) },
2223 { "f56", offsetof(CPUState, fpr[56]) },
2224 { "f58", offsetof(CPUState, fpr[58]) },
2225 { "f60", offsetof(CPUState, fpr[60]) },
2226 { "f62", offsetof(CPUState, fpr[62]) },
2227 { "asi", offsetof(CPUState, asi) },
2228 { "pstate", offsetof(CPUState, pstate) },
2229 { "cansave", offsetof(CPUState, cansave) },
2230 { "canrestore", offsetof(CPUState, canrestore) },
2231 { "otherwin", offsetof(CPUState, otherwin) },
2232 { "wstate", offsetof(CPUState, wstate) },
2233 { "cleanwin", offsetof(CPUState, cleanwin) },
2234 { "fprs", offsetof(CPUState, fprs) },
2235#endif
bellard9307c4c2004-04-04 12:57:25 +00002236#endif
2237 { NULL },
2238};
2239
aliguori376253e2009-03-05 23:01:23 +00002240static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002241{
aliguori376253e2009-03-05 23:01:23 +00002242 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002243 longjmp(expr_env, 1);
2244}
2245
bellard6a00d602005-11-21 23:25:50 +00002246/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002247static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002248{
blueswir18662d652008-10-02 18:32:44 +00002249 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002250 void *ptr;
2251
bellard9307c4c2004-04-04 12:57:25 +00002252 for(md = monitor_defs; md->name != NULL; md++) {
2253 if (compare_cmd(name, md->name)) {
2254 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002255 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002256 } else {
bellard6a00d602005-11-21 23:25:50 +00002257 CPUState *env = mon_get_cpu();
2258 if (!env)
2259 return -2;
2260 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002261 switch(md->type) {
2262 case MD_I32:
2263 *pval = *(int32_t *)ptr;
2264 break;
2265 case MD_TLONG:
2266 *pval = *(target_long *)ptr;
2267 break;
2268 default:
2269 *pval = 0;
2270 break;
2271 }
bellard9307c4c2004-04-04 12:57:25 +00002272 }
2273 return 0;
2274 }
2275 }
2276 return -1;
2277}
2278
2279static void next(void)
2280{
Blue Swirl660f11b2009-07-31 21:16:51 +00002281 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002282 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002283 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002284 pch++;
2285 }
2286}
2287
aliguori376253e2009-03-05 23:01:23 +00002288static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002289
aliguori376253e2009-03-05 23:01:23 +00002290static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002291{
blueswir1c2efc952007-09-25 17:28:42 +00002292 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002293 char *p;
bellard6a00d602005-11-21 23:25:50 +00002294 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002295
2296 switch(*pch) {
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_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002304 break;
2305 case '~':
2306 next();
aliguori376253e2009-03-05 23:01:23 +00002307 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002308 break;
2309 case '(':
2310 next();
aliguori376253e2009-03-05 23:01:23 +00002311 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002312 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002313 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002314 }
2315 next();
2316 break;
bellard81d09122004-07-14 17:21:37 +00002317 case '\'':
2318 pch++;
2319 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002320 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002321 n = *pch;
2322 pch++;
2323 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002324 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002325 next();
2326 break;
bellard9307c4c2004-04-04 12:57:25 +00002327 case '$':
2328 {
2329 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002330 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002331
bellard9307c4c2004-04-04 12:57:25 +00002332 pch++;
2333 q = buf;
2334 while ((*pch >= 'a' && *pch <= 'z') ||
2335 (*pch >= 'A' && *pch <= 'Z') ||
2336 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002337 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002338 if ((q - buf) < sizeof(buf) - 1)
2339 *q++ = *pch;
2340 pch++;
2341 }
blueswir1cd390082008-11-16 13:53:32 +00002342 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002343 pch++;
2344 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002345 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002346 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002347 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002348 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002349 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002350 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002351 }
2352 break;
2353 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002354 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002355 n = 0;
2356 break;
2357 default:
blueswir17743e582007-09-24 18:39:04 +00002358#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002359 n = strtoull(pch, &p, 0);
2360#else
bellard9307c4c2004-04-04 12:57:25 +00002361 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002362#endif
bellard9307c4c2004-04-04 12:57:25 +00002363 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002364 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002365 }
2366 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002367 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002368 pch++;
2369 break;
2370 }
2371 return n;
2372}
2373
2374
aliguori376253e2009-03-05 23:01:23 +00002375static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002376{
blueswir1c2efc952007-09-25 17:28:42 +00002377 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002378 int op;
ths3b46e622007-09-17 08:09:54 +00002379
aliguori376253e2009-03-05 23:01:23 +00002380 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002381 for(;;) {
2382 op = *pch;
2383 if (op != '*' && op != '/' && op != '%')
2384 break;
2385 next();
aliguori376253e2009-03-05 23:01:23 +00002386 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002387 switch(op) {
2388 default:
2389 case '*':
2390 val *= val2;
2391 break;
2392 case '/':
2393 case '%':
ths5fafdf22007-09-16 21:08:06 +00002394 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002395 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002396 if (op == '/')
2397 val /= val2;
2398 else
2399 val %= val2;
2400 break;
2401 }
2402 }
2403 return val;
2404}
2405
aliguori376253e2009-03-05 23:01:23 +00002406static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002407{
blueswir1c2efc952007-09-25 17:28:42 +00002408 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002409 int op;
bellard9307c4c2004-04-04 12:57:25 +00002410
aliguori376253e2009-03-05 23:01:23 +00002411 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002412 for(;;) {
2413 op = *pch;
2414 if (op != '&' && op != '|' && op != '^')
2415 break;
2416 next();
aliguori376253e2009-03-05 23:01:23 +00002417 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002418 switch(op) {
2419 default:
2420 case '&':
2421 val &= val2;
2422 break;
2423 case '|':
2424 val |= val2;
2425 break;
2426 case '^':
2427 val ^= val2;
2428 break;
2429 }
2430 }
2431 return val;
2432}
2433
aliguori376253e2009-03-05 23:01:23 +00002434static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002435{
blueswir1c2efc952007-09-25 17:28:42 +00002436 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002437 int op;
bellard9307c4c2004-04-04 12:57:25 +00002438
aliguori376253e2009-03-05 23:01:23 +00002439 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002440 for(;;) {
2441 op = *pch;
2442 if (op != '+' && op != '-')
2443 break;
2444 next();
aliguori376253e2009-03-05 23:01:23 +00002445 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002446 if (op == '+')
2447 val += val2;
2448 else
2449 val -= val2;
2450 }
2451 return val;
2452}
2453
aliguori376253e2009-03-05 23:01:23 +00002454static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002455{
2456 pch = *pp;
2457 if (setjmp(expr_env)) {
2458 *pp = pch;
2459 return -1;
2460 }
blueswir1cd390082008-11-16 13:53:32 +00002461 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002462 pch++;
aliguori376253e2009-03-05 23:01:23 +00002463 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002464 *pp = pch;
2465 return 0;
2466}
2467
2468static int get_str(char *buf, int buf_size, const char **pp)
2469{
2470 const char *p;
2471 char *q;
2472 int c;
2473
bellard81d09122004-07-14 17:21:37 +00002474 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002475 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002476 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002477 p++;
2478 if (*p == '\0') {
2479 fail:
bellard81d09122004-07-14 17:21:37 +00002480 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002481 *pp = p;
2482 return -1;
2483 }
bellard9307c4c2004-04-04 12:57:25 +00002484 if (*p == '\"') {
2485 p++;
2486 while (*p != '\0' && *p != '\"') {
2487 if (*p == '\\') {
2488 p++;
2489 c = *p++;
2490 switch(c) {
2491 case 'n':
2492 c = '\n';
2493 break;
2494 case 'r':
2495 c = '\r';
2496 break;
2497 case '\\':
2498 case '\'':
2499 case '\"':
2500 break;
2501 default:
2502 qemu_printf("unsupported escape code: '\\%c'\n", c);
2503 goto fail;
2504 }
2505 if ((q - buf) < buf_size - 1) {
2506 *q++ = c;
2507 }
2508 } else {
2509 if ((q - buf) < buf_size - 1) {
2510 *q++ = *p;
2511 }
2512 p++;
2513 }
2514 }
2515 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002516 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002517 goto fail;
2518 }
2519 p++;
2520 } else {
blueswir1cd390082008-11-16 13:53:32 +00002521 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002522 if ((q - buf) < buf_size - 1) {
2523 *q++ = *p;
2524 }
2525 p++;
2526 }
bellard9307c4c2004-04-04 12:57:25 +00002527 }
bellard81d09122004-07-14 17:21:37 +00002528 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002529 *pp = p;
2530 return 0;
2531}
2532
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002533/*
2534 * Store the command-name in cmdname, and return a pointer to
2535 * the remaining of the command string.
2536 */
2537static const char *get_command_name(const char *cmdline,
2538 char *cmdname, size_t nlen)
2539{
2540 size_t len;
2541 const char *p, *pstart;
2542
2543 p = cmdline;
2544 while (qemu_isspace(*p))
2545 p++;
2546 if (*p == '\0')
2547 return NULL;
2548 pstart = p;
2549 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2550 p++;
2551 len = p - pstart;
2552 if (len > nlen - 1)
2553 len = nlen - 1;
2554 memcpy(cmdname, pstart, len);
2555 cmdname[len] = '\0';
2556 return p;
2557}
2558
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002559/**
2560 * Read key of 'type' into 'key' and return the current
2561 * 'type' pointer.
2562 */
2563static char *key_get_info(const char *type, char **key)
2564{
2565 size_t len;
2566 char *p, *str;
2567
2568 if (*type == ',')
2569 type++;
2570
2571 p = strchr(type, ':');
2572 if (!p) {
2573 *key = NULL;
2574 return NULL;
2575 }
2576 len = p - type;
2577
2578 str = qemu_malloc(len + 1);
2579 memcpy(str, type, len);
2580 str[len] = '\0';
2581
2582 *key = str;
2583 return ++p;
2584}
2585
bellard9307c4c2004-04-04 12:57:25 +00002586static int default_fmt_format = 'x';
2587static int default_fmt_size = 4;
2588
2589#define MAX_ARGS 16
2590
Anthony Liguoric227f092009-10-01 16:12:16 -05002591static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002592 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002593 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00002594{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002595 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03002596 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05002597 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002598 char cmdname[256];
2599 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002600 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00002601
2602#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002603 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002604#endif
ths3b46e622007-09-17 08:09:54 +00002605
bellard9307c4c2004-04-04 12:57:25 +00002606 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002607 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2608 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002609 return NULL;
ths3b46e622007-09-17 08:09:54 +00002610
bellard9307c4c2004-04-04 12:57:25 +00002611 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002612 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002613 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002614 break;
bellard9dc39cb2004-03-14 21:38:27 +00002615 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002616
2617 if (cmd->name == NULL) {
2618 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002619 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002620 }
bellard9307c4c2004-04-04 12:57:25 +00002621
bellard9307c4c2004-04-04 12:57:25 +00002622 /* parse the parameters */
2623 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002624 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002625 typestr = key_get_info(typestr, &key);
2626 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002627 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002628 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002629 typestr++;
2630 switch(c) {
2631 case 'F':
bellard81d09122004-07-14 17:21:37 +00002632 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002633 case 's':
2634 {
2635 int ret;
ths3b46e622007-09-17 08:09:54 +00002636
blueswir1cd390082008-11-16 13:53:32 +00002637 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002638 p++;
2639 if (*typestr == '?') {
2640 typestr++;
2641 if (*p == '\0') {
2642 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002643 break;
bellard9307c4c2004-04-04 12:57:25 +00002644 }
2645 }
2646 ret = get_str(buf, sizeof(buf), &p);
2647 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002648 switch(c) {
2649 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002650 monitor_printf(mon, "%s: filename expected\n",
2651 cmdname);
bellard81d09122004-07-14 17:21:37 +00002652 break;
2653 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002654 monitor_printf(mon, "%s: block device name expected\n",
2655 cmdname);
bellard81d09122004-07-14 17:21:37 +00002656 break;
2657 default:
aliguori376253e2009-03-05 23:01:23 +00002658 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002659 break;
2660 }
bellard9307c4c2004-04-04 12:57:25 +00002661 goto fail;
2662 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002663 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002664 }
2665 break;
2666 case '/':
2667 {
2668 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002669
blueswir1cd390082008-11-16 13:53:32 +00002670 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002671 p++;
2672 if (*p == '/') {
2673 /* format found */
2674 p++;
2675 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002676 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002677 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002678 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002679 count = count * 10 + (*p - '0');
2680 p++;
2681 }
2682 }
2683 size = -1;
2684 format = -1;
2685 for(;;) {
2686 switch(*p) {
2687 case 'o':
2688 case 'd':
2689 case 'u':
2690 case 'x':
2691 case 'i':
2692 case 'c':
2693 format = *p++;
2694 break;
2695 case 'b':
2696 size = 1;
2697 p++;
2698 break;
2699 case 'h':
2700 size = 2;
2701 p++;
2702 break;
2703 case 'w':
2704 size = 4;
2705 p++;
2706 break;
2707 case 'g':
2708 case 'L':
2709 size = 8;
2710 p++;
2711 break;
2712 default:
2713 goto next;
2714 }
2715 }
2716 next:
blueswir1cd390082008-11-16 13:53:32 +00002717 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002718 monitor_printf(mon, "invalid char in format: '%c'\n",
2719 *p);
bellard9307c4c2004-04-04 12:57:25 +00002720 goto fail;
2721 }
bellard9307c4c2004-04-04 12:57:25 +00002722 if (format < 0)
2723 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002724 if (format != 'i') {
2725 /* for 'i', not specifying a size gives -1 as size */
2726 if (size < 0)
2727 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002728 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002729 }
bellard9307c4c2004-04-04 12:57:25 +00002730 default_fmt_format = format;
2731 } else {
2732 count = 1;
2733 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002734 if (format != 'i') {
2735 size = default_fmt_size;
2736 } else {
2737 size = -1;
2738 }
bellard9307c4c2004-04-04 12:57:25 +00002739 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002740 qdict_put(qdict, "count", qint_from_int(count));
2741 qdict_put(qdict, "format", qint_from_int(format));
2742 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002743 }
2744 break;
2745 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002746 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002747 {
blueswir1c2efc952007-09-25 17:28:42 +00002748 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002749
blueswir1cd390082008-11-16 13:53:32 +00002750 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002751 p++;
bellard34405572004-06-08 00:55:58 +00002752 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002753 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002754 if (*p == '\0') {
2755 typestr++;
2756 break;
2757 }
bellard34405572004-06-08 00:55:58 +00002758 } else {
2759 if (*p == '.') {
2760 p++;
blueswir1cd390082008-11-16 13:53:32 +00002761 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002762 p++;
bellard34405572004-06-08 00:55:58 +00002763 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002764 typestr++;
2765 break;
bellard34405572004-06-08 00:55:58 +00002766 }
2767 }
bellard13224a82006-07-14 22:03:35 +00002768 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002769 }
aliguori376253e2009-03-05 23:01:23 +00002770 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002771 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002772 /* Check if 'i' is greater than 32-bit */
2773 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2774 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
2775 monitor_printf(mon, "integer is for 32-bit values\n");
2776 goto fail;
2777 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002778 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002779 }
2780 break;
2781 case '-':
2782 {
2783 int has_option;
2784 /* option */
ths3b46e622007-09-17 08:09:54 +00002785
bellard9307c4c2004-04-04 12:57:25 +00002786 c = *typestr++;
2787 if (c == '\0')
2788 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002789 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002790 p++;
2791 has_option = 0;
2792 if (*p == '-') {
2793 p++;
2794 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002795 monitor_printf(mon, "%s: unsupported option -%c\n",
2796 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002797 goto fail;
2798 }
2799 p++;
2800 has_option = 1;
2801 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002802 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00002803 }
2804 break;
2805 default:
2806 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002807 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002808 goto fail;
2809 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002810 qemu_free(key);
2811 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002812 }
2813 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002814 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002815 p++;
2816 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002817 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2818 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002819 goto fail;
2820 }
2821
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002822 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002823
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002824fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002825 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002826 return NULL;
2827}
2828
2829static void monitor_handle_command(Monitor *mon, const char *cmdline)
2830{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002831 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002832 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002833
2834 qdict = qdict_new();
2835
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03002836 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002837 if (cmd) {
2838 void (*handler)(Monitor *mon, const QDict *qdict);
2839
2840 qemu_errors_to_mon(mon);
2841
2842 handler = cmd->handler;
2843 handler(mon, qdict);
2844
2845 qemu_errors_to_previous();
2846 }
2847
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002848 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002849}
2850
bellard81d09122004-07-14 17:21:37 +00002851static void cmd_completion(const char *name, const char *list)
2852{
2853 const char *p, *pstart;
2854 char cmd[128];
2855 int len;
2856
2857 p = list;
2858 for(;;) {
2859 pstart = p;
2860 p = strchr(p, '|');
2861 if (!p)
2862 p = pstart + strlen(pstart);
2863 len = p - pstart;
2864 if (len > sizeof(cmd) - 2)
2865 len = sizeof(cmd) - 2;
2866 memcpy(cmd, pstart, len);
2867 cmd[len] = '\0';
2868 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00002869 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002870 }
2871 if (*p == '\0')
2872 break;
2873 p++;
2874 }
2875}
2876
2877static void file_completion(const char *input)
2878{
2879 DIR *ffs;
2880 struct dirent *d;
2881 char path[1024];
2882 char file[1024], file_prefix[1024];
2883 int input_path_len;
2884 const char *p;
2885
ths5fafdf22007-09-16 21:08:06 +00002886 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002887 if (!p) {
2888 input_path_len = 0;
2889 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002890 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002891 } else {
2892 input_path_len = p - input + 1;
2893 memcpy(path, input, input_path_len);
2894 if (input_path_len > sizeof(path) - 1)
2895 input_path_len = sizeof(path) - 1;
2896 path[input_path_len] = '\0';
2897 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2898 }
2899#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00002900 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2901 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00002902#endif
2903 ffs = opendir(path);
2904 if (!ffs)
2905 return;
2906 for(;;) {
2907 struct stat sb;
2908 d = readdir(ffs);
2909 if (!d)
2910 break;
2911 if (strstart(d->d_name, file_prefix, NULL)) {
2912 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002913 if (input_path_len < sizeof(file))
2914 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2915 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002916 /* stat the file to find out if it's a directory.
2917 * In that case add a slash to speed up typing long paths
2918 */
2919 stat(file, &sb);
2920 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002921 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00002922 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002923 }
2924 }
2925 closedir(ffs);
2926}
2927
aliguori51de9762009-03-05 23:00:43 +00002928static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002929{
aliguori51de9762009-03-05 23:00:43 +00002930 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002931 const char *input = opaque;
2932
2933 if (input[0] == '\0' ||
2934 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00002935 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00002936 }
2937}
2938
2939/* NOTE: this parser is an approximate form of the real command parser */
2940static void parse_cmdline(const char *cmdline,
2941 int *pnb_args, char **args)
2942{
2943 const char *p;
2944 int nb_args, ret;
2945 char buf[1024];
2946
2947 p = cmdline;
2948 nb_args = 0;
2949 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002950 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002951 p++;
2952 if (*p == '\0')
2953 break;
2954 if (nb_args >= MAX_ARGS)
2955 break;
2956 ret = get_str(buf, sizeof(buf), &p);
2957 args[nb_args] = qemu_strdup(buf);
2958 nb_args++;
2959 if (ret < 0)
2960 break;
2961 }
2962 *pnb_args = nb_args;
2963}
2964
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002965static const char *next_arg_type(const char *typestr)
2966{
2967 const char *p = strchr(typestr, ':');
2968 return (p != NULL ? ++p : typestr);
2969}
2970
aliguori4c36ba32009-03-05 23:01:37 +00002971static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002972{
2973 const char *cmdname;
2974 char *args[MAX_ARGS];
2975 int nb_args, i, len;
2976 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05002977 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002978 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002979
2980 parse_cmdline(cmdline, &nb_args, args);
2981#ifdef DEBUG_COMPLETION
2982 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00002983 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00002984 }
2985#endif
2986
2987 /* if the line ends with a space, it means we want to complete the
2988 next arg */
2989 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002990 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002991 if (nb_args >= MAX_ARGS)
2992 return;
2993 args[nb_args++] = qemu_strdup("");
2994 }
2995 if (nb_args <= 1) {
2996 /* command completion */
2997 if (nb_args == 0)
2998 cmdname = "";
2999 else
3000 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003001 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003002 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003003 cmd_completion(cmdname, cmd->name);
3004 }
3005 } else {
3006 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003007 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003008 if (compare_cmd(args[0], cmd->name))
3009 goto found;
3010 }
3011 return;
3012 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003013 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003014 for(i = 0; i < nb_args - 2; i++) {
3015 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003016 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003017 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003018 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003019 }
3020 }
3021 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003022 if (*ptype == '-' && ptype[1] != '\0') {
3023 ptype += 2;
3024 }
bellard81d09122004-07-14 17:21:37 +00003025 switch(*ptype) {
3026 case 'F':
3027 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003028 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003029 file_completion(str);
3030 break;
3031 case 'B':
3032 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003033 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003034 bdrv_iterate(block_completion_it, (void *)str);
3035 break;
bellard7fe48482004-10-09 18:08:01 +00003036 case 's':
3037 /* XXX: more generic ? */
3038 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003039 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003040 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3041 cmd_completion(str, cmd->name);
3042 }
bellard64866c32006-05-07 18:03:31 +00003043 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003044 char *sep = strrchr(str, '-');
3045 if (sep)
3046 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003047 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003048 for(key = key_defs; key->name != NULL; key++) {
3049 cmd_completion(str, key->name);
3050 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003051 } else if (!strcmp(cmd->name, "help|?")) {
3052 readline_set_completion_index(cur_mon->rs, strlen(str));
3053 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3054 cmd_completion(str, cmd->name);
3055 }
bellard7fe48482004-10-09 18:08:01 +00003056 }
3057 break;
bellard81d09122004-07-14 17:21:37 +00003058 default:
3059 break;
3060 }
3061 }
3062 for(i = 0; i < nb_args; i++)
3063 qemu_free(args[i]);
3064}
3065
aliguori731b0362009-03-05 23:01:42 +00003066static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003067{
aliguori731b0362009-03-05 23:01:42 +00003068 Monitor *mon = opaque;
3069
3070 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003071}
3072
aliguori731b0362009-03-05 23:01:42 +00003073static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003074{
aliguori731b0362009-03-05 23:01:42 +00003075 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003076 int i;
aliguori376253e2009-03-05 23:01:23 +00003077
aliguori731b0362009-03-05 23:01:42 +00003078 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003079
aliguoricde76ee2009-03-05 23:01:51 +00003080 if (cur_mon->rs) {
3081 for (i = 0; i < size; i++)
3082 readline_handle_byte(cur_mon->rs, buf[i]);
3083 } else {
3084 if (size == 0 || buf[size - 1] != 0)
3085 monitor_printf(cur_mon, "corrupted command\n");
3086 else
3087 monitor_handle_command(cur_mon, (char *)buf);
3088 }
aliguori731b0362009-03-05 23:01:42 +00003089
3090 cur_mon = old_mon;
3091}
aliguorid8f44602008-10-06 13:52:44 +00003092
aliguori376253e2009-03-05 23:01:23 +00003093static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003094{
aliguori731b0362009-03-05 23:01:42 +00003095 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003096 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003097 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003098}
3099
aliguoricde76ee2009-03-05 23:01:51 +00003100int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003101{
aliguoricde76ee2009-03-05 23:01:51 +00003102 if (!mon->rs)
3103 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003104 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003105 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003106}
3107
aliguori376253e2009-03-05 23:01:23 +00003108void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003109{
aliguoricde76ee2009-03-05 23:01:51 +00003110 if (!mon->rs)
3111 return;
aliguori731b0362009-03-05 23:01:42 +00003112 if (--mon->suspend_cnt == 0)
3113 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003114}
3115
aliguori731b0362009-03-05 23:01:42 +00003116static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003117{
aliguori376253e2009-03-05 23:01:23 +00003118 Monitor *mon = opaque;
3119
aliguori2724b182009-03-05 23:01:47 +00003120 switch (event) {
3121 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003122 mon->mux_out = 0;
3123 if (mon->reset_seen) {
3124 readline_restart(mon->rs);
3125 monitor_resume(mon);
3126 monitor_flush(mon);
3127 } else {
3128 mon->suspend_cnt = 0;
3129 }
aliguori2724b182009-03-05 23:01:47 +00003130 break;
ths86e94de2007-01-05 22:01:59 +00003131
aliguori2724b182009-03-05 23:01:47 +00003132 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003133 if (mon->reset_seen) {
3134 if (mon->suspend_cnt == 0) {
3135 monitor_printf(mon, "\n");
3136 }
3137 monitor_flush(mon);
3138 monitor_suspend(mon);
3139 } else {
3140 mon->suspend_cnt++;
3141 }
3142 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00003143 break;
3144
3145 case CHR_EVENT_RESET:
3146 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3147 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003148 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00003149 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003150 }
3151 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00003152 break;
3153 }
ths86e94de2007-01-05 22:01:59 +00003154}
3155
aliguori76655d62009-03-06 20:27:37 +00003156
3157/*
3158 * Local variables:
3159 * c-indent-level: 4
3160 * c-basic-offset: 4
3161 * tab-width: 8
3162 * End:
3163 */
3164
aliguori731b0362009-03-05 23:01:42 +00003165void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003166{
aliguori731b0362009-03-05 23:01:42 +00003167 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003168 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003169
3170 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003171 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003172 is_first_init = 0;
3173 }
aliguori87127162009-03-05 23:01:29 +00003174
3175 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003176
aliguori87127162009-03-05 23:01:29 +00003177 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003178 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00003179 if (flags & MONITOR_USE_READLINE) {
3180 mon->rs = readline_init(mon, monitor_find_completion);
3181 monitor_read_command(mon, 0);
3182 }
aliguori87127162009-03-05 23:01:29 +00003183
aliguori731b0362009-03-05 23:01:42 +00003184 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3185 mon);
aliguori87127162009-03-05 23:01:29 +00003186
Blue Swirl72cf2d42009-09-12 07:36:22 +00003187 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003188 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003189 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003190}
3191
aliguori376253e2009-03-05 23:01:23 +00003192static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003193{
aliguoribb5fc202009-03-05 23:01:15 +00003194 BlockDriverState *bs = opaque;
3195 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003196
aliguoribb5fc202009-03-05 23:01:15 +00003197 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003198 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003199 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003200 }
aliguori731b0362009-03-05 23:01:42 +00003201 if (mon->password_completion_cb)
3202 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003203
aliguori731b0362009-03-05 23:01:42 +00003204 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003205}
aliguoric0f4ce72009-03-05 23:01:01 +00003206
aliguori376253e2009-03-05 23:01:23 +00003207void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003208 BlockDriverCompletionFunc *completion_cb,
3209 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003210{
aliguoricde76ee2009-03-05 23:01:51 +00003211 int err;
3212
aliguoribb5fc202009-03-05 23:01:15 +00003213 if (!bdrv_key_required(bs)) {
3214 if (completion_cb)
3215 completion_cb(opaque, 0);
3216 return;
3217 }
aliguoric0f4ce72009-03-05 23:01:01 +00003218
aliguori376253e2009-03-05 23:01:23 +00003219 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3220 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003221
aliguori731b0362009-03-05 23:01:42 +00003222 mon->password_completion_cb = completion_cb;
3223 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003224
aliguoricde76ee2009-03-05 23:01:51 +00003225 err = monitor_read_password(mon, bdrv_password_cb, bs);
3226
3227 if (err && completion_cb)
3228 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003229}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003230
3231typedef struct QemuErrorSink QemuErrorSink;
3232struct QemuErrorSink {
3233 enum {
3234 ERR_SINK_FILE,
3235 ERR_SINK_MONITOR,
3236 } dest;
3237 union {
3238 FILE *fp;
3239 Monitor *mon;
3240 };
3241 QemuErrorSink *previous;
3242};
3243
Blue Swirl528e93a2009-08-31 15:14:40 +00003244static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003245
3246void qemu_errors_to_file(FILE *fp)
3247{
3248 QemuErrorSink *sink;
3249
3250 sink = qemu_mallocz(sizeof(*sink));
3251 sink->dest = ERR_SINK_FILE;
3252 sink->fp = fp;
3253 sink->previous = qemu_error_sink;
3254 qemu_error_sink = sink;
3255}
3256
3257void qemu_errors_to_mon(Monitor *mon)
3258{
3259 QemuErrorSink *sink;
3260
3261 sink = qemu_mallocz(sizeof(*sink));
3262 sink->dest = ERR_SINK_MONITOR;
3263 sink->mon = mon;
3264 sink->previous = qemu_error_sink;
3265 qemu_error_sink = sink;
3266}
3267
3268void qemu_errors_to_previous(void)
3269{
3270 QemuErrorSink *sink;
3271
3272 assert(qemu_error_sink != NULL);
3273 sink = qemu_error_sink;
3274 qemu_error_sink = sink->previous;
3275 qemu_free(sink);
3276}
3277
3278void qemu_error(const char *fmt, ...)
3279{
3280 va_list args;
3281
3282 assert(qemu_error_sink != NULL);
3283 switch (qemu_error_sink->dest) {
3284 case ERR_SINK_FILE:
3285 va_start(args, fmt);
3286 vfprintf(qemu_error_sink->fp, fmt, args);
3287 va_end(args);
3288 break;
3289 case ERR_SINK_MONITOR:
3290 va_start(args, fmt);
3291 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3292 va_end(args);
3293 break;
3294 }
3295}