blob: ae0354a8380748fea8b5efcef53d6dadeb454174 [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[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001816 {
1817 .name = "version",
1818 .args_type = "",
1819 .handler = do_info_version,
1820 .params = "",
1821 .help = "show the version of QEMU",
1822 },
1823 {
1824 .name = "network",
1825 .args_type = "",
1826 .handler = do_info_network,
1827 .params = "",
1828 .help = "show the network state",
1829 },
1830 {
1831 .name = "chardev",
1832 .args_type = "",
1833 .handler = qemu_chr_info,
1834 .params = "",
1835 .help = "show the character devices",
1836 },
1837 {
1838 .name = "block",
1839 .args_type = "",
1840 .handler = bdrv_info,
1841 .params = "",
1842 .help = "show the block devices",
1843 },
1844 {
1845 .name = "blockstats",
1846 .args_type = "",
1847 .handler = bdrv_info_stats,
1848 .params = "",
1849 .help = "show block device statistics",
1850 },
1851 {
1852 .name = "registers",
1853 .args_type = "",
1854 .handler = do_info_registers,
1855 .params = "",
1856 .help = "show the cpu registers",
1857 },
1858 {
1859 .name = "cpus",
1860 .args_type = "",
1861 .handler = do_info_cpus,
1862 .params = "",
1863 .help = "show infos for each CPU",
1864 },
1865 {
1866 .name = "history",
1867 .args_type = "",
1868 .handler = do_info_history,
1869 .params = "",
1870 .help = "show the command line history",
1871 },
1872 {
1873 .name = "irq",
1874 .args_type = "",
1875 .handler = irq_info,
1876 .params = "",
1877 .help = "show the interrupts statistics (if available)",
1878 },
1879 {
1880 .name = "pic",
1881 .args_type = "",
1882 .handler = pic_info,
1883 .params = "",
1884 .help = "show i8259 (PIC) state",
1885 },
1886 {
1887 .name = "pci",
1888 .args_type = "",
1889 .handler = pci_info,
1890 .params = "",
1891 .help = "show PCI info",
1892 },
aurel327c664e22009-03-03 06:12:22 +00001893#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001894 {
1895 .name = "tlb",
1896 .args_type = "",
1897 .handler = tlb_info,
1898 .params = "",
1899 .help = "show virtual to physical memory mappings",
1900 },
aurel327c664e22009-03-03 06:12:22 +00001901#endif
1902#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001903 {
1904 .name = "mem",
1905 .args_type = "",
1906 .handler = mem_info,
1907 .params = "",
1908 .help = "show the active virtual memory mappings",
1909 },
1910 {
1911 .name = "hpet",
1912 .args_type = "",
1913 .handler = do_info_hpet,
1914 .params = "",
1915 .help = "show state of HPET",
1916 },
bellardb86bda52004-09-18 19:32:46 +00001917#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001918 {
1919 .name = "jit",
1920 .args_type = "",
1921 .handler = do_info_jit,
1922 .params = "",
1923 .help = "show dynamic compiler info",
1924 },
1925 {
1926 .name = "kvm",
1927 .args_type = "",
1928 .handler = do_info_kvm,
1929 .params = "",
1930 .help = "show KVM information",
1931 },
1932 {
1933 .name = "numa",
1934 .args_type = "",
1935 .handler = do_info_numa,
1936 .params = "",
1937 .help = "show NUMA information",
1938 },
1939 {
1940 .name = "usb",
1941 .args_type = "",
1942 .handler = usb_info,
1943 .params = "",
1944 .help = "show guest USB devices",
1945 },
1946 {
1947 .name = "usbhost",
1948 .args_type = "",
1949 .handler = usb_host_info,
1950 .params = "",
1951 .help = "show host USB devices",
1952 },
1953 {
1954 .name = "profile",
1955 .args_type = "",
1956 .handler = do_info_profile,
1957 .params = "",
1958 .help = "show profiling information",
1959 },
1960 {
1961 .name = "capture",
1962 .args_type = "",
1963 .handler = do_info_capture,
1964 .params = "",
1965 .help = "show capture information",
1966 },
1967 {
1968 .name = "snapshots",
1969 .args_type = "",
1970 .handler = do_info_snapshots,
1971 .params = "",
1972 .help = "show the currently saved VM snapshots",
1973 },
1974 {
1975 .name = "status",
1976 .args_type = "",
1977 .handler = do_info_status,
1978 .params = "",
1979 .help = "show the current VM status (running|paused)",
1980 },
1981 {
1982 .name = "pcmcia",
1983 .args_type = "",
1984 .handler = pcmcia_info,
1985 .params = "",
1986 .help = "show guest PCMCIA status",
1987 },
1988 {
1989 .name = "mice",
1990 .args_type = "",
1991 .handler = do_info_mice,
1992 .params = "",
1993 .help = "show which guest mouse is receiving events",
1994 },
1995 {
1996 .name = "vnc",
1997 .args_type = "",
1998 .handler = do_info_vnc,
1999 .params = "",
2000 .help = "show the vnc server status",
2001 },
2002 {
2003 .name = "name",
2004 .args_type = "",
2005 .handler = do_info_name,
2006 .params = "",
2007 .help = "show the current VM name",
2008 },
2009 {
2010 .name = "uuid",
2011 .args_type = "",
2012 .handler = do_info_uuid,
2013 .params = "",
2014 .help = "show the current VM UUID",
2015 },
j_mayer76a66252007-03-07 08:32:30 +00002016#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002017 {
2018 .name = "cpustats",
2019 .args_type = "",
2020 .handler = do_info_cpu_stats,
2021 .params = "",
2022 .help = "show CPU statistics",
2023 },
j_mayer76a66252007-03-07 08:32:30 +00002024#endif
blueswir131a60e22007-10-26 18:42:59 +00002025#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002026 {
2027 .name = "usernet",
2028 .args_type = "",
2029 .handler = do_info_usernet,
2030 .params = "",
2031 .help = "show user network stack connection states",
2032 },
blueswir131a60e22007-10-26 18:42:59 +00002033#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002034 {
2035 .name = "migrate",
2036 .args_type = "",
2037 .handler = do_info_migrate,
2038 .params = "",
2039 .help = "show migration status",
2040 },
2041 {
2042 .name = "balloon",
2043 .args_type = "",
2044 .handler = do_info_balloon,
2045 .params = "",
2046 .help = "show balloon information",
2047 },
2048 {
2049 .name = "qtree",
2050 .args_type = "",
2051 .handler = do_info_qtree,
2052 .params = "",
2053 .help = "show device tree",
2054 },
2055 {
2056 .name = "qdm",
2057 .args_type = "",
2058 .handler = do_info_qdm,
2059 .params = "",
2060 .help = "show qdev device model list",
2061 },
2062 {
2063 .name = "roms",
2064 .args_type = "",
2065 .handler = do_info_roms,
2066 .params = "",
2067 .help = "show roms",
2068 },
2069 {
2070 .name = NULL,
2071 },
bellard9dc39cb2004-03-14 21:38:27 +00002072};
2073
bellard9307c4c2004-04-04 12:57:25 +00002074/*******************************************************************/
2075
2076static const char *pch;
2077static jmp_buf expr_env;
2078
bellard92a31b12005-02-10 22:00:52 +00002079#define MD_TLONG 0
2080#define MD_I32 1
2081
bellard9307c4c2004-04-04 12:57:25 +00002082typedef struct MonitorDef {
2083 const char *name;
2084 int offset;
blueswir18662d652008-10-02 18:32:44 +00002085 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002086 int type;
bellard9307c4c2004-04-04 12:57:25 +00002087} MonitorDef;
2088
bellard57206fd2004-04-25 18:54:52 +00002089#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002090static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002091{
bellard6a00d602005-11-21 23:25:50 +00002092 CPUState *env = mon_get_cpu();
2093 if (!env)
2094 return 0;
2095 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002096}
2097#endif
2098
bellarda541f292004-04-12 20:39:29 +00002099#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002100static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002101{
bellard6a00d602005-11-21 23:25:50 +00002102 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002103 unsigned int u;
2104 int i;
2105
bellard6a00d602005-11-21 23:25:50 +00002106 if (!env)
2107 return 0;
2108
bellarda541f292004-04-12 20:39:29 +00002109 u = 0;
2110 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002111 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002112
2113 return u;
2114}
2115
blueswir18662d652008-10-02 18:32:44 +00002116static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002117{
bellard6a00d602005-11-21 23:25:50 +00002118 CPUState *env = mon_get_cpu();
2119 if (!env)
2120 return 0;
j_mayer0411a972007-10-25 21:35:50 +00002121 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002122}
2123
blueswir18662d652008-10-02 18:32:44 +00002124static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002125{
bellard6a00d602005-11-21 23:25:50 +00002126 CPUState *env = mon_get_cpu();
2127 if (!env)
2128 return 0;
aurel323d7b4172008-10-21 11:28:46 +00002129 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002130}
bellard9fddaa02004-05-21 12:59:32 +00002131
blueswir18662d652008-10-02 18:32:44 +00002132static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002133{
bellard6a00d602005-11-21 23:25:50 +00002134 CPUState *env = mon_get_cpu();
2135 if (!env)
2136 return 0;
2137 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002138}
2139
blueswir18662d652008-10-02 18:32:44 +00002140static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002141{
bellard6a00d602005-11-21 23:25:50 +00002142 CPUState *env = mon_get_cpu();
2143 if (!env)
2144 return 0;
2145 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002146}
2147
blueswir18662d652008-10-02 18:32:44 +00002148static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002149{
bellard6a00d602005-11-21 23:25:50 +00002150 CPUState *env = mon_get_cpu();
2151 if (!env)
2152 return 0;
2153 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002154}
bellarda541f292004-04-12 20:39:29 +00002155#endif
2156
bellarde95c8d52004-09-30 22:22:08 +00002157#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002158#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002159static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002160{
bellard6a00d602005-11-21 23:25:50 +00002161 CPUState *env = mon_get_cpu();
2162 if (!env)
2163 return 0;
2164 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002165}
bellard7b936c02005-10-30 17:05:13 +00002166#endif
bellarde95c8d52004-09-30 22:22:08 +00002167
blueswir18662d652008-10-02 18:32:44 +00002168static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002169{
bellard6a00d602005-11-21 23:25:50 +00002170 CPUState *env = mon_get_cpu();
2171 if (!env)
2172 return 0;
2173 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002174}
2175#endif
2176
blueswir18662d652008-10-02 18:32:44 +00002177static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002178#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002179
2180#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002181 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002182 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002183 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002184
bellard9307c4c2004-04-04 12:57:25 +00002185 { "eax", offsetof(CPUState, regs[0]) },
2186 { "ecx", offsetof(CPUState, regs[1]) },
2187 { "edx", offsetof(CPUState, regs[2]) },
2188 { "ebx", offsetof(CPUState, regs[3]) },
2189 { "esp|sp", offsetof(CPUState, regs[4]) },
2190 { "ebp|fp", offsetof(CPUState, regs[5]) },
2191 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002192 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002193#ifdef TARGET_X86_64
2194 { "r8", offsetof(CPUState, regs[8]) },
2195 { "r9", offsetof(CPUState, regs[9]) },
2196 { "r10", offsetof(CPUState, regs[10]) },
2197 { "r11", offsetof(CPUState, regs[11]) },
2198 { "r12", offsetof(CPUState, regs[12]) },
2199 { "r13", offsetof(CPUState, regs[13]) },
2200 { "r14", offsetof(CPUState, regs[14]) },
2201 { "r15", offsetof(CPUState, regs[15]) },
2202#endif
bellard9307c4c2004-04-04 12:57:25 +00002203 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002204 { "eip", offsetof(CPUState, eip) },
2205 SEG("cs", R_CS)
2206 SEG("ds", R_DS)
2207 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002208 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002209 SEG("fs", R_FS)
2210 SEG("gs", R_GS)
2211 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002212#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002213 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002214 { "r0", offsetof(CPUState, gpr[0]) },
2215 { "r1", offsetof(CPUState, gpr[1]) },
2216 { "r2", offsetof(CPUState, gpr[2]) },
2217 { "r3", offsetof(CPUState, gpr[3]) },
2218 { "r4", offsetof(CPUState, gpr[4]) },
2219 { "r5", offsetof(CPUState, gpr[5]) },
2220 { "r6", offsetof(CPUState, gpr[6]) },
2221 { "r7", offsetof(CPUState, gpr[7]) },
2222 { "r8", offsetof(CPUState, gpr[8]) },
2223 { "r9", offsetof(CPUState, gpr[9]) },
2224 { "r10", offsetof(CPUState, gpr[10]) },
2225 { "r11", offsetof(CPUState, gpr[11]) },
2226 { "r12", offsetof(CPUState, gpr[12]) },
2227 { "r13", offsetof(CPUState, gpr[13]) },
2228 { "r14", offsetof(CPUState, gpr[14]) },
2229 { "r15", offsetof(CPUState, gpr[15]) },
2230 { "r16", offsetof(CPUState, gpr[16]) },
2231 { "r17", offsetof(CPUState, gpr[17]) },
2232 { "r18", offsetof(CPUState, gpr[18]) },
2233 { "r19", offsetof(CPUState, gpr[19]) },
2234 { "r20", offsetof(CPUState, gpr[20]) },
2235 { "r21", offsetof(CPUState, gpr[21]) },
2236 { "r22", offsetof(CPUState, gpr[22]) },
2237 { "r23", offsetof(CPUState, gpr[23]) },
2238 { "r24", offsetof(CPUState, gpr[24]) },
2239 { "r25", offsetof(CPUState, gpr[25]) },
2240 { "r26", offsetof(CPUState, gpr[26]) },
2241 { "r27", offsetof(CPUState, gpr[27]) },
2242 { "r28", offsetof(CPUState, gpr[28]) },
2243 { "r29", offsetof(CPUState, gpr[29]) },
2244 { "r30", offsetof(CPUState, gpr[30]) },
2245 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002246 /* Floating point registers */
2247 { "f0", offsetof(CPUState, fpr[0]) },
2248 { "f1", offsetof(CPUState, fpr[1]) },
2249 { "f2", offsetof(CPUState, fpr[2]) },
2250 { "f3", offsetof(CPUState, fpr[3]) },
2251 { "f4", offsetof(CPUState, fpr[4]) },
2252 { "f5", offsetof(CPUState, fpr[5]) },
2253 { "f6", offsetof(CPUState, fpr[6]) },
2254 { "f7", offsetof(CPUState, fpr[7]) },
2255 { "f8", offsetof(CPUState, fpr[8]) },
2256 { "f9", offsetof(CPUState, fpr[9]) },
2257 { "f10", offsetof(CPUState, fpr[10]) },
2258 { "f11", offsetof(CPUState, fpr[11]) },
2259 { "f12", offsetof(CPUState, fpr[12]) },
2260 { "f13", offsetof(CPUState, fpr[13]) },
2261 { "f14", offsetof(CPUState, fpr[14]) },
2262 { "f15", offsetof(CPUState, fpr[15]) },
2263 { "f16", offsetof(CPUState, fpr[16]) },
2264 { "f17", offsetof(CPUState, fpr[17]) },
2265 { "f18", offsetof(CPUState, fpr[18]) },
2266 { "f19", offsetof(CPUState, fpr[19]) },
2267 { "f20", offsetof(CPUState, fpr[20]) },
2268 { "f21", offsetof(CPUState, fpr[21]) },
2269 { "f22", offsetof(CPUState, fpr[22]) },
2270 { "f23", offsetof(CPUState, fpr[23]) },
2271 { "f24", offsetof(CPUState, fpr[24]) },
2272 { "f25", offsetof(CPUState, fpr[25]) },
2273 { "f26", offsetof(CPUState, fpr[26]) },
2274 { "f27", offsetof(CPUState, fpr[27]) },
2275 { "f28", offsetof(CPUState, fpr[28]) },
2276 { "f29", offsetof(CPUState, fpr[29]) },
2277 { "f30", offsetof(CPUState, fpr[30]) },
2278 { "f31", offsetof(CPUState, fpr[31]) },
2279 { "fpscr", offsetof(CPUState, fpscr) },
2280 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002281 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002282 { "lr", offsetof(CPUState, lr) },
2283 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002284 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002285 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002286 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002287 { "msr", 0, &monitor_get_msr, },
2288 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002289 { "tbu", 0, &monitor_get_tbu, },
2290 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002291#if defined(TARGET_PPC64)
2292 /* Address space register */
2293 { "asr", offsetof(CPUState, asr) },
2294#endif
2295 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002296 { "sdr1", offsetof(CPUState, sdr1) },
2297 { "sr0", offsetof(CPUState, sr[0]) },
2298 { "sr1", offsetof(CPUState, sr[1]) },
2299 { "sr2", offsetof(CPUState, sr[2]) },
2300 { "sr3", offsetof(CPUState, sr[3]) },
2301 { "sr4", offsetof(CPUState, sr[4]) },
2302 { "sr5", offsetof(CPUState, sr[5]) },
2303 { "sr6", offsetof(CPUState, sr[6]) },
2304 { "sr7", offsetof(CPUState, sr[7]) },
2305 { "sr8", offsetof(CPUState, sr[8]) },
2306 { "sr9", offsetof(CPUState, sr[9]) },
2307 { "sr10", offsetof(CPUState, sr[10]) },
2308 { "sr11", offsetof(CPUState, sr[11]) },
2309 { "sr12", offsetof(CPUState, sr[12]) },
2310 { "sr13", offsetof(CPUState, sr[13]) },
2311 { "sr14", offsetof(CPUState, sr[14]) },
2312 { "sr15", offsetof(CPUState, sr[15]) },
2313 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002314#elif defined(TARGET_SPARC)
2315 { "g0", offsetof(CPUState, gregs[0]) },
2316 { "g1", offsetof(CPUState, gregs[1]) },
2317 { "g2", offsetof(CPUState, gregs[2]) },
2318 { "g3", offsetof(CPUState, gregs[3]) },
2319 { "g4", offsetof(CPUState, gregs[4]) },
2320 { "g5", offsetof(CPUState, gregs[5]) },
2321 { "g6", offsetof(CPUState, gregs[6]) },
2322 { "g7", offsetof(CPUState, gregs[7]) },
2323 { "o0", 0, monitor_get_reg },
2324 { "o1", 1, monitor_get_reg },
2325 { "o2", 2, monitor_get_reg },
2326 { "o3", 3, monitor_get_reg },
2327 { "o4", 4, monitor_get_reg },
2328 { "o5", 5, monitor_get_reg },
2329 { "o6", 6, monitor_get_reg },
2330 { "o7", 7, monitor_get_reg },
2331 { "l0", 8, monitor_get_reg },
2332 { "l1", 9, monitor_get_reg },
2333 { "l2", 10, monitor_get_reg },
2334 { "l3", 11, monitor_get_reg },
2335 { "l4", 12, monitor_get_reg },
2336 { "l5", 13, monitor_get_reg },
2337 { "l6", 14, monitor_get_reg },
2338 { "l7", 15, monitor_get_reg },
2339 { "i0", 16, monitor_get_reg },
2340 { "i1", 17, monitor_get_reg },
2341 { "i2", 18, monitor_get_reg },
2342 { "i3", 19, monitor_get_reg },
2343 { "i4", 20, monitor_get_reg },
2344 { "i5", 21, monitor_get_reg },
2345 { "i6", 22, monitor_get_reg },
2346 { "i7", 23, monitor_get_reg },
2347 { "pc", offsetof(CPUState, pc) },
2348 { "npc", offsetof(CPUState, npc) },
2349 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002350#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002351 { "psr", 0, &monitor_get_psr, },
2352 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002353#endif
bellarde95c8d52004-09-30 22:22:08 +00002354 { "tbr", offsetof(CPUState, tbr) },
2355 { "fsr", offsetof(CPUState, fsr) },
2356 { "f0", offsetof(CPUState, fpr[0]) },
2357 { "f1", offsetof(CPUState, fpr[1]) },
2358 { "f2", offsetof(CPUState, fpr[2]) },
2359 { "f3", offsetof(CPUState, fpr[3]) },
2360 { "f4", offsetof(CPUState, fpr[4]) },
2361 { "f5", offsetof(CPUState, fpr[5]) },
2362 { "f6", offsetof(CPUState, fpr[6]) },
2363 { "f7", offsetof(CPUState, fpr[7]) },
2364 { "f8", offsetof(CPUState, fpr[8]) },
2365 { "f9", offsetof(CPUState, fpr[9]) },
2366 { "f10", offsetof(CPUState, fpr[10]) },
2367 { "f11", offsetof(CPUState, fpr[11]) },
2368 { "f12", offsetof(CPUState, fpr[12]) },
2369 { "f13", offsetof(CPUState, fpr[13]) },
2370 { "f14", offsetof(CPUState, fpr[14]) },
2371 { "f15", offsetof(CPUState, fpr[15]) },
2372 { "f16", offsetof(CPUState, fpr[16]) },
2373 { "f17", offsetof(CPUState, fpr[17]) },
2374 { "f18", offsetof(CPUState, fpr[18]) },
2375 { "f19", offsetof(CPUState, fpr[19]) },
2376 { "f20", offsetof(CPUState, fpr[20]) },
2377 { "f21", offsetof(CPUState, fpr[21]) },
2378 { "f22", offsetof(CPUState, fpr[22]) },
2379 { "f23", offsetof(CPUState, fpr[23]) },
2380 { "f24", offsetof(CPUState, fpr[24]) },
2381 { "f25", offsetof(CPUState, fpr[25]) },
2382 { "f26", offsetof(CPUState, fpr[26]) },
2383 { "f27", offsetof(CPUState, fpr[27]) },
2384 { "f28", offsetof(CPUState, fpr[28]) },
2385 { "f29", offsetof(CPUState, fpr[29]) },
2386 { "f30", offsetof(CPUState, fpr[30]) },
2387 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002388#ifdef TARGET_SPARC64
2389 { "f32", offsetof(CPUState, fpr[32]) },
2390 { "f34", offsetof(CPUState, fpr[34]) },
2391 { "f36", offsetof(CPUState, fpr[36]) },
2392 { "f38", offsetof(CPUState, fpr[38]) },
2393 { "f40", offsetof(CPUState, fpr[40]) },
2394 { "f42", offsetof(CPUState, fpr[42]) },
2395 { "f44", offsetof(CPUState, fpr[44]) },
2396 { "f46", offsetof(CPUState, fpr[46]) },
2397 { "f48", offsetof(CPUState, fpr[48]) },
2398 { "f50", offsetof(CPUState, fpr[50]) },
2399 { "f52", offsetof(CPUState, fpr[52]) },
2400 { "f54", offsetof(CPUState, fpr[54]) },
2401 { "f56", offsetof(CPUState, fpr[56]) },
2402 { "f58", offsetof(CPUState, fpr[58]) },
2403 { "f60", offsetof(CPUState, fpr[60]) },
2404 { "f62", offsetof(CPUState, fpr[62]) },
2405 { "asi", offsetof(CPUState, asi) },
2406 { "pstate", offsetof(CPUState, pstate) },
2407 { "cansave", offsetof(CPUState, cansave) },
2408 { "canrestore", offsetof(CPUState, canrestore) },
2409 { "otherwin", offsetof(CPUState, otherwin) },
2410 { "wstate", offsetof(CPUState, wstate) },
2411 { "cleanwin", offsetof(CPUState, cleanwin) },
2412 { "fprs", offsetof(CPUState, fprs) },
2413#endif
bellard9307c4c2004-04-04 12:57:25 +00002414#endif
2415 { NULL },
2416};
2417
aliguori376253e2009-03-05 23:01:23 +00002418static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002419{
aliguori376253e2009-03-05 23:01:23 +00002420 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002421 longjmp(expr_env, 1);
2422}
2423
bellard6a00d602005-11-21 23:25:50 +00002424/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002425static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002426{
blueswir18662d652008-10-02 18:32:44 +00002427 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002428 void *ptr;
2429
bellard9307c4c2004-04-04 12:57:25 +00002430 for(md = monitor_defs; md->name != NULL; md++) {
2431 if (compare_cmd(name, md->name)) {
2432 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002433 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002434 } else {
bellard6a00d602005-11-21 23:25:50 +00002435 CPUState *env = mon_get_cpu();
2436 if (!env)
2437 return -2;
2438 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002439 switch(md->type) {
2440 case MD_I32:
2441 *pval = *(int32_t *)ptr;
2442 break;
2443 case MD_TLONG:
2444 *pval = *(target_long *)ptr;
2445 break;
2446 default:
2447 *pval = 0;
2448 break;
2449 }
bellard9307c4c2004-04-04 12:57:25 +00002450 }
2451 return 0;
2452 }
2453 }
2454 return -1;
2455}
2456
2457static void next(void)
2458{
Blue Swirl660f11b2009-07-31 21:16:51 +00002459 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002460 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002461 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002462 pch++;
2463 }
2464}
2465
aliguori376253e2009-03-05 23:01:23 +00002466static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002467
aliguori376253e2009-03-05 23:01:23 +00002468static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002469{
blueswir1c2efc952007-09-25 17:28:42 +00002470 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002471 char *p;
bellard6a00d602005-11-21 23:25:50 +00002472 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002473
2474 switch(*pch) {
2475 case '+':
2476 next();
aliguori376253e2009-03-05 23:01:23 +00002477 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002478 break;
2479 case '-':
2480 next();
aliguori376253e2009-03-05 23:01:23 +00002481 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002482 break;
2483 case '~':
2484 next();
aliguori376253e2009-03-05 23:01:23 +00002485 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002486 break;
2487 case '(':
2488 next();
aliguori376253e2009-03-05 23:01:23 +00002489 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002490 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002491 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002492 }
2493 next();
2494 break;
bellard81d09122004-07-14 17:21:37 +00002495 case '\'':
2496 pch++;
2497 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002498 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002499 n = *pch;
2500 pch++;
2501 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002502 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002503 next();
2504 break;
bellard9307c4c2004-04-04 12:57:25 +00002505 case '$':
2506 {
2507 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002508 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002509
bellard9307c4c2004-04-04 12:57:25 +00002510 pch++;
2511 q = buf;
2512 while ((*pch >= 'a' && *pch <= 'z') ||
2513 (*pch >= 'A' && *pch <= 'Z') ||
2514 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002515 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002516 if ((q - buf) < sizeof(buf) - 1)
2517 *q++ = *pch;
2518 pch++;
2519 }
blueswir1cd390082008-11-16 13:53:32 +00002520 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002521 pch++;
2522 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002523 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002524 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002525 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002526 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002527 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002528 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002529 }
2530 break;
2531 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002532 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002533 n = 0;
2534 break;
2535 default:
blueswir17743e582007-09-24 18:39:04 +00002536#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002537 n = strtoull(pch, &p, 0);
2538#else
bellard9307c4c2004-04-04 12:57:25 +00002539 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002540#endif
bellard9307c4c2004-04-04 12:57:25 +00002541 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002542 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002543 }
2544 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002545 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002546 pch++;
2547 break;
2548 }
2549 return n;
2550}
2551
2552
aliguori376253e2009-03-05 23:01:23 +00002553static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002554{
blueswir1c2efc952007-09-25 17:28:42 +00002555 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002556 int op;
ths3b46e622007-09-17 08:09:54 +00002557
aliguori376253e2009-03-05 23:01:23 +00002558 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002559 for(;;) {
2560 op = *pch;
2561 if (op != '*' && op != '/' && op != '%')
2562 break;
2563 next();
aliguori376253e2009-03-05 23:01:23 +00002564 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002565 switch(op) {
2566 default:
2567 case '*':
2568 val *= val2;
2569 break;
2570 case '/':
2571 case '%':
ths5fafdf22007-09-16 21:08:06 +00002572 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002573 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002574 if (op == '/')
2575 val /= val2;
2576 else
2577 val %= val2;
2578 break;
2579 }
2580 }
2581 return val;
2582}
2583
aliguori376253e2009-03-05 23:01:23 +00002584static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002585{
blueswir1c2efc952007-09-25 17:28:42 +00002586 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002587 int op;
bellard9307c4c2004-04-04 12:57:25 +00002588
aliguori376253e2009-03-05 23:01:23 +00002589 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002590 for(;;) {
2591 op = *pch;
2592 if (op != '&' && op != '|' && op != '^')
2593 break;
2594 next();
aliguori376253e2009-03-05 23:01:23 +00002595 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002596 switch(op) {
2597 default:
2598 case '&':
2599 val &= val2;
2600 break;
2601 case '|':
2602 val |= val2;
2603 break;
2604 case '^':
2605 val ^= val2;
2606 break;
2607 }
2608 }
2609 return val;
2610}
2611
aliguori376253e2009-03-05 23:01:23 +00002612static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002613{
blueswir1c2efc952007-09-25 17:28:42 +00002614 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002615 int op;
bellard9307c4c2004-04-04 12:57:25 +00002616
aliguori376253e2009-03-05 23:01:23 +00002617 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002618 for(;;) {
2619 op = *pch;
2620 if (op != '+' && op != '-')
2621 break;
2622 next();
aliguori376253e2009-03-05 23:01:23 +00002623 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002624 if (op == '+')
2625 val += val2;
2626 else
2627 val -= val2;
2628 }
2629 return val;
2630}
2631
aliguori376253e2009-03-05 23:01:23 +00002632static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002633{
2634 pch = *pp;
2635 if (setjmp(expr_env)) {
2636 *pp = pch;
2637 return -1;
2638 }
blueswir1cd390082008-11-16 13:53:32 +00002639 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002640 pch++;
aliguori376253e2009-03-05 23:01:23 +00002641 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002642 *pp = pch;
2643 return 0;
2644}
2645
2646static int get_str(char *buf, int buf_size, const char **pp)
2647{
2648 const char *p;
2649 char *q;
2650 int c;
2651
bellard81d09122004-07-14 17:21:37 +00002652 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002653 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002654 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002655 p++;
2656 if (*p == '\0') {
2657 fail:
bellard81d09122004-07-14 17:21:37 +00002658 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002659 *pp = p;
2660 return -1;
2661 }
bellard9307c4c2004-04-04 12:57:25 +00002662 if (*p == '\"') {
2663 p++;
2664 while (*p != '\0' && *p != '\"') {
2665 if (*p == '\\') {
2666 p++;
2667 c = *p++;
2668 switch(c) {
2669 case 'n':
2670 c = '\n';
2671 break;
2672 case 'r':
2673 c = '\r';
2674 break;
2675 case '\\':
2676 case '\'':
2677 case '\"':
2678 break;
2679 default:
2680 qemu_printf("unsupported escape code: '\\%c'\n", c);
2681 goto fail;
2682 }
2683 if ((q - buf) < buf_size - 1) {
2684 *q++ = c;
2685 }
2686 } else {
2687 if ((q - buf) < buf_size - 1) {
2688 *q++ = *p;
2689 }
2690 p++;
2691 }
2692 }
2693 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002694 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002695 goto fail;
2696 }
2697 p++;
2698 } else {
blueswir1cd390082008-11-16 13:53:32 +00002699 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002700 if ((q - buf) < buf_size - 1) {
2701 *q++ = *p;
2702 }
2703 p++;
2704 }
bellard9307c4c2004-04-04 12:57:25 +00002705 }
bellard81d09122004-07-14 17:21:37 +00002706 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002707 *pp = p;
2708 return 0;
2709}
2710
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002711/*
2712 * Store the command-name in cmdname, and return a pointer to
2713 * the remaining of the command string.
2714 */
2715static const char *get_command_name(const char *cmdline,
2716 char *cmdname, size_t nlen)
2717{
2718 size_t len;
2719 const char *p, *pstart;
2720
2721 p = cmdline;
2722 while (qemu_isspace(*p))
2723 p++;
2724 if (*p == '\0')
2725 return NULL;
2726 pstart = p;
2727 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2728 p++;
2729 len = p - pstart;
2730 if (len > nlen - 1)
2731 len = nlen - 1;
2732 memcpy(cmdname, pstart, len);
2733 cmdname[len] = '\0';
2734 return p;
2735}
2736
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002737/**
2738 * Read key of 'type' into 'key' and return the current
2739 * 'type' pointer.
2740 */
2741static char *key_get_info(const char *type, char **key)
2742{
2743 size_t len;
2744 char *p, *str;
2745
2746 if (*type == ',')
2747 type++;
2748
2749 p = strchr(type, ':');
2750 if (!p) {
2751 *key = NULL;
2752 return NULL;
2753 }
2754 len = p - type;
2755
2756 str = qemu_malloc(len + 1);
2757 memcpy(str, type, len);
2758 str[len] = '\0';
2759
2760 *key = str;
2761 return ++p;
2762}
2763
bellard9307c4c2004-04-04 12:57:25 +00002764static int default_fmt_format = 'x';
2765static int default_fmt_size = 4;
2766
2767#define MAX_ARGS 16
2768
Anthony Liguoric227f092009-10-01 16:12:16 -05002769static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002770 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002771 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00002772{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002773 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03002774 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05002775 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002776 char cmdname[256];
2777 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002778 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00002779
2780#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002781 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002782#endif
ths3b46e622007-09-17 08:09:54 +00002783
bellard9307c4c2004-04-04 12:57:25 +00002784 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002785 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2786 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002787 return NULL;
ths3b46e622007-09-17 08:09:54 +00002788
bellard9307c4c2004-04-04 12:57:25 +00002789 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002790 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002791 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002792 break;
bellard9dc39cb2004-03-14 21:38:27 +00002793 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002794
2795 if (cmd->name == NULL) {
2796 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002797 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002798 }
bellard9307c4c2004-04-04 12:57:25 +00002799
bellard9307c4c2004-04-04 12:57:25 +00002800 /* parse the parameters */
2801 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002802 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002803 typestr = key_get_info(typestr, &key);
2804 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002805 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002806 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002807 typestr++;
2808 switch(c) {
2809 case 'F':
bellard81d09122004-07-14 17:21:37 +00002810 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002811 case 's':
2812 {
2813 int ret;
ths3b46e622007-09-17 08:09:54 +00002814
blueswir1cd390082008-11-16 13:53:32 +00002815 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002816 p++;
2817 if (*typestr == '?') {
2818 typestr++;
2819 if (*p == '\0') {
2820 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002821 break;
bellard9307c4c2004-04-04 12:57:25 +00002822 }
2823 }
2824 ret = get_str(buf, sizeof(buf), &p);
2825 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002826 switch(c) {
2827 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002828 monitor_printf(mon, "%s: filename expected\n",
2829 cmdname);
bellard81d09122004-07-14 17:21:37 +00002830 break;
2831 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002832 monitor_printf(mon, "%s: block device name expected\n",
2833 cmdname);
bellard81d09122004-07-14 17:21:37 +00002834 break;
2835 default:
aliguori376253e2009-03-05 23:01:23 +00002836 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002837 break;
2838 }
bellard9307c4c2004-04-04 12:57:25 +00002839 goto fail;
2840 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002841 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002842 }
2843 break;
2844 case '/':
2845 {
2846 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002847
blueswir1cd390082008-11-16 13:53:32 +00002848 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002849 p++;
2850 if (*p == '/') {
2851 /* format found */
2852 p++;
2853 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002854 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002855 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002856 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002857 count = count * 10 + (*p - '0');
2858 p++;
2859 }
2860 }
2861 size = -1;
2862 format = -1;
2863 for(;;) {
2864 switch(*p) {
2865 case 'o':
2866 case 'd':
2867 case 'u':
2868 case 'x':
2869 case 'i':
2870 case 'c':
2871 format = *p++;
2872 break;
2873 case 'b':
2874 size = 1;
2875 p++;
2876 break;
2877 case 'h':
2878 size = 2;
2879 p++;
2880 break;
2881 case 'w':
2882 size = 4;
2883 p++;
2884 break;
2885 case 'g':
2886 case 'L':
2887 size = 8;
2888 p++;
2889 break;
2890 default:
2891 goto next;
2892 }
2893 }
2894 next:
blueswir1cd390082008-11-16 13:53:32 +00002895 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002896 monitor_printf(mon, "invalid char in format: '%c'\n",
2897 *p);
bellard9307c4c2004-04-04 12:57:25 +00002898 goto fail;
2899 }
bellard9307c4c2004-04-04 12:57:25 +00002900 if (format < 0)
2901 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002902 if (format != 'i') {
2903 /* for 'i', not specifying a size gives -1 as size */
2904 if (size < 0)
2905 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002906 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002907 }
bellard9307c4c2004-04-04 12:57:25 +00002908 default_fmt_format = format;
2909 } else {
2910 count = 1;
2911 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002912 if (format != 'i') {
2913 size = default_fmt_size;
2914 } else {
2915 size = -1;
2916 }
bellard9307c4c2004-04-04 12:57:25 +00002917 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002918 qdict_put(qdict, "count", qint_from_int(count));
2919 qdict_put(qdict, "format", qint_from_int(format));
2920 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002921 }
2922 break;
2923 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002924 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002925 {
blueswir1c2efc952007-09-25 17:28:42 +00002926 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002927
blueswir1cd390082008-11-16 13:53:32 +00002928 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002929 p++;
bellard34405572004-06-08 00:55:58 +00002930 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002931 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002932 if (*p == '\0') {
2933 typestr++;
2934 break;
2935 }
bellard34405572004-06-08 00:55:58 +00002936 } else {
2937 if (*p == '.') {
2938 p++;
blueswir1cd390082008-11-16 13:53:32 +00002939 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002940 p++;
bellard34405572004-06-08 00:55:58 +00002941 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002942 typestr++;
2943 break;
bellard34405572004-06-08 00:55:58 +00002944 }
2945 }
bellard13224a82006-07-14 22:03:35 +00002946 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002947 }
aliguori376253e2009-03-05 23:01:23 +00002948 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002949 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002950 /* Check if 'i' is greater than 32-bit */
2951 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2952 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
2953 monitor_printf(mon, "integer is for 32-bit values\n");
2954 goto fail;
2955 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002956 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002957 }
2958 break;
2959 case '-':
2960 {
2961 int has_option;
2962 /* option */
ths3b46e622007-09-17 08:09:54 +00002963
bellard9307c4c2004-04-04 12:57:25 +00002964 c = *typestr++;
2965 if (c == '\0')
2966 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002967 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002968 p++;
2969 has_option = 0;
2970 if (*p == '-') {
2971 p++;
2972 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002973 monitor_printf(mon, "%s: unsupported option -%c\n",
2974 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002975 goto fail;
2976 }
2977 p++;
2978 has_option = 1;
2979 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002980 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00002981 }
2982 break;
2983 default:
2984 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002985 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002986 goto fail;
2987 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002988 qemu_free(key);
2989 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002990 }
2991 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002992 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002993 p++;
2994 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002995 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2996 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002997 goto fail;
2998 }
2999
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003000 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003001
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003002fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003003 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003004 return NULL;
3005}
3006
3007static void monitor_handle_command(Monitor *mon, const char *cmdline)
3008{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003009 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003010 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003011
3012 qdict = qdict_new();
3013
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003014 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003015 if (cmd) {
3016 void (*handler)(Monitor *mon, const QDict *qdict);
3017
3018 qemu_errors_to_mon(mon);
3019
3020 handler = cmd->handler;
3021 handler(mon, qdict);
3022
3023 qemu_errors_to_previous();
3024 }
3025
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003026 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003027}
3028
bellard81d09122004-07-14 17:21:37 +00003029static void cmd_completion(const char *name, const char *list)
3030{
3031 const char *p, *pstart;
3032 char cmd[128];
3033 int len;
3034
3035 p = list;
3036 for(;;) {
3037 pstart = p;
3038 p = strchr(p, '|');
3039 if (!p)
3040 p = pstart + strlen(pstart);
3041 len = p - pstart;
3042 if (len > sizeof(cmd) - 2)
3043 len = sizeof(cmd) - 2;
3044 memcpy(cmd, pstart, len);
3045 cmd[len] = '\0';
3046 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003047 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003048 }
3049 if (*p == '\0')
3050 break;
3051 p++;
3052 }
3053}
3054
3055static void file_completion(const char *input)
3056{
3057 DIR *ffs;
3058 struct dirent *d;
3059 char path[1024];
3060 char file[1024], file_prefix[1024];
3061 int input_path_len;
3062 const char *p;
3063
ths5fafdf22007-09-16 21:08:06 +00003064 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003065 if (!p) {
3066 input_path_len = 0;
3067 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003068 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003069 } else {
3070 input_path_len = p - input + 1;
3071 memcpy(path, input, input_path_len);
3072 if (input_path_len > sizeof(path) - 1)
3073 input_path_len = sizeof(path) - 1;
3074 path[input_path_len] = '\0';
3075 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3076 }
3077#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003078 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3079 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003080#endif
3081 ffs = opendir(path);
3082 if (!ffs)
3083 return;
3084 for(;;) {
3085 struct stat sb;
3086 d = readdir(ffs);
3087 if (!d)
3088 break;
3089 if (strstart(d->d_name, file_prefix, NULL)) {
3090 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003091 if (input_path_len < sizeof(file))
3092 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3093 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003094 /* stat the file to find out if it's a directory.
3095 * In that case add a slash to speed up typing long paths
3096 */
3097 stat(file, &sb);
3098 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003099 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003100 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003101 }
3102 }
3103 closedir(ffs);
3104}
3105
aliguori51de9762009-03-05 23:00:43 +00003106static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003107{
aliguori51de9762009-03-05 23:00:43 +00003108 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003109 const char *input = opaque;
3110
3111 if (input[0] == '\0' ||
3112 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003113 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003114 }
3115}
3116
3117/* NOTE: this parser is an approximate form of the real command parser */
3118static void parse_cmdline(const char *cmdline,
3119 int *pnb_args, char **args)
3120{
3121 const char *p;
3122 int nb_args, ret;
3123 char buf[1024];
3124
3125 p = cmdline;
3126 nb_args = 0;
3127 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003128 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003129 p++;
3130 if (*p == '\0')
3131 break;
3132 if (nb_args >= MAX_ARGS)
3133 break;
3134 ret = get_str(buf, sizeof(buf), &p);
3135 args[nb_args] = qemu_strdup(buf);
3136 nb_args++;
3137 if (ret < 0)
3138 break;
3139 }
3140 *pnb_args = nb_args;
3141}
3142
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003143static const char *next_arg_type(const char *typestr)
3144{
3145 const char *p = strchr(typestr, ':');
3146 return (p != NULL ? ++p : typestr);
3147}
3148
aliguori4c36ba32009-03-05 23:01:37 +00003149static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003150{
3151 const char *cmdname;
3152 char *args[MAX_ARGS];
3153 int nb_args, i, len;
3154 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003155 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003156 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003157
3158 parse_cmdline(cmdline, &nb_args, args);
3159#ifdef DEBUG_COMPLETION
3160 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003161 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003162 }
3163#endif
3164
3165 /* if the line ends with a space, it means we want to complete the
3166 next arg */
3167 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003168 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003169 if (nb_args >= MAX_ARGS)
3170 return;
3171 args[nb_args++] = qemu_strdup("");
3172 }
3173 if (nb_args <= 1) {
3174 /* command completion */
3175 if (nb_args == 0)
3176 cmdname = "";
3177 else
3178 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003179 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003180 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003181 cmd_completion(cmdname, cmd->name);
3182 }
3183 } else {
3184 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003185 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003186 if (compare_cmd(args[0], cmd->name))
3187 goto found;
3188 }
3189 return;
3190 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003191 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003192 for(i = 0; i < nb_args - 2; i++) {
3193 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003194 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003195 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003196 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003197 }
3198 }
3199 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003200 if (*ptype == '-' && ptype[1] != '\0') {
3201 ptype += 2;
3202 }
bellard81d09122004-07-14 17:21:37 +00003203 switch(*ptype) {
3204 case 'F':
3205 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003206 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003207 file_completion(str);
3208 break;
3209 case 'B':
3210 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003211 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003212 bdrv_iterate(block_completion_it, (void *)str);
3213 break;
bellard7fe48482004-10-09 18:08:01 +00003214 case 's':
3215 /* XXX: more generic ? */
3216 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003217 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003218 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3219 cmd_completion(str, cmd->name);
3220 }
bellard64866c32006-05-07 18:03:31 +00003221 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003222 char *sep = strrchr(str, '-');
3223 if (sep)
3224 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003225 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003226 for(key = key_defs; key->name != NULL; key++) {
3227 cmd_completion(str, key->name);
3228 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003229 } else if (!strcmp(cmd->name, "help|?")) {
3230 readline_set_completion_index(cur_mon->rs, strlen(str));
3231 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3232 cmd_completion(str, cmd->name);
3233 }
bellard7fe48482004-10-09 18:08:01 +00003234 }
3235 break;
bellard81d09122004-07-14 17:21:37 +00003236 default:
3237 break;
3238 }
3239 }
3240 for(i = 0; i < nb_args; i++)
3241 qemu_free(args[i]);
3242}
3243
aliguori731b0362009-03-05 23:01:42 +00003244static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003245{
aliguori731b0362009-03-05 23:01:42 +00003246 Monitor *mon = opaque;
3247
3248 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003249}
3250
aliguori731b0362009-03-05 23:01:42 +00003251static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003252{
aliguori731b0362009-03-05 23:01:42 +00003253 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003254 int i;
aliguori376253e2009-03-05 23:01:23 +00003255
aliguori731b0362009-03-05 23:01:42 +00003256 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003257
aliguoricde76ee2009-03-05 23:01:51 +00003258 if (cur_mon->rs) {
3259 for (i = 0; i < size; i++)
3260 readline_handle_byte(cur_mon->rs, buf[i]);
3261 } else {
3262 if (size == 0 || buf[size - 1] != 0)
3263 monitor_printf(cur_mon, "corrupted command\n");
3264 else
3265 monitor_handle_command(cur_mon, (char *)buf);
3266 }
aliguori731b0362009-03-05 23:01:42 +00003267
3268 cur_mon = old_mon;
3269}
aliguorid8f44602008-10-06 13:52:44 +00003270
aliguori376253e2009-03-05 23:01:23 +00003271static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003272{
aliguori731b0362009-03-05 23:01:42 +00003273 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003274 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003275 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003276}
3277
aliguoricde76ee2009-03-05 23:01:51 +00003278int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003279{
aliguoricde76ee2009-03-05 23:01:51 +00003280 if (!mon->rs)
3281 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003282 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003283 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003284}
3285
aliguori376253e2009-03-05 23:01:23 +00003286void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003287{
aliguoricde76ee2009-03-05 23:01:51 +00003288 if (!mon->rs)
3289 return;
aliguori731b0362009-03-05 23:01:42 +00003290 if (--mon->suspend_cnt == 0)
3291 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003292}
3293
aliguori731b0362009-03-05 23:01:42 +00003294static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003295{
aliguori376253e2009-03-05 23:01:23 +00003296 Monitor *mon = opaque;
3297
aliguori2724b182009-03-05 23:01:47 +00003298 switch (event) {
3299 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003300 mon->mux_out = 0;
3301 if (mon->reset_seen) {
3302 readline_restart(mon->rs);
3303 monitor_resume(mon);
3304 monitor_flush(mon);
3305 } else {
3306 mon->suspend_cnt = 0;
3307 }
aliguori2724b182009-03-05 23:01:47 +00003308 break;
ths86e94de2007-01-05 22:01:59 +00003309
aliguori2724b182009-03-05 23:01:47 +00003310 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003311 if (mon->reset_seen) {
3312 if (mon->suspend_cnt == 0) {
3313 monitor_printf(mon, "\n");
3314 }
3315 monitor_flush(mon);
3316 monitor_suspend(mon);
3317 } else {
3318 mon->suspend_cnt++;
3319 }
3320 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00003321 break;
3322
3323 case CHR_EVENT_RESET:
3324 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3325 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003326 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00003327 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003328 }
3329 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00003330 break;
3331 }
ths86e94de2007-01-05 22:01:59 +00003332}
3333
aliguori76655d62009-03-06 20:27:37 +00003334
3335/*
3336 * Local variables:
3337 * c-indent-level: 4
3338 * c-basic-offset: 4
3339 * tab-width: 8
3340 * End:
3341 */
3342
aliguori731b0362009-03-05 23:01:42 +00003343void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003344{
aliguori731b0362009-03-05 23:01:42 +00003345 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003346 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003347
3348 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003349 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003350 is_first_init = 0;
3351 }
aliguori87127162009-03-05 23:01:29 +00003352
3353 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003354
aliguori87127162009-03-05 23:01:29 +00003355 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003356 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00003357 if (flags & MONITOR_USE_READLINE) {
3358 mon->rs = readline_init(mon, monitor_find_completion);
3359 monitor_read_command(mon, 0);
3360 }
aliguori87127162009-03-05 23:01:29 +00003361
aliguori731b0362009-03-05 23:01:42 +00003362 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3363 mon);
aliguori87127162009-03-05 23:01:29 +00003364
Blue Swirl72cf2d42009-09-12 07:36:22 +00003365 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003366 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003367 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003368}
3369
aliguori376253e2009-03-05 23:01:23 +00003370static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003371{
aliguoribb5fc202009-03-05 23:01:15 +00003372 BlockDriverState *bs = opaque;
3373 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003374
aliguoribb5fc202009-03-05 23:01:15 +00003375 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003376 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003377 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003378 }
aliguori731b0362009-03-05 23:01:42 +00003379 if (mon->password_completion_cb)
3380 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003381
aliguori731b0362009-03-05 23:01:42 +00003382 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003383}
aliguoric0f4ce72009-03-05 23:01:01 +00003384
aliguori376253e2009-03-05 23:01:23 +00003385void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003386 BlockDriverCompletionFunc *completion_cb,
3387 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003388{
aliguoricde76ee2009-03-05 23:01:51 +00003389 int err;
3390
aliguoribb5fc202009-03-05 23:01:15 +00003391 if (!bdrv_key_required(bs)) {
3392 if (completion_cb)
3393 completion_cb(opaque, 0);
3394 return;
3395 }
aliguoric0f4ce72009-03-05 23:01:01 +00003396
aliguori376253e2009-03-05 23:01:23 +00003397 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3398 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003399
aliguori731b0362009-03-05 23:01:42 +00003400 mon->password_completion_cb = completion_cb;
3401 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003402
aliguoricde76ee2009-03-05 23:01:51 +00003403 err = monitor_read_password(mon, bdrv_password_cb, bs);
3404
3405 if (err && completion_cb)
3406 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003407}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003408
3409typedef struct QemuErrorSink QemuErrorSink;
3410struct QemuErrorSink {
3411 enum {
3412 ERR_SINK_FILE,
3413 ERR_SINK_MONITOR,
3414 } dest;
3415 union {
3416 FILE *fp;
3417 Monitor *mon;
3418 };
3419 QemuErrorSink *previous;
3420};
3421
Blue Swirl528e93a2009-08-31 15:14:40 +00003422static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003423
3424void qemu_errors_to_file(FILE *fp)
3425{
3426 QemuErrorSink *sink;
3427
3428 sink = qemu_mallocz(sizeof(*sink));
3429 sink->dest = ERR_SINK_FILE;
3430 sink->fp = fp;
3431 sink->previous = qemu_error_sink;
3432 qemu_error_sink = sink;
3433}
3434
3435void qemu_errors_to_mon(Monitor *mon)
3436{
3437 QemuErrorSink *sink;
3438
3439 sink = qemu_mallocz(sizeof(*sink));
3440 sink->dest = ERR_SINK_MONITOR;
3441 sink->mon = mon;
3442 sink->previous = qemu_error_sink;
3443 qemu_error_sink = sink;
3444}
3445
3446void qemu_errors_to_previous(void)
3447{
3448 QemuErrorSink *sink;
3449
3450 assert(qemu_error_sink != NULL);
3451 sink = qemu_error_sink;
3452 qemu_error_sink = sink->previous;
3453 qemu_free(sink);
3454}
3455
3456void qemu_error(const char *fmt, ...)
3457{
3458 va_list args;
3459
3460 assert(qemu_error_sink != NULL);
3461 switch (qemu_error_sink->dest) {
3462 case ERR_SINK_FILE:
3463 va_start(args, fmt);
3464 vfprintf(qemu_error_sink->fp, fmt, args);
3465 va_end(args);
3466 break;
3467 case ERR_SINK_MONITOR:
3468 va_start(args, fmt);
3469 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3470 va_end(args);
3471 break;
3472 }
3473}