blob: f8340b6d7caa1a003e1de7f2801340d990b31a53 [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"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020053#include "qjson.h"
ths6a5bd302007-12-03 17:05:38 +000054
bellard9dc39cb2004-03-14 21:38:27 +000055//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000056//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000057
bellard9307c4c2004-04-04 12:57:25 +000058/*
59 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000060 *
bellard9307c4c2004-04-04 12:57:25 +000061 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000062 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000063 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000064 * 'i' 32 bit integer
65 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000066 * '/' optional gdb-like print format (like "/10x")
67 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030068 * '?' optional type (for all types, except '/')
69 * '.' other form of optional type (for 'i' and 'l')
70 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000071 *
72 */
73
Anthony Liguoric227f092009-10-01 16:12:16 -050074typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000075 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000076 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +000077 const char *params;
78 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -030079 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -030080 union {
81 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -030082 void (*info_new)(Monitor *mon, QObject **ret_data);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -030083 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -030084 void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Luiz Capitulino910df892009-10-07 13:41:51 -030085 } mhandler;
Anthony Liguoric227f092009-10-01 16:12:16 -050086} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000087
Mark McLoughlinf07918f2009-07-22 09:11:40 +010088/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -050089typedef struct mon_fd_t mon_fd_t;
90struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +010091 char *name;
92 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -050093 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010094};
95
aliguori87127162009-03-05 23:01:29 +000096struct Monitor {
97 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +020098 int mux_out;
99 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000100 int flags;
101 int suspend_cnt;
102 uint8_t outbuf[1024];
103 int outbuf_index;
104 ReadLineState *rs;
105 CPUState *mon_cpu;
106 BlockDriverCompletionFunc *password_completion_cb;
107 void *password_opaque;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200108 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500109 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000110 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000111};
112
Blue Swirl72cf2d42009-09-12 07:36:22 +0000113static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000114
Anthony Liguoric227f092009-10-01 16:12:16 -0500115static const mon_cmd_t mon_cmds[];
116static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000117
aliguori87127162009-03-05 23:01:29 +0000118Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000119
aliguori731b0362009-03-05 23:01:42 +0000120static void monitor_command_cb(Monitor *mon, const char *cmdline,
121 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000122
Luiz Capitulino418173c2009-11-26 22:58:51 -0200123/* Return true if in control mode, false otherwise */
124static inline int monitor_ctrl_mode(const Monitor *mon)
125{
126 return (mon->flags & MONITOR_USE_CONTROL);
127}
128
aliguori731b0362009-03-05 23:01:42 +0000129static void monitor_read_command(Monitor *mon, int show_prompt)
130{
131 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
132 if (show_prompt)
133 readline_show_prompt(mon->rs);
134}
bellard6a00d602005-11-21 23:25:50 +0000135
aliguoricde76ee2009-03-05 23:01:51 +0000136static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
137 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000138{
aliguoricde76ee2009-03-05 23:01:51 +0000139 if (mon->rs) {
140 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
141 /* prompt is printed on return from the command handler */
142 return 0;
143 } else {
144 monitor_printf(mon, "terminal does not support password prompting\n");
145 return -ENOTTY;
146 }
aliguoribb5fc202009-03-05 23:01:15 +0000147}
148
aliguori376253e2009-03-05 23:01:23 +0000149void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000150{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200151 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000152 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
153 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000154 }
155}
156
157/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000158static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000159{
ths60fe76f2007-12-16 03:02:09 +0000160 char c;
aliguori731b0362009-03-05 23:01:42 +0000161
162 if (!mon)
163 return;
164
bellard7e2515e2004-08-01 21:52:19 +0000165 for(;;) {
166 c = *str++;
167 if (c == '\0')
168 break;
bellard7ba12602006-07-14 20:26:42 +0000169 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000170 mon->outbuf[mon->outbuf_index++] = '\r';
171 mon->outbuf[mon->outbuf_index++] = c;
172 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
173 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000174 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000175 }
176}
177
aliguori376253e2009-03-05 23:01:23 +0000178void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000179{
180 char buf[4096];
181 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000182 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000183}
184
aliguori376253e2009-03-05 23:01:23 +0000185void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000186{
187 va_list ap;
188 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000189 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000190 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000191}
192
aliguori376253e2009-03-05 23:01:23 +0000193void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000194{
195 int i;
196
197 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000198 switch (filename[i]) {
199 case ' ':
200 case '"':
201 case '\\':
202 monitor_printf(mon, "\\%c", filename[i]);
203 break;
204 case '\t':
205 monitor_printf(mon, "\\t");
206 break;
207 case '\r':
208 monitor_printf(mon, "\\r");
209 break;
210 case '\n':
211 monitor_printf(mon, "\\n");
212 break;
213 default:
214 monitor_printf(mon, "%c", filename[i]);
215 break;
216 }
thsfef30742006-12-22 14:11:32 +0000217 }
218}
219
bellard7fe48482004-10-09 18:08:01 +0000220static int monitor_fprintf(FILE *stream, const char *fmt, ...)
221{
222 va_list ap;
223 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000224 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000225 va_end(ap);
226 return 0;
227}
228
Luiz Capitulino13c74252009-10-07 13:41:55 -0300229static void monitor_user_noop(Monitor *mon, const QObject *data) { }
230
Luiz Capitulino13917be2009-10-07 13:41:54 -0300231static inline int monitor_handler_ported(const mon_cmd_t *cmd)
232{
233 return cmd->user_print != NULL;
234}
235
Luiz Capitulino8204a912009-11-18 23:05:31 -0200236static inline int monitor_has_error(const Monitor *mon)
237{
238 return mon->error != NULL;
239}
240
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300241static void monitor_print_qobject(Monitor *mon, const QObject *data)
242{
243 switch (qobject_type(data)) {
244 case QTYPE_QSTRING:
245 monitor_printf(mon, "%s",qstring_get_str(qobject_to_qstring(data)));
246 break;
247 case QTYPE_QINT:
248 monitor_printf(mon, "%" PRId64,qint_get_int(qobject_to_qint(data)));
249 break;
250 default:
251 monitor_printf(mon, "ERROR: unsupported type: %d",
252 qobject_type(data));
253 break;
254 }
255
256 monitor_puts(mon, "\n");
257}
258
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200259static void monitor_json_emitter(Monitor *mon, const QObject *data)
260{
261 QString *json;
262
263 json = qobject_to_json(data);
264 assert(json != NULL);
265
266 monitor_printf(mon, "%s\n", qstring_get_str(json));
267 QDECREF(json);
268}
269
bellard9dc39cb2004-03-14 21:38:27 +0000270static int compare_cmd(const char *name, const char *list)
271{
272 const char *p, *pstart;
273 int len;
274 len = strlen(name);
275 p = list;
276 for(;;) {
277 pstart = p;
278 p = strchr(p, '|');
279 if (!p)
280 p = pstart + strlen(pstart);
281 if ((p - pstart) == len && !memcmp(pstart, name, len))
282 return 1;
283 if (*p == '\0')
284 break;
285 p++;
286 }
287 return 0;
288}
289
Anthony Liguoric227f092009-10-01 16:12:16 -0500290static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000291 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000292{
Anthony Liguoric227f092009-10-01 16:12:16 -0500293 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000294
295 for(cmd = cmds; cmd->name != NULL; cmd++) {
296 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000297 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
298 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000299 }
300}
301
aliguori376253e2009-03-05 23:01:23 +0000302static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000303{
304 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000305 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000306 } else {
aliguori376253e2009-03-05 23:01:23 +0000307 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000308 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000309 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000310 monitor_printf(mon, "Log items (comma separated):\n");
311 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000312 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000313 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000314 }
315 }
bellard9dc39cb2004-03-14 21:38:27 +0000316 }
317}
318
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300319static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300320{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300321 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300322}
323
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300324static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000325{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200326 int all_devices;
327 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300328 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000329
bellard7954c732006-08-01 15:52:40 +0000330 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000331 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200332 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300333 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200334 continue;
335 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000336 }
337}
338
Luiz Capitulino13c74252009-10-07 13:41:55 -0300339static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000340{
Anthony Liguoric227f092009-10-01 16:12:16 -0500341 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300342 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000343
bellard9307c4c2004-04-04 12:57:25 +0000344 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000345 goto help;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300346
347 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000348 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300349 break;
bellard9dc39cb2004-03-14 21:38:27 +0000350 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300351
352 if (cmd->name == NULL)
353 goto help;
354
355 if (monitor_handler_ported(cmd)) {
356 cmd->mhandler.info_new(mon, ret_data);
357 if (*ret_data)
358 cmd->user_print(mon, *ret_data);
359 } else {
360 cmd->mhandler.info(mon);
361 }
362
bellard9dc39cb2004-03-14 21:38:27 +0000363 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300364
365help:
366 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000367}
368
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300369/**
370 * do_info_version(): Show QEMU version
371 */
372static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000373{
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300374 *ret_data = QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION));
bellard9bc9d1c2004-10-10 15:15:51 +0000375}
376
aliguori376253e2009-03-05 23:01:23 +0000377static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000378{
379 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000380 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000381}
382
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200383/**
384 * do_info_commands(): List QMP available commands
385 *
386 * Return a QList of QStrings.
387 */
388static void do_info_commands(Monitor *mon, QObject **ret_data)
389{
390 QList *cmd_list;
391 const mon_cmd_t *cmd;
392
393 cmd_list = qlist_new();
394
395 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
396 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
397 qlist_append(cmd_list, qstring_from_str(cmd->name));
398 }
399 }
400
401 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
402 if (monitor_handler_ported(cmd)) {
403 char buf[128];
404 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
405 qlist_append(cmd_list, qstring_from_str(buf));
406 }
407 }
408
409 *ret_data = QOBJECT(cmd_list);
410}
411
aurel32bf4f74c2008-12-18 22:42:34 +0000412#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000413static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000414{
aliguori376253e2009-03-05 23:01:23 +0000415 monitor_printf(mon, "HPET is %s by QEMU\n",
416 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000417}
aurel32bf4f74c2008-12-18 22:42:34 +0000418#endif
aliguori16b29ae2008-12-17 23:28:44 +0000419
aliguori376253e2009-03-05 23:01:23 +0000420static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000421{
aliguori376253e2009-03-05 23:01:23 +0000422 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
423 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
424 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
425 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
426 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000427}
428
bellard6a00d602005-11-21 23:25:50 +0000429/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000430static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000431{
432 CPUState *env;
433
434 for(env = first_cpu; env != NULL; env = env->next_cpu) {
435 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000436 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000437 return 0;
438 }
439 }
440 return -1;
441}
442
pbrook9596ebb2007-11-18 01:44:38 +0000443static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000444{
aliguori731b0362009-03-05 23:01:42 +0000445 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000446 mon_set_cpu(0);
447 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300448 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000449 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000450}
451
aliguori376253e2009-03-05 23:01:23 +0000452static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000453{
bellard6a00d602005-11-21 23:25:50 +0000454 CPUState *env;
455 env = mon_get_cpu();
456 if (!env)
457 return;
bellard9307c4c2004-04-04 12:57:25 +0000458#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000459 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000460 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000461#else
aliguori376253e2009-03-05 23:01:23 +0000462 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000463 0);
bellard9307c4c2004-04-04 12:57:25 +0000464#endif
465}
466
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300467static void print_cpu_iter(QObject *obj, void *opaque)
468{
469 QDict *cpu;
470 int active = ' ';
471 Monitor *mon = opaque;
472
473 assert(qobject_type(obj) == QTYPE_QDICT);
474 cpu = qobject_to_qdict(obj);
475
476 if (strcmp(qdict_get_str(cpu, "current"), "yes") == 0)
477 active = '*';
478
479 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
480
481#if defined(TARGET_I386)
482 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
483 (target_ulong) qdict_get_int(cpu, "pc"));
484#elif defined(TARGET_PPC)
485 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
486 (target_long) qdict_get_int(cpu, "nip"));
487#elif defined(TARGET_SPARC)
488 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
489 (target_long) qdict_get_int(cpu, "pc"));
490 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
491 (target_long) qdict_get_int(cpu, "npc"));
492#elif defined(TARGET_MIPS)
493 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
494 (target_long) qdict_get_int(cpu, "PC"));
495#endif
496
497 if (strcmp(qdict_get_str(cpu, "halted"), "yes") == 0)
498 monitor_printf(mon, " (halted)");
499
500 monitor_printf(mon, "\n");
501}
502
503static void monitor_print_cpus(Monitor *mon, const QObject *data)
504{
505 QList *cpu_list;
506
507 assert(qobject_type(data) == QTYPE_QLIST);
508 cpu_list = qobject_to_qlist(data);
509 qlist_iter(cpu_list, print_cpu_iter, mon);
510}
511
512/**
513 * do_info_cpus(): Show CPU information
514 *
515 * Return a QList with a QDict for each CPU.
516 *
517 * For example:
518 *
519 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
520 * { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]
521 */
522static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000523{
524 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300525 QList *cpu_list;
526
527 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000528
529 /* just to set the default cpu if not already done */
530 mon_get_cpu();
531
532 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300533 const char *answer;
534 QDict *cpu = qdict_new();
535
Avi Kivity4c0960c2009-08-17 23:19:53 +0300536 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300537
538 qdict_put(cpu, "CPU", qint_from_int(env->cpu_index));
539 answer = (env == mon->mon_cpu) ? "yes" : "no";
540 qdict_put(cpu, "current", qstring_from_str(answer));
541
bellard6a00d602005-11-21 23:25:50 +0000542#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300543 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000544#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300545 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000546#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300547 qdict_put(cpu, "pc", qint_from_int(env->pc));
548 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000549#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300550 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000551#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300552 answer = env->halted ? "yes" : "no";
553 qdict_put(cpu, "halted", qstring_from_str(answer));
554
555 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000556 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300557
558 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000559}
560
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300561static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000562{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300563 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000564 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000565 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000566}
567
aliguori376253e2009-03-05 23:01:23 +0000568static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000569{
aliguori376253e2009-03-05 23:01:23 +0000570 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000571}
572
aliguori376253e2009-03-05 23:01:23 +0000573static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000574{
575 int i;
bellard7e2515e2004-08-01 21:52:19 +0000576 const char *str;
ths3b46e622007-09-17 08:09:54 +0000577
aliguoricde76ee2009-03-05 23:01:51 +0000578 if (!mon->rs)
579 return;
bellard7e2515e2004-08-01 21:52:19 +0000580 i = 0;
581 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000582 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000583 if (!str)
584 break;
aliguori376253e2009-03-05 23:01:23 +0000585 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000586 i++;
bellardaa455482004-04-04 13:07:25 +0000587 }
588}
589
j_mayer76a66252007-03-07 08:32:30 +0000590#if defined(TARGET_PPC)
591/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000592static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000593{
594 CPUState *env;
595
596 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000597 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000598}
599#endif
600
Luiz Capitulinob223f352009-10-07 13:41:56 -0300601/**
602 * do_quit(): Quit QEMU execution
603 */
604static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000605{
606 exit(0);
607}
608
aliguori376253e2009-03-05 23:01:23 +0000609static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000610{
611 if (bdrv_is_inserted(bs)) {
612 if (!force) {
613 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000614 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000615 return -1;
616 }
617 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000618 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000619 return -1;
620 }
621 }
622 bdrv_close(bs);
623 }
624 return 0;
625}
626
Luiz Capitulinoe1c923a2009-10-16 12:23:49 -0300627static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000628{
629 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300630 int force = qdict_get_int(qdict, "force");
631 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000632
bellard9307c4c2004-04-04 12:57:25 +0000633 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000634 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000635 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000636 return;
637 }
aliguori376253e2009-03-05 23:01:23 +0000638 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000639}
640
aliguori376253e2009-03-05 23:01:23 +0000641static void do_change_block(Monitor *mon, const char *device,
642 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000643{
644 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000645 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000646
bellard9307c4c2004-04-04 12:57:25 +0000647 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000648 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000649 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000650 return;
651 }
aurel322ecea9b2008-06-18 22:10:01 +0000652 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100653 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000654 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000655 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000656 return;
657 }
658 }
aliguori376253e2009-03-05 23:01:23 +0000659 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000660 return;
aurel322ecea9b2008-06-18 22:10:01 +0000661 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000662 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000663}
664
aliguori376253e2009-03-05 23:01:23 +0000665static void change_vnc_password_cb(Monitor *mon, const char *password,
666 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000667{
668 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000669 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000670
aliguori731b0362009-03-05 23:01:42 +0000671 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000672}
673
aliguori376253e2009-03-05 23:01:23 +0000674static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000675{
ths70848512007-08-25 01:37:05 +0000676 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000677 strcmp(target, "password") == 0) {
678 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000679 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000680 strncpy(password, arg, sizeof(password));
681 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000682 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000683 } else {
aliguori376253e2009-03-05 23:01:23 +0000684 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000685 }
ths70848512007-08-25 01:37:05 +0000686 } else {
aliguori28a76be2009-03-06 20:27:40 +0000687 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000688 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000689 }
thse25a5822007-08-25 01:36:20 +0000690}
691
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300692static void do_change(Monitor *mon, const QDict *qdict)
thse25a5822007-08-25 01:36:20 +0000693{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300694 const char *device = qdict_get_str(qdict, "device");
695 const char *target = qdict_get_str(qdict, "target");
696 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000697 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000698 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000699 } else {
aliguori28a76be2009-03-06 20:27:40 +0000700 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000701 }
702}
703
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300704static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000705{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300706 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000707}
708
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300709static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000710{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300711 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000712}
713
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300714static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000715{
716 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300717 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000718
bellard9307c4c2004-04-04 12:57:25 +0000719 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000720 mask = 0;
721 } else {
bellard9307c4c2004-04-04 12:57:25 +0000722 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000723 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000724 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000725 return;
726 }
727 }
728 cpu_set_log(mask);
729}
730
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300731static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000732{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300733 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000734 if (!option || !strcmp(option, "on")) {
735 singlestep = 1;
736 } else if (!strcmp(option, "off")) {
737 singlestep = 0;
738 } else {
739 monitor_printf(mon, "unexpected option %s\n", option);
740 }
741}
742
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -0300743/**
744 * do_stop(): Stop VM execution
745 */
746static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +0000747{
748 vm_stop(EXCP_INTERRUPT);
749}
750
aliguoribb5fc202009-03-05 23:01:15 +0000751static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000752
aliguori376253e2009-03-05 23:01:23 +0000753struct bdrv_iterate_context {
754 Monitor *mon;
755 int err;
756};
aliguoric0f4ce72009-03-05 23:01:01 +0000757
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300758/**
759 * do_cont(): Resume emulation.
760 */
761static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +0000762{
763 struct bdrv_iterate_context context = { mon, 0 };
764
765 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000766 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000767 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000768 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000769}
770
aliguoribb5fc202009-03-05 23:01:15 +0000771static void bdrv_key_cb(void *opaque, int err)
772{
aliguori376253e2009-03-05 23:01:23 +0000773 Monitor *mon = opaque;
774
aliguoribb5fc202009-03-05 23:01:15 +0000775 /* another key was set successfully, retry to continue */
776 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300777 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000778}
779
780static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
781{
aliguori376253e2009-03-05 23:01:23 +0000782 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000783
aliguori376253e2009-03-05 23:01:23 +0000784 if (!context->err && bdrv_key_required(bs)) {
785 context->err = -EBUSY;
786 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
787 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000788 }
789}
790
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300791static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000792{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300793 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000794 if (!device)
795 device = "tcp::" DEFAULT_GDBSTUB_PORT;
796 if (gdbserver_start(device) < 0) {
797 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
798 device);
799 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000800 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000801 } else {
aliguori59030a82009-04-05 18:43:41 +0000802 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
803 device);
bellard8a7ddc32004-03-31 19:00:16 +0000804 }
805}
806
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300807static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100808{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300809 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100810 if (select_watchdog_action(action) == -1) {
811 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
812 }
813}
814
aliguori376253e2009-03-05 23:01:23 +0000815static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000816{
aliguori376253e2009-03-05 23:01:23 +0000817 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000818 switch(c) {
819 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000820 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000821 break;
822 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000823 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000824 break;
825 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000826 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000827 break;
828 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000829 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000830 break;
831 default:
832 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000833 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000834 } else {
aliguori376253e2009-03-05 23:01:23 +0000835 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000836 }
837 break;
838 }
aliguori376253e2009-03-05 23:01:23 +0000839 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000840}
841
aliguori376253e2009-03-05 23:01:23 +0000842static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -0500843 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000844{
bellard6a00d602005-11-21 23:25:50 +0000845 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000846 int nb_per_line, l, line_size, i, max_digits, len;
847 uint8_t buf[16];
848 uint64_t v;
849
850 if (format == 'i') {
851 int flags;
852 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000853 env = mon_get_cpu();
854 if (!env && !is_physical)
855 return;
bellard9307c4c2004-04-04 12:57:25 +0000856#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000857 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000858 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000859 } else if (wsize == 4) {
860 flags = 0;
861 } else {
bellard6a15fd12006-04-12 21:07:07 +0000862 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000863 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000864 if (env) {
865#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000866 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000867 (env->segs[R_CS].flags & DESC_L_MASK))
868 flags = 2;
869 else
870#endif
871 if (!(env->segs[R_CS].flags & DESC_B_MASK))
872 flags = 1;
873 }
bellard4c27ba22004-04-25 18:05:08 +0000874 }
875#endif
aliguori376253e2009-03-05 23:01:23 +0000876 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000877 return;
878 }
879
880 len = wsize * count;
881 if (wsize == 1)
882 line_size = 8;
883 else
884 line_size = 16;
885 nb_per_line = line_size / wsize;
886 max_digits = 0;
887
888 switch(format) {
889 case 'o':
890 max_digits = (wsize * 8 + 2) / 3;
891 break;
892 default:
893 case 'x':
894 max_digits = (wsize * 8) / 4;
895 break;
896 case 'u':
897 case 'd':
898 max_digits = (wsize * 8 * 10 + 32) / 33;
899 break;
900 case 'c':
901 wsize = 1;
902 break;
903 }
904
905 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000906 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000907 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000908 else
aliguori376253e2009-03-05 23:01:23 +0000909 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000910 l = len;
911 if (l > line_size)
912 l = line_size;
913 if (is_physical) {
914 cpu_physical_memory_rw(addr, buf, l, 0);
915 } else {
bellard6a00d602005-11-21 23:25:50 +0000916 env = mon_get_cpu();
917 if (!env)
918 break;
aliguoric8f79b62008-08-18 14:00:20 +0000919 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000920 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000921 break;
922 }
bellard9307c4c2004-04-04 12:57:25 +0000923 }
ths5fafdf22007-09-16 21:08:06 +0000924 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000925 while (i < l) {
926 switch(wsize) {
927 default:
928 case 1:
929 v = ldub_raw(buf + i);
930 break;
931 case 2:
932 v = lduw_raw(buf + i);
933 break;
934 case 4:
bellard92a31b12005-02-10 22:00:52 +0000935 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000936 break;
937 case 8:
938 v = ldq_raw(buf + i);
939 break;
940 }
aliguori376253e2009-03-05 23:01:23 +0000941 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000942 switch(format) {
943 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000944 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000945 break;
946 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000947 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000948 break;
949 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000950 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000951 break;
952 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000953 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000954 break;
955 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000956 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000957 break;
958 }
959 i += wsize;
960 }
aliguori376253e2009-03-05 23:01:23 +0000961 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000962 addr += l;
963 len -= l;
964 }
965}
966
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300967static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000968{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300969 int count = qdict_get_int(qdict, "count");
970 int format = qdict_get_int(qdict, "format");
971 int size = qdict_get_int(qdict, "size");
972 target_long addr = qdict_get_int(qdict, "addr");
973
aliguori376253e2009-03-05 23:01:23 +0000974 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000975}
976
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300977static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000978{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300979 int count = qdict_get_int(qdict, "count");
980 int format = qdict_get_int(qdict, "format");
981 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -0500982 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300983
aliguori376253e2009-03-05 23:01:23 +0000984 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000985}
986
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300987static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000988{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300989 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -0500990 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300991
blueswir17743e582007-09-24 18:39:04 +0000992#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000993 switch(format) {
994 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000995 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000996 break;
997 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000998 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000999 break;
1000 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001001 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001002 break;
1003 default:
1004 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001005 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001006 break;
1007 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001008 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001009 break;
1010 }
bellard92a31b12005-02-10 22:00:52 +00001011#else
1012 switch(format) {
1013 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001014 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001015 break;
1016 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001017 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001018 break;
1019 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001020 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001021 break;
1022 default:
1023 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001024 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001025 break;
1026 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001027 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001028 break;
1029 }
1030#endif
aliguori376253e2009-03-05 23:01:23 +00001031 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001032}
1033
Luiz Capitulino57e09452009-10-16 12:23:43 -03001034static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001035{
1036 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001037 uint32_t size = qdict_get_int(qdict, "size");
1038 const char *filename = qdict_get_str(qdict, "filename");
1039 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001040 uint32_t l;
1041 CPUState *env;
1042 uint8_t buf[1024];
1043
1044 env = mon_get_cpu();
1045 if (!env)
1046 return;
1047
1048 f = fopen(filename, "wb");
1049 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001050 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +00001051 return;
1052 }
1053 while (size != 0) {
1054 l = sizeof(buf);
1055 if (l > size)
1056 l = size;
1057 cpu_memory_rw_debug(env, addr, buf, l, 0);
1058 fwrite(buf, 1, l, f);
1059 addr += l;
1060 size -= l;
1061 }
1062 fclose(f);
1063}
1064
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001065static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1066 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001067{
1068 FILE *f;
1069 uint32_t l;
1070 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001071 uint32_t size = qdict_get_int(qdict, "size");
1072 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001073 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +00001074
1075 f = fopen(filename, "wb");
1076 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001077 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +00001078 return;
1079 }
1080 while (size != 0) {
1081 l = sizeof(buf);
1082 if (l > size)
1083 l = size;
1084 cpu_physical_memory_rw(addr, buf, l, 0);
1085 fwrite(buf, 1, l, f);
1086 fflush(f);
1087 addr += l;
1088 size -= l;
1089 }
1090 fclose(f);
1091}
1092
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001093static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001094{
1095 uint32_t addr;
1096 uint8_t buf[1];
1097 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001098 uint32_t start = qdict_get_int(qdict, "start");
1099 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001100
1101 sum = 0;
1102 for(addr = start; addr < (start + size); addr++) {
1103 cpu_physical_memory_rw(addr, buf, 1, 0);
1104 /* BSD sum algorithm ('sum' Unix command) */
1105 sum = (sum >> 1) | (sum << 15);
1106 sum += buf[0];
1107 }
aliguori376253e2009-03-05 23:01:23 +00001108 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001109}
1110
bellarda3a91a32004-06-04 11:06:21 +00001111typedef struct {
1112 int keycode;
1113 const char *name;
1114} KeyDef;
1115
1116static const KeyDef key_defs[] = {
1117 { 0x2a, "shift" },
1118 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001119
bellarda3a91a32004-06-04 11:06:21 +00001120 { 0x38, "alt" },
1121 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001122 { 0x64, "altgr" },
1123 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001124 { 0x1d, "ctrl" },
1125 { 0x9d, "ctrl_r" },
1126
1127 { 0xdd, "menu" },
1128
1129 { 0x01, "esc" },
1130
1131 { 0x02, "1" },
1132 { 0x03, "2" },
1133 { 0x04, "3" },
1134 { 0x05, "4" },
1135 { 0x06, "5" },
1136 { 0x07, "6" },
1137 { 0x08, "7" },
1138 { 0x09, "8" },
1139 { 0x0a, "9" },
1140 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001141 { 0x0c, "minus" },
1142 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001143 { 0x0e, "backspace" },
1144
1145 { 0x0f, "tab" },
1146 { 0x10, "q" },
1147 { 0x11, "w" },
1148 { 0x12, "e" },
1149 { 0x13, "r" },
1150 { 0x14, "t" },
1151 { 0x15, "y" },
1152 { 0x16, "u" },
1153 { 0x17, "i" },
1154 { 0x18, "o" },
1155 { 0x19, "p" },
1156
1157 { 0x1c, "ret" },
1158
1159 { 0x1e, "a" },
1160 { 0x1f, "s" },
1161 { 0x20, "d" },
1162 { 0x21, "f" },
1163 { 0x22, "g" },
1164 { 0x23, "h" },
1165 { 0x24, "j" },
1166 { 0x25, "k" },
1167 { 0x26, "l" },
1168
1169 { 0x2c, "z" },
1170 { 0x2d, "x" },
1171 { 0x2e, "c" },
1172 { 0x2f, "v" },
1173 { 0x30, "b" },
1174 { 0x31, "n" },
1175 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001176 { 0x33, "comma" },
1177 { 0x34, "dot" },
1178 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001179
balrog4d3b6f62008-02-10 16:33:14 +00001180 { 0x37, "asterisk" },
1181
bellarda3a91a32004-06-04 11:06:21 +00001182 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001183 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001184 { 0x3b, "f1" },
1185 { 0x3c, "f2" },
1186 { 0x3d, "f3" },
1187 { 0x3e, "f4" },
1188 { 0x3f, "f5" },
1189 { 0x40, "f6" },
1190 { 0x41, "f7" },
1191 { 0x42, "f8" },
1192 { 0x43, "f9" },
1193 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001194 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001195 { 0x46, "scroll_lock" },
1196
bellard64866c32006-05-07 18:03:31 +00001197 { 0xb5, "kp_divide" },
1198 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001199 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001200 { 0x4e, "kp_add" },
1201 { 0x9c, "kp_enter" },
1202 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001203 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001204
1205 { 0x52, "kp_0" },
1206 { 0x4f, "kp_1" },
1207 { 0x50, "kp_2" },
1208 { 0x51, "kp_3" },
1209 { 0x4b, "kp_4" },
1210 { 0x4c, "kp_5" },
1211 { 0x4d, "kp_6" },
1212 { 0x47, "kp_7" },
1213 { 0x48, "kp_8" },
1214 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001215
bellarda3a91a32004-06-04 11:06:21 +00001216 { 0x56, "<" },
1217
1218 { 0x57, "f11" },
1219 { 0x58, "f12" },
1220
1221 { 0xb7, "print" },
1222
1223 { 0xc7, "home" },
1224 { 0xc9, "pgup" },
1225 { 0xd1, "pgdn" },
1226 { 0xcf, "end" },
1227
1228 { 0xcb, "left" },
1229 { 0xc8, "up" },
1230 { 0xd0, "down" },
1231 { 0xcd, "right" },
1232
1233 { 0xd2, "insert" },
1234 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001235#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1236 { 0xf0, "stop" },
1237 { 0xf1, "again" },
1238 { 0xf2, "props" },
1239 { 0xf3, "undo" },
1240 { 0xf4, "front" },
1241 { 0xf5, "copy" },
1242 { 0xf6, "open" },
1243 { 0xf7, "paste" },
1244 { 0xf8, "find" },
1245 { 0xf9, "cut" },
1246 { 0xfa, "lf" },
1247 { 0xfb, "help" },
1248 { 0xfc, "meta_l" },
1249 { 0xfd, "meta_r" },
1250 { 0xfe, "compose" },
1251#endif
bellarda3a91a32004-06-04 11:06:21 +00001252 { 0, NULL },
1253};
1254
1255static int get_keycode(const char *key)
1256{
1257 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001258 char *endp;
1259 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001260
1261 for(p = key_defs; p->name != NULL; p++) {
1262 if (!strcmp(key, p->name))
1263 return p->keycode;
1264 }
bellard64866c32006-05-07 18:03:31 +00001265 if (strstart(key, "0x", NULL)) {
1266 ret = strtoul(key, &endp, 0);
1267 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1268 return ret;
1269 }
bellarda3a91a32004-06-04 11:06:21 +00001270 return -1;
1271}
1272
balrogc8256f92008-06-08 22:45:01 +00001273#define MAX_KEYCODES 16
1274static uint8_t keycodes[MAX_KEYCODES];
1275static int nb_pending_keycodes;
1276static QEMUTimer *key_timer;
1277
1278static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001279{
balrogc8256f92008-06-08 22:45:01 +00001280 int keycode;
1281
1282 while (nb_pending_keycodes > 0) {
1283 nb_pending_keycodes--;
1284 keycode = keycodes[nb_pending_keycodes];
1285 if (keycode & 0x80)
1286 kbd_put_keycode(0xe0);
1287 kbd_put_keycode(keycode | 0x80);
1288 }
1289}
1290
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001291static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001292{
balrog3401c0d2008-06-04 10:05:59 +00001293 char keyname_buf[16];
1294 char *separator;
1295 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001296 const char *string = qdict_get_str(qdict, "string");
1297 int has_hold_time = qdict_haskey(qdict, "hold_time");
1298 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001299
balrogc8256f92008-06-08 22:45:01 +00001300 if (nb_pending_keycodes > 0) {
1301 qemu_del_timer(key_timer);
1302 release_keys(NULL);
1303 }
1304 if (!has_hold_time)
1305 hold_time = 100;
1306 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001307 while (1) {
1308 separator = strchr(string, '-');
1309 keyname_len = separator ? separator - string : strlen(string);
1310 if (keyname_len > 0) {
1311 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1312 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001313 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001314 return;
bellarda3a91a32004-06-04 11:06:21 +00001315 }
balrogc8256f92008-06-08 22:45:01 +00001316 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001317 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001318 return;
1319 }
1320 keyname_buf[keyname_len] = 0;
1321 keycode = get_keycode(keyname_buf);
1322 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001323 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001324 return;
1325 }
balrogc8256f92008-06-08 22:45:01 +00001326 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001327 }
balrog3401c0d2008-06-04 10:05:59 +00001328 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001329 break;
balrog3401c0d2008-06-04 10:05:59 +00001330 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001331 }
balrogc8256f92008-06-08 22:45:01 +00001332 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001333 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001334 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001335 keycode = keycodes[i];
1336 if (keycode & 0x80)
1337 kbd_put_keycode(0xe0);
1338 kbd_put_keycode(keycode & 0x7f);
1339 }
balrogc8256f92008-06-08 22:45:01 +00001340 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001341 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001342 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001343}
1344
bellard13224a82006-07-14 22:03:35 +00001345static int mouse_button_state;
1346
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001347static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001348{
1349 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001350 const char *dx_str = qdict_get_str(qdict, "dx_str");
1351 const char *dy_str = qdict_get_str(qdict, "dy_str");
1352 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001353 dx = strtol(dx_str, NULL, 0);
1354 dy = strtol(dy_str, NULL, 0);
1355 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001356 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001357 dz = strtol(dz_str, NULL, 0);
1358 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1359}
1360
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001361static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001362{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001363 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001364 mouse_button_state = button_state;
1365 kbd_mouse_event(0, 0, 0, mouse_button_state);
1366}
1367
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001368static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001369{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001370 int size = qdict_get_int(qdict, "size");
1371 int addr = qdict_get_int(qdict, "addr");
1372 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001373 uint32_t val;
1374 int suffix;
1375
1376 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001377 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001378 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001379 addr++;
1380 }
1381 addr &= 0xffff;
1382
1383 switch(size) {
1384 default:
1385 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001386 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001387 suffix = 'b';
1388 break;
1389 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001390 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001391 suffix = 'w';
1392 break;
1393 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001394 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001395 suffix = 'l';
1396 break;
1397 }
aliguori376253e2009-03-05 23:01:23 +00001398 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1399 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001400}
bellarda3a91a32004-06-04 11:06:21 +00001401
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001402static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001403{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001404 int size = qdict_get_int(qdict, "size");
1405 int addr = qdict_get_int(qdict, "addr");
1406 int val = qdict_get_int(qdict, "val");
1407
Jan Kiszkaf1147842009-07-14 10:20:11 +02001408 addr &= IOPORTS_MASK;
1409
1410 switch (size) {
1411 default:
1412 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001413 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001414 break;
1415 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001416 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001417 break;
1418 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001419 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001420 break;
1421 }
1422}
1423
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001424static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001425{
1426 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001427 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001428
Jan Kiszka76e30d02009-07-02 00:19:02 +02001429 res = qemu_boot_set(bootdevice);
1430 if (res == 0) {
1431 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1432 } else if (res > 0) {
1433 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001434 } else {
aliguori376253e2009-03-05 23:01:23 +00001435 monitor_printf(mon, "no function defined to set boot device list for "
1436 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001437 }
1438}
1439
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001440/**
1441 * do_system_reset(): Issue a machine reset
1442 */
1443static void do_system_reset(Monitor *mon, const QDict *qdict,
1444 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001445{
1446 qemu_system_reset_request();
1447}
1448
Luiz Capitulino43076662009-10-07 13:41:59 -03001449/**
1450 * do_system_powerdown(): Issue a machine powerdown
1451 */
1452static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1453 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001454{
1455 qemu_system_powerdown_request();
1456}
1457
bellardb86bda52004-09-18 19:32:46 +00001458#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001459static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001460{
aliguori376253e2009-03-05 23:01:23 +00001461 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1462 addr,
1463 pte & mask,
1464 pte & PG_GLOBAL_MASK ? 'G' : '-',
1465 pte & PG_PSE_MASK ? 'P' : '-',
1466 pte & PG_DIRTY_MASK ? 'D' : '-',
1467 pte & PG_ACCESSED_MASK ? 'A' : '-',
1468 pte & PG_PCD_MASK ? 'C' : '-',
1469 pte & PG_PWT_MASK ? 'T' : '-',
1470 pte & PG_USER_MASK ? 'U' : '-',
1471 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001472}
1473
aliguori376253e2009-03-05 23:01:23 +00001474static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001475{
bellard6a00d602005-11-21 23:25:50 +00001476 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001477 int l1, l2;
1478 uint32_t pgd, pde, pte;
1479
bellard6a00d602005-11-21 23:25:50 +00001480 env = mon_get_cpu();
1481 if (!env)
1482 return;
1483
bellardb86bda52004-09-18 19:32:46 +00001484 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001485 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001486 return;
1487 }
1488 pgd = env->cr[3] & ~0xfff;
1489 for(l1 = 0; l1 < 1024; l1++) {
1490 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1491 pde = le32_to_cpu(pde);
1492 if (pde & PG_PRESENT_MASK) {
1493 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001494 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001495 } else {
1496 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001497 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001498 (uint8_t *)&pte, 4);
1499 pte = le32_to_cpu(pte);
1500 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001501 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001502 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001503 ~0xfff);
1504 }
1505 }
1506 }
1507 }
1508 }
1509}
1510
aliguori376253e2009-03-05 23:01:23 +00001511static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001512 uint32_t end, int prot)
1513{
bellard9746b152004-11-11 18:30:24 +00001514 int prot1;
1515 prot1 = *plast_prot;
1516 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001517 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001518 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1519 *pstart, end, end - *pstart,
1520 prot1 & PG_USER_MASK ? 'u' : '-',
1521 'r',
1522 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001523 }
1524 if (prot != 0)
1525 *pstart = end;
1526 else
1527 *pstart = -1;
1528 *plast_prot = prot;
1529 }
1530}
1531
aliguori376253e2009-03-05 23:01:23 +00001532static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001533{
bellard6a00d602005-11-21 23:25:50 +00001534 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001535 int l1, l2, prot, last_prot;
1536 uint32_t pgd, pde, pte, start, end;
1537
bellard6a00d602005-11-21 23:25:50 +00001538 env = mon_get_cpu();
1539 if (!env)
1540 return;
1541
bellardb86bda52004-09-18 19:32:46 +00001542 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001543 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001544 return;
1545 }
1546 pgd = env->cr[3] & ~0xfff;
1547 last_prot = 0;
1548 start = -1;
1549 for(l1 = 0; l1 < 1024; l1++) {
1550 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1551 pde = le32_to_cpu(pde);
1552 end = l1 << 22;
1553 if (pde & PG_PRESENT_MASK) {
1554 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1555 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001556 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001557 } else {
1558 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001559 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001560 (uint8_t *)&pte, 4);
1561 pte = le32_to_cpu(pte);
1562 end = (l1 << 22) + (l2 << 12);
1563 if (pte & PG_PRESENT_MASK) {
1564 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1565 } else {
1566 prot = 0;
1567 }
aliguori376253e2009-03-05 23:01:23 +00001568 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001569 }
1570 }
1571 } else {
1572 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001573 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001574 }
1575 }
1576}
1577#endif
1578
aurel327c664e22009-03-03 06:12:22 +00001579#if defined(TARGET_SH4)
1580
aliguori376253e2009-03-05 23:01:23 +00001581static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001582{
aliguori376253e2009-03-05 23:01:23 +00001583 monitor_printf(mon, " tlb%i:\t"
1584 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1585 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1586 "dirty=%hhu writethrough=%hhu\n",
1587 idx,
1588 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1589 tlb->v, tlb->sh, tlb->c, tlb->pr,
1590 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001591}
1592
aliguori376253e2009-03-05 23:01:23 +00001593static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001594{
1595 CPUState *env = mon_get_cpu();
1596 int i;
1597
aliguori376253e2009-03-05 23:01:23 +00001598 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001599 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001600 print_tlb (mon, i, &env->itlb[i]);
1601 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001602 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001603 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001604}
1605
1606#endif
1607
aliguori376253e2009-03-05 23:01:23 +00001608static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001609{
1610#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001611 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001612 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001613 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001614 else
aliguori376253e2009-03-05 23:01:23 +00001615 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001616#else
aliguori376253e2009-03-05 23:01:23 +00001617 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001618#endif
1619}
1620
aliguori030ea372009-04-21 22:30:47 +00001621static void do_info_numa(Monitor *mon)
1622{
aliguorib28b6232009-04-22 20:20:29 +00001623 int i;
aliguori030ea372009-04-21 22:30:47 +00001624 CPUState *env;
1625
1626 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1627 for (i = 0; i < nb_numa_nodes; i++) {
1628 monitor_printf(mon, "node %d cpus:", i);
1629 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1630 if (env->numa_node == i) {
1631 monitor_printf(mon, " %d", env->cpu_index);
1632 }
1633 }
1634 monitor_printf(mon, "\n");
1635 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1636 node_mem[i] >> 20);
1637 }
1638}
1639
bellard5f1ce942006-02-08 22:40:15 +00001640#ifdef CONFIG_PROFILER
1641
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001642int64_t qemu_time;
1643int64_t dev_time;
1644
aliguori376253e2009-03-05 23:01:23 +00001645static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001646{
1647 int64_t total;
1648 total = qemu_time;
1649 if (total == 0)
1650 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001651 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001652 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001653 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001654 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001655 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001656 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001657}
1658#else
aliguori376253e2009-03-05 23:01:23 +00001659static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001660{
aliguori376253e2009-03-05 23:01:23 +00001661 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001662}
1663#endif
1664
bellardec36b692006-07-16 18:57:03 +00001665/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001666static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001667
aliguori376253e2009-03-05 23:01:23 +00001668static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001669{
1670 int i;
1671 CaptureState *s;
1672
1673 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001674 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001675 s->ops.info (s->opaque);
1676 }
1677}
1678
Blue Swirl23130862009-06-06 08:22:04 +00001679#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001680static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001681{
1682 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001683 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001684 CaptureState *s;
1685
1686 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1687 if (i == n) {
1688 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001689 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00001690 qemu_free (s);
1691 return;
1692 }
1693 }
1694}
1695
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001696static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001697{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001698 const char *path = qdict_get_str(qdict, "path");
1699 int has_freq = qdict_haskey(qdict, "freq");
1700 int freq = qdict_get_try_int(qdict, "freq", -1);
1701 int has_bits = qdict_haskey(qdict, "bits");
1702 int bits = qdict_get_try_int(qdict, "bits", -1);
1703 int has_channels = qdict_haskey(qdict, "nchannels");
1704 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001705 CaptureState *s;
1706
1707 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001708
1709 freq = has_freq ? freq : 44100;
1710 bits = has_bits ? bits : 16;
1711 nchannels = has_channels ? nchannels : 2;
1712
1713 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001714 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001715 qemu_free (s);
1716 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001717 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001718}
1719#endif
1720
aurel32dc1c0b72008-04-27 23:52:12 +00001721#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001722static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001723{
1724 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001725 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001726
1727 for (env = first_cpu; env != NULL; env = env->next_cpu)
1728 if (env->cpu_index == cpu_index) {
1729 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1730 break;
1731 }
1732}
1733#endif
1734
aliguori376253e2009-03-05 23:01:23 +00001735static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001736{
aurel321b530a62009-04-05 20:08:59 +00001737 if (vm_running) {
1738 if (singlestep) {
1739 monitor_printf(mon, "VM status: running (single step mode)\n");
1740 } else {
1741 monitor_printf(mon, "VM status: running\n");
1742 }
1743 } else
aliguori376253e2009-03-05 23:01:23 +00001744 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001745}
1746
Luiz Capitulino83fb1de2009-10-07 13:42:01 -03001747/**
1748 * do_balloon(): Request VM to change its memory allocation
1749 */
1750static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001751{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001752 int value = qdict_get_int(qdict, "value");
Anthony Liguoric227f092009-10-01 16:12:16 -05001753 ram_addr_t target = value;
aliguoridf751fa2008-12-04 20:19:35 +00001754 qemu_balloon(target << 20);
1755}
1756
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001757static void monitor_print_balloon(Monitor *mon, const QObject *data)
1758{
1759 monitor_printf(mon, "balloon: actual=%d\n",
1760 (int)qint_get_int(qobject_to_qint(data)));
1761}
1762
1763/**
1764 * do_info_balloon(): Balloon information
1765 */
1766static void do_info_balloon(Monitor *mon, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001767{
Anthony Liguoric227f092009-10-01 16:12:16 -05001768 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00001769
1770 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001771 if (kvm_enabled() && !kvm_has_sync_mmu())
Luiz Capitulino5d6c37f2009-11-18 23:05:36 -02001772 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
aliguoribd322082008-12-04 20:33:06 +00001773 else if (actual == 0)
Luiz Capitulino5d6c37f2009-11-18 23:05:36 -02001774 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
aliguoridf751fa2008-12-04 20:19:35 +00001775 else
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001776 *ret_data = QOBJECT(qint_from_int((int)(actual >> 20)));
aliguoridf751fa2008-12-04 20:19:35 +00001777}
1778
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001779static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001780{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001781 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001782
aliguori76655d62009-03-06 20:27:37 +00001783 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001784 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001785 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001786 return acl;
1787}
aliguori76655d62009-03-06 20:27:37 +00001788
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001789static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001790{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001791 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001792 qemu_acl *acl = find_acl(mon, aclname);
1793 qemu_acl_entry *entry;
1794 int i = 0;
1795
1796 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001797 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001798 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001799 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001800 i++;
1801 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001802 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001803 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001804 }
1805}
1806
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001807static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001808{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001809 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001810 qemu_acl *acl = find_acl(mon, aclname);
1811
1812 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001813 qemu_acl_reset(acl);
1814 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001815 }
1816}
aliguori76655d62009-03-06 20:27:37 +00001817
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001818static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001819{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001820 const char *aclname = qdict_get_str(qdict, "aclname");
1821 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001822 qemu_acl *acl = find_acl(mon, aclname);
1823
1824 if (acl) {
1825 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001826 acl->defaultDeny = 0;
1827 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001828 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001829 acl->defaultDeny = 1;
1830 monitor_printf(mon, "acl: policy set to 'deny'\n");
1831 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001832 monitor_printf(mon, "acl: unknown policy '%s', "
1833 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001834 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001835 }
1836}
aliguori76655d62009-03-06 20:27:37 +00001837
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001838static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001839{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001840 const char *aclname = qdict_get_str(qdict, "aclname");
1841 const char *match = qdict_get_str(qdict, "match");
1842 const char *policy = qdict_get_str(qdict, "policy");
1843 int has_index = qdict_haskey(qdict, "index");
1844 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001845 qemu_acl *acl = find_acl(mon, aclname);
1846 int deny, ret;
1847
1848 if (acl) {
1849 if (strcmp(policy, "allow") == 0) {
1850 deny = 0;
1851 } else if (strcmp(policy, "deny") == 0) {
1852 deny = 1;
1853 } else {
1854 monitor_printf(mon, "acl: unknown policy '%s', "
1855 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001856 return;
1857 }
aliguori28a76be2009-03-06 20:27:40 +00001858 if (has_index)
1859 ret = qemu_acl_insert(acl, deny, match, index);
1860 else
1861 ret = qemu_acl_append(acl, deny, match);
1862 if (ret < 0)
1863 monitor_printf(mon, "acl: unable to add acl entry\n");
1864 else
1865 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001866 }
1867}
aliguori76655d62009-03-06 20:27:37 +00001868
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001869static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001870{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001871 const char *aclname = qdict_get_str(qdict, "aclname");
1872 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001873 qemu_acl *acl = find_acl(mon, aclname);
1874 int ret;
aliguori76655d62009-03-06 20:27:37 +00001875
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001876 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001877 ret = qemu_acl_remove(acl, match);
1878 if (ret < 0)
1879 monitor_printf(mon, "acl: no matching acl entry\n");
1880 else
1881 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001882 }
1883}
1884
Huang Ying79c4f6b2009-06-23 10:05:14 +08001885#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001886static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001887{
1888 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001889 int cpu_index = qdict_get_int(qdict, "cpu_index");
1890 int bank = qdict_get_int(qdict, "bank");
1891 uint64_t status = qdict_get_int(qdict, "status");
1892 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1893 uint64_t addr = qdict_get_int(qdict, "addr");
1894 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08001895
1896 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1897 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1898 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1899 break;
1900 }
1901}
1902#endif
1903
Luiz Capitulinof0d60002009-10-16 12:23:50 -03001904static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001905{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001906 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001907 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001908 int fd;
1909
1910 fd = qemu_chr_get_msgfd(mon->chr);
1911 if (fd == -1) {
1912 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1913 return;
1914 }
1915
1916 if (qemu_isdigit(fdname[0])) {
1917 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1918 return;
1919 }
1920
1921 fd = dup(fd);
1922 if (fd == -1) {
1923 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1924 strerror(errno));
1925 return;
1926 }
1927
Blue Swirl72cf2d42009-09-12 07:36:22 +00001928 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001929 if (strcmp(monfd->name, fdname) != 0) {
1930 continue;
1931 }
1932
1933 close(monfd->fd);
1934 monfd->fd = fd;
1935 return;
1936 }
1937
Anthony Liguoric227f092009-10-01 16:12:16 -05001938 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001939 monfd->name = qemu_strdup(fdname);
1940 monfd->fd = fd;
1941
Blue Swirl72cf2d42009-09-12 07:36:22 +00001942 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001943}
1944
Luiz Capitulino18f3a512009-10-16 12:23:51 -03001945static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001946{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001947 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001948 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001949
Blue Swirl72cf2d42009-09-12 07:36:22 +00001950 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001951 if (strcmp(monfd->name, fdname) != 0) {
1952 continue;
1953 }
1954
Blue Swirl72cf2d42009-09-12 07:36:22 +00001955 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001956 close(monfd->fd);
1957 qemu_free(monfd->name);
1958 qemu_free(monfd);
1959 return;
1960 }
1961
1962 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1963 fdname);
1964}
1965
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001966static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001967{
1968 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001969 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001970
1971 vm_stop(0);
1972
Juan Quintela05f24012009-08-20 19:42:22 +02001973 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001974 vm_start();
1975}
1976
Mark McLoughlin7768e042009-07-22 09:11:41 +01001977int monitor_get_fd(Monitor *mon, const char *fdname)
1978{
Anthony Liguoric227f092009-10-01 16:12:16 -05001979 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001980
Blue Swirl72cf2d42009-09-12 07:36:22 +00001981 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001982 int fd;
1983
1984 if (strcmp(monfd->name, fdname) != 0) {
1985 continue;
1986 }
1987
1988 fd = monfd->fd;
1989
1990 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001991 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001992 qemu_free(monfd->name);
1993 qemu_free(monfd);
1994
1995 return fd;
1996 }
1997
1998 return -1;
1999}
2000
Anthony Liguoric227f092009-10-01 16:12:16 -05002001static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002002#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002003 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002004};
2005
Blue Swirl23130862009-06-06 08:22:04 +00002006/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002007static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002008 {
2009 .name = "version",
2010 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002011 .params = "",
2012 .help = "show the version of QEMU",
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002013 .user_print = monitor_print_qobject,
2014 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002015 },
2016 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002017 .name = "commands",
2018 .args_type = "",
2019 .params = "",
2020 .help = "list QMP available commands",
2021 .user_print = monitor_user_noop,
2022 .mhandler.info_new = do_info_commands,
2023 },
2024 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002025 .name = "network",
2026 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002027 .params = "",
2028 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002029 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002030 },
2031 {
2032 .name = "chardev",
2033 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002034 .params = "",
2035 .help = "show the character devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002036 .mhandler.info = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002037 },
2038 {
2039 .name = "block",
2040 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002041 .params = "",
2042 .help = "show the block devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002043 .mhandler.info = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002044 },
2045 {
2046 .name = "blockstats",
2047 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002048 .params = "",
2049 .help = "show block device statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002050 .mhandler.info = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002051 },
2052 {
2053 .name = "registers",
2054 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002055 .params = "",
2056 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002057 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002058 },
2059 {
2060 .name = "cpus",
2061 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002062 .params = "",
2063 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002064 .user_print = monitor_print_cpus,
2065 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002066 },
2067 {
2068 .name = "history",
2069 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002070 .params = "",
2071 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002072 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002073 },
2074 {
2075 .name = "irq",
2076 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002077 .params = "",
2078 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002079 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002080 },
2081 {
2082 .name = "pic",
2083 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002084 .params = "",
2085 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002086 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002087 },
2088 {
2089 .name = "pci",
2090 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002091 .params = "",
2092 .help = "show PCI info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002093 .mhandler.info = pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002094 },
aurel327c664e22009-03-03 06:12:22 +00002095#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002096 {
2097 .name = "tlb",
2098 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002099 .params = "",
2100 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002101 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002102 },
aurel327c664e22009-03-03 06:12:22 +00002103#endif
2104#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002105 {
2106 .name = "mem",
2107 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002108 .params = "",
2109 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002110 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002111 },
2112 {
2113 .name = "hpet",
2114 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002115 .params = "",
2116 .help = "show state of HPET",
Luiz Capitulino910df892009-10-07 13:41:51 -03002117 .mhandler.info = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002118 },
bellardb86bda52004-09-18 19:32:46 +00002119#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002120 {
2121 .name = "jit",
2122 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002123 .params = "",
2124 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002125 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002126 },
2127 {
2128 .name = "kvm",
2129 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002130 .params = "",
2131 .help = "show KVM information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002132 .mhandler.info = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002133 },
2134 {
2135 .name = "numa",
2136 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002137 .params = "",
2138 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002139 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002140 },
2141 {
2142 .name = "usb",
2143 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002144 .params = "",
2145 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002146 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002147 },
2148 {
2149 .name = "usbhost",
2150 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002151 .params = "",
2152 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002153 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002154 },
2155 {
2156 .name = "profile",
2157 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002158 .params = "",
2159 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002160 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002161 },
2162 {
2163 .name = "capture",
2164 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002165 .params = "",
2166 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002167 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002168 },
2169 {
2170 .name = "snapshots",
2171 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002172 .params = "",
2173 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002174 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002175 },
2176 {
2177 .name = "status",
2178 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002179 .params = "",
2180 .help = "show the current VM status (running|paused)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002181 .mhandler.info = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002182 },
2183 {
2184 .name = "pcmcia",
2185 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002186 .params = "",
2187 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002188 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002189 },
2190 {
2191 .name = "mice",
2192 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002193 .params = "",
2194 .help = "show which guest mouse is receiving events",
Luiz Capitulino910df892009-10-07 13:41:51 -03002195 .mhandler.info = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002196 },
2197 {
2198 .name = "vnc",
2199 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002200 .params = "",
2201 .help = "show the vnc server status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002202 .mhandler.info = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002203 },
2204 {
2205 .name = "name",
2206 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002207 .params = "",
2208 .help = "show the current VM name",
Luiz Capitulino910df892009-10-07 13:41:51 -03002209 .mhandler.info = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002210 },
2211 {
2212 .name = "uuid",
2213 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002214 .params = "",
2215 .help = "show the current VM UUID",
Luiz Capitulino910df892009-10-07 13:41:51 -03002216 .mhandler.info = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002217 },
j_mayer76a66252007-03-07 08:32:30 +00002218#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002219 {
2220 .name = "cpustats",
2221 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002222 .params = "",
2223 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002224 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002225 },
j_mayer76a66252007-03-07 08:32:30 +00002226#endif
blueswir131a60e22007-10-26 18:42:59 +00002227#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002228 {
2229 .name = "usernet",
2230 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002231 .params = "",
2232 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002233 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002234 },
blueswir131a60e22007-10-26 18:42:59 +00002235#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002236 {
2237 .name = "migrate",
2238 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002239 .params = "",
2240 .help = "show migration status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002241 .mhandler.info = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002242 },
2243 {
2244 .name = "balloon",
2245 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002246 .params = "",
2247 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002248 .user_print = monitor_print_balloon,
2249 .mhandler.info_new = do_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002250 },
2251 {
2252 .name = "qtree",
2253 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002254 .params = "",
2255 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002256 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002257 },
2258 {
2259 .name = "qdm",
2260 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002261 .params = "",
2262 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002263 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002264 },
2265 {
2266 .name = "roms",
2267 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002268 .params = "",
2269 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002270 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002271 },
2272 {
2273 .name = NULL,
2274 },
bellard9dc39cb2004-03-14 21:38:27 +00002275};
2276
bellard9307c4c2004-04-04 12:57:25 +00002277/*******************************************************************/
2278
2279static const char *pch;
2280static jmp_buf expr_env;
2281
bellard92a31b12005-02-10 22:00:52 +00002282#define MD_TLONG 0
2283#define MD_I32 1
2284
bellard9307c4c2004-04-04 12:57:25 +00002285typedef struct MonitorDef {
2286 const char *name;
2287 int offset;
blueswir18662d652008-10-02 18:32:44 +00002288 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002289 int type;
bellard9307c4c2004-04-04 12:57:25 +00002290} MonitorDef;
2291
bellard57206fd2004-04-25 18:54:52 +00002292#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002293static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002294{
bellard6a00d602005-11-21 23:25:50 +00002295 CPUState *env = mon_get_cpu();
2296 if (!env)
2297 return 0;
2298 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002299}
2300#endif
2301
bellarda541f292004-04-12 20:39:29 +00002302#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002303static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002304{
bellard6a00d602005-11-21 23:25:50 +00002305 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002306 unsigned int u;
2307 int i;
2308
bellard6a00d602005-11-21 23:25:50 +00002309 if (!env)
2310 return 0;
2311
bellarda541f292004-04-12 20:39:29 +00002312 u = 0;
2313 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002314 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002315
2316 return u;
2317}
2318
blueswir18662d652008-10-02 18:32:44 +00002319static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002320{
bellard6a00d602005-11-21 23:25:50 +00002321 CPUState *env = mon_get_cpu();
2322 if (!env)
2323 return 0;
j_mayer0411a972007-10-25 21:35:50 +00002324 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002325}
2326
blueswir18662d652008-10-02 18:32:44 +00002327static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002328{
bellard6a00d602005-11-21 23:25:50 +00002329 CPUState *env = mon_get_cpu();
2330 if (!env)
2331 return 0;
aurel323d7b4172008-10-21 11:28:46 +00002332 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002333}
bellard9fddaa02004-05-21 12:59:32 +00002334
blueswir18662d652008-10-02 18:32:44 +00002335static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002336{
bellard6a00d602005-11-21 23:25:50 +00002337 CPUState *env = mon_get_cpu();
2338 if (!env)
2339 return 0;
2340 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002341}
2342
blueswir18662d652008-10-02 18:32:44 +00002343static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002344{
bellard6a00d602005-11-21 23:25:50 +00002345 CPUState *env = mon_get_cpu();
2346 if (!env)
2347 return 0;
2348 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002349}
2350
blueswir18662d652008-10-02 18:32:44 +00002351static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002352{
bellard6a00d602005-11-21 23:25:50 +00002353 CPUState *env = mon_get_cpu();
2354 if (!env)
2355 return 0;
2356 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002357}
bellarda541f292004-04-12 20:39:29 +00002358#endif
2359
bellarde95c8d52004-09-30 22:22:08 +00002360#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002361#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002362static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002363{
bellard6a00d602005-11-21 23:25:50 +00002364 CPUState *env = mon_get_cpu();
2365 if (!env)
2366 return 0;
2367 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002368}
bellard7b936c02005-10-30 17:05:13 +00002369#endif
bellarde95c8d52004-09-30 22:22:08 +00002370
blueswir18662d652008-10-02 18:32:44 +00002371static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002372{
bellard6a00d602005-11-21 23:25:50 +00002373 CPUState *env = mon_get_cpu();
2374 if (!env)
2375 return 0;
2376 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002377}
2378#endif
2379
blueswir18662d652008-10-02 18:32:44 +00002380static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002381#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002382
2383#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002384 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002385 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002386 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002387
bellard9307c4c2004-04-04 12:57:25 +00002388 { "eax", offsetof(CPUState, regs[0]) },
2389 { "ecx", offsetof(CPUState, regs[1]) },
2390 { "edx", offsetof(CPUState, regs[2]) },
2391 { "ebx", offsetof(CPUState, regs[3]) },
2392 { "esp|sp", offsetof(CPUState, regs[4]) },
2393 { "ebp|fp", offsetof(CPUState, regs[5]) },
2394 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002395 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002396#ifdef TARGET_X86_64
2397 { "r8", offsetof(CPUState, regs[8]) },
2398 { "r9", offsetof(CPUState, regs[9]) },
2399 { "r10", offsetof(CPUState, regs[10]) },
2400 { "r11", offsetof(CPUState, regs[11]) },
2401 { "r12", offsetof(CPUState, regs[12]) },
2402 { "r13", offsetof(CPUState, regs[13]) },
2403 { "r14", offsetof(CPUState, regs[14]) },
2404 { "r15", offsetof(CPUState, regs[15]) },
2405#endif
bellard9307c4c2004-04-04 12:57:25 +00002406 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002407 { "eip", offsetof(CPUState, eip) },
2408 SEG("cs", R_CS)
2409 SEG("ds", R_DS)
2410 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002411 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002412 SEG("fs", R_FS)
2413 SEG("gs", R_GS)
2414 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002415#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002416 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002417 { "r0", offsetof(CPUState, gpr[0]) },
2418 { "r1", offsetof(CPUState, gpr[1]) },
2419 { "r2", offsetof(CPUState, gpr[2]) },
2420 { "r3", offsetof(CPUState, gpr[3]) },
2421 { "r4", offsetof(CPUState, gpr[4]) },
2422 { "r5", offsetof(CPUState, gpr[5]) },
2423 { "r6", offsetof(CPUState, gpr[6]) },
2424 { "r7", offsetof(CPUState, gpr[7]) },
2425 { "r8", offsetof(CPUState, gpr[8]) },
2426 { "r9", offsetof(CPUState, gpr[9]) },
2427 { "r10", offsetof(CPUState, gpr[10]) },
2428 { "r11", offsetof(CPUState, gpr[11]) },
2429 { "r12", offsetof(CPUState, gpr[12]) },
2430 { "r13", offsetof(CPUState, gpr[13]) },
2431 { "r14", offsetof(CPUState, gpr[14]) },
2432 { "r15", offsetof(CPUState, gpr[15]) },
2433 { "r16", offsetof(CPUState, gpr[16]) },
2434 { "r17", offsetof(CPUState, gpr[17]) },
2435 { "r18", offsetof(CPUState, gpr[18]) },
2436 { "r19", offsetof(CPUState, gpr[19]) },
2437 { "r20", offsetof(CPUState, gpr[20]) },
2438 { "r21", offsetof(CPUState, gpr[21]) },
2439 { "r22", offsetof(CPUState, gpr[22]) },
2440 { "r23", offsetof(CPUState, gpr[23]) },
2441 { "r24", offsetof(CPUState, gpr[24]) },
2442 { "r25", offsetof(CPUState, gpr[25]) },
2443 { "r26", offsetof(CPUState, gpr[26]) },
2444 { "r27", offsetof(CPUState, gpr[27]) },
2445 { "r28", offsetof(CPUState, gpr[28]) },
2446 { "r29", offsetof(CPUState, gpr[29]) },
2447 { "r30", offsetof(CPUState, gpr[30]) },
2448 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002449 /* Floating point registers */
2450 { "f0", offsetof(CPUState, fpr[0]) },
2451 { "f1", offsetof(CPUState, fpr[1]) },
2452 { "f2", offsetof(CPUState, fpr[2]) },
2453 { "f3", offsetof(CPUState, fpr[3]) },
2454 { "f4", offsetof(CPUState, fpr[4]) },
2455 { "f5", offsetof(CPUState, fpr[5]) },
2456 { "f6", offsetof(CPUState, fpr[6]) },
2457 { "f7", offsetof(CPUState, fpr[7]) },
2458 { "f8", offsetof(CPUState, fpr[8]) },
2459 { "f9", offsetof(CPUState, fpr[9]) },
2460 { "f10", offsetof(CPUState, fpr[10]) },
2461 { "f11", offsetof(CPUState, fpr[11]) },
2462 { "f12", offsetof(CPUState, fpr[12]) },
2463 { "f13", offsetof(CPUState, fpr[13]) },
2464 { "f14", offsetof(CPUState, fpr[14]) },
2465 { "f15", offsetof(CPUState, fpr[15]) },
2466 { "f16", offsetof(CPUState, fpr[16]) },
2467 { "f17", offsetof(CPUState, fpr[17]) },
2468 { "f18", offsetof(CPUState, fpr[18]) },
2469 { "f19", offsetof(CPUState, fpr[19]) },
2470 { "f20", offsetof(CPUState, fpr[20]) },
2471 { "f21", offsetof(CPUState, fpr[21]) },
2472 { "f22", offsetof(CPUState, fpr[22]) },
2473 { "f23", offsetof(CPUState, fpr[23]) },
2474 { "f24", offsetof(CPUState, fpr[24]) },
2475 { "f25", offsetof(CPUState, fpr[25]) },
2476 { "f26", offsetof(CPUState, fpr[26]) },
2477 { "f27", offsetof(CPUState, fpr[27]) },
2478 { "f28", offsetof(CPUState, fpr[28]) },
2479 { "f29", offsetof(CPUState, fpr[29]) },
2480 { "f30", offsetof(CPUState, fpr[30]) },
2481 { "f31", offsetof(CPUState, fpr[31]) },
2482 { "fpscr", offsetof(CPUState, fpscr) },
2483 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002484 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002485 { "lr", offsetof(CPUState, lr) },
2486 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002487 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002488 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002489 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002490 { "msr", 0, &monitor_get_msr, },
2491 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002492 { "tbu", 0, &monitor_get_tbu, },
2493 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002494#if defined(TARGET_PPC64)
2495 /* Address space register */
2496 { "asr", offsetof(CPUState, asr) },
2497#endif
2498 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002499 { "sdr1", offsetof(CPUState, sdr1) },
2500 { "sr0", offsetof(CPUState, sr[0]) },
2501 { "sr1", offsetof(CPUState, sr[1]) },
2502 { "sr2", offsetof(CPUState, sr[2]) },
2503 { "sr3", offsetof(CPUState, sr[3]) },
2504 { "sr4", offsetof(CPUState, sr[4]) },
2505 { "sr5", offsetof(CPUState, sr[5]) },
2506 { "sr6", offsetof(CPUState, sr[6]) },
2507 { "sr7", offsetof(CPUState, sr[7]) },
2508 { "sr8", offsetof(CPUState, sr[8]) },
2509 { "sr9", offsetof(CPUState, sr[9]) },
2510 { "sr10", offsetof(CPUState, sr[10]) },
2511 { "sr11", offsetof(CPUState, sr[11]) },
2512 { "sr12", offsetof(CPUState, sr[12]) },
2513 { "sr13", offsetof(CPUState, sr[13]) },
2514 { "sr14", offsetof(CPUState, sr[14]) },
2515 { "sr15", offsetof(CPUState, sr[15]) },
2516 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002517#elif defined(TARGET_SPARC)
2518 { "g0", offsetof(CPUState, gregs[0]) },
2519 { "g1", offsetof(CPUState, gregs[1]) },
2520 { "g2", offsetof(CPUState, gregs[2]) },
2521 { "g3", offsetof(CPUState, gregs[3]) },
2522 { "g4", offsetof(CPUState, gregs[4]) },
2523 { "g5", offsetof(CPUState, gregs[5]) },
2524 { "g6", offsetof(CPUState, gregs[6]) },
2525 { "g7", offsetof(CPUState, gregs[7]) },
2526 { "o0", 0, monitor_get_reg },
2527 { "o1", 1, monitor_get_reg },
2528 { "o2", 2, monitor_get_reg },
2529 { "o3", 3, monitor_get_reg },
2530 { "o4", 4, monitor_get_reg },
2531 { "o5", 5, monitor_get_reg },
2532 { "o6", 6, monitor_get_reg },
2533 { "o7", 7, monitor_get_reg },
2534 { "l0", 8, monitor_get_reg },
2535 { "l1", 9, monitor_get_reg },
2536 { "l2", 10, monitor_get_reg },
2537 { "l3", 11, monitor_get_reg },
2538 { "l4", 12, monitor_get_reg },
2539 { "l5", 13, monitor_get_reg },
2540 { "l6", 14, monitor_get_reg },
2541 { "l7", 15, monitor_get_reg },
2542 { "i0", 16, monitor_get_reg },
2543 { "i1", 17, monitor_get_reg },
2544 { "i2", 18, monitor_get_reg },
2545 { "i3", 19, monitor_get_reg },
2546 { "i4", 20, monitor_get_reg },
2547 { "i5", 21, monitor_get_reg },
2548 { "i6", 22, monitor_get_reg },
2549 { "i7", 23, monitor_get_reg },
2550 { "pc", offsetof(CPUState, pc) },
2551 { "npc", offsetof(CPUState, npc) },
2552 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002553#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002554 { "psr", 0, &monitor_get_psr, },
2555 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002556#endif
bellarde95c8d52004-09-30 22:22:08 +00002557 { "tbr", offsetof(CPUState, tbr) },
2558 { "fsr", offsetof(CPUState, fsr) },
2559 { "f0", offsetof(CPUState, fpr[0]) },
2560 { "f1", offsetof(CPUState, fpr[1]) },
2561 { "f2", offsetof(CPUState, fpr[2]) },
2562 { "f3", offsetof(CPUState, fpr[3]) },
2563 { "f4", offsetof(CPUState, fpr[4]) },
2564 { "f5", offsetof(CPUState, fpr[5]) },
2565 { "f6", offsetof(CPUState, fpr[6]) },
2566 { "f7", offsetof(CPUState, fpr[7]) },
2567 { "f8", offsetof(CPUState, fpr[8]) },
2568 { "f9", offsetof(CPUState, fpr[9]) },
2569 { "f10", offsetof(CPUState, fpr[10]) },
2570 { "f11", offsetof(CPUState, fpr[11]) },
2571 { "f12", offsetof(CPUState, fpr[12]) },
2572 { "f13", offsetof(CPUState, fpr[13]) },
2573 { "f14", offsetof(CPUState, fpr[14]) },
2574 { "f15", offsetof(CPUState, fpr[15]) },
2575 { "f16", offsetof(CPUState, fpr[16]) },
2576 { "f17", offsetof(CPUState, fpr[17]) },
2577 { "f18", offsetof(CPUState, fpr[18]) },
2578 { "f19", offsetof(CPUState, fpr[19]) },
2579 { "f20", offsetof(CPUState, fpr[20]) },
2580 { "f21", offsetof(CPUState, fpr[21]) },
2581 { "f22", offsetof(CPUState, fpr[22]) },
2582 { "f23", offsetof(CPUState, fpr[23]) },
2583 { "f24", offsetof(CPUState, fpr[24]) },
2584 { "f25", offsetof(CPUState, fpr[25]) },
2585 { "f26", offsetof(CPUState, fpr[26]) },
2586 { "f27", offsetof(CPUState, fpr[27]) },
2587 { "f28", offsetof(CPUState, fpr[28]) },
2588 { "f29", offsetof(CPUState, fpr[29]) },
2589 { "f30", offsetof(CPUState, fpr[30]) },
2590 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002591#ifdef TARGET_SPARC64
2592 { "f32", offsetof(CPUState, fpr[32]) },
2593 { "f34", offsetof(CPUState, fpr[34]) },
2594 { "f36", offsetof(CPUState, fpr[36]) },
2595 { "f38", offsetof(CPUState, fpr[38]) },
2596 { "f40", offsetof(CPUState, fpr[40]) },
2597 { "f42", offsetof(CPUState, fpr[42]) },
2598 { "f44", offsetof(CPUState, fpr[44]) },
2599 { "f46", offsetof(CPUState, fpr[46]) },
2600 { "f48", offsetof(CPUState, fpr[48]) },
2601 { "f50", offsetof(CPUState, fpr[50]) },
2602 { "f52", offsetof(CPUState, fpr[52]) },
2603 { "f54", offsetof(CPUState, fpr[54]) },
2604 { "f56", offsetof(CPUState, fpr[56]) },
2605 { "f58", offsetof(CPUState, fpr[58]) },
2606 { "f60", offsetof(CPUState, fpr[60]) },
2607 { "f62", offsetof(CPUState, fpr[62]) },
2608 { "asi", offsetof(CPUState, asi) },
2609 { "pstate", offsetof(CPUState, pstate) },
2610 { "cansave", offsetof(CPUState, cansave) },
2611 { "canrestore", offsetof(CPUState, canrestore) },
2612 { "otherwin", offsetof(CPUState, otherwin) },
2613 { "wstate", offsetof(CPUState, wstate) },
2614 { "cleanwin", offsetof(CPUState, cleanwin) },
2615 { "fprs", offsetof(CPUState, fprs) },
2616#endif
bellard9307c4c2004-04-04 12:57:25 +00002617#endif
2618 { NULL },
2619};
2620
aliguori376253e2009-03-05 23:01:23 +00002621static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002622{
aliguori376253e2009-03-05 23:01:23 +00002623 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002624 longjmp(expr_env, 1);
2625}
2626
bellard6a00d602005-11-21 23:25:50 +00002627/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002628static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002629{
blueswir18662d652008-10-02 18:32:44 +00002630 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002631 void *ptr;
2632
bellard9307c4c2004-04-04 12:57:25 +00002633 for(md = monitor_defs; md->name != NULL; md++) {
2634 if (compare_cmd(name, md->name)) {
2635 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002636 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002637 } else {
bellard6a00d602005-11-21 23:25:50 +00002638 CPUState *env = mon_get_cpu();
2639 if (!env)
2640 return -2;
2641 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002642 switch(md->type) {
2643 case MD_I32:
2644 *pval = *(int32_t *)ptr;
2645 break;
2646 case MD_TLONG:
2647 *pval = *(target_long *)ptr;
2648 break;
2649 default:
2650 *pval = 0;
2651 break;
2652 }
bellard9307c4c2004-04-04 12:57:25 +00002653 }
2654 return 0;
2655 }
2656 }
2657 return -1;
2658}
2659
2660static void next(void)
2661{
Blue Swirl660f11b2009-07-31 21:16:51 +00002662 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002663 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002664 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002665 pch++;
2666 }
2667}
2668
aliguori376253e2009-03-05 23:01:23 +00002669static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002670
aliguori376253e2009-03-05 23:01:23 +00002671static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002672{
blueswir1c2efc952007-09-25 17:28:42 +00002673 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002674 char *p;
bellard6a00d602005-11-21 23:25:50 +00002675 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002676
2677 switch(*pch) {
2678 case '+':
2679 next();
aliguori376253e2009-03-05 23:01:23 +00002680 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002681 break;
2682 case '-':
2683 next();
aliguori376253e2009-03-05 23:01:23 +00002684 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002685 break;
2686 case '~':
2687 next();
aliguori376253e2009-03-05 23:01:23 +00002688 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002689 break;
2690 case '(':
2691 next();
aliguori376253e2009-03-05 23:01:23 +00002692 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002693 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002694 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002695 }
2696 next();
2697 break;
bellard81d09122004-07-14 17:21:37 +00002698 case '\'':
2699 pch++;
2700 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002701 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002702 n = *pch;
2703 pch++;
2704 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002705 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002706 next();
2707 break;
bellard9307c4c2004-04-04 12:57:25 +00002708 case '$':
2709 {
2710 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002711 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002712
bellard9307c4c2004-04-04 12:57:25 +00002713 pch++;
2714 q = buf;
2715 while ((*pch >= 'a' && *pch <= 'z') ||
2716 (*pch >= 'A' && *pch <= 'Z') ||
2717 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002718 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002719 if ((q - buf) < sizeof(buf) - 1)
2720 *q++ = *pch;
2721 pch++;
2722 }
blueswir1cd390082008-11-16 13:53:32 +00002723 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002724 pch++;
2725 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002726 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002727 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002728 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002729 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002730 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002731 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002732 }
2733 break;
2734 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002735 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002736 n = 0;
2737 break;
2738 default:
blueswir17743e582007-09-24 18:39:04 +00002739#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002740 n = strtoull(pch, &p, 0);
2741#else
bellard9307c4c2004-04-04 12:57:25 +00002742 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002743#endif
bellard9307c4c2004-04-04 12:57:25 +00002744 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002745 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002746 }
2747 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002748 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002749 pch++;
2750 break;
2751 }
2752 return n;
2753}
2754
2755
aliguori376253e2009-03-05 23:01:23 +00002756static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002757{
blueswir1c2efc952007-09-25 17:28:42 +00002758 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002759 int op;
ths3b46e622007-09-17 08:09:54 +00002760
aliguori376253e2009-03-05 23:01:23 +00002761 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002762 for(;;) {
2763 op = *pch;
2764 if (op != '*' && op != '/' && op != '%')
2765 break;
2766 next();
aliguori376253e2009-03-05 23:01:23 +00002767 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002768 switch(op) {
2769 default:
2770 case '*':
2771 val *= val2;
2772 break;
2773 case '/':
2774 case '%':
ths5fafdf22007-09-16 21:08:06 +00002775 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002776 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002777 if (op == '/')
2778 val /= val2;
2779 else
2780 val %= val2;
2781 break;
2782 }
2783 }
2784 return val;
2785}
2786
aliguori376253e2009-03-05 23:01:23 +00002787static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002788{
blueswir1c2efc952007-09-25 17:28:42 +00002789 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002790 int op;
bellard9307c4c2004-04-04 12:57:25 +00002791
aliguori376253e2009-03-05 23:01:23 +00002792 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002793 for(;;) {
2794 op = *pch;
2795 if (op != '&' && op != '|' && op != '^')
2796 break;
2797 next();
aliguori376253e2009-03-05 23:01:23 +00002798 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002799 switch(op) {
2800 default:
2801 case '&':
2802 val &= val2;
2803 break;
2804 case '|':
2805 val |= val2;
2806 break;
2807 case '^':
2808 val ^= val2;
2809 break;
2810 }
2811 }
2812 return val;
2813}
2814
aliguori376253e2009-03-05 23:01:23 +00002815static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002816{
blueswir1c2efc952007-09-25 17:28:42 +00002817 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002818 int op;
bellard9307c4c2004-04-04 12:57:25 +00002819
aliguori376253e2009-03-05 23:01:23 +00002820 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002821 for(;;) {
2822 op = *pch;
2823 if (op != '+' && op != '-')
2824 break;
2825 next();
aliguori376253e2009-03-05 23:01:23 +00002826 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002827 if (op == '+')
2828 val += val2;
2829 else
2830 val -= val2;
2831 }
2832 return val;
2833}
2834
aliguori376253e2009-03-05 23:01:23 +00002835static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002836{
2837 pch = *pp;
2838 if (setjmp(expr_env)) {
2839 *pp = pch;
2840 return -1;
2841 }
blueswir1cd390082008-11-16 13:53:32 +00002842 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002843 pch++;
aliguori376253e2009-03-05 23:01:23 +00002844 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002845 *pp = pch;
2846 return 0;
2847}
2848
2849static int get_str(char *buf, int buf_size, const char **pp)
2850{
2851 const char *p;
2852 char *q;
2853 int c;
2854
bellard81d09122004-07-14 17:21:37 +00002855 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002856 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002857 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002858 p++;
2859 if (*p == '\0') {
2860 fail:
bellard81d09122004-07-14 17:21:37 +00002861 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002862 *pp = p;
2863 return -1;
2864 }
bellard9307c4c2004-04-04 12:57:25 +00002865 if (*p == '\"') {
2866 p++;
2867 while (*p != '\0' && *p != '\"') {
2868 if (*p == '\\') {
2869 p++;
2870 c = *p++;
2871 switch(c) {
2872 case 'n':
2873 c = '\n';
2874 break;
2875 case 'r':
2876 c = '\r';
2877 break;
2878 case '\\':
2879 case '\'':
2880 case '\"':
2881 break;
2882 default:
2883 qemu_printf("unsupported escape code: '\\%c'\n", c);
2884 goto fail;
2885 }
2886 if ((q - buf) < buf_size - 1) {
2887 *q++ = c;
2888 }
2889 } else {
2890 if ((q - buf) < buf_size - 1) {
2891 *q++ = *p;
2892 }
2893 p++;
2894 }
2895 }
2896 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002897 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002898 goto fail;
2899 }
2900 p++;
2901 } else {
blueswir1cd390082008-11-16 13:53:32 +00002902 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002903 if ((q - buf) < buf_size - 1) {
2904 *q++ = *p;
2905 }
2906 p++;
2907 }
bellard9307c4c2004-04-04 12:57:25 +00002908 }
bellard81d09122004-07-14 17:21:37 +00002909 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002910 *pp = p;
2911 return 0;
2912}
2913
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002914/*
2915 * Store the command-name in cmdname, and return a pointer to
2916 * the remaining of the command string.
2917 */
2918static const char *get_command_name(const char *cmdline,
2919 char *cmdname, size_t nlen)
2920{
2921 size_t len;
2922 const char *p, *pstart;
2923
2924 p = cmdline;
2925 while (qemu_isspace(*p))
2926 p++;
2927 if (*p == '\0')
2928 return NULL;
2929 pstart = p;
2930 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2931 p++;
2932 len = p - pstart;
2933 if (len > nlen - 1)
2934 len = nlen - 1;
2935 memcpy(cmdname, pstart, len);
2936 cmdname[len] = '\0';
2937 return p;
2938}
2939
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002940/**
2941 * Read key of 'type' into 'key' and return the current
2942 * 'type' pointer.
2943 */
2944static char *key_get_info(const char *type, char **key)
2945{
2946 size_t len;
2947 char *p, *str;
2948
2949 if (*type == ',')
2950 type++;
2951
2952 p = strchr(type, ':');
2953 if (!p) {
2954 *key = NULL;
2955 return NULL;
2956 }
2957 len = p - type;
2958
2959 str = qemu_malloc(len + 1);
2960 memcpy(str, type, len);
2961 str[len] = '\0';
2962
2963 *key = str;
2964 return ++p;
2965}
2966
bellard9307c4c2004-04-04 12:57:25 +00002967static int default_fmt_format = 'x';
2968static int default_fmt_size = 4;
2969
2970#define MAX_ARGS 16
2971
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002972static int is_valid_option(const char *c, const char *typestr)
2973{
2974 char option[3];
2975
2976 option[0] = '-';
2977 option[1] = *c;
2978 option[2] = '\0';
2979
2980 typestr = strstr(typestr, option);
2981 return (typestr != NULL);
2982}
2983
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002984static const mon_cmd_t *monitor_find_command(const char *cmdname)
2985{
2986 const mon_cmd_t *cmd;
2987
2988 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
2989 if (compare_cmd(cmdname, cmd->name)) {
2990 return cmd;
2991 }
2992 }
2993
2994 return NULL;
2995}
2996
Anthony Liguoric227f092009-10-01 16:12:16 -05002997static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002998 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002999 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003000{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003001 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003002 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003003 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003004 char cmdname[256];
3005 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003006 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003007
3008#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003009 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003010#endif
ths3b46e622007-09-17 08:09:54 +00003011
bellard9307c4c2004-04-04 12:57:25 +00003012 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003013 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3014 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003015 return NULL;
ths3b46e622007-09-17 08:09:54 +00003016
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003017 cmd = monitor_find_command(cmdname);
3018 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003019 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003020 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003021 }
bellard9307c4c2004-04-04 12:57:25 +00003022
bellard9307c4c2004-04-04 12:57:25 +00003023 /* parse the parameters */
3024 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003025 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003026 typestr = key_get_info(typestr, &key);
3027 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003028 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003029 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003030 typestr++;
3031 switch(c) {
3032 case 'F':
bellard81d09122004-07-14 17:21:37 +00003033 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003034 case 's':
3035 {
3036 int ret;
ths3b46e622007-09-17 08:09:54 +00003037
blueswir1cd390082008-11-16 13:53:32 +00003038 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003039 p++;
3040 if (*typestr == '?') {
3041 typestr++;
3042 if (*p == '\0') {
3043 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003044 break;
bellard9307c4c2004-04-04 12:57:25 +00003045 }
3046 }
3047 ret = get_str(buf, sizeof(buf), &p);
3048 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003049 switch(c) {
3050 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003051 monitor_printf(mon, "%s: filename expected\n",
3052 cmdname);
bellard81d09122004-07-14 17:21:37 +00003053 break;
3054 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003055 monitor_printf(mon, "%s: block device name expected\n",
3056 cmdname);
bellard81d09122004-07-14 17:21:37 +00003057 break;
3058 default:
aliguori376253e2009-03-05 23:01:23 +00003059 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003060 break;
3061 }
bellard9307c4c2004-04-04 12:57:25 +00003062 goto fail;
3063 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003064 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003065 }
3066 break;
3067 case '/':
3068 {
3069 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003070
blueswir1cd390082008-11-16 13:53:32 +00003071 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003072 p++;
3073 if (*p == '/') {
3074 /* format found */
3075 p++;
3076 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003077 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003078 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003079 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003080 count = count * 10 + (*p - '0');
3081 p++;
3082 }
3083 }
3084 size = -1;
3085 format = -1;
3086 for(;;) {
3087 switch(*p) {
3088 case 'o':
3089 case 'd':
3090 case 'u':
3091 case 'x':
3092 case 'i':
3093 case 'c':
3094 format = *p++;
3095 break;
3096 case 'b':
3097 size = 1;
3098 p++;
3099 break;
3100 case 'h':
3101 size = 2;
3102 p++;
3103 break;
3104 case 'w':
3105 size = 4;
3106 p++;
3107 break;
3108 case 'g':
3109 case 'L':
3110 size = 8;
3111 p++;
3112 break;
3113 default:
3114 goto next;
3115 }
3116 }
3117 next:
blueswir1cd390082008-11-16 13:53:32 +00003118 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003119 monitor_printf(mon, "invalid char in format: '%c'\n",
3120 *p);
bellard9307c4c2004-04-04 12:57:25 +00003121 goto fail;
3122 }
bellard9307c4c2004-04-04 12:57:25 +00003123 if (format < 0)
3124 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003125 if (format != 'i') {
3126 /* for 'i', not specifying a size gives -1 as size */
3127 if (size < 0)
3128 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003129 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003130 }
bellard9307c4c2004-04-04 12:57:25 +00003131 default_fmt_format = format;
3132 } else {
3133 count = 1;
3134 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003135 if (format != 'i') {
3136 size = default_fmt_size;
3137 } else {
3138 size = -1;
3139 }
bellard9307c4c2004-04-04 12:57:25 +00003140 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003141 qdict_put(qdict, "count", qint_from_int(count));
3142 qdict_put(qdict, "format", qint_from_int(format));
3143 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003144 }
3145 break;
3146 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003147 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00003148 {
blueswir1c2efc952007-09-25 17:28:42 +00003149 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003150
blueswir1cd390082008-11-16 13:53:32 +00003151 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003152 p++;
bellard34405572004-06-08 00:55:58 +00003153 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003154 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003155 if (*p == '\0') {
3156 typestr++;
3157 break;
3158 }
bellard34405572004-06-08 00:55:58 +00003159 } else {
3160 if (*p == '.') {
3161 p++;
blueswir1cd390082008-11-16 13:53:32 +00003162 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003163 p++;
bellard34405572004-06-08 00:55:58 +00003164 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003165 typestr++;
3166 break;
bellard34405572004-06-08 00:55:58 +00003167 }
3168 }
bellard13224a82006-07-14 22:03:35 +00003169 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003170 }
aliguori376253e2009-03-05 23:01:23 +00003171 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003172 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003173 /* Check if 'i' is greater than 32-bit */
3174 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3175 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3176 monitor_printf(mon, "integer is for 32-bit values\n");
3177 goto fail;
3178 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003179 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003180 }
3181 break;
3182 case '-':
3183 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003184 const char *tmp = p;
3185 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003186 /* option */
ths3b46e622007-09-17 08:09:54 +00003187
bellard9307c4c2004-04-04 12:57:25 +00003188 c = *typestr++;
3189 if (c == '\0')
3190 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003191 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003192 p++;
3193 has_option = 0;
3194 if (*p == '-') {
3195 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003196 if(c != *p) {
3197 if(!is_valid_option(p, typestr)) {
3198
3199 monitor_printf(mon, "%s: unsupported option -%c\n",
3200 cmdname, *p);
3201 goto fail;
3202 } else {
3203 skip_key = 1;
3204 }
bellard9307c4c2004-04-04 12:57:25 +00003205 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003206 if(skip_key) {
3207 p = tmp;
3208 } else {
3209 p++;
3210 has_option = 1;
3211 }
bellard9307c4c2004-04-04 12:57:25 +00003212 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003213 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003214 }
3215 break;
3216 default:
3217 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003218 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003219 goto fail;
3220 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003221 qemu_free(key);
3222 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003223 }
3224 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003225 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003226 p++;
3227 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003228 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3229 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003230 goto fail;
3231 }
3232
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003233 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003234
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003235fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003236 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003237 return NULL;
3238}
3239
Luiz Capitulino8204a912009-11-18 23:05:31 -02003240static void monitor_print_error(Monitor *mon)
3241{
3242 qerror_print(mon->error);
3243 QDECREF(mon->error);
3244 mon->error = NULL;
3245}
3246
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003247static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3248 const QDict *params)
3249{
3250 QObject *data = NULL;
3251
3252 cmd->mhandler.cmd_new(mon, params, &data);
3253 if (data)
3254 cmd->user_print(mon, data);
3255
3256 qobject_decref(data);
3257}
3258
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003259static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003260{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003261 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003262 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003263
3264 qdict = qdict_new();
3265
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003266 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003267 if (!cmd)
3268 goto out;
3269
3270 qemu_errors_to_mon(mon);
3271
3272 if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003273 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003274 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003275 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003276 }
3277
Luiz Capitulino8204a912009-11-18 23:05:31 -02003278 if (monitor_has_error(mon))
3279 monitor_print_error(mon);
3280
3281 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003282
3283out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003284 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003285}
3286
bellard81d09122004-07-14 17:21:37 +00003287static void cmd_completion(const char *name, const char *list)
3288{
3289 const char *p, *pstart;
3290 char cmd[128];
3291 int len;
3292
3293 p = list;
3294 for(;;) {
3295 pstart = p;
3296 p = strchr(p, '|');
3297 if (!p)
3298 p = pstart + strlen(pstart);
3299 len = p - pstart;
3300 if (len > sizeof(cmd) - 2)
3301 len = sizeof(cmd) - 2;
3302 memcpy(cmd, pstart, len);
3303 cmd[len] = '\0';
3304 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003305 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003306 }
3307 if (*p == '\0')
3308 break;
3309 p++;
3310 }
3311}
3312
3313static void file_completion(const char *input)
3314{
3315 DIR *ffs;
3316 struct dirent *d;
3317 char path[1024];
3318 char file[1024], file_prefix[1024];
3319 int input_path_len;
3320 const char *p;
3321
ths5fafdf22007-09-16 21:08:06 +00003322 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003323 if (!p) {
3324 input_path_len = 0;
3325 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003326 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003327 } else {
3328 input_path_len = p - input + 1;
3329 memcpy(path, input, input_path_len);
3330 if (input_path_len > sizeof(path) - 1)
3331 input_path_len = sizeof(path) - 1;
3332 path[input_path_len] = '\0';
3333 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3334 }
3335#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003336 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3337 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003338#endif
3339 ffs = opendir(path);
3340 if (!ffs)
3341 return;
3342 for(;;) {
3343 struct stat sb;
3344 d = readdir(ffs);
3345 if (!d)
3346 break;
3347 if (strstart(d->d_name, file_prefix, NULL)) {
3348 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003349 if (input_path_len < sizeof(file))
3350 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3351 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003352 /* stat the file to find out if it's a directory.
3353 * In that case add a slash to speed up typing long paths
3354 */
3355 stat(file, &sb);
3356 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003357 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003358 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003359 }
3360 }
3361 closedir(ffs);
3362}
3363
aliguori51de9762009-03-05 23:00:43 +00003364static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003365{
aliguori51de9762009-03-05 23:00:43 +00003366 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003367 const char *input = opaque;
3368
3369 if (input[0] == '\0' ||
3370 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003371 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003372 }
3373}
3374
3375/* NOTE: this parser is an approximate form of the real command parser */
3376static void parse_cmdline(const char *cmdline,
3377 int *pnb_args, char **args)
3378{
3379 const char *p;
3380 int nb_args, ret;
3381 char buf[1024];
3382
3383 p = cmdline;
3384 nb_args = 0;
3385 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003386 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003387 p++;
3388 if (*p == '\0')
3389 break;
3390 if (nb_args >= MAX_ARGS)
3391 break;
3392 ret = get_str(buf, sizeof(buf), &p);
3393 args[nb_args] = qemu_strdup(buf);
3394 nb_args++;
3395 if (ret < 0)
3396 break;
3397 }
3398 *pnb_args = nb_args;
3399}
3400
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003401static const char *next_arg_type(const char *typestr)
3402{
3403 const char *p = strchr(typestr, ':');
3404 return (p != NULL ? ++p : typestr);
3405}
3406
aliguori4c36ba32009-03-05 23:01:37 +00003407static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003408{
3409 const char *cmdname;
3410 char *args[MAX_ARGS];
3411 int nb_args, i, len;
3412 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003413 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003414 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003415
3416 parse_cmdline(cmdline, &nb_args, args);
3417#ifdef DEBUG_COMPLETION
3418 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003419 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003420 }
3421#endif
3422
3423 /* if the line ends with a space, it means we want to complete the
3424 next arg */
3425 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003426 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003427 if (nb_args >= MAX_ARGS)
3428 return;
3429 args[nb_args++] = qemu_strdup("");
3430 }
3431 if (nb_args <= 1) {
3432 /* command completion */
3433 if (nb_args == 0)
3434 cmdname = "";
3435 else
3436 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003437 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003438 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003439 cmd_completion(cmdname, cmd->name);
3440 }
3441 } else {
3442 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003443 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003444 if (compare_cmd(args[0], cmd->name))
3445 goto found;
3446 }
3447 return;
3448 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003449 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003450 for(i = 0; i < nb_args - 2; i++) {
3451 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003452 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003453 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003454 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003455 }
3456 }
3457 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003458 if (*ptype == '-' && ptype[1] != '\0') {
3459 ptype += 2;
3460 }
bellard81d09122004-07-14 17:21:37 +00003461 switch(*ptype) {
3462 case 'F':
3463 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003464 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003465 file_completion(str);
3466 break;
3467 case 'B':
3468 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003469 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003470 bdrv_iterate(block_completion_it, (void *)str);
3471 break;
bellard7fe48482004-10-09 18:08:01 +00003472 case 's':
3473 /* XXX: more generic ? */
3474 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003475 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003476 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3477 cmd_completion(str, cmd->name);
3478 }
bellard64866c32006-05-07 18:03:31 +00003479 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003480 char *sep = strrchr(str, '-');
3481 if (sep)
3482 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003483 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003484 for(key = key_defs; key->name != NULL; key++) {
3485 cmd_completion(str, key->name);
3486 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003487 } else if (!strcmp(cmd->name, "help|?")) {
3488 readline_set_completion_index(cur_mon->rs, strlen(str));
3489 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3490 cmd_completion(str, cmd->name);
3491 }
bellard7fe48482004-10-09 18:08:01 +00003492 }
3493 break;
bellard81d09122004-07-14 17:21:37 +00003494 default:
3495 break;
3496 }
3497 }
3498 for(i = 0; i < nb_args; i++)
3499 qemu_free(args[i]);
3500}
3501
aliguori731b0362009-03-05 23:01:42 +00003502static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003503{
aliguori731b0362009-03-05 23:01:42 +00003504 Monitor *mon = opaque;
3505
3506 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003507}
3508
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003509/**
3510 * monitor_control_read(): Read and handle QMP input
3511 */
3512static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
3513{
3514 Monitor *old_mon = cur_mon;
3515
3516 cur_mon = opaque;
3517
3518 // TODO: read QMP input
3519
3520 cur_mon = old_mon;
3521}
3522
aliguori731b0362009-03-05 23:01:42 +00003523static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003524{
aliguori731b0362009-03-05 23:01:42 +00003525 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003526 int i;
aliguori376253e2009-03-05 23:01:23 +00003527
aliguori731b0362009-03-05 23:01:42 +00003528 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003529
aliguoricde76ee2009-03-05 23:01:51 +00003530 if (cur_mon->rs) {
3531 for (i = 0; i < size; i++)
3532 readline_handle_byte(cur_mon->rs, buf[i]);
3533 } else {
3534 if (size == 0 || buf[size - 1] != 0)
3535 monitor_printf(cur_mon, "corrupted command\n");
3536 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003537 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003538 }
aliguori731b0362009-03-05 23:01:42 +00003539
3540 cur_mon = old_mon;
3541}
aliguorid8f44602008-10-06 13:52:44 +00003542
aliguori376253e2009-03-05 23:01:23 +00003543static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003544{
aliguori731b0362009-03-05 23:01:42 +00003545 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003546 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003547 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003548}
3549
aliguoricde76ee2009-03-05 23:01:51 +00003550int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003551{
aliguoricde76ee2009-03-05 23:01:51 +00003552 if (!mon->rs)
3553 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003554 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003555 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003556}
3557
aliguori376253e2009-03-05 23:01:23 +00003558void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003559{
aliguoricde76ee2009-03-05 23:01:51 +00003560 if (!mon->rs)
3561 return;
aliguori731b0362009-03-05 23:01:42 +00003562 if (--mon->suspend_cnt == 0)
3563 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003564}
3565
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003566/**
3567 * monitor_control_event(): Print QMP gretting
3568 */
3569static void monitor_control_event(void *opaque, int event)
3570{
3571 if (event == CHR_EVENT_OPENED) {
3572 QObject *data;
3573 Monitor *mon = opaque;
3574
3575 data = qobject_from_jsonf("{ 'QMP': { 'capabilities': [] } }");
3576 assert(data != NULL);
3577
3578 monitor_json_emitter(mon, data);
3579 qobject_decref(data);
3580 }
3581}
3582
aliguori731b0362009-03-05 23:01:42 +00003583static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003584{
aliguori376253e2009-03-05 23:01:23 +00003585 Monitor *mon = opaque;
3586
aliguori2724b182009-03-05 23:01:47 +00003587 switch (event) {
3588 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003589 mon->mux_out = 0;
3590 if (mon->reset_seen) {
3591 readline_restart(mon->rs);
3592 monitor_resume(mon);
3593 monitor_flush(mon);
3594 } else {
3595 mon->suspend_cnt = 0;
3596 }
aliguori2724b182009-03-05 23:01:47 +00003597 break;
ths86e94de2007-01-05 22:01:59 +00003598
aliguori2724b182009-03-05 23:01:47 +00003599 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003600 if (mon->reset_seen) {
3601 if (mon->suspend_cnt == 0) {
3602 monitor_printf(mon, "\n");
3603 }
3604 monitor_flush(mon);
3605 monitor_suspend(mon);
3606 } else {
3607 mon->suspend_cnt++;
3608 }
3609 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00003610 break;
3611
Amit Shahb6b8df52009-10-07 18:31:16 +05303612 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003613 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3614 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003615 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00003616 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003617 }
3618 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00003619 break;
3620 }
ths86e94de2007-01-05 22:01:59 +00003621}
3622
aliguori76655d62009-03-06 20:27:37 +00003623
3624/*
3625 * Local variables:
3626 * c-indent-level: 4
3627 * c-basic-offset: 4
3628 * tab-width: 8
3629 * End:
3630 */
3631
Luiz Capitulinoadcb1812009-11-26 22:58:52 -02003632const char *monitor_cmdline_parse(const char *cmdline, int *flags)
3633{
3634 const char *dev;
3635
3636 if (strstart(cmdline, "control,", &dev)) {
3637 if (strstart(dev, "vc", NULL)) {
3638 fprintf(stderr, "qemu: control mode is for low-level interaction ");
3639 fprintf(stderr, "cannot be used with device 'vc'\n");
3640 exit(1);
3641 }
3642 *flags &= ~MONITOR_USE_READLINE;
3643 *flags |= MONITOR_USE_CONTROL;
3644 return dev;
3645 }
3646
3647 return cmdline;
3648}
3649
aliguori731b0362009-03-05 23:01:42 +00003650void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003651{
aliguori731b0362009-03-05 23:01:42 +00003652 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003653 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003654
3655 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003656 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003657 is_first_init = 0;
3658 }
aliguori87127162009-03-05 23:01:29 +00003659
3660 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003661
aliguori87127162009-03-05 23:01:29 +00003662 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003663 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00003664 if (flags & MONITOR_USE_READLINE) {
3665 mon->rs = readline_init(mon, monitor_find_completion);
3666 monitor_read_command(mon, 0);
3667 }
aliguori87127162009-03-05 23:01:29 +00003668
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003669 if (monitor_ctrl_mode(mon)) {
3670 /* Control mode requires special handlers */
3671 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
3672 monitor_control_event, mon);
3673 } else {
3674 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
3675 monitor_event, mon);
3676 }
aliguori87127162009-03-05 23:01:29 +00003677
Blue Swirl72cf2d42009-09-12 07:36:22 +00003678 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003679 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003680 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003681}
3682
aliguori376253e2009-03-05 23:01:23 +00003683static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003684{
aliguoribb5fc202009-03-05 23:01:15 +00003685 BlockDriverState *bs = opaque;
3686 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003687
aliguoribb5fc202009-03-05 23:01:15 +00003688 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003689 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003690 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003691 }
aliguori731b0362009-03-05 23:01:42 +00003692 if (mon->password_completion_cb)
3693 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003694
aliguori731b0362009-03-05 23:01:42 +00003695 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003696}
aliguoric0f4ce72009-03-05 23:01:01 +00003697
aliguori376253e2009-03-05 23:01:23 +00003698void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003699 BlockDriverCompletionFunc *completion_cb,
3700 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003701{
aliguoricde76ee2009-03-05 23:01:51 +00003702 int err;
3703
aliguoribb5fc202009-03-05 23:01:15 +00003704 if (!bdrv_key_required(bs)) {
3705 if (completion_cb)
3706 completion_cb(opaque, 0);
3707 return;
3708 }
aliguoric0f4ce72009-03-05 23:01:01 +00003709
aliguori376253e2009-03-05 23:01:23 +00003710 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3711 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003712
aliguori731b0362009-03-05 23:01:42 +00003713 mon->password_completion_cb = completion_cb;
3714 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003715
aliguoricde76ee2009-03-05 23:01:51 +00003716 err = monitor_read_password(mon, bdrv_password_cb, bs);
3717
3718 if (err && completion_cb)
3719 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003720}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003721
3722typedef struct QemuErrorSink QemuErrorSink;
3723struct QemuErrorSink {
3724 enum {
3725 ERR_SINK_FILE,
3726 ERR_SINK_MONITOR,
3727 } dest;
3728 union {
3729 FILE *fp;
3730 Monitor *mon;
3731 };
3732 QemuErrorSink *previous;
3733};
3734
Blue Swirl528e93a2009-08-31 15:14:40 +00003735static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003736
3737void qemu_errors_to_file(FILE *fp)
3738{
3739 QemuErrorSink *sink;
3740
3741 sink = qemu_mallocz(sizeof(*sink));
3742 sink->dest = ERR_SINK_FILE;
3743 sink->fp = fp;
3744 sink->previous = qemu_error_sink;
3745 qemu_error_sink = sink;
3746}
3747
3748void qemu_errors_to_mon(Monitor *mon)
3749{
3750 QemuErrorSink *sink;
3751
3752 sink = qemu_mallocz(sizeof(*sink));
3753 sink->dest = ERR_SINK_MONITOR;
3754 sink->mon = mon;
3755 sink->previous = qemu_error_sink;
3756 qemu_error_sink = sink;
3757}
3758
3759void qemu_errors_to_previous(void)
3760{
3761 QemuErrorSink *sink;
3762
3763 assert(qemu_error_sink != NULL);
3764 sink = qemu_error_sink;
3765 qemu_error_sink = sink->previous;
3766 qemu_free(sink);
3767}
3768
3769void qemu_error(const char *fmt, ...)
3770{
3771 va_list args;
3772
3773 assert(qemu_error_sink != NULL);
3774 switch (qemu_error_sink->dest) {
3775 case ERR_SINK_FILE:
3776 va_start(args, fmt);
3777 vfprintf(qemu_error_sink->fp, fmt, args);
3778 va_end(args);
3779 break;
3780 case ERR_SINK_MONITOR:
3781 va_start(args, fmt);
3782 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3783 va_end(args);
3784 break;
3785 }
3786}
Luiz Capitulino8204a912009-11-18 23:05:31 -02003787
3788void qemu_error_internal(const char *file, int linenr, const char *func,
3789 const char *fmt, ...)
3790{
3791 va_list va;
3792 QError *qerror;
3793
3794 assert(qemu_error_sink != NULL);
3795
3796 va_start(va, fmt);
3797 qerror = qerror_from_info(file, linenr, func, fmt, &va);
3798 va_end(va);
3799
3800 switch (qemu_error_sink->dest) {
3801 case ERR_SINK_FILE:
3802 qerror_print(qerror);
3803 QDECREF(qerror);
3804 break;
3805 case ERR_SINK_MONITOR:
3806 assert(qemu_error_sink->mon->error == NULL);
3807 qemu_error_sink->mon->error = qerror;
3808 break;
3809 }
3810}