blob: 66cec831b609f9551c2b97b47ea2371132707599 [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"
Luiz Capitulino8204a912009-11-18 23:05:31 -020052#include "qerror.h"
ths6a5bd302007-12-03 17:05:38 +000053
bellard9dc39cb2004-03-14 21:38:27 +000054//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000055//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000056
bellard9307c4c2004-04-04 12:57:25 +000057/*
58 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000059 *
bellard9307c4c2004-04-04 12:57:25 +000060 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000061 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000062 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000063 * 'i' 32 bit integer
64 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000065 * '/' optional gdb-like print format (like "/10x")
66 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030067 * '?' optional type (for all types, except '/')
68 * '.' other form of optional type (for 'i' and 'l')
69 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000070 *
71 */
72
Anthony Liguoric227f092009-10-01 16:12:16 -050073typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000074 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000075 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +000076 const char *params;
77 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -030078 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -030079 union {
80 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -030081 void (*info_new)(Monitor *mon, QObject **ret_data);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -030082 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -030083 void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Luiz Capitulino910df892009-10-07 13:41:51 -030084 } mhandler;
Anthony Liguoric227f092009-10-01 16:12:16 -050085} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000086
Mark McLoughlinf07918f2009-07-22 09:11:40 +010087/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -050088typedef struct mon_fd_t mon_fd_t;
89struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +010090 char *name;
91 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -050092 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010093};
94
aliguori87127162009-03-05 23:01:29 +000095struct Monitor {
96 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +020097 int mux_out;
98 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +000099 int flags;
100 int suspend_cnt;
101 uint8_t outbuf[1024];
102 int outbuf_index;
103 ReadLineState *rs;
104 CPUState *mon_cpu;
105 BlockDriverCompletionFunc *password_completion_cb;
106 void *password_opaque;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200107 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500108 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000109 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000110};
111
Blue Swirl72cf2d42009-09-12 07:36:22 +0000112static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000113
Anthony Liguoric227f092009-10-01 16:12:16 -0500114static const mon_cmd_t mon_cmds[];
115static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000116
aliguori87127162009-03-05 23:01:29 +0000117Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000118
aliguori731b0362009-03-05 23:01:42 +0000119static void monitor_command_cb(Monitor *mon, const char *cmdline,
120 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000121
Luiz Capitulino418173c2009-11-26 22:58:51 -0200122/* Return true if in control mode, false otherwise */
123static inline int monitor_ctrl_mode(const Monitor *mon)
124{
125 return (mon->flags & MONITOR_USE_CONTROL);
126}
127
aliguori731b0362009-03-05 23:01:42 +0000128static void monitor_read_command(Monitor *mon, int show_prompt)
129{
130 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
131 if (show_prompt)
132 readline_show_prompt(mon->rs);
133}
bellard6a00d602005-11-21 23:25:50 +0000134
aliguoricde76ee2009-03-05 23:01:51 +0000135static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
136 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000137{
aliguoricde76ee2009-03-05 23:01:51 +0000138 if (mon->rs) {
139 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
140 /* prompt is printed on return from the command handler */
141 return 0;
142 } else {
143 monitor_printf(mon, "terminal does not support password prompting\n");
144 return -ENOTTY;
145 }
aliguoribb5fc202009-03-05 23:01:15 +0000146}
147
aliguori376253e2009-03-05 23:01:23 +0000148void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000149{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200150 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000151 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
152 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000153 }
154}
155
156/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000157static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000158{
ths60fe76f2007-12-16 03:02:09 +0000159 char c;
aliguori731b0362009-03-05 23:01:42 +0000160
161 if (!mon)
162 return;
163
bellard7e2515e2004-08-01 21:52:19 +0000164 for(;;) {
165 c = *str++;
166 if (c == '\0')
167 break;
bellard7ba12602006-07-14 20:26:42 +0000168 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000169 mon->outbuf[mon->outbuf_index++] = '\r';
170 mon->outbuf[mon->outbuf_index++] = c;
171 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
172 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000173 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000174 }
175}
176
aliguori376253e2009-03-05 23:01:23 +0000177void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000178{
179 char buf[4096];
180 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000181 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000182}
183
aliguori376253e2009-03-05 23:01:23 +0000184void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000185{
186 va_list ap;
187 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000188 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000189 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000190}
191
aliguori376253e2009-03-05 23:01:23 +0000192void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000193{
194 int i;
195
196 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000197 switch (filename[i]) {
198 case ' ':
199 case '"':
200 case '\\':
201 monitor_printf(mon, "\\%c", filename[i]);
202 break;
203 case '\t':
204 monitor_printf(mon, "\\t");
205 break;
206 case '\r':
207 monitor_printf(mon, "\\r");
208 break;
209 case '\n':
210 monitor_printf(mon, "\\n");
211 break;
212 default:
213 monitor_printf(mon, "%c", filename[i]);
214 break;
215 }
thsfef30742006-12-22 14:11:32 +0000216 }
217}
218
bellard7fe48482004-10-09 18:08:01 +0000219static int monitor_fprintf(FILE *stream, const char *fmt, ...)
220{
221 va_list ap;
222 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000223 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000224 va_end(ap);
225 return 0;
226}
227
Luiz Capitulino13c74252009-10-07 13:41:55 -0300228static void monitor_user_noop(Monitor *mon, const QObject *data) { }
229
Luiz Capitulino13917be2009-10-07 13:41:54 -0300230static inline int monitor_handler_ported(const mon_cmd_t *cmd)
231{
232 return cmd->user_print != NULL;
233}
234
Luiz Capitulino8204a912009-11-18 23:05:31 -0200235static inline int monitor_has_error(const Monitor *mon)
236{
237 return mon->error != NULL;
238}
239
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300240static void monitor_print_qobject(Monitor *mon, const QObject *data)
241{
242 switch (qobject_type(data)) {
243 case QTYPE_QSTRING:
244 monitor_printf(mon, "%s",qstring_get_str(qobject_to_qstring(data)));
245 break;
246 case QTYPE_QINT:
247 monitor_printf(mon, "%" PRId64,qint_get_int(qobject_to_qint(data)));
248 break;
249 default:
250 monitor_printf(mon, "ERROR: unsupported type: %d",
251 qobject_type(data));
252 break;
253 }
254
255 monitor_puts(mon, "\n");
256}
257
bellard9dc39cb2004-03-14 21:38:27 +0000258static int compare_cmd(const char *name, const char *list)
259{
260 const char *p, *pstart;
261 int len;
262 len = strlen(name);
263 p = list;
264 for(;;) {
265 pstart = p;
266 p = strchr(p, '|');
267 if (!p)
268 p = pstart + strlen(pstart);
269 if ((p - pstart) == len && !memcmp(pstart, name, len))
270 return 1;
271 if (*p == '\0')
272 break;
273 p++;
274 }
275 return 0;
276}
277
Anthony Liguoric227f092009-10-01 16:12:16 -0500278static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000279 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000280{
Anthony Liguoric227f092009-10-01 16:12:16 -0500281 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000282
283 for(cmd = cmds; cmd->name != NULL; cmd++) {
284 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000285 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
286 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000287 }
288}
289
aliguori376253e2009-03-05 23:01:23 +0000290static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000291{
292 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000293 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000294 } else {
aliguori376253e2009-03-05 23:01:23 +0000295 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000296 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000297 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000298 monitor_printf(mon, "Log items (comma separated):\n");
299 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000300 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000301 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000302 }
303 }
bellard9dc39cb2004-03-14 21:38:27 +0000304 }
305}
306
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300307static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300308{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300309 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300310}
311
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300312static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000313{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200314 int all_devices;
315 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300316 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000317
bellard7954c732006-08-01 15:52:40 +0000318 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000319 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200320 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300321 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200322 continue;
323 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000324 }
325}
326
Luiz Capitulino13c74252009-10-07 13:41:55 -0300327static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000328{
Anthony Liguoric227f092009-10-01 16:12:16 -0500329 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300330 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000331
bellard9307c4c2004-04-04 12:57:25 +0000332 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000333 goto help;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300334
335 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000336 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300337 break;
bellard9dc39cb2004-03-14 21:38:27 +0000338 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300339
340 if (cmd->name == NULL)
341 goto help;
342
343 if (monitor_handler_ported(cmd)) {
344 cmd->mhandler.info_new(mon, ret_data);
345 if (*ret_data)
346 cmd->user_print(mon, *ret_data);
347 } else {
348 cmd->mhandler.info(mon);
349 }
350
bellard9dc39cb2004-03-14 21:38:27 +0000351 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300352
353help:
354 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000355}
356
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300357/**
358 * do_info_version(): Show QEMU version
359 */
360static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000361{
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300362 *ret_data = QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION));
bellard9bc9d1c2004-10-10 15:15:51 +0000363}
364
aliguori376253e2009-03-05 23:01:23 +0000365static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000366{
367 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000368 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000369}
370
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200371/**
372 * do_info_commands(): List QMP available commands
373 *
374 * Return a QList of QStrings.
375 */
376static void do_info_commands(Monitor *mon, QObject **ret_data)
377{
378 QList *cmd_list;
379 const mon_cmd_t *cmd;
380
381 cmd_list = qlist_new();
382
383 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
384 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
385 qlist_append(cmd_list, qstring_from_str(cmd->name));
386 }
387 }
388
389 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
390 if (monitor_handler_ported(cmd)) {
391 char buf[128];
392 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
393 qlist_append(cmd_list, qstring_from_str(buf));
394 }
395 }
396
397 *ret_data = QOBJECT(cmd_list);
398}
399
aurel32bf4f74c2008-12-18 22:42:34 +0000400#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000401static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000402{
aliguori376253e2009-03-05 23:01:23 +0000403 monitor_printf(mon, "HPET is %s by QEMU\n",
404 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000405}
aurel32bf4f74c2008-12-18 22:42:34 +0000406#endif
aliguori16b29ae2008-12-17 23:28:44 +0000407
aliguori376253e2009-03-05 23:01:23 +0000408static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000409{
aliguori376253e2009-03-05 23:01:23 +0000410 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
411 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
412 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
413 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
414 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000415}
416
bellard6a00d602005-11-21 23:25:50 +0000417/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000418static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000419{
420 CPUState *env;
421
422 for(env = first_cpu; env != NULL; env = env->next_cpu) {
423 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000424 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000425 return 0;
426 }
427 }
428 return -1;
429}
430
pbrook9596ebb2007-11-18 01:44:38 +0000431static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000432{
aliguori731b0362009-03-05 23:01:42 +0000433 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000434 mon_set_cpu(0);
435 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300436 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000437 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000438}
439
aliguori376253e2009-03-05 23:01:23 +0000440static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000441{
bellard6a00d602005-11-21 23:25:50 +0000442 CPUState *env;
443 env = mon_get_cpu();
444 if (!env)
445 return;
bellard9307c4c2004-04-04 12:57:25 +0000446#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000447 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000448 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000449#else
aliguori376253e2009-03-05 23:01:23 +0000450 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000451 0);
bellard9307c4c2004-04-04 12:57:25 +0000452#endif
453}
454
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300455static void print_cpu_iter(QObject *obj, void *opaque)
456{
457 QDict *cpu;
458 int active = ' ';
459 Monitor *mon = opaque;
460
461 assert(qobject_type(obj) == QTYPE_QDICT);
462 cpu = qobject_to_qdict(obj);
463
464 if (strcmp(qdict_get_str(cpu, "current"), "yes") == 0)
465 active = '*';
466
467 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
468
469#if defined(TARGET_I386)
470 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
471 (target_ulong) qdict_get_int(cpu, "pc"));
472#elif defined(TARGET_PPC)
473 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
474 (target_long) qdict_get_int(cpu, "nip"));
475#elif defined(TARGET_SPARC)
476 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
477 (target_long) qdict_get_int(cpu, "pc"));
478 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
479 (target_long) qdict_get_int(cpu, "npc"));
480#elif defined(TARGET_MIPS)
481 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
482 (target_long) qdict_get_int(cpu, "PC"));
483#endif
484
485 if (strcmp(qdict_get_str(cpu, "halted"), "yes") == 0)
486 monitor_printf(mon, " (halted)");
487
488 monitor_printf(mon, "\n");
489}
490
491static void monitor_print_cpus(Monitor *mon, const QObject *data)
492{
493 QList *cpu_list;
494
495 assert(qobject_type(data) == QTYPE_QLIST);
496 cpu_list = qobject_to_qlist(data);
497 qlist_iter(cpu_list, print_cpu_iter, mon);
498}
499
500/**
501 * do_info_cpus(): Show CPU information
502 *
503 * Return a QList with a QDict for each CPU.
504 *
505 * For example:
506 *
507 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
508 * { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]
509 */
510static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000511{
512 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300513 QList *cpu_list;
514
515 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000516
517 /* just to set the default cpu if not already done */
518 mon_get_cpu();
519
520 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300521 const char *answer;
522 QDict *cpu = qdict_new();
523
Avi Kivity4c0960c2009-08-17 23:19:53 +0300524 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300525
526 qdict_put(cpu, "CPU", qint_from_int(env->cpu_index));
527 answer = (env == mon->mon_cpu) ? "yes" : "no";
528 qdict_put(cpu, "current", qstring_from_str(answer));
529
bellard6a00d602005-11-21 23:25:50 +0000530#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300531 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000532#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300533 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000534#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300535 qdict_put(cpu, "pc", qint_from_int(env->pc));
536 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000537#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300538 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000539#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300540 answer = env->halted ? "yes" : "no";
541 qdict_put(cpu, "halted", qstring_from_str(answer));
542
543 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000544 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300545
546 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000547}
548
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300549static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000550{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300551 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000552 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000553 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000554}
555
aliguori376253e2009-03-05 23:01:23 +0000556static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000557{
aliguori376253e2009-03-05 23:01:23 +0000558 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000559}
560
aliguori376253e2009-03-05 23:01:23 +0000561static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000562{
563 int i;
bellard7e2515e2004-08-01 21:52:19 +0000564 const char *str;
ths3b46e622007-09-17 08:09:54 +0000565
aliguoricde76ee2009-03-05 23:01:51 +0000566 if (!mon->rs)
567 return;
bellard7e2515e2004-08-01 21:52:19 +0000568 i = 0;
569 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000570 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000571 if (!str)
572 break;
aliguori376253e2009-03-05 23:01:23 +0000573 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000574 i++;
bellardaa455482004-04-04 13:07:25 +0000575 }
576}
577
j_mayer76a66252007-03-07 08:32:30 +0000578#if defined(TARGET_PPC)
579/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000580static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000581{
582 CPUState *env;
583
584 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000585 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000586}
587#endif
588
Luiz Capitulinob223f352009-10-07 13:41:56 -0300589/**
590 * do_quit(): Quit QEMU execution
591 */
592static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000593{
594 exit(0);
595}
596
aliguori376253e2009-03-05 23:01:23 +0000597static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000598{
599 if (bdrv_is_inserted(bs)) {
600 if (!force) {
601 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000602 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000603 return -1;
604 }
605 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000606 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000607 return -1;
608 }
609 }
610 bdrv_close(bs);
611 }
612 return 0;
613}
614
Luiz Capitulinoe1c923a2009-10-16 12:23:49 -0300615static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000616{
617 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300618 int force = qdict_get_int(qdict, "force");
619 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000620
bellard9307c4c2004-04-04 12:57:25 +0000621 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000622 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000623 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000624 return;
625 }
aliguori376253e2009-03-05 23:01:23 +0000626 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000627}
628
aliguori376253e2009-03-05 23:01:23 +0000629static void do_change_block(Monitor *mon, const char *device,
630 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000631{
632 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000633 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000634
bellard9307c4c2004-04-04 12:57:25 +0000635 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000636 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000637 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000638 return;
639 }
aurel322ecea9b2008-06-18 22:10:01 +0000640 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100641 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000642 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000643 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000644 return;
645 }
646 }
aliguori376253e2009-03-05 23:01:23 +0000647 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000648 return;
aurel322ecea9b2008-06-18 22:10:01 +0000649 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000650 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000651}
652
aliguori376253e2009-03-05 23:01:23 +0000653static void change_vnc_password_cb(Monitor *mon, const char *password,
654 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000655{
656 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000657 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000658
aliguori731b0362009-03-05 23:01:42 +0000659 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000660}
661
aliguori376253e2009-03-05 23:01:23 +0000662static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000663{
ths70848512007-08-25 01:37:05 +0000664 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000665 strcmp(target, "password") == 0) {
666 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000667 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000668 strncpy(password, arg, sizeof(password));
669 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000670 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000671 } else {
aliguori376253e2009-03-05 23:01:23 +0000672 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000673 }
ths70848512007-08-25 01:37:05 +0000674 } else {
aliguori28a76be2009-03-06 20:27:40 +0000675 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000676 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000677 }
thse25a5822007-08-25 01:36:20 +0000678}
679
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300680static void do_change(Monitor *mon, const QDict *qdict)
thse25a5822007-08-25 01:36:20 +0000681{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300682 const char *device = qdict_get_str(qdict, "device");
683 const char *target = qdict_get_str(qdict, "target");
684 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000685 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000686 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000687 } else {
aliguori28a76be2009-03-06 20:27:40 +0000688 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000689 }
690}
691
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300692static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000693{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300694 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000695}
696
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300697static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000698{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300699 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000700}
701
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300702static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000703{
704 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300705 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000706
bellard9307c4c2004-04-04 12:57:25 +0000707 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000708 mask = 0;
709 } else {
bellard9307c4c2004-04-04 12:57:25 +0000710 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000711 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000712 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000713 return;
714 }
715 }
716 cpu_set_log(mask);
717}
718
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300719static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000720{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300721 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000722 if (!option || !strcmp(option, "on")) {
723 singlestep = 1;
724 } else if (!strcmp(option, "off")) {
725 singlestep = 0;
726 } else {
727 monitor_printf(mon, "unexpected option %s\n", option);
728 }
729}
730
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -0300731/**
732 * do_stop(): Stop VM execution
733 */
734static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +0000735{
736 vm_stop(EXCP_INTERRUPT);
737}
738
aliguoribb5fc202009-03-05 23:01:15 +0000739static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000740
aliguori376253e2009-03-05 23:01:23 +0000741struct bdrv_iterate_context {
742 Monitor *mon;
743 int err;
744};
aliguoric0f4ce72009-03-05 23:01:01 +0000745
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300746/**
747 * do_cont(): Resume emulation.
748 */
749static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +0000750{
751 struct bdrv_iterate_context context = { mon, 0 };
752
753 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000754 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000755 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000756 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000757}
758
aliguoribb5fc202009-03-05 23:01:15 +0000759static void bdrv_key_cb(void *opaque, int err)
760{
aliguori376253e2009-03-05 23:01:23 +0000761 Monitor *mon = opaque;
762
aliguoribb5fc202009-03-05 23:01:15 +0000763 /* another key was set successfully, retry to continue */
764 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300765 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000766}
767
768static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
769{
aliguori376253e2009-03-05 23:01:23 +0000770 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000771
aliguori376253e2009-03-05 23:01:23 +0000772 if (!context->err && bdrv_key_required(bs)) {
773 context->err = -EBUSY;
774 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
775 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000776 }
777}
778
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300779static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000780{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300781 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000782 if (!device)
783 device = "tcp::" DEFAULT_GDBSTUB_PORT;
784 if (gdbserver_start(device) < 0) {
785 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
786 device);
787 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000788 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000789 } else {
aliguori59030a82009-04-05 18:43:41 +0000790 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
791 device);
bellard8a7ddc32004-03-31 19:00:16 +0000792 }
793}
794
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300795static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100796{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300797 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100798 if (select_watchdog_action(action) == -1) {
799 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
800 }
801}
802
aliguori376253e2009-03-05 23:01:23 +0000803static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000804{
aliguori376253e2009-03-05 23:01:23 +0000805 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000806 switch(c) {
807 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000808 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000809 break;
810 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000811 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000812 break;
813 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000814 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000815 break;
816 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000817 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000818 break;
819 default:
820 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000821 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000822 } else {
aliguori376253e2009-03-05 23:01:23 +0000823 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000824 }
825 break;
826 }
aliguori376253e2009-03-05 23:01:23 +0000827 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000828}
829
aliguori376253e2009-03-05 23:01:23 +0000830static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -0500831 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000832{
bellard6a00d602005-11-21 23:25:50 +0000833 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000834 int nb_per_line, l, line_size, i, max_digits, len;
835 uint8_t buf[16];
836 uint64_t v;
837
838 if (format == 'i') {
839 int flags;
840 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000841 env = mon_get_cpu();
842 if (!env && !is_physical)
843 return;
bellard9307c4c2004-04-04 12:57:25 +0000844#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000845 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000846 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000847 } else if (wsize == 4) {
848 flags = 0;
849 } else {
bellard6a15fd12006-04-12 21:07:07 +0000850 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000851 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000852 if (env) {
853#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000854 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000855 (env->segs[R_CS].flags & DESC_L_MASK))
856 flags = 2;
857 else
858#endif
859 if (!(env->segs[R_CS].flags & DESC_B_MASK))
860 flags = 1;
861 }
bellard4c27ba22004-04-25 18:05:08 +0000862 }
863#endif
aliguori376253e2009-03-05 23:01:23 +0000864 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000865 return;
866 }
867
868 len = wsize * count;
869 if (wsize == 1)
870 line_size = 8;
871 else
872 line_size = 16;
873 nb_per_line = line_size / wsize;
874 max_digits = 0;
875
876 switch(format) {
877 case 'o':
878 max_digits = (wsize * 8 + 2) / 3;
879 break;
880 default:
881 case 'x':
882 max_digits = (wsize * 8) / 4;
883 break;
884 case 'u':
885 case 'd':
886 max_digits = (wsize * 8 * 10 + 32) / 33;
887 break;
888 case 'c':
889 wsize = 1;
890 break;
891 }
892
893 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000894 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000895 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000896 else
aliguori376253e2009-03-05 23:01:23 +0000897 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000898 l = len;
899 if (l > line_size)
900 l = line_size;
901 if (is_physical) {
902 cpu_physical_memory_rw(addr, buf, l, 0);
903 } else {
bellard6a00d602005-11-21 23:25:50 +0000904 env = mon_get_cpu();
905 if (!env)
906 break;
aliguoric8f79b62008-08-18 14:00:20 +0000907 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000908 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000909 break;
910 }
bellard9307c4c2004-04-04 12:57:25 +0000911 }
ths5fafdf22007-09-16 21:08:06 +0000912 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000913 while (i < l) {
914 switch(wsize) {
915 default:
916 case 1:
917 v = ldub_raw(buf + i);
918 break;
919 case 2:
920 v = lduw_raw(buf + i);
921 break;
922 case 4:
bellard92a31b12005-02-10 22:00:52 +0000923 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000924 break;
925 case 8:
926 v = ldq_raw(buf + i);
927 break;
928 }
aliguori376253e2009-03-05 23:01:23 +0000929 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000930 switch(format) {
931 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000932 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000933 break;
934 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000935 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000936 break;
937 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000938 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000939 break;
940 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000941 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000942 break;
943 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000944 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000945 break;
946 }
947 i += wsize;
948 }
aliguori376253e2009-03-05 23:01:23 +0000949 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000950 addr += l;
951 len -= l;
952 }
953}
954
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300955static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000956{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300957 int count = qdict_get_int(qdict, "count");
958 int format = qdict_get_int(qdict, "format");
959 int size = qdict_get_int(qdict, "size");
960 target_long addr = qdict_get_int(qdict, "addr");
961
aliguori376253e2009-03-05 23:01:23 +0000962 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000963}
964
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300965static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000966{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300967 int count = qdict_get_int(qdict, "count");
968 int format = qdict_get_int(qdict, "format");
969 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -0500970 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300971
aliguori376253e2009-03-05 23:01:23 +0000972 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000973}
974
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300975static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000976{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300977 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -0500978 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300979
blueswir17743e582007-09-24 18:39:04 +0000980#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000981 switch(format) {
982 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000983 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000984 break;
985 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000986 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000987 break;
988 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000989 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000990 break;
991 default:
992 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000993 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000994 break;
995 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000996 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000997 break;
998 }
bellard92a31b12005-02-10 22:00:52 +0000999#else
1000 switch(format) {
1001 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001002 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001003 break;
1004 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001005 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001006 break;
1007 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001008 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001009 break;
1010 default:
1011 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001012 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001013 break;
1014 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001015 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001016 break;
1017 }
1018#endif
aliguori376253e2009-03-05 23:01:23 +00001019 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001020}
1021
Luiz Capitulino57e09452009-10-16 12:23:43 -03001022static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001023{
1024 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001025 uint32_t size = qdict_get_int(qdict, "size");
1026 const char *filename = qdict_get_str(qdict, "filename");
1027 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001028 uint32_t l;
1029 CPUState *env;
1030 uint8_t buf[1024];
1031
1032 env = mon_get_cpu();
1033 if (!env)
1034 return;
1035
1036 f = fopen(filename, "wb");
1037 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001038 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +00001039 return;
1040 }
1041 while (size != 0) {
1042 l = sizeof(buf);
1043 if (l > size)
1044 l = size;
1045 cpu_memory_rw_debug(env, addr, buf, l, 0);
1046 fwrite(buf, 1, l, f);
1047 addr += l;
1048 size -= l;
1049 }
1050 fclose(f);
1051}
1052
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001053static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1054 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001055{
1056 FILE *f;
1057 uint32_t l;
1058 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001059 uint32_t size = qdict_get_int(qdict, "size");
1060 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001061 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +00001062
1063 f = fopen(filename, "wb");
1064 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001065 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +00001066 return;
1067 }
1068 while (size != 0) {
1069 l = sizeof(buf);
1070 if (l > size)
1071 l = size;
1072 cpu_physical_memory_rw(addr, buf, l, 0);
1073 fwrite(buf, 1, l, f);
1074 fflush(f);
1075 addr += l;
1076 size -= l;
1077 }
1078 fclose(f);
1079}
1080
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001081static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001082{
1083 uint32_t addr;
1084 uint8_t buf[1];
1085 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001086 uint32_t start = qdict_get_int(qdict, "start");
1087 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001088
1089 sum = 0;
1090 for(addr = start; addr < (start + size); addr++) {
1091 cpu_physical_memory_rw(addr, buf, 1, 0);
1092 /* BSD sum algorithm ('sum' Unix command) */
1093 sum = (sum >> 1) | (sum << 15);
1094 sum += buf[0];
1095 }
aliguori376253e2009-03-05 23:01:23 +00001096 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001097}
1098
bellarda3a91a32004-06-04 11:06:21 +00001099typedef struct {
1100 int keycode;
1101 const char *name;
1102} KeyDef;
1103
1104static const KeyDef key_defs[] = {
1105 { 0x2a, "shift" },
1106 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001107
bellarda3a91a32004-06-04 11:06:21 +00001108 { 0x38, "alt" },
1109 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001110 { 0x64, "altgr" },
1111 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001112 { 0x1d, "ctrl" },
1113 { 0x9d, "ctrl_r" },
1114
1115 { 0xdd, "menu" },
1116
1117 { 0x01, "esc" },
1118
1119 { 0x02, "1" },
1120 { 0x03, "2" },
1121 { 0x04, "3" },
1122 { 0x05, "4" },
1123 { 0x06, "5" },
1124 { 0x07, "6" },
1125 { 0x08, "7" },
1126 { 0x09, "8" },
1127 { 0x0a, "9" },
1128 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001129 { 0x0c, "minus" },
1130 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001131 { 0x0e, "backspace" },
1132
1133 { 0x0f, "tab" },
1134 { 0x10, "q" },
1135 { 0x11, "w" },
1136 { 0x12, "e" },
1137 { 0x13, "r" },
1138 { 0x14, "t" },
1139 { 0x15, "y" },
1140 { 0x16, "u" },
1141 { 0x17, "i" },
1142 { 0x18, "o" },
1143 { 0x19, "p" },
1144
1145 { 0x1c, "ret" },
1146
1147 { 0x1e, "a" },
1148 { 0x1f, "s" },
1149 { 0x20, "d" },
1150 { 0x21, "f" },
1151 { 0x22, "g" },
1152 { 0x23, "h" },
1153 { 0x24, "j" },
1154 { 0x25, "k" },
1155 { 0x26, "l" },
1156
1157 { 0x2c, "z" },
1158 { 0x2d, "x" },
1159 { 0x2e, "c" },
1160 { 0x2f, "v" },
1161 { 0x30, "b" },
1162 { 0x31, "n" },
1163 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001164 { 0x33, "comma" },
1165 { 0x34, "dot" },
1166 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001167
balrog4d3b6f62008-02-10 16:33:14 +00001168 { 0x37, "asterisk" },
1169
bellarda3a91a32004-06-04 11:06:21 +00001170 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001171 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001172 { 0x3b, "f1" },
1173 { 0x3c, "f2" },
1174 { 0x3d, "f3" },
1175 { 0x3e, "f4" },
1176 { 0x3f, "f5" },
1177 { 0x40, "f6" },
1178 { 0x41, "f7" },
1179 { 0x42, "f8" },
1180 { 0x43, "f9" },
1181 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001182 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001183 { 0x46, "scroll_lock" },
1184
bellard64866c32006-05-07 18:03:31 +00001185 { 0xb5, "kp_divide" },
1186 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001187 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001188 { 0x4e, "kp_add" },
1189 { 0x9c, "kp_enter" },
1190 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001191 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001192
1193 { 0x52, "kp_0" },
1194 { 0x4f, "kp_1" },
1195 { 0x50, "kp_2" },
1196 { 0x51, "kp_3" },
1197 { 0x4b, "kp_4" },
1198 { 0x4c, "kp_5" },
1199 { 0x4d, "kp_6" },
1200 { 0x47, "kp_7" },
1201 { 0x48, "kp_8" },
1202 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001203
bellarda3a91a32004-06-04 11:06:21 +00001204 { 0x56, "<" },
1205
1206 { 0x57, "f11" },
1207 { 0x58, "f12" },
1208
1209 { 0xb7, "print" },
1210
1211 { 0xc7, "home" },
1212 { 0xc9, "pgup" },
1213 { 0xd1, "pgdn" },
1214 { 0xcf, "end" },
1215
1216 { 0xcb, "left" },
1217 { 0xc8, "up" },
1218 { 0xd0, "down" },
1219 { 0xcd, "right" },
1220
1221 { 0xd2, "insert" },
1222 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001223#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1224 { 0xf0, "stop" },
1225 { 0xf1, "again" },
1226 { 0xf2, "props" },
1227 { 0xf3, "undo" },
1228 { 0xf4, "front" },
1229 { 0xf5, "copy" },
1230 { 0xf6, "open" },
1231 { 0xf7, "paste" },
1232 { 0xf8, "find" },
1233 { 0xf9, "cut" },
1234 { 0xfa, "lf" },
1235 { 0xfb, "help" },
1236 { 0xfc, "meta_l" },
1237 { 0xfd, "meta_r" },
1238 { 0xfe, "compose" },
1239#endif
bellarda3a91a32004-06-04 11:06:21 +00001240 { 0, NULL },
1241};
1242
1243static int get_keycode(const char *key)
1244{
1245 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001246 char *endp;
1247 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001248
1249 for(p = key_defs; p->name != NULL; p++) {
1250 if (!strcmp(key, p->name))
1251 return p->keycode;
1252 }
bellard64866c32006-05-07 18:03:31 +00001253 if (strstart(key, "0x", NULL)) {
1254 ret = strtoul(key, &endp, 0);
1255 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1256 return ret;
1257 }
bellarda3a91a32004-06-04 11:06:21 +00001258 return -1;
1259}
1260
balrogc8256f92008-06-08 22:45:01 +00001261#define MAX_KEYCODES 16
1262static uint8_t keycodes[MAX_KEYCODES];
1263static int nb_pending_keycodes;
1264static QEMUTimer *key_timer;
1265
1266static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001267{
balrogc8256f92008-06-08 22:45:01 +00001268 int keycode;
1269
1270 while (nb_pending_keycodes > 0) {
1271 nb_pending_keycodes--;
1272 keycode = keycodes[nb_pending_keycodes];
1273 if (keycode & 0x80)
1274 kbd_put_keycode(0xe0);
1275 kbd_put_keycode(keycode | 0x80);
1276 }
1277}
1278
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001279static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001280{
balrog3401c0d2008-06-04 10:05:59 +00001281 char keyname_buf[16];
1282 char *separator;
1283 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001284 const char *string = qdict_get_str(qdict, "string");
1285 int has_hold_time = qdict_haskey(qdict, "hold_time");
1286 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001287
balrogc8256f92008-06-08 22:45:01 +00001288 if (nb_pending_keycodes > 0) {
1289 qemu_del_timer(key_timer);
1290 release_keys(NULL);
1291 }
1292 if (!has_hold_time)
1293 hold_time = 100;
1294 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001295 while (1) {
1296 separator = strchr(string, '-');
1297 keyname_len = separator ? separator - string : strlen(string);
1298 if (keyname_len > 0) {
1299 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1300 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001301 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001302 return;
bellarda3a91a32004-06-04 11:06:21 +00001303 }
balrogc8256f92008-06-08 22:45:01 +00001304 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001305 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001306 return;
1307 }
1308 keyname_buf[keyname_len] = 0;
1309 keycode = get_keycode(keyname_buf);
1310 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001311 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001312 return;
1313 }
balrogc8256f92008-06-08 22:45:01 +00001314 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001315 }
balrog3401c0d2008-06-04 10:05:59 +00001316 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001317 break;
balrog3401c0d2008-06-04 10:05:59 +00001318 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001319 }
balrogc8256f92008-06-08 22:45:01 +00001320 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001321 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001322 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001323 keycode = keycodes[i];
1324 if (keycode & 0x80)
1325 kbd_put_keycode(0xe0);
1326 kbd_put_keycode(keycode & 0x7f);
1327 }
balrogc8256f92008-06-08 22:45:01 +00001328 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001329 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001330 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001331}
1332
bellard13224a82006-07-14 22:03:35 +00001333static int mouse_button_state;
1334
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001335static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001336{
1337 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001338 const char *dx_str = qdict_get_str(qdict, "dx_str");
1339 const char *dy_str = qdict_get_str(qdict, "dy_str");
1340 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001341 dx = strtol(dx_str, NULL, 0);
1342 dy = strtol(dy_str, NULL, 0);
1343 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001344 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001345 dz = strtol(dz_str, NULL, 0);
1346 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1347}
1348
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001349static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001350{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001351 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001352 mouse_button_state = button_state;
1353 kbd_mouse_event(0, 0, 0, mouse_button_state);
1354}
1355
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001356static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001357{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001358 int size = qdict_get_int(qdict, "size");
1359 int addr = qdict_get_int(qdict, "addr");
1360 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001361 uint32_t val;
1362 int suffix;
1363
1364 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001365 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001366 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001367 addr++;
1368 }
1369 addr &= 0xffff;
1370
1371 switch(size) {
1372 default:
1373 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001374 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001375 suffix = 'b';
1376 break;
1377 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001378 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001379 suffix = 'w';
1380 break;
1381 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001382 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001383 suffix = 'l';
1384 break;
1385 }
aliguori376253e2009-03-05 23:01:23 +00001386 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1387 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001388}
bellarda3a91a32004-06-04 11:06:21 +00001389
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001390static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001391{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001392 int size = qdict_get_int(qdict, "size");
1393 int addr = qdict_get_int(qdict, "addr");
1394 int val = qdict_get_int(qdict, "val");
1395
Jan Kiszkaf1147842009-07-14 10:20:11 +02001396 addr &= IOPORTS_MASK;
1397
1398 switch (size) {
1399 default:
1400 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001401 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001402 break;
1403 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001404 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001405 break;
1406 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001407 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001408 break;
1409 }
1410}
1411
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001412static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001413{
1414 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001415 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001416
Jan Kiszka76e30d02009-07-02 00:19:02 +02001417 res = qemu_boot_set(bootdevice);
1418 if (res == 0) {
1419 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1420 } else if (res > 0) {
1421 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001422 } else {
aliguori376253e2009-03-05 23:01:23 +00001423 monitor_printf(mon, "no function defined to set boot device list for "
1424 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001425 }
1426}
1427
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001428/**
1429 * do_system_reset(): Issue a machine reset
1430 */
1431static void do_system_reset(Monitor *mon, const QDict *qdict,
1432 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001433{
1434 qemu_system_reset_request();
1435}
1436
Luiz Capitulino43076662009-10-07 13:41:59 -03001437/**
1438 * do_system_powerdown(): Issue a machine powerdown
1439 */
1440static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1441 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001442{
1443 qemu_system_powerdown_request();
1444}
1445
bellardb86bda52004-09-18 19:32:46 +00001446#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001447static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001448{
aliguori376253e2009-03-05 23:01:23 +00001449 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1450 addr,
1451 pte & mask,
1452 pte & PG_GLOBAL_MASK ? 'G' : '-',
1453 pte & PG_PSE_MASK ? 'P' : '-',
1454 pte & PG_DIRTY_MASK ? 'D' : '-',
1455 pte & PG_ACCESSED_MASK ? 'A' : '-',
1456 pte & PG_PCD_MASK ? 'C' : '-',
1457 pte & PG_PWT_MASK ? 'T' : '-',
1458 pte & PG_USER_MASK ? 'U' : '-',
1459 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001460}
1461
aliguori376253e2009-03-05 23:01:23 +00001462static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001463{
bellard6a00d602005-11-21 23:25:50 +00001464 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001465 int l1, l2;
1466 uint32_t pgd, pde, pte;
1467
bellard6a00d602005-11-21 23:25:50 +00001468 env = mon_get_cpu();
1469 if (!env)
1470 return;
1471
bellardb86bda52004-09-18 19:32:46 +00001472 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001473 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001474 return;
1475 }
1476 pgd = env->cr[3] & ~0xfff;
1477 for(l1 = 0; l1 < 1024; l1++) {
1478 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1479 pde = le32_to_cpu(pde);
1480 if (pde & PG_PRESENT_MASK) {
1481 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001482 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001483 } else {
1484 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001485 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001486 (uint8_t *)&pte, 4);
1487 pte = le32_to_cpu(pte);
1488 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001489 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001490 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001491 ~0xfff);
1492 }
1493 }
1494 }
1495 }
1496 }
1497}
1498
aliguori376253e2009-03-05 23:01:23 +00001499static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001500 uint32_t end, int prot)
1501{
bellard9746b152004-11-11 18:30:24 +00001502 int prot1;
1503 prot1 = *plast_prot;
1504 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001505 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001506 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1507 *pstart, end, end - *pstart,
1508 prot1 & PG_USER_MASK ? 'u' : '-',
1509 'r',
1510 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001511 }
1512 if (prot != 0)
1513 *pstart = end;
1514 else
1515 *pstart = -1;
1516 *plast_prot = prot;
1517 }
1518}
1519
aliguori376253e2009-03-05 23:01:23 +00001520static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001521{
bellard6a00d602005-11-21 23:25:50 +00001522 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001523 int l1, l2, prot, last_prot;
1524 uint32_t pgd, pde, pte, start, end;
1525
bellard6a00d602005-11-21 23:25:50 +00001526 env = mon_get_cpu();
1527 if (!env)
1528 return;
1529
bellardb86bda52004-09-18 19:32:46 +00001530 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001531 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001532 return;
1533 }
1534 pgd = env->cr[3] & ~0xfff;
1535 last_prot = 0;
1536 start = -1;
1537 for(l1 = 0; l1 < 1024; l1++) {
1538 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1539 pde = le32_to_cpu(pde);
1540 end = l1 << 22;
1541 if (pde & PG_PRESENT_MASK) {
1542 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1543 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001544 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001545 } else {
1546 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001547 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001548 (uint8_t *)&pte, 4);
1549 pte = le32_to_cpu(pte);
1550 end = (l1 << 22) + (l2 << 12);
1551 if (pte & PG_PRESENT_MASK) {
1552 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1553 } else {
1554 prot = 0;
1555 }
aliguori376253e2009-03-05 23:01:23 +00001556 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001557 }
1558 }
1559 } else {
1560 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001561 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001562 }
1563 }
1564}
1565#endif
1566
aurel327c664e22009-03-03 06:12:22 +00001567#if defined(TARGET_SH4)
1568
aliguori376253e2009-03-05 23:01:23 +00001569static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001570{
aliguori376253e2009-03-05 23:01:23 +00001571 monitor_printf(mon, " tlb%i:\t"
1572 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1573 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1574 "dirty=%hhu writethrough=%hhu\n",
1575 idx,
1576 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1577 tlb->v, tlb->sh, tlb->c, tlb->pr,
1578 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001579}
1580
aliguori376253e2009-03-05 23:01:23 +00001581static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001582{
1583 CPUState *env = mon_get_cpu();
1584 int i;
1585
aliguori376253e2009-03-05 23:01:23 +00001586 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001587 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001588 print_tlb (mon, i, &env->itlb[i]);
1589 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001590 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001591 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001592}
1593
1594#endif
1595
aliguori376253e2009-03-05 23:01:23 +00001596static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001597{
1598#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001599 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001600 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001601 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001602 else
aliguori376253e2009-03-05 23:01:23 +00001603 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001604#else
aliguori376253e2009-03-05 23:01:23 +00001605 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001606#endif
1607}
1608
aliguori030ea372009-04-21 22:30:47 +00001609static void do_info_numa(Monitor *mon)
1610{
aliguorib28b6232009-04-22 20:20:29 +00001611 int i;
aliguori030ea372009-04-21 22:30:47 +00001612 CPUState *env;
1613
1614 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1615 for (i = 0; i < nb_numa_nodes; i++) {
1616 monitor_printf(mon, "node %d cpus:", i);
1617 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1618 if (env->numa_node == i) {
1619 monitor_printf(mon, " %d", env->cpu_index);
1620 }
1621 }
1622 monitor_printf(mon, "\n");
1623 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1624 node_mem[i] >> 20);
1625 }
1626}
1627
bellard5f1ce942006-02-08 22:40:15 +00001628#ifdef CONFIG_PROFILER
1629
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001630int64_t qemu_time;
1631int64_t dev_time;
1632
aliguori376253e2009-03-05 23:01:23 +00001633static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001634{
1635 int64_t total;
1636 total = qemu_time;
1637 if (total == 0)
1638 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001639 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001640 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001641 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001642 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001643 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001644 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001645}
1646#else
aliguori376253e2009-03-05 23:01:23 +00001647static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001648{
aliguori376253e2009-03-05 23:01:23 +00001649 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001650}
1651#endif
1652
bellardec36b692006-07-16 18:57:03 +00001653/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001654static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001655
aliguori376253e2009-03-05 23:01:23 +00001656static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001657{
1658 int i;
1659 CaptureState *s;
1660
1661 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001662 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001663 s->ops.info (s->opaque);
1664 }
1665}
1666
Blue Swirl23130862009-06-06 08:22:04 +00001667#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001668static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001669{
1670 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001671 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001672 CaptureState *s;
1673
1674 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1675 if (i == n) {
1676 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001677 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00001678 qemu_free (s);
1679 return;
1680 }
1681 }
1682}
1683
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001684static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001685{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001686 const char *path = qdict_get_str(qdict, "path");
1687 int has_freq = qdict_haskey(qdict, "freq");
1688 int freq = qdict_get_try_int(qdict, "freq", -1);
1689 int has_bits = qdict_haskey(qdict, "bits");
1690 int bits = qdict_get_try_int(qdict, "bits", -1);
1691 int has_channels = qdict_haskey(qdict, "nchannels");
1692 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001693 CaptureState *s;
1694
1695 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001696
1697 freq = has_freq ? freq : 44100;
1698 bits = has_bits ? bits : 16;
1699 nchannels = has_channels ? nchannels : 2;
1700
1701 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001702 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001703 qemu_free (s);
1704 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001705 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001706}
1707#endif
1708
aurel32dc1c0b72008-04-27 23:52:12 +00001709#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001710static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001711{
1712 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001713 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001714
1715 for (env = first_cpu; env != NULL; env = env->next_cpu)
1716 if (env->cpu_index == cpu_index) {
1717 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1718 break;
1719 }
1720}
1721#endif
1722
aliguori376253e2009-03-05 23:01:23 +00001723static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001724{
aurel321b530a62009-04-05 20:08:59 +00001725 if (vm_running) {
1726 if (singlestep) {
1727 monitor_printf(mon, "VM status: running (single step mode)\n");
1728 } else {
1729 monitor_printf(mon, "VM status: running\n");
1730 }
1731 } else
aliguori376253e2009-03-05 23:01:23 +00001732 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001733}
1734
Luiz Capitulino83fb1de2009-10-07 13:42:01 -03001735/**
1736 * do_balloon(): Request VM to change its memory allocation
1737 */
1738static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001739{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001740 int value = qdict_get_int(qdict, "value");
Anthony Liguoric227f092009-10-01 16:12:16 -05001741 ram_addr_t target = value;
aliguoridf751fa2008-12-04 20:19:35 +00001742 qemu_balloon(target << 20);
1743}
1744
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001745static void monitor_print_balloon(Monitor *mon, const QObject *data)
1746{
1747 monitor_printf(mon, "balloon: actual=%d\n",
1748 (int)qint_get_int(qobject_to_qint(data)));
1749}
1750
1751/**
1752 * do_info_balloon(): Balloon information
1753 */
1754static void do_info_balloon(Monitor *mon, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001755{
Anthony Liguoric227f092009-10-01 16:12:16 -05001756 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00001757
1758 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001759 if (kvm_enabled() && !kvm_has_sync_mmu())
Luiz Capitulino5d6c37f2009-11-18 23:05:36 -02001760 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
aliguoribd322082008-12-04 20:33:06 +00001761 else if (actual == 0)
Luiz Capitulino5d6c37f2009-11-18 23:05:36 -02001762 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
aliguoridf751fa2008-12-04 20:19:35 +00001763 else
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001764 *ret_data = QOBJECT(qint_from_int((int)(actual >> 20)));
aliguoridf751fa2008-12-04 20:19:35 +00001765}
1766
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001767static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001768{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001769 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001770
aliguori76655d62009-03-06 20:27:37 +00001771 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001772 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001773 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001774 return acl;
1775}
aliguori76655d62009-03-06 20:27:37 +00001776
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001777static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001778{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001779 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001780 qemu_acl *acl = find_acl(mon, aclname);
1781 qemu_acl_entry *entry;
1782 int i = 0;
1783
1784 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001785 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001786 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001787 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001788 i++;
1789 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001790 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001791 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001792 }
1793}
1794
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001795static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001796{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001797 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001798 qemu_acl *acl = find_acl(mon, aclname);
1799
1800 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001801 qemu_acl_reset(acl);
1802 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001803 }
1804}
aliguori76655d62009-03-06 20:27:37 +00001805
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001806static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001807{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001808 const char *aclname = qdict_get_str(qdict, "aclname");
1809 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001810 qemu_acl *acl = find_acl(mon, aclname);
1811
1812 if (acl) {
1813 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001814 acl->defaultDeny = 0;
1815 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001816 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001817 acl->defaultDeny = 1;
1818 monitor_printf(mon, "acl: policy set to 'deny'\n");
1819 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001820 monitor_printf(mon, "acl: unknown policy '%s', "
1821 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001822 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001823 }
1824}
aliguori76655d62009-03-06 20:27:37 +00001825
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001826static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001827{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001828 const char *aclname = qdict_get_str(qdict, "aclname");
1829 const char *match = qdict_get_str(qdict, "match");
1830 const char *policy = qdict_get_str(qdict, "policy");
1831 int has_index = qdict_haskey(qdict, "index");
1832 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001833 qemu_acl *acl = find_acl(mon, aclname);
1834 int deny, ret;
1835
1836 if (acl) {
1837 if (strcmp(policy, "allow") == 0) {
1838 deny = 0;
1839 } else if (strcmp(policy, "deny") == 0) {
1840 deny = 1;
1841 } else {
1842 monitor_printf(mon, "acl: unknown policy '%s', "
1843 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001844 return;
1845 }
aliguori28a76be2009-03-06 20:27:40 +00001846 if (has_index)
1847 ret = qemu_acl_insert(acl, deny, match, index);
1848 else
1849 ret = qemu_acl_append(acl, deny, match);
1850 if (ret < 0)
1851 monitor_printf(mon, "acl: unable to add acl entry\n");
1852 else
1853 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001854 }
1855}
aliguori76655d62009-03-06 20:27:37 +00001856
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001857static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001858{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001859 const char *aclname = qdict_get_str(qdict, "aclname");
1860 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001861 qemu_acl *acl = find_acl(mon, aclname);
1862 int ret;
aliguori76655d62009-03-06 20:27:37 +00001863
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001864 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001865 ret = qemu_acl_remove(acl, match);
1866 if (ret < 0)
1867 monitor_printf(mon, "acl: no matching acl entry\n");
1868 else
1869 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001870 }
1871}
1872
Huang Ying79c4f6b2009-06-23 10:05:14 +08001873#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001874static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001875{
1876 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001877 int cpu_index = qdict_get_int(qdict, "cpu_index");
1878 int bank = qdict_get_int(qdict, "bank");
1879 uint64_t status = qdict_get_int(qdict, "status");
1880 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1881 uint64_t addr = qdict_get_int(qdict, "addr");
1882 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08001883
1884 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1885 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1886 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1887 break;
1888 }
1889}
1890#endif
1891
Luiz Capitulinof0d60002009-10-16 12:23:50 -03001892static void do_getfd(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 int fd;
1897
1898 fd = qemu_chr_get_msgfd(mon->chr);
1899 if (fd == -1) {
1900 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1901 return;
1902 }
1903
1904 if (qemu_isdigit(fdname[0])) {
1905 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1906 return;
1907 }
1908
1909 fd = dup(fd);
1910 if (fd == -1) {
1911 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1912 strerror(errno));
1913 return;
1914 }
1915
Blue Swirl72cf2d42009-09-12 07:36:22 +00001916 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001917 if (strcmp(monfd->name, fdname) != 0) {
1918 continue;
1919 }
1920
1921 close(monfd->fd);
1922 monfd->fd = fd;
1923 return;
1924 }
1925
Anthony Liguoric227f092009-10-01 16:12:16 -05001926 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001927 monfd->name = qemu_strdup(fdname);
1928 monfd->fd = fd;
1929
Blue Swirl72cf2d42009-09-12 07:36:22 +00001930 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001931}
1932
Luiz Capitulino18f3a512009-10-16 12:23:51 -03001933static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001934{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001935 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001936 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001937
Blue Swirl72cf2d42009-09-12 07:36:22 +00001938 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001939 if (strcmp(monfd->name, fdname) != 0) {
1940 continue;
1941 }
1942
Blue Swirl72cf2d42009-09-12 07:36:22 +00001943 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001944 close(monfd->fd);
1945 qemu_free(monfd->name);
1946 qemu_free(monfd);
1947 return;
1948 }
1949
1950 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1951 fdname);
1952}
1953
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001954static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001955{
1956 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001957 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001958
1959 vm_stop(0);
1960
Juan Quintela05f24012009-08-20 19:42:22 +02001961 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001962 vm_start();
1963}
1964
Mark McLoughlin7768e042009-07-22 09:11:41 +01001965int monitor_get_fd(Monitor *mon, const char *fdname)
1966{
Anthony Liguoric227f092009-10-01 16:12:16 -05001967 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001968
Blue Swirl72cf2d42009-09-12 07:36:22 +00001969 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001970 int fd;
1971
1972 if (strcmp(monfd->name, fdname) != 0) {
1973 continue;
1974 }
1975
1976 fd = monfd->fd;
1977
1978 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001979 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001980 qemu_free(monfd->name);
1981 qemu_free(monfd);
1982
1983 return fd;
1984 }
1985
1986 return -1;
1987}
1988
Anthony Liguoric227f092009-10-01 16:12:16 -05001989static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001990#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001991 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001992};
1993
Blue Swirl23130862009-06-06 08:22:04 +00001994/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05001995static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001996 {
1997 .name = "version",
1998 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001999 .params = "",
2000 .help = "show the version of QEMU",
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002001 .user_print = monitor_print_qobject,
2002 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002003 },
2004 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002005 .name = "commands",
2006 .args_type = "",
2007 .params = "",
2008 .help = "list QMP available commands",
2009 .user_print = monitor_user_noop,
2010 .mhandler.info_new = do_info_commands,
2011 },
2012 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002013 .name = "network",
2014 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002015 .params = "",
2016 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002017 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002018 },
2019 {
2020 .name = "chardev",
2021 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002022 .params = "",
2023 .help = "show the character devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002024 .mhandler.info = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002025 },
2026 {
2027 .name = "block",
2028 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002029 .params = "",
2030 .help = "show the block devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002031 .mhandler.info = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002032 },
2033 {
2034 .name = "blockstats",
2035 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002036 .params = "",
2037 .help = "show block device statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002038 .mhandler.info = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002039 },
2040 {
2041 .name = "registers",
2042 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002043 .params = "",
2044 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002045 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002046 },
2047 {
2048 .name = "cpus",
2049 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002050 .params = "",
2051 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002052 .user_print = monitor_print_cpus,
2053 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002054 },
2055 {
2056 .name = "history",
2057 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002058 .params = "",
2059 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002060 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002061 },
2062 {
2063 .name = "irq",
2064 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002065 .params = "",
2066 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002067 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002068 },
2069 {
2070 .name = "pic",
2071 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002072 .params = "",
2073 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002074 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002075 },
2076 {
2077 .name = "pci",
2078 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002079 .params = "",
2080 .help = "show PCI info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002081 .mhandler.info = pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002082 },
aurel327c664e22009-03-03 06:12:22 +00002083#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002084 {
2085 .name = "tlb",
2086 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002087 .params = "",
2088 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002089 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002090 },
aurel327c664e22009-03-03 06:12:22 +00002091#endif
2092#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002093 {
2094 .name = "mem",
2095 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002096 .params = "",
2097 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002098 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002099 },
2100 {
2101 .name = "hpet",
2102 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002103 .params = "",
2104 .help = "show state of HPET",
Luiz Capitulino910df892009-10-07 13:41:51 -03002105 .mhandler.info = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002106 },
bellardb86bda52004-09-18 19:32:46 +00002107#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002108 {
2109 .name = "jit",
2110 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002111 .params = "",
2112 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002113 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002114 },
2115 {
2116 .name = "kvm",
2117 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002118 .params = "",
2119 .help = "show KVM information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002120 .mhandler.info = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002121 },
2122 {
2123 .name = "numa",
2124 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002125 .params = "",
2126 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002127 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002128 },
2129 {
2130 .name = "usb",
2131 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002132 .params = "",
2133 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002134 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002135 },
2136 {
2137 .name = "usbhost",
2138 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002139 .params = "",
2140 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002141 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002142 },
2143 {
2144 .name = "profile",
2145 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002146 .params = "",
2147 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002148 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002149 },
2150 {
2151 .name = "capture",
2152 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002153 .params = "",
2154 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002155 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002156 },
2157 {
2158 .name = "snapshots",
2159 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002160 .params = "",
2161 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002162 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002163 },
2164 {
2165 .name = "status",
2166 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002167 .params = "",
2168 .help = "show the current VM status (running|paused)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002169 .mhandler.info = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002170 },
2171 {
2172 .name = "pcmcia",
2173 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002174 .params = "",
2175 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002176 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002177 },
2178 {
2179 .name = "mice",
2180 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002181 .params = "",
2182 .help = "show which guest mouse is receiving events",
Luiz Capitulino910df892009-10-07 13:41:51 -03002183 .mhandler.info = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002184 },
2185 {
2186 .name = "vnc",
2187 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002188 .params = "",
2189 .help = "show the vnc server status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002190 .mhandler.info = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002191 },
2192 {
2193 .name = "name",
2194 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002195 .params = "",
2196 .help = "show the current VM name",
Luiz Capitulino910df892009-10-07 13:41:51 -03002197 .mhandler.info = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002198 },
2199 {
2200 .name = "uuid",
2201 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002202 .params = "",
2203 .help = "show the current VM UUID",
Luiz Capitulino910df892009-10-07 13:41:51 -03002204 .mhandler.info = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002205 },
j_mayer76a66252007-03-07 08:32:30 +00002206#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002207 {
2208 .name = "cpustats",
2209 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002210 .params = "",
2211 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002212 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002213 },
j_mayer76a66252007-03-07 08:32:30 +00002214#endif
blueswir131a60e22007-10-26 18:42:59 +00002215#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002216 {
2217 .name = "usernet",
2218 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002219 .params = "",
2220 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002221 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002222 },
blueswir131a60e22007-10-26 18:42:59 +00002223#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002224 {
2225 .name = "migrate",
2226 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002227 .params = "",
2228 .help = "show migration status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002229 .mhandler.info = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002230 },
2231 {
2232 .name = "balloon",
2233 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002234 .params = "",
2235 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002236 .user_print = monitor_print_balloon,
2237 .mhandler.info_new = do_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002238 },
2239 {
2240 .name = "qtree",
2241 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002242 .params = "",
2243 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002244 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002245 },
2246 {
2247 .name = "qdm",
2248 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002249 .params = "",
2250 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002251 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002252 },
2253 {
2254 .name = "roms",
2255 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002256 .params = "",
2257 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002258 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002259 },
2260 {
2261 .name = NULL,
2262 },
bellard9dc39cb2004-03-14 21:38:27 +00002263};
2264
bellard9307c4c2004-04-04 12:57:25 +00002265/*******************************************************************/
2266
2267static const char *pch;
2268static jmp_buf expr_env;
2269
bellard92a31b12005-02-10 22:00:52 +00002270#define MD_TLONG 0
2271#define MD_I32 1
2272
bellard9307c4c2004-04-04 12:57:25 +00002273typedef struct MonitorDef {
2274 const char *name;
2275 int offset;
blueswir18662d652008-10-02 18:32:44 +00002276 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002277 int type;
bellard9307c4c2004-04-04 12:57:25 +00002278} MonitorDef;
2279
bellard57206fd2004-04-25 18:54:52 +00002280#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002281static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002282{
bellard6a00d602005-11-21 23:25:50 +00002283 CPUState *env = mon_get_cpu();
2284 if (!env)
2285 return 0;
2286 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002287}
2288#endif
2289
bellarda541f292004-04-12 20:39:29 +00002290#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002291static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002292{
bellard6a00d602005-11-21 23:25:50 +00002293 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002294 unsigned int u;
2295 int i;
2296
bellard6a00d602005-11-21 23:25:50 +00002297 if (!env)
2298 return 0;
2299
bellarda541f292004-04-12 20:39:29 +00002300 u = 0;
2301 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002302 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002303
2304 return u;
2305}
2306
blueswir18662d652008-10-02 18:32:44 +00002307static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002308{
bellard6a00d602005-11-21 23:25:50 +00002309 CPUState *env = mon_get_cpu();
2310 if (!env)
2311 return 0;
j_mayer0411a972007-10-25 21:35:50 +00002312 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002313}
2314
blueswir18662d652008-10-02 18:32:44 +00002315static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002316{
bellard6a00d602005-11-21 23:25:50 +00002317 CPUState *env = mon_get_cpu();
2318 if (!env)
2319 return 0;
aurel323d7b4172008-10-21 11:28:46 +00002320 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002321}
bellard9fddaa02004-05-21 12:59:32 +00002322
blueswir18662d652008-10-02 18:32:44 +00002323static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002324{
bellard6a00d602005-11-21 23:25:50 +00002325 CPUState *env = mon_get_cpu();
2326 if (!env)
2327 return 0;
2328 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002329}
2330
blueswir18662d652008-10-02 18:32:44 +00002331static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002332{
bellard6a00d602005-11-21 23:25:50 +00002333 CPUState *env = mon_get_cpu();
2334 if (!env)
2335 return 0;
2336 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002337}
2338
blueswir18662d652008-10-02 18:32:44 +00002339static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002340{
bellard6a00d602005-11-21 23:25:50 +00002341 CPUState *env = mon_get_cpu();
2342 if (!env)
2343 return 0;
2344 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002345}
bellarda541f292004-04-12 20:39:29 +00002346#endif
2347
bellarde95c8d52004-09-30 22:22:08 +00002348#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002349#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002350static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002351{
bellard6a00d602005-11-21 23:25:50 +00002352 CPUState *env = mon_get_cpu();
2353 if (!env)
2354 return 0;
2355 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002356}
bellard7b936c02005-10-30 17:05:13 +00002357#endif
bellarde95c8d52004-09-30 22:22:08 +00002358
blueswir18662d652008-10-02 18:32:44 +00002359static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002360{
bellard6a00d602005-11-21 23:25:50 +00002361 CPUState *env = mon_get_cpu();
2362 if (!env)
2363 return 0;
2364 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002365}
2366#endif
2367
blueswir18662d652008-10-02 18:32:44 +00002368static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002369#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002370
2371#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002372 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002373 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002374 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002375
bellard9307c4c2004-04-04 12:57:25 +00002376 { "eax", offsetof(CPUState, regs[0]) },
2377 { "ecx", offsetof(CPUState, regs[1]) },
2378 { "edx", offsetof(CPUState, regs[2]) },
2379 { "ebx", offsetof(CPUState, regs[3]) },
2380 { "esp|sp", offsetof(CPUState, regs[4]) },
2381 { "ebp|fp", offsetof(CPUState, regs[5]) },
2382 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002383 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002384#ifdef TARGET_X86_64
2385 { "r8", offsetof(CPUState, regs[8]) },
2386 { "r9", offsetof(CPUState, regs[9]) },
2387 { "r10", offsetof(CPUState, regs[10]) },
2388 { "r11", offsetof(CPUState, regs[11]) },
2389 { "r12", offsetof(CPUState, regs[12]) },
2390 { "r13", offsetof(CPUState, regs[13]) },
2391 { "r14", offsetof(CPUState, regs[14]) },
2392 { "r15", offsetof(CPUState, regs[15]) },
2393#endif
bellard9307c4c2004-04-04 12:57:25 +00002394 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002395 { "eip", offsetof(CPUState, eip) },
2396 SEG("cs", R_CS)
2397 SEG("ds", R_DS)
2398 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002399 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002400 SEG("fs", R_FS)
2401 SEG("gs", R_GS)
2402 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002403#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002404 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002405 { "r0", offsetof(CPUState, gpr[0]) },
2406 { "r1", offsetof(CPUState, gpr[1]) },
2407 { "r2", offsetof(CPUState, gpr[2]) },
2408 { "r3", offsetof(CPUState, gpr[3]) },
2409 { "r4", offsetof(CPUState, gpr[4]) },
2410 { "r5", offsetof(CPUState, gpr[5]) },
2411 { "r6", offsetof(CPUState, gpr[6]) },
2412 { "r7", offsetof(CPUState, gpr[7]) },
2413 { "r8", offsetof(CPUState, gpr[8]) },
2414 { "r9", offsetof(CPUState, gpr[9]) },
2415 { "r10", offsetof(CPUState, gpr[10]) },
2416 { "r11", offsetof(CPUState, gpr[11]) },
2417 { "r12", offsetof(CPUState, gpr[12]) },
2418 { "r13", offsetof(CPUState, gpr[13]) },
2419 { "r14", offsetof(CPUState, gpr[14]) },
2420 { "r15", offsetof(CPUState, gpr[15]) },
2421 { "r16", offsetof(CPUState, gpr[16]) },
2422 { "r17", offsetof(CPUState, gpr[17]) },
2423 { "r18", offsetof(CPUState, gpr[18]) },
2424 { "r19", offsetof(CPUState, gpr[19]) },
2425 { "r20", offsetof(CPUState, gpr[20]) },
2426 { "r21", offsetof(CPUState, gpr[21]) },
2427 { "r22", offsetof(CPUState, gpr[22]) },
2428 { "r23", offsetof(CPUState, gpr[23]) },
2429 { "r24", offsetof(CPUState, gpr[24]) },
2430 { "r25", offsetof(CPUState, gpr[25]) },
2431 { "r26", offsetof(CPUState, gpr[26]) },
2432 { "r27", offsetof(CPUState, gpr[27]) },
2433 { "r28", offsetof(CPUState, gpr[28]) },
2434 { "r29", offsetof(CPUState, gpr[29]) },
2435 { "r30", offsetof(CPUState, gpr[30]) },
2436 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002437 /* Floating point registers */
2438 { "f0", offsetof(CPUState, fpr[0]) },
2439 { "f1", offsetof(CPUState, fpr[1]) },
2440 { "f2", offsetof(CPUState, fpr[2]) },
2441 { "f3", offsetof(CPUState, fpr[3]) },
2442 { "f4", offsetof(CPUState, fpr[4]) },
2443 { "f5", offsetof(CPUState, fpr[5]) },
2444 { "f6", offsetof(CPUState, fpr[6]) },
2445 { "f7", offsetof(CPUState, fpr[7]) },
2446 { "f8", offsetof(CPUState, fpr[8]) },
2447 { "f9", offsetof(CPUState, fpr[9]) },
2448 { "f10", offsetof(CPUState, fpr[10]) },
2449 { "f11", offsetof(CPUState, fpr[11]) },
2450 { "f12", offsetof(CPUState, fpr[12]) },
2451 { "f13", offsetof(CPUState, fpr[13]) },
2452 { "f14", offsetof(CPUState, fpr[14]) },
2453 { "f15", offsetof(CPUState, fpr[15]) },
2454 { "f16", offsetof(CPUState, fpr[16]) },
2455 { "f17", offsetof(CPUState, fpr[17]) },
2456 { "f18", offsetof(CPUState, fpr[18]) },
2457 { "f19", offsetof(CPUState, fpr[19]) },
2458 { "f20", offsetof(CPUState, fpr[20]) },
2459 { "f21", offsetof(CPUState, fpr[21]) },
2460 { "f22", offsetof(CPUState, fpr[22]) },
2461 { "f23", offsetof(CPUState, fpr[23]) },
2462 { "f24", offsetof(CPUState, fpr[24]) },
2463 { "f25", offsetof(CPUState, fpr[25]) },
2464 { "f26", offsetof(CPUState, fpr[26]) },
2465 { "f27", offsetof(CPUState, fpr[27]) },
2466 { "f28", offsetof(CPUState, fpr[28]) },
2467 { "f29", offsetof(CPUState, fpr[29]) },
2468 { "f30", offsetof(CPUState, fpr[30]) },
2469 { "f31", offsetof(CPUState, fpr[31]) },
2470 { "fpscr", offsetof(CPUState, fpscr) },
2471 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002472 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002473 { "lr", offsetof(CPUState, lr) },
2474 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002475 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002476 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002477 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002478 { "msr", 0, &monitor_get_msr, },
2479 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002480 { "tbu", 0, &monitor_get_tbu, },
2481 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002482#if defined(TARGET_PPC64)
2483 /* Address space register */
2484 { "asr", offsetof(CPUState, asr) },
2485#endif
2486 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002487 { "sdr1", offsetof(CPUState, sdr1) },
2488 { "sr0", offsetof(CPUState, sr[0]) },
2489 { "sr1", offsetof(CPUState, sr[1]) },
2490 { "sr2", offsetof(CPUState, sr[2]) },
2491 { "sr3", offsetof(CPUState, sr[3]) },
2492 { "sr4", offsetof(CPUState, sr[4]) },
2493 { "sr5", offsetof(CPUState, sr[5]) },
2494 { "sr6", offsetof(CPUState, sr[6]) },
2495 { "sr7", offsetof(CPUState, sr[7]) },
2496 { "sr8", offsetof(CPUState, sr[8]) },
2497 { "sr9", offsetof(CPUState, sr[9]) },
2498 { "sr10", offsetof(CPUState, sr[10]) },
2499 { "sr11", offsetof(CPUState, sr[11]) },
2500 { "sr12", offsetof(CPUState, sr[12]) },
2501 { "sr13", offsetof(CPUState, sr[13]) },
2502 { "sr14", offsetof(CPUState, sr[14]) },
2503 { "sr15", offsetof(CPUState, sr[15]) },
2504 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002505#elif defined(TARGET_SPARC)
2506 { "g0", offsetof(CPUState, gregs[0]) },
2507 { "g1", offsetof(CPUState, gregs[1]) },
2508 { "g2", offsetof(CPUState, gregs[2]) },
2509 { "g3", offsetof(CPUState, gregs[3]) },
2510 { "g4", offsetof(CPUState, gregs[4]) },
2511 { "g5", offsetof(CPUState, gregs[5]) },
2512 { "g6", offsetof(CPUState, gregs[6]) },
2513 { "g7", offsetof(CPUState, gregs[7]) },
2514 { "o0", 0, monitor_get_reg },
2515 { "o1", 1, monitor_get_reg },
2516 { "o2", 2, monitor_get_reg },
2517 { "o3", 3, monitor_get_reg },
2518 { "o4", 4, monitor_get_reg },
2519 { "o5", 5, monitor_get_reg },
2520 { "o6", 6, monitor_get_reg },
2521 { "o7", 7, monitor_get_reg },
2522 { "l0", 8, monitor_get_reg },
2523 { "l1", 9, monitor_get_reg },
2524 { "l2", 10, monitor_get_reg },
2525 { "l3", 11, monitor_get_reg },
2526 { "l4", 12, monitor_get_reg },
2527 { "l5", 13, monitor_get_reg },
2528 { "l6", 14, monitor_get_reg },
2529 { "l7", 15, monitor_get_reg },
2530 { "i0", 16, monitor_get_reg },
2531 { "i1", 17, monitor_get_reg },
2532 { "i2", 18, monitor_get_reg },
2533 { "i3", 19, monitor_get_reg },
2534 { "i4", 20, monitor_get_reg },
2535 { "i5", 21, monitor_get_reg },
2536 { "i6", 22, monitor_get_reg },
2537 { "i7", 23, monitor_get_reg },
2538 { "pc", offsetof(CPUState, pc) },
2539 { "npc", offsetof(CPUState, npc) },
2540 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002541#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002542 { "psr", 0, &monitor_get_psr, },
2543 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002544#endif
bellarde95c8d52004-09-30 22:22:08 +00002545 { "tbr", offsetof(CPUState, tbr) },
2546 { "fsr", offsetof(CPUState, fsr) },
2547 { "f0", offsetof(CPUState, fpr[0]) },
2548 { "f1", offsetof(CPUState, fpr[1]) },
2549 { "f2", offsetof(CPUState, fpr[2]) },
2550 { "f3", offsetof(CPUState, fpr[3]) },
2551 { "f4", offsetof(CPUState, fpr[4]) },
2552 { "f5", offsetof(CPUState, fpr[5]) },
2553 { "f6", offsetof(CPUState, fpr[6]) },
2554 { "f7", offsetof(CPUState, fpr[7]) },
2555 { "f8", offsetof(CPUState, fpr[8]) },
2556 { "f9", offsetof(CPUState, fpr[9]) },
2557 { "f10", offsetof(CPUState, fpr[10]) },
2558 { "f11", offsetof(CPUState, fpr[11]) },
2559 { "f12", offsetof(CPUState, fpr[12]) },
2560 { "f13", offsetof(CPUState, fpr[13]) },
2561 { "f14", offsetof(CPUState, fpr[14]) },
2562 { "f15", offsetof(CPUState, fpr[15]) },
2563 { "f16", offsetof(CPUState, fpr[16]) },
2564 { "f17", offsetof(CPUState, fpr[17]) },
2565 { "f18", offsetof(CPUState, fpr[18]) },
2566 { "f19", offsetof(CPUState, fpr[19]) },
2567 { "f20", offsetof(CPUState, fpr[20]) },
2568 { "f21", offsetof(CPUState, fpr[21]) },
2569 { "f22", offsetof(CPUState, fpr[22]) },
2570 { "f23", offsetof(CPUState, fpr[23]) },
2571 { "f24", offsetof(CPUState, fpr[24]) },
2572 { "f25", offsetof(CPUState, fpr[25]) },
2573 { "f26", offsetof(CPUState, fpr[26]) },
2574 { "f27", offsetof(CPUState, fpr[27]) },
2575 { "f28", offsetof(CPUState, fpr[28]) },
2576 { "f29", offsetof(CPUState, fpr[29]) },
2577 { "f30", offsetof(CPUState, fpr[30]) },
2578 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002579#ifdef TARGET_SPARC64
2580 { "f32", offsetof(CPUState, fpr[32]) },
2581 { "f34", offsetof(CPUState, fpr[34]) },
2582 { "f36", offsetof(CPUState, fpr[36]) },
2583 { "f38", offsetof(CPUState, fpr[38]) },
2584 { "f40", offsetof(CPUState, fpr[40]) },
2585 { "f42", offsetof(CPUState, fpr[42]) },
2586 { "f44", offsetof(CPUState, fpr[44]) },
2587 { "f46", offsetof(CPUState, fpr[46]) },
2588 { "f48", offsetof(CPUState, fpr[48]) },
2589 { "f50", offsetof(CPUState, fpr[50]) },
2590 { "f52", offsetof(CPUState, fpr[52]) },
2591 { "f54", offsetof(CPUState, fpr[54]) },
2592 { "f56", offsetof(CPUState, fpr[56]) },
2593 { "f58", offsetof(CPUState, fpr[58]) },
2594 { "f60", offsetof(CPUState, fpr[60]) },
2595 { "f62", offsetof(CPUState, fpr[62]) },
2596 { "asi", offsetof(CPUState, asi) },
2597 { "pstate", offsetof(CPUState, pstate) },
2598 { "cansave", offsetof(CPUState, cansave) },
2599 { "canrestore", offsetof(CPUState, canrestore) },
2600 { "otherwin", offsetof(CPUState, otherwin) },
2601 { "wstate", offsetof(CPUState, wstate) },
2602 { "cleanwin", offsetof(CPUState, cleanwin) },
2603 { "fprs", offsetof(CPUState, fprs) },
2604#endif
bellard9307c4c2004-04-04 12:57:25 +00002605#endif
2606 { NULL },
2607};
2608
aliguori376253e2009-03-05 23:01:23 +00002609static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002610{
aliguori376253e2009-03-05 23:01:23 +00002611 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002612 longjmp(expr_env, 1);
2613}
2614
bellard6a00d602005-11-21 23:25:50 +00002615/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002616static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002617{
blueswir18662d652008-10-02 18:32:44 +00002618 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002619 void *ptr;
2620
bellard9307c4c2004-04-04 12:57:25 +00002621 for(md = monitor_defs; md->name != NULL; md++) {
2622 if (compare_cmd(name, md->name)) {
2623 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002624 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002625 } else {
bellard6a00d602005-11-21 23:25:50 +00002626 CPUState *env = mon_get_cpu();
2627 if (!env)
2628 return -2;
2629 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002630 switch(md->type) {
2631 case MD_I32:
2632 *pval = *(int32_t *)ptr;
2633 break;
2634 case MD_TLONG:
2635 *pval = *(target_long *)ptr;
2636 break;
2637 default:
2638 *pval = 0;
2639 break;
2640 }
bellard9307c4c2004-04-04 12:57:25 +00002641 }
2642 return 0;
2643 }
2644 }
2645 return -1;
2646}
2647
2648static void next(void)
2649{
Blue Swirl660f11b2009-07-31 21:16:51 +00002650 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002651 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002652 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002653 pch++;
2654 }
2655}
2656
aliguori376253e2009-03-05 23:01:23 +00002657static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002658
aliguori376253e2009-03-05 23:01:23 +00002659static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002660{
blueswir1c2efc952007-09-25 17:28:42 +00002661 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002662 char *p;
bellard6a00d602005-11-21 23:25:50 +00002663 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002664
2665 switch(*pch) {
2666 case '+':
2667 next();
aliguori376253e2009-03-05 23:01:23 +00002668 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002669 break;
2670 case '-':
2671 next();
aliguori376253e2009-03-05 23:01:23 +00002672 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002673 break;
2674 case '~':
2675 next();
aliguori376253e2009-03-05 23:01:23 +00002676 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002677 break;
2678 case '(':
2679 next();
aliguori376253e2009-03-05 23:01:23 +00002680 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002681 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002682 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002683 }
2684 next();
2685 break;
bellard81d09122004-07-14 17:21:37 +00002686 case '\'':
2687 pch++;
2688 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002689 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002690 n = *pch;
2691 pch++;
2692 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002693 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002694 next();
2695 break;
bellard9307c4c2004-04-04 12:57:25 +00002696 case '$':
2697 {
2698 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002699 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002700
bellard9307c4c2004-04-04 12:57:25 +00002701 pch++;
2702 q = buf;
2703 while ((*pch >= 'a' && *pch <= 'z') ||
2704 (*pch >= 'A' && *pch <= 'Z') ||
2705 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002706 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002707 if ((q - buf) < sizeof(buf) - 1)
2708 *q++ = *pch;
2709 pch++;
2710 }
blueswir1cd390082008-11-16 13:53:32 +00002711 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002712 pch++;
2713 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002714 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002715 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002716 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002717 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002718 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002719 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002720 }
2721 break;
2722 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002723 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002724 n = 0;
2725 break;
2726 default:
blueswir17743e582007-09-24 18:39:04 +00002727#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002728 n = strtoull(pch, &p, 0);
2729#else
bellard9307c4c2004-04-04 12:57:25 +00002730 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002731#endif
bellard9307c4c2004-04-04 12:57:25 +00002732 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002733 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002734 }
2735 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002736 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002737 pch++;
2738 break;
2739 }
2740 return n;
2741}
2742
2743
aliguori376253e2009-03-05 23:01:23 +00002744static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002745{
blueswir1c2efc952007-09-25 17:28:42 +00002746 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002747 int op;
ths3b46e622007-09-17 08:09:54 +00002748
aliguori376253e2009-03-05 23:01:23 +00002749 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002750 for(;;) {
2751 op = *pch;
2752 if (op != '*' && op != '/' && op != '%')
2753 break;
2754 next();
aliguori376253e2009-03-05 23:01:23 +00002755 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002756 switch(op) {
2757 default:
2758 case '*':
2759 val *= val2;
2760 break;
2761 case '/':
2762 case '%':
ths5fafdf22007-09-16 21:08:06 +00002763 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002764 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002765 if (op == '/')
2766 val /= val2;
2767 else
2768 val %= val2;
2769 break;
2770 }
2771 }
2772 return val;
2773}
2774
aliguori376253e2009-03-05 23:01:23 +00002775static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002776{
blueswir1c2efc952007-09-25 17:28:42 +00002777 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002778 int op;
bellard9307c4c2004-04-04 12:57:25 +00002779
aliguori376253e2009-03-05 23:01:23 +00002780 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002781 for(;;) {
2782 op = *pch;
2783 if (op != '&' && op != '|' && op != '^')
2784 break;
2785 next();
aliguori376253e2009-03-05 23:01:23 +00002786 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002787 switch(op) {
2788 default:
2789 case '&':
2790 val &= val2;
2791 break;
2792 case '|':
2793 val |= val2;
2794 break;
2795 case '^':
2796 val ^= val2;
2797 break;
2798 }
2799 }
2800 return val;
2801}
2802
aliguori376253e2009-03-05 23:01:23 +00002803static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002804{
blueswir1c2efc952007-09-25 17:28:42 +00002805 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002806 int op;
bellard9307c4c2004-04-04 12:57:25 +00002807
aliguori376253e2009-03-05 23:01:23 +00002808 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002809 for(;;) {
2810 op = *pch;
2811 if (op != '+' && op != '-')
2812 break;
2813 next();
aliguori376253e2009-03-05 23:01:23 +00002814 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002815 if (op == '+')
2816 val += val2;
2817 else
2818 val -= val2;
2819 }
2820 return val;
2821}
2822
aliguori376253e2009-03-05 23:01:23 +00002823static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002824{
2825 pch = *pp;
2826 if (setjmp(expr_env)) {
2827 *pp = pch;
2828 return -1;
2829 }
blueswir1cd390082008-11-16 13:53:32 +00002830 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002831 pch++;
aliguori376253e2009-03-05 23:01:23 +00002832 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002833 *pp = pch;
2834 return 0;
2835}
2836
2837static int get_str(char *buf, int buf_size, const char **pp)
2838{
2839 const char *p;
2840 char *q;
2841 int c;
2842
bellard81d09122004-07-14 17:21:37 +00002843 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002844 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002845 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002846 p++;
2847 if (*p == '\0') {
2848 fail:
bellard81d09122004-07-14 17:21:37 +00002849 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002850 *pp = p;
2851 return -1;
2852 }
bellard9307c4c2004-04-04 12:57:25 +00002853 if (*p == '\"') {
2854 p++;
2855 while (*p != '\0' && *p != '\"') {
2856 if (*p == '\\') {
2857 p++;
2858 c = *p++;
2859 switch(c) {
2860 case 'n':
2861 c = '\n';
2862 break;
2863 case 'r':
2864 c = '\r';
2865 break;
2866 case '\\':
2867 case '\'':
2868 case '\"':
2869 break;
2870 default:
2871 qemu_printf("unsupported escape code: '\\%c'\n", c);
2872 goto fail;
2873 }
2874 if ((q - buf) < buf_size - 1) {
2875 *q++ = c;
2876 }
2877 } else {
2878 if ((q - buf) < buf_size - 1) {
2879 *q++ = *p;
2880 }
2881 p++;
2882 }
2883 }
2884 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002885 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002886 goto fail;
2887 }
2888 p++;
2889 } else {
blueswir1cd390082008-11-16 13:53:32 +00002890 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002891 if ((q - buf) < buf_size - 1) {
2892 *q++ = *p;
2893 }
2894 p++;
2895 }
bellard9307c4c2004-04-04 12:57:25 +00002896 }
bellard81d09122004-07-14 17:21:37 +00002897 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002898 *pp = p;
2899 return 0;
2900}
2901
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002902/*
2903 * Store the command-name in cmdname, and return a pointer to
2904 * the remaining of the command string.
2905 */
2906static const char *get_command_name(const char *cmdline,
2907 char *cmdname, size_t nlen)
2908{
2909 size_t len;
2910 const char *p, *pstart;
2911
2912 p = cmdline;
2913 while (qemu_isspace(*p))
2914 p++;
2915 if (*p == '\0')
2916 return NULL;
2917 pstart = p;
2918 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2919 p++;
2920 len = p - pstart;
2921 if (len > nlen - 1)
2922 len = nlen - 1;
2923 memcpy(cmdname, pstart, len);
2924 cmdname[len] = '\0';
2925 return p;
2926}
2927
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002928/**
2929 * Read key of 'type' into 'key' and return the current
2930 * 'type' pointer.
2931 */
2932static char *key_get_info(const char *type, char **key)
2933{
2934 size_t len;
2935 char *p, *str;
2936
2937 if (*type == ',')
2938 type++;
2939
2940 p = strchr(type, ':');
2941 if (!p) {
2942 *key = NULL;
2943 return NULL;
2944 }
2945 len = p - type;
2946
2947 str = qemu_malloc(len + 1);
2948 memcpy(str, type, len);
2949 str[len] = '\0';
2950
2951 *key = str;
2952 return ++p;
2953}
2954
bellard9307c4c2004-04-04 12:57:25 +00002955static int default_fmt_format = 'x';
2956static int default_fmt_size = 4;
2957
2958#define MAX_ARGS 16
2959
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002960static int is_valid_option(const char *c, const char *typestr)
2961{
2962 char option[3];
2963
2964 option[0] = '-';
2965 option[1] = *c;
2966 option[2] = '\0';
2967
2968 typestr = strstr(typestr, option);
2969 return (typestr != NULL);
2970}
2971
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002972static const mon_cmd_t *monitor_find_command(const char *cmdname)
2973{
2974 const mon_cmd_t *cmd;
2975
2976 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
2977 if (compare_cmd(cmdname, cmd->name)) {
2978 return cmd;
2979 }
2980 }
2981
2982 return NULL;
2983}
2984
Anthony Liguoric227f092009-10-01 16:12:16 -05002985static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002986 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002987 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00002988{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002989 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03002990 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05002991 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002992 char cmdname[256];
2993 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002994 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00002995
2996#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002997 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002998#endif
ths3b46e622007-09-17 08:09:54 +00002999
bellard9307c4c2004-04-04 12:57:25 +00003000 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003001 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3002 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003003 return NULL;
ths3b46e622007-09-17 08:09:54 +00003004
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003005 cmd = monitor_find_command(cmdname);
3006 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003007 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003008 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003009 }
bellard9307c4c2004-04-04 12:57:25 +00003010
bellard9307c4c2004-04-04 12:57:25 +00003011 /* parse the parameters */
3012 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003013 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003014 typestr = key_get_info(typestr, &key);
3015 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003016 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003017 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003018 typestr++;
3019 switch(c) {
3020 case 'F':
bellard81d09122004-07-14 17:21:37 +00003021 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003022 case 's':
3023 {
3024 int ret;
ths3b46e622007-09-17 08:09:54 +00003025
blueswir1cd390082008-11-16 13:53:32 +00003026 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003027 p++;
3028 if (*typestr == '?') {
3029 typestr++;
3030 if (*p == '\0') {
3031 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003032 break;
bellard9307c4c2004-04-04 12:57:25 +00003033 }
3034 }
3035 ret = get_str(buf, sizeof(buf), &p);
3036 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003037 switch(c) {
3038 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003039 monitor_printf(mon, "%s: filename expected\n",
3040 cmdname);
bellard81d09122004-07-14 17:21:37 +00003041 break;
3042 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003043 monitor_printf(mon, "%s: block device name expected\n",
3044 cmdname);
bellard81d09122004-07-14 17:21:37 +00003045 break;
3046 default:
aliguori376253e2009-03-05 23:01:23 +00003047 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003048 break;
3049 }
bellard9307c4c2004-04-04 12:57:25 +00003050 goto fail;
3051 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003052 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003053 }
3054 break;
3055 case '/':
3056 {
3057 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003058
blueswir1cd390082008-11-16 13:53:32 +00003059 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003060 p++;
3061 if (*p == '/') {
3062 /* format found */
3063 p++;
3064 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003065 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003066 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003067 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003068 count = count * 10 + (*p - '0');
3069 p++;
3070 }
3071 }
3072 size = -1;
3073 format = -1;
3074 for(;;) {
3075 switch(*p) {
3076 case 'o':
3077 case 'd':
3078 case 'u':
3079 case 'x':
3080 case 'i':
3081 case 'c':
3082 format = *p++;
3083 break;
3084 case 'b':
3085 size = 1;
3086 p++;
3087 break;
3088 case 'h':
3089 size = 2;
3090 p++;
3091 break;
3092 case 'w':
3093 size = 4;
3094 p++;
3095 break;
3096 case 'g':
3097 case 'L':
3098 size = 8;
3099 p++;
3100 break;
3101 default:
3102 goto next;
3103 }
3104 }
3105 next:
blueswir1cd390082008-11-16 13:53:32 +00003106 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003107 monitor_printf(mon, "invalid char in format: '%c'\n",
3108 *p);
bellard9307c4c2004-04-04 12:57:25 +00003109 goto fail;
3110 }
bellard9307c4c2004-04-04 12:57:25 +00003111 if (format < 0)
3112 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003113 if (format != 'i') {
3114 /* for 'i', not specifying a size gives -1 as size */
3115 if (size < 0)
3116 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003117 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003118 }
bellard9307c4c2004-04-04 12:57:25 +00003119 default_fmt_format = format;
3120 } else {
3121 count = 1;
3122 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003123 if (format != 'i') {
3124 size = default_fmt_size;
3125 } else {
3126 size = -1;
3127 }
bellard9307c4c2004-04-04 12:57:25 +00003128 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003129 qdict_put(qdict, "count", qint_from_int(count));
3130 qdict_put(qdict, "format", qint_from_int(format));
3131 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003132 }
3133 break;
3134 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003135 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00003136 {
blueswir1c2efc952007-09-25 17:28:42 +00003137 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003138
blueswir1cd390082008-11-16 13:53:32 +00003139 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003140 p++;
bellard34405572004-06-08 00:55:58 +00003141 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003142 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003143 if (*p == '\0') {
3144 typestr++;
3145 break;
3146 }
bellard34405572004-06-08 00:55:58 +00003147 } else {
3148 if (*p == '.') {
3149 p++;
blueswir1cd390082008-11-16 13:53:32 +00003150 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003151 p++;
bellard34405572004-06-08 00:55:58 +00003152 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003153 typestr++;
3154 break;
bellard34405572004-06-08 00:55:58 +00003155 }
3156 }
bellard13224a82006-07-14 22:03:35 +00003157 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003158 }
aliguori376253e2009-03-05 23:01:23 +00003159 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003160 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003161 /* Check if 'i' is greater than 32-bit */
3162 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3163 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3164 monitor_printf(mon, "integer is for 32-bit values\n");
3165 goto fail;
3166 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003167 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003168 }
3169 break;
3170 case '-':
3171 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003172 const char *tmp = p;
3173 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003174 /* option */
ths3b46e622007-09-17 08:09:54 +00003175
bellard9307c4c2004-04-04 12:57:25 +00003176 c = *typestr++;
3177 if (c == '\0')
3178 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003179 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003180 p++;
3181 has_option = 0;
3182 if (*p == '-') {
3183 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003184 if(c != *p) {
3185 if(!is_valid_option(p, typestr)) {
3186
3187 monitor_printf(mon, "%s: unsupported option -%c\n",
3188 cmdname, *p);
3189 goto fail;
3190 } else {
3191 skip_key = 1;
3192 }
bellard9307c4c2004-04-04 12:57:25 +00003193 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003194 if(skip_key) {
3195 p = tmp;
3196 } else {
3197 p++;
3198 has_option = 1;
3199 }
bellard9307c4c2004-04-04 12:57:25 +00003200 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003201 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003202 }
3203 break;
3204 default:
3205 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003206 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003207 goto fail;
3208 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003209 qemu_free(key);
3210 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003211 }
3212 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003213 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003214 p++;
3215 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003216 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3217 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003218 goto fail;
3219 }
3220
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003221 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003222
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003223fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003224 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003225 return NULL;
3226}
3227
Luiz Capitulino8204a912009-11-18 23:05:31 -02003228static void monitor_print_error(Monitor *mon)
3229{
3230 qerror_print(mon->error);
3231 QDECREF(mon->error);
3232 mon->error = NULL;
3233}
3234
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003235static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3236 const QDict *params)
3237{
3238 QObject *data = NULL;
3239
3240 cmd->mhandler.cmd_new(mon, params, &data);
3241 if (data)
3242 cmd->user_print(mon, data);
3243
3244 qobject_decref(data);
3245}
3246
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003247static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003248{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003249 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003250 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003251
3252 qdict = qdict_new();
3253
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003254 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003255 if (!cmd)
3256 goto out;
3257
3258 qemu_errors_to_mon(mon);
3259
3260 if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003261 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003262 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003263 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003264 }
3265
Luiz Capitulino8204a912009-11-18 23:05:31 -02003266 if (monitor_has_error(mon))
3267 monitor_print_error(mon);
3268
3269 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003270
3271out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003272 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003273}
3274
bellard81d09122004-07-14 17:21:37 +00003275static void cmd_completion(const char *name, const char *list)
3276{
3277 const char *p, *pstart;
3278 char cmd[128];
3279 int len;
3280
3281 p = list;
3282 for(;;) {
3283 pstart = p;
3284 p = strchr(p, '|');
3285 if (!p)
3286 p = pstart + strlen(pstart);
3287 len = p - pstart;
3288 if (len > sizeof(cmd) - 2)
3289 len = sizeof(cmd) - 2;
3290 memcpy(cmd, pstart, len);
3291 cmd[len] = '\0';
3292 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003293 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003294 }
3295 if (*p == '\0')
3296 break;
3297 p++;
3298 }
3299}
3300
3301static void file_completion(const char *input)
3302{
3303 DIR *ffs;
3304 struct dirent *d;
3305 char path[1024];
3306 char file[1024], file_prefix[1024];
3307 int input_path_len;
3308 const char *p;
3309
ths5fafdf22007-09-16 21:08:06 +00003310 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003311 if (!p) {
3312 input_path_len = 0;
3313 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003314 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003315 } else {
3316 input_path_len = p - input + 1;
3317 memcpy(path, input, input_path_len);
3318 if (input_path_len > sizeof(path) - 1)
3319 input_path_len = sizeof(path) - 1;
3320 path[input_path_len] = '\0';
3321 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3322 }
3323#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003324 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3325 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003326#endif
3327 ffs = opendir(path);
3328 if (!ffs)
3329 return;
3330 for(;;) {
3331 struct stat sb;
3332 d = readdir(ffs);
3333 if (!d)
3334 break;
3335 if (strstart(d->d_name, file_prefix, NULL)) {
3336 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003337 if (input_path_len < sizeof(file))
3338 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3339 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003340 /* stat the file to find out if it's a directory.
3341 * In that case add a slash to speed up typing long paths
3342 */
3343 stat(file, &sb);
3344 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003345 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003346 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003347 }
3348 }
3349 closedir(ffs);
3350}
3351
aliguori51de9762009-03-05 23:00:43 +00003352static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003353{
aliguori51de9762009-03-05 23:00:43 +00003354 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003355 const char *input = opaque;
3356
3357 if (input[0] == '\0' ||
3358 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003359 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003360 }
3361}
3362
3363/* NOTE: this parser is an approximate form of the real command parser */
3364static void parse_cmdline(const char *cmdline,
3365 int *pnb_args, char **args)
3366{
3367 const char *p;
3368 int nb_args, ret;
3369 char buf[1024];
3370
3371 p = cmdline;
3372 nb_args = 0;
3373 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003374 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003375 p++;
3376 if (*p == '\0')
3377 break;
3378 if (nb_args >= MAX_ARGS)
3379 break;
3380 ret = get_str(buf, sizeof(buf), &p);
3381 args[nb_args] = qemu_strdup(buf);
3382 nb_args++;
3383 if (ret < 0)
3384 break;
3385 }
3386 *pnb_args = nb_args;
3387}
3388
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003389static const char *next_arg_type(const char *typestr)
3390{
3391 const char *p = strchr(typestr, ':');
3392 return (p != NULL ? ++p : typestr);
3393}
3394
aliguori4c36ba32009-03-05 23:01:37 +00003395static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003396{
3397 const char *cmdname;
3398 char *args[MAX_ARGS];
3399 int nb_args, i, len;
3400 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003401 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003402 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003403
3404 parse_cmdline(cmdline, &nb_args, args);
3405#ifdef DEBUG_COMPLETION
3406 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003407 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003408 }
3409#endif
3410
3411 /* if the line ends with a space, it means we want to complete the
3412 next arg */
3413 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003414 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003415 if (nb_args >= MAX_ARGS)
3416 return;
3417 args[nb_args++] = qemu_strdup("");
3418 }
3419 if (nb_args <= 1) {
3420 /* command completion */
3421 if (nb_args == 0)
3422 cmdname = "";
3423 else
3424 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003425 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003426 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003427 cmd_completion(cmdname, cmd->name);
3428 }
3429 } else {
3430 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003431 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003432 if (compare_cmd(args[0], cmd->name))
3433 goto found;
3434 }
3435 return;
3436 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003437 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003438 for(i = 0; i < nb_args - 2; i++) {
3439 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003440 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003441 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003442 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003443 }
3444 }
3445 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003446 if (*ptype == '-' && ptype[1] != '\0') {
3447 ptype += 2;
3448 }
bellard81d09122004-07-14 17:21:37 +00003449 switch(*ptype) {
3450 case 'F':
3451 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003452 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003453 file_completion(str);
3454 break;
3455 case 'B':
3456 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003457 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003458 bdrv_iterate(block_completion_it, (void *)str);
3459 break;
bellard7fe48482004-10-09 18:08:01 +00003460 case 's':
3461 /* XXX: more generic ? */
3462 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003463 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003464 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3465 cmd_completion(str, cmd->name);
3466 }
bellard64866c32006-05-07 18:03:31 +00003467 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003468 char *sep = strrchr(str, '-');
3469 if (sep)
3470 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003471 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003472 for(key = key_defs; key->name != NULL; key++) {
3473 cmd_completion(str, key->name);
3474 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003475 } else if (!strcmp(cmd->name, "help|?")) {
3476 readline_set_completion_index(cur_mon->rs, strlen(str));
3477 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3478 cmd_completion(str, cmd->name);
3479 }
bellard7fe48482004-10-09 18:08:01 +00003480 }
3481 break;
bellard81d09122004-07-14 17:21:37 +00003482 default:
3483 break;
3484 }
3485 }
3486 for(i = 0; i < nb_args; i++)
3487 qemu_free(args[i]);
3488}
3489
aliguori731b0362009-03-05 23:01:42 +00003490static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003491{
aliguori731b0362009-03-05 23:01:42 +00003492 Monitor *mon = opaque;
3493
3494 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003495}
3496
aliguori731b0362009-03-05 23:01:42 +00003497static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003498{
aliguori731b0362009-03-05 23:01:42 +00003499 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003500 int i;
aliguori376253e2009-03-05 23:01:23 +00003501
aliguori731b0362009-03-05 23:01:42 +00003502 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003503
aliguoricde76ee2009-03-05 23:01:51 +00003504 if (cur_mon->rs) {
3505 for (i = 0; i < size; i++)
3506 readline_handle_byte(cur_mon->rs, buf[i]);
3507 } else {
3508 if (size == 0 || buf[size - 1] != 0)
3509 monitor_printf(cur_mon, "corrupted command\n");
3510 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003511 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003512 }
aliguori731b0362009-03-05 23:01:42 +00003513
3514 cur_mon = old_mon;
3515}
aliguorid8f44602008-10-06 13:52:44 +00003516
aliguori376253e2009-03-05 23:01:23 +00003517static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003518{
aliguori731b0362009-03-05 23:01:42 +00003519 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003520 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003521 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003522}
3523
aliguoricde76ee2009-03-05 23:01:51 +00003524int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003525{
aliguoricde76ee2009-03-05 23:01:51 +00003526 if (!mon->rs)
3527 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003528 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003529 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003530}
3531
aliguori376253e2009-03-05 23:01:23 +00003532void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003533{
aliguoricde76ee2009-03-05 23:01:51 +00003534 if (!mon->rs)
3535 return;
aliguori731b0362009-03-05 23:01:42 +00003536 if (--mon->suspend_cnt == 0)
3537 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003538}
3539
aliguori731b0362009-03-05 23:01:42 +00003540static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003541{
aliguori376253e2009-03-05 23:01:23 +00003542 Monitor *mon = opaque;
3543
aliguori2724b182009-03-05 23:01:47 +00003544 switch (event) {
3545 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003546 mon->mux_out = 0;
3547 if (mon->reset_seen) {
3548 readline_restart(mon->rs);
3549 monitor_resume(mon);
3550 monitor_flush(mon);
3551 } else {
3552 mon->suspend_cnt = 0;
3553 }
aliguori2724b182009-03-05 23:01:47 +00003554 break;
ths86e94de2007-01-05 22:01:59 +00003555
aliguori2724b182009-03-05 23:01:47 +00003556 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003557 if (mon->reset_seen) {
3558 if (mon->suspend_cnt == 0) {
3559 monitor_printf(mon, "\n");
3560 }
3561 monitor_flush(mon);
3562 monitor_suspend(mon);
3563 } else {
3564 mon->suspend_cnt++;
3565 }
3566 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00003567 break;
3568
Amit Shahb6b8df52009-10-07 18:31:16 +05303569 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003570 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3571 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003572 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00003573 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003574 }
3575 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00003576 break;
3577 }
ths86e94de2007-01-05 22:01:59 +00003578}
3579
aliguori76655d62009-03-06 20:27:37 +00003580
3581/*
3582 * Local variables:
3583 * c-indent-level: 4
3584 * c-basic-offset: 4
3585 * tab-width: 8
3586 * End:
3587 */
3588
Luiz Capitulinoadcb1812009-11-26 22:58:52 -02003589const char *monitor_cmdline_parse(const char *cmdline, int *flags)
3590{
3591 const char *dev;
3592
3593 if (strstart(cmdline, "control,", &dev)) {
3594 if (strstart(dev, "vc", NULL)) {
3595 fprintf(stderr, "qemu: control mode is for low-level interaction ");
3596 fprintf(stderr, "cannot be used with device 'vc'\n");
3597 exit(1);
3598 }
3599 *flags &= ~MONITOR_USE_READLINE;
3600 *flags |= MONITOR_USE_CONTROL;
3601 return dev;
3602 }
3603
3604 return cmdline;
3605}
3606
aliguori731b0362009-03-05 23:01:42 +00003607void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003608{
aliguori731b0362009-03-05 23:01:42 +00003609 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003610 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003611
3612 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003613 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003614 is_first_init = 0;
3615 }
aliguori87127162009-03-05 23:01:29 +00003616
3617 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003618
aliguori87127162009-03-05 23:01:29 +00003619 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003620 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00003621 if (flags & MONITOR_USE_READLINE) {
3622 mon->rs = readline_init(mon, monitor_find_completion);
3623 monitor_read_command(mon, 0);
3624 }
aliguori87127162009-03-05 23:01:29 +00003625
aliguori731b0362009-03-05 23:01:42 +00003626 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3627 mon);
aliguori87127162009-03-05 23:01:29 +00003628
Blue Swirl72cf2d42009-09-12 07:36:22 +00003629 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003630 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003631 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003632}
3633
aliguori376253e2009-03-05 23:01:23 +00003634static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003635{
aliguoribb5fc202009-03-05 23:01:15 +00003636 BlockDriverState *bs = opaque;
3637 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003638
aliguoribb5fc202009-03-05 23:01:15 +00003639 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003640 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003641 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003642 }
aliguori731b0362009-03-05 23:01:42 +00003643 if (mon->password_completion_cb)
3644 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003645
aliguori731b0362009-03-05 23:01:42 +00003646 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003647}
aliguoric0f4ce72009-03-05 23:01:01 +00003648
aliguori376253e2009-03-05 23:01:23 +00003649void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003650 BlockDriverCompletionFunc *completion_cb,
3651 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003652{
aliguoricde76ee2009-03-05 23:01:51 +00003653 int err;
3654
aliguoribb5fc202009-03-05 23:01:15 +00003655 if (!bdrv_key_required(bs)) {
3656 if (completion_cb)
3657 completion_cb(opaque, 0);
3658 return;
3659 }
aliguoric0f4ce72009-03-05 23:01:01 +00003660
aliguori376253e2009-03-05 23:01:23 +00003661 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3662 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003663
aliguori731b0362009-03-05 23:01:42 +00003664 mon->password_completion_cb = completion_cb;
3665 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003666
aliguoricde76ee2009-03-05 23:01:51 +00003667 err = monitor_read_password(mon, bdrv_password_cb, bs);
3668
3669 if (err && completion_cb)
3670 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003671}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003672
3673typedef struct QemuErrorSink QemuErrorSink;
3674struct QemuErrorSink {
3675 enum {
3676 ERR_SINK_FILE,
3677 ERR_SINK_MONITOR,
3678 } dest;
3679 union {
3680 FILE *fp;
3681 Monitor *mon;
3682 };
3683 QemuErrorSink *previous;
3684};
3685
Blue Swirl528e93a2009-08-31 15:14:40 +00003686static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003687
3688void qemu_errors_to_file(FILE *fp)
3689{
3690 QemuErrorSink *sink;
3691
3692 sink = qemu_mallocz(sizeof(*sink));
3693 sink->dest = ERR_SINK_FILE;
3694 sink->fp = fp;
3695 sink->previous = qemu_error_sink;
3696 qemu_error_sink = sink;
3697}
3698
3699void qemu_errors_to_mon(Monitor *mon)
3700{
3701 QemuErrorSink *sink;
3702
3703 sink = qemu_mallocz(sizeof(*sink));
3704 sink->dest = ERR_SINK_MONITOR;
3705 sink->mon = mon;
3706 sink->previous = qemu_error_sink;
3707 qemu_error_sink = sink;
3708}
3709
3710void qemu_errors_to_previous(void)
3711{
3712 QemuErrorSink *sink;
3713
3714 assert(qemu_error_sink != NULL);
3715 sink = qemu_error_sink;
3716 qemu_error_sink = sink->previous;
3717 qemu_free(sink);
3718}
3719
3720void qemu_error(const char *fmt, ...)
3721{
3722 va_list args;
3723
3724 assert(qemu_error_sink != NULL);
3725 switch (qemu_error_sink->dest) {
3726 case ERR_SINK_FILE:
3727 va_start(args, fmt);
3728 vfprintf(qemu_error_sink->fp, fmt, args);
3729 va_end(args);
3730 break;
3731 case ERR_SINK_MONITOR:
3732 va_start(args, fmt);
3733 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3734 va_end(args);
3735 break;
3736 }
3737}
Luiz Capitulino8204a912009-11-18 23:05:31 -02003738
3739void qemu_error_internal(const char *file, int linenr, const char *func,
3740 const char *fmt, ...)
3741{
3742 va_list va;
3743 QError *qerror;
3744
3745 assert(qemu_error_sink != NULL);
3746
3747 va_start(va, fmt);
3748 qerror = qerror_from_info(file, linenr, func, fmt, &va);
3749 va_end(va);
3750
3751 switch (qemu_error_sink->dest) {
3752 case ERR_SINK_FILE:
3753 qerror_print(qerror);
3754 QDECREF(qerror);
3755 break;
3756 case ERR_SINK_MONITOR:
3757 assert(qemu_error_sink->mon->error == NULL);
3758 qemu_error_sink->mon->error = qerror;
3759 break;
3760 }
3761}