blob: b589d9826eb7973c9ec18d0674d363b485cdb99c [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;
bellard9dc39cb2004-03-14 21:38:27 +000074 const char *params;
75 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -030076 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -030077 union {
78 void (*info)(Monitor *mon);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -030079 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino910df892009-10-07 13:41:51 -030080 } mhandler;
Anthony Liguoric227f092009-10-01 16:12:16 -050081} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000082
Mark McLoughlinf07918f2009-07-22 09:11:40 +010083/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -050084typedef struct mon_fd_t mon_fd_t;
85struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +010086 char *name;
87 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -050088 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010089};
90
aliguori87127162009-03-05 23:01:29 +000091struct Monitor {
92 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +020093 int mux_out;
94 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +000095 int flags;
96 int suspend_cnt;
97 uint8_t outbuf[1024];
98 int outbuf_index;
99 ReadLineState *rs;
100 CPUState *mon_cpu;
101 BlockDriverCompletionFunc *password_completion_cb;
102 void *password_opaque;
Anthony Liguoric227f092009-10-01 16:12:16 -0500103 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000104 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000105};
106
Blue Swirl72cf2d42009-09-12 07:36:22 +0000107static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000108
Anthony Liguoric227f092009-10-01 16:12:16 -0500109static const mon_cmd_t mon_cmds[];
110static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000111
aliguori87127162009-03-05 23:01:29 +0000112Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000113
aliguori731b0362009-03-05 23:01:42 +0000114static void monitor_command_cb(Monitor *mon, const char *cmdline,
115 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000116
aliguori731b0362009-03-05 23:01:42 +0000117static void monitor_read_command(Monitor *mon, int show_prompt)
118{
119 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
120 if (show_prompt)
121 readline_show_prompt(mon->rs);
122}
bellard6a00d602005-11-21 23:25:50 +0000123
aliguoricde76ee2009-03-05 23:01:51 +0000124static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
125 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000126{
aliguoricde76ee2009-03-05 23:01:51 +0000127 if (mon->rs) {
128 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
129 /* prompt is printed on return from the command handler */
130 return 0;
131 } else {
132 monitor_printf(mon, "terminal does not support password prompting\n");
133 return -ENOTTY;
134 }
aliguoribb5fc202009-03-05 23:01:15 +0000135}
136
aliguori376253e2009-03-05 23:01:23 +0000137void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000138{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200139 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000140 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
141 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000142 }
143}
144
145/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000146static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000147{
ths60fe76f2007-12-16 03:02:09 +0000148 char c;
aliguori731b0362009-03-05 23:01:42 +0000149
150 if (!mon)
151 return;
152
bellard7e2515e2004-08-01 21:52:19 +0000153 for(;;) {
154 c = *str++;
155 if (c == '\0')
156 break;
bellard7ba12602006-07-14 20:26:42 +0000157 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000158 mon->outbuf[mon->outbuf_index++] = '\r';
159 mon->outbuf[mon->outbuf_index++] = c;
160 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
161 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000162 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000163 }
164}
165
aliguori376253e2009-03-05 23:01:23 +0000166void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000167{
168 char buf[4096];
169 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000170 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000171}
172
aliguori376253e2009-03-05 23:01:23 +0000173void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000174{
175 va_list ap;
176 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000177 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000178 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000179}
180
aliguori376253e2009-03-05 23:01:23 +0000181void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000182{
183 int i;
184
185 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000186 switch (filename[i]) {
187 case ' ':
188 case '"':
189 case '\\':
190 monitor_printf(mon, "\\%c", filename[i]);
191 break;
192 case '\t':
193 monitor_printf(mon, "\\t");
194 break;
195 case '\r':
196 monitor_printf(mon, "\\r");
197 break;
198 case '\n':
199 monitor_printf(mon, "\\n");
200 break;
201 default:
202 monitor_printf(mon, "%c", filename[i]);
203 break;
204 }
thsfef30742006-12-22 14:11:32 +0000205 }
206}
207
bellard7fe48482004-10-09 18:08:01 +0000208static int monitor_fprintf(FILE *stream, const char *fmt, ...)
209{
210 va_list ap;
211 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000212 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000213 va_end(ap);
214 return 0;
215}
216
bellard9dc39cb2004-03-14 21:38:27 +0000217static int compare_cmd(const char *name, const char *list)
218{
219 const char *p, *pstart;
220 int len;
221 len = strlen(name);
222 p = list;
223 for(;;) {
224 pstart = p;
225 p = strchr(p, '|');
226 if (!p)
227 p = pstart + strlen(pstart);
228 if ((p - pstart) == len && !memcmp(pstart, name, len))
229 return 1;
230 if (*p == '\0')
231 break;
232 p++;
233 }
234 return 0;
235}
236
Anthony Liguoric227f092009-10-01 16:12:16 -0500237static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000238 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000239{
Anthony Liguoric227f092009-10-01 16:12:16 -0500240 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000241
242 for(cmd = cmds; cmd->name != NULL; cmd++) {
243 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000244 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
245 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000246 }
247}
248
aliguori376253e2009-03-05 23:01:23 +0000249static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000250{
251 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000252 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000253 } else {
aliguori376253e2009-03-05 23:01:23 +0000254 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000255 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000256 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000257 monitor_printf(mon, "Log items (comma separated):\n");
258 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000259 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000260 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000261 }
262 }
bellard9dc39cb2004-03-14 21:38:27 +0000263 }
264}
265
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300266static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300267{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300268 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300269}
270
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300271static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000272{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200273 int all_devices;
274 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300275 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000276
bellard7954c732006-08-01 15:52:40 +0000277 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000278 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200279 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300280 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200281 continue;
282 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000283 }
284}
285
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300286static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000287{
Anthony Liguoric227f092009-10-01 16:12:16 -0500288 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300289 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000290
bellard9307c4c2004-04-04 12:57:25 +0000291 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000292 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000293 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000294 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000295 goto found;
296 }
297 help:
aliguori376253e2009-03-05 23:01:23 +0000298 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000299 return;
300 found:
Luiz Capitulino910df892009-10-07 13:41:51 -0300301 cmd->mhandler.info(mon);
bellard9dc39cb2004-03-14 21:38:27 +0000302}
303
aliguori376253e2009-03-05 23:01:23 +0000304static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000305{
pbrook4a19f1e2009-04-07 23:17:49 +0000306 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000307}
308
aliguori376253e2009-03-05 23:01:23 +0000309static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000310{
311 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000312 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000313}
314
aurel32bf4f74c2008-12-18 22:42:34 +0000315#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000316static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000317{
aliguori376253e2009-03-05 23:01:23 +0000318 monitor_printf(mon, "HPET is %s by QEMU\n",
319 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000320}
aurel32bf4f74c2008-12-18 22:42:34 +0000321#endif
aliguori16b29ae2008-12-17 23:28:44 +0000322
aliguori376253e2009-03-05 23:01:23 +0000323static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000324{
aliguori376253e2009-03-05 23:01:23 +0000325 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
326 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
327 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
328 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
329 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000330}
331
bellard6a00d602005-11-21 23:25:50 +0000332/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000333static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000334{
335 CPUState *env;
336
337 for(env = first_cpu; env != NULL; env = env->next_cpu) {
338 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000339 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000340 return 0;
341 }
342 }
343 return -1;
344}
345
pbrook9596ebb2007-11-18 01:44:38 +0000346static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000347{
aliguori731b0362009-03-05 23:01:42 +0000348 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000349 mon_set_cpu(0);
350 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300351 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000352 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000353}
354
aliguori376253e2009-03-05 23:01:23 +0000355static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000356{
bellard6a00d602005-11-21 23:25:50 +0000357 CPUState *env;
358 env = mon_get_cpu();
359 if (!env)
360 return;
bellard9307c4c2004-04-04 12:57:25 +0000361#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000362 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000363 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000364#else
aliguori376253e2009-03-05 23:01:23 +0000365 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000366 0);
bellard9307c4c2004-04-04 12:57:25 +0000367#endif
368}
369
aliguori376253e2009-03-05 23:01:23 +0000370static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000371{
372 CPUState *env;
373
374 /* just to set the default cpu if not already done */
375 mon_get_cpu();
376
377 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Avi Kivity4c0960c2009-08-17 23:19:53 +0300378 cpu_synchronize_state(env);
aliguori376253e2009-03-05 23:01:23 +0000379 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000380 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000381 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000382#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000383 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
384 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000385#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000386 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000387#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000388 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
389 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000390#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000391 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000392#endif
thsead93602007-09-06 00:18:15 +0000393 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000394 monitor_printf(mon, " (halted)");
395 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000396 }
397}
398
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300399static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000400{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300401 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000402 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000403 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000404}
405
aliguori376253e2009-03-05 23:01:23 +0000406static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000407{
aliguori376253e2009-03-05 23:01:23 +0000408 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000409}
410
aliguori376253e2009-03-05 23:01:23 +0000411static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000412{
413 int i;
bellard7e2515e2004-08-01 21:52:19 +0000414 const char *str;
ths3b46e622007-09-17 08:09:54 +0000415
aliguoricde76ee2009-03-05 23:01:51 +0000416 if (!mon->rs)
417 return;
bellard7e2515e2004-08-01 21:52:19 +0000418 i = 0;
419 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000420 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000421 if (!str)
422 break;
aliguori376253e2009-03-05 23:01:23 +0000423 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000424 i++;
bellardaa455482004-04-04 13:07:25 +0000425 }
426}
427
j_mayer76a66252007-03-07 08:32:30 +0000428#if defined(TARGET_PPC)
429/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000430static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000431{
432 CPUState *env;
433
434 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000435 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000436}
437#endif
438
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300439static void do_quit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000440{
441 exit(0);
442}
443
aliguori376253e2009-03-05 23:01:23 +0000444static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000445{
446 if (bdrv_is_inserted(bs)) {
447 if (!force) {
448 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000449 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000450 return -1;
451 }
452 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000453 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000454 return -1;
455 }
456 }
457 bdrv_close(bs);
458 }
459 return 0;
460}
461
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300462static void do_eject(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000463{
464 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300465 int force = qdict_get_int(qdict, "force");
466 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000467
bellard9307c4c2004-04-04 12:57:25 +0000468 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000469 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000470 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000471 return;
472 }
aliguori376253e2009-03-05 23:01:23 +0000473 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000474}
475
aliguori376253e2009-03-05 23:01:23 +0000476static void do_change_block(Monitor *mon, const char *device,
477 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000478{
479 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000480 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000481
bellard9307c4c2004-04-04 12:57:25 +0000482 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000483 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000484 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000485 return;
486 }
aurel322ecea9b2008-06-18 22:10:01 +0000487 if (fmt) {
488 drv = bdrv_find_format(fmt);
489 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000490 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000491 return;
492 }
493 }
aliguori376253e2009-03-05 23:01:23 +0000494 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000495 return;
aurel322ecea9b2008-06-18 22:10:01 +0000496 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000497 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000498}
499
aliguori376253e2009-03-05 23:01:23 +0000500static void change_vnc_password_cb(Monitor *mon, const char *password,
501 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000502{
503 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000504 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000505
aliguori731b0362009-03-05 23:01:42 +0000506 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000507}
508
aliguori376253e2009-03-05 23:01:23 +0000509static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000510{
ths70848512007-08-25 01:37:05 +0000511 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000512 strcmp(target, "password") == 0) {
513 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000514 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000515 strncpy(password, arg, sizeof(password));
516 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000517 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000518 } else {
aliguori376253e2009-03-05 23:01:23 +0000519 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000520 }
ths70848512007-08-25 01:37:05 +0000521 } else {
aliguori28a76be2009-03-06 20:27:40 +0000522 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000523 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000524 }
thse25a5822007-08-25 01:36:20 +0000525}
526
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300527static void do_change(Monitor *mon, const QDict *qdict)
thse25a5822007-08-25 01:36:20 +0000528{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300529 const char *device = qdict_get_str(qdict, "device");
530 const char *target = qdict_get_str(qdict, "target");
531 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000532 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000533 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000534 } else {
aliguori28a76be2009-03-06 20:27:40 +0000535 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000536 }
537}
538
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300539static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000540{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300541 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000542}
543
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300544static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000545{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300546 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000547}
548
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300549static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000550{
551 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300552 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000553
bellard9307c4c2004-04-04 12:57:25 +0000554 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000555 mask = 0;
556 } else {
bellard9307c4c2004-04-04 12:57:25 +0000557 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000558 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000559 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000560 return;
561 }
562 }
563 cpu_set_log(mask);
564}
565
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300566static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000567{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300568 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000569 if (!option || !strcmp(option, "on")) {
570 singlestep = 1;
571 } else if (!strcmp(option, "off")) {
572 singlestep = 0;
573 } else {
574 monitor_printf(mon, "unexpected option %s\n", option);
575 }
576}
577
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300578static void do_stop(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000579{
580 vm_stop(EXCP_INTERRUPT);
581}
582
aliguoribb5fc202009-03-05 23:01:15 +0000583static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000584
aliguori376253e2009-03-05 23:01:23 +0000585struct bdrv_iterate_context {
586 Monitor *mon;
587 int err;
588};
aliguoric0f4ce72009-03-05 23:01:01 +0000589
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300590static void do_cont(Monitor *mon, const QDict *qdict)
aliguori376253e2009-03-05 23:01:23 +0000591{
592 struct bdrv_iterate_context context = { mon, 0 };
593
594 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000595 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000596 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000597 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000598}
599
aliguoribb5fc202009-03-05 23:01:15 +0000600static void bdrv_key_cb(void *opaque, int err)
601{
aliguori376253e2009-03-05 23:01:23 +0000602 Monitor *mon = opaque;
603
aliguoribb5fc202009-03-05 23:01:15 +0000604 /* another key was set successfully, retry to continue */
605 if (!err)
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300606 do_cont(mon, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000607}
608
609static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
610{
aliguori376253e2009-03-05 23:01:23 +0000611 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000612
aliguori376253e2009-03-05 23:01:23 +0000613 if (!context->err && bdrv_key_required(bs)) {
614 context->err = -EBUSY;
615 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
616 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000617 }
618}
619
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300620static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000621{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300622 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000623 if (!device)
624 device = "tcp::" DEFAULT_GDBSTUB_PORT;
625 if (gdbserver_start(device) < 0) {
626 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
627 device);
628 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000629 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000630 } else {
aliguori59030a82009-04-05 18:43:41 +0000631 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
632 device);
bellard8a7ddc32004-03-31 19:00:16 +0000633 }
634}
635
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300636static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100637{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300638 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100639 if (select_watchdog_action(action) == -1) {
640 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
641 }
642}
643
aliguori376253e2009-03-05 23:01:23 +0000644static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000645{
aliguori376253e2009-03-05 23:01:23 +0000646 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000647 switch(c) {
648 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000649 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000650 break;
651 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000652 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000653 break;
654 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000655 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000656 break;
657 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000658 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000659 break;
660 default:
661 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000662 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000663 } else {
aliguori376253e2009-03-05 23:01:23 +0000664 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000665 }
666 break;
667 }
aliguori376253e2009-03-05 23:01:23 +0000668 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000669}
670
aliguori376253e2009-03-05 23:01:23 +0000671static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -0500672 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000673{
bellard6a00d602005-11-21 23:25:50 +0000674 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000675 int nb_per_line, l, line_size, i, max_digits, len;
676 uint8_t buf[16];
677 uint64_t v;
678
679 if (format == 'i') {
680 int flags;
681 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000682 env = mon_get_cpu();
683 if (!env && !is_physical)
684 return;
bellard9307c4c2004-04-04 12:57:25 +0000685#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000686 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000687 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000688 } else if (wsize == 4) {
689 flags = 0;
690 } else {
bellard6a15fd12006-04-12 21:07:07 +0000691 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000692 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000693 if (env) {
694#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000695 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000696 (env->segs[R_CS].flags & DESC_L_MASK))
697 flags = 2;
698 else
699#endif
700 if (!(env->segs[R_CS].flags & DESC_B_MASK))
701 flags = 1;
702 }
bellard4c27ba22004-04-25 18:05:08 +0000703 }
704#endif
aliguori376253e2009-03-05 23:01:23 +0000705 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000706 return;
707 }
708
709 len = wsize * count;
710 if (wsize == 1)
711 line_size = 8;
712 else
713 line_size = 16;
714 nb_per_line = line_size / wsize;
715 max_digits = 0;
716
717 switch(format) {
718 case 'o':
719 max_digits = (wsize * 8 + 2) / 3;
720 break;
721 default:
722 case 'x':
723 max_digits = (wsize * 8) / 4;
724 break;
725 case 'u':
726 case 'd':
727 max_digits = (wsize * 8 * 10 + 32) / 33;
728 break;
729 case 'c':
730 wsize = 1;
731 break;
732 }
733
734 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000735 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000736 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000737 else
aliguori376253e2009-03-05 23:01:23 +0000738 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000739 l = len;
740 if (l > line_size)
741 l = line_size;
742 if (is_physical) {
743 cpu_physical_memory_rw(addr, buf, l, 0);
744 } else {
bellard6a00d602005-11-21 23:25:50 +0000745 env = mon_get_cpu();
746 if (!env)
747 break;
aliguoric8f79b62008-08-18 14:00:20 +0000748 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000749 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000750 break;
751 }
bellard9307c4c2004-04-04 12:57:25 +0000752 }
ths5fafdf22007-09-16 21:08:06 +0000753 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000754 while (i < l) {
755 switch(wsize) {
756 default:
757 case 1:
758 v = ldub_raw(buf + i);
759 break;
760 case 2:
761 v = lduw_raw(buf + i);
762 break;
763 case 4:
bellard92a31b12005-02-10 22:00:52 +0000764 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000765 break;
766 case 8:
767 v = ldq_raw(buf + i);
768 break;
769 }
aliguori376253e2009-03-05 23:01:23 +0000770 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000771 switch(format) {
772 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000773 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000774 break;
775 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000776 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000777 break;
778 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000779 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000780 break;
781 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000782 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000783 break;
784 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000785 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000786 break;
787 }
788 i += wsize;
789 }
aliguori376253e2009-03-05 23:01:23 +0000790 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000791 addr += l;
792 len -= l;
793 }
794}
795
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300796static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000797{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300798 int count = qdict_get_int(qdict, "count");
799 int format = qdict_get_int(qdict, "format");
800 int size = qdict_get_int(qdict, "size");
801 target_long addr = qdict_get_int(qdict, "addr");
802
aliguori376253e2009-03-05 23:01:23 +0000803 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000804}
805
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300806static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000807{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300808 int count = qdict_get_int(qdict, "count");
809 int format = qdict_get_int(qdict, "format");
810 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -0500811 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300812
aliguori376253e2009-03-05 23:01:23 +0000813 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000814}
815
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300816static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000817{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300818 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -0500819 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300820
blueswir17743e582007-09-24 18:39:04 +0000821#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000822 switch(format) {
823 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000824 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000825 break;
826 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000827 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000828 break;
829 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000830 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000831 break;
832 default:
833 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000834 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000835 break;
836 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000837 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000838 break;
839 }
bellard92a31b12005-02-10 22:00:52 +0000840#else
841 switch(format) {
842 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000843 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000844 break;
845 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000846 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000847 break;
848 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000849 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000850 break;
851 default:
852 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000853 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000854 break;
855 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000856 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000857 break;
858 }
859#endif
aliguori376253e2009-03-05 23:01:23 +0000860 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000861}
862
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300863static void do_memory_save(Monitor *mon, const QDict *qdict)
bellardb371dc52007-01-03 15:20:39 +0000864{
865 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300866 uint32_t size = qdict_get_int(qdict, "size");
867 const char *filename = qdict_get_str(qdict, "filename");
868 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +0000869 uint32_t l;
870 CPUState *env;
871 uint8_t buf[1024];
872
873 env = mon_get_cpu();
874 if (!env)
875 return;
876
877 f = fopen(filename, "wb");
878 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000879 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000880 return;
881 }
882 while (size != 0) {
883 l = sizeof(buf);
884 if (l > size)
885 l = size;
886 cpu_memory_rw_debug(env, addr, buf, l, 0);
887 fwrite(buf, 1, l, f);
888 addr += l;
889 size -= l;
890 }
891 fclose(f);
892}
893
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300894static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
aurel32a8bdf7a2008-04-11 21:36:14 +0000895{
896 FILE *f;
897 uint32_t l;
898 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300899 uint32_t size = qdict_get_int(qdict, "size");
900 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -0500901 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +0000902
903 f = fopen(filename, "wb");
904 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000905 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000906 return;
907 }
908 while (size != 0) {
909 l = sizeof(buf);
910 if (l > size)
911 l = size;
912 cpu_physical_memory_rw(addr, buf, l, 0);
913 fwrite(buf, 1, l, f);
914 fflush(f);
915 addr += l;
916 size -= l;
917 }
918 fclose(f);
919}
920
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300921static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000922{
923 uint32_t addr;
924 uint8_t buf[1];
925 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300926 uint32_t start = qdict_get_int(qdict, "start");
927 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000928
929 sum = 0;
930 for(addr = start; addr < (start + size); addr++) {
931 cpu_physical_memory_rw(addr, buf, 1, 0);
932 /* BSD sum algorithm ('sum' Unix command) */
933 sum = (sum >> 1) | (sum << 15);
934 sum += buf[0];
935 }
aliguori376253e2009-03-05 23:01:23 +0000936 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000937}
938
bellarda3a91a32004-06-04 11:06:21 +0000939typedef struct {
940 int keycode;
941 const char *name;
942} KeyDef;
943
944static const KeyDef key_defs[] = {
945 { 0x2a, "shift" },
946 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000947
bellarda3a91a32004-06-04 11:06:21 +0000948 { 0x38, "alt" },
949 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000950 { 0x64, "altgr" },
951 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000952 { 0x1d, "ctrl" },
953 { 0x9d, "ctrl_r" },
954
955 { 0xdd, "menu" },
956
957 { 0x01, "esc" },
958
959 { 0x02, "1" },
960 { 0x03, "2" },
961 { 0x04, "3" },
962 { 0x05, "4" },
963 { 0x06, "5" },
964 { 0x07, "6" },
965 { 0x08, "7" },
966 { 0x09, "8" },
967 { 0x0a, "9" },
968 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000969 { 0x0c, "minus" },
970 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000971 { 0x0e, "backspace" },
972
973 { 0x0f, "tab" },
974 { 0x10, "q" },
975 { 0x11, "w" },
976 { 0x12, "e" },
977 { 0x13, "r" },
978 { 0x14, "t" },
979 { 0x15, "y" },
980 { 0x16, "u" },
981 { 0x17, "i" },
982 { 0x18, "o" },
983 { 0x19, "p" },
984
985 { 0x1c, "ret" },
986
987 { 0x1e, "a" },
988 { 0x1f, "s" },
989 { 0x20, "d" },
990 { 0x21, "f" },
991 { 0x22, "g" },
992 { 0x23, "h" },
993 { 0x24, "j" },
994 { 0x25, "k" },
995 { 0x26, "l" },
996
997 { 0x2c, "z" },
998 { 0x2d, "x" },
999 { 0x2e, "c" },
1000 { 0x2f, "v" },
1001 { 0x30, "b" },
1002 { 0x31, "n" },
1003 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001004 { 0x33, "comma" },
1005 { 0x34, "dot" },
1006 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001007
balrog4d3b6f62008-02-10 16:33:14 +00001008 { 0x37, "asterisk" },
1009
bellarda3a91a32004-06-04 11:06:21 +00001010 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001011 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001012 { 0x3b, "f1" },
1013 { 0x3c, "f2" },
1014 { 0x3d, "f3" },
1015 { 0x3e, "f4" },
1016 { 0x3f, "f5" },
1017 { 0x40, "f6" },
1018 { 0x41, "f7" },
1019 { 0x42, "f8" },
1020 { 0x43, "f9" },
1021 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001022 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001023 { 0x46, "scroll_lock" },
1024
bellard64866c32006-05-07 18:03:31 +00001025 { 0xb5, "kp_divide" },
1026 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001027 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001028 { 0x4e, "kp_add" },
1029 { 0x9c, "kp_enter" },
1030 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001031 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001032
1033 { 0x52, "kp_0" },
1034 { 0x4f, "kp_1" },
1035 { 0x50, "kp_2" },
1036 { 0x51, "kp_3" },
1037 { 0x4b, "kp_4" },
1038 { 0x4c, "kp_5" },
1039 { 0x4d, "kp_6" },
1040 { 0x47, "kp_7" },
1041 { 0x48, "kp_8" },
1042 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001043
bellarda3a91a32004-06-04 11:06:21 +00001044 { 0x56, "<" },
1045
1046 { 0x57, "f11" },
1047 { 0x58, "f12" },
1048
1049 { 0xb7, "print" },
1050
1051 { 0xc7, "home" },
1052 { 0xc9, "pgup" },
1053 { 0xd1, "pgdn" },
1054 { 0xcf, "end" },
1055
1056 { 0xcb, "left" },
1057 { 0xc8, "up" },
1058 { 0xd0, "down" },
1059 { 0xcd, "right" },
1060
1061 { 0xd2, "insert" },
1062 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001063#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1064 { 0xf0, "stop" },
1065 { 0xf1, "again" },
1066 { 0xf2, "props" },
1067 { 0xf3, "undo" },
1068 { 0xf4, "front" },
1069 { 0xf5, "copy" },
1070 { 0xf6, "open" },
1071 { 0xf7, "paste" },
1072 { 0xf8, "find" },
1073 { 0xf9, "cut" },
1074 { 0xfa, "lf" },
1075 { 0xfb, "help" },
1076 { 0xfc, "meta_l" },
1077 { 0xfd, "meta_r" },
1078 { 0xfe, "compose" },
1079#endif
bellarda3a91a32004-06-04 11:06:21 +00001080 { 0, NULL },
1081};
1082
1083static int get_keycode(const char *key)
1084{
1085 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001086 char *endp;
1087 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001088
1089 for(p = key_defs; p->name != NULL; p++) {
1090 if (!strcmp(key, p->name))
1091 return p->keycode;
1092 }
bellard64866c32006-05-07 18:03:31 +00001093 if (strstart(key, "0x", NULL)) {
1094 ret = strtoul(key, &endp, 0);
1095 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1096 return ret;
1097 }
bellarda3a91a32004-06-04 11:06:21 +00001098 return -1;
1099}
1100
balrogc8256f92008-06-08 22:45:01 +00001101#define MAX_KEYCODES 16
1102static uint8_t keycodes[MAX_KEYCODES];
1103static int nb_pending_keycodes;
1104static QEMUTimer *key_timer;
1105
1106static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001107{
balrogc8256f92008-06-08 22:45:01 +00001108 int keycode;
1109
1110 while (nb_pending_keycodes > 0) {
1111 nb_pending_keycodes--;
1112 keycode = keycodes[nb_pending_keycodes];
1113 if (keycode & 0x80)
1114 kbd_put_keycode(0xe0);
1115 kbd_put_keycode(keycode | 0x80);
1116 }
1117}
1118
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001119static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001120{
balrog3401c0d2008-06-04 10:05:59 +00001121 char keyname_buf[16];
1122 char *separator;
1123 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001124 const char *string = qdict_get_str(qdict, "string");
1125 int has_hold_time = qdict_haskey(qdict, "hold_time");
1126 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001127
balrogc8256f92008-06-08 22:45:01 +00001128 if (nb_pending_keycodes > 0) {
1129 qemu_del_timer(key_timer);
1130 release_keys(NULL);
1131 }
1132 if (!has_hold_time)
1133 hold_time = 100;
1134 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001135 while (1) {
1136 separator = strchr(string, '-');
1137 keyname_len = separator ? separator - string : strlen(string);
1138 if (keyname_len > 0) {
1139 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1140 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001141 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001142 return;
bellarda3a91a32004-06-04 11:06:21 +00001143 }
balrogc8256f92008-06-08 22:45:01 +00001144 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001145 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001146 return;
1147 }
1148 keyname_buf[keyname_len] = 0;
1149 keycode = get_keycode(keyname_buf);
1150 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001151 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001152 return;
1153 }
balrogc8256f92008-06-08 22:45:01 +00001154 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001155 }
balrog3401c0d2008-06-04 10:05:59 +00001156 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001157 break;
balrog3401c0d2008-06-04 10:05:59 +00001158 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001159 }
balrogc8256f92008-06-08 22:45:01 +00001160 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001161 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001162 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001163 keycode = keycodes[i];
1164 if (keycode & 0x80)
1165 kbd_put_keycode(0xe0);
1166 kbd_put_keycode(keycode & 0x7f);
1167 }
balrogc8256f92008-06-08 22:45:01 +00001168 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001169 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001170 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001171}
1172
bellard13224a82006-07-14 22:03:35 +00001173static int mouse_button_state;
1174
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001175static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001176{
1177 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001178 const char *dx_str = qdict_get_str(qdict, "dx_str");
1179 const char *dy_str = qdict_get_str(qdict, "dy_str");
1180 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001181 dx = strtol(dx_str, NULL, 0);
1182 dy = strtol(dy_str, NULL, 0);
1183 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001184 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001185 dz = strtol(dz_str, NULL, 0);
1186 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1187}
1188
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001189static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001190{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001191 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001192 mouse_button_state = button_state;
1193 kbd_mouse_event(0, 0, 0, mouse_button_state);
1194}
1195
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001196static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001197{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001198 int size = qdict_get_int(qdict, "size");
1199 int addr = qdict_get_int(qdict, "addr");
1200 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001201 uint32_t val;
1202 int suffix;
1203
1204 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001205 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001206 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001207 addr++;
1208 }
1209 addr &= 0xffff;
1210
1211 switch(size) {
1212 default:
1213 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001214 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001215 suffix = 'b';
1216 break;
1217 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001218 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001219 suffix = 'w';
1220 break;
1221 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001222 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001223 suffix = 'l';
1224 break;
1225 }
aliguori376253e2009-03-05 23:01:23 +00001226 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1227 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001228}
bellarda3a91a32004-06-04 11:06:21 +00001229
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001230static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001231{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001232 int size = qdict_get_int(qdict, "size");
1233 int addr = qdict_get_int(qdict, "addr");
1234 int val = qdict_get_int(qdict, "val");
1235
Jan Kiszkaf1147842009-07-14 10:20:11 +02001236 addr &= IOPORTS_MASK;
1237
1238 switch (size) {
1239 default:
1240 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001241 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001242 break;
1243 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001244 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001245 break;
1246 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001247 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001248 break;
1249 }
1250}
1251
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001252static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001253{
1254 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001255 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001256
Jan Kiszka76e30d02009-07-02 00:19:02 +02001257 res = qemu_boot_set(bootdevice);
1258 if (res == 0) {
1259 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1260 } else if (res > 0) {
1261 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001262 } else {
aliguori376253e2009-03-05 23:01:23 +00001263 monitor_printf(mon, "no function defined to set boot device list for "
1264 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001265 }
1266}
1267
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001268static void do_system_reset(Monitor *mon, const QDict *qdict)
bellarde4f90822004-06-20 12:35:44 +00001269{
1270 qemu_system_reset_request();
1271}
1272
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001273static void do_system_powerdown(Monitor *mon, const QDict *qdict)
bellard34751872005-07-02 14:31:34 +00001274{
1275 qemu_system_powerdown_request();
1276}
1277
bellardb86bda52004-09-18 19:32:46 +00001278#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001279static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001280{
aliguori376253e2009-03-05 23:01:23 +00001281 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1282 addr,
1283 pte & mask,
1284 pte & PG_GLOBAL_MASK ? 'G' : '-',
1285 pte & PG_PSE_MASK ? 'P' : '-',
1286 pte & PG_DIRTY_MASK ? 'D' : '-',
1287 pte & PG_ACCESSED_MASK ? 'A' : '-',
1288 pte & PG_PCD_MASK ? 'C' : '-',
1289 pte & PG_PWT_MASK ? 'T' : '-',
1290 pte & PG_USER_MASK ? 'U' : '-',
1291 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001292}
1293
aliguori376253e2009-03-05 23:01:23 +00001294static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001295{
bellard6a00d602005-11-21 23:25:50 +00001296 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001297 int l1, l2;
1298 uint32_t pgd, pde, pte;
1299
bellard6a00d602005-11-21 23:25:50 +00001300 env = mon_get_cpu();
1301 if (!env)
1302 return;
1303
bellardb86bda52004-09-18 19:32:46 +00001304 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001305 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001306 return;
1307 }
1308 pgd = env->cr[3] & ~0xfff;
1309 for(l1 = 0; l1 < 1024; l1++) {
1310 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1311 pde = le32_to_cpu(pde);
1312 if (pde & PG_PRESENT_MASK) {
1313 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001314 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001315 } else {
1316 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001317 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001318 (uint8_t *)&pte, 4);
1319 pte = le32_to_cpu(pte);
1320 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001321 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001322 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001323 ~0xfff);
1324 }
1325 }
1326 }
1327 }
1328 }
1329}
1330
aliguori376253e2009-03-05 23:01:23 +00001331static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001332 uint32_t end, int prot)
1333{
bellard9746b152004-11-11 18:30:24 +00001334 int prot1;
1335 prot1 = *plast_prot;
1336 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001337 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001338 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1339 *pstart, end, end - *pstart,
1340 prot1 & PG_USER_MASK ? 'u' : '-',
1341 'r',
1342 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001343 }
1344 if (prot != 0)
1345 *pstart = end;
1346 else
1347 *pstart = -1;
1348 *plast_prot = prot;
1349 }
1350}
1351
aliguori376253e2009-03-05 23:01:23 +00001352static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001353{
bellard6a00d602005-11-21 23:25:50 +00001354 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001355 int l1, l2, prot, last_prot;
1356 uint32_t pgd, pde, pte, start, end;
1357
bellard6a00d602005-11-21 23:25:50 +00001358 env = mon_get_cpu();
1359 if (!env)
1360 return;
1361
bellardb86bda52004-09-18 19:32:46 +00001362 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001363 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001364 return;
1365 }
1366 pgd = env->cr[3] & ~0xfff;
1367 last_prot = 0;
1368 start = -1;
1369 for(l1 = 0; l1 < 1024; l1++) {
1370 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1371 pde = le32_to_cpu(pde);
1372 end = l1 << 22;
1373 if (pde & PG_PRESENT_MASK) {
1374 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1375 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001376 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001377 } else {
1378 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001379 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001380 (uint8_t *)&pte, 4);
1381 pte = le32_to_cpu(pte);
1382 end = (l1 << 22) + (l2 << 12);
1383 if (pte & PG_PRESENT_MASK) {
1384 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1385 } else {
1386 prot = 0;
1387 }
aliguori376253e2009-03-05 23:01:23 +00001388 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001389 }
1390 }
1391 } else {
1392 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001393 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001394 }
1395 }
1396}
1397#endif
1398
aurel327c664e22009-03-03 06:12:22 +00001399#if defined(TARGET_SH4)
1400
aliguori376253e2009-03-05 23:01:23 +00001401static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001402{
aliguori376253e2009-03-05 23:01:23 +00001403 monitor_printf(mon, " tlb%i:\t"
1404 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1405 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1406 "dirty=%hhu writethrough=%hhu\n",
1407 idx,
1408 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1409 tlb->v, tlb->sh, tlb->c, tlb->pr,
1410 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001411}
1412
aliguori376253e2009-03-05 23:01:23 +00001413static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001414{
1415 CPUState *env = mon_get_cpu();
1416 int i;
1417
aliguori376253e2009-03-05 23:01:23 +00001418 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001419 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001420 print_tlb (mon, i, &env->itlb[i]);
1421 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001422 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001423 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001424}
1425
1426#endif
1427
aliguori376253e2009-03-05 23:01:23 +00001428static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001429{
1430#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001431 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001432 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001433 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001434 else
aliguori376253e2009-03-05 23:01:23 +00001435 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001436#else
aliguori376253e2009-03-05 23:01:23 +00001437 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001438#endif
1439}
1440
aliguori030ea372009-04-21 22:30:47 +00001441static void do_info_numa(Monitor *mon)
1442{
aliguorib28b6232009-04-22 20:20:29 +00001443 int i;
aliguori030ea372009-04-21 22:30:47 +00001444 CPUState *env;
1445
1446 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1447 for (i = 0; i < nb_numa_nodes; i++) {
1448 monitor_printf(mon, "node %d cpus:", i);
1449 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1450 if (env->numa_node == i) {
1451 monitor_printf(mon, " %d", env->cpu_index);
1452 }
1453 }
1454 monitor_printf(mon, "\n");
1455 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1456 node_mem[i] >> 20);
1457 }
1458}
1459
bellard5f1ce942006-02-08 22:40:15 +00001460#ifdef CONFIG_PROFILER
1461
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001462int64_t qemu_time;
1463int64_t dev_time;
1464
aliguori376253e2009-03-05 23:01:23 +00001465static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001466{
1467 int64_t total;
1468 total = qemu_time;
1469 if (total == 0)
1470 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001471 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001472 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001473 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001474 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001475 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001476 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001477}
1478#else
aliguori376253e2009-03-05 23:01:23 +00001479static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001480{
aliguori376253e2009-03-05 23:01:23 +00001481 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001482}
1483#endif
1484
bellardec36b692006-07-16 18:57:03 +00001485/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001486static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001487
aliguori376253e2009-03-05 23:01:23 +00001488static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001489{
1490 int i;
1491 CaptureState *s;
1492
1493 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001494 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001495 s->ops.info (s->opaque);
1496 }
1497}
1498
Blue Swirl23130862009-06-06 08:22:04 +00001499#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001500static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001501{
1502 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001503 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001504 CaptureState *s;
1505
1506 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1507 if (i == n) {
1508 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001509 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00001510 qemu_free (s);
1511 return;
1512 }
1513 }
1514}
1515
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001516static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001517{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001518 const char *path = qdict_get_str(qdict, "path");
1519 int has_freq = qdict_haskey(qdict, "freq");
1520 int freq = qdict_get_try_int(qdict, "freq", -1);
1521 int has_bits = qdict_haskey(qdict, "bits");
1522 int bits = qdict_get_try_int(qdict, "bits", -1);
1523 int has_channels = qdict_haskey(qdict, "nchannels");
1524 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001525 CaptureState *s;
1526
1527 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001528
1529 freq = has_freq ? freq : 44100;
1530 bits = has_bits ? bits : 16;
1531 nchannels = has_channels ? nchannels : 2;
1532
1533 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001534 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001535 qemu_free (s);
1536 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001537 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001538}
1539#endif
1540
aurel32dc1c0b72008-04-27 23:52:12 +00001541#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001542static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001543{
1544 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001545 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001546
1547 for (env = first_cpu; env != NULL; env = env->next_cpu)
1548 if (env->cpu_index == cpu_index) {
1549 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1550 break;
1551 }
1552}
1553#endif
1554
aliguori376253e2009-03-05 23:01:23 +00001555static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001556{
aurel321b530a62009-04-05 20:08:59 +00001557 if (vm_running) {
1558 if (singlestep) {
1559 monitor_printf(mon, "VM status: running (single step mode)\n");
1560 } else {
1561 monitor_printf(mon, "VM status: running\n");
1562 }
1563 } else
aliguori376253e2009-03-05 23:01:23 +00001564 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001565}
1566
1567
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001568static void do_balloon(Monitor *mon, const QDict *qdict)
aliguoridf751fa2008-12-04 20:19:35 +00001569{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001570 int value = qdict_get_int(qdict, "value");
Anthony Liguoric227f092009-10-01 16:12:16 -05001571 ram_addr_t target = value;
aliguoridf751fa2008-12-04 20:19:35 +00001572 qemu_balloon(target << 20);
1573}
1574
aliguori376253e2009-03-05 23:01:23 +00001575static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001576{
Anthony Liguoric227f092009-10-01 16:12:16 -05001577 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00001578
1579 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001580 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001581 monitor_printf(mon, "Using KVM without synchronous MMU, "
1582 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001583 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001584 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001585 else
aliguori376253e2009-03-05 23:01:23 +00001586 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001587}
1588
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001589static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001590{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001591 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001592
aliguori76655d62009-03-06 20:27:37 +00001593 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001594 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001595 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001596 return acl;
1597}
aliguori76655d62009-03-06 20:27:37 +00001598
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001599static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001600{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001601 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001602 qemu_acl *acl = find_acl(mon, aclname);
1603 qemu_acl_entry *entry;
1604 int i = 0;
1605
1606 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001607 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001608 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001609 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001610 i++;
1611 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001612 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001613 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001614 }
1615}
1616
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001617static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001618{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001619 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001620 qemu_acl *acl = find_acl(mon, aclname);
1621
1622 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001623 qemu_acl_reset(acl);
1624 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001625 }
1626}
aliguori76655d62009-03-06 20:27:37 +00001627
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001628static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001629{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001630 const char *aclname = qdict_get_str(qdict, "aclname");
1631 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001632 qemu_acl *acl = find_acl(mon, aclname);
1633
1634 if (acl) {
1635 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001636 acl->defaultDeny = 0;
1637 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001638 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001639 acl->defaultDeny = 1;
1640 monitor_printf(mon, "acl: policy set to 'deny'\n");
1641 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001642 monitor_printf(mon, "acl: unknown policy '%s', "
1643 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001644 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001645 }
1646}
aliguori76655d62009-03-06 20:27:37 +00001647
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001648static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001649{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001650 const char *aclname = qdict_get_str(qdict, "aclname");
1651 const char *match = qdict_get_str(qdict, "match");
1652 const char *policy = qdict_get_str(qdict, "policy");
1653 int has_index = qdict_haskey(qdict, "index");
1654 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001655 qemu_acl *acl = find_acl(mon, aclname);
1656 int deny, ret;
1657
1658 if (acl) {
1659 if (strcmp(policy, "allow") == 0) {
1660 deny = 0;
1661 } else if (strcmp(policy, "deny") == 0) {
1662 deny = 1;
1663 } else {
1664 monitor_printf(mon, "acl: unknown policy '%s', "
1665 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001666 return;
1667 }
aliguori28a76be2009-03-06 20:27:40 +00001668 if (has_index)
1669 ret = qemu_acl_insert(acl, deny, match, index);
1670 else
1671 ret = qemu_acl_append(acl, deny, match);
1672 if (ret < 0)
1673 monitor_printf(mon, "acl: unable to add acl entry\n");
1674 else
1675 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001676 }
1677}
aliguori76655d62009-03-06 20:27:37 +00001678
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001679static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001680{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001681 const char *aclname = qdict_get_str(qdict, "aclname");
1682 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001683 qemu_acl *acl = find_acl(mon, aclname);
1684 int ret;
aliguori76655d62009-03-06 20:27:37 +00001685
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001686 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001687 ret = qemu_acl_remove(acl, match);
1688 if (ret < 0)
1689 monitor_printf(mon, "acl: no matching acl entry\n");
1690 else
1691 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001692 }
1693}
1694
Huang Ying79c4f6b2009-06-23 10:05:14 +08001695#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001696static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001697{
1698 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001699 int cpu_index = qdict_get_int(qdict, "cpu_index");
1700 int bank = qdict_get_int(qdict, "bank");
1701 uint64_t status = qdict_get_int(qdict, "status");
1702 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1703 uint64_t addr = qdict_get_int(qdict, "addr");
1704 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08001705
1706 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1707 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1708 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1709 break;
1710 }
1711}
1712#endif
1713
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001714static void do_getfd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001715{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001716 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001717 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001718 int fd;
1719
1720 fd = qemu_chr_get_msgfd(mon->chr);
1721 if (fd == -1) {
1722 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1723 return;
1724 }
1725
1726 if (qemu_isdigit(fdname[0])) {
1727 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1728 return;
1729 }
1730
1731 fd = dup(fd);
1732 if (fd == -1) {
1733 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1734 strerror(errno));
1735 return;
1736 }
1737
Blue Swirl72cf2d42009-09-12 07:36:22 +00001738 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001739 if (strcmp(monfd->name, fdname) != 0) {
1740 continue;
1741 }
1742
1743 close(monfd->fd);
1744 monfd->fd = fd;
1745 return;
1746 }
1747
Anthony Liguoric227f092009-10-01 16:12:16 -05001748 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001749 monfd->name = qemu_strdup(fdname);
1750 monfd->fd = fd;
1751
Blue Swirl72cf2d42009-09-12 07:36:22 +00001752 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001753}
1754
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001755static void do_closefd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001756{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001757 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001758 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001759
Blue Swirl72cf2d42009-09-12 07:36:22 +00001760 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001761 if (strcmp(monfd->name, fdname) != 0) {
1762 continue;
1763 }
1764
Blue Swirl72cf2d42009-09-12 07:36:22 +00001765 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001766 close(monfd->fd);
1767 qemu_free(monfd->name);
1768 qemu_free(monfd);
1769 return;
1770 }
1771
1772 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1773 fdname);
1774}
1775
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001776static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001777{
1778 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001779 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001780
1781 vm_stop(0);
1782
Juan Quintela05f24012009-08-20 19:42:22 +02001783 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001784 vm_start();
1785}
1786
Mark McLoughlin7768e042009-07-22 09:11:41 +01001787int monitor_get_fd(Monitor *mon, const char *fdname)
1788{
Anthony Liguoric227f092009-10-01 16:12:16 -05001789 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001790
Blue Swirl72cf2d42009-09-12 07:36:22 +00001791 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001792 int fd;
1793
1794 if (strcmp(monfd->name, fdname) != 0) {
1795 continue;
1796 }
1797
1798 fd = monfd->fd;
1799
1800 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001801 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001802 qemu_free(monfd->name);
1803 qemu_free(monfd);
1804
1805 return fd;
1806 }
1807
1808 return -1;
1809}
1810
Anthony Liguoric227f092009-10-01 16:12:16 -05001811static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001812#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001813 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001814};
1815
Blue Swirl23130862009-06-06 08:22:04 +00001816/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05001817static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001818 {
1819 .name = "version",
1820 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001821 .params = "",
1822 .help = "show the version of QEMU",
Luiz Capitulino910df892009-10-07 13:41:51 -03001823 .mhandler.info = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001824 },
1825 {
1826 .name = "network",
1827 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001828 .params = "",
1829 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03001830 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001831 },
1832 {
1833 .name = "chardev",
1834 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001835 .params = "",
1836 .help = "show the character devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001837 .mhandler.info = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001838 },
1839 {
1840 .name = "block",
1841 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001842 .params = "",
1843 .help = "show the block devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001844 .mhandler.info = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001845 },
1846 {
1847 .name = "blockstats",
1848 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001849 .params = "",
1850 .help = "show block device statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03001851 .mhandler.info = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001852 },
1853 {
1854 .name = "registers",
1855 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001856 .params = "",
1857 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03001858 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001859 },
1860 {
1861 .name = "cpus",
1862 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001863 .params = "",
1864 .help = "show infos for each CPU",
Luiz Capitulino910df892009-10-07 13:41:51 -03001865 .mhandler.info = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001866 },
1867 {
1868 .name = "history",
1869 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001870 .params = "",
1871 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03001872 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001873 },
1874 {
1875 .name = "irq",
1876 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001877 .params = "",
1878 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03001879 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001880 },
1881 {
1882 .name = "pic",
1883 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001884 .params = "",
1885 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03001886 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001887 },
1888 {
1889 .name = "pci",
1890 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001891 .params = "",
1892 .help = "show PCI info",
Luiz Capitulino910df892009-10-07 13:41:51 -03001893 .mhandler.info = pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001894 },
aurel327c664e22009-03-03 06:12:22 +00001895#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001896 {
1897 .name = "tlb",
1898 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001899 .params = "",
1900 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03001901 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001902 },
aurel327c664e22009-03-03 06:12:22 +00001903#endif
1904#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001905 {
1906 .name = "mem",
1907 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001908 .params = "",
1909 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03001910 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001911 },
1912 {
1913 .name = "hpet",
1914 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001915 .params = "",
1916 .help = "show state of HPET",
Luiz Capitulino910df892009-10-07 13:41:51 -03001917 .mhandler.info = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001918 },
bellardb86bda52004-09-18 19:32:46 +00001919#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001920 {
1921 .name = "jit",
1922 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001923 .params = "",
1924 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03001925 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001926 },
1927 {
1928 .name = "kvm",
1929 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001930 .params = "",
1931 .help = "show KVM information",
Luiz Capitulino910df892009-10-07 13:41:51 -03001932 .mhandler.info = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001933 },
1934 {
1935 .name = "numa",
1936 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001937 .params = "",
1938 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03001939 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001940 },
1941 {
1942 .name = "usb",
1943 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001944 .params = "",
1945 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001946 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001947 },
1948 {
1949 .name = "usbhost",
1950 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001951 .params = "",
1952 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001953 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001954 },
1955 {
1956 .name = "profile",
1957 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001958 .params = "",
1959 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03001960 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001961 },
1962 {
1963 .name = "capture",
1964 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001965 .params = "",
1966 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03001967 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001968 },
1969 {
1970 .name = "snapshots",
1971 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001972 .params = "",
1973 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03001974 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001975 },
1976 {
1977 .name = "status",
1978 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001979 .params = "",
1980 .help = "show the current VM status (running|paused)",
Luiz Capitulino910df892009-10-07 13:41:51 -03001981 .mhandler.info = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001982 },
1983 {
1984 .name = "pcmcia",
1985 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001986 .params = "",
1987 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03001988 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001989 },
1990 {
1991 .name = "mice",
1992 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001993 .params = "",
1994 .help = "show which guest mouse is receiving events",
Luiz Capitulino910df892009-10-07 13:41:51 -03001995 .mhandler.info = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001996 },
1997 {
1998 .name = "vnc",
1999 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002000 .params = "",
2001 .help = "show the vnc server status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002002 .mhandler.info = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002003 },
2004 {
2005 .name = "name",
2006 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002007 .params = "",
2008 .help = "show the current VM name",
Luiz Capitulino910df892009-10-07 13:41:51 -03002009 .mhandler.info = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002010 },
2011 {
2012 .name = "uuid",
2013 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002014 .params = "",
2015 .help = "show the current VM UUID",
Luiz Capitulino910df892009-10-07 13:41:51 -03002016 .mhandler.info = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002017 },
j_mayer76a66252007-03-07 08:32:30 +00002018#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002019 {
2020 .name = "cpustats",
2021 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002022 .params = "",
2023 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002024 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002025 },
j_mayer76a66252007-03-07 08:32:30 +00002026#endif
blueswir131a60e22007-10-26 18:42:59 +00002027#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002028 {
2029 .name = "usernet",
2030 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002031 .params = "",
2032 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002033 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002034 },
blueswir131a60e22007-10-26 18:42:59 +00002035#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002036 {
2037 .name = "migrate",
2038 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002039 .params = "",
2040 .help = "show migration status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002041 .mhandler.info = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002042 },
2043 {
2044 .name = "balloon",
2045 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002046 .params = "",
2047 .help = "show balloon information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002048 .mhandler.info = do_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002049 },
2050 {
2051 .name = "qtree",
2052 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002053 .params = "",
2054 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002055 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002056 },
2057 {
2058 .name = "qdm",
2059 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002060 .params = "",
2061 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002062 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002063 },
2064 {
2065 .name = "roms",
2066 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002067 .params = "",
2068 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002069 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002070 },
2071 {
2072 .name = NULL,
2073 },
bellard9dc39cb2004-03-14 21:38:27 +00002074};
2075
bellard9307c4c2004-04-04 12:57:25 +00002076/*******************************************************************/
2077
2078static const char *pch;
2079static jmp_buf expr_env;
2080
bellard92a31b12005-02-10 22:00:52 +00002081#define MD_TLONG 0
2082#define MD_I32 1
2083
bellard9307c4c2004-04-04 12:57:25 +00002084typedef struct MonitorDef {
2085 const char *name;
2086 int offset;
blueswir18662d652008-10-02 18:32:44 +00002087 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002088 int type;
bellard9307c4c2004-04-04 12:57:25 +00002089} MonitorDef;
2090
bellard57206fd2004-04-25 18:54:52 +00002091#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002092static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002093{
bellard6a00d602005-11-21 23:25:50 +00002094 CPUState *env = mon_get_cpu();
2095 if (!env)
2096 return 0;
2097 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002098}
2099#endif
2100
bellarda541f292004-04-12 20:39:29 +00002101#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002102static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002103{
bellard6a00d602005-11-21 23:25:50 +00002104 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002105 unsigned int u;
2106 int i;
2107
bellard6a00d602005-11-21 23:25:50 +00002108 if (!env)
2109 return 0;
2110
bellarda541f292004-04-12 20:39:29 +00002111 u = 0;
2112 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002113 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002114
2115 return u;
2116}
2117
blueswir18662d652008-10-02 18:32:44 +00002118static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002119{
bellard6a00d602005-11-21 23:25:50 +00002120 CPUState *env = mon_get_cpu();
2121 if (!env)
2122 return 0;
j_mayer0411a972007-10-25 21:35:50 +00002123 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002124}
2125
blueswir18662d652008-10-02 18:32:44 +00002126static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002127{
bellard6a00d602005-11-21 23:25:50 +00002128 CPUState *env = mon_get_cpu();
2129 if (!env)
2130 return 0;
aurel323d7b4172008-10-21 11:28:46 +00002131 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002132}
bellard9fddaa02004-05-21 12:59:32 +00002133
blueswir18662d652008-10-02 18:32:44 +00002134static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002135{
bellard6a00d602005-11-21 23:25:50 +00002136 CPUState *env = mon_get_cpu();
2137 if (!env)
2138 return 0;
2139 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002140}
2141
blueswir18662d652008-10-02 18:32:44 +00002142static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002143{
bellard6a00d602005-11-21 23:25:50 +00002144 CPUState *env = mon_get_cpu();
2145 if (!env)
2146 return 0;
2147 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002148}
2149
blueswir18662d652008-10-02 18:32:44 +00002150static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002151{
bellard6a00d602005-11-21 23:25:50 +00002152 CPUState *env = mon_get_cpu();
2153 if (!env)
2154 return 0;
2155 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002156}
bellarda541f292004-04-12 20:39:29 +00002157#endif
2158
bellarde95c8d52004-09-30 22:22:08 +00002159#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002160#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002161static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002162{
bellard6a00d602005-11-21 23:25:50 +00002163 CPUState *env = mon_get_cpu();
2164 if (!env)
2165 return 0;
2166 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002167}
bellard7b936c02005-10-30 17:05:13 +00002168#endif
bellarde95c8d52004-09-30 22:22:08 +00002169
blueswir18662d652008-10-02 18:32:44 +00002170static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002171{
bellard6a00d602005-11-21 23:25:50 +00002172 CPUState *env = mon_get_cpu();
2173 if (!env)
2174 return 0;
2175 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002176}
2177#endif
2178
blueswir18662d652008-10-02 18:32:44 +00002179static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002180#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002181
2182#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002183 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002184 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002185 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002186
bellard9307c4c2004-04-04 12:57:25 +00002187 { "eax", offsetof(CPUState, regs[0]) },
2188 { "ecx", offsetof(CPUState, regs[1]) },
2189 { "edx", offsetof(CPUState, regs[2]) },
2190 { "ebx", offsetof(CPUState, regs[3]) },
2191 { "esp|sp", offsetof(CPUState, regs[4]) },
2192 { "ebp|fp", offsetof(CPUState, regs[5]) },
2193 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002194 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002195#ifdef TARGET_X86_64
2196 { "r8", offsetof(CPUState, regs[8]) },
2197 { "r9", offsetof(CPUState, regs[9]) },
2198 { "r10", offsetof(CPUState, regs[10]) },
2199 { "r11", offsetof(CPUState, regs[11]) },
2200 { "r12", offsetof(CPUState, regs[12]) },
2201 { "r13", offsetof(CPUState, regs[13]) },
2202 { "r14", offsetof(CPUState, regs[14]) },
2203 { "r15", offsetof(CPUState, regs[15]) },
2204#endif
bellard9307c4c2004-04-04 12:57:25 +00002205 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002206 { "eip", offsetof(CPUState, eip) },
2207 SEG("cs", R_CS)
2208 SEG("ds", R_DS)
2209 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002210 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002211 SEG("fs", R_FS)
2212 SEG("gs", R_GS)
2213 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002214#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002215 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002216 { "r0", offsetof(CPUState, gpr[0]) },
2217 { "r1", offsetof(CPUState, gpr[1]) },
2218 { "r2", offsetof(CPUState, gpr[2]) },
2219 { "r3", offsetof(CPUState, gpr[3]) },
2220 { "r4", offsetof(CPUState, gpr[4]) },
2221 { "r5", offsetof(CPUState, gpr[5]) },
2222 { "r6", offsetof(CPUState, gpr[6]) },
2223 { "r7", offsetof(CPUState, gpr[7]) },
2224 { "r8", offsetof(CPUState, gpr[8]) },
2225 { "r9", offsetof(CPUState, gpr[9]) },
2226 { "r10", offsetof(CPUState, gpr[10]) },
2227 { "r11", offsetof(CPUState, gpr[11]) },
2228 { "r12", offsetof(CPUState, gpr[12]) },
2229 { "r13", offsetof(CPUState, gpr[13]) },
2230 { "r14", offsetof(CPUState, gpr[14]) },
2231 { "r15", offsetof(CPUState, gpr[15]) },
2232 { "r16", offsetof(CPUState, gpr[16]) },
2233 { "r17", offsetof(CPUState, gpr[17]) },
2234 { "r18", offsetof(CPUState, gpr[18]) },
2235 { "r19", offsetof(CPUState, gpr[19]) },
2236 { "r20", offsetof(CPUState, gpr[20]) },
2237 { "r21", offsetof(CPUState, gpr[21]) },
2238 { "r22", offsetof(CPUState, gpr[22]) },
2239 { "r23", offsetof(CPUState, gpr[23]) },
2240 { "r24", offsetof(CPUState, gpr[24]) },
2241 { "r25", offsetof(CPUState, gpr[25]) },
2242 { "r26", offsetof(CPUState, gpr[26]) },
2243 { "r27", offsetof(CPUState, gpr[27]) },
2244 { "r28", offsetof(CPUState, gpr[28]) },
2245 { "r29", offsetof(CPUState, gpr[29]) },
2246 { "r30", offsetof(CPUState, gpr[30]) },
2247 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002248 /* Floating point registers */
2249 { "f0", offsetof(CPUState, fpr[0]) },
2250 { "f1", offsetof(CPUState, fpr[1]) },
2251 { "f2", offsetof(CPUState, fpr[2]) },
2252 { "f3", offsetof(CPUState, fpr[3]) },
2253 { "f4", offsetof(CPUState, fpr[4]) },
2254 { "f5", offsetof(CPUState, fpr[5]) },
2255 { "f6", offsetof(CPUState, fpr[6]) },
2256 { "f7", offsetof(CPUState, fpr[7]) },
2257 { "f8", offsetof(CPUState, fpr[8]) },
2258 { "f9", offsetof(CPUState, fpr[9]) },
2259 { "f10", offsetof(CPUState, fpr[10]) },
2260 { "f11", offsetof(CPUState, fpr[11]) },
2261 { "f12", offsetof(CPUState, fpr[12]) },
2262 { "f13", offsetof(CPUState, fpr[13]) },
2263 { "f14", offsetof(CPUState, fpr[14]) },
2264 { "f15", offsetof(CPUState, fpr[15]) },
2265 { "f16", offsetof(CPUState, fpr[16]) },
2266 { "f17", offsetof(CPUState, fpr[17]) },
2267 { "f18", offsetof(CPUState, fpr[18]) },
2268 { "f19", offsetof(CPUState, fpr[19]) },
2269 { "f20", offsetof(CPUState, fpr[20]) },
2270 { "f21", offsetof(CPUState, fpr[21]) },
2271 { "f22", offsetof(CPUState, fpr[22]) },
2272 { "f23", offsetof(CPUState, fpr[23]) },
2273 { "f24", offsetof(CPUState, fpr[24]) },
2274 { "f25", offsetof(CPUState, fpr[25]) },
2275 { "f26", offsetof(CPUState, fpr[26]) },
2276 { "f27", offsetof(CPUState, fpr[27]) },
2277 { "f28", offsetof(CPUState, fpr[28]) },
2278 { "f29", offsetof(CPUState, fpr[29]) },
2279 { "f30", offsetof(CPUState, fpr[30]) },
2280 { "f31", offsetof(CPUState, fpr[31]) },
2281 { "fpscr", offsetof(CPUState, fpscr) },
2282 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002283 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002284 { "lr", offsetof(CPUState, lr) },
2285 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002286 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002287 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002288 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002289 { "msr", 0, &monitor_get_msr, },
2290 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002291 { "tbu", 0, &monitor_get_tbu, },
2292 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002293#if defined(TARGET_PPC64)
2294 /* Address space register */
2295 { "asr", offsetof(CPUState, asr) },
2296#endif
2297 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002298 { "sdr1", offsetof(CPUState, sdr1) },
2299 { "sr0", offsetof(CPUState, sr[0]) },
2300 { "sr1", offsetof(CPUState, sr[1]) },
2301 { "sr2", offsetof(CPUState, sr[2]) },
2302 { "sr3", offsetof(CPUState, sr[3]) },
2303 { "sr4", offsetof(CPUState, sr[4]) },
2304 { "sr5", offsetof(CPUState, sr[5]) },
2305 { "sr6", offsetof(CPUState, sr[6]) },
2306 { "sr7", offsetof(CPUState, sr[7]) },
2307 { "sr8", offsetof(CPUState, sr[8]) },
2308 { "sr9", offsetof(CPUState, sr[9]) },
2309 { "sr10", offsetof(CPUState, sr[10]) },
2310 { "sr11", offsetof(CPUState, sr[11]) },
2311 { "sr12", offsetof(CPUState, sr[12]) },
2312 { "sr13", offsetof(CPUState, sr[13]) },
2313 { "sr14", offsetof(CPUState, sr[14]) },
2314 { "sr15", offsetof(CPUState, sr[15]) },
2315 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002316#elif defined(TARGET_SPARC)
2317 { "g0", offsetof(CPUState, gregs[0]) },
2318 { "g1", offsetof(CPUState, gregs[1]) },
2319 { "g2", offsetof(CPUState, gregs[2]) },
2320 { "g3", offsetof(CPUState, gregs[3]) },
2321 { "g4", offsetof(CPUState, gregs[4]) },
2322 { "g5", offsetof(CPUState, gregs[5]) },
2323 { "g6", offsetof(CPUState, gregs[6]) },
2324 { "g7", offsetof(CPUState, gregs[7]) },
2325 { "o0", 0, monitor_get_reg },
2326 { "o1", 1, monitor_get_reg },
2327 { "o2", 2, monitor_get_reg },
2328 { "o3", 3, monitor_get_reg },
2329 { "o4", 4, monitor_get_reg },
2330 { "o5", 5, monitor_get_reg },
2331 { "o6", 6, monitor_get_reg },
2332 { "o7", 7, monitor_get_reg },
2333 { "l0", 8, monitor_get_reg },
2334 { "l1", 9, monitor_get_reg },
2335 { "l2", 10, monitor_get_reg },
2336 { "l3", 11, monitor_get_reg },
2337 { "l4", 12, monitor_get_reg },
2338 { "l5", 13, monitor_get_reg },
2339 { "l6", 14, monitor_get_reg },
2340 { "l7", 15, monitor_get_reg },
2341 { "i0", 16, monitor_get_reg },
2342 { "i1", 17, monitor_get_reg },
2343 { "i2", 18, monitor_get_reg },
2344 { "i3", 19, monitor_get_reg },
2345 { "i4", 20, monitor_get_reg },
2346 { "i5", 21, monitor_get_reg },
2347 { "i6", 22, monitor_get_reg },
2348 { "i7", 23, monitor_get_reg },
2349 { "pc", offsetof(CPUState, pc) },
2350 { "npc", offsetof(CPUState, npc) },
2351 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002352#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002353 { "psr", 0, &monitor_get_psr, },
2354 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002355#endif
bellarde95c8d52004-09-30 22:22:08 +00002356 { "tbr", offsetof(CPUState, tbr) },
2357 { "fsr", offsetof(CPUState, fsr) },
2358 { "f0", offsetof(CPUState, fpr[0]) },
2359 { "f1", offsetof(CPUState, fpr[1]) },
2360 { "f2", offsetof(CPUState, fpr[2]) },
2361 { "f3", offsetof(CPUState, fpr[3]) },
2362 { "f4", offsetof(CPUState, fpr[4]) },
2363 { "f5", offsetof(CPUState, fpr[5]) },
2364 { "f6", offsetof(CPUState, fpr[6]) },
2365 { "f7", offsetof(CPUState, fpr[7]) },
2366 { "f8", offsetof(CPUState, fpr[8]) },
2367 { "f9", offsetof(CPUState, fpr[9]) },
2368 { "f10", offsetof(CPUState, fpr[10]) },
2369 { "f11", offsetof(CPUState, fpr[11]) },
2370 { "f12", offsetof(CPUState, fpr[12]) },
2371 { "f13", offsetof(CPUState, fpr[13]) },
2372 { "f14", offsetof(CPUState, fpr[14]) },
2373 { "f15", offsetof(CPUState, fpr[15]) },
2374 { "f16", offsetof(CPUState, fpr[16]) },
2375 { "f17", offsetof(CPUState, fpr[17]) },
2376 { "f18", offsetof(CPUState, fpr[18]) },
2377 { "f19", offsetof(CPUState, fpr[19]) },
2378 { "f20", offsetof(CPUState, fpr[20]) },
2379 { "f21", offsetof(CPUState, fpr[21]) },
2380 { "f22", offsetof(CPUState, fpr[22]) },
2381 { "f23", offsetof(CPUState, fpr[23]) },
2382 { "f24", offsetof(CPUState, fpr[24]) },
2383 { "f25", offsetof(CPUState, fpr[25]) },
2384 { "f26", offsetof(CPUState, fpr[26]) },
2385 { "f27", offsetof(CPUState, fpr[27]) },
2386 { "f28", offsetof(CPUState, fpr[28]) },
2387 { "f29", offsetof(CPUState, fpr[29]) },
2388 { "f30", offsetof(CPUState, fpr[30]) },
2389 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002390#ifdef TARGET_SPARC64
2391 { "f32", offsetof(CPUState, fpr[32]) },
2392 { "f34", offsetof(CPUState, fpr[34]) },
2393 { "f36", offsetof(CPUState, fpr[36]) },
2394 { "f38", offsetof(CPUState, fpr[38]) },
2395 { "f40", offsetof(CPUState, fpr[40]) },
2396 { "f42", offsetof(CPUState, fpr[42]) },
2397 { "f44", offsetof(CPUState, fpr[44]) },
2398 { "f46", offsetof(CPUState, fpr[46]) },
2399 { "f48", offsetof(CPUState, fpr[48]) },
2400 { "f50", offsetof(CPUState, fpr[50]) },
2401 { "f52", offsetof(CPUState, fpr[52]) },
2402 { "f54", offsetof(CPUState, fpr[54]) },
2403 { "f56", offsetof(CPUState, fpr[56]) },
2404 { "f58", offsetof(CPUState, fpr[58]) },
2405 { "f60", offsetof(CPUState, fpr[60]) },
2406 { "f62", offsetof(CPUState, fpr[62]) },
2407 { "asi", offsetof(CPUState, asi) },
2408 { "pstate", offsetof(CPUState, pstate) },
2409 { "cansave", offsetof(CPUState, cansave) },
2410 { "canrestore", offsetof(CPUState, canrestore) },
2411 { "otherwin", offsetof(CPUState, otherwin) },
2412 { "wstate", offsetof(CPUState, wstate) },
2413 { "cleanwin", offsetof(CPUState, cleanwin) },
2414 { "fprs", offsetof(CPUState, fprs) },
2415#endif
bellard9307c4c2004-04-04 12:57:25 +00002416#endif
2417 { NULL },
2418};
2419
aliguori376253e2009-03-05 23:01:23 +00002420static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002421{
aliguori376253e2009-03-05 23:01:23 +00002422 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002423 longjmp(expr_env, 1);
2424}
2425
bellard6a00d602005-11-21 23:25:50 +00002426/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002427static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002428{
blueswir18662d652008-10-02 18:32:44 +00002429 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002430 void *ptr;
2431
bellard9307c4c2004-04-04 12:57:25 +00002432 for(md = monitor_defs; md->name != NULL; md++) {
2433 if (compare_cmd(name, md->name)) {
2434 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002435 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002436 } else {
bellard6a00d602005-11-21 23:25:50 +00002437 CPUState *env = mon_get_cpu();
2438 if (!env)
2439 return -2;
2440 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002441 switch(md->type) {
2442 case MD_I32:
2443 *pval = *(int32_t *)ptr;
2444 break;
2445 case MD_TLONG:
2446 *pval = *(target_long *)ptr;
2447 break;
2448 default:
2449 *pval = 0;
2450 break;
2451 }
bellard9307c4c2004-04-04 12:57:25 +00002452 }
2453 return 0;
2454 }
2455 }
2456 return -1;
2457}
2458
2459static void next(void)
2460{
Blue Swirl660f11b2009-07-31 21:16:51 +00002461 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002462 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002463 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002464 pch++;
2465 }
2466}
2467
aliguori376253e2009-03-05 23:01:23 +00002468static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002469
aliguori376253e2009-03-05 23:01:23 +00002470static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002471{
blueswir1c2efc952007-09-25 17:28:42 +00002472 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002473 char *p;
bellard6a00d602005-11-21 23:25:50 +00002474 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002475
2476 switch(*pch) {
2477 case '+':
2478 next();
aliguori376253e2009-03-05 23:01:23 +00002479 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002480 break;
2481 case '-':
2482 next();
aliguori376253e2009-03-05 23:01:23 +00002483 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002484 break;
2485 case '~':
2486 next();
aliguori376253e2009-03-05 23:01:23 +00002487 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002488 break;
2489 case '(':
2490 next();
aliguori376253e2009-03-05 23:01:23 +00002491 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002492 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002493 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002494 }
2495 next();
2496 break;
bellard81d09122004-07-14 17:21:37 +00002497 case '\'':
2498 pch++;
2499 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002500 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002501 n = *pch;
2502 pch++;
2503 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002504 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002505 next();
2506 break;
bellard9307c4c2004-04-04 12:57:25 +00002507 case '$':
2508 {
2509 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002510 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002511
bellard9307c4c2004-04-04 12:57:25 +00002512 pch++;
2513 q = buf;
2514 while ((*pch >= 'a' && *pch <= 'z') ||
2515 (*pch >= 'A' && *pch <= 'Z') ||
2516 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002517 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002518 if ((q - buf) < sizeof(buf) - 1)
2519 *q++ = *pch;
2520 pch++;
2521 }
blueswir1cd390082008-11-16 13:53:32 +00002522 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002523 pch++;
2524 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002525 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002526 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002527 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002528 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002529 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002530 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002531 }
2532 break;
2533 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002534 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002535 n = 0;
2536 break;
2537 default:
blueswir17743e582007-09-24 18:39:04 +00002538#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002539 n = strtoull(pch, &p, 0);
2540#else
bellard9307c4c2004-04-04 12:57:25 +00002541 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002542#endif
bellard9307c4c2004-04-04 12:57:25 +00002543 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002544 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002545 }
2546 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002547 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002548 pch++;
2549 break;
2550 }
2551 return n;
2552}
2553
2554
aliguori376253e2009-03-05 23:01:23 +00002555static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002556{
blueswir1c2efc952007-09-25 17:28:42 +00002557 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002558 int op;
ths3b46e622007-09-17 08:09:54 +00002559
aliguori376253e2009-03-05 23:01:23 +00002560 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002561 for(;;) {
2562 op = *pch;
2563 if (op != '*' && op != '/' && op != '%')
2564 break;
2565 next();
aliguori376253e2009-03-05 23:01:23 +00002566 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002567 switch(op) {
2568 default:
2569 case '*':
2570 val *= val2;
2571 break;
2572 case '/':
2573 case '%':
ths5fafdf22007-09-16 21:08:06 +00002574 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002575 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002576 if (op == '/')
2577 val /= val2;
2578 else
2579 val %= val2;
2580 break;
2581 }
2582 }
2583 return val;
2584}
2585
aliguori376253e2009-03-05 23:01:23 +00002586static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002587{
blueswir1c2efc952007-09-25 17:28:42 +00002588 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002589 int op;
bellard9307c4c2004-04-04 12:57:25 +00002590
aliguori376253e2009-03-05 23:01:23 +00002591 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002592 for(;;) {
2593 op = *pch;
2594 if (op != '&' && op != '|' && op != '^')
2595 break;
2596 next();
aliguori376253e2009-03-05 23:01:23 +00002597 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002598 switch(op) {
2599 default:
2600 case '&':
2601 val &= val2;
2602 break;
2603 case '|':
2604 val |= val2;
2605 break;
2606 case '^':
2607 val ^= val2;
2608 break;
2609 }
2610 }
2611 return val;
2612}
2613
aliguori376253e2009-03-05 23:01:23 +00002614static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002615{
blueswir1c2efc952007-09-25 17:28:42 +00002616 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002617 int op;
bellard9307c4c2004-04-04 12:57:25 +00002618
aliguori376253e2009-03-05 23:01:23 +00002619 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002620 for(;;) {
2621 op = *pch;
2622 if (op != '+' && op != '-')
2623 break;
2624 next();
aliguori376253e2009-03-05 23:01:23 +00002625 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002626 if (op == '+')
2627 val += val2;
2628 else
2629 val -= val2;
2630 }
2631 return val;
2632}
2633
aliguori376253e2009-03-05 23:01:23 +00002634static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002635{
2636 pch = *pp;
2637 if (setjmp(expr_env)) {
2638 *pp = pch;
2639 return -1;
2640 }
blueswir1cd390082008-11-16 13:53:32 +00002641 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002642 pch++;
aliguori376253e2009-03-05 23:01:23 +00002643 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002644 *pp = pch;
2645 return 0;
2646}
2647
2648static int get_str(char *buf, int buf_size, const char **pp)
2649{
2650 const char *p;
2651 char *q;
2652 int c;
2653
bellard81d09122004-07-14 17:21:37 +00002654 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002655 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002656 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002657 p++;
2658 if (*p == '\0') {
2659 fail:
bellard81d09122004-07-14 17:21:37 +00002660 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002661 *pp = p;
2662 return -1;
2663 }
bellard9307c4c2004-04-04 12:57:25 +00002664 if (*p == '\"') {
2665 p++;
2666 while (*p != '\0' && *p != '\"') {
2667 if (*p == '\\') {
2668 p++;
2669 c = *p++;
2670 switch(c) {
2671 case 'n':
2672 c = '\n';
2673 break;
2674 case 'r':
2675 c = '\r';
2676 break;
2677 case '\\':
2678 case '\'':
2679 case '\"':
2680 break;
2681 default:
2682 qemu_printf("unsupported escape code: '\\%c'\n", c);
2683 goto fail;
2684 }
2685 if ((q - buf) < buf_size - 1) {
2686 *q++ = c;
2687 }
2688 } else {
2689 if ((q - buf) < buf_size - 1) {
2690 *q++ = *p;
2691 }
2692 p++;
2693 }
2694 }
2695 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002696 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002697 goto fail;
2698 }
2699 p++;
2700 } else {
blueswir1cd390082008-11-16 13:53:32 +00002701 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002702 if ((q - buf) < buf_size - 1) {
2703 *q++ = *p;
2704 }
2705 p++;
2706 }
bellard9307c4c2004-04-04 12:57:25 +00002707 }
bellard81d09122004-07-14 17:21:37 +00002708 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002709 *pp = p;
2710 return 0;
2711}
2712
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002713/*
2714 * Store the command-name in cmdname, and return a pointer to
2715 * the remaining of the command string.
2716 */
2717static const char *get_command_name(const char *cmdline,
2718 char *cmdname, size_t nlen)
2719{
2720 size_t len;
2721 const char *p, *pstart;
2722
2723 p = cmdline;
2724 while (qemu_isspace(*p))
2725 p++;
2726 if (*p == '\0')
2727 return NULL;
2728 pstart = p;
2729 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2730 p++;
2731 len = p - pstart;
2732 if (len > nlen - 1)
2733 len = nlen - 1;
2734 memcpy(cmdname, pstart, len);
2735 cmdname[len] = '\0';
2736 return p;
2737}
2738
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002739/**
2740 * Read key of 'type' into 'key' and return the current
2741 * 'type' pointer.
2742 */
2743static char *key_get_info(const char *type, char **key)
2744{
2745 size_t len;
2746 char *p, *str;
2747
2748 if (*type == ',')
2749 type++;
2750
2751 p = strchr(type, ':');
2752 if (!p) {
2753 *key = NULL;
2754 return NULL;
2755 }
2756 len = p - type;
2757
2758 str = qemu_malloc(len + 1);
2759 memcpy(str, type, len);
2760 str[len] = '\0';
2761
2762 *key = str;
2763 return ++p;
2764}
2765
bellard9307c4c2004-04-04 12:57:25 +00002766static int default_fmt_format = 'x';
2767static int default_fmt_size = 4;
2768
2769#define MAX_ARGS 16
2770
Anthony Liguoric227f092009-10-01 16:12:16 -05002771static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002772 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002773 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00002774{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002775 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03002776 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05002777 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002778 char cmdname[256];
2779 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002780 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00002781
2782#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002783 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002784#endif
ths3b46e622007-09-17 08:09:54 +00002785
bellard9307c4c2004-04-04 12:57:25 +00002786 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002787 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2788 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002789 return NULL;
ths3b46e622007-09-17 08:09:54 +00002790
bellard9307c4c2004-04-04 12:57:25 +00002791 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002792 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002793 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002794 break;
bellard9dc39cb2004-03-14 21:38:27 +00002795 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002796
2797 if (cmd->name == NULL) {
2798 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002799 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002800 }
bellard9307c4c2004-04-04 12:57:25 +00002801
bellard9307c4c2004-04-04 12:57:25 +00002802 /* parse the parameters */
2803 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002804 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002805 typestr = key_get_info(typestr, &key);
2806 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002807 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002808 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002809 typestr++;
2810 switch(c) {
2811 case 'F':
bellard81d09122004-07-14 17:21:37 +00002812 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002813 case 's':
2814 {
2815 int ret;
ths3b46e622007-09-17 08:09:54 +00002816
blueswir1cd390082008-11-16 13:53:32 +00002817 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002818 p++;
2819 if (*typestr == '?') {
2820 typestr++;
2821 if (*p == '\0') {
2822 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002823 break;
bellard9307c4c2004-04-04 12:57:25 +00002824 }
2825 }
2826 ret = get_str(buf, sizeof(buf), &p);
2827 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002828 switch(c) {
2829 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002830 monitor_printf(mon, "%s: filename expected\n",
2831 cmdname);
bellard81d09122004-07-14 17:21:37 +00002832 break;
2833 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002834 monitor_printf(mon, "%s: block device name expected\n",
2835 cmdname);
bellard81d09122004-07-14 17:21:37 +00002836 break;
2837 default:
aliguori376253e2009-03-05 23:01:23 +00002838 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002839 break;
2840 }
bellard9307c4c2004-04-04 12:57:25 +00002841 goto fail;
2842 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002843 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002844 }
2845 break;
2846 case '/':
2847 {
2848 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002849
blueswir1cd390082008-11-16 13:53:32 +00002850 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002851 p++;
2852 if (*p == '/') {
2853 /* format found */
2854 p++;
2855 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002856 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002857 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002858 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002859 count = count * 10 + (*p - '0');
2860 p++;
2861 }
2862 }
2863 size = -1;
2864 format = -1;
2865 for(;;) {
2866 switch(*p) {
2867 case 'o':
2868 case 'd':
2869 case 'u':
2870 case 'x':
2871 case 'i':
2872 case 'c':
2873 format = *p++;
2874 break;
2875 case 'b':
2876 size = 1;
2877 p++;
2878 break;
2879 case 'h':
2880 size = 2;
2881 p++;
2882 break;
2883 case 'w':
2884 size = 4;
2885 p++;
2886 break;
2887 case 'g':
2888 case 'L':
2889 size = 8;
2890 p++;
2891 break;
2892 default:
2893 goto next;
2894 }
2895 }
2896 next:
blueswir1cd390082008-11-16 13:53:32 +00002897 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002898 monitor_printf(mon, "invalid char in format: '%c'\n",
2899 *p);
bellard9307c4c2004-04-04 12:57:25 +00002900 goto fail;
2901 }
bellard9307c4c2004-04-04 12:57:25 +00002902 if (format < 0)
2903 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002904 if (format != 'i') {
2905 /* for 'i', not specifying a size gives -1 as size */
2906 if (size < 0)
2907 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002908 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002909 }
bellard9307c4c2004-04-04 12:57:25 +00002910 default_fmt_format = format;
2911 } else {
2912 count = 1;
2913 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002914 if (format != 'i') {
2915 size = default_fmt_size;
2916 } else {
2917 size = -1;
2918 }
bellard9307c4c2004-04-04 12:57:25 +00002919 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002920 qdict_put(qdict, "count", qint_from_int(count));
2921 qdict_put(qdict, "format", qint_from_int(format));
2922 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002923 }
2924 break;
2925 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002926 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002927 {
blueswir1c2efc952007-09-25 17:28:42 +00002928 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002929
blueswir1cd390082008-11-16 13:53:32 +00002930 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002931 p++;
bellard34405572004-06-08 00:55:58 +00002932 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002933 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002934 if (*p == '\0') {
2935 typestr++;
2936 break;
2937 }
bellard34405572004-06-08 00:55:58 +00002938 } else {
2939 if (*p == '.') {
2940 p++;
blueswir1cd390082008-11-16 13:53:32 +00002941 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002942 p++;
bellard34405572004-06-08 00:55:58 +00002943 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002944 typestr++;
2945 break;
bellard34405572004-06-08 00:55:58 +00002946 }
2947 }
bellard13224a82006-07-14 22:03:35 +00002948 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002949 }
aliguori376253e2009-03-05 23:01:23 +00002950 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002951 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002952 /* Check if 'i' is greater than 32-bit */
2953 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2954 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
2955 monitor_printf(mon, "integer is for 32-bit values\n");
2956 goto fail;
2957 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002958 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002959 }
2960 break;
2961 case '-':
2962 {
2963 int has_option;
2964 /* option */
ths3b46e622007-09-17 08:09:54 +00002965
bellard9307c4c2004-04-04 12:57:25 +00002966 c = *typestr++;
2967 if (c == '\0')
2968 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002969 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002970 p++;
2971 has_option = 0;
2972 if (*p == '-') {
2973 p++;
2974 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002975 monitor_printf(mon, "%s: unsupported option -%c\n",
2976 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002977 goto fail;
2978 }
2979 p++;
2980 has_option = 1;
2981 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002982 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00002983 }
2984 break;
2985 default:
2986 bad_type:
aliguori376253e2009-03-05 23:01:23 +00002987 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00002988 goto fail;
2989 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002990 qemu_free(key);
2991 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002992 }
2993 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002994 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002995 p++;
2996 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002997 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2998 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00002999 goto fail;
3000 }
3001
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003002 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003003
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003004fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003005 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003006 return NULL;
3007}
3008
3009static void monitor_handle_command(Monitor *mon, const char *cmdline)
3010{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003011 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003012 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003013
3014 qdict = qdict_new();
3015
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003016 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003017 if (cmd) {
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003018 qemu_errors_to_mon(mon);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003019 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003020 qemu_errors_to_previous();
3021 }
3022
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003023 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003024}
3025
bellard81d09122004-07-14 17:21:37 +00003026static void cmd_completion(const char *name, const char *list)
3027{
3028 const char *p, *pstart;
3029 char cmd[128];
3030 int len;
3031
3032 p = list;
3033 for(;;) {
3034 pstart = p;
3035 p = strchr(p, '|');
3036 if (!p)
3037 p = pstart + strlen(pstart);
3038 len = p - pstart;
3039 if (len > sizeof(cmd) - 2)
3040 len = sizeof(cmd) - 2;
3041 memcpy(cmd, pstart, len);
3042 cmd[len] = '\0';
3043 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003044 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003045 }
3046 if (*p == '\0')
3047 break;
3048 p++;
3049 }
3050}
3051
3052static void file_completion(const char *input)
3053{
3054 DIR *ffs;
3055 struct dirent *d;
3056 char path[1024];
3057 char file[1024], file_prefix[1024];
3058 int input_path_len;
3059 const char *p;
3060
ths5fafdf22007-09-16 21:08:06 +00003061 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003062 if (!p) {
3063 input_path_len = 0;
3064 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003065 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003066 } else {
3067 input_path_len = p - input + 1;
3068 memcpy(path, input, input_path_len);
3069 if (input_path_len > sizeof(path) - 1)
3070 input_path_len = sizeof(path) - 1;
3071 path[input_path_len] = '\0';
3072 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3073 }
3074#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003075 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3076 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003077#endif
3078 ffs = opendir(path);
3079 if (!ffs)
3080 return;
3081 for(;;) {
3082 struct stat sb;
3083 d = readdir(ffs);
3084 if (!d)
3085 break;
3086 if (strstart(d->d_name, file_prefix, NULL)) {
3087 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003088 if (input_path_len < sizeof(file))
3089 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3090 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003091 /* stat the file to find out if it's a directory.
3092 * In that case add a slash to speed up typing long paths
3093 */
3094 stat(file, &sb);
3095 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003096 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003097 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003098 }
3099 }
3100 closedir(ffs);
3101}
3102
aliguori51de9762009-03-05 23:00:43 +00003103static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003104{
aliguori51de9762009-03-05 23:00:43 +00003105 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003106 const char *input = opaque;
3107
3108 if (input[0] == '\0' ||
3109 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003110 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003111 }
3112}
3113
3114/* NOTE: this parser is an approximate form of the real command parser */
3115static void parse_cmdline(const char *cmdline,
3116 int *pnb_args, char **args)
3117{
3118 const char *p;
3119 int nb_args, ret;
3120 char buf[1024];
3121
3122 p = cmdline;
3123 nb_args = 0;
3124 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003125 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003126 p++;
3127 if (*p == '\0')
3128 break;
3129 if (nb_args >= MAX_ARGS)
3130 break;
3131 ret = get_str(buf, sizeof(buf), &p);
3132 args[nb_args] = qemu_strdup(buf);
3133 nb_args++;
3134 if (ret < 0)
3135 break;
3136 }
3137 *pnb_args = nb_args;
3138}
3139
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003140static const char *next_arg_type(const char *typestr)
3141{
3142 const char *p = strchr(typestr, ':');
3143 return (p != NULL ? ++p : typestr);
3144}
3145
aliguori4c36ba32009-03-05 23:01:37 +00003146static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003147{
3148 const char *cmdname;
3149 char *args[MAX_ARGS];
3150 int nb_args, i, len;
3151 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003152 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003153 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003154
3155 parse_cmdline(cmdline, &nb_args, args);
3156#ifdef DEBUG_COMPLETION
3157 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003158 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003159 }
3160#endif
3161
3162 /* if the line ends with a space, it means we want to complete the
3163 next arg */
3164 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003165 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003166 if (nb_args >= MAX_ARGS)
3167 return;
3168 args[nb_args++] = qemu_strdup("");
3169 }
3170 if (nb_args <= 1) {
3171 /* command completion */
3172 if (nb_args == 0)
3173 cmdname = "";
3174 else
3175 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003176 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003177 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003178 cmd_completion(cmdname, cmd->name);
3179 }
3180 } else {
3181 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003182 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003183 if (compare_cmd(args[0], cmd->name))
3184 goto found;
3185 }
3186 return;
3187 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003188 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003189 for(i = 0; i < nb_args - 2; i++) {
3190 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003191 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003192 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003193 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003194 }
3195 }
3196 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003197 if (*ptype == '-' && ptype[1] != '\0') {
3198 ptype += 2;
3199 }
bellard81d09122004-07-14 17:21:37 +00003200 switch(*ptype) {
3201 case 'F':
3202 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003203 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003204 file_completion(str);
3205 break;
3206 case 'B':
3207 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003208 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003209 bdrv_iterate(block_completion_it, (void *)str);
3210 break;
bellard7fe48482004-10-09 18:08:01 +00003211 case 's':
3212 /* XXX: more generic ? */
3213 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003214 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003215 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3216 cmd_completion(str, cmd->name);
3217 }
bellard64866c32006-05-07 18:03:31 +00003218 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003219 char *sep = strrchr(str, '-');
3220 if (sep)
3221 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003222 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003223 for(key = key_defs; key->name != NULL; key++) {
3224 cmd_completion(str, key->name);
3225 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003226 } else if (!strcmp(cmd->name, "help|?")) {
3227 readline_set_completion_index(cur_mon->rs, strlen(str));
3228 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3229 cmd_completion(str, cmd->name);
3230 }
bellard7fe48482004-10-09 18:08:01 +00003231 }
3232 break;
bellard81d09122004-07-14 17:21:37 +00003233 default:
3234 break;
3235 }
3236 }
3237 for(i = 0; i < nb_args; i++)
3238 qemu_free(args[i]);
3239}
3240
aliguori731b0362009-03-05 23:01:42 +00003241static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003242{
aliguori731b0362009-03-05 23:01:42 +00003243 Monitor *mon = opaque;
3244
3245 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003246}
3247
aliguori731b0362009-03-05 23:01:42 +00003248static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003249{
aliguori731b0362009-03-05 23:01:42 +00003250 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003251 int i;
aliguori376253e2009-03-05 23:01:23 +00003252
aliguori731b0362009-03-05 23:01:42 +00003253 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003254
aliguoricde76ee2009-03-05 23:01:51 +00003255 if (cur_mon->rs) {
3256 for (i = 0; i < size; i++)
3257 readline_handle_byte(cur_mon->rs, buf[i]);
3258 } else {
3259 if (size == 0 || buf[size - 1] != 0)
3260 monitor_printf(cur_mon, "corrupted command\n");
3261 else
3262 monitor_handle_command(cur_mon, (char *)buf);
3263 }
aliguori731b0362009-03-05 23:01:42 +00003264
3265 cur_mon = old_mon;
3266}
aliguorid8f44602008-10-06 13:52:44 +00003267
aliguori376253e2009-03-05 23:01:23 +00003268static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003269{
aliguori731b0362009-03-05 23:01:42 +00003270 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003271 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003272 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003273}
3274
aliguoricde76ee2009-03-05 23:01:51 +00003275int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003276{
aliguoricde76ee2009-03-05 23:01:51 +00003277 if (!mon->rs)
3278 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003279 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003280 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003281}
3282
aliguori376253e2009-03-05 23:01:23 +00003283void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003284{
aliguoricde76ee2009-03-05 23:01:51 +00003285 if (!mon->rs)
3286 return;
aliguori731b0362009-03-05 23:01:42 +00003287 if (--mon->suspend_cnt == 0)
3288 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003289}
3290
aliguori731b0362009-03-05 23:01:42 +00003291static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003292{
aliguori376253e2009-03-05 23:01:23 +00003293 Monitor *mon = opaque;
3294
aliguori2724b182009-03-05 23:01:47 +00003295 switch (event) {
3296 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003297 mon->mux_out = 0;
3298 if (mon->reset_seen) {
3299 readline_restart(mon->rs);
3300 monitor_resume(mon);
3301 monitor_flush(mon);
3302 } else {
3303 mon->suspend_cnt = 0;
3304 }
aliguori2724b182009-03-05 23:01:47 +00003305 break;
ths86e94de2007-01-05 22:01:59 +00003306
aliguori2724b182009-03-05 23:01:47 +00003307 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003308 if (mon->reset_seen) {
3309 if (mon->suspend_cnt == 0) {
3310 monitor_printf(mon, "\n");
3311 }
3312 monitor_flush(mon);
3313 monitor_suspend(mon);
3314 } else {
3315 mon->suspend_cnt++;
3316 }
3317 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00003318 break;
3319
3320 case CHR_EVENT_RESET:
3321 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3322 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003323 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00003324 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003325 }
3326 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00003327 break;
3328 }
ths86e94de2007-01-05 22:01:59 +00003329}
3330
aliguori76655d62009-03-06 20:27:37 +00003331
3332/*
3333 * Local variables:
3334 * c-indent-level: 4
3335 * c-basic-offset: 4
3336 * tab-width: 8
3337 * End:
3338 */
3339
aliguori731b0362009-03-05 23:01:42 +00003340void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003341{
aliguori731b0362009-03-05 23:01:42 +00003342 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003343 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003344
3345 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003346 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003347 is_first_init = 0;
3348 }
aliguori87127162009-03-05 23:01:29 +00003349
3350 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003351
aliguori87127162009-03-05 23:01:29 +00003352 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003353 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00003354 if (flags & MONITOR_USE_READLINE) {
3355 mon->rs = readline_init(mon, monitor_find_completion);
3356 monitor_read_command(mon, 0);
3357 }
aliguori87127162009-03-05 23:01:29 +00003358
aliguori731b0362009-03-05 23:01:42 +00003359 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3360 mon);
aliguori87127162009-03-05 23:01:29 +00003361
Blue Swirl72cf2d42009-09-12 07:36:22 +00003362 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003363 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003364 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003365}
3366
aliguori376253e2009-03-05 23:01:23 +00003367static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003368{
aliguoribb5fc202009-03-05 23:01:15 +00003369 BlockDriverState *bs = opaque;
3370 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003371
aliguoribb5fc202009-03-05 23:01:15 +00003372 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003373 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003374 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003375 }
aliguori731b0362009-03-05 23:01:42 +00003376 if (mon->password_completion_cb)
3377 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003378
aliguori731b0362009-03-05 23:01:42 +00003379 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003380}
aliguoric0f4ce72009-03-05 23:01:01 +00003381
aliguori376253e2009-03-05 23:01:23 +00003382void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003383 BlockDriverCompletionFunc *completion_cb,
3384 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003385{
aliguoricde76ee2009-03-05 23:01:51 +00003386 int err;
3387
aliguoribb5fc202009-03-05 23:01:15 +00003388 if (!bdrv_key_required(bs)) {
3389 if (completion_cb)
3390 completion_cb(opaque, 0);
3391 return;
3392 }
aliguoric0f4ce72009-03-05 23:01:01 +00003393
aliguori376253e2009-03-05 23:01:23 +00003394 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3395 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003396
aliguori731b0362009-03-05 23:01:42 +00003397 mon->password_completion_cb = completion_cb;
3398 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003399
aliguoricde76ee2009-03-05 23:01:51 +00003400 err = monitor_read_password(mon, bdrv_password_cb, bs);
3401
3402 if (err && completion_cb)
3403 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003404}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003405
3406typedef struct QemuErrorSink QemuErrorSink;
3407struct QemuErrorSink {
3408 enum {
3409 ERR_SINK_FILE,
3410 ERR_SINK_MONITOR,
3411 } dest;
3412 union {
3413 FILE *fp;
3414 Monitor *mon;
3415 };
3416 QemuErrorSink *previous;
3417};
3418
Blue Swirl528e93a2009-08-31 15:14:40 +00003419static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003420
3421void qemu_errors_to_file(FILE *fp)
3422{
3423 QemuErrorSink *sink;
3424
3425 sink = qemu_mallocz(sizeof(*sink));
3426 sink->dest = ERR_SINK_FILE;
3427 sink->fp = fp;
3428 sink->previous = qemu_error_sink;
3429 qemu_error_sink = sink;
3430}
3431
3432void qemu_errors_to_mon(Monitor *mon)
3433{
3434 QemuErrorSink *sink;
3435
3436 sink = qemu_mallocz(sizeof(*sink));
3437 sink->dest = ERR_SINK_MONITOR;
3438 sink->mon = mon;
3439 sink->previous = qemu_error_sink;
3440 qemu_error_sink = sink;
3441}
3442
3443void qemu_errors_to_previous(void)
3444{
3445 QemuErrorSink *sink;
3446
3447 assert(qemu_error_sink != NULL);
3448 sink = qemu_error_sink;
3449 qemu_error_sink = sink->previous;
3450 qemu_free(sink);
3451}
3452
3453void qemu_error(const char *fmt, ...)
3454{
3455 va_list args;
3456
3457 assert(qemu_error_sink != NULL);
3458 switch (qemu_error_sink->dest) {
3459 case ERR_SINK_FILE:
3460 va_start(args, fmt);
3461 vfprintf(qemu_error_sink->fp, fmt, args);
3462 va_end(args);
3463 break;
3464 case ERR_SINK_MONITOR:
3465 va_start(args, fmt);
3466 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3467 va_end(args);
3468 break;
3469 }
3470}