blob: 3286ba2b00db51129c6848f99699facc0f95e299 [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"
Luiz Capitulino8f3cec02009-10-07 13:42:04 -030049#include "qlist.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030050#include "qdict.h"
51#include "qstring.h"
ths6a5bd302007-12-03 17:05:38 +000052
bellard9dc39cb2004-03-14 21:38:27 +000053//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000054//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000055
bellard9307c4c2004-04-04 12:57:25 +000056/*
57 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000058 *
bellard9307c4c2004-04-04 12:57:25 +000059 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000060 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000061 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000062 * 'i' 32 bit integer
63 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000064 * '/' optional gdb-like print format (like "/10x")
65 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030066 * '?' optional type (for all types, except '/')
67 * '.' other form of optional type (for 'i' and 'l')
68 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000069 *
70 */
71
Anthony Liguoric227f092009-10-01 16:12:16 -050072typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000073 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000074 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +000075 const char *params;
76 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -030077 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -030078 union {
79 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -030080 void (*info_new)(Monitor *mon, QObject **ret_data);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -030081 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -030082 void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Luiz Capitulino910df892009-10-07 13:41:51 -030083 } mhandler;
Anthony Liguoric227f092009-10-01 16:12:16 -050084} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000085
Mark McLoughlinf07918f2009-07-22 09:11:40 +010086/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -050087typedef struct mon_fd_t mon_fd_t;
88struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +010089 char *name;
90 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -050091 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010092};
93
aliguori87127162009-03-05 23:01:29 +000094struct Monitor {
95 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +020096 int mux_out;
97 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +000098 int flags;
99 int suspend_cnt;
100 uint8_t outbuf[1024];
101 int outbuf_index;
102 ReadLineState *rs;
103 CPUState *mon_cpu;
104 BlockDriverCompletionFunc *password_completion_cb;
105 void *password_opaque;
Anthony Liguoric227f092009-10-01 16:12:16 -0500106 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000107 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000108};
109
Blue Swirl72cf2d42009-09-12 07:36:22 +0000110static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000111
Anthony Liguoric227f092009-10-01 16:12:16 -0500112static const mon_cmd_t mon_cmds[];
113static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000114
aliguori87127162009-03-05 23:01:29 +0000115Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000116
aliguori731b0362009-03-05 23:01:42 +0000117static void monitor_command_cb(Monitor *mon, const char *cmdline,
118 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000119
aliguori731b0362009-03-05 23:01:42 +0000120static void monitor_read_command(Monitor *mon, int show_prompt)
121{
122 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
123 if (show_prompt)
124 readline_show_prompt(mon->rs);
125}
bellard6a00d602005-11-21 23:25:50 +0000126
aliguoricde76ee2009-03-05 23:01:51 +0000127static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
128 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000129{
aliguoricde76ee2009-03-05 23:01:51 +0000130 if (mon->rs) {
131 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
132 /* prompt is printed on return from the command handler */
133 return 0;
134 } else {
135 monitor_printf(mon, "terminal does not support password prompting\n");
136 return -ENOTTY;
137 }
aliguoribb5fc202009-03-05 23:01:15 +0000138}
139
aliguori376253e2009-03-05 23:01:23 +0000140void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000141{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200142 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000143 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
144 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000145 }
146}
147
148/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000149static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000150{
ths60fe76f2007-12-16 03:02:09 +0000151 char c;
aliguori731b0362009-03-05 23:01:42 +0000152
153 if (!mon)
154 return;
155
bellard7e2515e2004-08-01 21:52:19 +0000156 for(;;) {
157 c = *str++;
158 if (c == '\0')
159 break;
bellard7ba12602006-07-14 20:26:42 +0000160 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000161 mon->outbuf[mon->outbuf_index++] = '\r';
162 mon->outbuf[mon->outbuf_index++] = c;
163 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
164 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000165 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000166 }
167}
168
aliguori376253e2009-03-05 23:01:23 +0000169void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000170{
171 char buf[4096];
172 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000173 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000174}
175
aliguori376253e2009-03-05 23:01:23 +0000176void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000177{
178 va_list ap;
179 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000180 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000181 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000182}
183
aliguori376253e2009-03-05 23:01:23 +0000184void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000185{
186 int i;
187
188 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000189 switch (filename[i]) {
190 case ' ':
191 case '"':
192 case '\\':
193 monitor_printf(mon, "\\%c", filename[i]);
194 break;
195 case '\t':
196 monitor_printf(mon, "\\t");
197 break;
198 case '\r':
199 monitor_printf(mon, "\\r");
200 break;
201 case '\n':
202 monitor_printf(mon, "\\n");
203 break;
204 default:
205 monitor_printf(mon, "%c", filename[i]);
206 break;
207 }
thsfef30742006-12-22 14:11:32 +0000208 }
209}
210
bellard7fe48482004-10-09 18:08:01 +0000211static int monitor_fprintf(FILE *stream, const char *fmt, ...)
212{
213 va_list ap;
214 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000215 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000216 va_end(ap);
217 return 0;
218}
219
Luiz Capitulino13c74252009-10-07 13:41:55 -0300220static void monitor_user_noop(Monitor *mon, const QObject *data) { }
221
Luiz Capitulino13917be2009-10-07 13:41:54 -0300222static inline int monitor_handler_ported(const mon_cmd_t *cmd)
223{
224 return cmd->user_print != NULL;
225}
226
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300227static void monitor_print_qobject(Monitor *mon, const QObject *data)
228{
229 switch (qobject_type(data)) {
230 case QTYPE_QSTRING:
231 monitor_printf(mon, "%s",qstring_get_str(qobject_to_qstring(data)));
232 break;
233 case QTYPE_QINT:
234 monitor_printf(mon, "%" PRId64,qint_get_int(qobject_to_qint(data)));
235 break;
236 default:
237 monitor_printf(mon, "ERROR: unsupported type: %d",
238 qobject_type(data));
239 break;
240 }
241
242 monitor_puts(mon, "\n");
243}
244
bellard9dc39cb2004-03-14 21:38:27 +0000245static int compare_cmd(const char *name, const char *list)
246{
247 const char *p, *pstart;
248 int len;
249 len = strlen(name);
250 p = list;
251 for(;;) {
252 pstart = p;
253 p = strchr(p, '|');
254 if (!p)
255 p = pstart + strlen(pstart);
256 if ((p - pstart) == len && !memcmp(pstart, name, len))
257 return 1;
258 if (*p == '\0')
259 break;
260 p++;
261 }
262 return 0;
263}
264
Anthony Liguoric227f092009-10-01 16:12:16 -0500265static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000266 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000267{
Anthony Liguoric227f092009-10-01 16:12:16 -0500268 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000269
270 for(cmd = cmds; cmd->name != NULL; cmd++) {
271 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000272 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
273 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000274 }
275}
276
aliguori376253e2009-03-05 23:01:23 +0000277static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000278{
279 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000280 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000281 } else {
aliguori376253e2009-03-05 23:01:23 +0000282 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000283 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000284 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000285 monitor_printf(mon, "Log items (comma separated):\n");
286 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000287 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000288 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000289 }
290 }
bellard9dc39cb2004-03-14 21:38:27 +0000291 }
292}
293
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300294static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300295{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300296 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300297}
298
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300299static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000300{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200301 int all_devices;
302 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300303 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000304
bellard7954c732006-08-01 15:52:40 +0000305 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000306 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200307 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300308 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200309 continue;
310 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000311 }
312}
313
Luiz Capitulino13c74252009-10-07 13:41:55 -0300314static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000315{
Anthony Liguoric227f092009-10-01 16:12:16 -0500316 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300317 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000318
bellard9307c4c2004-04-04 12:57:25 +0000319 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000320 goto help;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300321
322 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000323 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300324 break;
bellard9dc39cb2004-03-14 21:38:27 +0000325 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300326
327 if (cmd->name == NULL)
328 goto help;
329
330 if (monitor_handler_ported(cmd)) {
331 cmd->mhandler.info_new(mon, ret_data);
332 if (*ret_data)
333 cmd->user_print(mon, *ret_data);
334 } else {
335 cmd->mhandler.info(mon);
336 }
337
bellard9dc39cb2004-03-14 21:38:27 +0000338 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300339
340help:
341 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000342}
343
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300344/**
345 * do_info_version(): Show QEMU version
346 */
347static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000348{
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300349 *ret_data = QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION));
bellard9bc9d1c2004-10-10 15:15:51 +0000350}
351
aliguori376253e2009-03-05 23:01:23 +0000352static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000353{
354 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000355 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000356}
357
aurel32bf4f74c2008-12-18 22:42:34 +0000358#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000359static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000360{
aliguori376253e2009-03-05 23:01:23 +0000361 monitor_printf(mon, "HPET is %s by QEMU\n",
362 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000363}
aurel32bf4f74c2008-12-18 22:42:34 +0000364#endif
aliguori16b29ae2008-12-17 23:28:44 +0000365
aliguori376253e2009-03-05 23:01:23 +0000366static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000367{
aliguori376253e2009-03-05 23:01:23 +0000368 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
369 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
370 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
371 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
372 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000373}
374
bellard6a00d602005-11-21 23:25:50 +0000375/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000376static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000377{
378 CPUState *env;
379
380 for(env = first_cpu; env != NULL; env = env->next_cpu) {
381 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000382 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000383 return 0;
384 }
385 }
386 return -1;
387}
388
pbrook9596ebb2007-11-18 01:44:38 +0000389static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000390{
aliguori731b0362009-03-05 23:01:42 +0000391 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000392 mon_set_cpu(0);
393 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300394 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000395 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000396}
397
aliguori376253e2009-03-05 23:01:23 +0000398static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000399{
bellard6a00d602005-11-21 23:25:50 +0000400 CPUState *env;
401 env = mon_get_cpu();
402 if (!env)
403 return;
bellard9307c4c2004-04-04 12:57:25 +0000404#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000405 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000406 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000407#else
aliguori376253e2009-03-05 23:01:23 +0000408 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000409 0);
bellard9307c4c2004-04-04 12:57:25 +0000410#endif
411}
412
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300413static void print_cpu_iter(QObject *obj, void *opaque)
414{
415 QDict *cpu;
416 int active = ' ';
417 Monitor *mon = opaque;
418
419 assert(qobject_type(obj) == QTYPE_QDICT);
420 cpu = qobject_to_qdict(obj);
421
422 if (strcmp(qdict_get_str(cpu, "current"), "yes") == 0)
423 active = '*';
424
425 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
426
427#if defined(TARGET_I386)
428 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
429 (target_ulong) qdict_get_int(cpu, "pc"));
430#elif defined(TARGET_PPC)
431 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
432 (target_long) qdict_get_int(cpu, "nip"));
433#elif defined(TARGET_SPARC)
434 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
435 (target_long) qdict_get_int(cpu, "pc"));
436 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
437 (target_long) qdict_get_int(cpu, "npc"));
438#elif defined(TARGET_MIPS)
439 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
440 (target_long) qdict_get_int(cpu, "PC"));
441#endif
442
443 if (strcmp(qdict_get_str(cpu, "halted"), "yes") == 0)
444 monitor_printf(mon, " (halted)");
445
446 monitor_printf(mon, "\n");
447}
448
449static void monitor_print_cpus(Monitor *mon, const QObject *data)
450{
451 QList *cpu_list;
452
453 assert(qobject_type(data) == QTYPE_QLIST);
454 cpu_list = qobject_to_qlist(data);
455 qlist_iter(cpu_list, print_cpu_iter, mon);
456}
457
458/**
459 * do_info_cpus(): Show CPU information
460 *
461 * Return a QList with a QDict for each CPU.
462 *
463 * For example:
464 *
465 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
466 * { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]
467 */
468static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000469{
470 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300471 QList *cpu_list;
472
473 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000474
475 /* just to set the default cpu if not already done */
476 mon_get_cpu();
477
478 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300479 const char *answer;
480 QDict *cpu = qdict_new();
481
Avi Kivity4c0960c2009-08-17 23:19:53 +0300482 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300483
484 qdict_put(cpu, "CPU", qint_from_int(env->cpu_index));
485 answer = (env == mon->mon_cpu) ? "yes" : "no";
486 qdict_put(cpu, "current", qstring_from_str(answer));
487
bellard6a00d602005-11-21 23:25:50 +0000488#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300489 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000490#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300491 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000492#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300493 qdict_put(cpu, "pc", qint_from_int(env->pc));
494 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000495#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300496 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000497#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300498 answer = env->halted ? "yes" : "no";
499 qdict_put(cpu, "halted", qstring_from_str(answer));
500
501 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000502 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300503
504 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000505}
506
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300507static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000508{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300509 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000510 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000511 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000512}
513
aliguori376253e2009-03-05 23:01:23 +0000514static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000515{
aliguori376253e2009-03-05 23:01:23 +0000516 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000517}
518
aliguori376253e2009-03-05 23:01:23 +0000519static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000520{
521 int i;
bellard7e2515e2004-08-01 21:52:19 +0000522 const char *str;
ths3b46e622007-09-17 08:09:54 +0000523
aliguoricde76ee2009-03-05 23:01:51 +0000524 if (!mon->rs)
525 return;
bellard7e2515e2004-08-01 21:52:19 +0000526 i = 0;
527 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000528 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000529 if (!str)
530 break;
aliguori376253e2009-03-05 23:01:23 +0000531 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000532 i++;
bellardaa455482004-04-04 13:07:25 +0000533 }
534}
535
j_mayer76a66252007-03-07 08:32:30 +0000536#if defined(TARGET_PPC)
537/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000538static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000539{
540 CPUState *env;
541
542 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000543 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000544}
545#endif
546
Luiz Capitulinob223f352009-10-07 13:41:56 -0300547/**
548 * do_quit(): Quit QEMU execution
549 */
550static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000551{
552 exit(0);
553}
554
aliguori376253e2009-03-05 23:01:23 +0000555static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000556{
557 if (bdrv_is_inserted(bs)) {
558 if (!force) {
559 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000560 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000561 return -1;
562 }
563 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000564 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000565 return -1;
566 }
567 }
568 bdrv_close(bs);
569 }
570 return 0;
571}
572
Luiz Capitulinoe1c923a2009-10-16 12:23:49 -0300573static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000574{
575 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300576 int force = qdict_get_int(qdict, "force");
577 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000578
bellard9307c4c2004-04-04 12:57:25 +0000579 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000580 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000581 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000582 return;
583 }
aliguori376253e2009-03-05 23:01:23 +0000584 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000585}
586
aliguori376253e2009-03-05 23:01:23 +0000587static void do_change_block(Monitor *mon, const char *device,
588 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000589{
590 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000591 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000592
bellard9307c4c2004-04-04 12:57:25 +0000593 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000594 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000595 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000596 return;
597 }
aurel322ecea9b2008-06-18 22:10:01 +0000598 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100599 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000600 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000601 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000602 return;
603 }
604 }
aliguori376253e2009-03-05 23:01:23 +0000605 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000606 return;
aurel322ecea9b2008-06-18 22:10:01 +0000607 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000608 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000609}
610
aliguori376253e2009-03-05 23:01:23 +0000611static void change_vnc_password_cb(Monitor *mon, const char *password,
612 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000613{
614 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000615 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000616
aliguori731b0362009-03-05 23:01:42 +0000617 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000618}
619
aliguori376253e2009-03-05 23:01:23 +0000620static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000621{
ths70848512007-08-25 01:37:05 +0000622 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000623 strcmp(target, "password") == 0) {
624 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000625 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000626 strncpy(password, arg, sizeof(password));
627 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000628 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000629 } else {
aliguori376253e2009-03-05 23:01:23 +0000630 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000631 }
ths70848512007-08-25 01:37:05 +0000632 } else {
aliguori28a76be2009-03-06 20:27:40 +0000633 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000634 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000635 }
thse25a5822007-08-25 01:36:20 +0000636}
637
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300638static void do_change(Monitor *mon, const QDict *qdict)
thse25a5822007-08-25 01:36:20 +0000639{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300640 const char *device = qdict_get_str(qdict, "device");
641 const char *target = qdict_get_str(qdict, "target");
642 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000643 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000644 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000645 } else {
aliguori28a76be2009-03-06 20:27:40 +0000646 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000647 }
648}
649
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300650static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000651{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300652 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000653}
654
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300655static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000656{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300657 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000658}
659
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300660static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000661{
662 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300663 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000664
bellard9307c4c2004-04-04 12:57:25 +0000665 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000666 mask = 0;
667 } else {
bellard9307c4c2004-04-04 12:57:25 +0000668 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000669 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000670 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000671 return;
672 }
673 }
674 cpu_set_log(mask);
675}
676
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300677static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000678{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300679 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000680 if (!option || !strcmp(option, "on")) {
681 singlestep = 1;
682 } else if (!strcmp(option, "off")) {
683 singlestep = 0;
684 } else {
685 monitor_printf(mon, "unexpected option %s\n", option);
686 }
687}
688
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -0300689/**
690 * do_stop(): Stop VM execution
691 */
692static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +0000693{
694 vm_stop(EXCP_INTERRUPT);
695}
696
aliguoribb5fc202009-03-05 23:01:15 +0000697static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000698
aliguori376253e2009-03-05 23:01:23 +0000699struct bdrv_iterate_context {
700 Monitor *mon;
701 int err;
702};
aliguoric0f4ce72009-03-05 23:01:01 +0000703
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300704/**
705 * do_cont(): Resume emulation.
706 */
707static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +0000708{
709 struct bdrv_iterate_context context = { mon, 0 };
710
711 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000712 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000713 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000714 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000715}
716
aliguoribb5fc202009-03-05 23:01:15 +0000717static void bdrv_key_cb(void *opaque, int err)
718{
aliguori376253e2009-03-05 23:01:23 +0000719 Monitor *mon = opaque;
720
aliguoribb5fc202009-03-05 23:01:15 +0000721 /* another key was set successfully, retry to continue */
722 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300723 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000724}
725
726static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
727{
aliguori376253e2009-03-05 23:01:23 +0000728 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000729
aliguori376253e2009-03-05 23:01:23 +0000730 if (!context->err && bdrv_key_required(bs)) {
731 context->err = -EBUSY;
732 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
733 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000734 }
735}
736
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300737static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000738{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300739 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000740 if (!device)
741 device = "tcp::" DEFAULT_GDBSTUB_PORT;
742 if (gdbserver_start(device) < 0) {
743 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
744 device);
745 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000746 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000747 } else {
aliguori59030a82009-04-05 18:43:41 +0000748 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
749 device);
bellard8a7ddc32004-03-31 19:00:16 +0000750 }
751}
752
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300753static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100754{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300755 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100756 if (select_watchdog_action(action) == -1) {
757 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
758 }
759}
760
aliguori376253e2009-03-05 23:01:23 +0000761static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000762{
aliguori376253e2009-03-05 23:01:23 +0000763 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000764 switch(c) {
765 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000766 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000767 break;
768 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000769 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000770 break;
771 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000772 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000773 break;
774 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000775 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000776 break;
777 default:
778 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000779 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000780 } else {
aliguori376253e2009-03-05 23:01:23 +0000781 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000782 }
783 break;
784 }
aliguori376253e2009-03-05 23:01:23 +0000785 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000786}
787
aliguori376253e2009-03-05 23:01:23 +0000788static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -0500789 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000790{
bellard6a00d602005-11-21 23:25:50 +0000791 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000792 int nb_per_line, l, line_size, i, max_digits, len;
793 uint8_t buf[16];
794 uint64_t v;
795
796 if (format == 'i') {
797 int flags;
798 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000799 env = mon_get_cpu();
800 if (!env && !is_physical)
801 return;
bellard9307c4c2004-04-04 12:57:25 +0000802#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000803 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000804 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000805 } else if (wsize == 4) {
806 flags = 0;
807 } else {
bellard6a15fd12006-04-12 21:07:07 +0000808 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000809 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000810 if (env) {
811#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000812 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000813 (env->segs[R_CS].flags & DESC_L_MASK))
814 flags = 2;
815 else
816#endif
817 if (!(env->segs[R_CS].flags & DESC_B_MASK))
818 flags = 1;
819 }
bellard4c27ba22004-04-25 18:05:08 +0000820 }
821#endif
aliguori376253e2009-03-05 23:01:23 +0000822 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000823 return;
824 }
825
826 len = wsize * count;
827 if (wsize == 1)
828 line_size = 8;
829 else
830 line_size = 16;
831 nb_per_line = line_size / wsize;
832 max_digits = 0;
833
834 switch(format) {
835 case 'o':
836 max_digits = (wsize * 8 + 2) / 3;
837 break;
838 default:
839 case 'x':
840 max_digits = (wsize * 8) / 4;
841 break;
842 case 'u':
843 case 'd':
844 max_digits = (wsize * 8 * 10 + 32) / 33;
845 break;
846 case 'c':
847 wsize = 1;
848 break;
849 }
850
851 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000852 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000853 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000854 else
aliguori376253e2009-03-05 23:01:23 +0000855 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000856 l = len;
857 if (l > line_size)
858 l = line_size;
859 if (is_physical) {
860 cpu_physical_memory_rw(addr, buf, l, 0);
861 } else {
bellard6a00d602005-11-21 23:25:50 +0000862 env = mon_get_cpu();
863 if (!env)
864 break;
aliguoric8f79b62008-08-18 14:00:20 +0000865 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000866 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000867 break;
868 }
bellard9307c4c2004-04-04 12:57:25 +0000869 }
ths5fafdf22007-09-16 21:08:06 +0000870 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000871 while (i < l) {
872 switch(wsize) {
873 default:
874 case 1:
875 v = ldub_raw(buf + i);
876 break;
877 case 2:
878 v = lduw_raw(buf + i);
879 break;
880 case 4:
bellard92a31b12005-02-10 22:00:52 +0000881 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000882 break;
883 case 8:
884 v = ldq_raw(buf + i);
885 break;
886 }
aliguori376253e2009-03-05 23:01:23 +0000887 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000888 switch(format) {
889 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000890 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000891 break;
892 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000893 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000894 break;
895 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000896 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000897 break;
898 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000899 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000900 break;
901 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000902 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000903 break;
904 }
905 i += wsize;
906 }
aliguori376253e2009-03-05 23:01:23 +0000907 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000908 addr += l;
909 len -= l;
910 }
911}
912
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300913static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000914{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300915 int count = qdict_get_int(qdict, "count");
916 int format = qdict_get_int(qdict, "format");
917 int size = qdict_get_int(qdict, "size");
918 target_long addr = qdict_get_int(qdict, "addr");
919
aliguori376253e2009-03-05 23:01:23 +0000920 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000921}
922
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300923static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000924{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300925 int count = qdict_get_int(qdict, "count");
926 int format = qdict_get_int(qdict, "format");
927 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -0500928 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300929
aliguori376253e2009-03-05 23:01:23 +0000930 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000931}
932
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300933static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000934{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300935 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -0500936 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300937
blueswir17743e582007-09-24 18:39:04 +0000938#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000939 switch(format) {
940 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000941 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000942 break;
943 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000944 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000945 break;
946 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000947 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000948 break;
949 default:
950 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000951 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000952 break;
953 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000954 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000955 break;
956 }
bellard92a31b12005-02-10 22:00:52 +0000957#else
958 switch(format) {
959 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000960 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000961 break;
962 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000963 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000964 break;
965 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000966 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000967 break;
968 default:
969 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000970 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000971 break;
972 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000973 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000974 break;
975 }
976#endif
aliguori376253e2009-03-05 23:01:23 +0000977 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000978}
979
Luiz Capitulino57e09452009-10-16 12:23:43 -0300980static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +0000981{
982 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300983 uint32_t size = qdict_get_int(qdict, "size");
984 const char *filename = qdict_get_str(qdict, "filename");
985 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +0000986 uint32_t l;
987 CPUState *env;
988 uint8_t buf[1024];
989
990 env = mon_get_cpu();
991 if (!env)
992 return;
993
994 f = fopen(filename, "wb");
995 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000996 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000997 return;
998 }
999 while (size != 0) {
1000 l = sizeof(buf);
1001 if (l > size)
1002 l = size;
1003 cpu_memory_rw_debug(env, addr, buf, l, 0);
1004 fwrite(buf, 1, l, f);
1005 addr += l;
1006 size -= l;
1007 }
1008 fclose(f);
1009}
1010
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001011static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1012 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001013{
1014 FILE *f;
1015 uint32_t l;
1016 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001017 uint32_t size = qdict_get_int(qdict, "size");
1018 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001019 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +00001020
1021 f = fopen(filename, "wb");
1022 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001023 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +00001024 return;
1025 }
1026 while (size != 0) {
1027 l = sizeof(buf);
1028 if (l > size)
1029 l = size;
1030 cpu_physical_memory_rw(addr, buf, l, 0);
1031 fwrite(buf, 1, l, f);
1032 fflush(f);
1033 addr += l;
1034 size -= l;
1035 }
1036 fclose(f);
1037}
1038
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001039static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001040{
1041 uint32_t addr;
1042 uint8_t buf[1];
1043 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001044 uint32_t start = qdict_get_int(qdict, "start");
1045 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001046
1047 sum = 0;
1048 for(addr = start; addr < (start + size); addr++) {
1049 cpu_physical_memory_rw(addr, buf, 1, 0);
1050 /* BSD sum algorithm ('sum' Unix command) */
1051 sum = (sum >> 1) | (sum << 15);
1052 sum += buf[0];
1053 }
aliguori376253e2009-03-05 23:01:23 +00001054 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001055}
1056
bellarda3a91a32004-06-04 11:06:21 +00001057typedef struct {
1058 int keycode;
1059 const char *name;
1060} KeyDef;
1061
1062static const KeyDef key_defs[] = {
1063 { 0x2a, "shift" },
1064 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001065
bellarda3a91a32004-06-04 11:06:21 +00001066 { 0x38, "alt" },
1067 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001068 { 0x64, "altgr" },
1069 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001070 { 0x1d, "ctrl" },
1071 { 0x9d, "ctrl_r" },
1072
1073 { 0xdd, "menu" },
1074
1075 { 0x01, "esc" },
1076
1077 { 0x02, "1" },
1078 { 0x03, "2" },
1079 { 0x04, "3" },
1080 { 0x05, "4" },
1081 { 0x06, "5" },
1082 { 0x07, "6" },
1083 { 0x08, "7" },
1084 { 0x09, "8" },
1085 { 0x0a, "9" },
1086 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001087 { 0x0c, "minus" },
1088 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001089 { 0x0e, "backspace" },
1090
1091 { 0x0f, "tab" },
1092 { 0x10, "q" },
1093 { 0x11, "w" },
1094 { 0x12, "e" },
1095 { 0x13, "r" },
1096 { 0x14, "t" },
1097 { 0x15, "y" },
1098 { 0x16, "u" },
1099 { 0x17, "i" },
1100 { 0x18, "o" },
1101 { 0x19, "p" },
1102
1103 { 0x1c, "ret" },
1104
1105 { 0x1e, "a" },
1106 { 0x1f, "s" },
1107 { 0x20, "d" },
1108 { 0x21, "f" },
1109 { 0x22, "g" },
1110 { 0x23, "h" },
1111 { 0x24, "j" },
1112 { 0x25, "k" },
1113 { 0x26, "l" },
1114
1115 { 0x2c, "z" },
1116 { 0x2d, "x" },
1117 { 0x2e, "c" },
1118 { 0x2f, "v" },
1119 { 0x30, "b" },
1120 { 0x31, "n" },
1121 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001122 { 0x33, "comma" },
1123 { 0x34, "dot" },
1124 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001125
balrog4d3b6f62008-02-10 16:33:14 +00001126 { 0x37, "asterisk" },
1127
bellarda3a91a32004-06-04 11:06:21 +00001128 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001129 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001130 { 0x3b, "f1" },
1131 { 0x3c, "f2" },
1132 { 0x3d, "f3" },
1133 { 0x3e, "f4" },
1134 { 0x3f, "f5" },
1135 { 0x40, "f6" },
1136 { 0x41, "f7" },
1137 { 0x42, "f8" },
1138 { 0x43, "f9" },
1139 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001140 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001141 { 0x46, "scroll_lock" },
1142
bellard64866c32006-05-07 18:03:31 +00001143 { 0xb5, "kp_divide" },
1144 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001145 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001146 { 0x4e, "kp_add" },
1147 { 0x9c, "kp_enter" },
1148 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001149 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001150
1151 { 0x52, "kp_0" },
1152 { 0x4f, "kp_1" },
1153 { 0x50, "kp_2" },
1154 { 0x51, "kp_3" },
1155 { 0x4b, "kp_4" },
1156 { 0x4c, "kp_5" },
1157 { 0x4d, "kp_6" },
1158 { 0x47, "kp_7" },
1159 { 0x48, "kp_8" },
1160 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001161
bellarda3a91a32004-06-04 11:06:21 +00001162 { 0x56, "<" },
1163
1164 { 0x57, "f11" },
1165 { 0x58, "f12" },
1166
1167 { 0xb7, "print" },
1168
1169 { 0xc7, "home" },
1170 { 0xc9, "pgup" },
1171 { 0xd1, "pgdn" },
1172 { 0xcf, "end" },
1173
1174 { 0xcb, "left" },
1175 { 0xc8, "up" },
1176 { 0xd0, "down" },
1177 { 0xcd, "right" },
1178
1179 { 0xd2, "insert" },
1180 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001181#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1182 { 0xf0, "stop" },
1183 { 0xf1, "again" },
1184 { 0xf2, "props" },
1185 { 0xf3, "undo" },
1186 { 0xf4, "front" },
1187 { 0xf5, "copy" },
1188 { 0xf6, "open" },
1189 { 0xf7, "paste" },
1190 { 0xf8, "find" },
1191 { 0xf9, "cut" },
1192 { 0xfa, "lf" },
1193 { 0xfb, "help" },
1194 { 0xfc, "meta_l" },
1195 { 0xfd, "meta_r" },
1196 { 0xfe, "compose" },
1197#endif
bellarda3a91a32004-06-04 11:06:21 +00001198 { 0, NULL },
1199};
1200
1201static int get_keycode(const char *key)
1202{
1203 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001204 char *endp;
1205 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001206
1207 for(p = key_defs; p->name != NULL; p++) {
1208 if (!strcmp(key, p->name))
1209 return p->keycode;
1210 }
bellard64866c32006-05-07 18:03:31 +00001211 if (strstart(key, "0x", NULL)) {
1212 ret = strtoul(key, &endp, 0);
1213 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1214 return ret;
1215 }
bellarda3a91a32004-06-04 11:06:21 +00001216 return -1;
1217}
1218
balrogc8256f92008-06-08 22:45:01 +00001219#define MAX_KEYCODES 16
1220static uint8_t keycodes[MAX_KEYCODES];
1221static int nb_pending_keycodes;
1222static QEMUTimer *key_timer;
1223
1224static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001225{
balrogc8256f92008-06-08 22:45:01 +00001226 int keycode;
1227
1228 while (nb_pending_keycodes > 0) {
1229 nb_pending_keycodes--;
1230 keycode = keycodes[nb_pending_keycodes];
1231 if (keycode & 0x80)
1232 kbd_put_keycode(0xe0);
1233 kbd_put_keycode(keycode | 0x80);
1234 }
1235}
1236
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001237static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001238{
balrog3401c0d2008-06-04 10:05:59 +00001239 char keyname_buf[16];
1240 char *separator;
1241 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001242 const char *string = qdict_get_str(qdict, "string");
1243 int has_hold_time = qdict_haskey(qdict, "hold_time");
1244 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001245
balrogc8256f92008-06-08 22:45:01 +00001246 if (nb_pending_keycodes > 0) {
1247 qemu_del_timer(key_timer);
1248 release_keys(NULL);
1249 }
1250 if (!has_hold_time)
1251 hold_time = 100;
1252 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001253 while (1) {
1254 separator = strchr(string, '-');
1255 keyname_len = separator ? separator - string : strlen(string);
1256 if (keyname_len > 0) {
1257 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1258 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001259 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001260 return;
bellarda3a91a32004-06-04 11:06:21 +00001261 }
balrogc8256f92008-06-08 22:45:01 +00001262 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001263 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001264 return;
1265 }
1266 keyname_buf[keyname_len] = 0;
1267 keycode = get_keycode(keyname_buf);
1268 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001269 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001270 return;
1271 }
balrogc8256f92008-06-08 22:45:01 +00001272 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001273 }
balrog3401c0d2008-06-04 10:05:59 +00001274 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001275 break;
balrog3401c0d2008-06-04 10:05:59 +00001276 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001277 }
balrogc8256f92008-06-08 22:45:01 +00001278 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001279 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001280 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001281 keycode = keycodes[i];
1282 if (keycode & 0x80)
1283 kbd_put_keycode(0xe0);
1284 kbd_put_keycode(keycode & 0x7f);
1285 }
balrogc8256f92008-06-08 22:45:01 +00001286 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001287 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001288 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001289}
1290
bellard13224a82006-07-14 22:03:35 +00001291static int mouse_button_state;
1292
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001293static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001294{
1295 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001296 const char *dx_str = qdict_get_str(qdict, "dx_str");
1297 const char *dy_str = qdict_get_str(qdict, "dy_str");
1298 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001299 dx = strtol(dx_str, NULL, 0);
1300 dy = strtol(dy_str, NULL, 0);
1301 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001302 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001303 dz = strtol(dz_str, NULL, 0);
1304 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1305}
1306
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001307static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001308{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001309 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001310 mouse_button_state = button_state;
1311 kbd_mouse_event(0, 0, 0, mouse_button_state);
1312}
1313
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001314static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001315{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001316 int size = qdict_get_int(qdict, "size");
1317 int addr = qdict_get_int(qdict, "addr");
1318 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001319 uint32_t val;
1320 int suffix;
1321
1322 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001323 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001324 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001325 addr++;
1326 }
1327 addr &= 0xffff;
1328
1329 switch(size) {
1330 default:
1331 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001332 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001333 suffix = 'b';
1334 break;
1335 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001336 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001337 suffix = 'w';
1338 break;
1339 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001340 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001341 suffix = 'l';
1342 break;
1343 }
aliguori376253e2009-03-05 23:01:23 +00001344 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1345 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001346}
bellarda3a91a32004-06-04 11:06:21 +00001347
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001348static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001349{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001350 int size = qdict_get_int(qdict, "size");
1351 int addr = qdict_get_int(qdict, "addr");
1352 int val = qdict_get_int(qdict, "val");
1353
Jan Kiszkaf1147842009-07-14 10:20:11 +02001354 addr &= IOPORTS_MASK;
1355
1356 switch (size) {
1357 default:
1358 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001359 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001360 break;
1361 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001362 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001363 break;
1364 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001365 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001366 break;
1367 }
1368}
1369
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001370static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001371{
1372 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001373 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001374
Jan Kiszka76e30d02009-07-02 00:19:02 +02001375 res = qemu_boot_set(bootdevice);
1376 if (res == 0) {
1377 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1378 } else if (res > 0) {
1379 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001380 } else {
aliguori376253e2009-03-05 23:01:23 +00001381 monitor_printf(mon, "no function defined to set boot device list for "
1382 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001383 }
1384}
1385
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001386/**
1387 * do_system_reset(): Issue a machine reset
1388 */
1389static void do_system_reset(Monitor *mon, const QDict *qdict,
1390 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001391{
1392 qemu_system_reset_request();
1393}
1394
Luiz Capitulino43076662009-10-07 13:41:59 -03001395/**
1396 * do_system_powerdown(): Issue a machine powerdown
1397 */
1398static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1399 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001400{
1401 qemu_system_powerdown_request();
1402}
1403
bellardb86bda52004-09-18 19:32:46 +00001404#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001405static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001406{
aliguori376253e2009-03-05 23:01:23 +00001407 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1408 addr,
1409 pte & mask,
1410 pte & PG_GLOBAL_MASK ? 'G' : '-',
1411 pte & PG_PSE_MASK ? 'P' : '-',
1412 pte & PG_DIRTY_MASK ? 'D' : '-',
1413 pte & PG_ACCESSED_MASK ? 'A' : '-',
1414 pte & PG_PCD_MASK ? 'C' : '-',
1415 pte & PG_PWT_MASK ? 'T' : '-',
1416 pte & PG_USER_MASK ? 'U' : '-',
1417 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001418}
1419
aliguori376253e2009-03-05 23:01:23 +00001420static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001421{
bellard6a00d602005-11-21 23:25:50 +00001422 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001423 int l1, l2;
1424 uint32_t pgd, pde, pte;
1425
bellard6a00d602005-11-21 23:25:50 +00001426 env = mon_get_cpu();
1427 if (!env)
1428 return;
1429
bellardb86bda52004-09-18 19:32:46 +00001430 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001431 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001432 return;
1433 }
1434 pgd = env->cr[3] & ~0xfff;
1435 for(l1 = 0; l1 < 1024; l1++) {
1436 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1437 pde = le32_to_cpu(pde);
1438 if (pde & PG_PRESENT_MASK) {
1439 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001440 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001441 } else {
1442 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001443 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001444 (uint8_t *)&pte, 4);
1445 pte = le32_to_cpu(pte);
1446 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001447 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001448 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001449 ~0xfff);
1450 }
1451 }
1452 }
1453 }
1454 }
1455}
1456
aliguori376253e2009-03-05 23:01:23 +00001457static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001458 uint32_t end, int prot)
1459{
bellard9746b152004-11-11 18:30:24 +00001460 int prot1;
1461 prot1 = *plast_prot;
1462 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001463 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001464 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1465 *pstart, end, end - *pstart,
1466 prot1 & PG_USER_MASK ? 'u' : '-',
1467 'r',
1468 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001469 }
1470 if (prot != 0)
1471 *pstart = end;
1472 else
1473 *pstart = -1;
1474 *plast_prot = prot;
1475 }
1476}
1477
aliguori376253e2009-03-05 23:01:23 +00001478static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001479{
bellard6a00d602005-11-21 23:25:50 +00001480 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001481 int l1, l2, prot, last_prot;
1482 uint32_t pgd, pde, pte, start, end;
1483
bellard6a00d602005-11-21 23:25:50 +00001484 env = mon_get_cpu();
1485 if (!env)
1486 return;
1487
bellardb86bda52004-09-18 19:32:46 +00001488 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001489 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001490 return;
1491 }
1492 pgd = env->cr[3] & ~0xfff;
1493 last_prot = 0;
1494 start = -1;
1495 for(l1 = 0; l1 < 1024; l1++) {
1496 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1497 pde = le32_to_cpu(pde);
1498 end = l1 << 22;
1499 if (pde & PG_PRESENT_MASK) {
1500 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1501 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001502 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001503 } else {
1504 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001505 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001506 (uint8_t *)&pte, 4);
1507 pte = le32_to_cpu(pte);
1508 end = (l1 << 22) + (l2 << 12);
1509 if (pte & PG_PRESENT_MASK) {
1510 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1511 } else {
1512 prot = 0;
1513 }
aliguori376253e2009-03-05 23:01:23 +00001514 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001515 }
1516 }
1517 } else {
1518 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001519 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001520 }
1521 }
1522}
1523#endif
1524
aurel327c664e22009-03-03 06:12:22 +00001525#if defined(TARGET_SH4)
1526
aliguori376253e2009-03-05 23:01:23 +00001527static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001528{
aliguori376253e2009-03-05 23:01:23 +00001529 monitor_printf(mon, " tlb%i:\t"
1530 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1531 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1532 "dirty=%hhu writethrough=%hhu\n",
1533 idx,
1534 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1535 tlb->v, tlb->sh, tlb->c, tlb->pr,
1536 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001537}
1538
aliguori376253e2009-03-05 23:01:23 +00001539static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001540{
1541 CPUState *env = mon_get_cpu();
1542 int i;
1543
aliguori376253e2009-03-05 23:01:23 +00001544 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001545 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001546 print_tlb (mon, i, &env->itlb[i]);
1547 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001548 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001549 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001550}
1551
1552#endif
1553
aliguori376253e2009-03-05 23:01:23 +00001554static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001555{
1556#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001557 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001558 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001559 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001560 else
aliguori376253e2009-03-05 23:01:23 +00001561 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001562#else
aliguori376253e2009-03-05 23:01:23 +00001563 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001564#endif
1565}
1566
aliguori030ea372009-04-21 22:30:47 +00001567static void do_info_numa(Monitor *mon)
1568{
aliguorib28b6232009-04-22 20:20:29 +00001569 int i;
aliguori030ea372009-04-21 22:30:47 +00001570 CPUState *env;
1571
1572 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1573 for (i = 0; i < nb_numa_nodes; i++) {
1574 monitor_printf(mon, "node %d cpus:", i);
1575 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1576 if (env->numa_node == i) {
1577 monitor_printf(mon, " %d", env->cpu_index);
1578 }
1579 }
1580 monitor_printf(mon, "\n");
1581 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1582 node_mem[i] >> 20);
1583 }
1584}
1585
bellard5f1ce942006-02-08 22:40:15 +00001586#ifdef CONFIG_PROFILER
1587
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001588int64_t qemu_time;
1589int64_t dev_time;
1590
aliguori376253e2009-03-05 23:01:23 +00001591static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001592{
1593 int64_t total;
1594 total = qemu_time;
1595 if (total == 0)
1596 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001597 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001598 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001599 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001600 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001601 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001602 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001603}
1604#else
aliguori376253e2009-03-05 23:01:23 +00001605static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001606{
aliguori376253e2009-03-05 23:01:23 +00001607 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001608}
1609#endif
1610
bellardec36b692006-07-16 18:57:03 +00001611/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001612static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001613
aliguori376253e2009-03-05 23:01:23 +00001614static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001615{
1616 int i;
1617 CaptureState *s;
1618
1619 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001620 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001621 s->ops.info (s->opaque);
1622 }
1623}
1624
Blue Swirl23130862009-06-06 08:22:04 +00001625#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001626static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001627{
1628 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001629 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001630 CaptureState *s;
1631
1632 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1633 if (i == n) {
1634 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001635 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00001636 qemu_free (s);
1637 return;
1638 }
1639 }
1640}
1641
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001642static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001643{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001644 const char *path = qdict_get_str(qdict, "path");
1645 int has_freq = qdict_haskey(qdict, "freq");
1646 int freq = qdict_get_try_int(qdict, "freq", -1);
1647 int has_bits = qdict_haskey(qdict, "bits");
1648 int bits = qdict_get_try_int(qdict, "bits", -1);
1649 int has_channels = qdict_haskey(qdict, "nchannels");
1650 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001651 CaptureState *s;
1652
1653 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001654
1655 freq = has_freq ? freq : 44100;
1656 bits = has_bits ? bits : 16;
1657 nchannels = has_channels ? nchannels : 2;
1658
1659 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001660 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001661 qemu_free (s);
1662 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001663 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001664}
1665#endif
1666
aurel32dc1c0b72008-04-27 23:52:12 +00001667#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001668static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001669{
1670 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001671 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001672
1673 for (env = first_cpu; env != NULL; env = env->next_cpu)
1674 if (env->cpu_index == cpu_index) {
1675 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1676 break;
1677 }
1678}
1679#endif
1680
aliguori376253e2009-03-05 23:01:23 +00001681static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001682{
aurel321b530a62009-04-05 20:08:59 +00001683 if (vm_running) {
1684 if (singlestep) {
1685 monitor_printf(mon, "VM status: running (single step mode)\n");
1686 } else {
1687 monitor_printf(mon, "VM status: running\n");
1688 }
1689 } else
aliguori376253e2009-03-05 23:01:23 +00001690 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001691}
1692
Luiz Capitulino83fb1de2009-10-07 13:42:01 -03001693/**
1694 * do_balloon(): Request VM to change its memory allocation
1695 */
1696static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001697{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001698 int value = qdict_get_int(qdict, "value");
Anthony Liguoric227f092009-10-01 16:12:16 -05001699 ram_addr_t target = value;
aliguoridf751fa2008-12-04 20:19:35 +00001700 qemu_balloon(target << 20);
1701}
1702
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001703static void monitor_print_balloon(Monitor *mon, const QObject *data)
1704{
1705 monitor_printf(mon, "balloon: actual=%d\n",
1706 (int)qint_get_int(qobject_to_qint(data)));
1707}
1708
1709/**
1710 * do_info_balloon(): Balloon information
1711 */
1712static void do_info_balloon(Monitor *mon, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001713{
Anthony Liguoric227f092009-10-01 16:12:16 -05001714 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00001715
1716 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001717 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001718 monitor_printf(mon, "Using KVM without synchronous MMU, "
1719 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001720 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001721 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001722 else
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001723 *ret_data = QOBJECT(qint_from_int((int)(actual >> 20)));
aliguoridf751fa2008-12-04 20:19:35 +00001724}
1725
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001726static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001727{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001728 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001729
aliguori76655d62009-03-06 20:27:37 +00001730 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001731 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001732 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001733 return acl;
1734}
aliguori76655d62009-03-06 20:27:37 +00001735
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001736static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001737{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001738 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001739 qemu_acl *acl = find_acl(mon, aclname);
1740 qemu_acl_entry *entry;
1741 int i = 0;
1742
1743 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001744 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001745 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001746 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001747 i++;
1748 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001749 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001750 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001751 }
1752}
1753
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001754static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001755{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001756 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001757 qemu_acl *acl = find_acl(mon, aclname);
1758
1759 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001760 qemu_acl_reset(acl);
1761 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001762 }
1763}
aliguori76655d62009-03-06 20:27:37 +00001764
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001765static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001766{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001767 const char *aclname = qdict_get_str(qdict, "aclname");
1768 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001769 qemu_acl *acl = find_acl(mon, aclname);
1770
1771 if (acl) {
1772 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001773 acl->defaultDeny = 0;
1774 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001775 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001776 acl->defaultDeny = 1;
1777 monitor_printf(mon, "acl: policy set to 'deny'\n");
1778 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001779 monitor_printf(mon, "acl: unknown policy '%s', "
1780 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001781 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001782 }
1783}
aliguori76655d62009-03-06 20:27:37 +00001784
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001785static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001786{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001787 const char *aclname = qdict_get_str(qdict, "aclname");
1788 const char *match = qdict_get_str(qdict, "match");
1789 const char *policy = qdict_get_str(qdict, "policy");
1790 int has_index = qdict_haskey(qdict, "index");
1791 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001792 qemu_acl *acl = find_acl(mon, aclname);
1793 int deny, ret;
1794
1795 if (acl) {
1796 if (strcmp(policy, "allow") == 0) {
1797 deny = 0;
1798 } else if (strcmp(policy, "deny") == 0) {
1799 deny = 1;
1800 } else {
1801 monitor_printf(mon, "acl: unknown policy '%s', "
1802 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001803 return;
1804 }
aliguori28a76be2009-03-06 20:27:40 +00001805 if (has_index)
1806 ret = qemu_acl_insert(acl, deny, match, index);
1807 else
1808 ret = qemu_acl_append(acl, deny, match);
1809 if (ret < 0)
1810 monitor_printf(mon, "acl: unable to add acl entry\n");
1811 else
1812 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001813 }
1814}
aliguori76655d62009-03-06 20:27:37 +00001815
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001816static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001817{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001818 const char *aclname = qdict_get_str(qdict, "aclname");
1819 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001820 qemu_acl *acl = find_acl(mon, aclname);
1821 int ret;
aliguori76655d62009-03-06 20:27:37 +00001822
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001823 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001824 ret = qemu_acl_remove(acl, match);
1825 if (ret < 0)
1826 monitor_printf(mon, "acl: no matching acl entry\n");
1827 else
1828 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001829 }
1830}
1831
Huang Ying79c4f6b2009-06-23 10:05:14 +08001832#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001833static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001834{
1835 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001836 int cpu_index = qdict_get_int(qdict, "cpu_index");
1837 int bank = qdict_get_int(qdict, "bank");
1838 uint64_t status = qdict_get_int(qdict, "status");
1839 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1840 uint64_t addr = qdict_get_int(qdict, "addr");
1841 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08001842
1843 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1844 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1845 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1846 break;
1847 }
1848}
1849#endif
1850
Luiz Capitulinof0d60002009-10-16 12:23:50 -03001851static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001852{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001853 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001854 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001855 int fd;
1856
1857 fd = qemu_chr_get_msgfd(mon->chr);
1858 if (fd == -1) {
1859 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1860 return;
1861 }
1862
1863 if (qemu_isdigit(fdname[0])) {
1864 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1865 return;
1866 }
1867
1868 fd = dup(fd);
1869 if (fd == -1) {
1870 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1871 strerror(errno));
1872 return;
1873 }
1874
Blue Swirl72cf2d42009-09-12 07:36:22 +00001875 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001876 if (strcmp(monfd->name, fdname) != 0) {
1877 continue;
1878 }
1879
1880 close(monfd->fd);
1881 monfd->fd = fd;
1882 return;
1883 }
1884
Anthony Liguoric227f092009-10-01 16:12:16 -05001885 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001886 monfd->name = qemu_strdup(fdname);
1887 monfd->fd = fd;
1888
Blue Swirl72cf2d42009-09-12 07:36:22 +00001889 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001890}
1891
Luiz Capitulino18f3a512009-10-16 12:23:51 -03001892static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001893{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001894 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001895 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001896
Blue Swirl72cf2d42009-09-12 07:36:22 +00001897 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001898 if (strcmp(monfd->name, fdname) != 0) {
1899 continue;
1900 }
1901
Blue Swirl72cf2d42009-09-12 07:36:22 +00001902 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001903 close(monfd->fd);
1904 qemu_free(monfd->name);
1905 qemu_free(monfd);
1906 return;
1907 }
1908
1909 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1910 fdname);
1911}
1912
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001913static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001914{
1915 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001916 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001917
1918 vm_stop(0);
1919
Juan Quintela05f24012009-08-20 19:42:22 +02001920 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001921 vm_start();
1922}
1923
Mark McLoughlin7768e042009-07-22 09:11:41 +01001924int monitor_get_fd(Monitor *mon, const char *fdname)
1925{
Anthony Liguoric227f092009-10-01 16:12:16 -05001926 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001927
Blue Swirl72cf2d42009-09-12 07:36:22 +00001928 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001929 int fd;
1930
1931 if (strcmp(monfd->name, fdname) != 0) {
1932 continue;
1933 }
1934
1935 fd = monfd->fd;
1936
1937 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001938 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001939 qemu_free(monfd->name);
1940 qemu_free(monfd);
1941
1942 return fd;
1943 }
1944
1945 return -1;
1946}
1947
Anthony Liguoric227f092009-10-01 16:12:16 -05001948static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001949#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001950 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001951};
1952
Blue Swirl23130862009-06-06 08:22:04 +00001953/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05001954static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001955 {
1956 .name = "version",
1957 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001958 .params = "",
1959 .help = "show the version of QEMU",
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03001960 .user_print = monitor_print_qobject,
1961 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001962 },
1963 {
1964 .name = "network",
1965 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001966 .params = "",
1967 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03001968 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001969 },
1970 {
1971 .name = "chardev",
1972 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001973 .params = "",
1974 .help = "show the character devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001975 .mhandler.info = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001976 },
1977 {
1978 .name = "block",
1979 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001980 .params = "",
1981 .help = "show the block devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001982 .mhandler.info = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001983 },
1984 {
1985 .name = "blockstats",
1986 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001987 .params = "",
1988 .help = "show block device statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03001989 .mhandler.info = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001990 },
1991 {
1992 .name = "registers",
1993 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001994 .params = "",
1995 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03001996 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001997 },
1998 {
1999 .name = "cpus",
2000 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002001 .params = "",
2002 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002003 .user_print = monitor_print_cpus,
2004 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002005 },
2006 {
2007 .name = "history",
2008 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002009 .params = "",
2010 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002011 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002012 },
2013 {
2014 .name = "irq",
2015 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002016 .params = "",
2017 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002018 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002019 },
2020 {
2021 .name = "pic",
2022 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002023 .params = "",
2024 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002025 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002026 },
2027 {
2028 .name = "pci",
2029 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002030 .params = "",
2031 .help = "show PCI info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002032 .mhandler.info = pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002033 },
aurel327c664e22009-03-03 06:12:22 +00002034#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002035 {
2036 .name = "tlb",
2037 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002038 .params = "",
2039 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002040 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002041 },
aurel327c664e22009-03-03 06:12:22 +00002042#endif
2043#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002044 {
2045 .name = "mem",
2046 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002047 .params = "",
2048 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002049 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002050 },
2051 {
2052 .name = "hpet",
2053 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002054 .params = "",
2055 .help = "show state of HPET",
Luiz Capitulino910df892009-10-07 13:41:51 -03002056 .mhandler.info = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002057 },
bellardb86bda52004-09-18 19:32:46 +00002058#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002059 {
2060 .name = "jit",
2061 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002062 .params = "",
2063 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002064 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002065 },
2066 {
2067 .name = "kvm",
2068 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002069 .params = "",
2070 .help = "show KVM information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002071 .mhandler.info = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002072 },
2073 {
2074 .name = "numa",
2075 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002076 .params = "",
2077 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002078 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002079 },
2080 {
2081 .name = "usb",
2082 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002083 .params = "",
2084 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002085 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002086 },
2087 {
2088 .name = "usbhost",
2089 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002090 .params = "",
2091 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002092 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002093 },
2094 {
2095 .name = "profile",
2096 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002097 .params = "",
2098 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002099 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002100 },
2101 {
2102 .name = "capture",
2103 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002104 .params = "",
2105 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002106 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002107 },
2108 {
2109 .name = "snapshots",
2110 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002111 .params = "",
2112 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002113 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002114 },
2115 {
2116 .name = "status",
2117 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002118 .params = "",
2119 .help = "show the current VM status (running|paused)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002120 .mhandler.info = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002121 },
2122 {
2123 .name = "pcmcia",
2124 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002125 .params = "",
2126 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002127 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002128 },
2129 {
2130 .name = "mice",
2131 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002132 .params = "",
2133 .help = "show which guest mouse is receiving events",
Luiz Capitulino910df892009-10-07 13:41:51 -03002134 .mhandler.info = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002135 },
2136 {
2137 .name = "vnc",
2138 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002139 .params = "",
2140 .help = "show the vnc server status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002141 .mhandler.info = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002142 },
2143 {
2144 .name = "name",
2145 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002146 .params = "",
2147 .help = "show the current VM name",
Luiz Capitulino910df892009-10-07 13:41:51 -03002148 .mhandler.info = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002149 },
2150 {
2151 .name = "uuid",
2152 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002153 .params = "",
2154 .help = "show the current VM UUID",
Luiz Capitulino910df892009-10-07 13:41:51 -03002155 .mhandler.info = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002156 },
j_mayer76a66252007-03-07 08:32:30 +00002157#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002158 {
2159 .name = "cpustats",
2160 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002161 .params = "",
2162 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002163 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002164 },
j_mayer76a66252007-03-07 08:32:30 +00002165#endif
blueswir131a60e22007-10-26 18:42:59 +00002166#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002167 {
2168 .name = "usernet",
2169 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002170 .params = "",
2171 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002172 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002173 },
blueswir131a60e22007-10-26 18:42:59 +00002174#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002175 {
2176 .name = "migrate",
2177 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002178 .params = "",
2179 .help = "show migration status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002180 .mhandler.info = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002181 },
2182 {
2183 .name = "balloon",
2184 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002185 .params = "",
2186 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002187 .user_print = monitor_print_balloon,
2188 .mhandler.info_new = do_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002189 },
2190 {
2191 .name = "qtree",
2192 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002193 .params = "",
2194 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002195 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002196 },
2197 {
2198 .name = "qdm",
2199 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002200 .params = "",
2201 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002202 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002203 },
2204 {
2205 .name = "roms",
2206 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002207 .params = "",
2208 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002209 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002210 },
2211 {
2212 .name = NULL,
2213 },
bellard9dc39cb2004-03-14 21:38:27 +00002214};
2215
bellard9307c4c2004-04-04 12:57:25 +00002216/*******************************************************************/
2217
2218static const char *pch;
2219static jmp_buf expr_env;
2220
bellard92a31b12005-02-10 22:00:52 +00002221#define MD_TLONG 0
2222#define MD_I32 1
2223
bellard9307c4c2004-04-04 12:57:25 +00002224typedef struct MonitorDef {
2225 const char *name;
2226 int offset;
blueswir18662d652008-10-02 18:32:44 +00002227 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002228 int type;
bellard9307c4c2004-04-04 12:57:25 +00002229} MonitorDef;
2230
bellard57206fd2004-04-25 18:54:52 +00002231#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002232static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002233{
bellard6a00d602005-11-21 23:25:50 +00002234 CPUState *env = mon_get_cpu();
2235 if (!env)
2236 return 0;
2237 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002238}
2239#endif
2240
bellarda541f292004-04-12 20:39:29 +00002241#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002242static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002243{
bellard6a00d602005-11-21 23:25:50 +00002244 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002245 unsigned int u;
2246 int i;
2247
bellard6a00d602005-11-21 23:25:50 +00002248 if (!env)
2249 return 0;
2250
bellarda541f292004-04-12 20:39:29 +00002251 u = 0;
2252 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002253 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002254
2255 return u;
2256}
2257
blueswir18662d652008-10-02 18:32:44 +00002258static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002259{
bellard6a00d602005-11-21 23:25:50 +00002260 CPUState *env = mon_get_cpu();
2261 if (!env)
2262 return 0;
j_mayer0411a972007-10-25 21:35:50 +00002263 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002264}
2265
blueswir18662d652008-10-02 18:32:44 +00002266static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002267{
bellard6a00d602005-11-21 23:25:50 +00002268 CPUState *env = mon_get_cpu();
2269 if (!env)
2270 return 0;
aurel323d7b4172008-10-21 11:28:46 +00002271 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002272}
bellard9fddaa02004-05-21 12:59:32 +00002273
blueswir18662d652008-10-02 18:32:44 +00002274static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002275{
bellard6a00d602005-11-21 23:25:50 +00002276 CPUState *env = mon_get_cpu();
2277 if (!env)
2278 return 0;
2279 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002280}
2281
blueswir18662d652008-10-02 18:32:44 +00002282static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002283{
bellard6a00d602005-11-21 23:25:50 +00002284 CPUState *env = mon_get_cpu();
2285 if (!env)
2286 return 0;
2287 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002288}
2289
blueswir18662d652008-10-02 18:32:44 +00002290static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002291{
bellard6a00d602005-11-21 23:25:50 +00002292 CPUState *env = mon_get_cpu();
2293 if (!env)
2294 return 0;
2295 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002296}
bellarda541f292004-04-12 20:39:29 +00002297#endif
2298
bellarde95c8d52004-09-30 22:22:08 +00002299#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002300#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002301static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002302{
bellard6a00d602005-11-21 23:25:50 +00002303 CPUState *env = mon_get_cpu();
2304 if (!env)
2305 return 0;
2306 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002307}
bellard7b936c02005-10-30 17:05:13 +00002308#endif
bellarde95c8d52004-09-30 22:22:08 +00002309
blueswir18662d652008-10-02 18:32:44 +00002310static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002311{
bellard6a00d602005-11-21 23:25:50 +00002312 CPUState *env = mon_get_cpu();
2313 if (!env)
2314 return 0;
2315 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002316}
2317#endif
2318
blueswir18662d652008-10-02 18:32:44 +00002319static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002320#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002321
2322#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002323 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002324 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002325 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002326
bellard9307c4c2004-04-04 12:57:25 +00002327 { "eax", offsetof(CPUState, regs[0]) },
2328 { "ecx", offsetof(CPUState, regs[1]) },
2329 { "edx", offsetof(CPUState, regs[2]) },
2330 { "ebx", offsetof(CPUState, regs[3]) },
2331 { "esp|sp", offsetof(CPUState, regs[4]) },
2332 { "ebp|fp", offsetof(CPUState, regs[5]) },
2333 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002334 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002335#ifdef TARGET_X86_64
2336 { "r8", offsetof(CPUState, regs[8]) },
2337 { "r9", offsetof(CPUState, regs[9]) },
2338 { "r10", offsetof(CPUState, regs[10]) },
2339 { "r11", offsetof(CPUState, regs[11]) },
2340 { "r12", offsetof(CPUState, regs[12]) },
2341 { "r13", offsetof(CPUState, regs[13]) },
2342 { "r14", offsetof(CPUState, regs[14]) },
2343 { "r15", offsetof(CPUState, regs[15]) },
2344#endif
bellard9307c4c2004-04-04 12:57:25 +00002345 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002346 { "eip", offsetof(CPUState, eip) },
2347 SEG("cs", R_CS)
2348 SEG("ds", R_DS)
2349 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002350 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002351 SEG("fs", R_FS)
2352 SEG("gs", R_GS)
2353 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002354#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002355 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002356 { "r0", offsetof(CPUState, gpr[0]) },
2357 { "r1", offsetof(CPUState, gpr[1]) },
2358 { "r2", offsetof(CPUState, gpr[2]) },
2359 { "r3", offsetof(CPUState, gpr[3]) },
2360 { "r4", offsetof(CPUState, gpr[4]) },
2361 { "r5", offsetof(CPUState, gpr[5]) },
2362 { "r6", offsetof(CPUState, gpr[6]) },
2363 { "r7", offsetof(CPUState, gpr[7]) },
2364 { "r8", offsetof(CPUState, gpr[8]) },
2365 { "r9", offsetof(CPUState, gpr[9]) },
2366 { "r10", offsetof(CPUState, gpr[10]) },
2367 { "r11", offsetof(CPUState, gpr[11]) },
2368 { "r12", offsetof(CPUState, gpr[12]) },
2369 { "r13", offsetof(CPUState, gpr[13]) },
2370 { "r14", offsetof(CPUState, gpr[14]) },
2371 { "r15", offsetof(CPUState, gpr[15]) },
2372 { "r16", offsetof(CPUState, gpr[16]) },
2373 { "r17", offsetof(CPUState, gpr[17]) },
2374 { "r18", offsetof(CPUState, gpr[18]) },
2375 { "r19", offsetof(CPUState, gpr[19]) },
2376 { "r20", offsetof(CPUState, gpr[20]) },
2377 { "r21", offsetof(CPUState, gpr[21]) },
2378 { "r22", offsetof(CPUState, gpr[22]) },
2379 { "r23", offsetof(CPUState, gpr[23]) },
2380 { "r24", offsetof(CPUState, gpr[24]) },
2381 { "r25", offsetof(CPUState, gpr[25]) },
2382 { "r26", offsetof(CPUState, gpr[26]) },
2383 { "r27", offsetof(CPUState, gpr[27]) },
2384 { "r28", offsetof(CPUState, gpr[28]) },
2385 { "r29", offsetof(CPUState, gpr[29]) },
2386 { "r30", offsetof(CPUState, gpr[30]) },
2387 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002388 /* Floating point registers */
2389 { "f0", offsetof(CPUState, fpr[0]) },
2390 { "f1", offsetof(CPUState, fpr[1]) },
2391 { "f2", offsetof(CPUState, fpr[2]) },
2392 { "f3", offsetof(CPUState, fpr[3]) },
2393 { "f4", offsetof(CPUState, fpr[4]) },
2394 { "f5", offsetof(CPUState, fpr[5]) },
2395 { "f6", offsetof(CPUState, fpr[6]) },
2396 { "f7", offsetof(CPUState, fpr[7]) },
2397 { "f8", offsetof(CPUState, fpr[8]) },
2398 { "f9", offsetof(CPUState, fpr[9]) },
2399 { "f10", offsetof(CPUState, fpr[10]) },
2400 { "f11", offsetof(CPUState, fpr[11]) },
2401 { "f12", offsetof(CPUState, fpr[12]) },
2402 { "f13", offsetof(CPUState, fpr[13]) },
2403 { "f14", offsetof(CPUState, fpr[14]) },
2404 { "f15", offsetof(CPUState, fpr[15]) },
2405 { "f16", offsetof(CPUState, fpr[16]) },
2406 { "f17", offsetof(CPUState, fpr[17]) },
2407 { "f18", offsetof(CPUState, fpr[18]) },
2408 { "f19", offsetof(CPUState, fpr[19]) },
2409 { "f20", offsetof(CPUState, fpr[20]) },
2410 { "f21", offsetof(CPUState, fpr[21]) },
2411 { "f22", offsetof(CPUState, fpr[22]) },
2412 { "f23", offsetof(CPUState, fpr[23]) },
2413 { "f24", offsetof(CPUState, fpr[24]) },
2414 { "f25", offsetof(CPUState, fpr[25]) },
2415 { "f26", offsetof(CPUState, fpr[26]) },
2416 { "f27", offsetof(CPUState, fpr[27]) },
2417 { "f28", offsetof(CPUState, fpr[28]) },
2418 { "f29", offsetof(CPUState, fpr[29]) },
2419 { "f30", offsetof(CPUState, fpr[30]) },
2420 { "f31", offsetof(CPUState, fpr[31]) },
2421 { "fpscr", offsetof(CPUState, fpscr) },
2422 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002423 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002424 { "lr", offsetof(CPUState, lr) },
2425 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002426 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002427 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002428 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002429 { "msr", 0, &monitor_get_msr, },
2430 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002431 { "tbu", 0, &monitor_get_tbu, },
2432 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002433#if defined(TARGET_PPC64)
2434 /* Address space register */
2435 { "asr", offsetof(CPUState, asr) },
2436#endif
2437 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002438 { "sdr1", offsetof(CPUState, sdr1) },
2439 { "sr0", offsetof(CPUState, sr[0]) },
2440 { "sr1", offsetof(CPUState, sr[1]) },
2441 { "sr2", offsetof(CPUState, sr[2]) },
2442 { "sr3", offsetof(CPUState, sr[3]) },
2443 { "sr4", offsetof(CPUState, sr[4]) },
2444 { "sr5", offsetof(CPUState, sr[5]) },
2445 { "sr6", offsetof(CPUState, sr[6]) },
2446 { "sr7", offsetof(CPUState, sr[7]) },
2447 { "sr8", offsetof(CPUState, sr[8]) },
2448 { "sr9", offsetof(CPUState, sr[9]) },
2449 { "sr10", offsetof(CPUState, sr[10]) },
2450 { "sr11", offsetof(CPUState, sr[11]) },
2451 { "sr12", offsetof(CPUState, sr[12]) },
2452 { "sr13", offsetof(CPUState, sr[13]) },
2453 { "sr14", offsetof(CPUState, sr[14]) },
2454 { "sr15", offsetof(CPUState, sr[15]) },
2455 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002456#elif defined(TARGET_SPARC)
2457 { "g0", offsetof(CPUState, gregs[0]) },
2458 { "g1", offsetof(CPUState, gregs[1]) },
2459 { "g2", offsetof(CPUState, gregs[2]) },
2460 { "g3", offsetof(CPUState, gregs[3]) },
2461 { "g4", offsetof(CPUState, gregs[4]) },
2462 { "g5", offsetof(CPUState, gregs[5]) },
2463 { "g6", offsetof(CPUState, gregs[6]) },
2464 { "g7", offsetof(CPUState, gregs[7]) },
2465 { "o0", 0, monitor_get_reg },
2466 { "o1", 1, monitor_get_reg },
2467 { "o2", 2, monitor_get_reg },
2468 { "o3", 3, monitor_get_reg },
2469 { "o4", 4, monitor_get_reg },
2470 { "o5", 5, monitor_get_reg },
2471 { "o6", 6, monitor_get_reg },
2472 { "o7", 7, monitor_get_reg },
2473 { "l0", 8, monitor_get_reg },
2474 { "l1", 9, monitor_get_reg },
2475 { "l2", 10, monitor_get_reg },
2476 { "l3", 11, monitor_get_reg },
2477 { "l4", 12, monitor_get_reg },
2478 { "l5", 13, monitor_get_reg },
2479 { "l6", 14, monitor_get_reg },
2480 { "l7", 15, monitor_get_reg },
2481 { "i0", 16, monitor_get_reg },
2482 { "i1", 17, monitor_get_reg },
2483 { "i2", 18, monitor_get_reg },
2484 { "i3", 19, monitor_get_reg },
2485 { "i4", 20, monitor_get_reg },
2486 { "i5", 21, monitor_get_reg },
2487 { "i6", 22, monitor_get_reg },
2488 { "i7", 23, monitor_get_reg },
2489 { "pc", offsetof(CPUState, pc) },
2490 { "npc", offsetof(CPUState, npc) },
2491 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002492#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002493 { "psr", 0, &monitor_get_psr, },
2494 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002495#endif
bellarde95c8d52004-09-30 22:22:08 +00002496 { "tbr", offsetof(CPUState, tbr) },
2497 { "fsr", offsetof(CPUState, fsr) },
2498 { "f0", offsetof(CPUState, fpr[0]) },
2499 { "f1", offsetof(CPUState, fpr[1]) },
2500 { "f2", offsetof(CPUState, fpr[2]) },
2501 { "f3", offsetof(CPUState, fpr[3]) },
2502 { "f4", offsetof(CPUState, fpr[4]) },
2503 { "f5", offsetof(CPUState, fpr[5]) },
2504 { "f6", offsetof(CPUState, fpr[6]) },
2505 { "f7", offsetof(CPUState, fpr[7]) },
2506 { "f8", offsetof(CPUState, fpr[8]) },
2507 { "f9", offsetof(CPUState, fpr[9]) },
2508 { "f10", offsetof(CPUState, fpr[10]) },
2509 { "f11", offsetof(CPUState, fpr[11]) },
2510 { "f12", offsetof(CPUState, fpr[12]) },
2511 { "f13", offsetof(CPUState, fpr[13]) },
2512 { "f14", offsetof(CPUState, fpr[14]) },
2513 { "f15", offsetof(CPUState, fpr[15]) },
2514 { "f16", offsetof(CPUState, fpr[16]) },
2515 { "f17", offsetof(CPUState, fpr[17]) },
2516 { "f18", offsetof(CPUState, fpr[18]) },
2517 { "f19", offsetof(CPUState, fpr[19]) },
2518 { "f20", offsetof(CPUState, fpr[20]) },
2519 { "f21", offsetof(CPUState, fpr[21]) },
2520 { "f22", offsetof(CPUState, fpr[22]) },
2521 { "f23", offsetof(CPUState, fpr[23]) },
2522 { "f24", offsetof(CPUState, fpr[24]) },
2523 { "f25", offsetof(CPUState, fpr[25]) },
2524 { "f26", offsetof(CPUState, fpr[26]) },
2525 { "f27", offsetof(CPUState, fpr[27]) },
2526 { "f28", offsetof(CPUState, fpr[28]) },
2527 { "f29", offsetof(CPUState, fpr[29]) },
2528 { "f30", offsetof(CPUState, fpr[30]) },
2529 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002530#ifdef TARGET_SPARC64
2531 { "f32", offsetof(CPUState, fpr[32]) },
2532 { "f34", offsetof(CPUState, fpr[34]) },
2533 { "f36", offsetof(CPUState, fpr[36]) },
2534 { "f38", offsetof(CPUState, fpr[38]) },
2535 { "f40", offsetof(CPUState, fpr[40]) },
2536 { "f42", offsetof(CPUState, fpr[42]) },
2537 { "f44", offsetof(CPUState, fpr[44]) },
2538 { "f46", offsetof(CPUState, fpr[46]) },
2539 { "f48", offsetof(CPUState, fpr[48]) },
2540 { "f50", offsetof(CPUState, fpr[50]) },
2541 { "f52", offsetof(CPUState, fpr[52]) },
2542 { "f54", offsetof(CPUState, fpr[54]) },
2543 { "f56", offsetof(CPUState, fpr[56]) },
2544 { "f58", offsetof(CPUState, fpr[58]) },
2545 { "f60", offsetof(CPUState, fpr[60]) },
2546 { "f62", offsetof(CPUState, fpr[62]) },
2547 { "asi", offsetof(CPUState, asi) },
2548 { "pstate", offsetof(CPUState, pstate) },
2549 { "cansave", offsetof(CPUState, cansave) },
2550 { "canrestore", offsetof(CPUState, canrestore) },
2551 { "otherwin", offsetof(CPUState, otherwin) },
2552 { "wstate", offsetof(CPUState, wstate) },
2553 { "cleanwin", offsetof(CPUState, cleanwin) },
2554 { "fprs", offsetof(CPUState, fprs) },
2555#endif
bellard9307c4c2004-04-04 12:57:25 +00002556#endif
2557 { NULL },
2558};
2559
aliguori376253e2009-03-05 23:01:23 +00002560static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002561{
aliguori376253e2009-03-05 23:01:23 +00002562 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002563 longjmp(expr_env, 1);
2564}
2565
bellard6a00d602005-11-21 23:25:50 +00002566/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002567static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002568{
blueswir18662d652008-10-02 18:32:44 +00002569 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002570 void *ptr;
2571
bellard9307c4c2004-04-04 12:57:25 +00002572 for(md = monitor_defs; md->name != NULL; md++) {
2573 if (compare_cmd(name, md->name)) {
2574 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002575 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002576 } else {
bellard6a00d602005-11-21 23:25:50 +00002577 CPUState *env = mon_get_cpu();
2578 if (!env)
2579 return -2;
2580 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002581 switch(md->type) {
2582 case MD_I32:
2583 *pval = *(int32_t *)ptr;
2584 break;
2585 case MD_TLONG:
2586 *pval = *(target_long *)ptr;
2587 break;
2588 default:
2589 *pval = 0;
2590 break;
2591 }
bellard9307c4c2004-04-04 12:57:25 +00002592 }
2593 return 0;
2594 }
2595 }
2596 return -1;
2597}
2598
2599static void next(void)
2600{
Blue Swirl660f11b2009-07-31 21:16:51 +00002601 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002602 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002603 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002604 pch++;
2605 }
2606}
2607
aliguori376253e2009-03-05 23:01:23 +00002608static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002609
aliguori376253e2009-03-05 23:01:23 +00002610static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002611{
blueswir1c2efc952007-09-25 17:28:42 +00002612 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002613 char *p;
bellard6a00d602005-11-21 23:25:50 +00002614 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002615
2616 switch(*pch) {
2617 case '+':
2618 next();
aliguori376253e2009-03-05 23:01:23 +00002619 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002620 break;
2621 case '-':
2622 next();
aliguori376253e2009-03-05 23:01:23 +00002623 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002624 break;
2625 case '~':
2626 next();
aliguori376253e2009-03-05 23:01:23 +00002627 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002628 break;
2629 case '(':
2630 next();
aliguori376253e2009-03-05 23:01:23 +00002631 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002632 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002633 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002634 }
2635 next();
2636 break;
bellard81d09122004-07-14 17:21:37 +00002637 case '\'':
2638 pch++;
2639 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002640 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002641 n = *pch;
2642 pch++;
2643 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002644 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002645 next();
2646 break;
bellard9307c4c2004-04-04 12:57:25 +00002647 case '$':
2648 {
2649 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002650 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002651
bellard9307c4c2004-04-04 12:57:25 +00002652 pch++;
2653 q = buf;
2654 while ((*pch >= 'a' && *pch <= 'z') ||
2655 (*pch >= 'A' && *pch <= 'Z') ||
2656 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002657 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002658 if ((q - buf) < sizeof(buf) - 1)
2659 *q++ = *pch;
2660 pch++;
2661 }
blueswir1cd390082008-11-16 13:53:32 +00002662 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002663 pch++;
2664 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002665 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002666 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002667 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002668 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002669 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002670 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002671 }
2672 break;
2673 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002674 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002675 n = 0;
2676 break;
2677 default:
blueswir17743e582007-09-24 18:39:04 +00002678#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002679 n = strtoull(pch, &p, 0);
2680#else
bellard9307c4c2004-04-04 12:57:25 +00002681 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002682#endif
bellard9307c4c2004-04-04 12:57:25 +00002683 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002684 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002685 }
2686 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002687 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002688 pch++;
2689 break;
2690 }
2691 return n;
2692}
2693
2694
aliguori376253e2009-03-05 23:01:23 +00002695static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002696{
blueswir1c2efc952007-09-25 17:28:42 +00002697 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002698 int op;
ths3b46e622007-09-17 08:09:54 +00002699
aliguori376253e2009-03-05 23:01:23 +00002700 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002701 for(;;) {
2702 op = *pch;
2703 if (op != '*' && op != '/' && op != '%')
2704 break;
2705 next();
aliguori376253e2009-03-05 23:01:23 +00002706 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002707 switch(op) {
2708 default:
2709 case '*':
2710 val *= val2;
2711 break;
2712 case '/':
2713 case '%':
ths5fafdf22007-09-16 21:08:06 +00002714 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002715 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002716 if (op == '/')
2717 val /= val2;
2718 else
2719 val %= val2;
2720 break;
2721 }
2722 }
2723 return val;
2724}
2725
aliguori376253e2009-03-05 23:01:23 +00002726static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002727{
blueswir1c2efc952007-09-25 17:28:42 +00002728 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002729 int op;
bellard9307c4c2004-04-04 12:57:25 +00002730
aliguori376253e2009-03-05 23:01:23 +00002731 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002732 for(;;) {
2733 op = *pch;
2734 if (op != '&' && op != '|' && op != '^')
2735 break;
2736 next();
aliguori376253e2009-03-05 23:01:23 +00002737 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002738 switch(op) {
2739 default:
2740 case '&':
2741 val &= val2;
2742 break;
2743 case '|':
2744 val |= val2;
2745 break;
2746 case '^':
2747 val ^= val2;
2748 break;
2749 }
2750 }
2751 return val;
2752}
2753
aliguori376253e2009-03-05 23:01:23 +00002754static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002755{
blueswir1c2efc952007-09-25 17:28:42 +00002756 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002757 int op;
bellard9307c4c2004-04-04 12:57:25 +00002758
aliguori376253e2009-03-05 23:01:23 +00002759 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002760 for(;;) {
2761 op = *pch;
2762 if (op != '+' && op != '-')
2763 break;
2764 next();
aliguori376253e2009-03-05 23:01:23 +00002765 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002766 if (op == '+')
2767 val += val2;
2768 else
2769 val -= val2;
2770 }
2771 return val;
2772}
2773
aliguori376253e2009-03-05 23:01:23 +00002774static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002775{
2776 pch = *pp;
2777 if (setjmp(expr_env)) {
2778 *pp = pch;
2779 return -1;
2780 }
blueswir1cd390082008-11-16 13:53:32 +00002781 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002782 pch++;
aliguori376253e2009-03-05 23:01:23 +00002783 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002784 *pp = pch;
2785 return 0;
2786}
2787
2788static int get_str(char *buf, int buf_size, const char **pp)
2789{
2790 const char *p;
2791 char *q;
2792 int c;
2793
bellard81d09122004-07-14 17:21:37 +00002794 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002795 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002796 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002797 p++;
2798 if (*p == '\0') {
2799 fail:
bellard81d09122004-07-14 17:21:37 +00002800 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002801 *pp = p;
2802 return -1;
2803 }
bellard9307c4c2004-04-04 12:57:25 +00002804 if (*p == '\"') {
2805 p++;
2806 while (*p != '\0' && *p != '\"') {
2807 if (*p == '\\') {
2808 p++;
2809 c = *p++;
2810 switch(c) {
2811 case 'n':
2812 c = '\n';
2813 break;
2814 case 'r':
2815 c = '\r';
2816 break;
2817 case '\\':
2818 case '\'':
2819 case '\"':
2820 break;
2821 default:
2822 qemu_printf("unsupported escape code: '\\%c'\n", c);
2823 goto fail;
2824 }
2825 if ((q - buf) < buf_size - 1) {
2826 *q++ = c;
2827 }
2828 } else {
2829 if ((q - buf) < buf_size - 1) {
2830 *q++ = *p;
2831 }
2832 p++;
2833 }
2834 }
2835 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002836 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002837 goto fail;
2838 }
2839 p++;
2840 } else {
blueswir1cd390082008-11-16 13:53:32 +00002841 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002842 if ((q - buf) < buf_size - 1) {
2843 *q++ = *p;
2844 }
2845 p++;
2846 }
bellard9307c4c2004-04-04 12:57:25 +00002847 }
bellard81d09122004-07-14 17:21:37 +00002848 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002849 *pp = p;
2850 return 0;
2851}
2852
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002853/*
2854 * Store the command-name in cmdname, and return a pointer to
2855 * the remaining of the command string.
2856 */
2857static const char *get_command_name(const char *cmdline,
2858 char *cmdname, size_t nlen)
2859{
2860 size_t len;
2861 const char *p, *pstart;
2862
2863 p = cmdline;
2864 while (qemu_isspace(*p))
2865 p++;
2866 if (*p == '\0')
2867 return NULL;
2868 pstart = p;
2869 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2870 p++;
2871 len = p - pstart;
2872 if (len > nlen - 1)
2873 len = nlen - 1;
2874 memcpy(cmdname, pstart, len);
2875 cmdname[len] = '\0';
2876 return p;
2877}
2878
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002879/**
2880 * Read key of 'type' into 'key' and return the current
2881 * 'type' pointer.
2882 */
2883static char *key_get_info(const char *type, char **key)
2884{
2885 size_t len;
2886 char *p, *str;
2887
2888 if (*type == ',')
2889 type++;
2890
2891 p = strchr(type, ':');
2892 if (!p) {
2893 *key = NULL;
2894 return NULL;
2895 }
2896 len = p - type;
2897
2898 str = qemu_malloc(len + 1);
2899 memcpy(str, type, len);
2900 str[len] = '\0';
2901
2902 *key = str;
2903 return ++p;
2904}
2905
bellard9307c4c2004-04-04 12:57:25 +00002906static int default_fmt_format = 'x';
2907static int default_fmt_size = 4;
2908
2909#define MAX_ARGS 16
2910
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002911static int is_valid_option(const char *c, const char *typestr)
2912{
2913 char option[3];
2914
2915 option[0] = '-';
2916 option[1] = *c;
2917 option[2] = '\0';
2918
2919 typestr = strstr(typestr, option);
2920 return (typestr != NULL);
2921}
2922
Anthony Liguoric227f092009-10-01 16:12:16 -05002923static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002924 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002925 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00002926{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002927 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03002928 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05002929 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002930 char cmdname[256];
2931 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002932 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00002933
2934#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002935 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002936#endif
ths3b46e622007-09-17 08:09:54 +00002937
bellard9307c4c2004-04-04 12:57:25 +00002938 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002939 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2940 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002941 return NULL;
ths3b46e622007-09-17 08:09:54 +00002942
bellard9307c4c2004-04-04 12:57:25 +00002943 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002944 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002945 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002946 break;
bellard9dc39cb2004-03-14 21:38:27 +00002947 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002948
2949 if (cmd->name == NULL) {
2950 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002951 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002952 }
bellard9307c4c2004-04-04 12:57:25 +00002953
bellard9307c4c2004-04-04 12:57:25 +00002954 /* parse the parameters */
2955 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002956 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002957 typestr = key_get_info(typestr, &key);
2958 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002959 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002960 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002961 typestr++;
2962 switch(c) {
2963 case 'F':
bellard81d09122004-07-14 17:21:37 +00002964 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002965 case 's':
2966 {
2967 int ret;
ths3b46e622007-09-17 08:09:54 +00002968
blueswir1cd390082008-11-16 13:53:32 +00002969 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002970 p++;
2971 if (*typestr == '?') {
2972 typestr++;
2973 if (*p == '\0') {
2974 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002975 break;
bellard9307c4c2004-04-04 12:57:25 +00002976 }
2977 }
2978 ret = get_str(buf, sizeof(buf), &p);
2979 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002980 switch(c) {
2981 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002982 monitor_printf(mon, "%s: filename expected\n",
2983 cmdname);
bellard81d09122004-07-14 17:21:37 +00002984 break;
2985 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002986 monitor_printf(mon, "%s: block device name expected\n",
2987 cmdname);
bellard81d09122004-07-14 17:21:37 +00002988 break;
2989 default:
aliguori376253e2009-03-05 23:01:23 +00002990 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002991 break;
2992 }
bellard9307c4c2004-04-04 12:57:25 +00002993 goto fail;
2994 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002995 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002996 }
2997 break;
2998 case '/':
2999 {
3000 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003001
blueswir1cd390082008-11-16 13:53:32 +00003002 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003003 p++;
3004 if (*p == '/') {
3005 /* format found */
3006 p++;
3007 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003008 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003009 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003010 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003011 count = count * 10 + (*p - '0');
3012 p++;
3013 }
3014 }
3015 size = -1;
3016 format = -1;
3017 for(;;) {
3018 switch(*p) {
3019 case 'o':
3020 case 'd':
3021 case 'u':
3022 case 'x':
3023 case 'i':
3024 case 'c':
3025 format = *p++;
3026 break;
3027 case 'b':
3028 size = 1;
3029 p++;
3030 break;
3031 case 'h':
3032 size = 2;
3033 p++;
3034 break;
3035 case 'w':
3036 size = 4;
3037 p++;
3038 break;
3039 case 'g':
3040 case 'L':
3041 size = 8;
3042 p++;
3043 break;
3044 default:
3045 goto next;
3046 }
3047 }
3048 next:
blueswir1cd390082008-11-16 13:53:32 +00003049 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003050 monitor_printf(mon, "invalid char in format: '%c'\n",
3051 *p);
bellard9307c4c2004-04-04 12:57:25 +00003052 goto fail;
3053 }
bellard9307c4c2004-04-04 12:57:25 +00003054 if (format < 0)
3055 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003056 if (format != 'i') {
3057 /* for 'i', not specifying a size gives -1 as size */
3058 if (size < 0)
3059 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003060 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003061 }
bellard9307c4c2004-04-04 12:57:25 +00003062 default_fmt_format = format;
3063 } else {
3064 count = 1;
3065 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003066 if (format != 'i') {
3067 size = default_fmt_size;
3068 } else {
3069 size = -1;
3070 }
bellard9307c4c2004-04-04 12:57:25 +00003071 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003072 qdict_put(qdict, "count", qint_from_int(count));
3073 qdict_put(qdict, "format", qint_from_int(format));
3074 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003075 }
3076 break;
3077 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003078 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00003079 {
blueswir1c2efc952007-09-25 17:28:42 +00003080 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003081
blueswir1cd390082008-11-16 13:53:32 +00003082 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003083 p++;
bellard34405572004-06-08 00:55:58 +00003084 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003085 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003086 if (*p == '\0') {
3087 typestr++;
3088 break;
3089 }
bellard34405572004-06-08 00:55:58 +00003090 } else {
3091 if (*p == '.') {
3092 p++;
blueswir1cd390082008-11-16 13:53:32 +00003093 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003094 p++;
bellard34405572004-06-08 00:55:58 +00003095 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003096 typestr++;
3097 break;
bellard34405572004-06-08 00:55:58 +00003098 }
3099 }
bellard13224a82006-07-14 22:03:35 +00003100 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003101 }
aliguori376253e2009-03-05 23:01:23 +00003102 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003103 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003104 /* Check if 'i' is greater than 32-bit */
3105 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3106 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3107 monitor_printf(mon, "integer is for 32-bit values\n");
3108 goto fail;
3109 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003110 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003111 }
3112 break;
3113 case '-':
3114 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003115 const char *tmp = p;
3116 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003117 /* option */
ths3b46e622007-09-17 08:09:54 +00003118
bellard9307c4c2004-04-04 12:57:25 +00003119 c = *typestr++;
3120 if (c == '\0')
3121 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003122 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003123 p++;
3124 has_option = 0;
3125 if (*p == '-') {
3126 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003127 if(c != *p) {
3128 if(!is_valid_option(p, typestr)) {
3129
3130 monitor_printf(mon, "%s: unsupported option -%c\n",
3131 cmdname, *p);
3132 goto fail;
3133 } else {
3134 skip_key = 1;
3135 }
bellard9307c4c2004-04-04 12:57:25 +00003136 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003137 if(skip_key) {
3138 p = tmp;
3139 } else {
3140 p++;
3141 has_option = 1;
3142 }
bellard9307c4c2004-04-04 12:57:25 +00003143 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003144 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003145 }
3146 break;
3147 default:
3148 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003149 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003150 goto fail;
3151 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003152 qemu_free(key);
3153 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003154 }
3155 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003156 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003157 p++;
3158 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003159 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3160 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003161 goto fail;
3162 }
3163
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003164 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003165
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003166fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003167 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003168 return NULL;
3169}
3170
3171static void monitor_handle_command(Monitor *mon, const char *cmdline)
3172{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003173 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003174 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003175
3176 qdict = qdict_new();
3177
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003178 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003179 if (!cmd)
3180 goto out;
3181
3182 qemu_errors_to_mon(mon);
3183
3184 if (monitor_handler_ported(cmd)) {
3185 QObject *data = NULL;
3186
3187 cmd->mhandler.cmd_new(mon, qdict, &data);
3188 if (data)
3189 cmd->user_print(mon, data);
3190
3191 qobject_decref(data);
3192 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003193 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003194 }
3195
Luiz Capitulino13917be2009-10-07 13:41:54 -03003196 qemu_errors_to_previous();
3197
3198out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003199 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003200}
3201
bellard81d09122004-07-14 17:21:37 +00003202static void cmd_completion(const char *name, const char *list)
3203{
3204 const char *p, *pstart;
3205 char cmd[128];
3206 int len;
3207
3208 p = list;
3209 for(;;) {
3210 pstart = p;
3211 p = strchr(p, '|');
3212 if (!p)
3213 p = pstart + strlen(pstart);
3214 len = p - pstart;
3215 if (len > sizeof(cmd) - 2)
3216 len = sizeof(cmd) - 2;
3217 memcpy(cmd, pstart, len);
3218 cmd[len] = '\0';
3219 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003220 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003221 }
3222 if (*p == '\0')
3223 break;
3224 p++;
3225 }
3226}
3227
3228static void file_completion(const char *input)
3229{
3230 DIR *ffs;
3231 struct dirent *d;
3232 char path[1024];
3233 char file[1024], file_prefix[1024];
3234 int input_path_len;
3235 const char *p;
3236
ths5fafdf22007-09-16 21:08:06 +00003237 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003238 if (!p) {
3239 input_path_len = 0;
3240 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003241 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003242 } else {
3243 input_path_len = p - input + 1;
3244 memcpy(path, input, input_path_len);
3245 if (input_path_len > sizeof(path) - 1)
3246 input_path_len = sizeof(path) - 1;
3247 path[input_path_len] = '\0';
3248 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3249 }
3250#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003251 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3252 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003253#endif
3254 ffs = opendir(path);
3255 if (!ffs)
3256 return;
3257 for(;;) {
3258 struct stat sb;
3259 d = readdir(ffs);
3260 if (!d)
3261 break;
3262 if (strstart(d->d_name, file_prefix, NULL)) {
3263 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003264 if (input_path_len < sizeof(file))
3265 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3266 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003267 /* stat the file to find out if it's a directory.
3268 * In that case add a slash to speed up typing long paths
3269 */
3270 stat(file, &sb);
3271 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003272 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003273 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003274 }
3275 }
3276 closedir(ffs);
3277}
3278
aliguori51de9762009-03-05 23:00:43 +00003279static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003280{
aliguori51de9762009-03-05 23:00:43 +00003281 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003282 const char *input = opaque;
3283
3284 if (input[0] == '\0' ||
3285 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003286 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003287 }
3288}
3289
3290/* NOTE: this parser is an approximate form of the real command parser */
3291static void parse_cmdline(const char *cmdline,
3292 int *pnb_args, char **args)
3293{
3294 const char *p;
3295 int nb_args, ret;
3296 char buf[1024];
3297
3298 p = cmdline;
3299 nb_args = 0;
3300 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003301 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003302 p++;
3303 if (*p == '\0')
3304 break;
3305 if (nb_args >= MAX_ARGS)
3306 break;
3307 ret = get_str(buf, sizeof(buf), &p);
3308 args[nb_args] = qemu_strdup(buf);
3309 nb_args++;
3310 if (ret < 0)
3311 break;
3312 }
3313 *pnb_args = nb_args;
3314}
3315
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003316static const char *next_arg_type(const char *typestr)
3317{
3318 const char *p = strchr(typestr, ':');
3319 return (p != NULL ? ++p : typestr);
3320}
3321
aliguori4c36ba32009-03-05 23:01:37 +00003322static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003323{
3324 const char *cmdname;
3325 char *args[MAX_ARGS];
3326 int nb_args, i, len;
3327 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003328 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003329 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003330
3331 parse_cmdline(cmdline, &nb_args, args);
3332#ifdef DEBUG_COMPLETION
3333 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003334 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003335 }
3336#endif
3337
3338 /* if the line ends with a space, it means we want to complete the
3339 next arg */
3340 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003341 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003342 if (nb_args >= MAX_ARGS)
3343 return;
3344 args[nb_args++] = qemu_strdup("");
3345 }
3346 if (nb_args <= 1) {
3347 /* command completion */
3348 if (nb_args == 0)
3349 cmdname = "";
3350 else
3351 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003352 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003353 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003354 cmd_completion(cmdname, cmd->name);
3355 }
3356 } else {
3357 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003358 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003359 if (compare_cmd(args[0], cmd->name))
3360 goto found;
3361 }
3362 return;
3363 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003364 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003365 for(i = 0; i < nb_args - 2; i++) {
3366 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003367 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003368 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003369 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003370 }
3371 }
3372 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003373 if (*ptype == '-' && ptype[1] != '\0') {
3374 ptype += 2;
3375 }
bellard81d09122004-07-14 17:21:37 +00003376 switch(*ptype) {
3377 case 'F':
3378 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003379 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003380 file_completion(str);
3381 break;
3382 case 'B':
3383 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003384 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003385 bdrv_iterate(block_completion_it, (void *)str);
3386 break;
bellard7fe48482004-10-09 18:08:01 +00003387 case 's':
3388 /* XXX: more generic ? */
3389 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003390 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003391 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3392 cmd_completion(str, cmd->name);
3393 }
bellard64866c32006-05-07 18:03:31 +00003394 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003395 char *sep = strrchr(str, '-');
3396 if (sep)
3397 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003398 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003399 for(key = key_defs; key->name != NULL; key++) {
3400 cmd_completion(str, key->name);
3401 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003402 } else if (!strcmp(cmd->name, "help|?")) {
3403 readline_set_completion_index(cur_mon->rs, strlen(str));
3404 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3405 cmd_completion(str, cmd->name);
3406 }
bellard7fe48482004-10-09 18:08:01 +00003407 }
3408 break;
bellard81d09122004-07-14 17:21:37 +00003409 default:
3410 break;
3411 }
3412 }
3413 for(i = 0; i < nb_args; i++)
3414 qemu_free(args[i]);
3415}
3416
aliguori731b0362009-03-05 23:01:42 +00003417static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003418{
aliguori731b0362009-03-05 23:01:42 +00003419 Monitor *mon = opaque;
3420
3421 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003422}
3423
aliguori731b0362009-03-05 23:01:42 +00003424static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003425{
aliguori731b0362009-03-05 23:01:42 +00003426 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003427 int i;
aliguori376253e2009-03-05 23:01:23 +00003428
aliguori731b0362009-03-05 23:01:42 +00003429 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003430
aliguoricde76ee2009-03-05 23:01:51 +00003431 if (cur_mon->rs) {
3432 for (i = 0; i < size; i++)
3433 readline_handle_byte(cur_mon->rs, buf[i]);
3434 } else {
3435 if (size == 0 || buf[size - 1] != 0)
3436 monitor_printf(cur_mon, "corrupted command\n");
3437 else
3438 monitor_handle_command(cur_mon, (char *)buf);
3439 }
aliguori731b0362009-03-05 23:01:42 +00003440
3441 cur_mon = old_mon;
3442}
aliguorid8f44602008-10-06 13:52:44 +00003443
aliguori376253e2009-03-05 23:01:23 +00003444static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003445{
aliguori731b0362009-03-05 23:01:42 +00003446 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003447 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003448 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003449}
3450
aliguoricde76ee2009-03-05 23:01:51 +00003451int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003452{
aliguoricde76ee2009-03-05 23:01:51 +00003453 if (!mon->rs)
3454 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003455 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003456 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003457}
3458
aliguori376253e2009-03-05 23:01:23 +00003459void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003460{
aliguoricde76ee2009-03-05 23:01:51 +00003461 if (!mon->rs)
3462 return;
aliguori731b0362009-03-05 23:01:42 +00003463 if (--mon->suspend_cnt == 0)
3464 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003465}
3466
aliguori731b0362009-03-05 23:01:42 +00003467static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003468{
aliguori376253e2009-03-05 23:01:23 +00003469 Monitor *mon = opaque;
3470
aliguori2724b182009-03-05 23:01:47 +00003471 switch (event) {
3472 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003473 mon->mux_out = 0;
3474 if (mon->reset_seen) {
3475 readline_restart(mon->rs);
3476 monitor_resume(mon);
3477 monitor_flush(mon);
3478 } else {
3479 mon->suspend_cnt = 0;
3480 }
aliguori2724b182009-03-05 23:01:47 +00003481 break;
ths86e94de2007-01-05 22:01:59 +00003482
aliguori2724b182009-03-05 23:01:47 +00003483 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003484 if (mon->reset_seen) {
3485 if (mon->suspend_cnt == 0) {
3486 monitor_printf(mon, "\n");
3487 }
3488 monitor_flush(mon);
3489 monitor_suspend(mon);
3490 } else {
3491 mon->suspend_cnt++;
3492 }
3493 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00003494 break;
3495
Amit Shahb6b8df52009-10-07 18:31:16 +05303496 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003497 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3498 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003499 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00003500 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003501 }
3502 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00003503 break;
3504 }
ths86e94de2007-01-05 22:01:59 +00003505}
3506
aliguori76655d62009-03-06 20:27:37 +00003507
3508/*
3509 * Local variables:
3510 * c-indent-level: 4
3511 * c-basic-offset: 4
3512 * tab-width: 8
3513 * End:
3514 */
3515
aliguori731b0362009-03-05 23:01:42 +00003516void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003517{
aliguori731b0362009-03-05 23:01:42 +00003518 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003519 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003520
3521 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003522 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003523 is_first_init = 0;
3524 }
aliguori87127162009-03-05 23:01:29 +00003525
3526 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003527
aliguori87127162009-03-05 23:01:29 +00003528 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003529 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00003530 if (flags & MONITOR_USE_READLINE) {
3531 mon->rs = readline_init(mon, monitor_find_completion);
3532 monitor_read_command(mon, 0);
3533 }
aliguori87127162009-03-05 23:01:29 +00003534
aliguori731b0362009-03-05 23:01:42 +00003535 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3536 mon);
aliguori87127162009-03-05 23:01:29 +00003537
Blue Swirl72cf2d42009-09-12 07:36:22 +00003538 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003539 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003540 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003541}
3542
aliguori376253e2009-03-05 23:01:23 +00003543static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003544{
aliguoribb5fc202009-03-05 23:01:15 +00003545 BlockDriverState *bs = opaque;
3546 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003547
aliguoribb5fc202009-03-05 23:01:15 +00003548 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003549 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003550 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003551 }
aliguori731b0362009-03-05 23:01:42 +00003552 if (mon->password_completion_cb)
3553 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003554
aliguori731b0362009-03-05 23:01:42 +00003555 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003556}
aliguoric0f4ce72009-03-05 23:01:01 +00003557
aliguori376253e2009-03-05 23:01:23 +00003558void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003559 BlockDriverCompletionFunc *completion_cb,
3560 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003561{
aliguoricde76ee2009-03-05 23:01:51 +00003562 int err;
3563
aliguoribb5fc202009-03-05 23:01:15 +00003564 if (!bdrv_key_required(bs)) {
3565 if (completion_cb)
3566 completion_cb(opaque, 0);
3567 return;
3568 }
aliguoric0f4ce72009-03-05 23:01:01 +00003569
aliguori376253e2009-03-05 23:01:23 +00003570 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3571 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003572
aliguori731b0362009-03-05 23:01:42 +00003573 mon->password_completion_cb = completion_cb;
3574 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003575
aliguoricde76ee2009-03-05 23:01:51 +00003576 err = monitor_read_password(mon, bdrv_password_cb, bs);
3577
3578 if (err && completion_cb)
3579 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003580}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003581
3582typedef struct QemuErrorSink QemuErrorSink;
3583struct QemuErrorSink {
3584 enum {
3585 ERR_SINK_FILE,
3586 ERR_SINK_MONITOR,
3587 } dest;
3588 union {
3589 FILE *fp;
3590 Monitor *mon;
3591 };
3592 QemuErrorSink *previous;
3593};
3594
Blue Swirl528e93a2009-08-31 15:14:40 +00003595static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003596
3597void qemu_errors_to_file(FILE *fp)
3598{
3599 QemuErrorSink *sink;
3600
3601 sink = qemu_mallocz(sizeof(*sink));
3602 sink->dest = ERR_SINK_FILE;
3603 sink->fp = fp;
3604 sink->previous = qemu_error_sink;
3605 qemu_error_sink = sink;
3606}
3607
3608void qemu_errors_to_mon(Monitor *mon)
3609{
3610 QemuErrorSink *sink;
3611
3612 sink = qemu_mallocz(sizeof(*sink));
3613 sink->dest = ERR_SINK_MONITOR;
3614 sink->mon = mon;
3615 sink->previous = qemu_error_sink;
3616 qemu_error_sink = sink;
3617}
3618
3619void qemu_errors_to_previous(void)
3620{
3621 QemuErrorSink *sink;
3622
3623 assert(qemu_error_sink != NULL);
3624 sink = qemu_error_sink;
3625 qemu_error_sink = sink->previous;
3626 qemu_free(sink);
3627}
3628
3629void qemu_error(const char *fmt, ...)
3630{
3631 va_list args;
3632
3633 assert(qemu_error_sink != NULL);
3634 switch (qemu_error_sink->dest) {
3635 case ERR_SINK_FILE:
3636 va_start(args, fmt);
3637 vfprintf(qemu_error_sink->fp, fmt, args);
3638 va_end(args);
3639 break;
3640 case ERR_SINK_MONITOR:
3641 va_start(args, fmt);
3642 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3643 va_end(args);
3644 break;
3645 }
3646}