blob: 70665bd789be3fa2f3e99814eb75207e078052f3 [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
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200270static void monitor_protocol_emitter(Monitor *mon, QObject *data)
271{
272 QDict *qmp;
273
274 qmp = qdict_new();
275
276 if (!monitor_has_error(mon)) {
277 /* success response */
278 if (data) {
279 qobject_incref(data);
280 qdict_put_obj(qmp, "return", data);
281 } else {
282 qdict_put(qmp, "return", qstring_from_str("OK"));
283 }
284 } else {
285 /* error response */
286 qdict_put(qmp, "error", mon->error->error);
287 QINCREF(mon->error->error);
288 QDECREF(mon->error);
289 mon->error = NULL;
290 }
291
292 monitor_json_emitter(mon, QOBJECT(qmp));
293 QDECREF(qmp);
294}
295
bellard9dc39cb2004-03-14 21:38:27 +0000296static int compare_cmd(const char *name, const char *list)
297{
298 const char *p, *pstart;
299 int len;
300 len = strlen(name);
301 p = list;
302 for(;;) {
303 pstart = p;
304 p = strchr(p, '|');
305 if (!p)
306 p = pstart + strlen(pstart);
307 if ((p - pstart) == len && !memcmp(pstart, name, len))
308 return 1;
309 if (*p == '\0')
310 break;
311 p++;
312 }
313 return 0;
314}
315
Anthony Liguoric227f092009-10-01 16:12:16 -0500316static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000317 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000318{
Anthony Liguoric227f092009-10-01 16:12:16 -0500319 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000320
321 for(cmd = cmds; cmd->name != NULL; cmd++) {
322 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000323 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
324 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000325 }
326}
327
aliguori376253e2009-03-05 23:01:23 +0000328static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000329{
330 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000331 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000332 } else {
aliguori376253e2009-03-05 23:01:23 +0000333 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000334 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000335 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000336 monitor_printf(mon, "Log items (comma separated):\n");
337 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000338 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000339 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000340 }
341 }
bellard9dc39cb2004-03-14 21:38:27 +0000342 }
343}
344
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300345static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300346{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300347 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300348}
349
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300350static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000351{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200352 int all_devices;
353 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300354 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000355
bellard7954c732006-08-01 15:52:40 +0000356 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000357 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200358 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300359 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200360 continue;
361 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000362 }
363}
364
Luiz Capitulino13c74252009-10-07 13:41:55 -0300365static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000366{
Anthony Liguoric227f092009-10-01 16:12:16 -0500367 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300368 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000369
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200370 if (!item) {
371 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000372 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200373 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300374
375 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000376 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300377 break;
bellard9dc39cb2004-03-14 21:38:27 +0000378 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300379
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200380 if (cmd->name == NULL) {
381 if (monitor_ctrl_mode(mon)) {
382 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
383 return;
384 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300385 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200386 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300387
388 if (monitor_handler_ported(cmd)) {
389 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200390
391 if (!monitor_ctrl_mode(mon)) {
392 /*
393 * User Protocol function is called here, Monitor Protocol is
394 * handled by monitor_call_handler()
395 */
396 if (*ret_data)
397 cmd->user_print(mon, *ret_data);
398 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300399 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200400 if (monitor_ctrl_mode(mon)) {
401 /* handler not converted yet */
402 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
403 } else {
404 cmd->mhandler.info(mon);
405 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300406 }
407
bellard9dc39cb2004-03-14 21:38:27 +0000408 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300409
410help:
411 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000412}
413
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300414/**
415 * do_info_version(): Show QEMU version
416 */
417static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000418{
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300419 *ret_data = QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION));
bellard9bc9d1c2004-10-10 15:15:51 +0000420}
421
aliguori376253e2009-03-05 23:01:23 +0000422static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000423{
424 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000425 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000426}
427
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200428/**
429 * do_info_commands(): List QMP available commands
430 *
431 * Return a QList of QStrings.
432 */
433static void do_info_commands(Monitor *mon, QObject **ret_data)
434{
435 QList *cmd_list;
436 const mon_cmd_t *cmd;
437
438 cmd_list = qlist_new();
439
440 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
441 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
442 qlist_append(cmd_list, qstring_from_str(cmd->name));
443 }
444 }
445
446 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
447 if (monitor_handler_ported(cmd)) {
448 char buf[128];
449 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
450 qlist_append(cmd_list, qstring_from_str(buf));
451 }
452 }
453
454 *ret_data = QOBJECT(cmd_list);
455}
456
aurel32bf4f74c2008-12-18 22:42:34 +0000457#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000458static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000459{
aliguori376253e2009-03-05 23:01:23 +0000460 monitor_printf(mon, "HPET is %s by QEMU\n",
461 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000462}
aurel32bf4f74c2008-12-18 22:42:34 +0000463#endif
aliguori16b29ae2008-12-17 23:28:44 +0000464
aliguori376253e2009-03-05 23:01:23 +0000465static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000466{
aliguori376253e2009-03-05 23:01:23 +0000467 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
468 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
469 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
470 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
471 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000472}
473
bellard6a00d602005-11-21 23:25:50 +0000474/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000475static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000476{
477 CPUState *env;
478
479 for(env = first_cpu; env != NULL; env = env->next_cpu) {
480 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000481 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000482 return 0;
483 }
484 }
485 return -1;
486}
487
pbrook9596ebb2007-11-18 01:44:38 +0000488static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000489{
aliguori731b0362009-03-05 23:01:42 +0000490 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000491 mon_set_cpu(0);
492 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300493 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000494 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000495}
496
aliguori376253e2009-03-05 23:01:23 +0000497static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000498{
bellard6a00d602005-11-21 23:25:50 +0000499 CPUState *env;
500 env = mon_get_cpu();
501 if (!env)
502 return;
bellard9307c4c2004-04-04 12:57:25 +0000503#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000504 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000505 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000506#else
aliguori376253e2009-03-05 23:01:23 +0000507 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000508 0);
bellard9307c4c2004-04-04 12:57:25 +0000509#endif
510}
511
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300512static void print_cpu_iter(QObject *obj, void *opaque)
513{
514 QDict *cpu;
515 int active = ' ';
516 Monitor *mon = opaque;
517
518 assert(qobject_type(obj) == QTYPE_QDICT);
519 cpu = qobject_to_qdict(obj);
520
521 if (strcmp(qdict_get_str(cpu, "current"), "yes") == 0)
522 active = '*';
523
524 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
525
526#if defined(TARGET_I386)
527 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
528 (target_ulong) qdict_get_int(cpu, "pc"));
529#elif defined(TARGET_PPC)
530 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
531 (target_long) qdict_get_int(cpu, "nip"));
532#elif defined(TARGET_SPARC)
533 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
534 (target_long) qdict_get_int(cpu, "pc"));
535 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
536 (target_long) qdict_get_int(cpu, "npc"));
537#elif defined(TARGET_MIPS)
538 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
539 (target_long) qdict_get_int(cpu, "PC"));
540#endif
541
542 if (strcmp(qdict_get_str(cpu, "halted"), "yes") == 0)
543 monitor_printf(mon, " (halted)");
544
545 monitor_printf(mon, "\n");
546}
547
548static void monitor_print_cpus(Monitor *mon, const QObject *data)
549{
550 QList *cpu_list;
551
552 assert(qobject_type(data) == QTYPE_QLIST);
553 cpu_list = qobject_to_qlist(data);
554 qlist_iter(cpu_list, print_cpu_iter, mon);
555}
556
557/**
558 * do_info_cpus(): Show CPU information
559 *
560 * Return a QList with a QDict for each CPU.
561 *
562 * For example:
563 *
564 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
565 * { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]
566 */
567static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000568{
569 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300570 QList *cpu_list;
571
572 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000573
574 /* just to set the default cpu if not already done */
575 mon_get_cpu();
576
577 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300578 const char *answer;
579 QDict *cpu = qdict_new();
580
Avi Kivity4c0960c2009-08-17 23:19:53 +0300581 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300582
583 qdict_put(cpu, "CPU", qint_from_int(env->cpu_index));
584 answer = (env == mon->mon_cpu) ? "yes" : "no";
585 qdict_put(cpu, "current", qstring_from_str(answer));
586
bellard6a00d602005-11-21 23:25:50 +0000587#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300588 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000589#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300590 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000591#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300592 qdict_put(cpu, "pc", qint_from_int(env->pc));
593 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000594#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300595 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000596#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300597 answer = env->halted ? "yes" : "no";
598 qdict_put(cpu, "halted", qstring_from_str(answer));
599
600 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000601 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300602
603 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000604}
605
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300606static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000607{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300608 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000609 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000610 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000611}
612
aliguori376253e2009-03-05 23:01:23 +0000613static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000614{
aliguori376253e2009-03-05 23:01:23 +0000615 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000616}
617
aliguori376253e2009-03-05 23:01:23 +0000618static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000619{
620 int i;
bellard7e2515e2004-08-01 21:52:19 +0000621 const char *str;
ths3b46e622007-09-17 08:09:54 +0000622
aliguoricde76ee2009-03-05 23:01:51 +0000623 if (!mon->rs)
624 return;
bellard7e2515e2004-08-01 21:52:19 +0000625 i = 0;
626 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000627 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000628 if (!str)
629 break;
aliguori376253e2009-03-05 23:01:23 +0000630 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000631 i++;
bellardaa455482004-04-04 13:07:25 +0000632 }
633}
634
j_mayer76a66252007-03-07 08:32:30 +0000635#if defined(TARGET_PPC)
636/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000637static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000638{
639 CPUState *env;
640
641 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000642 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000643}
644#endif
645
Luiz Capitulinob223f352009-10-07 13:41:56 -0300646/**
647 * do_quit(): Quit QEMU execution
648 */
649static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000650{
651 exit(0);
652}
653
aliguori376253e2009-03-05 23:01:23 +0000654static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000655{
656 if (bdrv_is_inserted(bs)) {
657 if (!force) {
658 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000659 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000660 return -1;
661 }
662 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000663 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000664 return -1;
665 }
666 }
667 bdrv_close(bs);
668 }
669 return 0;
670}
671
Luiz Capitulinoe1c923a2009-10-16 12:23:49 -0300672static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000673{
674 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300675 int force = qdict_get_int(qdict, "force");
676 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000677
bellard9307c4c2004-04-04 12:57:25 +0000678 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000679 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000680 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000681 return;
682 }
aliguori376253e2009-03-05 23:01:23 +0000683 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000684}
685
aliguori376253e2009-03-05 23:01:23 +0000686static void do_change_block(Monitor *mon, const char *device,
687 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000688{
689 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000690 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000691
bellard9307c4c2004-04-04 12:57:25 +0000692 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000693 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000694 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000695 return;
696 }
aurel322ecea9b2008-06-18 22:10:01 +0000697 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100698 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000699 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000700 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000701 return;
702 }
703 }
aliguori376253e2009-03-05 23:01:23 +0000704 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000705 return;
aurel322ecea9b2008-06-18 22:10:01 +0000706 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000707 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000708}
709
aliguori376253e2009-03-05 23:01:23 +0000710static void change_vnc_password_cb(Monitor *mon, const char *password,
711 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000712{
713 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000714 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000715
aliguori731b0362009-03-05 23:01:42 +0000716 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000717}
718
aliguori376253e2009-03-05 23:01:23 +0000719static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000720{
ths70848512007-08-25 01:37:05 +0000721 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000722 strcmp(target, "password") == 0) {
723 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000724 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000725 strncpy(password, arg, sizeof(password));
726 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000727 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000728 } else {
aliguori376253e2009-03-05 23:01:23 +0000729 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000730 }
ths70848512007-08-25 01:37:05 +0000731 } else {
aliguori28a76be2009-03-06 20:27:40 +0000732 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000733 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000734 }
thse25a5822007-08-25 01:36:20 +0000735}
736
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300737static void do_change(Monitor *mon, const QDict *qdict)
thse25a5822007-08-25 01:36:20 +0000738{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300739 const char *device = qdict_get_str(qdict, "device");
740 const char *target = qdict_get_str(qdict, "target");
741 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000742 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000743 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000744 } else {
aliguori28a76be2009-03-06 20:27:40 +0000745 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000746 }
747}
748
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300749static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000750{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300751 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000752}
753
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300754static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000755{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300756 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000757}
758
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300759static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000760{
761 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300762 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000763
bellard9307c4c2004-04-04 12:57:25 +0000764 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000765 mask = 0;
766 } else {
bellard9307c4c2004-04-04 12:57:25 +0000767 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000768 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000769 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000770 return;
771 }
772 }
773 cpu_set_log(mask);
774}
775
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300776static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000777{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300778 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000779 if (!option || !strcmp(option, "on")) {
780 singlestep = 1;
781 } else if (!strcmp(option, "off")) {
782 singlestep = 0;
783 } else {
784 monitor_printf(mon, "unexpected option %s\n", option);
785 }
786}
787
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -0300788/**
789 * do_stop(): Stop VM execution
790 */
791static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +0000792{
793 vm_stop(EXCP_INTERRUPT);
794}
795
aliguoribb5fc202009-03-05 23:01:15 +0000796static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000797
aliguori376253e2009-03-05 23:01:23 +0000798struct bdrv_iterate_context {
799 Monitor *mon;
800 int err;
801};
aliguoric0f4ce72009-03-05 23:01:01 +0000802
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300803/**
804 * do_cont(): Resume emulation.
805 */
806static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +0000807{
808 struct bdrv_iterate_context context = { mon, 0 };
809
810 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000811 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000812 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000813 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000814}
815
aliguoribb5fc202009-03-05 23:01:15 +0000816static void bdrv_key_cb(void *opaque, int err)
817{
aliguori376253e2009-03-05 23:01:23 +0000818 Monitor *mon = opaque;
819
aliguoribb5fc202009-03-05 23:01:15 +0000820 /* another key was set successfully, retry to continue */
821 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300822 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000823}
824
825static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
826{
aliguori376253e2009-03-05 23:01:23 +0000827 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000828
aliguori376253e2009-03-05 23:01:23 +0000829 if (!context->err && bdrv_key_required(bs)) {
830 context->err = -EBUSY;
831 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
832 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000833 }
834}
835
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300836static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000837{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300838 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000839 if (!device)
840 device = "tcp::" DEFAULT_GDBSTUB_PORT;
841 if (gdbserver_start(device) < 0) {
842 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
843 device);
844 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000845 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000846 } else {
aliguori59030a82009-04-05 18:43:41 +0000847 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
848 device);
bellard8a7ddc32004-03-31 19:00:16 +0000849 }
850}
851
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300852static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100853{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300854 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100855 if (select_watchdog_action(action) == -1) {
856 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
857 }
858}
859
aliguori376253e2009-03-05 23:01:23 +0000860static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000861{
aliguori376253e2009-03-05 23:01:23 +0000862 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000863 switch(c) {
864 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000865 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000866 break;
867 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000868 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000869 break;
870 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000871 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000872 break;
873 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000874 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000875 break;
876 default:
877 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000878 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000879 } else {
aliguori376253e2009-03-05 23:01:23 +0000880 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000881 }
882 break;
883 }
aliguori376253e2009-03-05 23:01:23 +0000884 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000885}
886
aliguori376253e2009-03-05 23:01:23 +0000887static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -0500888 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000889{
bellard6a00d602005-11-21 23:25:50 +0000890 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000891 int nb_per_line, l, line_size, i, max_digits, len;
892 uint8_t buf[16];
893 uint64_t v;
894
895 if (format == 'i') {
896 int flags;
897 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000898 env = mon_get_cpu();
899 if (!env && !is_physical)
900 return;
bellard9307c4c2004-04-04 12:57:25 +0000901#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000902 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000903 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000904 } else if (wsize == 4) {
905 flags = 0;
906 } else {
bellard6a15fd12006-04-12 21:07:07 +0000907 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000908 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000909 if (env) {
910#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000911 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000912 (env->segs[R_CS].flags & DESC_L_MASK))
913 flags = 2;
914 else
915#endif
916 if (!(env->segs[R_CS].flags & DESC_B_MASK))
917 flags = 1;
918 }
bellard4c27ba22004-04-25 18:05:08 +0000919 }
920#endif
aliguori376253e2009-03-05 23:01:23 +0000921 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000922 return;
923 }
924
925 len = wsize * count;
926 if (wsize == 1)
927 line_size = 8;
928 else
929 line_size = 16;
930 nb_per_line = line_size / wsize;
931 max_digits = 0;
932
933 switch(format) {
934 case 'o':
935 max_digits = (wsize * 8 + 2) / 3;
936 break;
937 default:
938 case 'x':
939 max_digits = (wsize * 8) / 4;
940 break;
941 case 'u':
942 case 'd':
943 max_digits = (wsize * 8 * 10 + 32) / 33;
944 break;
945 case 'c':
946 wsize = 1;
947 break;
948 }
949
950 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000951 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000952 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000953 else
aliguori376253e2009-03-05 23:01:23 +0000954 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000955 l = len;
956 if (l > line_size)
957 l = line_size;
958 if (is_physical) {
959 cpu_physical_memory_rw(addr, buf, l, 0);
960 } else {
bellard6a00d602005-11-21 23:25:50 +0000961 env = mon_get_cpu();
962 if (!env)
963 break;
aliguoric8f79b62008-08-18 14:00:20 +0000964 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000965 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000966 break;
967 }
bellard9307c4c2004-04-04 12:57:25 +0000968 }
ths5fafdf22007-09-16 21:08:06 +0000969 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000970 while (i < l) {
971 switch(wsize) {
972 default:
973 case 1:
974 v = ldub_raw(buf + i);
975 break;
976 case 2:
977 v = lduw_raw(buf + i);
978 break;
979 case 4:
bellard92a31b12005-02-10 22:00:52 +0000980 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000981 break;
982 case 8:
983 v = ldq_raw(buf + i);
984 break;
985 }
aliguori376253e2009-03-05 23:01:23 +0000986 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000987 switch(format) {
988 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000989 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000990 break;
991 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000992 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000993 break;
994 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000995 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000996 break;
997 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000998 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000999 break;
1000 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001001 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001002 break;
1003 }
1004 i += wsize;
1005 }
aliguori376253e2009-03-05 23:01:23 +00001006 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001007 addr += l;
1008 len -= l;
1009 }
1010}
1011
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001012static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001013{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001014 int count = qdict_get_int(qdict, "count");
1015 int format = qdict_get_int(qdict, "format");
1016 int size = qdict_get_int(qdict, "size");
1017 target_long addr = qdict_get_int(qdict, "addr");
1018
aliguori376253e2009-03-05 23:01:23 +00001019 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001020}
1021
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001022static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001023{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001024 int count = qdict_get_int(qdict, "count");
1025 int format = qdict_get_int(qdict, "format");
1026 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001027 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001028
aliguori376253e2009-03-05 23:01:23 +00001029 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001030}
1031
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001032static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001033{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001034 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001035 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001036
blueswir17743e582007-09-24 18:39:04 +00001037#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001038 switch(format) {
1039 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001040 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001041 break;
1042 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001043 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001044 break;
1045 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001046 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001047 break;
1048 default:
1049 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001050 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001051 break;
1052 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001053 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001054 break;
1055 }
bellard92a31b12005-02-10 22:00:52 +00001056#else
1057 switch(format) {
1058 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001059 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001060 break;
1061 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001062 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001063 break;
1064 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001065 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001066 break;
1067 default:
1068 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001069 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001070 break;
1071 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001072 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001073 break;
1074 }
1075#endif
aliguori376253e2009-03-05 23:01:23 +00001076 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001077}
1078
Luiz Capitulino57e09452009-10-16 12:23:43 -03001079static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001080{
1081 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001082 uint32_t size = qdict_get_int(qdict, "size");
1083 const char *filename = qdict_get_str(qdict, "filename");
1084 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001085 uint32_t l;
1086 CPUState *env;
1087 uint8_t buf[1024];
1088
1089 env = mon_get_cpu();
1090 if (!env)
1091 return;
1092
1093 f = fopen(filename, "wb");
1094 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001095 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +00001096 return;
1097 }
1098 while (size != 0) {
1099 l = sizeof(buf);
1100 if (l > size)
1101 l = size;
1102 cpu_memory_rw_debug(env, addr, buf, l, 0);
1103 fwrite(buf, 1, l, f);
1104 addr += l;
1105 size -= l;
1106 }
1107 fclose(f);
1108}
1109
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001110static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1111 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001112{
1113 FILE *f;
1114 uint32_t l;
1115 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001116 uint32_t size = qdict_get_int(qdict, "size");
1117 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001118 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +00001119
1120 f = fopen(filename, "wb");
1121 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001122 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +00001123 return;
1124 }
1125 while (size != 0) {
1126 l = sizeof(buf);
1127 if (l > size)
1128 l = size;
1129 cpu_physical_memory_rw(addr, buf, l, 0);
1130 fwrite(buf, 1, l, f);
1131 fflush(f);
1132 addr += l;
1133 size -= l;
1134 }
1135 fclose(f);
1136}
1137
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001138static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001139{
1140 uint32_t addr;
1141 uint8_t buf[1];
1142 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001143 uint32_t start = qdict_get_int(qdict, "start");
1144 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001145
1146 sum = 0;
1147 for(addr = start; addr < (start + size); addr++) {
1148 cpu_physical_memory_rw(addr, buf, 1, 0);
1149 /* BSD sum algorithm ('sum' Unix command) */
1150 sum = (sum >> 1) | (sum << 15);
1151 sum += buf[0];
1152 }
aliguori376253e2009-03-05 23:01:23 +00001153 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001154}
1155
bellarda3a91a32004-06-04 11:06:21 +00001156typedef struct {
1157 int keycode;
1158 const char *name;
1159} KeyDef;
1160
1161static const KeyDef key_defs[] = {
1162 { 0x2a, "shift" },
1163 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001164
bellarda3a91a32004-06-04 11:06:21 +00001165 { 0x38, "alt" },
1166 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001167 { 0x64, "altgr" },
1168 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001169 { 0x1d, "ctrl" },
1170 { 0x9d, "ctrl_r" },
1171
1172 { 0xdd, "menu" },
1173
1174 { 0x01, "esc" },
1175
1176 { 0x02, "1" },
1177 { 0x03, "2" },
1178 { 0x04, "3" },
1179 { 0x05, "4" },
1180 { 0x06, "5" },
1181 { 0x07, "6" },
1182 { 0x08, "7" },
1183 { 0x09, "8" },
1184 { 0x0a, "9" },
1185 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001186 { 0x0c, "minus" },
1187 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001188 { 0x0e, "backspace" },
1189
1190 { 0x0f, "tab" },
1191 { 0x10, "q" },
1192 { 0x11, "w" },
1193 { 0x12, "e" },
1194 { 0x13, "r" },
1195 { 0x14, "t" },
1196 { 0x15, "y" },
1197 { 0x16, "u" },
1198 { 0x17, "i" },
1199 { 0x18, "o" },
1200 { 0x19, "p" },
1201
1202 { 0x1c, "ret" },
1203
1204 { 0x1e, "a" },
1205 { 0x1f, "s" },
1206 { 0x20, "d" },
1207 { 0x21, "f" },
1208 { 0x22, "g" },
1209 { 0x23, "h" },
1210 { 0x24, "j" },
1211 { 0x25, "k" },
1212 { 0x26, "l" },
1213
1214 { 0x2c, "z" },
1215 { 0x2d, "x" },
1216 { 0x2e, "c" },
1217 { 0x2f, "v" },
1218 { 0x30, "b" },
1219 { 0x31, "n" },
1220 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001221 { 0x33, "comma" },
1222 { 0x34, "dot" },
1223 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001224
balrog4d3b6f62008-02-10 16:33:14 +00001225 { 0x37, "asterisk" },
1226
bellarda3a91a32004-06-04 11:06:21 +00001227 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001228 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001229 { 0x3b, "f1" },
1230 { 0x3c, "f2" },
1231 { 0x3d, "f3" },
1232 { 0x3e, "f4" },
1233 { 0x3f, "f5" },
1234 { 0x40, "f6" },
1235 { 0x41, "f7" },
1236 { 0x42, "f8" },
1237 { 0x43, "f9" },
1238 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001239 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001240 { 0x46, "scroll_lock" },
1241
bellard64866c32006-05-07 18:03:31 +00001242 { 0xb5, "kp_divide" },
1243 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001244 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001245 { 0x4e, "kp_add" },
1246 { 0x9c, "kp_enter" },
1247 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001248 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001249
1250 { 0x52, "kp_0" },
1251 { 0x4f, "kp_1" },
1252 { 0x50, "kp_2" },
1253 { 0x51, "kp_3" },
1254 { 0x4b, "kp_4" },
1255 { 0x4c, "kp_5" },
1256 { 0x4d, "kp_6" },
1257 { 0x47, "kp_7" },
1258 { 0x48, "kp_8" },
1259 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001260
bellarda3a91a32004-06-04 11:06:21 +00001261 { 0x56, "<" },
1262
1263 { 0x57, "f11" },
1264 { 0x58, "f12" },
1265
1266 { 0xb7, "print" },
1267
1268 { 0xc7, "home" },
1269 { 0xc9, "pgup" },
1270 { 0xd1, "pgdn" },
1271 { 0xcf, "end" },
1272
1273 { 0xcb, "left" },
1274 { 0xc8, "up" },
1275 { 0xd0, "down" },
1276 { 0xcd, "right" },
1277
1278 { 0xd2, "insert" },
1279 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001280#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1281 { 0xf0, "stop" },
1282 { 0xf1, "again" },
1283 { 0xf2, "props" },
1284 { 0xf3, "undo" },
1285 { 0xf4, "front" },
1286 { 0xf5, "copy" },
1287 { 0xf6, "open" },
1288 { 0xf7, "paste" },
1289 { 0xf8, "find" },
1290 { 0xf9, "cut" },
1291 { 0xfa, "lf" },
1292 { 0xfb, "help" },
1293 { 0xfc, "meta_l" },
1294 { 0xfd, "meta_r" },
1295 { 0xfe, "compose" },
1296#endif
bellarda3a91a32004-06-04 11:06:21 +00001297 { 0, NULL },
1298};
1299
1300static int get_keycode(const char *key)
1301{
1302 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001303 char *endp;
1304 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001305
1306 for(p = key_defs; p->name != NULL; p++) {
1307 if (!strcmp(key, p->name))
1308 return p->keycode;
1309 }
bellard64866c32006-05-07 18:03:31 +00001310 if (strstart(key, "0x", NULL)) {
1311 ret = strtoul(key, &endp, 0);
1312 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1313 return ret;
1314 }
bellarda3a91a32004-06-04 11:06:21 +00001315 return -1;
1316}
1317
balrogc8256f92008-06-08 22:45:01 +00001318#define MAX_KEYCODES 16
1319static uint8_t keycodes[MAX_KEYCODES];
1320static int nb_pending_keycodes;
1321static QEMUTimer *key_timer;
1322
1323static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001324{
balrogc8256f92008-06-08 22:45:01 +00001325 int keycode;
1326
1327 while (nb_pending_keycodes > 0) {
1328 nb_pending_keycodes--;
1329 keycode = keycodes[nb_pending_keycodes];
1330 if (keycode & 0x80)
1331 kbd_put_keycode(0xe0);
1332 kbd_put_keycode(keycode | 0x80);
1333 }
1334}
1335
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001336static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001337{
balrog3401c0d2008-06-04 10:05:59 +00001338 char keyname_buf[16];
1339 char *separator;
1340 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001341 const char *string = qdict_get_str(qdict, "string");
1342 int has_hold_time = qdict_haskey(qdict, "hold_time");
1343 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001344
balrogc8256f92008-06-08 22:45:01 +00001345 if (nb_pending_keycodes > 0) {
1346 qemu_del_timer(key_timer);
1347 release_keys(NULL);
1348 }
1349 if (!has_hold_time)
1350 hold_time = 100;
1351 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001352 while (1) {
1353 separator = strchr(string, '-');
1354 keyname_len = separator ? separator - string : strlen(string);
1355 if (keyname_len > 0) {
1356 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1357 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001358 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001359 return;
bellarda3a91a32004-06-04 11:06:21 +00001360 }
balrogc8256f92008-06-08 22:45:01 +00001361 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001362 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001363 return;
1364 }
1365 keyname_buf[keyname_len] = 0;
1366 keycode = get_keycode(keyname_buf);
1367 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001368 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001369 return;
1370 }
balrogc8256f92008-06-08 22:45:01 +00001371 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001372 }
balrog3401c0d2008-06-04 10:05:59 +00001373 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001374 break;
balrog3401c0d2008-06-04 10:05:59 +00001375 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001376 }
balrogc8256f92008-06-08 22:45:01 +00001377 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001378 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001379 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001380 keycode = keycodes[i];
1381 if (keycode & 0x80)
1382 kbd_put_keycode(0xe0);
1383 kbd_put_keycode(keycode & 0x7f);
1384 }
balrogc8256f92008-06-08 22:45:01 +00001385 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001386 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001387 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001388}
1389
bellard13224a82006-07-14 22:03:35 +00001390static int mouse_button_state;
1391
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001392static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001393{
1394 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001395 const char *dx_str = qdict_get_str(qdict, "dx_str");
1396 const char *dy_str = qdict_get_str(qdict, "dy_str");
1397 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001398 dx = strtol(dx_str, NULL, 0);
1399 dy = strtol(dy_str, NULL, 0);
1400 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001401 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001402 dz = strtol(dz_str, NULL, 0);
1403 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1404}
1405
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001406static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001407{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001408 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001409 mouse_button_state = button_state;
1410 kbd_mouse_event(0, 0, 0, mouse_button_state);
1411}
1412
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001413static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001414{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001415 int size = qdict_get_int(qdict, "size");
1416 int addr = qdict_get_int(qdict, "addr");
1417 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001418 uint32_t val;
1419 int suffix;
1420
1421 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001422 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001423 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001424 addr++;
1425 }
1426 addr &= 0xffff;
1427
1428 switch(size) {
1429 default:
1430 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001431 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001432 suffix = 'b';
1433 break;
1434 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001435 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001436 suffix = 'w';
1437 break;
1438 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001439 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001440 suffix = 'l';
1441 break;
1442 }
aliguori376253e2009-03-05 23:01:23 +00001443 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1444 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001445}
bellarda3a91a32004-06-04 11:06:21 +00001446
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001447static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001448{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001449 int size = qdict_get_int(qdict, "size");
1450 int addr = qdict_get_int(qdict, "addr");
1451 int val = qdict_get_int(qdict, "val");
1452
Jan Kiszkaf1147842009-07-14 10:20:11 +02001453 addr &= IOPORTS_MASK;
1454
1455 switch (size) {
1456 default:
1457 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001458 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001459 break;
1460 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001461 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001462 break;
1463 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001464 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001465 break;
1466 }
1467}
1468
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001469static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001470{
1471 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001472 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001473
Jan Kiszka76e30d02009-07-02 00:19:02 +02001474 res = qemu_boot_set(bootdevice);
1475 if (res == 0) {
1476 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1477 } else if (res > 0) {
1478 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001479 } else {
aliguori376253e2009-03-05 23:01:23 +00001480 monitor_printf(mon, "no function defined to set boot device list for "
1481 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001482 }
1483}
1484
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001485/**
1486 * do_system_reset(): Issue a machine reset
1487 */
1488static void do_system_reset(Monitor *mon, const QDict *qdict,
1489 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001490{
1491 qemu_system_reset_request();
1492}
1493
Luiz Capitulino43076662009-10-07 13:41:59 -03001494/**
1495 * do_system_powerdown(): Issue a machine powerdown
1496 */
1497static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1498 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001499{
1500 qemu_system_powerdown_request();
1501}
1502
bellardb86bda52004-09-18 19:32:46 +00001503#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001504static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001505{
aliguori376253e2009-03-05 23:01:23 +00001506 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1507 addr,
1508 pte & mask,
1509 pte & PG_GLOBAL_MASK ? 'G' : '-',
1510 pte & PG_PSE_MASK ? 'P' : '-',
1511 pte & PG_DIRTY_MASK ? 'D' : '-',
1512 pte & PG_ACCESSED_MASK ? 'A' : '-',
1513 pte & PG_PCD_MASK ? 'C' : '-',
1514 pte & PG_PWT_MASK ? 'T' : '-',
1515 pte & PG_USER_MASK ? 'U' : '-',
1516 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001517}
1518
aliguori376253e2009-03-05 23:01:23 +00001519static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001520{
bellard6a00d602005-11-21 23:25:50 +00001521 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001522 int l1, l2;
1523 uint32_t pgd, pde, pte;
1524
bellard6a00d602005-11-21 23:25:50 +00001525 env = mon_get_cpu();
1526 if (!env)
1527 return;
1528
bellardb86bda52004-09-18 19:32:46 +00001529 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001530 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001531 return;
1532 }
1533 pgd = env->cr[3] & ~0xfff;
1534 for(l1 = 0; l1 < 1024; l1++) {
1535 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1536 pde = le32_to_cpu(pde);
1537 if (pde & PG_PRESENT_MASK) {
1538 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001539 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001540 } else {
1541 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001542 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001543 (uint8_t *)&pte, 4);
1544 pte = le32_to_cpu(pte);
1545 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001546 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001547 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001548 ~0xfff);
1549 }
1550 }
1551 }
1552 }
1553 }
1554}
1555
aliguori376253e2009-03-05 23:01:23 +00001556static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001557 uint32_t end, int prot)
1558{
bellard9746b152004-11-11 18:30:24 +00001559 int prot1;
1560 prot1 = *plast_prot;
1561 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001562 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001563 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1564 *pstart, end, end - *pstart,
1565 prot1 & PG_USER_MASK ? 'u' : '-',
1566 'r',
1567 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001568 }
1569 if (prot != 0)
1570 *pstart = end;
1571 else
1572 *pstart = -1;
1573 *plast_prot = prot;
1574 }
1575}
1576
aliguori376253e2009-03-05 23:01:23 +00001577static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001578{
bellard6a00d602005-11-21 23:25:50 +00001579 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001580 int l1, l2, prot, last_prot;
1581 uint32_t pgd, pde, pte, start, end;
1582
bellard6a00d602005-11-21 23:25:50 +00001583 env = mon_get_cpu();
1584 if (!env)
1585 return;
1586
bellardb86bda52004-09-18 19:32:46 +00001587 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001588 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001589 return;
1590 }
1591 pgd = env->cr[3] & ~0xfff;
1592 last_prot = 0;
1593 start = -1;
1594 for(l1 = 0; l1 < 1024; l1++) {
1595 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1596 pde = le32_to_cpu(pde);
1597 end = l1 << 22;
1598 if (pde & PG_PRESENT_MASK) {
1599 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1600 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001601 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001602 } else {
1603 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001604 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001605 (uint8_t *)&pte, 4);
1606 pte = le32_to_cpu(pte);
1607 end = (l1 << 22) + (l2 << 12);
1608 if (pte & PG_PRESENT_MASK) {
1609 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1610 } else {
1611 prot = 0;
1612 }
aliguori376253e2009-03-05 23:01:23 +00001613 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001614 }
1615 }
1616 } else {
1617 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001618 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001619 }
1620 }
1621}
1622#endif
1623
aurel327c664e22009-03-03 06:12:22 +00001624#if defined(TARGET_SH4)
1625
aliguori376253e2009-03-05 23:01:23 +00001626static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001627{
aliguori376253e2009-03-05 23:01:23 +00001628 monitor_printf(mon, " tlb%i:\t"
1629 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1630 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1631 "dirty=%hhu writethrough=%hhu\n",
1632 idx,
1633 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1634 tlb->v, tlb->sh, tlb->c, tlb->pr,
1635 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001636}
1637
aliguori376253e2009-03-05 23:01:23 +00001638static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001639{
1640 CPUState *env = mon_get_cpu();
1641 int i;
1642
aliguori376253e2009-03-05 23:01:23 +00001643 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001644 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001645 print_tlb (mon, i, &env->itlb[i]);
1646 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001647 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001648 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001649}
1650
1651#endif
1652
aliguori376253e2009-03-05 23:01:23 +00001653static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001654{
1655#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001656 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001657 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001658 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001659 else
aliguori376253e2009-03-05 23:01:23 +00001660 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001661#else
aliguori376253e2009-03-05 23:01:23 +00001662 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001663#endif
1664}
1665
aliguori030ea372009-04-21 22:30:47 +00001666static void do_info_numa(Monitor *mon)
1667{
aliguorib28b6232009-04-22 20:20:29 +00001668 int i;
aliguori030ea372009-04-21 22:30:47 +00001669 CPUState *env;
1670
1671 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1672 for (i = 0; i < nb_numa_nodes; i++) {
1673 monitor_printf(mon, "node %d cpus:", i);
1674 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1675 if (env->numa_node == i) {
1676 monitor_printf(mon, " %d", env->cpu_index);
1677 }
1678 }
1679 monitor_printf(mon, "\n");
1680 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1681 node_mem[i] >> 20);
1682 }
1683}
1684
bellard5f1ce942006-02-08 22:40:15 +00001685#ifdef CONFIG_PROFILER
1686
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001687int64_t qemu_time;
1688int64_t dev_time;
1689
aliguori376253e2009-03-05 23:01:23 +00001690static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001691{
1692 int64_t total;
1693 total = qemu_time;
1694 if (total == 0)
1695 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001696 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001697 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001698 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001699 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001700 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001701 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001702}
1703#else
aliguori376253e2009-03-05 23:01:23 +00001704static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001705{
aliguori376253e2009-03-05 23:01:23 +00001706 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001707}
1708#endif
1709
bellardec36b692006-07-16 18:57:03 +00001710/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001711static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001712
aliguori376253e2009-03-05 23:01:23 +00001713static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001714{
1715 int i;
1716 CaptureState *s;
1717
1718 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001719 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001720 s->ops.info (s->opaque);
1721 }
1722}
1723
Blue Swirl23130862009-06-06 08:22:04 +00001724#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001725static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001726{
1727 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001728 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001729 CaptureState *s;
1730
1731 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1732 if (i == n) {
1733 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001734 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00001735 qemu_free (s);
1736 return;
1737 }
1738 }
1739}
1740
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001741static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001742{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001743 const char *path = qdict_get_str(qdict, "path");
1744 int has_freq = qdict_haskey(qdict, "freq");
1745 int freq = qdict_get_try_int(qdict, "freq", -1);
1746 int has_bits = qdict_haskey(qdict, "bits");
1747 int bits = qdict_get_try_int(qdict, "bits", -1);
1748 int has_channels = qdict_haskey(qdict, "nchannels");
1749 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001750 CaptureState *s;
1751
1752 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001753
1754 freq = has_freq ? freq : 44100;
1755 bits = has_bits ? bits : 16;
1756 nchannels = has_channels ? nchannels : 2;
1757
1758 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001759 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001760 qemu_free (s);
1761 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001762 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001763}
1764#endif
1765
aurel32dc1c0b72008-04-27 23:52:12 +00001766#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001767static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001768{
1769 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001770 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001771
1772 for (env = first_cpu; env != NULL; env = env->next_cpu)
1773 if (env->cpu_index == cpu_index) {
1774 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1775 break;
1776 }
1777}
1778#endif
1779
aliguori376253e2009-03-05 23:01:23 +00001780static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001781{
aurel321b530a62009-04-05 20:08:59 +00001782 if (vm_running) {
1783 if (singlestep) {
1784 monitor_printf(mon, "VM status: running (single step mode)\n");
1785 } else {
1786 monitor_printf(mon, "VM status: running\n");
1787 }
1788 } else
aliguori376253e2009-03-05 23:01:23 +00001789 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001790}
1791
Luiz Capitulino83fb1de2009-10-07 13:42:01 -03001792/**
1793 * do_balloon(): Request VM to change its memory allocation
1794 */
1795static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001796{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001797 int value = qdict_get_int(qdict, "value");
Anthony Liguoric227f092009-10-01 16:12:16 -05001798 ram_addr_t target = value;
aliguoridf751fa2008-12-04 20:19:35 +00001799 qemu_balloon(target << 20);
1800}
1801
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001802static void monitor_print_balloon(Monitor *mon, const QObject *data)
1803{
1804 monitor_printf(mon, "balloon: actual=%d\n",
1805 (int)qint_get_int(qobject_to_qint(data)));
1806}
1807
1808/**
1809 * do_info_balloon(): Balloon information
1810 */
1811static void do_info_balloon(Monitor *mon, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001812{
Anthony Liguoric227f092009-10-01 16:12:16 -05001813 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00001814
1815 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001816 if (kvm_enabled() && !kvm_has_sync_mmu())
Luiz Capitulino5d6c37f2009-11-18 23:05:36 -02001817 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
aliguoribd322082008-12-04 20:33:06 +00001818 else if (actual == 0)
Luiz Capitulino5d6c37f2009-11-18 23:05:36 -02001819 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
aliguoridf751fa2008-12-04 20:19:35 +00001820 else
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001821 *ret_data = QOBJECT(qint_from_int((int)(actual >> 20)));
aliguoridf751fa2008-12-04 20:19:35 +00001822}
1823
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001824static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001825{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001826 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001827
aliguori76655d62009-03-06 20:27:37 +00001828 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001829 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001830 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001831 return acl;
1832}
aliguori76655d62009-03-06 20:27:37 +00001833
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001834static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001835{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001836 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001837 qemu_acl *acl = find_acl(mon, aclname);
1838 qemu_acl_entry *entry;
1839 int i = 0;
1840
1841 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001842 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001843 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001844 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001845 i++;
1846 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001847 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001848 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001849 }
1850}
1851
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001852static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001853{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001854 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001855 qemu_acl *acl = find_acl(mon, aclname);
1856
1857 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001858 qemu_acl_reset(acl);
1859 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001860 }
1861}
aliguori76655d62009-03-06 20:27:37 +00001862
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001863static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001864{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001865 const char *aclname = qdict_get_str(qdict, "aclname");
1866 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001867 qemu_acl *acl = find_acl(mon, aclname);
1868
1869 if (acl) {
1870 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001871 acl->defaultDeny = 0;
1872 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001873 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001874 acl->defaultDeny = 1;
1875 monitor_printf(mon, "acl: policy set to 'deny'\n");
1876 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001877 monitor_printf(mon, "acl: unknown policy '%s', "
1878 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001879 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001880 }
1881}
aliguori76655d62009-03-06 20:27:37 +00001882
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001883static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001884{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001885 const char *aclname = qdict_get_str(qdict, "aclname");
1886 const char *match = qdict_get_str(qdict, "match");
1887 const char *policy = qdict_get_str(qdict, "policy");
1888 int has_index = qdict_haskey(qdict, "index");
1889 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001890 qemu_acl *acl = find_acl(mon, aclname);
1891 int deny, ret;
1892
1893 if (acl) {
1894 if (strcmp(policy, "allow") == 0) {
1895 deny = 0;
1896 } else if (strcmp(policy, "deny") == 0) {
1897 deny = 1;
1898 } else {
1899 monitor_printf(mon, "acl: unknown policy '%s', "
1900 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001901 return;
1902 }
aliguori28a76be2009-03-06 20:27:40 +00001903 if (has_index)
1904 ret = qemu_acl_insert(acl, deny, match, index);
1905 else
1906 ret = qemu_acl_append(acl, deny, match);
1907 if (ret < 0)
1908 monitor_printf(mon, "acl: unable to add acl entry\n");
1909 else
1910 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001911 }
1912}
aliguori76655d62009-03-06 20:27:37 +00001913
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001914static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001915{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001916 const char *aclname = qdict_get_str(qdict, "aclname");
1917 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001918 qemu_acl *acl = find_acl(mon, aclname);
1919 int ret;
aliguori76655d62009-03-06 20:27:37 +00001920
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001921 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001922 ret = qemu_acl_remove(acl, match);
1923 if (ret < 0)
1924 monitor_printf(mon, "acl: no matching acl entry\n");
1925 else
1926 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001927 }
1928}
1929
Huang Ying79c4f6b2009-06-23 10:05:14 +08001930#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001931static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001932{
1933 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001934 int cpu_index = qdict_get_int(qdict, "cpu_index");
1935 int bank = qdict_get_int(qdict, "bank");
1936 uint64_t status = qdict_get_int(qdict, "status");
1937 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1938 uint64_t addr = qdict_get_int(qdict, "addr");
1939 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08001940
1941 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1942 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1943 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1944 break;
1945 }
1946}
1947#endif
1948
Luiz Capitulinof0d60002009-10-16 12:23:50 -03001949static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001950{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001951 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001952 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001953 int fd;
1954
1955 fd = qemu_chr_get_msgfd(mon->chr);
1956 if (fd == -1) {
1957 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1958 return;
1959 }
1960
1961 if (qemu_isdigit(fdname[0])) {
1962 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1963 return;
1964 }
1965
1966 fd = dup(fd);
1967 if (fd == -1) {
1968 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1969 strerror(errno));
1970 return;
1971 }
1972
Blue Swirl72cf2d42009-09-12 07:36:22 +00001973 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001974 if (strcmp(monfd->name, fdname) != 0) {
1975 continue;
1976 }
1977
1978 close(monfd->fd);
1979 monfd->fd = fd;
1980 return;
1981 }
1982
Anthony Liguoric227f092009-10-01 16:12:16 -05001983 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001984 monfd->name = qemu_strdup(fdname);
1985 monfd->fd = fd;
1986
Blue Swirl72cf2d42009-09-12 07:36:22 +00001987 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001988}
1989
Luiz Capitulino18f3a512009-10-16 12:23:51 -03001990static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001991{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001992 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001993 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001994
Blue Swirl72cf2d42009-09-12 07:36:22 +00001995 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001996 if (strcmp(monfd->name, fdname) != 0) {
1997 continue;
1998 }
1999
Blue Swirl72cf2d42009-09-12 07:36:22 +00002000 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002001 close(monfd->fd);
2002 qemu_free(monfd->name);
2003 qemu_free(monfd);
2004 return;
2005 }
2006
2007 monitor_printf(mon, "Failed to find file descriptor named %s\n",
2008 fdname);
2009}
2010
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002011static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002012{
2013 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002014 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002015
2016 vm_stop(0);
2017
Juan Quintela05f24012009-08-20 19:42:22 +02002018 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002019 vm_start();
2020}
2021
Mark McLoughlin7768e042009-07-22 09:11:41 +01002022int monitor_get_fd(Monitor *mon, const char *fdname)
2023{
Anthony Liguoric227f092009-10-01 16:12:16 -05002024 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002025
Blue Swirl72cf2d42009-09-12 07:36:22 +00002026 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002027 int fd;
2028
2029 if (strcmp(monfd->name, fdname) != 0) {
2030 continue;
2031 }
2032
2033 fd = monfd->fd;
2034
2035 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002036 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002037 qemu_free(monfd->name);
2038 qemu_free(monfd);
2039
2040 return fd;
2041 }
2042
2043 return -1;
2044}
2045
Anthony Liguoric227f092009-10-01 16:12:16 -05002046static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002047#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002048 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002049};
2050
Blue Swirl23130862009-06-06 08:22:04 +00002051/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002052static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002053 {
2054 .name = "version",
2055 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002056 .params = "",
2057 .help = "show the version of QEMU",
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002058 .user_print = monitor_print_qobject,
2059 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002060 },
2061 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002062 .name = "commands",
2063 .args_type = "",
2064 .params = "",
2065 .help = "list QMP available commands",
2066 .user_print = monitor_user_noop,
2067 .mhandler.info_new = do_info_commands,
2068 },
2069 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002070 .name = "network",
2071 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002072 .params = "",
2073 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002074 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002075 },
2076 {
2077 .name = "chardev",
2078 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002079 .params = "",
2080 .help = "show the character devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002081 .mhandler.info = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002082 },
2083 {
2084 .name = "block",
2085 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002086 .params = "",
2087 .help = "show the block devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002088 .mhandler.info = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002089 },
2090 {
2091 .name = "blockstats",
2092 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002093 .params = "",
2094 .help = "show block device statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002095 .mhandler.info = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002096 },
2097 {
2098 .name = "registers",
2099 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002100 .params = "",
2101 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002102 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002103 },
2104 {
2105 .name = "cpus",
2106 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002107 .params = "",
2108 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002109 .user_print = monitor_print_cpus,
2110 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002111 },
2112 {
2113 .name = "history",
2114 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002115 .params = "",
2116 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002117 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002118 },
2119 {
2120 .name = "irq",
2121 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002122 .params = "",
2123 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002124 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002125 },
2126 {
2127 .name = "pic",
2128 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002129 .params = "",
2130 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002131 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002132 },
2133 {
2134 .name = "pci",
2135 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002136 .params = "",
2137 .help = "show PCI info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002138 .mhandler.info = pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002139 },
aurel327c664e22009-03-03 06:12:22 +00002140#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002141 {
2142 .name = "tlb",
2143 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002144 .params = "",
2145 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002146 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002147 },
aurel327c664e22009-03-03 06:12:22 +00002148#endif
2149#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002150 {
2151 .name = "mem",
2152 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002153 .params = "",
2154 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002155 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002156 },
2157 {
2158 .name = "hpet",
2159 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002160 .params = "",
2161 .help = "show state of HPET",
Luiz Capitulino910df892009-10-07 13:41:51 -03002162 .mhandler.info = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002163 },
bellardb86bda52004-09-18 19:32:46 +00002164#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002165 {
2166 .name = "jit",
2167 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002168 .params = "",
2169 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002170 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002171 },
2172 {
2173 .name = "kvm",
2174 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002175 .params = "",
2176 .help = "show KVM information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002177 .mhandler.info = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002178 },
2179 {
2180 .name = "numa",
2181 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002182 .params = "",
2183 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002184 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002185 },
2186 {
2187 .name = "usb",
2188 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002189 .params = "",
2190 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002191 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002192 },
2193 {
2194 .name = "usbhost",
2195 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002196 .params = "",
2197 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002198 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002199 },
2200 {
2201 .name = "profile",
2202 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002203 .params = "",
2204 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002205 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002206 },
2207 {
2208 .name = "capture",
2209 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002210 .params = "",
2211 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002212 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002213 },
2214 {
2215 .name = "snapshots",
2216 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002217 .params = "",
2218 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002219 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002220 },
2221 {
2222 .name = "status",
2223 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002224 .params = "",
2225 .help = "show the current VM status (running|paused)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002226 .mhandler.info = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002227 },
2228 {
2229 .name = "pcmcia",
2230 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002231 .params = "",
2232 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002233 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002234 },
2235 {
2236 .name = "mice",
2237 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002238 .params = "",
2239 .help = "show which guest mouse is receiving events",
Luiz Capitulino910df892009-10-07 13:41:51 -03002240 .mhandler.info = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002241 },
2242 {
2243 .name = "vnc",
2244 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002245 .params = "",
2246 .help = "show the vnc server status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002247 .mhandler.info = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002248 },
2249 {
2250 .name = "name",
2251 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002252 .params = "",
2253 .help = "show the current VM name",
Luiz Capitulino910df892009-10-07 13:41:51 -03002254 .mhandler.info = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002255 },
2256 {
2257 .name = "uuid",
2258 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002259 .params = "",
2260 .help = "show the current VM UUID",
Luiz Capitulino910df892009-10-07 13:41:51 -03002261 .mhandler.info = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002262 },
j_mayer76a66252007-03-07 08:32:30 +00002263#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002264 {
2265 .name = "cpustats",
2266 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002267 .params = "",
2268 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002269 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002270 },
j_mayer76a66252007-03-07 08:32:30 +00002271#endif
blueswir131a60e22007-10-26 18:42:59 +00002272#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002273 {
2274 .name = "usernet",
2275 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002276 .params = "",
2277 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002278 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002279 },
blueswir131a60e22007-10-26 18:42:59 +00002280#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002281 {
2282 .name = "migrate",
2283 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002284 .params = "",
2285 .help = "show migration status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002286 .mhandler.info = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002287 },
2288 {
2289 .name = "balloon",
2290 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002291 .params = "",
2292 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002293 .user_print = monitor_print_balloon,
2294 .mhandler.info_new = do_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002295 },
2296 {
2297 .name = "qtree",
2298 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002299 .params = "",
2300 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002301 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002302 },
2303 {
2304 .name = "qdm",
2305 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002306 .params = "",
2307 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002308 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002309 },
2310 {
2311 .name = "roms",
2312 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002313 .params = "",
2314 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002315 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002316 },
2317 {
2318 .name = NULL,
2319 },
bellard9dc39cb2004-03-14 21:38:27 +00002320};
2321
bellard9307c4c2004-04-04 12:57:25 +00002322/*******************************************************************/
2323
2324static const char *pch;
2325static jmp_buf expr_env;
2326
bellard92a31b12005-02-10 22:00:52 +00002327#define MD_TLONG 0
2328#define MD_I32 1
2329
bellard9307c4c2004-04-04 12:57:25 +00002330typedef struct MonitorDef {
2331 const char *name;
2332 int offset;
blueswir18662d652008-10-02 18:32:44 +00002333 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002334 int type;
bellard9307c4c2004-04-04 12:57:25 +00002335} MonitorDef;
2336
bellard57206fd2004-04-25 18:54:52 +00002337#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002338static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002339{
bellard6a00d602005-11-21 23:25:50 +00002340 CPUState *env = mon_get_cpu();
2341 if (!env)
2342 return 0;
2343 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002344}
2345#endif
2346
bellarda541f292004-04-12 20:39:29 +00002347#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002348static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002349{
bellard6a00d602005-11-21 23:25:50 +00002350 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002351 unsigned int u;
2352 int i;
2353
bellard6a00d602005-11-21 23:25:50 +00002354 if (!env)
2355 return 0;
2356
bellarda541f292004-04-12 20:39:29 +00002357 u = 0;
2358 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002359 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002360
2361 return u;
2362}
2363
blueswir18662d652008-10-02 18:32:44 +00002364static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002365{
bellard6a00d602005-11-21 23:25:50 +00002366 CPUState *env = mon_get_cpu();
2367 if (!env)
2368 return 0;
j_mayer0411a972007-10-25 21:35:50 +00002369 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002370}
2371
blueswir18662d652008-10-02 18:32:44 +00002372static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002373{
bellard6a00d602005-11-21 23:25:50 +00002374 CPUState *env = mon_get_cpu();
2375 if (!env)
2376 return 0;
aurel323d7b4172008-10-21 11:28:46 +00002377 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002378}
bellard9fddaa02004-05-21 12:59:32 +00002379
blueswir18662d652008-10-02 18:32:44 +00002380static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002381{
bellard6a00d602005-11-21 23:25:50 +00002382 CPUState *env = mon_get_cpu();
2383 if (!env)
2384 return 0;
2385 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002386}
2387
blueswir18662d652008-10-02 18:32:44 +00002388static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002389{
bellard6a00d602005-11-21 23:25:50 +00002390 CPUState *env = mon_get_cpu();
2391 if (!env)
2392 return 0;
2393 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002394}
2395
blueswir18662d652008-10-02 18:32:44 +00002396static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002397{
bellard6a00d602005-11-21 23:25:50 +00002398 CPUState *env = mon_get_cpu();
2399 if (!env)
2400 return 0;
2401 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002402}
bellarda541f292004-04-12 20:39:29 +00002403#endif
2404
bellarde95c8d52004-09-30 22:22:08 +00002405#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002406#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002407static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002408{
bellard6a00d602005-11-21 23:25:50 +00002409 CPUState *env = mon_get_cpu();
2410 if (!env)
2411 return 0;
2412 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002413}
bellard7b936c02005-10-30 17:05:13 +00002414#endif
bellarde95c8d52004-09-30 22:22:08 +00002415
blueswir18662d652008-10-02 18:32:44 +00002416static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002417{
bellard6a00d602005-11-21 23:25:50 +00002418 CPUState *env = mon_get_cpu();
2419 if (!env)
2420 return 0;
2421 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002422}
2423#endif
2424
blueswir18662d652008-10-02 18:32:44 +00002425static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002426#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002427
2428#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002429 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002430 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002431 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002432
bellard9307c4c2004-04-04 12:57:25 +00002433 { "eax", offsetof(CPUState, regs[0]) },
2434 { "ecx", offsetof(CPUState, regs[1]) },
2435 { "edx", offsetof(CPUState, regs[2]) },
2436 { "ebx", offsetof(CPUState, regs[3]) },
2437 { "esp|sp", offsetof(CPUState, regs[4]) },
2438 { "ebp|fp", offsetof(CPUState, regs[5]) },
2439 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002440 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002441#ifdef TARGET_X86_64
2442 { "r8", offsetof(CPUState, regs[8]) },
2443 { "r9", offsetof(CPUState, regs[9]) },
2444 { "r10", offsetof(CPUState, regs[10]) },
2445 { "r11", offsetof(CPUState, regs[11]) },
2446 { "r12", offsetof(CPUState, regs[12]) },
2447 { "r13", offsetof(CPUState, regs[13]) },
2448 { "r14", offsetof(CPUState, regs[14]) },
2449 { "r15", offsetof(CPUState, regs[15]) },
2450#endif
bellard9307c4c2004-04-04 12:57:25 +00002451 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002452 { "eip", offsetof(CPUState, eip) },
2453 SEG("cs", R_CS)
2454 SEG("ds", R_DS)
2455 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002456 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002457 SEG("fs", R_FS)
2458 SEG("gs", R_GS)
2459 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002460#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002461 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002462 { "r0", offsetof(CPUState, gpr[0]) },
2463 { "r1", offsetof(CPUState, gpr[1]) },
2464 { "r2", offsetof(CPUState, gpr[2]) },
2465 { "r3", offsetof(CPUState, gpr[3]) },
2466 { "r4", offsetof(CPUState, gpr[4]) },
2467 { "r5", offsetof(CPUState, gpr[5]) },
2468 { "r6", offsetof(CPUState, gpr[6]) },
2469 { "r7", offsetof(CPUState, gpr[7]) },
2470 { "r8", offsetof(CPUState, gpr[8]) },
2471 { "r9", offsetof(CPUState, gpr[9]) },
2472 { "r10", offsetof(CPUState, gpr[10]) },
2473 { "r11", offsetof(CPUState, gpr[11]) },
2474 { "r12", offsetof(CPUState, gpr[12]) },
2475 { "r13", offsetof(CPUState, gpr[13]) },
2476 { "r14", offsetof(CPUState, gpr[14]) },
2477 { "r15", offsetof(CPUState, gpr[15]) },
2478 { "r16", offsetof(CPUState, gpr[16]) },
2479 { "r17", offsetof(CPUState, gpr[17]) },
2480 { "r18", offsetof(CPUState, gpr[18]) },
2481 { "r19", offsetof(CPUState, gpr[19]) },
2482 { "r20", offsetof(CPUState, gpr[20]) },
2483 { "r21", offsetof(CPUState, gpr[21]) },
2484 { "r22", offsetof(CPUState, gpr[22]) },
2485 { "r23", offsetof(CPUState, gpr[23]) },
2486 { "r24", offsetof(CPUState, gpr[24]) },
2487 { "r25", offsetof(CPUState, gpr[25]) },
2488 { "r26", offsetof(CPUState, gpr[26]) },
2489 { "r27", offsetof(CPUState, gpr[27]) },
2490 { "r28", offsetof(CPUState, gpr[28]) },
2491 { "r29", offsetof(CPUState, gpr[29]) },
2492 { "r30", offsetof(CPUState, gpr[30]) },
2493 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002494 /* Floating point registers */
2495 { "f0", offsetof(CPUState, fpr[0]) },
2496 { "f1", offsetof(CPUState, fpr[1]) },
2497 { "f2", offsetof(CPUState, fpr[2]) },
2498 { "f3", offsetof(CPUState, fpr[3]) },
2499 { "f4", offsetof(CPUState, fpr[4]) },
2500 { "f5", offsetof(CPUState, fpr[5]) },
2501 { "f6", offsetof(CPUState, fpr[6]) },
2502 { "f7", offsetof(CPUState, fpr[7]) },
2503 { "f8", offsetof(CPUState, fpr[8]) },
2504 { "f9", offsetof(CPUState, fpr[9]) },
2505 { "f10", offsetof(CPUState, fpr[10]) },
2506 { "f11", offsetof(CPUState, fpr[11]) },
2507 { "f12", offsetof(CPUState, fpr[12]) },
2508 { "f13", offsetof(CPUState, fpr[13]) },
2509 { "f14", offsetof(CPUState, fpr[14]) },
2510 { "f15", offsetof(CPUState, fpr[15]) },
2511 { "f16", offsetof(CPUState, fpr[16]) },
2512 { "f17", offsetof(CPUState, fpr[17]) },
2513 { "f18", offsetof(CPUState, fpr[18]) },
2514 { "f19", offsetof(CPUState, fpr[19]) },
2515 { "f20", offsetof(CPUState, fpr[20]) },
2516 { "f21", offsetof(CPUState, fpr[21]) },
2517 { "f22", offsetof(CPUState, fpr[22]) },
2518 { "f23", offsetof(CPUState, fpr[23]) },
2519 { "f24", offsetof(CPUState, fpr[24]) },
2520 { "f25", offsetof(CPUState, fpr[25]) },
2521 { "f26", offsetof(CPUState, fpr[26]) },
2522 { "f27", offsetof(CPUState, fpr[27]) },
2523 { "f28", offsetof(CPUState, fpr[28]) },
2524 { "f29", offsetof(CPUState, fpr[29]) },
2525 { "f30", offsetof(CPUState, fpr[30]) },
2526 { "f31", offsetof(CPUState, fpr[31]) },
2527 { "fpscr", offsetof(CPUState, fpscr) },
2528 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002529 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002530 { "lr", offsetof(CPUState, lr) },
2531 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002532 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002533 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002534 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002535 { "msr", 0, &monitor_get_msr, },
2536 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002537 { "tbu", 0, &monitor_get_tbu, },
2538 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002539#if defined(TARGET_PPC64)
2540 /* Address space register */
2541 { "asr", offsetof(CPUState, asr) },
2542#endif
2543 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002544 { "sdr1", offsetof(CPUState, sdr1) },
2545 { "sr0", offsetof(CPUState, sr[0]) },
2546 { "sr1", offsetof(CPUState, sr[1]) },
2547 { "sr2", offsetof(CPUState, sr[2]) },
2548 { "sr3", offsetof(CPUState, sr[3]) },
2549 { "sr4", offsetof(CPUState, sr[4]) },
2550 { "sr5", offsetof(CPUState, sr[5]) },
2551 { "sr6", offsetof(CPUState, sr[6]) },
2552 { "sr7", offsetof(CPUState, sr[7]) },
2553 { "sr8", offsetof(CPUState, sr[8]) },
2554 { "sr9", offsetof(CPUState, sr[9]) },
2555 { "sr10", offsetof(CPUState, sr[10]) },
2556 { "sr11", offsetof(CPUState, sr[11]) },
2557 { "sr12", offsetof(CPUState, sr[12]) },
2558 { "sr13", offsetof(CPUState, sr[13]) },
2559 { "sr14", offsetof(CPUState, sr[14]) },
2560 { "sr15", offsetof(CPUState, sr[15]) },
2561 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002562#elif defined(TARGET_SPARC)
2563 { "g0", offsetof(CPUState, gregs[0]) },
2564 { "g1", offsetof(CPUState, gregs[1]) },
2565 { "g2", offsetof(CPUState, gregs[2]) },
2566 { "g3", offsetof(CPUState, gregs[3]) },
2567 { "g4", offsetof(CPUState, gregs[4]) },
2568 { "g5", offsetof(CPUState, gregs[5]) },
2569 { "g6", offsetof(CPUState, gregs[6]) },
2570 { "g7", offsetof(CPUState, gregs[7]) },
2571 { "o0", 0, monitor_get_reg },
2572 { "o1", 1, monitor_get_reg },
2573 { "o2", 2, monitor_get_reg },
2574 { "o3", 3, monitor_get_reg },
2575 { "o4", 4, monitor_get_reg },
2576 { "o5", 5, monitor_get_reg },
2577 { "o6", 6, monitor_get_reg },
2578 { "o7", 7, monitor_get_reg },
2579 { "l0", 8, monitor_get_reg },
2580 { "l1", 9, monitor_get_reg },
2581 { "l2", 10, monitor_get_reg },
2582 { "l3", 11, monitor_get_reg },
2583 { "l4", 12, monitor_get_reg },
2584 { "l5", 13, monitor_get_reg },
2585 { "l6", 14, monitor_get_reg },
2586 { "l7", 15, monitor_get_reg },
2587 { "i0", 16, monitor_get_reg },
2588 { "i1", 17, monitor_get_reg },
2589 { "i2", 18, monitor_get_reg },
2590 { "i3", 19, monitor_get_reg },
2591 { "i4", 20, monitor_get_reg },
2592 { "i5", 21, monitor_get_reg },
2593 { "i6", 22, monitor_get_reg },
2594 { "i7", 23, monitor_get_reg },
2595 { "pc", offsetof(CPUState, pc) },
2596 { "npc", offsetof(CPUState, npc) },
2597 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002598#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002599 { "psr", 0, &monitor_get_psr, },
2600 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002601#endif
bellarde95c8d52004-09-30 22:22:08 +00002602 { "tbr", offsetof(CPUState, tbr) },
2603 { "fsr", offsetof(CPUState, fsr) },
2604 { "f0", offsetof(CPUState, fpr[0]) },
2605 { "f1", offsetof(CPUState, fpr[1]) },
2606 { "f2", offsetof(CPUState, fpr[2]) },
2607 { "f3", offsetof(CPUState, fpr[3]) },
2608 { "f4", offsetof(CPUState, fpr[4]) },
2609 { "f5", offsetof(CPUState, fpr[5]) },
2610 { "f6", offsetof(CPUState, fpr[6]) },
2611 { "f7", offsetof(CPUState, fpr[7]) },
2612 { "f8", offsetof(CPUState, fpr[8]) },
2613 { "f9", offsetof(CPUState, fpr[9]) },
2614 { "f10", offsetof(CPUState, fpr[10]) },
2615 { "f11", offsetof(CPUState, fpr[11]) },
2616 { "f12", offsetof(CPUState, fpr[12]) },
2617 { "f13", offsetof(CPUState, fpr[13]) },
2618 { "f14", offsetof(CPUState, fpr[14]) },
2619 { "f15", offsetof(CPUState, fpr[15]) },
2620 { "f16", offsetof(CPUState, fpr[16]) },
2621 { "f17", offsetof(CPUState, fpr[17]) },
2622 { "f18", offsetof(CPUState, fpr[18]) },
2623 { "f19", offsetof(CPUState, fpr[19]) },
2624 { "f20", offsetof(CPUState, fpr[20]) },
2625 { "f21", offsetof(CPUState, fpr[21]) },
2626 { "f22", offsetof(CPUState, fpr[22]) },
2627 { "f23", offsetof(CPUState, fpr[23]) },
2628 { "f24", offsetof(CPUState, fpr[24]) },
2629 { "f25", offsetof(CPUState, fpr[25]) },
2630 { "f26", offsetof(CPUState, fpr[26]) },
2631 { "f27", offsetof(CPUState, fpr[27]) },
2632 { "f28", offsetof(CPUState, fpr[28]) },
2633 { "f29", offsetof(CPUState, fpr[29]) },
2634 { "f30", offsetof(CPUState, fpr[30]) },
2635 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002636#ifdef TARGET_SPARC64
2637 { "f32", offsetof(CPUState, fpr[32]) },
2638 { "f34", offsetof(CPUState, fpr[34]) },
2639 { "f36", offsetof(CPUState, fpr[36]) },
2640 { "f38", offsetof(CPUState, fpr[38]) },
2641 { "f40", offsetof(CPUState, fpr[40]) },
2642 { "f42", offsetof(CPUState, fpr[42]) },
2643 { "f44", offsetof(CPUState, fpr[44]) },
2644 { "f46", offsetof(CPUState, fpr[46]) },
2645 { "f48", offsetof(CPUState, fpr[48]) },
2646 { "f50", offsetof(CPUState, fpr[50]) },
2647 { "f52", offsetof(CPUState, fpr[52]) },
2648 { "f54", offsetof(CPUState, fpr[54]) },
2649 { "f56", offsetof(CPUState, fpr[56]) },
2650 { "f58", offsetof(CPUState, fpr[58]) },
2651 { "f60", offsetof(CPUState, fpr[60]) },
2652 { "f62", offsetof(CPUState, fpr[62]) },
2653 { "asi", offsetof(CPUState, asi) },
2654 { "pstate", offsetof(CPUState, pstate) },
2655 { "cansave", offsetof(CPUState, cansave) },
2656 { "canrestore", offsetof(CPUState, canrestore) },
2657 { "otherwin", offsetof(CPUState, otherwin) },
2658 { "wstate", offsetof(CPUState, wstate) },
2659 { "cleanwin", offsetof(CPUState, cleanwin) },
2660 { "fprs", offsetof(CPUState, fprs) },
2661#endif
bellard9307c4c2004-04-04 12:57:25 +00002662#endif
2663 { NULL },
2664};
2665
aliguori376253e2009-03-05 23:01:23 +00002666static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002667{
aliguori376253e2009-03-05 23:01:23 +00002668 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002669 longjmp(expr_env, 1);
2670}
2671
bellard6a00d602005-11-21 23:25:50 +00002672/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002673static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002674{
blueswir18662d652008-10-02 18:32:44 +00002675 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002676 void *ptr;
2677
bellard9307c4c2004-04-04 12:57:25 +00002678 for(md = monitor_defs; md->name != NULL; md++) {
2679 if (compare_cmd(name, md->name)) {
2680 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002681 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002682 } else {
bellard6a00d602005-11-21 23:25:50 +00002683 CPUState *env = mon_get_cpu();
2684 if (!env)
2685 return -2;
2686 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002687 switch(md->type) {
2688 case MD_I32:
2689 *pval = *(int32_t *)ptr;
2690 break;
2691 case MD_TLONG:
2692 *pval = *(target_long *)ptr;
2693 break;
2694 default:
2695 *pval = 0;
2696 break;
2697 }
bellard9307c4c2004-04-04 12:57:25 +00002698 }
2699 return 0;
2700 }
2701 }
2702 return -1;
2703}
2704
2705static void next(void)
2706{
Blue Swirl660f11b2009-07-31 21:16:51 +00002707 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002708 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002709 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002710 pch++;
2711 }
2712}
2713
aliguori376253e2009-03-05 23:01:23 +00002714static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002715
aliguori376253e2009-03-05 23:01:23 +00002716static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002717{
blueswir1c2efc952007-09-25 17:28:42 +00002718 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002719 char *p;
bellard6a00d602005-11-21 23:25:50 +00002720 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002721
2722 switch(*pch) {
2723 case '+':
2724 next();
aliguori376253e2009-03-05 23:01:23 +00002725 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002726 break;
2727 case '-':
2728 next();
aliguori376253e2009-03-05 23:01:23 +00002729 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002730 break;
2731 case '~':
2732 next();
aliguori376253e2009-03-05 23:01:23 +00002733 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002734 break;
2735 case '(':
2736 next();
aliguori376253e2009-03-05 23:01:23 +00002737 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002738 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002739 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002740 }
2741 next();
2742 break;
bellard81d09122004-07-14 17:21:37 +00002743 case '\'':
2744 pch++;
2745 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002746 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002747 n = *pch;
2748 pch++;
2749 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002750 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002751 next();
2752 break;
bellard9307c4c2004-04-04 12:57:25 +00002753 case '$':
2754 {
2755 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002756 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002757
bellard9307c4c2004-04-04 12:57:25 +00002758 pch++;
2759 q = buf;
2760 while ((*pch >= 'a' && *pch <= 'z') ||
2761 (*pch >= 'A' && *pch <= 'Z') ||
2762 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002763 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002764 if ((q - buf) < sizeof(buf) - 1)
2765 *q++ = *pch;
2766 pch++;
2767 }
blueswir1cd390082008-11-16 13:53:32 +00002768 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002769 pch++;
2770 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002771 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002772 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002773 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002774 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002775 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002776 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002777 }
2778 break;
2779 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002780 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002781 n = 0;
2782 break;
2783 default:
blueswir17743e582007-09-24 18:39:04 +00002784#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002785 n = strtoull(pch, &p, 0);
2786#else
bellard9307c4c2004-04-04 12:57:25 +00002787 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002788#endif
bellard9307c4c2004-04-04 12:57:25 +00002789 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002790 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002791 }
2792 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002793 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002794 pch++;
2795 break;
2796 }
2797 return n;
2798}
2799
2800
aliguori376253e2009-03-05 23:01:23 +00002801static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002802{
blueswir1c2efc952007-09-25 17:28:42 +00002803 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002804 int op;
ths3b46e622007-09-17 08:09:54 +00002805
aliguori376253e2009-03-05 23:01:23 +00002806 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002807 for(;;) {
2808 op = *pch;
2809 if (op != '*' && op != '/' && op != '%')
2810 break;
2811 next();
aliguori376253e2009-03-05 23:01:23 +00002812 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002813 switch(op) {
2814 default:
2815 case '*':
2816 val *= val2;
2817 break;
2818 case '/':
2819 case '%':
ths5fafdf22007-09-16 21:08:06 +00002820 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002821 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002822 if (op == '/')
2823 val /= val2;
2824 else
2825 val %= val2;
2826 break;
2827 }
2828 }
2829 return val;
2830}
2831
aliguori376253e2009-03-05 23:01:23 +00002832static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002833{
blueswir1c2efc952007-09-25 17:28:42 +00002834 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002835 int op;
bellard9307c4c2004-04-04 12:57:25 +00002836
aliguori376253e2009-03-05 23:01:23 +00002837 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002838 for(;;) {
2839 op = *pch;
2840 if (op != '&' && op != '|' && op != '^')
2841 break;
2842 next();
aliguori376253e2009-03-05 23:01:23 +00002843 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002844 switch(op) {
2845 default:
2846 case '&':
2847 val &= val2;
2848 break;
2849 case '|':
2850 val |= val2;
2851 break;
2852 case '^':
2853 val ^= val2;
2854 break;
2855 }
2856 }
2857 return val;
2858}
2859
aliguori376253e2009-03-05 23:01:23 +00002860static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002861{
blueswir1c2efc952007-09-25 17:28:42 +00002862 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002863 int op;
bellard9307c4c2004-04-04 12:57:25 +00002864
aliguori376253e2009-03-05 23:01:23 +00002865 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002866 for(;;) {
2867 op = *pch;
2868 if (op != '+' && op != '-')
2869 break;
2870 next();
aliguori376253e2009-03-05 23:01:23 +00002871 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002872 if (op == '+')
2873 val += val2;
2874 else
2875 val -= val2;
2876 }
2877 return val;
2878}
2879
aliguori376253e2009-03-05 23:01:23 +00002880static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002881{
2882 pch = *pp;
2883 if (setjmp(expr_env)) {
2884 *pp = pch;
2885 return -1;
2886 }
blueswir1cd390082008-11-16 13:53:32 +00002887 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002888 pch++;
aliguori376253e2009-03-05 23:01:23 +00002889 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002890 *pp = pch;
2891 return 0;
2892}
2893
2894static int get_str(char *buf, int buf_size, const char **pp)
2895{
2896 const char *p;
2897 char *q;
2898 int c;
2899
bellard81d09122004-07-14 17:21:37 +00002900 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002901 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002902 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002903 p++;
2904 if (*p == '\0') {
2905 fail:
bellard81d09122004-07-14 17:21:37 +00002906 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002907 *pp = p;
2908 return -1;
2909 }
bellard9307c4c2004-04-04 12:57:25 +00002910 if (*p == '\"') {
2911 p++;
2912 while (*p != '\0' && *p != '\"') {
2913 if (*p == '\\') {
2914 p++;
2915 c = *p++;
2916 switch(c) {
2917 case 'n':
2918 c = '\n';
2919 break;
2920 case 'r':
2921 c = '\r';
2922 break;
2923 case '\\':
2924 case '\'':
2925 case '\"':
2926 break;
2927 default:
2928 qemu_printf("unsupported escape code: '\\%c'\n", c);
2929 goto fail;
2930 }
2931 if ((q - buf) < buf_size - 1) {
2932 *q++ = c;
2933 }
2934 } else {
2935 if ((q - buf) < buf_size - 1) {
2936 *q++ = *p;
2937 }
2938 p++;
2939 }
2940 }
2941 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002942 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002943 goto fail;
2944 }
2945 p++;
2946 } else {
blueswir1cd390082008-11-16 13:53:32 +00002947 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002948 if ((q - buf) < buf_size - 1) {
2949 *q++ = *p;
2950 }
2951 p++;
2952 }
bellard9307c4c2004-04-04 12:57:25 +00002953 }
bellard81d09122004-07-14 17:21:37 +00002954 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002955 *pp = p;
2956 return 0;
2957}
2958
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002959/*
2960 * Store the command-name in cmdname, and return a pointer to
2961 * the remaining of the command string.
2962 */
2963static const char *get_command_name(const char *cmdline,
2964 char *cmdname, size_t nlen)
2965{
2966 size_t len;
2967 const char *p, *pstart;
2968
2969 p = cmdline;
2970 while (qemu_isspace(*p))
2971 p++;
2972 if (*p == '\0')
2973 return NULL;
2974 pstart = p;
2975 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2976 p++;
2977 len = p - pstart;
2978 if (len > nlen - 1)
2979 len = nlen - 1;
2980 memcpy(cmdname, pstart, len);
2981 cmdname[len] = '\0';
2982 return p;
2983}
2984
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002985/**
2986 * Read key of 'type' into 'key' and return the current
2987 * 'type' pointer.
2988 */
2989static char *key_get_info(const char *type, char **key)
2990{
2991 size_t len;
2992 char *p, *str;
2993
2994 if (*type == ',')
2995 type++;
2996
2997 p = strchr(type, ':');
2998 if (!p) {
2999 *key = NULL;
3000 return NULL;
3001 }
3002 len = p - type;
3003
3004 str = qemu_malloc(len + 1);
3005 memcpy(str, type, len);
3006 str[len] = '\0';
3007
3008 *key = str;
3009 return ++p;
3010}
3011
bellard9307c4c2004-04-04 12:57:25 +00003012static int default_fmt_format = 'x';
3013static int default_fmt_size = 4;
3014
3015#define MAX_ARGS 16
3016
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003017static int is_valid_option(const char *c, const char *typestr)
3018{
3019 char option[3];
3020
3021 option[0] = '-';
3022 option[1] = *c;
3023 option[2] = '\0';
3024
3025 typestr = strstr(typestr, option);
3026 return (typestr != NULL);
3027}
3028
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003029static const mon_cmd_t *monitor_find_command(const char *cmdname)
3030{
3031 const mon_cmd_t *cmd;
3032
3033 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3034 if (compare_cmd(cmdname, cmd->name)) {
3035 return cmd;
3036 }
3037 }
3038
3039 return NULL;
3040}
3041
Anthony Liguoric227f092009-10-01 16:12:16 -05003042static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003043 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003044 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003045{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003046 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003047 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003048 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003049 char cmdname[256];
3050 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003051 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003052
3053#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003054 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003055#endif
ths3b46e622007-09-17 08:09:54 +00003056
bellard9307c4c2004-04-04 12:57:25 +00003057 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003058 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3059 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003060 return NULL;
ths3b46e622007-09-17 08:09:54 +00003061
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003062 cmd = monitor_find_command(cmdname);
3063 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003064 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003065 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003066 }
bellard9307c4c2004-04-04 12:57:25 +00003067
bellard9307c4c2004-04-04 12:57:25 +00003068 /* parse the parameters */
3069 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003070 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003071 typestr = key_get_info(typestr, &key);
3072 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003073 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003074 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003075 typestr++;
3076 switch(c) {
3077 case 'F':
bellard81d09122004-07-14 17:21:37 +00003078 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003079 case 's':
3080 {
3081 int ret;
ths3b46e622007-09-17 08:09:54 +00003082
blueswir1cd390082008-11-16 13:53:32 +00003083 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003084 p++;
3085 if (*typestr == '?') {
3086 typestr++;
3087 if (*p == '\0') {
3088 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003089 break;
bellard9307c4c2004-04-04 12:57:25 +00003090 }
3091 }
3092 ret = get_str(buf, sizeof(buf), &p);
3093 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003094 switch(c) {
3095 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003096 monitor_printf(mon, "%s: filename expected\n",
3097 cmdname);
bellard81d09122004-07-14 17:21:37 +00003098 break;
3099 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003100 monitor_printf(mon, "%s: block device name expected\n",
3101 cmdname);
bellard81d09122004-07-14 17:21:37 +00003102 break;
3103 default:
aliguori376253e2009-03-05 23:01:23 +00003104 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003105 break;
3106 }
bellard9307c4c2004-04-04 12:57:25 +00003107 goto fail;
3108 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003109 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003110 }
3111 break;
3112 case '/':
3113 {
3114 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003115
blueswir1cd390082008-11-16 13:53:32 +00003116 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003117 p++;
3118 if (*p == '/') {
3119 /* format found */
3120 p++;
3121 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003122 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003123 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003124 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003125 count = count * 10 + (*p - '0');
3126 p++;
3127 }
3128 }
3129 size = -1;
3130 format = -1;
3131 for(;;) {
3132 switch(*p) {
3133 case 'o':
3134 case 'd':
3135 case 'u':
3136 case 'x':
3137 case 'i':
3138 case 'c':
3139 format = *p++;
3140 break;
3141 case 'b':
3142 size = 1;
3143 p++;
3144 break;
3145 case 'h':
3146 size = 2;
3147 p++;
3148 break;
3149 case 'w':
3150 size = 4;
3151 p++;
3152 break;
3153 case 'g':
3154 case 'L':
3155 size = 8;
3156 p++;
3157 break;
3158 default:
3159 goto next;
3160 }
3161 }
3162 next:
blueswir1cd390082008-11-16 13:53:32 +00003163 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003164 monitor_printf(mon, "invalid char in format: '%c'\n",
3165 *p);
bellard9307c4c2004-04-04 12:57:25 +00003166 goto fail;
3167 }
bellard9307c4c2004-04-04 12:57:25 +00003168 if (format < 0)
3169 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003170 if (format != 'i') {
3171 /* for 'i', not specifying a size gives -1 as size */
3172 if (size < 0)
3173 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003174 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003175 }
bellard9307c4c2004-04-04 12:57:25 +00003176 default_fmt_format = format;
3177 } else {
3178 count = 1;
3179 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003180 if (format != 'i') {
3181 size = default_fmt_size;
3182 } else {
3183 size = -1;
3184 }
bellard9307c4c2004-04-04 12:57:25 +00003185 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003186 qdict_put(qdict, "count", qint_from_int(count));
3187 qdict_put(qdict, "format", qint_from_int(format));
3188 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003189 }
3190 break;
3191 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003192 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00003193 {
blueswir1c2efc952007-09-25 17:28:42 +00003194 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003195
blueswir1cd390082008-11-16 13:53:32 +00003196 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003197 p++;
bellard34405572004-06-08 00:55:58 +00003198 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003199 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003200 if (*p == '\0') {
3201 typestr++;
3202 break;
3203 }
bellard34405572004-06-08 00:55:58 +00003204 } else {
3205 if (*p == '.') {
3206 p++;
blueswir1cd390082008-11-16 13:53:32 +00003207 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003208 p++;
bellard34405572004-06-08 00:55:58 +00003209 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003210 typestr++;
3211 break;
bellard34405572004-06-08 00:55:58 +00003212 }
3213 }
bellard13224a82006-07-14 22:03:35 +00003214 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003215 }
aliguori376253e2009-03-05 23:01:23 +00003216 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003217 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003218 /* Check if 'i' is greater than 32-bit */
3219 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3220 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3221 monitor_printf(mon, "integer is for 32-bit values\n");
3222 goto fail;
3223 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003224 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003225 }
3226 break;
3227 case '-':
3228 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003229 const char *tmp = p;
3230 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003231 /* option */
ths3b46e622007-09-17 08:09:54 +00003232
bellard9307c4c2004-04-04 12:57:25 +00003233 c = *typestr++;
3234 if (c == '\0')
3235 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003236 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003237 p++;
3238 has_option = 0;
3239 if (*p == '-') {
3240 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003241 if(c != *p) {
3242 if(!is_valid_option(p, typestr)) {
3243
3244 monitor_printf(mon, "%s: unsupported option -%c\n",
3245 cmdname, *p);
3246 goto fail;
3247 } else {
3248 skip_key = 1;
3249 }
bellard9307c4c2004-04-04 12:57:25 +00003250 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003251 if(skip_key) {
3252 p = tmp;
3253 } else {
3254 p++;
3255 has_option = 1;
3256 }
bellard9307c4c2004-04-04 12:57:25 +00003257 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003258 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003259 }
3260 break;
3261 default:
3262 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003263 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003264 goto fail;
3265 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003266 qemu_free(key);
3267 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003268 }
3269 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003270 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003271 p++;
3272 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003273 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3274 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003275 goto fail;
3276 }
3277
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003278 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003279
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003280fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003281 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003282 return NULL;
3283}
3284
Luiz Capitulino8204a912009-11-18 23:05:31 -02003285static void monitor_print_error(Monitor *mon)
3286{
3287 qerror_print(mon->error);
3288 QDECREF(mon->error);
3289 mon->error = NULL;
3290}
3291
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003292static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3293 const QDict *params)
3294{
3295 QObject *data = NULL;
3296
3297 cmd->mhandler.cmd_new(mon, params, &data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003298
3299 if (monitor_ctrl_mode(mon)) {
3300 /* Monitor Protocol */
3301 monitor_protocol_emitter(mon, data);
3302 } else {
3303 /* User Protocol */
3304 if (data)
3305 cmd->user_print(mon, data);
3306 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003307
3308 qobject_decref(data);
3309}
3310
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003311static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003312{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003313 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003314 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003315
3316 qdict = qdict_new();
3317
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003318 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003319 if (!cmd)
3320 goto out;
3321
3322 qemu_errors_to_mon(mon);
3323
3324 if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003325 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003326 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003327 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003328 }
3329
Luiz Capitulino8204a912009-11-18 23:05:31 -02003330 if (monitor_has_error(mon))
3331 monitor_print_error(mon);
3332
3333 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003334
3335out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003336 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003337}
3338
bellard81d09122004-07-14 17:21:37 +00003339static void cmd_completion(const char *name, const char *list)
3340{
3341 const char *p, *pstart;
3342 char cmd[128];
3343 int len;
3344
3345 p = list;
3346 for(;;) {
3347 pstart = p;
3348 p = strchr(p, '|');
3349 if (!p)
3350 p = pstart + strlen(pstart);
3351 len = p - pstart;
3352 if (len > sizeof(cmd) - 2)
3353 len = sizeof(cmd) - 2;
3354 memcpy(cmd, pstart, len);
3355 cmd[len] = '\0';
3356 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003357 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003358 }
3359 if (*p == '\0')
3360 break;
3361 p++;
3362 }
3363}
3364
3365static void file_completion(const char *input)
3366{
3367 DIR *ffs;
3368 struct dirent *d;
3369 char path[1024];
3370 char file[1024], file_prefix[1024];
3371 int input_path_len;
3372 const char *p;
3373
ths5fafdf22007-09-16 21:08:06 +00003374 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003375 if (!p) {
3376 input_path_len = 0;
3377 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003378 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003379 } else {
3380 input_path_len = p - input + 1;
3381 memcpy(path, input, input_path_len);
3382 if (input_path_len > sizeof(path) - 1)
3383 input_path_len = sizeof(path) - 1;
3384 path[input_path_len] = '\0';
3385 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3386 }
3387#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003388 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3389 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003390#endif
3391 ffs = opendir(path);
3392 if (!ffs)
3393 return;
3394 for(;;) {
3395 struct stat sb;
3396 d = readdir(ffs);
3397 if (!d)
3398 break;
3399 if (strstart(d->d_name, file_prefix, NULL)) {
3400 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003401 if (input_path_len < sizeof(file))
3402 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3403 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003404 /* stat the file to find out if it's a directory.
3405 * In that case add a slash to speed up typing long paths
3406 */
3407 stat(file, &sb);
3408 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003409 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003410 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003411 }
3412 }
3413 closedir(ffs);
3414}
3415
aliguori51de9762009-03-05 23:00:43 +00003416static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003417{
aliguori51de9762009-03-05 23:00:43 +00003418 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003419 const char *input = opaque;
3420
3421 if (input[0] == '\0' ||
3422 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003423 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003424 }
3425}
3426
3427/* NOTE: this parser is an approximate form of the real command parser */
3428static void parse_cmdline(const char *cmdline,
3429 int *pnb_args, char **args)
3430{
3431 const char *p;
3432 int nb_args, ret;
3433 char buf[1024];
3434
3435 p = cmdline;
3436 nb_args = 0;
3437 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003438 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003439 p++;
3440 if (*p == '\0')
3441 break;
3442 if (nb_args >= MAX_ARGS)
3443 break;
3444 ret = get_str(buf, sizeof(buf), &p);
3445 args[nb_args] = qemu_strdup(buf);
3446 nb_args++;
3447 if (ret < 0)
3448 break;
3449 }
3450 *pnb_args = nb_args;
3451}
3452
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003453static const char *next_arg_type(const char *typestr)
3454{
3455 const char *p = strchr(typestr, ':');
3456 return (p != NULL ? ++p : typestr);
3457}
3458
aliguori4c36ba32009-03-05 23:01:37 +00003459static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003460{
3461 const char *cmdname;
3462 char *args[MAX_ARGS];
3463 int nb_args, i, len;
3464 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003465 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003466 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003467
3468 parse_cmdline(cmdline, &nb_args, args);
3469#ifdef DEBUG_COMPLETION
3470 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003471 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003472 }
3473#endif
3474
3475 /* if the line ends with a space, it means we want to complete the
3476 next arg */
3477 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003478 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003479 if (nb_args >= MAX_ARGS)
3480 return;
3481 args[nb_args++] = qemu_strdup("");
3482 }
3483 if (nb_args <= 1) {
3484 /* command completion */
3485 if (nb_args == 0)
3486 cmdname = "";
3487 else
3488 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003489 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003490 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003491 cmd_completion(cmdname, cmd->name);
3492 }
3493 } else {
3494 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003495 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003496 if (compare_cmd(args[0], cmd->name))
3497 goto found;
3498 }
3499 return;
3500 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003501 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003502 for(i = 0; i < nb_args - 2; i++) {
3503 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003504 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003505 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003506 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003507 }
3508 }
3509 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003510 if (*ptype == '-' && ptype[1] != '\0') {
3511 ptype += 2;
3512 }
bellard81d09122004-07-14 17:21:37 +00003513 switch(*ptype) {
3514 case 'F':
3515 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003516 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003517 file_completion(str);
3518 break;
3519 case 'B':
3520 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003521 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003522 bdrv_iterate(block_completion_it, (void *)str);
3523 break;
bellard7fe48482004-10-09 18:08:01 +00003524 case 's':
3525 /* XXX: more generic ? */
3526 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003527 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003528 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3529 cmd_completion(str, cmd->name);
3530 }
bellard64866c32006-05-07 18:03:31 +00003531 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003532 char *sep = strrchr(str, '-');
3533 if (sep)
3534 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003535 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003536 for(key = key_defs; key->name != NULL; key++) {
3537 cmd_completion(str, key->name);
3538 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003539 } else if (!strcmp(cmd->name, "help|?")) {
3540 readline_set_completion_index(cur_mon->rs, strlen(str));
3541 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3542 cmd_completion(str, cmd->name);
3543 }
bellard7fe48482004-10-09 18:08:01 +00003544 }
3545 break;
bellard81d09122004-07-14 17:21:37 +00003546 default:
3547 break;
3548 }
3549 }
3550 for(i = 0; i < nb_args; i++)
3551 qemu_free(args[i]);
3552}
3553
aliguori731b0362009-03-05 23:01:42 +00003554static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003555{
aliguori731b0362009-03-05 23:01:42 +00003556 Monitor *mon = opaque;
3557
3558 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003559}
3560
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003561/**
3562 * monitor_control_read(): Read and handle QMP input
3563 */
3564static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
3565{
3566 Monitor *old_mon = cur_mon;
3567
3568 cur_mon = opaque;
3569
3570 // TODO: read QMP input
3571
3572 cur_mon = old_mon;
3573}
3574
aliguori731b0362009-03-05 23:01:42 +00003575static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003576{
aliguori731b0362009-03-05 23:01:42 +00003577 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003578 int i;
aliguori376253e2009-03-05 23:01:23 +00003579
aliguori731b0362009-03-05 23:01:42 +00003580 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003581
aliguoricde76ee2009-03-05 23:01:51 +00003582 if (cur_mon->rs) {
3583 for (i = 0; i < size; i++)
3584 readline_handle_byte(cur_mon->rs, buf[i]);
3585 } else {
3586 if (size == 0 || buf[size - 1] != 0)
3587 monitor_printf(cur_mon, "corrupted command\n");
3588 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003589 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003590 }
aliguori731b0362009-03-05 23:01:42 +00003591
3592 cur_mon = old_mon;
3593}
aliguorid8f44602008-10-06 13:52:44 +00003594
aliguori376253e2009-03-05 23:01:23 +00003595static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003596{
aliguori731b0362009-03-05 23:01:42 +00003597 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003598 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003599 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003600}
3601
aliguoricde76ee2009-03-05 23:01:51 +00003602int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003603{
aliguoricde76ee2009-03-05 23:01:51 +00003604 if (!mon->rs)
3605 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003606 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003607 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003608}
3609
aliguori376253e2009-03-05 23:01:23 +00003610void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003611{
aliguoricde76ee2009-03-05 23:01:51 +00003612 if (!mon->rs)
3613 return;
aliguori731b0362009-03-05 23:01:42 +00003614 if (--mon->suspend_cnt == 0)
3615 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003616}
3617
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003618/**
3619 * monitor_control_event(): Print QMP gretting
3620 */
3621static void monitor_control_event(void *opaque, int event)
3622{
3623 if (event == CHR_EVENT_OPENED) {
3624 QObject *data;
3625 Monitor *mon = opaque;
3626
3627 data = qobject_from_jsonf("{ 'QMP': { 'capabilities': [] } }");
3628 assert(data != NULL);
3629
3630 monitor_json_emitter(mon, data);
3631 qobject_decref(data);
3632 }
3633}
3634
aliguori731b0362009-03-05 23:01:42 +00003635static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003636{
aliguori376253e2009-03-05 23:01:23 +00003637 Monitor *mon = opaque;
3638
aliguori2724b182009-03-05 23:01:47 +00003639 switch (event) {
3640 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003641 mon->mux_out = 0;
3642 if (mon->reset_seen) {
3643 readline_restart(mon->rs);
3644 monitor_resume(mon);
3645 monitor_flush(mon);
3646 } else {
3647 mon->suspend_cnt = 0;
3648 }
aliguori2724b182009-03-05 23:01:47 +00003649 break;
ths86e94de2007-01-05 22:01:59 +00003650
aliguori2724b182009-03-05 23:01:47 +00003651 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003652 if (mon->reset_seen) {
3653 if (mon->suspend_cnt == 0) {
3654 monitor_printf(mon, "\n");
3655 }
3656 monitor_flush(mon);
3657 monitor_suspend(mon);
3658 } else {
3659 mon->suspend_cnt++;
3660 }
3661 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00003662 break;
3663
Amit Shahb6b8df52009-10-07 18:31:16 +05303664 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003665 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3666 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003667 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00003668 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003669 }
3670 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00003671 break;
3672 }
ths86e94de2007-01-05 22:01:59 +00003673}
3674
aliguori76655d62009-03-06 20:27:37 +00003675
3676/*
3677 * Local variables:
3678 * c-indent-level: 4
3679 * c-basic-offset: 4
3680 * tab-width: 8
3681 * End:
3682 */
3683
Luiz Capitulinoadcb1812009-11-26 22:58:52 -02003684const char *monitor_cmdline_parse(const char *cmdline, int *flags)
3685{
3686 const char *dev;
3687
3688 if (strstart(cmdline, "control,", &dev)) {
3689 if (strstart(dev, "vc", NULL)) {
3690 fprintf(stderr, "qemu: control mode is for low-level interaction ");
3691 fprintf(stderr, "cannot be used with device 'vc'\n");
3692 exit(1);
3693 }
3694 *flags &= ~MONITOR_USE_READLINE;
3695 *flags |= MONITOR_USE_CONTROL;
3696 return dev;
3697 }
3698
3699 return cmdline;
3700}
3701
aliguori731b0362009-03-05 23:01:42 +00003702void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003703{
aliguori731b0362009-03-05 23:01:42 +00003704 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003705 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003706
3707 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003708 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003709 is_first_init = 0;
3710 }
aliguori87127162009-03-05 23:01:29 +00003711
3712 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003713
aliguori87127162009-03-05 23:01:29 +00003714 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003715 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00003716 if (flags & MONITOR_USE_READLINE) {
3717 mon->rs = readline_init(mon, monitor_find_completion);
3718 monitor_read_command(mon, 0);
3719 }
aliguori87127162009-03-05 23:01:29 +00003720
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003721 if (monitor_ctrl_mode(mon)) {
3722 /* Control mode requires special handlers */
3723 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
3724 monitor_control_event, mon);
3725 } else {
3726 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
3727 monitor_event, mon);
3728 }
aliguori87127162009-03-05 23:01:29 +00003729
Blue Swirl72cf2d42009-09-12 07:36:22 +00003730 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003731 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003732 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003733}
3734
aliguori376253e2009-03-05 23:01:23 +00003735static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003736{
aliguoribb5fc202009-03-05 23:01:15 +00003737 BlockDriverState *bs = opaque;
3738 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003739
aliguoribb5fc202009-03-05 23:01:15 +00003740 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003741 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003742 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003743 }
aliguori731b0362009-03-05 23:01:42 +00003744 if (mon->password_completion_cb)
3745 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003746
aliguori731b0362009-03-05 23:01:42 +00003747 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003748}
aliguoric0f4ce72009-03-05 23:01:01 +00003749
aliguori376253e2009-03-05 23:01:23 +00003750void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003751 BlockDriverCompletionFunc *completion_cb,
3752 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003753{
aliguoricde76ee2009-03-05 23:01:51 +00003754 int err;
3755
aliguoribb5fc202009-03-05 23:01:15 +00003756 if (!bdrv_key_required(bs)) {
3757 if (completion_cb)
3758 completion_cb(opaque, 0);
3759 return;
3760 }
aliguoric0f4ce72009-03-05 23:01:01 +00003761
aliguori376253e2009-03-05 23:01:23 +00003762 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3763 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003764
aliguori731b0362009-03-05 23:01:42 +00003765 mon->password_completion_cb = completion_cb;
3766 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003767
aliguoricde76ee2009-03-05 23:01:51 +00003768 err = monitor_read_password(mon, bdrv_password_cb, bs);
3769
3770 if (err && completion_cb)
3771 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003772}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003773
3774typedef struct QemuErrorSink QemuErrorSink;
3775struct QemuErrorSink {
3776 enum {
3777 ERR_SINK_FILE,
3778 ERR_SINK_MONITOR,
3779 } dest;
3780 union {
3781 FILE *fp;
3782 Monitor *mon;
3783 };
3784 QemuErrorSink *previous;
3785};
3786
Blue Swirl528e93a2009-08-31 15:14:40 +00003787static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003788
3789void qemu_errors_to_file(FILE *fp)
3790{
3791 QemuErrorSink *sink;
3792
3793 sink = qemu_mallocz(sizeof(*sink));
3794 sink->dest = ERR_SINK_FILE;
3795 sink->fp = fp;
3796 sink->previous = qemu_error_sink;
3797 qemu_error_sink = sink;
3798}
3799
3800void qemu_errors_to_mon(Monitor *mon)
3801{
3802 QemuErrorSink *sink;
3803
3804 sink = qemu_mallocz(sizeof(*sink));
3805 sink->dest = ERR_SINK_MONITOR;
3806 sink->mon = mon;
3807 sink->previous = qemu_error_sink;
3808 qemu_error_sink = sink;
3809}
3810
3811void qemu_errors_to_previous(void)
3812{
3813 QemuErrorSink *sink;
3814
3815 assert(qemu_error_sink != NULL);
3816 sink = qemu_error_sink;
3817 qemu_error_sink = sink->previous;
3818 qemu_free(sink);
3819}
3820
3821void qemu_error(const char *fmt, ...)
3822{
3823 va_list args;
3824
3825 assert(qemu_error_sink != NULL);
3826 switch (qemu_error_sink->dest) {
3827 case ERR_SINK_FILE:
3828 va_start(args, fmt);
3829 vfprintf(qemu_error_sink->fp, fmt, args);
3830 va_end(args);
3831 break;
3832 case ERR_SINK_MONITOR:
3833 va_start(args, fmt);
3834 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3835 va_end(args);
3836 break;
3837 }
3838}
Luiz Capitulino8204a912009-11-18 23:05:31 -02003839
3840void qemu_error_internal(const char *file, int linenr, const char *func,
3841 const char *fmt, ...)
3842{
3843 va_list va;
3844 QError *qerror;
3845
3846 assert(qemu_error_sink != NULL);
3847
3848 va_start(va, fmt);
3849 qerror = qerror_from_info(file, linenr, func, fmt, &va);
3850 va_end(va);
3851
3852 switch (qemu_error_sink->dest) {
3853 case ERR_SINK_FILE:
3854 qerror_print(qerror);
3855 QDECREF(qerror);
3856 break;
3857 case ERR_SINK_MONITOR:
3858 assert(qemu_error_sink->mon->error == NULL);
3859 qemu_error_sink->mon->error = qerror;
3860 break;
3861 }
3862}