blob: 8d05164ac28076742f93f3199876b4a61d9a12ab [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
bellard9307c4c2004-04-04 12:57:25 +0000370 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000371 goto help;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300372
373 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000374 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300375 break;
bellard9dc39cb2004-03-14 21:38:27 +0000376 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300377
378 if (cmd->name == NULL)
379 goto help;
380
381 if (monitor_handler_ported(cmd)) {
382 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200383
384 if (!monitor_ctrl_mode(mon)) {
385 /*
386 * User Protocol function is called here, Monitor Protocol is
387 * handled by monitor_call_handler()
388 */
389 if (*ret_data)
390 cmd->user_print(mon, *ret_data);
391 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300392 } else {
393 cmd->mhandler.info(mon);
394 }
395
bellard9dc39cb2004-03-14 21:38:27 +0000396 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300397
398help:
399 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000400}
401
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300402/**
403 * do_info_version(): Show QEMU version
404 */
405static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000406{
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300407 *ret_data = QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION));
bellard9bc9d1c2004-10-10 15:15:51 +0000408}
409
aliguori376253e2009-03-05 23:01:23 +0000410static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000411{
412 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000413 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000414}
415
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200416/**
417 * do_info_commands(): List QMP available commands
418 *
419 * Return a QList of QStrings.
420 */
421static void do_info_commands(Monitor *mon, QObject **ret_data)
422{
423 QList *cmd_list;
424 const mon_cmd_t *cmd;
425
426 cmd_list = qlist_new();
427
428 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
429 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
430 qlist_append(cmd_list, qstring_from_str(cmd->name));
431 }
432 }
433
434 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
435 if (monitor_handler_ported(cmd)) {
436 char buf[128];
437 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
438 qlist_append(cmd_list, qstring_from_str(buf));
439 }
440 }
441
442 *ret_data = QOBJECT(cmd_list);
443}
444
aurel32bf4f74c2008-12-18 22:42:34 +0000445#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000446static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000447{
aliguori376253e2009-03-05 23:01:23 +0000448 monitor_printf(mon, "HPET is %s by QEMU\n",
449 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000450}
aurel32bf4f74c2008-12-18 22:42:34 +0000451#endif
aliguori16b29ae2008-12-17 23:28:44 +0000452
aliguori376253e2009-03-05 23:01:23 +0000453static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000454{
aliguori376253e2009-03-05 23:01:23 +0000455 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
456 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
457 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
458 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
459 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000460}
461
bellard6a00d602005-11-21 23:25:50 +0000462/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000463static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000464{
465 CPUState *env;
466
467 for(env = first_cpu; env != NULL; env = env->next_cpu) {
468 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000469 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000470 return 0;
471 }
472 }
473 return -1;
474}
475
pbrook9596ebb2007-11-18 01:44:38 +0000476static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000477{
aliguori731b0362009-03-05 23:01:42 +0000478 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000479 mon_set_cpu(0);
480 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300481 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000482 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000483}
484
aliguori376253e2009-03-05 23:01:23 +0000485static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000486{
bellard6a00d602005-11-21 23:25:50 +0000487 CPUState *env;
488 env = mon_get_cpu();
489 if (!env)
490 return;
bellard9307c4c2004-04-04 12:57:25 +0000491#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000492 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000493 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000494#else
aliguori376253e2009-03-05 23:01:23 +0000495 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000496 0);
bellard9307c4c2004-04-04 12:57:25 +0000497#endif
498}
499
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300500static void print_cpu_iter(QObject *obj, void *opaque)
501{
502 QDict *cpu;
503 int active = ' ';
504 Monitor *mon = opaque;
505
506 assert(qobject_type(obj) == QTYPE_QDICT);
507 cpu = qobject_to_qdict(obj);
508
509 if (strcmp(qdict_get_str(cpu, "current"), "yes") == 0)
510 active = '*';
511
512 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
513
514#if defined(TARGET_I386)
515 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
516 (target_ulong) qdict_get_int(cpu, "pc"));
517#elif defined(TARGET_PPC)
518 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
519 (target_long) qdict_get_int(cpu, "nip"));
520#elif defined(TARGET_SPARC)
521 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
522 (target_long) qdict_get_int(cpu, "pc"));
523 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
524 (target_long) qdict_get_int(cpu, "npc"));
525#elif defined(TARGET_MIPS)
526 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
527 (target_long) qdict_get_int(cpu, "PC"));
528#endif
529
530 if (strcmp(qdict_get_str(cpu, "halted"), "yes") == 0)
531 monitor_printf(mon, " (halted)");
532
533 monitor_printf(mon, "\n");
534}
535
536static void monitor_print_cpus(Monitor *mon, const QObject *data)
537{
538 QList *cpu_list;
539
540 assert(qobject_type(data) == QTYPE_QLIST);
541 cpu_list = qobject_to_qlist(data);
542 qlist_iter(cpu_list, print_cpu_iter, mon);
543}
544
545/**
546 * do_info_cpus(): Show CPU information
547 *
548 * Return a QList with a QDict for each CPU.
549 *
550 * For example:
551 *
552 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
553 * { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]
554 */
555static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000556{
557 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300558 QList *cpu_list;
559
560 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000561
562 /* just to set the default cpu if not already done */
563 mon_get_cpu();
564
565 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300566 const char *answer;
567 QDict *cpu = qdict_new();
568
Avi Kivity4c0960c2009-08-17 23:19:53 +0300569 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300570
571 qdict_put(cpu, "CPU", qint_from_int(env->cpu_index));
572 answer = (env == mon->mon_cpu) ? "yes" : "no";
573 qdict_put(cpu, "current", qstring_from_str(answer));
574
bellard6a00d602005-11-21 23:25:50 +0000575#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300576 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000577#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300578 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000579#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300580 qdict_put(cpu, "pc", qint_from_int(env->pc));
581 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000582#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300583 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000584#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300585 answer = env->halted ? "yes" : "no";
586 qdict_put(cpu, "halted", qstring_from_str(answer));
587
588 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000589 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300590
591 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000592}
593
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300594static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000595{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300596 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000597 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000598 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000599}
600
aliguori376253e2009-03-05 23:01:23 +0000601static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000602{
aliguori376253e2009-03-05 23:01:23 +0000603 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000604}
605
aliguori376253e2009-03-05 23:01:23 +0000606static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000607{
608 int i;
bellard7e2515e2004-08-01 21:52:19 +0000609 const char *str;
ths3b46e622007-09-17 08:09:54 +0000610
aliguoricde76ee2009-03-05 23:01:51 +0000611 if (!mon->rs)
612 return;
bellard7e2515e2004-08-01 21:52:19 +0000613 i = 0;
614 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000615 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000616 if (!str)
617 break;
aliguori376253e2009-03-05 23:01:23 +0000618 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000619 i++;
bellardaa455482004-04-04 13:07:25 +0000620 }
621}
622
j_mayer76a66252007-03-07 08:32:30 +0000623#if defined(TARGET_PPC)
624/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000625static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000626{
627 CPUState *env;
628
629 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000630 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000631}
632#endif
633
Luiz Capitulinob223f352009-10-07 13:41:56 -0300634/**
635 * do_quit(): Quit QEMU execution
636 */
637static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000638{
639 exit(0);
640}
641
aliguori376253e2009-03-05 23:01:23 +0000642static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000643{
644 if (bdrv_is_inserted(bs)) {
645 if (!force) {
646 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000647 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000648 return -1;
649 }
650 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000651 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000652 return -1;
653 }
654 }
655 bdrv_close(bs);
656 }
657 return 0;
658}
659
Luiz Capitulinoe1c923a2009-10-16 12:23:49 -0300660static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000661{
662 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300663 int force = qdict_get_int(qdict, "force");
664 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000665
bellard9307c4c2004-04-04 12:57:25 +0000666 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000667 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000668 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000669 return;
670 }
aliguori376253e2009-03-05 23:01:23 +0000671 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000672}
673
aliguori376253e2009-03-05 23:01:23 +0000674static void do_change_block(Monitor *mon, const char *device,
675 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000676{
677 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000678 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000679
bellard9307c4c2004-04-04 12:57:25 +0000680 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000681 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000682 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000683 return;
684 }
aurel322ecea9b2008-06-18 22:10:01 +0000685 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100686 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000687 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000688 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000689 return;
690 }
691 }
aliguori376253e2009-03-05 23:01:23 +0000692 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000693 return;
aurel322ecea9b2008-06-18 22:10:01 +0000694 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000695 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000696}
697
aliguori376253e2009-03-05 23:01:23 +0000698static void change_vnc_password_cb(Monitor *mon, const char *password,
699 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000700{
701 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000702 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000703
aliguori731b0362009-03-05 23:01:42 +0000704 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000705}
706
aliguori376253e2009-03-05 23:01:23 +0000707static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000708{
ths70848512007-08-25 01:37:05 +0000709 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000710 strcmp(target, "password") == 0) {
711 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000712 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000713 strncpy(password, arg, sizeof(password));
714 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000715 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000716 } else {
aliguori376253e2009-03-05 23:01:23 +0000717 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000718 }
ths70848512007-08-25 01:37:05 +0000719 } else {
aliguori28a76be2009-03-06 20:27:40 +0000720 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000721 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000722 }
thse25a5822007-08-25 01:36:20 +0000723}
724
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300725static void do_change(Monitor *mon, const QDict *qdict)
thse25a5822007-08-25 01:36:20 +0000726{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300727 const char *device = qdict_get_str(qdict, "device");
728 const char *target = qdict_get_str(qdict, "target");
729 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000730 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000731 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000732 } else {
aliguori28a76be2009-03-06 20:27:40 +0000733 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000734 }
735}
736
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300737static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000738{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300739 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000740}
741
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300742static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000743{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300744 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000745}
746
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300747static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000748{
749 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300750 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000751
bellard9307c4c2004-04-04 12:57:25 +0000752 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000753 mask = 0;
754 } else {
bellard9307c4c2004-04-04 12:57:25 +0000755 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000756 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000757 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000758 return;
759 }
760 }
761 cpu_set_log(mask);
762}
763
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300764static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000765{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300766 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000767 if (!option || !strcmp(option, "on")) {
768 singlestep = 1;
769 } else if (!strcmp(option, "off")) {
770 singlestep = 0;
771 } else {
772 monitor_printf(mon, "unexpected option %s\n", option);
773 }
774}
775
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -0300776/**
777 * do_stop(): Stop VM execution
778 */
779static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +0000780{
781 vm_stop(EXCP_INTERRUPT);
782}
783
aliguoribb5fc202009-03-05 23:01:15 +0000784static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000785
aliguori376253e2009-03-05 23:01:23 +0000786struct bdrv_iterate_context {
787 Monitor *mon;
788 int err;
789};
aliguoric0f4ce72009-03-05 23:01:01 +0000790
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300791/**
792 * do_cont(): Resume emulation.
793 */
794static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +0000795{
796 struct bdrv_iterate_context context = { mon, 0 };
797
798 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000799 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000800 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000801 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000802}
803
aliguoribb5fc202009-03-05 23:01:15 +0000804static void bdrv_key_cb(void *opaque, int err)
805{
aliguori376253e2009-03-05 23:01:23 +0000806 Monitor *mon = opaque;
807
aliguoribb5fc202009-03-05 23:01:15 +0000808 /* another key was set successfully, retry to continue */
809 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300810 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000811}
812
813static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
814{
aliguori376253e2009-03-05 23:01:23 +0000815 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000816
aliguori376253e2009-03-05 23:01:23 +0000817 if (!context->err && bdrv_key_required(bs)) {
818 context->err = -EBUSY;
819 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
820 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000821 }
822}
823
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300824static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000825{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300826 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000827 if (!device)
828 device = "tcp::" DEFAULT_GDBSTUB_PORT;
829 if (gdbserver_start(device) < 0) {
830 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
831 device);
832 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000833 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000834 } else {
aliguori59030a82009-04-05 18:43:41 +0000835 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
836 device);
bellard8a7ddc32004-03-31 19:00:16 +0000837 }
838}
839
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300840static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100841{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300842 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100843 if (select_watchdog_action(action) == -1) {
844 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
845 }
846}
847
aliguori376253e2009-03-05 23:01:23 +0000848static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000849{
aliguori376253e2009-03-05 23:01:23 +0000850 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000851 switch(c) {
852 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000853 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000854 break;
855 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000856 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000857 break;
858 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000859 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000860 break;
861 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000862 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000863 break;
864 default:
865 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000866 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000867 } else {
aliguori376253e2009-03-05 23:01:23 +0000868 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000869 }
870 break;
871 }
aliguori376253e2009-03-05 23:01:23 +0000872 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000873}
874
aliguori376253e2009-03-05 23:01:23 +0000875static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -0500876 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000877{
bellard6a00d602005-11-21 23:25:50 +0000878 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000879 int nb_per_line, l, line_size, i, max_digits, len;
880 uint8_t buf[16];
881 uint64_t v;
882
883 if (format == 'i') {
884 int flags;
885 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000886 env = mon_get_cpu();
887 if (!env && !is_physical)
888 return;
bellard9307c4c2004-04-04 12:57:25 +0000889#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000890 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000891 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000892 } else if (wsize == 4) {
893 flags = 0;
894 } else {
bellard6a15fd12006-04-12 21:07:07 +0000895 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000896 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000897 if (env) {
898#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000899 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000900 (env->segs[R_CS].flags & DESC_L_MASK))
901 flags = 2;
902 else
903#endif
904 if (!(env->segs[R_CS].flags & DESC_B_MASK))
905 flags = 1;
906 }
bellard4c27ba22004-04-25 18:05:08 +0000907 }
908#endif
aliguori376253e2009-03-05 23:01:23 +0000909 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000910 return;
911 }
912
913 len = wsize * count;
914 if (wsize == 1)
915 line_size = 8;
916 else
917 line_size = 16;
918 nb_per_line = line_size / wsize;
919 max_digits = 0;
920
921 switch(format) {
922 case 'o':
923 max_digits = (wsize * 8 + 2) / 3;
924 break;
925 default:
926 case 'x':
927 max_digits = (wsize * 8) / 4;
928 break;
929 case 'u':
930 case 'd':
931 max_digits = (wsize * 8 * 10 + 32) / 33;
932 break;
933 case 'c':
934 wsize = 1;
935 break;
936 }
937
938 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000939 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000940 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000941 else
aliguori376253e2009-03-05 23:01:23 +0000942 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000943 l = len;
944 if (l > line_size)
945 l = line_size;
946 if (is_physical) {
947 cpu_physical_memory_rw(addr, buf, l, 0);
948 } else {
bellard6a00d602005-11-21 23:25:50 +0000949 env = mon_get_cpu();
950 if (!env)
951 break;
aliguoric8f79b62008-08-18 14:00:20 +0000952 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000953 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000954 break;
955 }
bellard9307c4c2004-04-04 12:57:25 +0000956 }
ths5fafdf22007-09-16 21:08:06 +0000957 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000958 while (i < l) {
959 switch(wsize) {
960 default:
961 case 1:
962 v = ldub_raw(buf + i);
963 break;
964 case 2:
965 v = lduw_raw(buf + i);
966 break;
967 case 4:
bellard92a31b12005-02-10 22:00:52 +0000968 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000969 break;
970 case 8:
971 v = ldq_raw(buf + i);
972 break;
973 }
aliguori376253e2009-03-05 23:01:23 +0000974 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000975 switch(format) {
976 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000977 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000978 break;
979 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000980 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000981 break;
982 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000983 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000984 break;
985 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000986 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000987 break;
988 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000989 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000990 break;
991 }
992 i += wsize;
993 }
aliguori376253e2009-03-05 23:01:23 +0000994 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000995 addr += l;
996 len -= l;
997 }
998}
999
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001000static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001001{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001002 int count = qdict_get_int(qdict, "count");
1003 int format = qdict_get_int(qdict, "format");
1004 int size = qdict_get_int(qdict, "size");
1005 target_long addr = qdict_get_int(qdict, "addr");
1006
aliguori376253e2009-03-05 23:01:23 +00001007 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001008}
1009
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001010static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001011{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001012 int count = qdict_get_int(qdict, "count");
1013 int format = qdict_get_int(qdict, "format");
1014 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001015 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001016
aliguori376253e2009-03-05 23:01:23 +00001017 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001018}
1019
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001020static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001021{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001022 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001023 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001024
blueswir17743e582007-09-24 18:39:04 +00001025#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001026 switch(format) {
1027 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001028 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001029 break;
1030 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001031 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001032 break;
1033 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001034 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001035 break;
1036 default:
1037 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001038 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001039 break;
1040 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001041 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001042 break;
1043 }
bellard92a31b12005-02-10 22:00:52 +00001044#else
1045 switch(format) {
1046 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001047 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001048 break;
1049 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001050 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001051 break;
1052 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001053 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001054 break;
1055 default:
1056 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001057 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001058 break;
1059 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001060 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001061 break;
1062 }
1063#endif
aliguori376253e2009-03-05 23:01:23 +00001064 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001065}
1066
Luiz Capitulino57e09452009-10-16 12:23:43 -03001067static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001068{
1069 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001070 uint32_t size = qdict_get_int(qdict, "size");
1071 const char *filename = qdict_get_str(qdict, "filename");
1072 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001073 uint32_t l;
1074 CPUState *env;
1075 uint8_t buf[1024];
1076
1077 env = mon_get_cpu();
1078 if (!env)
1079 return;
1080
1081 f = fopen(filename, "wb");
1082 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001083 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +00001084 return;
1085 }
1086 while (size != 0) {
1087 l = sizeof(buf);
1088 if (l > size)
1089 l = size;
1090 cpu_memory_rw_debug(env, addr, buf, l, 0);
1091 fwrite(buf, 1, l, f);
1092 addr += l;
1093 size -= l;
1094 }
1095 fclose(f);
1096}
1097
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001098static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1099 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001100{
1101 FILE *f;
1102 uint32_t l;
1103 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001104 uint32_t size = qdict_get_int(qdict, "size");
1105 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001106 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +00001107
1108 f = fopen(filename, "wb");
1109 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001110 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +00001111 return;
1112 }
1113 while (size != 0) {
1114 l = sizeof(buf);
1115 if (l > size)
1116 l = size;
1117 cpu_physical_memory_rw(addr, buf, l, 0);
1118 fwrite(buf, 1, l, f);
1119 fflush(f);
1120 addr += l;
1121 size -= l;
1122 }
1123 fclose(f);
1124}
1125
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001126static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001127{
1128 uint32_t addr;
1129 uint8_t buf[1];
1130 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001131 uint32_t start = qdict_get_int(qdict, "start");
1132 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001133
1134 sum = 0;
1135 for(addr = start; addr < (start + size); addr++) {
1136 cpu_physical_memory_rw(addr, buf, 1, 0);
1137 /* BSD sum algorithm ('sum' Unix command) */
1138 sum = (sum >> 1) | (sum << 15);
1139 sum += buf[0];
1140 }
aliguori376253e2009-03-05 23:01:23 +00001141 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001142}
1143
bellarda3a91a32004-06-04 11:06:21 +00001144typedef struct {
1145 int keycode;
1146 const char *name;
1147} KeyDef;
1148
1149static const KeyDef key_defs[] = {
1150 { 0x2a, "shift" },
1151 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001152
bellarda3a91a32004-06-04 11:06:21 +00001153 { 0x38, "alt" },
1154 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001155 { 0x64, "altgr" },
1156 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001157 { 0x1d, "ctrl" },
1158 { 0x9d, "ctrl_r" },
1159
1160 { 0xdd, "menu" },
1161
1162 { 0x01, "esc" },
1163
1164 { 0x02, "1" },
1165 { 0x03, "2" },
1166 { 0x04, "3" },
1167 { 0x05, "4" },
1168 { 0x06, "5" },
1169 { 0x07, "6" },
1170 { 0x08, "7" },
1171 { 0x09, "8" },
1172 { 0x0a, "9" },
1173 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001174 { 0x0c, "minus" },
1175 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001176 { 0x0e, "backspace" },
1177
1178 { 0x0f, "tab" },
1179 { 0x10, "q" },
1180 { 0x11, "w" },
1181 { 0x12, "e" },
1182 { 0x13, "r" },
1183 { 0x14, "t" },
1184 { 0x15, "y" },
1185 { 0x16, "u" },
1186 { 0x17, "i" },
1187 { 0x18, "o" },
1188 { 0x19, "p" },
1189
1190 { 0x1c, "ret" },
1191
1192 { 0x1e, "a" },
1193 { 0x1f, "s" },
1194 { 0x20, "d" },
1195 { 0x21, "f" },
1196 { 0x22, "g" },
1197 { 0x23, "h" },
1198 { 0x24, "j" },
1199 { 0x25, "k" },
1200 { 0x26, "l" },
1201
1202 { 0x2c, "z" },
1203 { 0x2d, "x" },
1204 { 0x2e, "c" },
1205 { 0x2f, "v" },
1206 { 0x30, "b" },
1207 { 0x31, "n" },
1208 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001209 { 0x33, "comma" },
1210 { 0x34, "dot" },
1211 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001212
balrog4d3b6f62008-02-10 16:33:14 +00001213 { 0x37, "asterisk" },
1214
bellarda3a91a32004-06-04 11:06:21 +00001215 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001216 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001217 { 0x3b, "f1" },
1218 { 0x3c, "f2" },
1219 { 0x3d, "f3" },
1220 { 0x3e, "f4" },
1221 { 0x3f, "f5" },
1222 { 0x40, "f6" },
1223 { 0x41, "f7" },
1224 { 0x42, "f8" },
1225 { 0x43, "f9" },
1226 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001227 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001228 { 0x46, "scroll_lock" },
1229
bellard64866c32006-05-07 18:03:31 +00001230 { 0xb5, "kp_divide" },
1231 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001232 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001233 { 0x4e, "kp_add" },
1234 { 0x9c, "kp_enter" },
1235 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001236 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001237
1238 { 0x52, "kp_0" },
1239 { 0x4f, "kp_1" },
1240 { 0x50, "kp_2" },
1241 { 0x51, "kp_3" },
1242 { 0x4b, "kp_4" },
1243 { 0x4c, "kp_5" },
1244 { 0x4d, "kp_6" },
1245 { 0x47, "kp_7" },
1246 { 0x48, "kp_8" },
1247 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001248
bellarda3a91a32004-06-04 11:06:21 +00001249 { 0x56, "<" },
1250
1251 { 0x57, "f11" },
1252 { 0x58, "f12" },
1253
1254 { 0xb7, "print" },
1255
1256 { 0xc7, "home" },
1257 { 0xc9, "pgup" },
1258 { 0xd1, "pgdn" },
1259 { 0xcf, "end" },
1260
1261 { 0xcb, "left" },
1262 { 0xc8, "up" },
1263 { 0xd0, "down" },
1264 { 0xcd, "right" },
1265
1266 { 0xd2, "insert" },
1267 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001268#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1269 { 0xf0, "stop" },
1270 { 0xf1, "again" },
1271 { 0xf2, "props" },
1272 { 0xf3, "undo" },
1273 { 0xf4, "front" },
1274 { 0xf5, "copy" },
1275 { 0xf6, "open" },
1276 { 0xf7, "paste" },
1277 { 0xf8, "find" },
1278 { 0xf9, "cut" },
1279 { 0xfa, "lf" },
1280 { 0xfb, "help" },
1281 { 0xfc, "meta_l" },
1282 { 0xfd, "meta_r" },
1283 { 0xfe, "compose" },
1284#endif
bellarda3a91a32004-06-04 11:06:21 +00001285 { 0, NULL },
1286};
1287
1288static int get_keycode(const char *key)
1289{
1290 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001291 char *endp;
1292 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001293
1294 for(p = key_defs; p->name != NULL; p++) {
1295 if (!strcmp(key, p->name))
1296 return p->keycode;
1297 }
bellard64866c32006-05-07 18:03:31 +00001298 if (strstart(key, "0x", NULL)) {
1299 ret = strtoul(key, &endp, 0);
1300 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1301 return ret;
1302 }
bellarda3a91a32004-06-04 11:06:21 +00001303 return -1;
1304}
1305
balrogc8256f92008-06-08 22:45:01 +00001306#define MAX_KEYCODES 16
1307static uint8_t keycodes[MAX_KEYCODES];
1308static int nb_pending_keycodes;
1309static QEMUTimer *key_timer;
1310
1311static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001312{
balrogc8256f92008-06-08 22:45:01 +00001313 int keycode;
1314
1315 while (nb_pending_keycodes > 0) {
1316 nb_pending_keycodes--;
1317 keycode = keycodes[nb_pending_keycodes];
1318 if (keycode & 0x80)
1319 kbd_put_keycode(0xe0);
1320 kbd_put_keycode(keycode | 0x80);
1321 }
1322}
1323
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001324static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001325{
balrog3401c0d2008-06-04 10:05:59 +00001326 char keyname_buf[16];
1327 char *separator;
1328 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001329 const char *string = qdict_get_str(qdict, "string");
1330 int has_hold_time = qdict_haskey(qdict, "hold_time");
1331 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001332
balrogc8256f92008-06-08 22:45:01 +00001333 if (nb_pending_keycodes > 0) {
1334 qemu_del_timer(key_timer);
1335 release_keys(NULL);
1336 }
1337 if (!has_hold_time)
1338 hold_time = 100;
1339 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001340 while (1) {
1341 separator = strchr(string, '-');
1342 keyname_len = separator ? separator - string : strlen(string);
1343 if (keyname_len > 0) {
1344 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1345 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001346 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001347 return;
bellarda3a91a32004-06-04 11:06:21 +00001348 }
balrogc8256f92008-06-08 22:45:01 +00001349 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001350 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001351 return;
1352 }
1353 keyname_buf[keyname_len] = 0;
1354 keycode = get_keycode(keyname_buf);
1355 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001356 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001357 return;
1358 }
balrogc8256f92008-06-08 22:45:01 +00001359 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001360 }
balrog3401c0d2008-06-04 10:05:59 +00001361 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001362 break;
balrog3401c0d2008-06-04 10:05:59 +00001363 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001364 }
balrogc8256f92008-06-08 22:45:01 +00001365 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001366 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001367 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001368 keycode = keycodes[i];
1369 if (keycode & 0x80)
1370 kbd_put_keycode(0xe0);
1371 kbd_put_keycode(keycode & 0x7f);
1372 }
balrogc8256f92008-06-08 22:45:01 +00001373 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001374 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001375 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001376}
1377
bellard13224a82006-07-14 22:03:35 +00001378static int mouse_button_state;
1379
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001380static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001381{
1382 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001383 const char *dx_str = qdict_get_str(qdict, "dx_str");
1384 const char *dy_str = qdict_get_str(qdict, "dy_str");
1385 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001386 dx = strtol(dx_str, NULL, 0);
1387 dy = strtol(dy_str, NULL, 0);
1388 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001389 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001390 dz = strtol(dz_str, NULL, 0);
1391 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1392}
1393
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001394static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001395{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001396 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001397 mouse_button_state = button_state;
1398 kbd_mouse_event(0, 0, 0, mouse_button_state);
1399}
1400
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001401static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001402{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001403 int size = qdict_get_int(qdict, "size");
1404 int addr = qdict_get_int(qdict, "addr");
1405 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001406 uint32_t val;
1407 int suffix;
1408
1409 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001410 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001411 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001412 addr++;
1413 }
1414 addr &= 0xffff;
1415
1416 switch(size) {
1417 default:
1418 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001419 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001420 suffix = 'b';
1421 break;
1422 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001423 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001424 suffix = 'w';
1425 break;
1426 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001427 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001428 suffix = 'l';
1429 break;
1430 }
aliguori376253e2009-03-05 23:01:23 +00001431 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1432 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001433}
bellarda3a91a32004-06-04 11:06:21 +00001434
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001435static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001436{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001437 int size = qdict_get_int(qdict, "size");
1438 int addr = qdict_get_int(qdict, "addr");
1439 int val = qdict_get_int(qdict, "val");
1440
Jan Kiszkaf1147842009-07-14 10:20:11 +02001441 addr &= IOPORTS_MASK;
1442
1443 switch (size) {
1444 default:
1445 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001446 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001447 break;
1448 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001449 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001450 break;
1451 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001452 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001453 break;
1454 }
1455}
1456
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001457static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001458{
1459 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001460 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001461
Jan Kiszka76e30d02009-07-02 00:19:02 +02001462 res = qemu_boot_set(bootdevice);
1463 if (res == 0) {
1464 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1465 } else if (res > 0) {
1466 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001467 } else {
aliguori376253e2009-03-05 23:01:23 +00001468 monitor_printf(mon, "no function defined to set boot device list for "
1469 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001470 }
1471}
1472
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001473/**
1474 * do_system_reset(): Issue a machine reset
1475 */
1476static void do_system_reset(Monitor *mon, const QDict *qdict,
1477 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001478{
1479 qemu_system_reset_request();
1480}
1481
Luiz Capitulino43076662009-10-07 13:41:59 -03001482/**
1483 * do_system_powerdown(): Issue a machine powerdown
1484 */
1485static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1486 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001487{
1488 qemu_system_powerdown_request();
1489}
1490
bellardb86bda52004-09-18 19:32:46 +00001491#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001492static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001493{
aliguori376253e2009-03-05 23:01:23 +00001494 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1495 addr,
1496 pte & mask,
1497 pte & PG_GLOBAL_MASK ? 'G' : '-',
1498 pte & PG_PSE_MASK ? 'P' : '-',
1499 pte & PG_DIRTY_MASK ? 'D' : '-',
1500 pte & PG_ACCESSED_MASK ? 'A' : '-',
1501 pte & PG_PCD_MASK ? 'C' : '-',
1502 pte & PG_PWT_MASK ? 'T' : '-',
1503 pte & PG_USER_MASK ? 'U' : '-',
1504 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001505}
1506
aliguori376253e2009-03-05 23:01:23 +00001507static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001508{
bellard6a00d602005-11-21 23:25:50 +00001509 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001510 int l1, l2;
1511 uint32_t pgd, pde, pte;
1512
bellard6a00d602005-11-21 23:25:50 +00001513 env = mon_get_cpu();
1514 if (!env)
1515 return;
1516
bellardb86bda52004-09-18 19:32:46 +00001517 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001518 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001519 return;
1520 }
1521 pgd = env->cr[3] & ~0xfff;
1522 for(l1 = 0; l1 < 1024; l1++) {
1523 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1524 pde = le32_to_cpu(pde);
1525 if (pde & PG_PRESENT_MASK) {
1526 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001527 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001528 } else {
1529 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001530 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001531 (uint8_t *)&pte, 4);
1532 pte = le32_to_cpu(pte);
1533 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001534 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001535 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001536 ~0xfff);
1537 }
1538 }
1539 }
1540 }
1541 }
1542}
1543
aliguori376253e2009-03-05 23:01:23 +00001544static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001545 uint32_t end, int prot)
1546{
bellard9746b152004-11-11 18:30:24 +00001547 int prot1;
1548 prot1 = *plast_prot;
1549 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001550 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001551 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1552 *pstart, end, end - *pstart,
1553 prot1 & PG_USER_MASK ? 'u' : '-',
1554 'r',
1555 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001556 }
1557 if (prot != 0)
1558 *pstart = end;
1559 else
1560 *pstart = -1;
1561 *plast_prot = prot;
1562 }
1563}
1564
aliguori376253e2009-03-05 23:01:23 +00001565static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001566{
bellard6a00d602005-11-21 23:25:50 +00001567 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001568 int l1, l2, prot, last_prot;
1569 uint32_t pgd, pde, pte, start, end;
1570
bellard6a00d602005-11-21 23:25:50 +00001571 env = mon_get_cpu();
1572 if (!env)
1573 return;
1574
bellardb86bda52004-09-18 19:32:46 +00001575 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001576 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001577 return;
1578 }
1579 pgd = env->cr[3] & ~0xfff;
1580 last_prot = 0;
1581 start = -1;
1582 for(l1 = 0; l1 < 1024; l1++) {
1583 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1584 pde = le32_to_cpu(pde);
1585 end = l1 << 22;
1586 if (pde & PG_PRESENT_MASK) {
1587 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1588 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001589 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001590 } else {
1591 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001592 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001593 (uint8_t *)&pte, 4);
1594 pte = le32_to_cpu(pte);
1595 end = (l1 << 22) + (l2 << 12);
1596 if (pte & PG_PRESENT_MASK) {
1597 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1598 } else {
1599 prot = 0;
1600 }
aliguori376253e2009-03-05 23:01:23 +00001601 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001602 }
1603 }
1604 } else {
1605 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001606 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001607 }
1608 }
1609}
1610#endif
1611
aurel327c664e22009-03-03 06:12:22 +00001612#if defined(TARGET_SH4)
1613
aliguori376253e2009-03-05 23:01:23 +00001614static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001615{
aliguori376253e2009-03-05 23:01:23 +00001616 monitor_printf(mon, " tlb%i:\t"
1617 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1618 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1619 "dirty=%hhu writethrough=%hhu\n",
1620 idx,
1621 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1622 tlb->v, tlb->sh, tlb->c, tlb->pr,
1623 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001624}
1625
aliguori376253e2009-03-05 23:01:23 +00001626static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001627{
1628 CPUState *env = mon_get_cpu();
1629 int i;
1630
aliguori376253e2009-03-05 23:01:23 +00001631 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001632 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001633 print_tlb (mon, i, &env->itlb[i]);
1634 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001635 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001636 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001637}
1638
1639#endif
1640
aliguori376253e2009-03-05 23:01:23 +00001641static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001642{
1643#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001644 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001645 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001646 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001647 else
aliguori376253e2009-03-05 23:01:23 +00001648 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001649#else
aliguori376253e2009-03-05 23:01:23 +00001650 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001651#endif
1652}
1653
aliguori030ea372009-04-21 22:30:47 +00001654static void do_info_numa(Monitor *mon)
1655{
aliguorib28b6232009-04-22 20:20:29 +00001656 int i;
aliguori030ea372009-04-21 22:30:47 +00001657 CPUState *env;
1658
1659 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1660 for (i = 0; i < nb_numa_nodes; i++) {
1661 monitor_printf(mon, "node %d cpus:", i);
1662 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1663 if (env->numa_node == i) {
1664 monitor_printf(mon, " %d", env->cpu_index);
1665 }
1666 }
1667 monitor_printf(mon, "\n");
1668 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1669 node_mem[i] >> 20);
1670 }
1671}
1672
bellard5f1ce942006-02-08 22:40:15 +00001673#ifdef CONFIG_PROFILER
1674
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001675int64_t qemu_time;
1676int64_t dev_time;
1677
aliguori376253e2009-03-05 23:01:23 +00001678static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001679{
1680 int64_t total;
1681 total = qemu_time;
1682 if (total == 0)
1683 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001684 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001685 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001686 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001687 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001688 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001689 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001690}
1691#else
aliguori376253e2009-03-05 23:01:23 +00001692static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001693{
aliguori376253e2009-03-05 23:01:23 +00001694 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001695}
1696#endif
1697
bellardec36b692006-07-16 18:57:03 +00001698/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001699static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001700
aliguori376253e2009-03-05 23:01:23 +00001701static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001702{
1703 int i;
1704 CaptureState *s;
1705
1706 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001707 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001708 s->ops.info (s->opaque);
1709 }
1710}
1711
Blue Swirl23130862009-06-06 08:22:04 +00001712#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001713static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001714{
1715 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001716 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001717 CaptureState *s;
1718
1719 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1720 if (i == n) {
1721 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001722 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00001723 qemu_free (s);
1724 return;
1725 }
1726 }
1727}
1728
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001729static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001730{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001731 const char *path = qdict_get_str(qdict, "path");
1732 int has_freq = qdict_haskey(qdict, "freq");
1733 int freq = qdict_get_try_int(qdict, "freq", -1);
1734 int has_bits = qdict_haskey(qdict, "bits");
1735 int bits = qdict_get_try_int(qdict, "bits", -1);
1736 int has_channels = qdict_haskey(qdict, "nchannels");
1737 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001738 CaptureState *s;
1739
1740 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001741
1742 freq = has_freq ? freq : 44100;
1743 bits = has_bits ? bits : 16;
1744 nchannels = has_channels ? nchannels : 2;
1745
1746 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001747 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001748 qemu_free (s);
1749 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001750 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001751}
1752#endif
1753
aurel32dc1c0b72008-04-27 23:52:12 +00001754#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001755static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001756{
1757 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001758 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001759
1760 for (env = first_cpu; env != NULL; env = env->next_cpu)
1761 if (env->cpu_index == cpu_index) {
1762 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1763 break;
1764 }
1765}
1766#endif
1767
aliguori376253e2009-03-05 23:01:23 +00001768static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001769{
aurel321b530a62009-04-05 20:08:59 +00001770 if (vm_running) {
1771 if (singlestep) {
1772 monitor_printf(mon, "VM status: running (single step mode)\n");
1773 } else {
1774 monitor_printf(mon, "VM status: running\n");
1775 }
1776 } else
aliguori376253e2009-03-05 23:01:23 +00001777 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001778}
1779
Luiz Capitulino83fb1de2009-10-07 13:42:01 -03001780/**
1781 * do_balloon(): Request VM to change its memory allocation
1782 */
1783static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001784{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001785 int value = qdict_get_int(qdict, "value");
Anthony Liguoric227f092009-10-01 16:12:16 -05001786 ram_addr_t target = value;
aliguoridf751fa2008-12-04 20:19:35 +00001787 qemu_balloon(target << 20);
1788}
1789
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001790static void monitor_print_balloon(Monitor *mon, const QObject *data)
1791{
1792 monitor_printf(mon, "balloon: actual=%d\n",
1793 (int)qint_get_int(qobject_to_qint(data)));
1794}
1795
1796/**
1797 * do_info_balloon(): Balloon information
1798 */
1799static void do_info_balloon(Monitor *mon, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001800{
Anthony Liguoric227f092009-10-01 16:12:16 -05001801 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00001802
1803 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001804 if (kvm_enabled() && !kvm_has_sync_mmu())
Luiz Capitulino5d6c37f2009-11-18 23:05:36 -02001805 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
aliguoribd322082008-12-04 20:33:06 +00001806 else if (actual == 0)
Luiz Capitulino5d6c37f2009-11-18 23:05:36 -02001807 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
aliguoridf751fa2008-12-04 20:19:35 +00001808 else
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001809 *ret_data = QOBJECT(qint_from_int((int)(actual >> 20)));
aliguoridf751fa2008-12-04 20:19:35 +00001810}
1811
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001812static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001813{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001814 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001815
aliguori76655d62009-03-06 20:27:37 +00001816 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001817 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001818 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001819 return acl;
1820}
aliguori76655d62009-03-06 20:27:37 +00001821
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001822static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001823{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001824 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001825 qemu_acl *acl = find_acl(mon, aclname);
1826 qemu_acl_entry *entry;
1827 int i = 0;
1828
1829 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001830 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001831 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001832 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001833 i++;
1834 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001835 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001836 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001837 }
1838}
1839
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001840static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001841{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001842 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001843 qemu_acl *acl = find_acl(mon, aclname);
1844
1845 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001846 qemu_acl_reset(acl);
1847 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001848 }
1849}
aliguori76655d62009-03-06 20:27:37 +00001850
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001851static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001852{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001853 const char *aclname = qdict_get_str(qdict, "aclname");
1854 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001855 qemu_acl *acl = find_acl(mon, aclname);
1856
1857 if (acl) {
1858 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001859 acl->defaultDeny = 0;
1860 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001861 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001862 acl->defaultDeny = 1;
1863 monitor_printf(mon, "acl: policy set to 'deny'\n");
1864 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001865 monitor_printf(mon, "acl: unknown policy '%s', "
1866 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001867 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001868 }
1869}
aliguori76655d62009-03-06 20:27:37 +00001870
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001871static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001872{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001873 const char *aclname = qdict_get_str(qdict, "aclname");
1874 const char *match = qdict_get_str(qdict, "match");
1875 const char *policy = qdict_get_str(qdict, "policy");
1876 int has_index = qdict_haskey(qdict, "index");
1877 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001878 qemu_acl *acl = find_acl(mon, aclname);
1879 int deny, ret;
1880
1881 if (acl) {
1882 if (strcmp(policy, "allow") == 0) {
1883 deny = 0;
1884 } else if (strcmp(policy, "deny") == 0) {
1885 deny = 1;
1886 } else {
1887 monitor_printf(mon, "acl: unknown policy '%s', "
1888 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001889 return;
1890 }
aliguori28a76be2009-03-06 20:27:40 +00001891 if (has_index)
1892 ret = qemu_acl_insert(acl, deny, match, index);
1893 else
1894 ret = qemu_acl_append(acl, deny, match);
1895 if (ret < 0)
1896 monitor_printf(mon, "acl: unable to add acl entry\n");
1897 else
1898 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001899 }
1900}
aliguori76655d62009-03-06 20:27:37 +00001901
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001902static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001903{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001904 const char *aclname = qdict_get_str(qdict, "aclname");
1905 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001906 qemu_acl *acl = find_acl(mon, aclname);
1907 int ret;
aliguori76655d62009-03-06 20:27:37 +00001908
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001909 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001910 ret = qemu_acl_remove(acl, match);
1911 if (ret < 0)
1912 monitor_printf(mon, "acl: no matching acl entry\n");
1913 else
1914 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001915 }
1916}
1917
Huang Ying79c4f6b2009-06-23 10:05:14 +08001918#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001919static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001920{
1921 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001922 int cpu_index = qdict_get_int(qdict, "cpu_index");
1923 int bank = qdict_get_int(qdict, "bank");
1924 uint64_t status = qdict_get_int(qdict, "status");
1925 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1926 uint64_t addr = qdict_get_int(qdict, "addr");
1927 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08001928
1929 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1930 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1931 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1932 break;
1933 }
1934}
1935#endif
1936
Luiz Capitulinof0d60002009-10-16 12:23:50 -03001937static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001938{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001939 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001940 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001941 int fd;
1942
1943 fd = qemu_chr_get_msgfd(mon->chr);
1944 if (fd == -1) {
1945 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1946 return;
1947 }
1948
1949 if (qemu_isdigit(fdname[0])) {
1950 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1951 return;
1952 }
1953
1954 fd = dup(fd);
1955 if (fd == -1) {
1956 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1957 strerror(errno));
1958 return;
1959 }
1960
Blue Swirl72cf2d42009-09-12 07:36:22 +00001961 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001962 if (strcmp(monfd->name, fdname) != 0) {
1963 continue;
1964 }
1965
1966 close(monfd->fd);
1967 monfd->fd = fd;
1968 return;
1969 }
1970
Anthony Liguoric227f092009-10-01 16:12:16 -05001971 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001972 monfd->name = qemu_strdup(fdname);
1973 monfd->fd = fd;
1974
Blue Swirl72cf2d42009-09-12 07:36:22 +00001975 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001976}
1977
Luiz Capitulino18f3a512009-10-16 12:23:51 -03001978static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001979{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001980 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001981 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001982
Blue Swirl72cf2d42009-09-12 07:36:22 +00001983 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001984 if (strcmp(monfd->name, fdname) != 0) {
1985 continue;
1986 }
1987
Blue Swirl72cf2d42009-09-12 07:36:22 +00001988 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001989 close(monfd->fd);
1990 qemu_free(monfd->name);
1991 qemu_free(monfd);
1992 return;
1993 }
1994
1995 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1996 fdname);
1997}
1998
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001999static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002000{
2001 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002002 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002003
2004 vm_stop(0);
2005
Juan Quintela05f24012009-08-20 19:42:22 +02002006 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002007 vm_start();
2008}
2009
Mark McLoughlin7768e042009-07-22 09:11:41 +01002010int monitor_get_fd(Monitor *mon, const char *fdname)
2011{
Anthony Liguoric227f092009-10-01 16:12:16 -05002012 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002013
Blue Swirl72cf2d42009-09-12 07:36:22 +00002014 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002015 int fd;
2016
2017 if (strcmp(monfd->name, fdname) != 0) {
2018 continue;
2019 }
2020
2021 fd = monfd->fd;
2022
2023 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002024 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002025 qemu_free(monfd->name);
2026 qemu_free(monfd);
2027
2028 return fd;
2029 }
2030
2031 return -1;
2032}
2033
Anthony Liguoric227f092009-10-01 16:12:16 -05002034static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002035#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002036 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002037};
2038
Blue Swirl23130862009-06-06 08:22:04 +00002039/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002040static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002041 {
2042 .name = "version",
2043 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002044 .params = "",
2045 .help = "show the version of QEMU",
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002046 .user_print = monitor_print_qobject,
2047 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002048 },
2049 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002050 .name = "commands",
2051 .args_type = "",
2052 .params = "",
2053 .help = "list QMP available commands",
2054 .user_print = monitor_user_noop,
2055 .mhandler.info_new = do_info_commands,
2056 },
2057 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002058 .name = "network",
2059 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002060 .params = "",
2061 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002062 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002063 },
2064 {
2065 .name = "chardev",
2066 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002067 .params = "",
2068 .help = "show the character devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002069 .mhandler.info = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002070 },
2071 {
2072 .name = "block",
2073 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002074 .params = "",
2075 .help = "show the block devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002076 .mhandler.info = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002077 },
2078 {
2079 .name = "blockstats",
2080 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002081 .params = "",
2082 .help = "show block device statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002083 .mhandler.info = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002084 },
2085 {
2086 .name = "registers",
2087 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002088 .params = "",
2089 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002090 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002091 },
2092 {
2093 .name = "cpus",
2094 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002095 .params = "",
2096 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002097 .user_print = monitor_print_cpus,
2098 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002099 },
2100 {
2101 .name = "history",
2102 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002103 .params = "",
2104 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002105 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002106 },
2107 {
2108 .name = "irq",
2109 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002110 .params = "",
2111 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002112 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002113 },
2114 {
2115 .name = "pic",
2116 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002117 .params = "",
2118 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002119 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002120 },
2121 {
2122 .name = "pci",
2123 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002124 .params = "",
2125 .help = "show PCI info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002126 .mhandler.info = pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002127 },
aurel327c664e22009-03-03 06:12:22 +00002128#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002129 {
2130 .name = "tlb",
2131 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002132 .params = "",
2133 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002134 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002135 },
aurel327c664e22009-03-03 06:12:22 +00002136#endif
2137#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002138 {
2139 .name = "mem",
2140 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002141 .params = "",
2142 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002143 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002144 },
2145 {
2146 .name = "hpet",
2147 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002148 .params = "",
2149 .help = "show state of HPET",
Luiz Capitulino910df892009-10-07 13:41:51 -03002150 .mhandler.info = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002151 },
bellardb86bda52004-09-18 19:32:46 +00002152#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002153 {
2154 .name = "jit",
2155 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002156 .params = "",
2157 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002158 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002159 },
2160 {
2161 .name = "kvm",
2162 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002163 .params = "",
2164 .help = "show KVM information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002165 .mhandler.info = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002166 },
2167 {
2168 .name = "numa",
2169 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002170 .params = "",
2171 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002172 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002173 },
2174 {
2175 .name = "usb",
2176 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002177 .params = "",
2178 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002179 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002180 },
2181 {
2182 .name = "usbhost",
2183 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002184 .params = "",
2185 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002186 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002187 },
2188 {
2189 .name = "profile",
2190 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002191 .params = "",
2192 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002193 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002194 },
2195 {
2196 .name = "capture",
2197 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002198 .params = "",
2199 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002200 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002201 },
2202 {
2203 .name = "snapshots",
2204 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002205 .params = "",
2206 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002207 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002208 },
2209 {
2210 .name = "status",
2211 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002212 .params = "",
2213 .help = "show the current VM status (running|paused)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002214 .mhandler.info = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002215 },
2216 {
2217 .name = "pcmcia",
2218 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002219 .params = "",
2220 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002221 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002222 },
2223 {
2224 .name = "mice",
2225 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002226 .params = "",
2227 .help = "show which guest mouse is receiving events",
Luiz Capitulino910df892009-10-07 13:41:51 -03002228 .mhandler.info = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002229 },
2230 {
2231 .name = "vnc",
2232 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002233 .params = "",
2234 .help = "show the vnc server status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002235 .mhandler.info = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002236 },
2237 {
2238 .name = "name",
2239 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002240 .params = "",
2241 .help = "show the current VM name",
Luiz Capitulino910df892009-10-07 13:41:51 -03002242 .mhandler.info = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002243 },
2244 {
2245 .name = "uuid",
2246 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002247 .params = "",
2248 .help = "show the current VM UUID",
Luiz Capitulino910df892009-10-07 13:41:51 -03002249 .mhandler.info = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002250 },
j_mayer76a66252007-03-07 08:32:30 +00002251#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002252 {
2253 .name = "cpustats",
2254 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002255 .params = "",
2256 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002257 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002258 },
j_mayer76a66252007-03-07 08:32:30 +00002259#endif
blueswir131a60e22007-10-26 18:42:59 +00002260#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002261 {
2262 .name = "usernet",
2263 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002264 .params = "",
2265 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002266 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002267 },
blueswir131a60e22007-10-26 18:42:59 +00002268#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002269 {
2270 .name = "migrate",
2271 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002272 .params = "",
2273 .help = "show migration status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002274 .mhandler.info = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002275 },
2276 {
2277 .name = "balloon",
2278 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002279 .params = "",
2280 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002281 .user_print = monitor_print_balloon,
2282 .mhandler.info_new = do_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002283 },
2284 {
2285 .name = "qtree",
2286 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002287 .params = "",
2288 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002289 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002290 },
2291 {
2292 .name = "qdm",
2293 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002294 .params = "",
2295 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002296 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002297 },
2298 {
2299 .name = "roms",
2300 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002301 .params = "",
2302 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002303 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002304 },
2305 {
2306 .name = NULL,
2307 },
bellard9dc39cb2004-03-14 21:38:27 +00002308};
2309
bellard9307c4c2004-04-04 12:57:25 +00002310/*******************************************************************/
2311
2312static const char *pch;
2313static jmp_buf expr_env;
2314
bellard92a31b12005-02-10 22:00:52 +00002315#define MD_TLONG 0
2316#define MD_I32 1
2317
bellard9307c4c2004-04-04 12:57:25 +00002318typedef struct MonitorDef {
2319 const char *name;
2320 int offset;
blueswir18662d652008-10-02 18:32:44 +00002321 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002322 int type;
bellard9307c4c2004-04-04 12:57:25 +00002323} MonitorDef;
2324
bellard57206fd2004-04-25 18:54:52 +00002325#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002326static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002327{
bellard6a00d602005-11-21 23:25:50 +00002328 CPUState *env = mon_get_cpu();
2329 if (!env)
2330 return 0;
2331 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002332}
2333#endif
2334
bellarda541f292004-04-12 20:39:29 +00002335#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002336static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002337{
bellard6a00d602005-11-21 23:25:50 +00002338 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002339 unsigned int u;
2340 int i;
2341
bellard6a00d602005-11-21 23:25:50 +00002342 if (!env)
2343 return 0;
2344
bellarda541f292004-04-12 20:39:29 +00002345 u = 0;
2346 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002347 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002348
2349 return u;
2350}
2351
blueswir18662d652008-10-02 18:32:44 +00002352static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002353{
bellard6a00d602005-11-21 23:25:50 +00002354 CPUState *env = mon_get_cpu();
2355 if (!env)
2356 return 0;
j_mayer0411a972007-10-25 21:35:50 +00002357 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002358}
2359
blueswir18662d652008-10-02 18:32:44 +00002360static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002361{
bellard6a00d602005-11-21 23:25:50 +00002362 CPUState *env = mon_get_cpu();
2363 if (!env)
2364 return 0;
aurel323d7b4172008-10-21 11:28:46 +00002365 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002366}
bellard9fddaa02004-05-21 12:59:32 +00002367
blueswir18662d652008-10-02 18:32:44 +00002368static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002369{
bellard6a00d602005-11-21 23:25:50 +00002370 CPUState *env = mon_get_cpu();
2371 if (!env)
2372 return 0;
2373 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002374}
2375
blueswir18662d652008-10-02 18:32:44 +00002376static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002377{
bellard6a00d602005-11-21 23:25:50 +00002378 CPUState *env = mon_get_cpu();
2379 if (!env)
2380 return 0;
2381 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002382}
2383
blueswir18662d652008-10-02 18:32:44 +00002384static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002385{
bellard6a00d602005-11-21 23:25:50 +00002386 CPUState *env = mon_get_cpu();
2387 if (!env)
2388 return 0;
2389 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002390}
bellarda541f292004-04-12 20:39:29 +00002391#endif
2392
bellarde95c8d52004-09-30 22:22:08 +00002393#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002394#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002395static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002396{
bellard6a00d602005-11-21 23:25:50 +00002397 CPUState *env = mon_get_cpu();
2398 if (!env)
2399 return 0;
2400 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002401}
bellard7b936c02005-10-30 17:05:13 +00002402#endif
bellarde95c8d52004-09-30 22:22:08 +00002403
blueswir18662d652008-10-02 18:32:44 +00002404static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002405{
bellard6a00d602005-11-21 23:25:50 +00002406 CPUState *env = mon_get_cpu();
2407 if (!env)
2408 return 0;
2409 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002410}
2411#endif
2412
blueswir18662d652008-10-02 18:32:44 +00002413static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002414#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002415
2416#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002417 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002418 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002419 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002420
bellard9307c4c2004-04-04 12:57:25 +00002421 { "eax", offsetof(CPUState, regs[0]) },
2422 { "ecx", offsetof(CPUState, regs[1]) },
2423 { "edx", offsetof(CPUState, regs[2]) },
2424 { "ebx", offsetof(CPUState, regs[3]) },
2425 { "esp|sp", offsetof(CPUState, regs[4]) },
2426 { "ebp|fp", offsetof(CPUState, regs[5]) },
2427 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002428 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002429#ifdef TARGET_X86_64
2430 { "r8", offsetof(CPUState, regs[8]) },
2431 { "r9", offsetof(CPUState, regs[9]) },
2432 { "r10", offsetof(CPUState, regs[10]) },
2433 { "r11", offsetof(CPUState, regs[11]) },
2434 { "r12", offsetof(CPUState, regs[12]) },
2435 { "r13", offsetof(CPUState, regs[13]) },
2436 { "r14", offsetof(CPUState, regs[14]) },
2437 { "r15", offsetof(CPUState, regs[15]) },
2438#endif
bellard9307c4c2004-04-04 12:57:25 +00002439 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002440 { "eip", offsetof(CPUState, eip) },
2441 SEG("cs", R_CS)
2442 SEG("ds", R_DS)
2443 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002444 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002445 SEG("fs", R_FS)
2446 SEG("gs", R_GS)
2447 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002448#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002449 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002450 { "r0", offsetof(CPUState, gpr[0]) },
2451 { "r1", offsetof(CPUState, gpr[1]) },
2452 { "r2", offsetof(CPUState, gpr[2]) },
2453 { "r3", offsetof(CPUState, gpr[3]) },
2454 { "r4", offsetof(CPUState, gpr[4]) },
2455 { "r5", offsetof(CPUState, gpr[5]) },
2456 { "r6", offsetof(CPUState, gpr[6]) },
2457 { "r7", offsetof(CPUState, gpr[7]) },
2458 { "r8", offsetof(CPUState, gpr[8]) },
2459 { "r9", offsetof(CPUState, gpr[9]) },
2460 { "r10", offsetof(CPUState, gpr[10]) },
2461 { "r11", offsetof(CPUState, gpr[11]) },
2462 { "r12", offsetof(CPUState, gpr[12]) },
2463 { "r13", offsetof(CPUState, gpr[13]) },
2464 { "r14", offsetof(CPUState, gpr[14]) },
2465 { "r15", offsetof(CPUState, gpr[15]) },
2466 { "r16", offsetof(CPUState, gpr[16]) },
2467 { "r17", offsetof(CPUState, gpr[17]) },
2468 { "r18", offsetof(CPUState, gpr[18]) },
2469 { "r19", offsetof(CPUState, gpr[19]) },
2470 { "r20", offsetof(CPUState, gpr[20]) },
2471 { "r21", offsetof(CPUState, gpr[21]) },
2472 { "r22", offsetof(CPUState, gpr[22]) },
2473 { "r23", offsetof(CPUState, gpr[23]) },
2474 { "r24", offsetof(CPUState, gpr[24]) },
2475 { "r25", offsetof(CPUState, gpr[25]) },
2476 { "r26", offsetof(CPUState, gpr[26]) },
2477 { "r27", offsetof(CPUState, gpr[27]) },
2478 { "r28", offsetof(CPUState, gpr[28]) },
2479 { "r29", offsetof(CPUState, gpr[29]) },
2480 { "r30", offsetof(CPUState, gpr[30]) },
2481 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002482 /* Floating point registers */
2483 { "f0", offsetof(CPUState, fpr[0]) },
2484 { "f1", offsetof(CPUState, fpr[1]) },
2485 { "f2", offsetof(CPUState, fpr[2]) },
2486 { "f3", offsetof(CPUState, fpr[3]) },
2487 { "f4", offsetof(CPUState, fpr[4]) },
2488 { "f5", offsetof(CPUState, fpr[5]) },
2489 { "f6", offsetof(CPUState, fpr[6]) },
2490 { "f7", offsetof(CPUState, fpr[7]) },
2491 { "f8", offsetof(CPUState, fpr[8]) },
2492 { "f9", offsetof(CPUState, fpr[9]) },
2493 { "f10", offsetof(CPUState, fpr[10]) },
2494 { "f11", offsetof(CPUState, fpr[11]) },
2495 { "f12", offsetof(CPUState, fpr[12]) },
2496 { "f13", offsetof(CPUState, fpr[13]) },
2497 { "f14", offsetof(CPUState, fpr[14]) },
2498 { "f15", offsetof(CPUState, fpr[15]) },
2499 { "f16", offsetof(CPUState, fpr[16]) },
2500 { "f17", offsetof(CPUState, fpr[17]) },
2501 { "f18", offsetof(CPUState, fpr[18]) },
2502 { "f19", offsetof(CPUState, fpr[19]) },
2503 { "f20", offsetof(CPUState, fpr[20]) },
2504 { "f21", offsetof(CPUState, fpr[21]) },
2505 { "f22", offsetof(CPUState, fpr[22]) },
2506 { "f23", offsetof(CPUState, fpr[23]) },
2507 { "f24", offsetof(CPUState, fpr[24]) },
2508 { "f25", offsetof(CPUState, fpr[25]) },
2509 { "f26", offsetof(CPUState, fpr[26]) },
2510 { "f27", offsetof(CPUState, fpr[27]) },
2511 { "f28", offsetof(CPUState, fpr[28]) },
2512 { "f29", offsetof(CPUState, fpr[29]) },
2513 { "f30", offsetof(CPUState, fpr[30]) },
2514 { "f31", offsetof(CPUState, fpr[31]) },
2515 { "fpscr", offsetof(CPUState, fpscr) },
2516 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002517 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002518 { "lr", offsetof(CPUState, lr) },
2519 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002520 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002521 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002522 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002523 { "msr", 0, &monitor_get_msr, },
2524 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002525 { "tbu", 0, &monitor_get_tbu, },
2526 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002527#if defined(TARGET_PPC64)
2528 /* Address space register */
2529 { "asr", offsetof(CPUState, asr) },
2530#endif
2531 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002532 { "sdr1", offsetof(CPUState, sdr1) },
2533 { "sr0", offsetof(CPUState, sr[0]) },
2534 { "sr1", offsetof(CPUState, sr[1]) },
2535 { "sr2", offsetof(CPUState, sr[2]) },
2536 { "sr3", offsetof(CPUState, sr[3]) },
2537 { "sr4", offsetof(CPUState, sr[4]) },
2538 { "sr5", offsetof(CPUState, sr[5]) },
2539 { "sr6", offsetof(CPUState, sr[6]) },
2540 { "sr7", offsetof(CPUState, sr[7]) },
2541 { "sr8", offsetof(CPUState, sr[8]) },
2542 { "sr9", offsetof(CPUState, sr[9]) },
2543 { "sr10", offsetof(CPUState, sr[10]) },
2544 { "sr11", offsetof(CPUState, sr[11]) },
2545 { "sr12", offsetof(CPUState, sr[12]) },
2546 { "sr13", offsetof(CPUState, sr[13]) },
2547 { "sr14", offsetof(CPUState, sr[14]) },
2548 { "sr15", offsetof(CPUState, sr[15]) },
2549 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002550#elif defined(TARGET_SPARC)
2551 { "g0", offsetof(CPUState, gregs[0]) },
2552 { "g1", offsetof(CPUState, gregs[1]) },
2553 { "g2", offsetof(CPUState, gregs[2]) },
2554 { "g3", offsetof(CPUState, gregs[3]) },
2555 { "g4", offsetof(CPUState, gregs[4]) },
2556 { "g5", offsetof(CPUState, gregs[5]) },
2557 { "g6", offsetof(CPUState, gregs[6]) },
2558 { "g7", offsetof(CPUState, gregs[7]) },
2559 { "o0", 0, monitor_get_reg },
2560 { "o1", 1, monitor_get_reg },
2561 { "o2", 2, monitor_get_reg },
2562 { "o3", 3, monitor_get_reg },
2563 { "o4", 4, monitor_get_reg },
2564 { "o5", 5, monitor_get_reg },
2565 { "o6", 6, monitor_get_reg },
2566 { "o7", 7, monitor_get_reg },
2567 { "l0", 8, monitor_get_reg },
2568 { "l1", 9, monitor_get_reg },
2569 { "l2", 10, monitor_get_reg },
2570 { "l3", 11, monitor_get_reg },
2571 { "l4", 12, monitor_get_reg },
2572 { "l5", 13, monitor_get_reg },
2573 { "l6", 14, monitor_get_reg },
2574 { "l7", 15, monitor_get_reg },
2575 { "i0", 16, monitor_get_reg },
2576 { "i1", 17, monitor_get_reg },
2577 { "i2", 18, monitor_get_reg },
2578 { "i3", 19, monitor_get_reg },
2579 { "i4", 20, monitor_get_reg },
2580 { "i5", 21, monitor_get_reg },
2581 { "i6", 22, monitor_get_reg },
2582 { "i7", 23, monitor_get_reg },
2583 { "pc", offsetof(CPUState, pc) },
2584 { "npc", offsetof(CPUState, npc) },
2585 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002586#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002587 { "psr", 0, &monitor_get_psr, },
2588 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002589#endif
bellarde95c8d52004-09-30 22:22:08 +00002590 { "tbr", offsetof(CPUState, tbr) },
2591 { "fsr", offsetof(CPUState, fsr) },
2592 { "f0", offsetof(CPUState, fpr[0]) },
2593 { "f1", offsetof(CPUState, fpr[1]) },
2594 { "f2", offsetof(CPUState, fpr[2]) },
2595 { "f3", offsetof(CPUState, fpr[3]) },
2596 { "f4", offsetof(CPUState, fpr[4]) },
2597 { "f5", offsetof(CPUState, fpr[5]) },
2598 { "f6", offsetof(CPUState, fpr[6]) },
2599 { "f7", offsetof(CPUState, fpr[7]) },
2600 { "f8", offsetof(CPUState, fpr[8]) },
2601 { "f9", offsetof(CPUState, fpr[9]) },
2602 { "f10", offsetof(CPUState, fpr[10]) },
2603 { "f11", offsetof(CPUState, fpr[11]) },
2604 { "f12", offsetof(CPUState, fpr[12]) },
2605 { "f13", offsetof(CPUState, fpr[13]) },
2606 { "f14", offsetof(CPUState, fpr[14]) },
2607 { "f15", offsetof(CPUState, fpr[15]) },
2608 { "f16", offsetof(CPUState, fpr[16]) },
2609 { "f17", offsetof(CPUState, fpr[17]) },
2610 { "f18", offsetof(CPUState, fpr[18]) },
2611 { "f19", offsetof(CPUState, fpr[19]) },
2612 { "f20", offsetof(CPUState, fpr[20]) },
2613 { "f21", offsetof(CPUState, fpr[21]) },
2614 { "f22", offsetof(CPUState, fpr[22]) },
2615 { "f23", offsetof(CPUState, fpr[23]) },
2616 { "f24", offsetof(CPUState, fpr[24]) },
2617 { "f25", offsetof(CPUState, fpr[25]) },
2618 { "f26", offsetof(CPUState, fpr[26]) },
2619 { "f27", offsetof(CPUState, fpr[27]) },
2620 { "f28", offsetof(CPUState, fpr[28]) },
2621 { "f29", offsetof(CPUState, fpr[29]) },
2622 { "f30", offsetof(CPUState, fpr[30]) },
2623 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002624#ifdef TARGET_SPARC64
2625 { "f32", offsetof(CPUState, fpr[32]) },
2626 { "f34", offsetof(CPUState, fpr[34]) },
2627 { "f36", offsetof(CPUState, fpr[36]) },
2628 { "f38", offsetof(CPUState, fpr[38]) },
2629 { "f40", offsetof(CPUState, fpr[40]) },
2630 { "f42", offsetof(CPUState, fpr[42]) },
2631 { "f44", offsetof(CPUState, fpr[44]) },
2632 { "f46", offsetof(CPUState, fpr[46]) },
2633 { "f48", offsetof(CPUState, fpr[48]) },
2634 { "f50", offsetof(CPUState, fpr[50]) },
2635 { "f52", offsetof(CPUState, fpr[52]) },
2636 { "f54", offsetof(CPUState, fpr[54]) },
2637 { "f56", offsetof(CPUState, fpr[56]) },
2638 { "f58", offsetof(CPUState, fpr[58]) },
2639 { "f60", offsetof(CPUState, fpr[60]) },
2640 { "f62", offsetof(CPUState, fpr[62]) },
2641 { "asi", offsetof(CPUState, asi) },
2642 { "pstate", offsetof(CPUState, pstate) },
2643 { "cansave", offsetof(CPUState, cansave) },
2644 { "canrestore", offsetof(CPUState, canrestore) },
2645 { "otherwin", offsetof(CPUState, otherwin) },
2646 { "wstate", offsetof(CPUState, wstate) },
2647 { "cleanwin", offsetof(CPUState, cleanwin) },
2648 { "fprs", offsetof(CPUState, fprs) },
2649#endif
bellard9307c4c2004-04-04 12:57:25 +00002650#endif
2651 { NULL },
2652};
2653
aliguori376253e2009-03-05 23:01:23 +00002654static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002655{
aliguori376253e2009-03-05 23:01:23 +00002656 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002657 longjmp(expr_env, 1);
2658}
2659
bellard6a00d602005-11-21 23:25:50 +00002660/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002661static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002662{
blueswir18662d652008-10-02 18:32:44 +00002663 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002664 void *ptr;
2665
bellard9307c4c2004-04-04 12:57:25 +00002666 for(md = monitor_defs; md->name != NULL; md++) {
2667 if (compare_cmd(name, md->name)) {
2668 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002669 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002670 } else {
bellard6a00d602005-11-21 23:25:50 +00002671 CPUState *env = mon_get_cpu();
2672 if (!env)
2673 return -2;
2674 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002675 switch(md->type) {
2676 case MD_I32:
2677 *pval = *(int32_t *)ptr;
2678 break;
2679 case MD_TLONG:
2680 *pval = *(target_long *)ptr;
2681 break;
2682 default:
2683 *pval = 0;
2684 break;
2685 }
bellard9307c4c2004-04-04 12:57:25 +00002686 }
2687 return 0;
2688 }
2689 }
2690 return -1;
2691}
2692
2693static void next(void)
2694{
Blue Swirl660f11b2009-07-31 21:16:51 +00002695 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002696 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002697 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002698 pch++;
2699 }
2700}
2701
aliguori376253e2009-03-05 23:01:23 +00002702static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002703
aliguori376253e2009-03-05 23:01:23 +00002704static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002705{
blueswir1c2efc952007-09-25 17:28:42 +00002706 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002707 char *p;
bellard6a00d602005-11-21 23:25:50 +00002708 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002709
2710 switch(*pch) {
2711 case '+':
2712 next();
aliguori376253e2009-03-05 23:01:23 +00002713 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002714 break;
2715 case '-':
2716 next();
aliguori376253e2009-03-05 23:01:23 +00002717 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002718 break;
2719 case '~':
2720 next();
aliguori376253e2009-03-05 23:01:23 +00002721 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002722 break;
2723 case '(':
2724 next();
aliguori376253e2009-03-05 23:01:23 +00002725 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002726 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002727 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002728 }
2729 next();
2730 break;
bellard81d09122004-07-14 17:21:37 +00002731 case '\'':
2732 pch++;
2733 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002734 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002735 n = *pch;
2736 pch++;
2737 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002738 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002739 next();
2740 break;
bellard9307c4c2004-04-04 12:57:25 +00002741 case '$':
2742 {
2743 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002744 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002745
bellard9307c4c2004-04-04 12:57:25 +00002746 pch++;
2747 q = buf;
2748 while ((*pch >= 'a' && *pch <= 'z') ||
2749 (*pch >= 'A' && *pch <= 'Z') ||
2750 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002751 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002752 if ((q - buf) < sizeof(buf) - 1)
2753 *q++ = *pch;
2754 pch++;
2755 }
blueswir1cd390082008-11-16 13:53:32 +00002756 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002757 pch++;
2758 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002759 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002760 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002761 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002762 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002763 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002764 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002765 }
2766 break;
2767 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002768 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002769 n = 0;
2770 break;
2771 default:
blueswir17743e582007-09-24 18:39:04 +00002772#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002773 n = strtoull(pch, &p, 0);
2774#else
bellard9307c4c2004-04-04 12:57:25 +00002775 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002776#endif
bellard9307c4c2004-04-04 12:57:25 +00002777 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002778 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002779 }
2780 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002781 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002782 pch++;
2783 break;
2784 }
2785 return n;
2786}
2787
2788
aliguori376253e2009-03-05 23:01:23 +00002789static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002790{
blueswir1c2efc952007-09-25 17:28:42 +00002791 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002792 int op;
ths3b46e622007-09-17 08:09:54 +00002793
aliguori376253e2009-03-05 23:01:23 +00002794 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002795 for(;;) {
2796 op = *pch;
2797 if (op != '*' && op != '/' && op != '%')
2798 break;
2799 next();
aliguori376253e2009-03-05 23:01:23 +00002800 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002801 switch(op) {
2802 default:
2803 case '*':
2804 val *= val2;
2805 break;
2806 case '/':
2807 case '%':
ths5fafdf22007-09-16 21:08:06 +00002808 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002809 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002810 if (op == '/')
2811 val /= val2;
2812 else
2813 val %= val2;
2814 break;
2815 }
2816 }
2817 return val;
2818}
2819
aliguori376253e2009-03-05 23:01:23 +00002820static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002821{
blueswir1c2efc952007-09-25 17:28:42 +00002822 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002823 int op;
bellard9307c4c2004-04-04 12:57:25 +00002824
aliguori376253e2009-03-05 23:01:23 +00002825 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002826 for(;;) {
2827 op = *pch;
2828 if (op != '&' && op != '|' && op != '^')
2829 break;
2830 next();
aliguori376253e2009-03-05 23:01:23 +00002831 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002832 switch(op) {
2833 default:
2834 case '&':
2835 val &= val2;
2836 break;
2837 case '|':
2838 val |= val2;
2839 break;
2840 case '^':
2841 val ^= val2;
2842 break;
2843 }
2844 }
2845 return val;
2846}
2847
aliguori376253e2009-03-05 23:01:23 +00002848static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002849{
blueswir1c2efc952007-09-25 17:28:42 +00002850 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002851 int op;
bellard9307c4c2004-04-04 12:57:25 +00002852
aliguori376253e2009-03-05 23:01:23 +00002853 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002854 for(;;) {
2855 op = *pch;
2856 if (op != '+' && op != '-')
2857 break;
2858 next();
aliguori376253e2009-03-05 23:01:23 +00002859 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002860 if (op == '+')
2861 val += val2;
2862 else
2863 val -= val2;
2864 }
2865 return val;
2866}
2867
aliguori376253e2009-03-05 23:01:23 +00002868static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002869{
2870 pch = *pp;
2871 if (setjmp(expr_env)) {
2872 *pp = pch;
2873 return -1;
2874 }
blueswir1cd390082008-11-16 13:53:32 +00002875 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002876 pch++;
aliguori376253e2009-03-05 23:01:23 +00002877 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002878 *pp = pch;
2879 return 0;
2880}
2881
2882static int get_str(char *buf, int buf_size, const char **pp)
2883{
2884 const char *p;
2885 char *q;
2886 int c;
2887
bellard81d09122004-07-14 17:21:37 +00002888 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002889 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002890 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002891 p++;
2892 if (*p == '\0') {
2893 fail:
bellard81d09122004-07-14 17:21:37 +00002894 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002895 *pp = p;
2896 return -1;
2897 }
bellard9307c4c2004-04-04 12:57:25 +00002898 if (*p == '\"') {
2899 p++;
2900 while (*p != '\0' && *p != '\"') {
2901 if (*p == '\\') {
2902 p++;
2903 c = *p++;
2904 switch(c) {
2905 case 'n':
2906 c = '\n';
2907 break;
2908 case 'r':
2909 c = '\r';
2910 break;
2911 case '\\':
2912 case '\'':
2913 case '\"':
2914 break;
2915 default:
2916 qemu_printf("unsupported escape code: '\\%c'\n", c);
2917 goto fail;
2918 }
2919 if ((q - buf) < buf_size - 1) {
2920 *q++ = c;
2921 }
2922 } else {
2923 if ((q - buf) < buf_size - 1) {
2924 *q++ = *p;
2925 }
2926 p++;
2927 }
2928 }
2929 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002930 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002931 goto fail;
2932 }
2933 p++;
2934 } else {
blueswir1cd390082008-11-16 13:53:32 +00002935 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002936 if ((q - buf) < buf_size - 1) {
2937 *q++ = *p;
2938 }
2939 p++;
2940 }
bellard9307c4c2004-04-04 12:57:25 +00002941 }
bellard81d09122004-07-14 17:21:37 +00002942 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002943 *pp = p;
2944 return 0;
2945}
2946
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002947/*
2948 * Store the command-name in cmdname, and return a pointer to
2949 * the remaining of the command string.
2950 */
2951static const char *get_command_name(const char *cmdline,
2952 char *cmdname, size_t nlen)
2953{
2954 size_t len;
2955 const char *p, *pstart;
2956
2957 p = cmdline;
2958 while (qemu_isspace(*p))
2959 p++;
2960 if (*p == '\0')
2961 return NULL;
2962 pstart = p;
2963 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2964 p++;
2965 len = p - pstart;
2966 if (len > nlen - 1)
2967 len = nlen - 1;
2968 memcpy(cmdname, pstart, len);
2969 cmdname[len] = '\0';
2970 return p;
2971}
2972
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002973/**
2974 * Read key of 'type' into 'key' and return the current
2975 * 'type' pointer.
2976 */
2977static char *key_get_info(const char *type, char **key)
2978{
2979 size_t len;
2980 char *p, *str;
2981
2982 if (*type == ',')
2983 type++;
2984
2985 p = strchr(type, ':');
2986 if (!p) {
2987 *key = NULL;
2988 return NULL;
2989 }
2990 len = p - type;
2991
2992 str = qemu_malloc(len + 1);
2993 memcpy(str, type, len);
2994 str[len] = '\0';
2995
2996 *key = str;
2997 return ++p;
2998}
2999
bellard9307c4c2004-04-04 12:57:25 +00003000static int default_fmt_format = 'x';
3001static int default_fmt_size = 4;
3002
3003#define MAX_ARGS 16
3004
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003005static int is_valid_option(const char *c, const char *typestr)
3006{
3007 char option[3];
3008
3009 option[0] = '-';
3010 option[1] = *c;
3011 option[2] = '\0';
3012
3013 typestr = strstr(typestr, option);
3014 return (typestr != NULL);
3015}
3016
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003017static const mon_cmd_t *monitor_find_command(const char *cmdname)
3018{
3019 const mon_cmd_t *cmd;
3020
3021 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3022 if (compare_cmd(cmdname, cmd->name)) {
3023 return cmd;
3024 }
3025 }
3026
3027 return NULL;
3028}
3029
Anthony Liguoric227f092009-10-01 16:12:16 -05003030static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003031 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003032 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003033{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003034 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003035 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003036 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003037 char cmdname[256];
3038 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003039 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003040
3041#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003042 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003043#endif
ths3b46e622007-09-17 08:09:54 +00003044
bellard9307c4c2004-04-04 12:57:25 +00003045 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003046 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3047 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003048 return NULL;
ths3b46e622007-09-17 08:09:54 +00003049
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003050 cmd = monitor_find_command(cmdname);
3051 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003052 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003053 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003054 }
bellard9307c4c2004-04-04 12:57:25 +00003055
bellard9307c4c2004-04-04 12:57:25 +00003056 /* parse the parameters */
3057 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003058 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003059 typestr = key_get_info(typestr, &key);
3060 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003061 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003062 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003063 typestr++;
3064 switch(c) {
3065 case 'F':
bellard81d09122004-07-14 17:21:37 +00003066 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003067 case 's':
3068 {
3069 int ret;
ths3b46e622007-09-17 08:09:54 +00003070
blueswir1cd390082008-11-16 13:53:32 +00003071 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003072 p++;
3073 if (*typestr == '?') {
3074 typestr++;
3075 if (*p == '\0') {
3076 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003077 break;
bellard9307c4c2004-04-04 12:57:25 +00003078 }
3079 }
3080 ret = get_str(buf, sizeof(buf), &p);
3081 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003082 switch(c) {
3083 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003084 monitor_printf(mon, "%s: filename expected\n",
3085 cmdname);
bellard81d09122004-07-14 17:21:37 +00003086 break;
3087 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003088 monitor_printf(mon, "%s: block device name expected\n",
3089 cmdname);
bellard81d09122004-07-14 17:21:37 +00003090 break;
3091 default:
aliguori376253e2009-03-05 23:01:23 +00003092 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003093 break;
3094 }
bellard9307c4c2004-04-04 12:57:25 +00003095 goto fail;
3096 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003097 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003098 }
3099 break;
3100 case '/':
3101 {
3102 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003103
blueswir1cd390082008-11-16 13:53:32 +00003104 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003105 p++;
3106 if (*p == '/') {
3107 /* format found */
3108 p++;
3109 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003110 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003111 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003112 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003113 count = count * 10 + (*p - '0');
3114 p++;
3115 }
3116 }
3117 size = -1;
3118 format = -1;
3119 for(;;) {
3120 switch(*p) {
3121 case 'o':
3122 case 'd':
3123 case 'u':
3124 case 'x':
3125 case 'i':
3126 case 'c':
3127 format = *p++;
3128 break;
3129 case 'b':
3130 size = 1;
3131 p++;
3132 break;
3133 case 'h':
3134 size = 2;
3135 p++;
3136 break;
3137 case 'w':
3138 size = 4;
3139 p++;
3140 break;
3141 case 'g':
3142 case 'L':
3143 size = 8;
3144 p++;
3145 break;
3146 default:
3147 goto next;
3148 }
3149 }
3150 next:
blueswir1cd390082008-11-16 13:53:32 +00003151 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003152 monitor_printf(mon, "invalid char in format: '%c'\n",
3153 *p);
bellard9307c4c2004-04-04 12:57:25 +00003154 goto fail;
3155 }
bellard9307c4c2004-04-04 12:57:25 +00003156 if (format < 0)
3157 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003158 if (format != 'i') {
3159 /* for 'i', not specifying a size gives -1 as size */
3160 if (size < 0)
3161 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003162 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003163 }
bellard9307c4c2004-04-04 12:57:25 +00003164 default_fmt_format = format;
3165 } else {
3166 count = 1;
3167 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003168 if (format != 'i') {
3169 size = default_fmt_size;
3170 } else {
3171 size = -1;
3172 }
bellard9307c4c2004-04-04 12:57:25 +00003173 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003174 qdict_put(qdict, "count", qint_from_int(count));
3175 qdict_put(qdict, "format", qint_from_int(format));
3176 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003177 }
3178 break;
3179 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003180 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00003181 {
blueswir1c2efc952007-09-25 17:28:42 +00003182 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003183
blueswir1cd390082008-11-16 13:53:32 +00003184 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003185 p++;
bellard34405572004-06-08 00:55:58 +00003186 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003187 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003188 if (*p == '\0') {
3189 typestr++;
3190 break;
3191 }
bellard34405572004-06-08 00:55:58 +00003192 } else {
3193 if (*p == '.') {
3194 p++;
blueswir1cd390082008-11-16 13:53:32 +00003195 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003196 p++;
bellard34405572004-06-08 00:55:58 +00003197 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003198 typestr++;
3199 break;
bellard34405572004-06-08 00:55:58 +00003200 }
3201 }
bellard13224a82006-07-14 22:03:35 +00003202 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003203 }
aliguori376253e2009-03-05 23:01:23 +00003204 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003205 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003206 /* Check if 'i' is greater than 32-bit */
3207 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3208 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3209 monitor_printf(mon, "integer is for 32-bit values\n");
3210 goto fail;
3211 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003212 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003213 }
3214 break;
3215 case '-':
3216 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003217 const char *tmp = p;
3218 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003219 /* option */
ths3b46e622007-09-17 08:09:54 +00003220
bellard9307c4c2004-04-04 12:57:25 +00003221 c = *typestr++;
3222 if (c == '\0')
3223 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003224 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003225 p++;
3226 has_option = 0;
3227 if (*p == '-') {
3228 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003229 if(c != *p) {
3230 if(!is_valid_option(p, typestr)) {
3231
3232 monitor_printf(mon, "%s: unsupported option -%c\n",
3233 cmdname, *p);
3234 goto fail;
3235 } else {
3236 skip_key = 1;
3237 }
bellard9307c4c2004-04-04 12:57:25 +00003238 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003239 if(skip_key) {
3240 p = tmp;
3241 } else {
3242 p++;
3243 has_option = 1;
3244 }
bellard9307c4c2004-04-04 12:57:25 +00003245 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003246 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003247 }
3248 break;
3249 default:
3250 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003251 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003252 goto fail;
3253 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003254 qemu_free(key);
3255 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003256 }
3257 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003258 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003259 p++;
3260 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003261 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3262 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003263 goto fail;
3264 }
3265
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003266 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003267
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003268fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003269 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003270 return NULL;
3271}
3272
Luiz Capitulino8204a912009-11-18 23:05:31 -02003273static void monitor_print_error(Monitor *mon)
3274{
3275 qerror_print(mon->error);
3276 QDECREF(mon->error);
3277 mon->error = NULL;
3278}
3279
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003280static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3281 const QDict *params)
3282{
3283 QObject *data = NULL;
3284
3285 cmd->mhandler.cmd_new(mon, params, &data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003286
3287 if (monitor_ctrl_mode(mon)) {
3288 /* Monitor Protocol */
3289 monitor_protocol_emitter(mon, data);
3290 } else {
3291 /* User Protocol */
3292 if (data)
3293 cmd->user_print(mon, data);
3294 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003295
3296 qobject_decref(data);
3297}
3298
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003299static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003300{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003301 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003302 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003303
3304 qdict = qdict_new();
3305
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003306 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003307 if (!cmd)
3308 goto out;
3309
3310 qemu_errors_to_mon(mon);
3311
3312 if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003313 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003314 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003315 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003316 }
3317
Luiz Capitulino8204a912009-11-18 23:05:31 -02003318 if (monitor_has_error(mon))
3319 monitor_print_error(mon);
3320
3321 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003322
3323out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003324 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003325}
3326
bellard81d09122004-07-14 17:21:37 +00003327static void cmd_completion(const char *name, const char *list)
3328{
3329 const char *p, *pstart;
3330 char cmd[128];
3331 int len;
3332
3333 p = list;
3334 for(;;) {
3335 pstart = p;
3336 p = strchr(p, '|');
3337 if (!p)
3338 p = pstart + strlen(pstart);
3339 len = p - pstart;
3340 if (len > sizeof(cmd) - 2)
3341 len = sizeof(cmd) - 2;
3342 memcpy(cmd, pstart, len);
3343 cmd[len] = '\0';
3344 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003345 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003346 }
3347 if (*p == '\0')
3348 break;
3349 p++;
3350 }
3351}
3352
3353static void file_completion(const char *input)
3354{
3355 DIR *ffs;
3356 struct dirent *d;
3357 char path[1024];
3358 char file[1024], file_prefix[1024];
3359 int input_path_len;
3360 const char *p;
3361
ths5fafdf22007-09-16 21:08:06 +00003362 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003363 if (!p) {
3364 input_path_len = 0;
3365 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003366 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003367 } else {
3368 input_path_len = p - input + 1;
3369 memcpy(path, input, input_path_len);
3370 if (input_path_len > sizeof(path) - 1)
3371 input_path_len = sizeof(path) - 1;
3372 path[input_path_len] = '\0';
3373 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3374 }
3375#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003376 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3377 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003378#endif
3379 ffs = opendir(path);
3380 if (!ffs)
3381 return;
3382 for(;;) {
3383 struct stat sb;
3384 d = readdir(ffs);
3385 if (!d)
3386 break;
3387 if (strstart(d->d_name, file_prefix, NULL)) {
3388 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003389 if (input_path_len < sizeof(file))
3390 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3391 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003392 /* stat the file to find out if it's a directory.
3393 * In that case add a slash to speed up typing long paths
3394 */
3395 stat(file, &sb);
3396 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003397 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003398 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003399 }
3400 }
3401 closedir(ffs);
3402}
3403
aliguori51de9762009-03-05 23:00:43 +00003404static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003405{
aliguori51de9762009-03-05 23:00:43 +00003406 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003407 const char *input = opaque;
3408
3409 if (input[0] == '\0' ||
3410 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003411 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003412 }
3413}
3414
3415/* NOTE: this parser is an approximate form of the real command parser */
3416static void parse_cmdline(const char *cmdline,
3417 int *pnb_args, char **args)
3418{
3419 const char *p;
3420 int nb_args, ret;
3421 char buf[1024];
3422
3423 p = cmdline;
3424 nb_args = 0;
3425 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003426 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003427 p++;
3428 if (*p == '\0')
3429 break;
3430 if (nb_args >= MAX_ARGS)
3431 break;
3432 ret = get_str(buf, sizeof(buf), &p);
3433 args[nb_args] = qemu_strdup(buf);
3434 nb_args++;
3435 if (ret < 0)
3436 break;
3437 }
3438 *pnb_args = nb_args;
3439}
3440
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003441static const char *next_arg_type(const char *typestr)
3442{
3443 const char *p = strchr(typestr, ':');
3444 return (p != NULL ? ++p : typestr);
3445}
3446
aliguori4c36ba32009-03-05 23:01:37 +00003447static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003448{
3449 const char *cmdname;
3450 char *args[MAX_ARGS];
3451 int nb_args, i, len;
3452 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003453 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003454 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003455
3456 parse_cmdline(cmdline, &nb_args, args);
3457#ifdef DEBUG_COMPLETION
3458 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003459 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003460 }
3461#endif
3462
3463 /* if the line ends with a space, it means we want to complete the
3464 next arg */
3465 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003466 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003467 if (nb_args >= MAX_ARGS)
3468 return;
3469 args[nb_args++] = qemu_strdup("");
3470 }
3471 if (nb_args <= 1) {
3472 /* command completion */
3473 if (nb_args == 0)
3474 cmdname = "";
3475 else
3476 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003477 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003478 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003479 cmd_completion(cmdname, cmd->name);
3480 }
3481 } else {
3482 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003483 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003484 if (compare_cmd(args[0], cmd->name))
3485 goto found;
3486 }
3487 return;
3488 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003489 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003490 for(i = 0; i < nb_args - 2; i++) {
3491 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003492 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003493 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003494 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003495 }
3496 }
3497 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003498 if (*ptype == '-' && ptype[1] != '\0') {
3499 ptype += 2;
3500 }
bellard81d09122004-07-14 17:21:37 +00003501 switch(*ptype) {
3502 case 'F':
3503 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003504 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003505 file_completion(str);
3506 break;
3507 case 'B':
3508 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003509 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003510 bdrv_iterate(block_completion_it, (void *)str);
3511 break;
bellard7fe48482004-10-09 18:08:01 +00003512 case 's':
3513 /* XXX: more generic ? */
3514 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003515 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003516 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3517 cmd_completion(str, cmd->name);
3518 }
bellard64866c32006-05-07 18:03:31 +00003519 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003520 char *sep = strrchr(str, '-');
3521 if (sep)
3522 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003523 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003524 for(key = key_defs; key->name != NULL; key++) {
3525 cmd_completion(str, key->name);
3526 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003527 } else if (!strcmp(cmd->name, "help|?")) {
3528 readline_set_completion_index(cur_mon->rs, strlen(str));
3529 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3530 cmd_completion(str, cmd->name);
3531 }
bellard7fe48482004-10-09 18:08:01 +00003532 }
3533 break;
bellard81d09122004-07-14 17:21:37 +00003534 default:
3535 break;
3536 }
3537 }
3538 for(i = 0; i < nb_args; i++)
3539 qemu_free(args[i]);
3540}
3541
aliguori731b0362009-03-05 23:01:42 +00003542static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003543{
aliguori731b0362009-03-05 23:01:42 +00003544 Monitor *mon = opaque;
3545
3546 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003547}
3548
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003549/**
3550 * monitor_control_read(): Read and handle QMP input
3551 */
3552static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
3553{
3554 Monitor *old_mon = cur_mon;
3555
3556 cur_mon = opaque;
3557
3558 // TODO: read QMP input
3559
3560 cur_mon = old_mon;
3561}
3562
aliguori731b0362009-03-05 23:01:42 +00003563static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003564{
aliguori731b0362009-03-05 23:01:42 +00003565 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003566 int i;
aliguori376253e2009-03-05 23:01:23 +00003567
aliguori731b0362009-03-05 23:01:42 +00003568 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003569
aliguoricde76ee2009-03-05 23:01:51 +00003570 if (cur_mon->rs) {
3571 for (i = 0; i < size; i++)
3572 readline_handle_byte(cur_mon->rs, buf[i]);
3573 } else {
3574 if (size == 0 || buf[size - 1] != 0)
3575 monitor_printf(cur_mon, "corrupted command\n");
3576 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003577 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003578 }
aliguori731b0362009-03-05 23:01:42 +00003579
3580 cur_mon = old_mon;
3581}
aliguorid8f44602008-10-06 13:52:44 +00003582
aliguori376253e2009-03-05 23:01:23 +00003583static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003584{
aliguori731b0362009-03-05 23:01:42 +00003585 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003586 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003587 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003588}
3589
aliguoricde76ee2009-03-05 23:01:51 +00003590int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003591{
aliguoricde76ee2009-03-05 23:01:51 +00003592 if (!mon->rs)
3593 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003594 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003595 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003596}
3597
aliguori376253e2009-03-05 23:01:23 +00003598void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003599{
aliguoricde76ee2009-03-05 23:01:51 +00003600 if (!mon->rs)
3601 return;
aliguori731b0362009-03-05 23:01:42 +00003602 if (--mon->suspend_cnt == 0)
3603 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003604}
3605
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003606/**
3607 * monitor_control_event(): Print QMP gretting
3608 */
3609static void monitor_control_event(void *opaque, int event)
3610{
3611 if (event == CHR_EVENT_OPENED) {
3612 QObject *data;
3613 Monitor *mon = opaque;
3614
3615 data = qobject_from_jsonf("{ 'QMP': { 'capabilities': [] } }");
3616 assert(data != NULL);
3617
3618 monitor_json_emitter(mon, data);
3619 qobject_decref(data);
3620 }
3621}
3622
aliguori731b0362009-03-05 23:01:42 +00003623static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003624{
aliguori376253e2009-03-05 23:01:23 +00003625 Monitor *mon = opaque;
3626
aliguori2724b182009-03-05 23:01:47 +00003627 switch (event) {
3628 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003629 mon->mux_out = 0;
3630 if (mon->reset_seen) {
3631 readline_restart(mon->rs);
3632 monitor_resume(mon);
3633 monitor_flush(mon);
3634 } else {
3635 mon->suspend_cnt = 0;
3636 }
aliguori2724b182009-03-05 23:01:47 +00003637 break;
ths86e94de2007-01-05 22:01:59 +00003638
aliguori2724b182009-03-05 23:01:47 +00003639 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003640 if (mon->reset_seen) {
3641 if (mon->suspend_cnt == 0) {
3642 monitor_printf(mon, "\n");
3643 }
3644 monitor_flush(mon);
3645 monitor_suspend(mon);
3646 } else {
3647 mon->suspend_cnt++;
3648 }
3649 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00003650 break;
3651
Amit Shahb6b8df52009-10-07 18:31:16 +05303652 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003653 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3654 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003655 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00003656 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003657 }
3658 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00003659 break;
3660 }
ths86e94de2007-01-05 22:01:59 +00003661}
3662
aliguori76655d62009-03-06 20:27:37 +00003663
3664/*
3665 * Local variables:
3666 * c-indent-level: 4
3667 * c-basic-offset: 4
3668 * tab-width: 8
3669 * End:
3670 */
3671
Luiz Capitulinoadcb1812009-11-26 22:58:52 -02003672const char *monitor_cmdline_parse(const char *cmdline, int *flags)
3673{
3674 const char *dev;
3675
3676 if (strstart(cmdline, "control,", &dev)) {
3677 if (strstart(dev, "vc", NULL)) {
3678 fprintf(stderr, "qemu: control mode is for low-level interaction ");
3679 fprintf(stderr, "cannot be used with device 'vc'\n");
3680 exit(1);
3681 }
3682 *flags &= ~MONITOR_USE_READLINE;
3683 *flags |= MONITOR_USE_CONTROL;
3684 return dev;
3685 }
3686
3687 return cmdline;
3688}
3689
aliguori731b0362009-03-05 23:01:42 +00003690void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003691{
aliguori731b0362009-03-05 23:01:42 +00003692 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003693 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003694
3695 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003696 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003697 is_first_init = 0;
3698 }
aliguori87127162009-03-05 23:01:29 +00003699
3700 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003701
aliguori87127162009-03-05 23:01:29 +00003702 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003703 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00003704 if (flags & MONITOR_USE_READLINE) {
3705 mon->rs = readline_init(mon, monitor_find_completion);
3706 monitor_read_command(mon, 0);
3707 }
aliguori87127162009-03-05 23:01:29 +00003708
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003709 if (monitor_ctrl_mode(mon)) {
3710 /* Control mode requires special handlers */
3711 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
3712 monitor_control_event, mon);
3713 } else {
3714 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
3715 monitor_event, mon);
3716 }
aliguori87127162009-03-05 23:01:29 +00003717
Blue Swirl72cf2d42009-09-12 07:36:22 +00003718 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003719 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003720 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003721}
3722
aliguori376253e2009-03-05 23:01:23 +00003723static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003724{
aliguoribb5fc202009-03-05 23:01:15 +00003725 BlockDriverState *bs = opaque;
3726 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003727
aliguoribb5fc202009-03-05 23:01:15 +00003728 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003729 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003730 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003731 }
aliguori731b0362009-03-05 23:01:42 +00003732 if (mon->password_completion_cb)
3733 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003734
aliguori731b0362009-03-05 23:01:42 +00003735 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003736}
aliguoric0f4ce72009-03-05 23:01:01 +00003737
aliguori376253e2009-03-05 23:01:23 +00003738void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003739 BlockDriverCompletionFunc *completion_cb,
3740 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003741{
aliguoricde76ee2009-03-05 23:01:51 +00003742 int err;
3743
aliguoribb5fc202009-03-05 23:01:15 +00003744 if (!bdrv_key_required(bs)) {
3745 if (completion_cb)
3746 completion_cb(opaque, 0);
3747 return;
3748 }
aliguoric0f4ce72009-03-05 23:01:01 +00003749
aliguori376253e2009-03-05 23:01:23 +00003750 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3751 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003752
aliguori731b0362009-03-05 23:01:42 +00003753 mon->password_completion_cb = completion_cb;
3754 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003755
aliguoricde76ee2009-03-05 23:01:51 +00003756 err = monitor_read_password(mon, bdrv_password_cb, bs);
3757
3758 if (err && completion_cb)
3759 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003760}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003761
3762typedef struct QemuErrorSink QemuErrorSink;
3763struct QemuErrorSink {
3764 enum {
3765 ERR_SINK_FILE,
3766 ERR_SINK_MONITOR,
3767 } dest;
3768 union {
3769 FILE *fp;
3770 Monitor *mon;
3771 };
3772 QemuErrorSink *previous;
3773};
3774
Blue Swirl528e93a2009-08-31 15:14:40 +00003775static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003776
3777void qemu_errors_to_file(FILE *fp)
3778{
3779 QemuErrorSink *sink;
3780
3781 sink = qemu_mallocz(sizeof(*sink));
3782 sink->dest = ERR_SINK_FILE;
3783 sink->fp = fp;
3784 sink->previous = qemu_error_sink;
3785 qemu_error_sink = sink;
3786}
3787
3788void qemu_errors_to_mon(Monitor *mon)
3789{
3790 QemuErrorSink *sink;
3791
3792 sink = qemu_mallocz(sizeof(*sink));
3793 sink->dest = ERR_SINK_MONITOR;
3794 sink->mon = mon;
3795 sink->previous = qemu_error_sink;
3796 qemu_error_sink = sink;
3797}
3798
3799void qemu_errors_to_previous(void)
3800{
3801 QemuErrorSink *sink;
3802
3803 assert(qemu_error_sink != NULL);
3804 sink = qemu_error_sink;
3805 qemu_error_sink = sink->previous;
3806 qemu_free(sink);
3807}
3808
3809void qemu_error(const char *fmt, ...)
3810{
3811 va_list args;
3812
3813 assert(qemu_error_sink != NULL);
3814 switch (qemu_error_sink->dest) {
3815 case ERR_SINK_FILE:
3816 va_start(args, fmt);
3817 vfprintf(qemu_error_sink->fp, fmt, args);
3818 va_end(args);
3819 break;
3820 case ERR_SINK_MONITOR:
3821 va_start(args, fmt);
3822 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3823 va_end(args);
3824 break;
3825 }
3826}
Luiz Capitulino8204a912009-11-18 23:05:31 -02003827
3828void qemu_error_internal(const char *file, int linenr, const char *func,
3829 const char *fmt, ...)
3830{
3831 va_list va;
3832 QError *qerror;
3833
3834 assert(qemu_error_sink != NULL);
3835
3836 va_start(va, fmt);
3837 qerror = qerror_from_info(file, linenr, func, fmt, &va);
3838 va_end(va);
3839
3840 switch (qemu_error_sink->dest) {
3841 case ERR_SINK_FILE:
3842 qerror_print(qerror);
3843 QDECREF(qerror);
3844 break;
3845 case ERR_SINK_MONITOR:
3846 assert(qemu_error_sink->mon->error == NULL);
3847 qemu_error_sink->mon->error = qerror;
3848 break;
3849 }
3850}