blob: fea73834d0da9510e42e452cbbc371ced8facda1 [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"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000035#include "net/slirp.h"
pbrook87ecb682007-11-17 17:14:51 +000036#include "qemu-char.h"
37#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000038#include "monitor.h"
39#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000040#include "console.h"
41#include "block.h"
42#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000043#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000044#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000045#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000046#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000047#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000048#include "acl.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030049#include "qint.h"
Luiz Capitulino8f3cec02009-10-07 13:42:04 -030050#include "qlist.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030051#include "qdict.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020052#include "qbool.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030053#include "qstring.h"
Luiz Capitulino8204a912009-11-18 23:05:31 -020054#include "qerror.h"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020055#include "qjson.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020056#include "json-streamer.h"
57#include "json-parser.h"
Blue Swirld08d6f02009-12-04 18:06:39 +000058#include "osdep.h"
ths6a5bd302007-12-03 17:05:38 +000059
bellard9dc39cb2004-03-14 21:38:27 +000060//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000061//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000062
bellard9307c4c2004-04-04 12:57:25 +000063/*
64 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000065 *
bellard9307c4c2004-04-04 12:57:25 +000066 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000067 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000068 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000069 * 'i' 32 bit integer
70 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000071 * '/' optional gdb-like print format (like "/10x")
72 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030073 * '?' optional type (for all types, except '/')
74 * '.' other form of optional type (for 'i' and 'l')
75 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000076 *
77 */
78
Anthony Liguoric227f092009-10-01 16:12:16 -050079typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000080 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000081 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +000082 const char *params;
83 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -030084 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -030085 union {
86 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -030087 void (*info_new)(Monitor *mon, QObject **ret_data);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -030088 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -030089 void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Luiz Capitulino910df892009-10-07 13:41:51 -030090 } mhandler;
Anthony Liguoric227f092009-10-01 16:12:16 -050091} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000092
Mark McLoughlinf07918f2009-07-22 09:11:40 +010093/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -050094typedef struct mon_fd_t mon_fd_t;
95struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +010096 char *name;
97 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -050098 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010099};
100
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200101typedef struct MonitorControl {
102 QObject *id;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200103 int print_enabled;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200104 JSONMessageParser parser;
105} MonitorControl;
106
aliguori87127162009-03-05 23:01:29 +0000107struct Monitor {
108 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200109 int mux_out;
110 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000111 int flags;
112 int suspend_cnt;
113 uint8_t outbuf[1024];
114 int outbuf_index;
115 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200116 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000117 CPUState *mon_cpu;
118 BlockDriverCompletionFunc *password_completion_cb;
119 void *password_opaque;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200120 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500121 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000122 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000123};
124
Blue Swirl72cf2d42009-09-12 07:36:22 +0000125static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000126
Anthony Liguoric227f092009-10-01 16:12:16 -0500127static const mon_cmd_t mon_cmds[];
128static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000129
aliguori87127162009-03-05 23:01:29 +0000130Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000131
aliguori731b0362009-03-05 23:01:42 +0000132static void monitor_command_cb(Monitor *mon, const char *cmdline,
133 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000134
Luiz Capitulino418173c2009-11-26 22:58:51 -0200135/* Return true if in control mode, false otherwise */
136static inline int monitor_ctrl_mode(const Monitor *mon)
137{
138 return (mon->flags & MONITOR_USE_CONTROL);
139}
140
aliguori731b0362009-03-05 23:01:42 +0000141static void monitor_read_command(Monitor *mon, int show_prompt)
142{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200143 if (!mon->rs)
144 return;
145
aliguori731b0362009-03-05 23:01:42 +0000146 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
147 if (show_prompt)
148 readline_show_prompt(mon->rs);
149}
bellard6a00d602005-11-21 23:25:50 +0000150
aliguoricde76ee2009-03-05 23:01:51 +0000151static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
152 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000153{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100154 if (monitor_ctrl_mode(mon)) {
155 qemu_error_new(QERR_MISSING_PARAMETER, "password");
156 return -EINVAL;
157 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000158 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
159 /* prompt is printed on return from the command handler */
160 return 0;
161 } else {
162 monitor_printf(mon, "terminal does not support password prompting\n");
163 return -ENOTTY;
164 }
aliguoribb5fc202009-03-05 23:01:15 +0000165}
166
aliguori376253e2009-03-05 23:01:23 +0000167void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000168{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200169 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000170 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
171 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000172 }
173}
174
175/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000176static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000177{
ths60fe76f2007-12-16 03:02:09 +0000178 char c;
aliguori731b0362009-03-05 23:01:42 +0000179
bellard7e2515e2004-08-01 21:52:19 +0000180 for(;;) {
181 c = *str++;
182 if (c == '\0')
183 break;
bellard7ba12602006-07-14 20:26:42 +0000184 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000185 mon->outbuf[mon->outbuf_index++] = '\r';
186 mon->outbuf[mon->outbuf_index++] = c;
187 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
188 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000189 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000190 }
191}
192
aliguori376253e2009-03-05 23:01:23 +0000193void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000194{
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200195 if (!mon)
196 return;
197
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200198 if (mon->mc && !mon->mc->print_enabled) {
199 qemu_error_new(QERR_UNDEFINED_ERROR);
200 } else {
201 char buf[4096];
202 vsnprintf(buf, sizeof(buf), fmt, ap);
203 monitor_puts(mon, buf);
204 }
bellard7e2515e2004-08-01 21:52:19 +0000205}
206
aliguori376253e2009-03-05 23:01:23 +0000207void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000208{
209 va_list ap;
210 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000211 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000212 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000213}
214
aliguori376253e2009-03-05 23:01:23 +0000215void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000216{
217 int i;
218
219 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000220 switch (filename[i]) {
221 case ' ':
222 case '"':
223 case '\\':
224 monitor_printf(mon, "\\%c", filename[i]);
225 break;
226 case '\t':
227 monitor_printf(mon, "\\t");
228 break;
229 case '\r':
230 monitor_printf(mon, "\\r");
231 break;
232 case '\n':
233 monitor_printf(mon, "\\n");
234 break;
235 default:
236 monitor_printf(mon, "%c", filename[i]);
237 break;
238 }
thsfef30742006-12-22 14:11:32 +0000239 }
240}
241
bellard7fe48482004-10-09 18:08:01 +0000242static int monitor_fprintf(FILE *stream, const char *fmt, ...)
243{
244 va_list ap;
245 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000246 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000247 va_end(ap);
248 return 0;
249}
250
Luiz Capitulino13c74252009-10-07 13:41:55 -0300251static void monitor_user_noop(Monitor *mon, const QObject *data) { }
252
Luiz Capitulino13917be2009-10-07 13:41:54 -0300253static inline int monitor_handler_ported(const mon_cmd_t *cmd)
254{
255 return cmd->user_print != NULL;
256}
257
Luiz Capitulino8204a912009-11-18 23:05:31 -0200258static inline int monitor_has_error(const Monitor *mon)
259{
260 return mon->error != NULL;
261}
262
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200263static void monitor_json_emitter(Monitor *mon, const QObject *data)
264{
265 QString *json;
266
267 json = qobject_to_json(data);
268 assert(json != NULL);
269
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200270 mon->mc->print_enabled = 1;
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200271 monitor_printf(mon, "%s\n", qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200272 mon->mc->print_enabled = 0;
273
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200274 QDECREF(json);
275}
276
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200277static void monitor_protocol_emitter(Monitor *mon, QObject *data)
278{
279 QDict *qmp;
280
281 qmp = qdict_new();
282
283 if (!monitor_has_error(mon)) {
284 /* success response */
285 if (data) {
286 qobject_incref(data);
287 qdict_put_obj(qmp, "return", data);
288 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200289 /* return an empty QDict by default */
290 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200291 }
292 } else {
293 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100294 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200295 qdict_put(qmp, "error", mon->error->error);
296 QINCREF(mon->error->error);
297 QDECREF(mon->error);
298 mon->error = NULL;
299 }
300
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200301 if (mon->mc->id) {
302 qdict_put_obj(qmp, "id", mon->mc->id);
303 mon->mc->id = NULL;
304 }
305
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200306 monitor_json_emitter(mon, QOBJECT(qmp));
307 QDECREF(qmp);
308}
309
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200310static void timestamp_put(QDict *qdict)
311{
312 int err;
313 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000314 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200315
Blue Swirld08d6f02009-12-04 18:06:39 +0000316 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200317 if (err < 0)
318 return;
319
320 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
321 "'microseconds': %" PRId64 " }",
322 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
323 assert(obj != NULL);
324
325 qdict_put_obj(qdict, "timestamp", obj);
326}
327
328/**
329 * monitor_protocol_event(): Generate a Monitor event
330 *
331 * Event-specific data can be emitted through the (optional) 'data' parameter.
332 */
333void monitor_protocol_event(MonitorEvent event, QObject *data)
334{
335 QDict *qmp;
336 const char *event_name;
337 Monitor *mon = cur_mon;
338
Blue Swirl242cd002009-12-04 18:05:45 +0000339 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200340
341 if (!monitor_ctrl_mode(mon))
342 return;
343
344 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000345 case QEVENT_DEBUG:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200346 event_name = "DEBUG";
347 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000348 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200349 event_name = "SHUTDOWN";
350 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000351 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200352 event_name = "RESET";
353 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000354 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200355 event_name = "POWERDOWN";
356 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000357 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200358 event_name = "STOP";
359 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200360 case QEVENT_VNC_CONNECTED:
361 event_name = "VNC_CONNECTED";
362 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200363 case QEVENT_VNC_INITIALIZED:
364 event_name = "VNC_INITIALIZED";
365 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200366 case QEVENT_VNC_DISCONNECTED:
367 event_name = "VNC_DISCONNECTED";
368 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200369 default:
370 abort();
371 break;
372 }
373
374 qmp = qdict_new();
375 timestamp_put(qmp);
376 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200377 if (data) {
378 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200379 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200380 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200381
382 monitor_json_emitter(mon, QOBJECT(qmp));
383 QDECREF(qmp);
384}
385
bellard9dc39cb2004-03-14 21:38:27 +0000386static int compare_cmd(const char *name, const char *list)
387{
388 const char *p, *pstart;
389 int len;
390 len = strlen(name);
391 p = list;
392 for(;;) {
393 pstart = p;
394 p = strchr(p, '|');
395 if (!p)
396 p = pstart + strlen(pstart);
397 if ((p - pstart) == len && !memcmp(pstart, name, len))
398 return 1;
399 if (*p == '\0')
400 break;
401 p++;
402 }
403 return 0;
404}
405
Anthony Liguoric227f092009-10-01 16:12:16 -0500406static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000407 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000408{
Anthony Liguoric227f092009-10-01 16:12:16 -0500409 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000410
411 for(cmd = cmds; cmd->name != NULL; cmd++) {
412 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000413 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
414 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000415 }
416}
417
aliguori376253e2009-03-05 23:01:23 +0000418static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000419{
420 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000421 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000422 } else {
aliguori376253e2009-03-05 23:01:23 +0000423 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000424 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000425 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000426 monitor_printf(mon, "Log items (comma separated):\n");
427 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000428 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000429 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000430 }
431 }
bellard9dc39cb2004-03-14 21:38:27 +0000432 }
433}
434
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300435static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300436{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300437 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300438}
439
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300440static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000441{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200442 int all_devices;
443 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300444 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000445
bellard7954c732006-08-01 15:52:40 +0000446 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000447 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200448 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300449 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200450 continue;
451 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000452 }
453}
454
Luiz Capitulino13c74252009-10-07 13:41:55 -0300455static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000456{
Anthony Liguoric227f092009-10-01 16:12:16 -0500457 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300458 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000459
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200460 if (!item) {
461 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000462 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200463 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300464
465 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000466 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300467 break;
bellard9dc39cb2004-03-14 21:38:27 +0000468 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300469
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200470 if (cmd->name == NULL) {
471 if (monitor_ctrl_mode(mon)) {
472 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
473 return;
474 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300475 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200476 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300477
478 if (monitor_handler_ported(cmd)) {
479 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200480
481 if (!monitor_ctrl_mode(mon)) {
482 /*
483 * User Protocol function is called here, Monitor Protocol is
484 * handled by monitor_call_handler()
485 */
486 if (*ret_data)
487 cmd->user_print(mon, *ret_data);
488 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300489 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200490 if (monitor_ctrl_mode(mon)) {
491 /* handler not converted yet */
492 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
493 } else {
494 cmd->mhandler.info(mon);
495 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300496 }
497
bellard9dc39cb2004-03-14 21:38:27 +0000498 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300499
500help:
501 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000502}
503
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200504static void do_info_version_print(Monitor *mon, const QObject *data)
505{
506 QDict *qdict;
507
508 qdict = qobject_to_qdict(data);
509
510 monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
511 qdict_get_str(qdict, "package"));
512}
513
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300514/**
515 * do_info_version(): Show QEMU version
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200516 *
517 * Return a QDict with the following information:
518 *
519 * - "qemu": QEMU's version
520 * - "package": package's version
521 *
522 * Example:
523 *
524 * { "qemu": "0.11.50", "package": "" }
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300525 */
526static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000527{
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200528 *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
529 QEMU_VERSION, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000530}
531
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200532static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000533{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200534 QDict *qdict;
535
536 qdict = qobject_to_qdict(data);
537 if (qdict_size(qdict) == 0) {
538 return;
539 }
540
541 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
542}
543
544/**
545 * do_info_name(): Show VM name
546 *
547 * Return a QDict with the following information:
548 *
549 * - "name": VM's name (optional)
550 *
551 * Example:
552 *
553 * { "name": "qemu-name" }
554 */
555static void do_info_name(Monitor *mon, QObject **ret_data)
556{
557 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
558 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000559}
560
Luiz Capitulino1a728672009-12-10 17:15:56 -0200561static QObject *get_cmd_dict(const char *name)
562{
563 const char *p;
564
565 /* Remove '|' from some commands */
566 p = strchr(name, '|');
567 if (p) {
568 p++;
569 } else {
570 p = name;
571 }
572
573 return qobject_from_jsonf("{ 'name': %s }", p);
574}
575
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200576/**
577 * do_info_commands(): List QMP available commands
578 *
Luiz Capitulino1a728672009-12-10 17:15:56 -0200579 * Each command is represented by a QDict, the returned QObject is a QList
580 * of all commands.
581 *
582 * The QDict contains:
583 *
584 * - "name": command's name
585 *
586 * Example:
587 *
588 * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] }
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200589 */
590static void do_info_commands(Monitor *mon, QObject **ret_data)
591{
592 QList *cmd_list;
593 const mon_cmd_t *cmd;
594
595 cmd_list = qlist_new();
596
597 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
598 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200599 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200600 }
601 }
602
603 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
604 if (monitor_handler_ported(cmd)) {
605 char buf[128];
606 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200607 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200608 }
609 }
610
611 *ret_data = QOBJECT(cmd_list);
612}
613
aurel32bf4f74c2008-12-18 22:42:34 +0000614#if defined(TARGET_I386)
Luiz Capitulino14f07202009-12-10 17:16:02 -0200615static void do_info_hpet_print(Monitor *mon, const QObject *data)
aliguori16b29ae2008-12-17 23:28:44 +0000616{
aliguori376253e2009-03-05 23:01:23 +0000617 monitor_printf(mon, "HPET is %s by QEMU\n",
Luiz Capitulino14f07202009-12-10 17:16:02 -0200618 qdict_get_bool(qobject_to_qdict(data), "enabled") ?
619 "enabled" : "disabled");
620}
621
622/**
623 * do_info_hpet(): Show HPET state
624 *
625 * Return a QDict with the following information:
626 *
627 * - "enabled": true if hpet if enabled, false otherwise
628 *
629 * Example:
630 *
631 * { "enabled": true }
632 */
633static void do_info_hpet(Monitor *mon, QObject **ret_data)
634{
635 *ret_data = qobject_from_jsonf("{ 'enabled': %i }", !no_hpet);
aliguori16b29ae2008-12-17 23:28:44 +0000636}
aurel32bf4f74c2008-12-18 22:42:34 +0000637#endif
aliguori16b29ae2008-12-17 23:28:44 +0000638
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200639static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000640{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200641 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
642}
643
644/**
645 * do_info_uuid(): Show VM UUID
646 *
647 * Return a QDict with the following information:
648 *
649 * - "UUID": Universally Unique Identifier
650 *
651 * Example:
652 *
653 * { "UUID": "550e8400-e29b-41d4-a716-446655440000" }
654 */
655static void do_info_uuid(Monitor *mon, QObject **ret_data)
656{
657 char uuid[64];
658
659 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000660 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
661 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
662 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
663 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200664 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000665}
666
bellard6a00d602005-11-21 23:25:50 +0000667/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000668static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000669{
670 CPUState *env;
671
672 for(env = first_cpu; env != NULL; env = env->next_cpu) {
673 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000674 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000675 return 0;
676 }
677 }
678 return -1;
679}
680
pbrook9596ebb2007-11-18 01:44:38 +0000681static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000682{
aliguori731b0362009-03-05 23:01:42 +0000683 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000684 mon_set_cpu(0);
685 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300686 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000687 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000688}
689
aliguori376253e2009-03-05 23:01:23 +0000690static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000691{
bellard6a00d602005-11-21 23:25:50 +0000692 CPUState *env;
693 env = mon_get_cpu();
694 if (!env)
695 return;
bellard9307c4c2004-04-04 12:57:25 +0000696#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000697 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000698 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000699#else
aliguori376253e2009-03-05 23:01:23 +0000700 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000701 0);
bellard9307c4c2004-04-04 12:57:25 +0000702#endif
703}
704
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300705static void print_cpu_iter(QObject *obj, void *opaque)
706{
707 QDict *cpu;
708 int active = ' ';
709 Monitor *mon = opaque;
710
711 assert(qobject_type(obj) == QTYPE_QDICT);
712 cpu = qobject_to_qdict(obj);
713
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200714 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300715 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200716 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300717
718 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
719
720#if defined(TARGET_I386)
721 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
722 (target_ulong) qdict_get_int(cpu, "pc"));
723#elif defined(TARGET_PPC)
724 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
725 (target_long) qdict_get_int(cpu, "nip"));
726#elif defined(TARGET_SPARC)
727 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
728 (target_long) qdict_get_int(cpu, "pc"));
729 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
730 (target_long) qdict_get_int(cpu, "npc"));
731#elif defined(TARGET_MIPS)
732 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
733 (target_long) qdict_get_int(cpu, "PC"));
734#endif
735
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200736 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300737 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200738 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300739
740 monitor_printf(mon, "\n");
741}
742
743static void monitor_print_cpus(Monitor *mon, const QObject *data)
744{
745 QList *cpu_list;
746
747 assert(qobject_type(data) == QTYPE_QLIST);
748 cpu_list = qobject_to_qlist(data);
749 qlist_iter(cpu_list, print_cpu_iter, mon);
750}
751
752/**
753 * do_info_cpus(): Show CPU information
754 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200755 * Return a QList. Each CPU is represented by a QDict, which contains:
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300756 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200757 * - "cpu": CPU index
758 * - "current": true if this is the current CPU, false otherwise
759 * - "halted": true if the cpu is halted, false otherwise
760 * - Current program counter. The key's name depends on the architecture:
761 * "pc": i386/x86)64
762 * "nip": PPC
763 * "pc" and "npc": sparc
764 * "PC": mips
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300765 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200766 * Example:
767 *
768 * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 },
769 * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ]
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300770 */
771static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000772{
773 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300774 QList *cpu_list;
775
776 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000777
778 /* just to set the default cpu if not already done */
779 mon_get_cpu();
780
781 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200782 QDict *cpu;
783 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300784
Avi Kivity4c0960c2009-08-17 23:19:53 +0300785 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300786
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200787 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
788 env->cpu_index, env == mon->mon_cpu,
789 env->halted);
790 assert(obj != NULL);
791
792 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300793
bellard6a00d602005-11-21 23:25:50 +0000794#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300795 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000796#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300797 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000798#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300799 qdict_put(cpu, "pc", qint_from_int(env->pc));
800 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000801#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300802 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000803#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300804
805 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000806 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300807
808 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000809}
810
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300811static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000812{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300813 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000814 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000815 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000816}
817
aliguori376253e2009-03-05 23:01:23 +0000818static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000819{
aliguori376253e2009-03-05 23:01:23 +0000820 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000821}
822
aliguori376253e2009-03-05 23:01:23 +0000823static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000824{
825 int i;
bellard7e2515e2004-08-01 21:52:19 +0000826 const char *str;
ths3b46e622007-09-17 08:09:54 +0000827
aliguoricde76ee2009-03-05 23:01:51 +0000828 if (!mon->rs)
829 return;
bellard7e2515e2004-08-01 21:52:19 +0000830 i = 0;
831 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000832 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000833 if (!str)
834 break;
aliguori376253e2009-03-05 23:01:23 +0000835 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000836 i++;
bellardaa455482004-04-04 13:07:25 +0000837 }
838}
839
j_mayer76a66252007-03-07 08:32:30 +0000840#if defined(TARGET_PPC)
841/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000842static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000843{
844 CPUState *env;
845
846 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000847 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000848}
849#endif
850
Luiz Capitulinob223f352009-10-07 13:41:56 -0300851/**
852 * do_quit(): Quit QEMU execution
853 */
854static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000855{
856 exit(0);
857}
858
aliguori376253e2009-03-05 23:01:23 +0000859static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000860{
861 if (bdrv_is_inserted(bs)) {
862 if (!force) {
863 if (!bdrv_is_removable(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100864 qemu_error_new(QERR_DEVICE_NOT_REMOVABLE,
865 bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000866 return -1;
867 }
868 if (bdrv_is_locked(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100869 qemu_error_new(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000870 return -1;
871 }
872 }
873 bdrv_close(bs);
874 }
875 return 0;
876}
877
Luiz Capitulinoe1c923a2009-10-16 12:23:49 -0300878static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000879{
880 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300881 int force = qdict_get_int(qdict, "force");
Luiz Capitulino78d714e2009-12-14 18:53:21 -0200882 const char *filename = qdict_get_str(qdict, "device");
bellard9dc39cb2004-03-14 21:38:27 +0000883
bellard9307c4c2004-04-04 12:57:25 +0000884 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000885 if (!bs) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100886 qemu_error_new(QERR_DEVICE_NOT_FOUND, filename);
bellard9dc39cb2004-03-14 21:38:27 +0000887 return;
888 }
aliguori376253e2009-03-05 23:01:23 +0000889 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000890}
891
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -0200892static void do_block_set_passwd(Monitor *mon, const QDict *qdict,
893 QObject **ret_data)
894{
895 BlockDriverState *bs;
896
897 bs = bdrv_find(qdict_get_str(qdict, "device"));
898 if (!bs) {
899 qemu_error_new(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
900 return;
901 }
902
903 if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) {
904 qemu_error_new(QERR_INVALID_PASSWORD);
905 }
906}
907
aliguori376253e2009-03-05 23:01:23 +0000908static void do_change_block(Monitor *mon, const char *device,
909 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000910{
911 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000912 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000913
bellard9307c4c2004-04-04 12:57:25 +0000914 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000915 if (!bs) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100916 qemu_error_new(QERR_DEVICE_NOT_FOUND, device);
bellard9dc39cb2004-03-14 21:38:27 +0000917 return;
918 }
aurel322ecea9b2008-06-18 22:10:01 +0000919 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100920 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000921 if (!drv) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100922 qemu_error_new(QERR_INVALID_BLOCK_FORMAT, fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000923 return;
924 }
925 }
aliguori376253e2009-03-05 23:01:23 +0000926 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000927 return;
aurel322ecea9b2008-06-18 22:10:01 +0000928 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000929 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000930}
931
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100932static void change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000933{
934 if (vnc_display_password(NULL, password) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100935 qemu_error_new(QERR_SET_PASSWD_FAILED);
aliguoribb5fc202009-03-05 23:01:15 +0000936
Markus Armbruster2895e072009-12-07 21:37:01 +0100937}
938
939static void change_vnc_password_cb(Monitor *mon, const char *password,
940 void *opaque)
941{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100942 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000943 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000944}
945
aliguori376253e2009-03-05 23:01:23 +0000946static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000947{
ths70848512007-08-25 01:37:05 +0000948 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000949 strcmp(target, "password") == 0) {
950 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000951 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000952 strncpy(password, arg, sizeof(password));
953 password[sizeof(password) - 1] = '\0';
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100954 change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000955 } else {
aliguori376253e2009-03-05 23:01:23 +0000956 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000957 }
ths70848512007-08-25 01:37:05 +0000958 } else {
aliguori28a76be2009-03-06 20:27:40 +0000959 if (vnc_display_open(NULL, target) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100960 qemu_error_new(QERR_VNC_SERVER_FAILED, target);
ths70848512007-08-25 01:37:05 +0000961 }
thse25a5822007-08-25 01:36:20 +0000962}
963
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100964/**
965 * do_change(): Change a removable medium, or VNC configuration
966 */
967static void do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +0000968{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300969 const char *device = qdict_get_str(qdict, "device");
970 const char *target = qdict_get_str(qdict, "target");
971 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000972 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000973 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000974 } else {
aliguori28a76be2009-03-06 20:27:40 +0000975 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000976 }
977}
978
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300979static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000980{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300981 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000982}
983
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300984static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000985{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300986 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000987}
988
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300989static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000990{
991 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300992 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000993
bellard9307c4c2004-04-04 12:57:25 +0000994 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000995 mask = 0;
996 } else {
bellard9307c4c2004-04-04 12:57:25 +0000997 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000998 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000999 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001000 return;
1001 }
1002 }
1003 cpu_set_log(mask);
1004}
1005
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001006static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001007{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001008 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001009 if (!option || !strcmp(option, "on")) {
1010 singlestep = 1;
1011 } else if (!strcmp(option, "off")) {
1012 singlestep = 0;
1013 } else {
1014 monitor_printf(mon, "unexpected option %s\n", option);
1015 }
1016}
1017
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001018/**
1019 * do_stop(): Stop VM execution
1020 */
1021static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001022{
1023 vm_stop(EXCP_INTERRUPT);
1024}
1025
aliguoribb5fc202009-03-05 23:01:15 +00001026static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001027
aliguori376253e2009-03-05 23:01:23 +00001028struct bdrv_iterate_context {
1029 Monitor *mon;
1030 int err;
1031};
aliguoric0f4ce72009-03-05 23:01:01 +00001032
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001033/**
1034 * do_cont(): Resume emulation.
1035 */
1036static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001037{
1038 struct bdrv_iterate_context context = { mon, 0 };
1039
1040 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001041 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +00001042 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +00001043 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +00001044}
1045
aliguoribb5fc202009-03-05 23:01:15 +00001046static void bdrv_key_cb(void *opaque, int err)
1047{
aliguori376253e2009-03-05 23:01:23 +00001048 Monitor *mon = opaque;
1049
aliguoribb5fc202009-03-05 23:01:15 +00001050 /* another key was set successfully, retry to continue */
1051 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001052 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001053}
1054
1055static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1056{
aliguori376253e2009-03-05 23:01:23 +00001057 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001058
aliguori376253e2009-03-05 23:01:23 +00001059 if (!context->err && bdrv_key_required(bs)) {
1060 context->err = -EBUSY;
1061 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1062 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001063 }
1064}
1065
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001066static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001067{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001068 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001069 if (!device)
1070 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1071 if (gdbserver_start(device) < 0) {
1072 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1073 device);
1074 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001075 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001076 } else {
aliguori59030a82009-04-05 18:43:41 +00001077 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1078 device);
bellard8a7ddc32004-03-31 19:00:16 +00001079 }
1080}
1081
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001082static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001083{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001084 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001085 if (select_watchdog_action(action) == -1) {
1086 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1087 }
1088}
1089
aliguori376253e2009-03-05 23:01:23 +00001090static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001091{
aliguori376253e2009-03-05 23:01:23 +00001092 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001093 switch(c) {
1094 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001095 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001096 break;
1097 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001098 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001099 break;
1100 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001101 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001102 break;
1103 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001104 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001105 break;
1106 default:
1107 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001108 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001109 } else {
aliguori376253e2009-03-05 23:01:23 +00001110 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001111 }
1112 break;
1113 }
aliguori376253e2009-03-05 23:01:23 +00001114 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001115}
1116
aliguori376253e2009-03-05 23:01:23 +00001117static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001118 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001119{
bellard6a00d602005-11-21 23:25:50 +00001120 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001121 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001122 uint8_t buf[16];
1123 uint64_t v;
1124
1125 if (format == 'i') {
1126 int flags;
1127 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001128 env = mon_get_cpu();
1129 if (!env && !is_physical)
1130 return;
bellard9307c4c2004-04-04 12:57:25 +00001131#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001132 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001133 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001134 } else if (wsize == 4) {
1135 flags = 0;
1136 } else {
bellard6a15fd12006-04-12 21:07:07 +00001137 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001138 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001139 if (env) {
1140#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001141 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001142 (env->segs[R_CS].flags & DESC_L_MASK))
1143 flags = 2;
1144 else
1145#endif
1146 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1147 flags = 1;
1148 }
bellard4c27ba22004-04-25 18:05:08 +00001149 }
1150#endif
aliguori376253e2009-03-05 23:01:23 +00001151 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001152 return;
1153 }
1154
1155 len = wsize * count;
1156 if (wsize == 1)
1157 line_size = 8;
1158 else
1159 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001160 max_digits = 0;
1161
1162 switch(format) {
1163 case 'o':
1164 max_digits = (wsize * 8 + 2) / 3;
1165 break;
1166 default:
1167 case 'x':
1168 max_digits = (wsize * 8) / 4;
1169 break;
1170 case 'u':
1171 case 'd':
1172 max_digits = (wsize * 8 * 10 + 32) / 33;
1173 break;
1174 case 'c':
1175 wsize = 1;
1176 break;
1177 }
1178
1179 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001180 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001181 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001182 else
aliguori376253e2009-03-05 23:01:23 +00001183 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001184 l = len;
1185 if (l > line_size)
1186 l = line_size;
1187 if (is_physical) {
1188 cpu_physical_memory_rw(addr, buf, l, 0);
1189 } else {
bellard6a00d602005-11-21 23:25:50 +00001190 env = mon_get_cpu();
1191 if (!env)
1192 break;
aliguoric8f79b62008-08-18 14:00:20 +00001193 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001194 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001195 break;
1196 }
bellard9307c4c2004-04-04 12:57:25 +00001197 }
ths5fafdf22007-09-16 21:08:06 +00001198 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001199 while (i < l) {
1200 switch(wsize) {
1201 default:
1202 case 1:
1203 v = ldub_raw(buf + i);
1204 break;
1205 case 2:
1206 v = lduw_raw(buf + i);
1207 break;
1208 case 4:
bellard92a31b12005-02-10 22:00:52 +00001209 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001210 break;
1211 case 8:
1212 v = ldq_raw(buf + i);
1213 break;
1214 }
aliguori376253e2009-03-05 23:01:23 +00001215 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001216 switch(format) {
1217 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001218 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001219 break;
1220 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001221 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001222 break;
1223 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001224 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001225 break;
1226 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001227 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001228 break;
1229 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001230 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001231 break;
1232 }
1233 i += wsize;
1234 }
aliguori376253e2009-03-05 23:01:23 +00001235 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001236 addr += l;
1237 len -= l;
1238 }
1239}
1240
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001241static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001242{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001243 int count = qdict_get_int(qdict, "count");
1244 int format = qdict_get_int(qdict, "format");
1245 int size = qdict_get_int(qdict, "size");
1246 target_long addr = qdict_get_int(qdict, "addr");
1247
aliguori376253e2009-03-05 23:01:23 +00001248 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001249}
1250
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001251static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001252{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001253 int count = qdict_get_int(qdict, "count");
1254 int format = qdict_get_int(qdict, "format");
1255 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001256 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001257
aliguori376253e2009-03-05 23:01:23 +00001258 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001259}
1260
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001261static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001262{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001263 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001264 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001265
blueswir17743e582007-09-24 18:39:04 +00001266#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001267 switch(format) {
1268 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001269 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001270 break;
1271 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001272 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001273 break;
1274 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001275 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001276 break;
1277 default:
1278 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001279 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001280 break;
1281 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001282 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001283 break;
1284 }
bellard92a31b12005-02-10 22:00:52 +00001285#else
1286 switch(format) {
1287 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001288 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001289 break;
1290 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001291 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001292 break;
1293 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001294 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001295 break;
1296 default:
1297 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001298 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001299 break;
1300 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001301 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001302 break;
1303 }
1304#endif
aliguori376253e2009-03-05 23:01:23 +00001305 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001306}
1307
Luiz Capitulino57e09452009-10-16 12:23:43 -03001308static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001309{
1310 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001311 uint32_t size = qdict_get_int(qdict, "size");
1312 const char *filename = qdict_get_str(qdict, "filename");
1313 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001314 uint32_t l;
1315 CPUState *env;
1316 uint8_t buf[1024];
1317
1318 env = mon_get_cpu();
1319 if (!env)
1320 return;
1321
1322 f = fopen(filename, "wb");
1323 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001324 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +00001325 return;
1326 }
1327 while (size != 0) {
1328 l = sizeof(buf);
1329 if (l > size)
1330 l = size;
1331 cpu_memory_rw_debug(env, addr, buf, l, 0);
1332 fwrite(buf, 1, l, f);
1333 addr += l;
1334 size -= l;
1335 }
1336 fclose(f);
1337}
1338
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001339static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1340 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001341{
1342 FILE *f;
1343 uint32_t l;
1344 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001345 uint32_t size = qdict_get_int(qdict, "size");
1346 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001347 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +00001348
1349 f = fopen(filename, "wb");
1350 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001351 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +00001352 return;
1353 }
1354 while (size != 0) {
1355 l = sizeof(buf);
1356 if (l > size)
1357 l = size;
1358 cpu_physical_memory_rw(addr, buf, l, 0);
1359 fwrite(buf, 1, l, f);
1360 fflush(f);
1361 addr += l;
1362 size -= l;
1363 }
1364 fclose(f);
1365}
1366
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001367static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001368{
1369 uint32_t addr;
1370 uint8_t buf[1];
1371 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001372 uint32_t start = qdict_get_int(qdict, "start");
1373 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001374
1375 sum = 0;
1376 for(addr = start; addr < (start + size); addr++) {
1377 cpu_physical_memory_rw(addr, buf, 1, 0);
1378 /* BSD sum algorithm ('sum' Unix command) */
1379 sum = (sum >> 1) | (sum << 15);
1380 sum += buf[0];
1381 }
aliguori376253e2009-03-05 23:01:23 +00001382 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001383}
1384
bellarda3a91a32004-06-04 11:06:21 +00001385typedef struct {
1386 int keycode;
1387 const char *name;
1388} KeyDef;
1389
1390static const KeyDef key_defs[] = {
1391 { 0x2a, "shift" },
1392 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001393
bellarda3a91a32004-06-04 11:06:21 +00001394 { 0x38, "alt" },
1395 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001396 { 0x64, "altgr" },
1397 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001398 { 0x1d, "ctrl" },
1399 { 0x9d, "ctrl_r" },
1400
1401 { 0xdd, "menu" },
1402
1403 { 0x01, "esc" },
1404
1405 { 0x02, "1" },
1406 { 0x03, "2" },
1407 { 0x04, "3" },
1408 { 0x05, "4" },
1409 { 0x06, "5" },
1410 { 0x07, "6" },
1411 { 0x08, "7" },
1412 { 0x09, "8" },
1413 { 0x0a, "9" },
1414 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001415 { 0x0c, "minus" },
1416 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001417 { 0x0e, "backspace" },
1418
1419 { 0x0f, "tab" },
1420 { 0x10, "q" },
1421 { 0x11, "w" },
1422 { 0x12, "e" },
1423 { 0x13, "r" },
1424 { 0x14, "t" },
1425 { 0x15, "y" },
1426 { 0x16, "u" },
1427 { 0x17, "i" },
1428 { 0x18, "o" },
1429 { 0x19, "p" },
1430
1431 { 0x1c, "ret" },
1432
1433 { 0x1e, "a" },
1434 { 0x1f, "s" },
1435 { 0x20, "d" },
1436 { 0x21, "f" },
1437 { 0x22, "g" },
1438 { 0x23, "h" },
1439 { 0x24, "j" },
1440 { 0x25, "k" },
1441 { 0x26, "l" },
1442
1443 { 0x2c, "z" },
1444 { 0x2d, "x" },
1445 { 0x2e, "c" },
1446 { 0x2f, "v" },
1447 { 0x30, "b" },
1448 { 0x31, "n" },
1449 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001450 { 0x33, "comma" },
1451 { 0x34, "dot" },
1452 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001453
balrog4d3b6f62008-02-10 16:33:14 +00001454 { 0x37, "asterisk" },
1455
bellarda3a91a32004-06-04 11:06:21 +00001456 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001457 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001458 { 0x3b, "f1" },
1459 { 0x3c, "f2" },
1460 { 0x3d, "f3" },
1461 { 0x3e, "f4" },
1462 { 0x3f, "f5" },
1463 { 0x40, "f6" },
1464 { 0x41, "f7" },
1465 { 0x42, "f8" },
1466 { 0x43, "f9" },
1467 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001468 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001469 { 0x46, "scroll_lock" },
1470
bellard64866c32006-05-07 18:03:31 +00001471 { 0xb5, "kp_divide" },
1472 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001473 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001474 { 0x4e, "kp_add" },
1475 { 0x9c, "kp_enter" },
1476 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001477 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001478
1479 { 0x52, "kp_0" },
1480 { 0x4f, "kp_1" },
1481 { 0x50, "kp_2" },
1482 { 0x51, "kp_3" },
1483 { 0x4b, "kp_4" },
1484 { 0x4c, "kp_5" },
1485 { 0x4d, "kp_6" },
1486 { 0x47, "kp_7" },
1487 { 0x48, "kp_8" },
1488 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001489
bellarda3a91a32004-06-04 11:06:21 +00001490 { 0x56, "<" },
1491
1492 { 0x57, "f11" },
1493 { 0x58, "f12" },
1494
1495 { 0xb7, "print" },
1496
1497 { 0xc7, "home" },
1498 { 0xc9, "pgup" },
1499 { 0xd1, "pgdn" },
1500 { 0xcf, "end" },
1501
1502 { 0xcb, "left" },
1503 { 0xc8, "up" },
1504 { 0xd0, "down" },
1505 { 0xcd, "right" },
1506
1507 { 0xd2, "insert" },
1508 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001509#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1510 { 0xf0, "stop" },
1511 { 0xf1, "again" },
1512 { 0xf2, "props" },
1513 { 0xf3, "undo" },
1514 { 0xf4, "front" },
1515 { 0xf5, "copy" },
1516 { 0xf6, "open" },
1517 { 0xf7, "paste" },
1518 { 0xf8, "find" },
1519 { 0xf9, "cut" },
1520 { 0xfa, "lf" },
1521 { 0xfb, "help" },
1522 { 0xfc, "meta_l" },
1523 { 0xfd, "meta_r" },
1524 { 0xfe, "compose" },
1525#endif
bellarda3a91a32004-06-04 11:06:21 +00001526 { 0, NULL },
1527};
1528
1529static int get_keycode(const char *key)
1530{
1531 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001532 char *endp;
1533 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001534
1535 for(p = key_defs; p->name != NULL; p++) {
1536 if (!strcmp(key, p->name))
1537 return p->keycode;
1538 }
bellard64866c32006-05-07 18:03:31 +00001539 if (strstart(key, "0x", NULL)) {
1540 ret = strtoul(key, &endp, 0);
1541 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1542 return ret;
1543 }
bellarda3a91a32004-06-04 11:06:21 +00001544 return -1;
1545}
1546
balrogc8256f92008-06-08 22:45:01 +00001547#define MAX_KEYCODES 16
1548static uint8_t keycodes[MAX_KEYCODES];
1549static int nb_pending_keycodes;
1550static QEMUTimer *key_timer;
1551
1552static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001553{
balrogc8256f92008-06-08 22:45:01 +00001554 int keycode;
1555
1556 while (nb_pending_keycodes > 0) {
1557 nb_pending_keycodes--;
1558 keycode = keycodes[nb_pending_keycodes];
1559 if (keycode & 0x80)
1560 kbd_put_keycode(0xe0);
1561 kbd_put_keycode(keycode | 0x80);
1562 }
1563}
1564
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001565static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001566{
balrog3401c0d2008-06-04 10:05:59 +00001567 char keyname_buf[16];
1568 char *separator;
1569 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001570 const char *string = qdict_get_str(qdict, "string");
1571 int has_hold_time = qdict_haskey(qdict, "hold_time");
1572 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001573
balrogc8256f92008-06-08 22:45:01 +00001574 if (nb_pending_keycodes > 0) {
1575 qemu_del_timer(key_timer);
1576 release_keys(NULL);
1577 }
1578 if (!has_hold_time)
1579 hold_time = 100;
1580 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001581 while (1) {
1582 separator = strchr(string, '-');
1583 keyname_len = separator ? separator - string : strlen(string);
1584 if (keyname_len > 0) {
1585 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1586 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001587 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001588 return;
bellarda3a91a32004-06-04 11:06:21 +00001589 }
balrogc8256f92008-06-08 22:45:01 +00001590 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001591 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001592 return;
1593 }
1594 keyname_buf[keyname_len] = 0;
1595 keycode = get_keycode(keyname_buf);
1596 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001597 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001598 return;
1599 }
balrogc8256f92008-06-08 22:45:01 +00001600 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001601 }
balrog3401c0d2008-06-04 10:05:59 +00001602 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001603 break;
balrog3401c0d2008-06-04 10:05:59 +00001604 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001605 }
balrogc8256f92008-06-08 22:45:01 +00001606 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001607 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001608 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001609 keycode = keycodes[i];
1610 if (keycode & 0x80)
1611 kbd_put_keycode(0xe0);
1612 kbd_put_keycode(keycode & 0x7f);
1613 }
balrogc8256f92008-06-08 22:45:01 +00001614 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001615 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001616 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001617}
1618
bellard13224a82006-07-14 22:03:35 +00001619static int mouse_button_state;
1620
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001621static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001622{
1623 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001624 const char *dx_str = qdict_get_str(qdict, "dx_str");
1625 const char *dy_str = qdict_get_str(qdict, "dy_str");
1626 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001627 dx = strtol(dx_str, NULL, 0);
1628 dy = strtol(dy_str, NULL, 0);
1629 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001630 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001631 dz = strtol(dz_str, NULL, 0);
1632 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1633}
1634
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001635static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001636{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001637 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001638 mouse_button_state = button_state;
1639 kbd_mouse_event(0, 0, 0, mouse_button_state);
1640}
1641
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001642static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001643{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001644 int size = qdict_get_int(qdict, "size");
1645 int addr = qdict_get_int(qdict, "addr");
1646 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001647 uint32_t val;
1648 int suffix;
1649
1650 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001651 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001652 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001653 addr++;
1654 }
1655 addr &= 0xffff;
1656
1657 switch(size) {
1658 default:
1659 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001660 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001661 suffix = 'b';
1662 break;
1663 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001664 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001665 suffix = 'w';
1666 break;
1667 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001668 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001669 suffix = 'l';
1670 break;
1671 }
aliguori376253e2009-03-05 23:01:23 +00001672 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1673 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001674}
bellarda3a91a32004-06-04 11:06:21 +00001675
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001676static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001677{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001678 int size = qdict_get_int(qdict, "size");
1679 int addr = qdict_get_int(qdict, "addr");
1680 int val = qdict_get_int(qdict, "val");
1681
Jan Kiszkaf1147842009-07-14 10:20:11 +02001682 addr &= IOPORTS_MASK;
1683
1684 switch (size) {
1685 default:
1686 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001687 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001688 break;
1689 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001690 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001691 break;
1692 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001693 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001694 break;
1695 }
1696}
1697
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001698static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001699{
1700 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001701 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001702
Jan Kiszka76e30d02009-07-02 00:19:02 +02001703 res = qemu_boot_set(bootdevice);
1704 if (res == 0) {
1705 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1706 } else if (res > 0) {
1707 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001708 } else {
aliguori376253e2009-03-05 23:01:23 +00001709 monitor_printf(mon, "no function defined to set boot device list for "
1710 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001711 }
1712}
1713
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001714/**
1715 * do_system_reset(): Issue a machine reset
1716 */
1717static void do_system_reset(Monitor *mon, const QDict *qdict,
1718 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001719{
1720 qemu_system_reset_request();
1721}
1722
Luiz Capitulino43076662009-10-07 13:41:59 -03001723/**
1724 * do_system_powerdown(): Issue a machine powerdown
1725 */
1726static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1727 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001728{
1729 qemu_system_powerdown_request();
1730}
1731
bellardb86bda52004-09-18 19:32:46 +00001732#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001733static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001734{
aliguori376253e2009-03-05 23:01:23 +00001735 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1736 addr,
1737 pte & mask,
1738 pte & PG_GLOBAL_MASK ? 'G' : '-',
1739 pte & PG_PSE_MASK ? 'P' : '-',
1740 pte & PG_DIRTY_MASK ? 'D' : '-',
1741 pte & PG_ACCESSED_MASK ? 'A' : '-',
1742 pte & PG_PCD_MASK ? 'C' : '-',
1743 pte & PG_PWT_MASK ? 'T' : '-',
1744 pte & PG_USER_MASK ? 'U' : '-',
1745 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001746}
1747
aliguori376253e2009-03-05 23:01:23 +00001748static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001749{
bellard6a00d602005-11-21 23:25:50 +00001750 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001751 int l1, l2;
1752 uint32_t pgd, pde, pte;
1753
bellard6a00d602005-11-21 23:25:50 +00001754 env = mon_get_cpu();
1755 if (!env)
1756 return;
1757
bellardb86bda52004-09-18 19:32:46 +00001758 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001759 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001760 return;
1761 }
1762 pgd = env->cr[3] & ~0xfff;
1763 for(l1 = 0; l1 < 1024; l1++) {
1764 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1765 pde = le32_to_cpu(pde);
1766 if (pde & PG_PRESENT_MASK) {
1767 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001768 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001769 } else {
1770 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001771 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001772 (uint8_t *)&pte, 4);
1773 pte = le32_to_cpu(pte);
1774 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001775 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001776 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001777 ~0xfff);
1778 }
1779 }
1780 }
1781 }
1782 }
1783}
1784
aliguori376253e2009-03-05 23:01:23 +00001785static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001786 uint32_t end, int prot)
1787{
bellard9746b152004-11-11 18:30:24 +00001788 int prot1;
1789 prot1 = *plast_prot;
1790 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001791 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001792 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1793 *pstart, end, end - *pstart,
1794 prot1 & PG_USER_MASK ? 'u' : '-',
1795 'r',
1796 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001797 }
1798 if (prot != 0)
1799 *pstart = end;
1800 else
1801 *pstart = -1;
1802 *plast_prot = prot;
1803 }
1804}
1805
aliguori376253e2009-03-05 23:01:23 +00001806static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001807{
bellard6a00d602005-11-21 23:25:50 +00001808 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001809 int l1, l2, prot, last_prot;
1810 uint32_t pgd, pde, pte, start, end;
1811
bellard6a00d602005-11-21 23:25:50 +00001812 env = mon_get_cpu();
1813 if (!env)
1814 return;
1815
bellardb86bda52004-09-18 19:32:46 +00001816 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001817 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001818 return;
1819 }
1820 pgd = env->cr[3] & ~0xfff;
1821 last_prot = 0;
1822 start = -1;
1823 for(l1 = 0; l1 < 1024; l1++) {
1824 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1825 pde = le32_to_cpu(pde);
1826 end = l1 << 22;
1827 if (pde & PG_PRESENT_MASK) {
1828 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1829 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001830 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001831 } else {
1832 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001833 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001834 (uint8_t *)&pte, 4);
1835 pte = le32_to_cpu(pte);
1836 end = (l1 << 22) + (l2 << 12);
1837 if (pte & PG_PRESENT_MASK) {
1838 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1839 } else {
1840 prot = 0;
1841 }
aliguori376253e2009-03-05 23:01:23 +00001842 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001843 }
1844 }
1845 } else {
1846 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001847 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001848 }
1849 }
1850}
1851#endif
1852
aurel327c664e22009-03-03 06:12:22 +00001853#if defined(TARGET_SH4)
1854
aliguori376253e2009-03-05 23:01:23 +00001855static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001856{
aliguori376253e2009-03-05 23:01:23 +00001857 monitor_printf(mon, " tlb%i:\t"
1858 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1859 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1860 "dirty=%hhu writethrough=%hhu\n",
1861 idx,
1862 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1863 tlb->v, tlb->sh, tlb->c, tlb->pr,
1864 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001865}
1866
aliguori376253e2009-03-05 23:01:23 +00001867static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001868{
1869 CPUState *env = mon_get_cpu();
1870 int i;
1871
aliguori376253e2009-03-05 23:01:23 +00001872 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001873 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001874 print_tlb (mon, i, &env->itlb[i]);
1875 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001876 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001877 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001878}
1879
1880#endif
1881
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001882static void do_info_kvm_print(Monitor *mon, const QObject *data)
1883{
1884 QDict *qdict;
1885
1886 qdict = qobject_to_qdict(data);
1887
1888 monitor_printf(mon, "kvm support: ");
1889 if (qdict_get_bool(qdict, "present")) {
1890 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
1891 "enabled" : "disabled");
1892 } else {
1893 monitor_printf(mon, "not compiled\n");
1894 }
1895}
1896
1897/**
1898 * do_info_kvm(): Show KVM information
1899 *
1900 * Return a QDict with the following information:
1901 *
1902 * - "enabled": true if KVM support is enabled, false otherwise
1903 * - "present": true if QEMU has KVM support, false otherwise
1904 *
1905 * Example:
1906 *
1907 * { "enabled": true, "present": true }
1908 */
1909static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00001910{
1911#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001912 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
1913 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00001914#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001915 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00001916#endif
1917}
1918
aliguori030ea372009-04-21 22:30:47 +00001919static void do_info_numa(Monitor *mon)
1920{
aliguorib28b6232009-04-22 20:20:29 +00001921 int i;
aliguori030ea372009-04-21 22:30:47 +00001922 CPUState *env;
1923
1924 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1925 for (i = 0; i < nb_numa_nodes; i++) {
1926 monitor_printf(mon, "node %d cpus:", i);
1927 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1928 if (env->numa_node == i) {
1929 monitor_printf(mon, " %d", env->cpu_index);
1930 }
1931 }
1932 monitor_printf(mon, "\n");
1933 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1934 node_mem[i] >> 20);
1935 }
1936}
1937
bellard5f1ce942006-02-08 22:40:15 +00001938#ifdef CONFIG_PROFILER
1939
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001940int64_t qemu_time;
1941int64_t dev_time;
1942
aliguori376253e2009-03-05 23:01:23 +00001943static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001944{
1945 int64_t total;
1946 total = qemu_time;
1947 if (total == 0)
1948 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001949 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001950 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001951 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001952 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001953 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001954 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001955}
1956#else
aliguori376253e2009-03-05 23:01:23 +00001957static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001958{
aliguori376253e2009-03-05 23:01:23 +00001959 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001960}
1961#endif
1962
bellardec36b692006-07-16 18:57:03 +00001963/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001964static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001965
aliguori376253e2009-03-05 23:01:23 +00001966static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001967{
1968 int i;
1969 CaptureState *s;
1970
1971 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001972 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001973 s->ops.info (s->opaque);
1974 }
1975}
1976
Blue Swirl23130862009-06-06 08:22:04 +00001977#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001978static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001979{
1980 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001981 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001982 CaptureState *s;
1983
1984 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1985 if (i == n) {
1986 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001987 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00001988 qemu_free (s);
1989 return;
1990 }
1991 }
1992}
1993
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001994static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001995{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001996 const char *path = qdict_get_str(qdict, "path");
1997 int has_freq = qdict_haskey(qdict, "freq");
1998 int freq = qdict_get_try_int(qdict, "freq", -1);
1999 int has_bits = qdict_haskey(qdict, "bits");
2000 int bits = qdict_get_try_int(qdict, "bits", -1);
2001 int has_channels = qdict_haskey(qdict, "nchannels");
2002 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002003 CaptureState *s;
2004
2005 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002006
2007 freq = has_freq ? freq : 44100;
2008 bits = has_bits ? bits : 16;
2009 nchannels = has_channels ? nchannels : 2;
2010
2011 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002012 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002013 qemu_free (s);
2014 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002015 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002016}
2017#endif
2018
aurel32dc1c0b72008-04-27 23:52:12 +00002019#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002020static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002021{
2022 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002023 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002024
2025 for (env = first_cpu; env != NULL; env = env->next_cpu)
2026 if (env->cpu_index == cpu_index) {
2027 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2028 break;
2029 }
2030}
2031#endif
2032
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002033static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002034{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002035 QDict *qdict;
2036
2037 qdict = qobject_to_qdict(data);
2038
2039 monitor_printf(mon, "VM status: ");
2040 if (qdict_get_bool(qdict, "running")) {
2041 monitor_printf(mon, "running");
2042 if (qdict_get_bool(qdict, "singlestep")) {
2043 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002044 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002045 } else {
2046 monitor_printf(mon, "paused");
2047 }
2048
2049 monitor_printf(mon, "\n");
2050}
2051
2052/**
2053 * do_info_status(): VM status
2054 *
2055 * Return a QDict with the following information:
2056 *
2057 * - "running": true if the VM is running, or false if it is paused
2058 * - "singlestep": true if the VM is in single step mode, false otherwise
2059 *
2060 * Example:
2061 *
2062 * { "running": true, "singlestep": false }
2063 */
2064static void do_info_status(Monitor *mon, QObject **ret_data)
2065{
2066 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2067 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002068}
2069
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002070static ram_addr_t balloon_get_value(void)
2071{
2072 ram_addr_t actual;
2073
2074 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2075 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2076 return 0;
2077 }
2078
2079 actual = qemu_balloon_status();
2080 if (actual == 0) {
2081 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2082 return 0;
2083 }
2084
2085 return actual;
2086}
2087
Luiz Capitulino83fb1de2009-10-07 13:42:01 -03002088/**
2089 * do_balloon(): Request VM to change its memory allocation
2090 */
2091static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00002092{
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002093 if (balloon_get_value()) {
2094 /* ballooning is active */
Luiz Capitulino3b0bd6e2009-12-18 13:25:05 -02002095 qemu_balloon(qdict_get_int(qdict, "value"));
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002096 }
aliguoridf751fa2008-12-04 20:19:35 +00002097}
2098
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002099static void monitor_print_balloon(Monitor *mon, const QObject *data)
2100{
Luiz Capitulino7f179672009-12-10 17:15:55 -02002101 QDict *qdict;
2102
2103 qdict = qobject_to_qdict(data);
2104
2105 monitor_printf(mon, "balloon: actual=%" PRId64 "\n",
2106 qdict_get_int(qdict, "balloon") >> 20);
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002107}
2108
2109/**
2110 * do_info_balloon(): Balloon information
Luiz Capitulino7f179672009-12-10 17:15:55 -02002111 *
2112 * Return a QDict with the following information:
2113 *
2114 * - "balloon": current balloon value in bytes
2115 *
2116 * Example:
2117 *
2118 * { "balloon": 1073741824 }
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002119 */
2120static void do_info_balloon(Monitor *mon, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00002121{
Anthony Liguoric227f092009-10-01 16:12:16 -05002122 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00002123
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002124 actual = balloon_get_value();
2125 if (actual != 0) {
Luiz Capitulino7f179672009-12-10 17:15:55 -02002126 *ret_data = qobject_from_jsonf("{ 'balloon': %" PRId64 "}",
2127 (int64_t) actual);
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002128 }
aliguoridf751fa2008-12-04 20:19:35 +00002129}
2130
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002131static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002132{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002133 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002134
aliguori76655d62009-03-06 20:27:37 +00002135 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002136 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002137 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002138 return acl;
2139}
aliguori76655d62009-03-06 20:27:37 +00002140
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002141static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002142{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002143 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002144 qemu_acl *acl = find_acl(mon, aclname);
2145 qemu_acl_entry *entry;
2146 int i = 0;
2147
2148 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002149 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002150 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002151 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002152 i++;
2153 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002154 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002155 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002156 }
2157}
2158
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002159static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002160{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002161 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002162 qemu_acl *acl = find_acl(mon, aclname);
2163
2164 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002165 qemu_acl_reset(acl);
2166 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002167 }
2168}
aliguori76655d62009-03-06 20:27:37 +00002169
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002170static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002171{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002172 const char *aclname = qdict_get_str(qdict, "aclname");
2173 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002174 qemu_acl *acl = find_acl(mon, aclname);
2175
2176 if (acl) {
2177 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002178 acl->defaultDeny = 0;
2179 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002180 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002181 acl->defaultDeny = 1;
2182 monitor_printf(mon, "acl: policy set to 'deny'\n");
2183 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002184 monitor_printf(mon, "acl: unknown policy '%s', "
2185 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002186 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002187 }
2188}
aliguori76655d62009-03-06 20:27:37 +00002189
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002190static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002191{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002192 const char *aclname = qdict_get_str(qdict, "aclname");
2193 const char *match = qdict_get_str(qdict, "match");
2194 const char *policy = qdict_get_str(qdict, "policy");
2195 int has_index = qdict_haskey(qdict, "index");
2196 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002197 qemu_acl *acl = find_acl(mon, aclname);
2198 int deny, ret;
2199
2200 if (acl) {
2201 if (strcmp(policy, "allow") == 0) {
2202 deny = 0;
2203 } else if (strcmp(policy, "deny") == 0) {
2204 deny = 1;
2205 } else {
2206 monitor_printf(mon, "acl: unknown policy '%s', "
2207 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002208 return;
2209 }
aliguori28a76be2009-03-06 20:27:40 +00002210 if (has_index)
2211 ret = qemu_acl_insert(acl, deny, match, index);
2212 else
2213 ret = qemu_acl_append(acl, deny, match);
2214 if (ret < 0)
2215 monitor_printf(mon, "acl: unable to add acl entry\n");
2216 else
2217 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002218 }
2219}
aliguori76655d62009-03-06 20:27:37 +00002220
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002221static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002222{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002223 const char *aclname = qdict_get_str(qdict, "aclname");
2224 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002225 qemu_acl *acl = find_acl(mon, aclname);
2226 int ret;
aliguori76655d62009-03-06 20:27:37 +00002227
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002228 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002229 ret = qemu_acl_remove(acl, match);
2230 if (ret < 0)
2231 monitor_printf(mon, "acl: no matching acl entry\n");
2232 else
2233 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002234 }
2235}
2236
Huang Ying79c4f6b2009-06-23 10:05:14 +08002237#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002238static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002239{
2240 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002241 int cpu_index = qdict_get_int(qdict, "cpu_index");
2242 int bank = qdict_get_int(qdict, "bank");
2243 uint64_t status = qdict_get_int(qdict, "status");
2244 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2245 uint64_t addr = qdict_get_int(qdict, "addr");
2246 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002247
2248 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2249 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2250 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2251 break;
2252 }
2253}
2254#endif
2255
Luiz Capitulinof0d60002009-10-16 12:23:50 -03002256static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002257{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002258 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002259 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002260 int fd;
2261
2262 fd = qemu_chr_get_msgfd(mon->chr);
2263 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002264 qemu_error_new(QERR_FD_NOT_SUPPLIED);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002265 return;
2266 }
2267
2268 if (qemu_isdigit(fdname[0])) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002269 qemu_error_new(QERR_INVALID_PARAMETER, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002270 return;
2271 }
2272
2273 fd = dup(fd);
2274 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002275 if (errno == EMFILE)
2276 qemu_error_new(QERR_TOO_MANY_FILES);
2277 else
2278 qemu_error_new(QERR_UNDEFINED_ERROR);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002279 return;
2280 }
2281
Blue Swirl72cf2d42009-09-12 07:36:22 +00002282 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002283 if (strcmp(monfd->name, fdname) != 0) {
2284 continue;
2285 }
2286
2287 close(monfd->fd);
2288 monfd->fd = fd;
2289 return;
2290 }
2291
Anthony Liguoric227f092009-10-01 16:12:16 -05002292 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002293 monfd->name = qemu_strdup(fdname);
2294 monfd->fd = fd;
2295
Blue Swirl72cf2d42009-09-12 07:36:22 +00002296 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002297}
2298
Luiz Capitulino18f3a512009-10-16 12:23:51 -03002299static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002300{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002301 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002302 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002303
Blue Swirl72cf2d42009-09-12 07:36:22 +00002304 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002305 if (strcmp(monfd->name, fdname) != 0) {
2306 continue;
2307 }
2308
Blue Swirl72cf2d42009-09-12 07:36:22 +00002309 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002310 close(monfd->fd);
2311 qemu_free(monfd->name);
2312 qemu_free(monfd);
2313 return;
2314 }
2315
Markus Armbruster063c1a02009-12-07 21:37:11 +01002316 qemu_error_new(QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002317}
2318
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002319static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002320{
2321 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002322 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002323
2324 vm_stop(0);
2325
Juan Quintela05f24012009-08-20 19:42:22 +02002326 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002327 vm_start();
2328}
2329
Mark McLoughlin7768e042009-07-22 09:11:41 +01002330int monitor_get_fd(Monitor *mon, const char *fdname)
2331{
Anthony Liguoric227f092009-10-01 16:12:16 -05002332 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002333
Blue Swirl72cf2d42009-09-12 07:36:22 +00002334 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002335 int fd;
2336
2337 if (strcmp(monfd->name, fdname) != 0) {
2338 continue;
2339 }
2340
2341 fd = monfd->fd;
2342
2343 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002344 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002345 qemu_free(monfd->name);
2346 qemu_free(monfd);
2347
2348 return fd;
2349 }
2350
2351 return -1;
2352}
2353
Anthony Liguoric227f092009-10-01 16:12:16 -05002354static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002355#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002356 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002357};
2358
Blue Swirl23130862009-06-06 08:22:04 +00002359/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002360static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002361 {
2362 .name = "version",
2363 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002364 .params = "",
2365 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002366 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002367 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002368 },
2369 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002370 .name = "commands",
2371 .args_type = "",
2372 .params = "",
2373 .help = "list QMP available commands",
2374 .user_print = monitor_user_noop,
2375 .mhandler.info_new = do_info_commands,
2376 },
2377 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002378 .name = "network",
2379 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002380 .params = "",
2381 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002382 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002383 },
2384 {
2385 .name = "chardev",
2386 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002387 .params = "",
2388 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002389 .user_print = qemu_chr_info_print,
2390 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002391 },
2392 {
2393 .name = "block",
2394 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002395 .params = "",
2396 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002397 .user_print = bdrv_info_print,
2398 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002399 },
2400 {
2401 .name = "blockstats",
2402 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002403 .params = "",
2404 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002405 .user_print = bdrv_stats_print,
2406 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002407 },
2408 {
2409 .name = "registers",
2410 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002411 .params = "",
2412 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002413 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002414 },
2415 {
2416 .name = "cpus",
2417 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002418 .params = "",
2419 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002420 .user_print = monitor_print_cpus,
2421 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002422 },
2423 {
2424 .name = "history",
2425 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002426 .params = "",
2427 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002428 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002429 },
2430 {
2431 .name = "irq",
2432 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002433 .params = "",
2434 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002435 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002436 },
2437 {
2438 .name = "pic",
2439 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002440 .params = "",
2441 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002442 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002443 },
2444 {
2445 .name = "pci",
2446 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002447 .params = "",
2448 .help = "show PCI info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002449 .mhandler.info = pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002450 },
aurel327c664e22009-03-03 06:12:22 +00002451#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002452 {
2453 .name = "tlb",
2454 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002455 .params = "",
2456 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002457 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002458 },
aurel327c664e22009-03-03 06:12:22 +00002459#endif
2460#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002461 {
2462 .name = "mem",
2463 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002464 .params = "",
2465 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002466 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002467 },
2468 {
2469 .name = "hpet",
2470 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002471 .params = "",
2472 .help = "show state of HPET",
Luiz Capitulino14f07202009-12-10 17:16:02 -02002473 .user_print = do_info_hpet_print,
2474 .mhandler.info_new = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002475 },
bellardb86bda52004-09-18 19:32:46 +00002476#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002477 {
2478 .name = "jit",
2479 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002480 .params = "",
2481 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002482 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002483 },
2484 {
2485 .name = "kvm",
2486 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002487 .params = "",
2488 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002489 .user_print = do_info_kvm_print,
2490 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002491 },
2492 {
2493 .name = "numa",
2494 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002495 .params = "",
2496 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002497 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002498 },
2499 {
2500 .name = "usb",
2501 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002502 .params = "",
2503 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002504 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002505 },
2506 {
2507 .name = "usbhost",
2508 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002509 .params = "",
2510 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002511 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002512 },
2513 {
2514 .name = "profile",
2515 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002516 .params = "",
2517 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002518 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002519 },
2520 {
2521 .name = "capture",
2522 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002523 .params = "",
2524 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002525 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002526 },
2527 {
2528 .name = "snapshots",
2529 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002530 .params = "",
2531 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002532 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002533 },
2534 {
2535 .name = "status",
2536 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002537 .params = "",
2538 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002539 .user_print = do_info_status_print,
2540 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002541 },
2542 {
2543 .name = "pcmcia",
2544 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002545 .params = "",
2546 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002547 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002548 },
2549 {
2550 .name = "mice",
2551 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002552 .params = "",
2553 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002554 .user_print = do_info_mice_print,
2555 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002556 },
2557 {
2558 .name = "vnc",
2559 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002560 .params = "",
2561 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002562 .user_print = do_info_vnc_print,
2563 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002564 },
2565 {
2566 .name = "name",
2567 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002568 .params = "",
2569 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002570 .user_print = do_info_name_print,
2571 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002572 },
2573 {
2574 .name = "uuid",
2575 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002576 .params = "",
2577 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002578 .user_print = do_info_uuid_print,
2579 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 },
j_mayer76a66252007-03-07 08:32:30 +00002581#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002582 {
2583 .name = "cpustats",
2584 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002585 .params = "",
2586 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002587 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002588 },
j_mayer76a66252007-03-07 08:32:30 +00002589#endif
blueswir131a60e22007-10-26 18:42:59 +00002590#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002591 {
2592 .name = "usernet",
2593 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002594 .params = "",
2595 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002596 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002597 },
blueswir131a60e22007-10-26 18:42:59 +00002598#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002599 {
2600 .name = "migrate",
2601 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002602 .params = "",
2603 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002604 .user_print = do_info_migrate_print,
2605 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002606 },
2607 {
2608 .name = "balloon",
2609 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002610 .params = "",
2611 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002612 .user_print = monitor_print_balloon,
2613 .mhandler.info_new = do_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002614 },
2615 {
2616 .name = "qtree",
2617 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002618 .params = "",
2619 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002620 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002621 },
2622 {
2623 .name = "qdm",
2624 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002625 .params = "",
2626 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002627 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002628 },
2629 {
2630 .name = "roms",
2631 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002632 .params = "",
2633 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002634 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002635 },
2636 {
2637 .name = NULL,
2638 },
bellard9dc39cb2004-03-14 21:38:27 +00002639};
2640
bellard9307c4c2004-04-04 12:57:25 +00002641/*******************************************************************/
2642
2643static const char *pch;
2644static jmp_buf expr_env;
2645
bellard92a31b12005-02-10 22:00:52 +00002646#define MD_TLONG 0
2647#define MD_I32 1
2648
bellard9307c4c2004-04-04 12:57:25 +00002649typedef struct MonitorDef {
2650 const char *name;
2651 int offset;
blueswir18662d652008-10-02 18:32:44 +00002652 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002653 int type;
bellard9307c4c2004-04-04 12:57:25 +00002654} MonitorDef;
2655
bellard57206fd2004-04-25 18:54:52 +00002656#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002657static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002658{
bellard6a00d602005-11-21 23:25:50 +00002659 CPUState *env = mon_get_cpu();
2660 if (!env)
2661 return 0;
2662 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002663}
2664#endif
2665
bellarda541f292004-04-12 20:39:29 +00002666#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002667static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002668{
bellard6a00d602005-11-21 23:25:50 +00002669 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002670 unsigned int u;
2671 int i;
2672
bellard6a00d602005-11-21 23:25:50 +00002673 if (!env)
2674 return 0;
2675
bellarda541f292004-04-12 20:39:29 +00002676 u = 0;
2677 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002678 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002679
2680 return u;
2681}
2682
blueswir18662d652008-10-02 18:32:44 +00002683static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002684{
bellard6a00d602005-11-21 23:25:50 +00002685 CPUState *env = mon_get_cpu();
2686 if (!env)
2687 return 0;
j_mayer0411a972007-10-25 21:35:50 +00002688 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002689}
2690
blueswir18662d652008-10-02 18:32:44 +00002691static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002692{
bellard6a00d602005-11-21 23:25:50 +00002693 CPUState *env = mon_get_cpu();
2694 if (!env)
2695 return 0;
aurel323d7b4172008-10-21 11:28:46 +00002696 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002697}
bellard9fddaa02004-05-21 12:59:32 +00002698
blueswir18662d652008-10-02 18:32:44 +00002699static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002700{
bellard6a00d602005-11-21 23:25:50 +00002701 CPUState *env = mon_get_cpu();
2702 if (!env)
2703 return 0;
2704 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002705}
2706
blueswir18662d652008-10-02 18:32:44 +00002707static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002708{
bellard6a00d602005-11-21 23:25:50 +00002709 CPUState *env = mon_get_cpu();
2710 if (!env)
2711 return 0;
2712 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002713}
2714
blueswir18662d652008-10-02 18:32:44 +00002715static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002716{
bellard6a00d602005-11-21 23:25:50 +00002717 CPUState *env = mon_get_cpu();
2718 if (!env)
2719 return 0;
2720 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002721}
bellarda541f292004-04-12 20:39:29 +00002722#endif
2723
bellarde95c8d52004-09-30 22:22:08 +00002724#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002725#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002726static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002727{
bellard6a00d602005-11-21 23:25:50 +00002728 CPUState *env = mon_get_cpu();
2729 if (!env)
2730 return 0;
2731 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002732}
bellard7b936c02005-10-30 17:05:13 +00002733#endif
bellarde95c8d52004-09-30 22:22:08 +00002734
blueswir18662d652008-10-02 18:32:44 +00002735static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002736{
bellard6a00d602005-11-21 23:25:50 +00002737 CPUState *env = mon_get_cpu();
2738 if (!env)
2739 return 0;
2740 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002741}
2742#endif
2743
blueswir18662d652008-10-02 18:32:44 +00002744static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002745#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002746
2747#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002748 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002749 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002750 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002751
bellard9307c4c2004-04-04 12:57:25 +00002752 { "eax", offsetof(CPUState, regs[0]) },
2753 { "ecx", offsetof(CPUState, regs[1]) },
2754 { "edx", offsetof(CPUState, regs[2]) },
2755 { "ebx", offsetof(CPUState, regs[3]) },
2756 { "esp|sp", offsetof(CPUState, regs[4]) },
2757 { "ebp|fp", offsetof(CPUState, regs[5]) },
2758 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002759 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002760#ifdef TARGET_X86_64
2761 { "r8", offsetof(CPUState, regs[8]) },
2762 { "r9", offsetof(CPUState, regs[9]) },
2763 { "r10", offsetof(CPUState, regs[10]) },
2764 { "r11", offsetof(CPUState, regs[11]) },
2765 { "r12", offsetof(CPUState, regs[12]) },
2766 { "r13", offsetof(CPUState, regs[13]) },
2767 { "r14", offsetof(CPUState, regs[14]) },
2768 { "r15", offsetof(CPUState, regs[15]) },
2769#endif
bellard9307c4c2004-04-04 12:57:25 +00002770 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002771 { "eip", offsetof(CPUState, eip) },
2772 SEG("cs", R_CS)
2773 SEG("ds", R_DS)
2774 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002775 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002776 SEG("fs", R_FS)
2777 SEG("gs", R_GS)
2778 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002779#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002780 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002781 { "r0", offsetof(CPUState, gpr[0]) },
2782 { "r1", offsetof(CPUState, gpr[1]) },
2783 { "r2", offsetof(CPUState, gpr[2]) },
2784 { "r3", offsetof(CPUState, gpr[3]) },
2785 { "r4", offsetof(CPUState, gpr[4]) },
2786 { "r5", offsetof(CPUState, gpr[5]) },
2787 { "r6", offsetof(CPUState, gpr[6]) },
2788 { "r7", offsetof(CPUState, gpr[7]) },
2789 { "r8", offsetof(CPUState, gpr[8]) },
2790 { "r9", offsetof(CPUState, gpr[9]) },
2791 { "r10", offsetof(CPUState, gpr[10]) },
2792 { "r11", offsetof(CPUState, gpr[11]) },
2793 { "r12", offsetof(CPUState, gpr[12]) },
2794 { "r13", offsetof(CPUState, gpr[13]) },
2795 { "r14", offsetof(CPUState, gpr[14]) },
2796 { "r15", offsetof(CPUState, gpr[15]) },
2797 { "r16", offsetof(CPUState, gpr[16]) },
2798 { "r17", offsetof(CPUState, gpr[17]) },
2799 { "r18", offsetof(CPUState, gpr[18]) },
2800 { "r19", offsetof(CPUState, gpr[19]) },
2801 { "r20", offsetof(CPUState, gpr[20]) },
2802 { "r21", offsetof(CPUState, gpr[21]) },
2803 { "r22", offsetof(CPUState, gpr[22]) },
2804 { "r23", offsetof(CPUState, gpr[23]) },
2805 { "r24", offsetof(CPUState, gpr[24]) },
2806 { "r25", offsetof(CPUState, gpr[25]) },
2807 { "r26", offsetof(CPUState, gpr[26]) },
2808 { "r27", offsetof(CPUState, gpr[27]) },
2809 { "r28", offsetof(CPUState, gpr[28]) },
2810 { "r29", offsetof(CPUState, gpr[29]) },
2811 { "r30", offsetof(CPUState, gpr[30]) },
2812 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002813 /* Floating point registers */
2814 { "f0", offsetof(CPUState, fpr[0]) },
2815 { "f1", offsetof(CPUState, fpr[1]) },
2816 { "f2", offsetof(CPUState, fpr[2]) },
2817 { "f3", offsetof(CPUState, fpr[3]) },
2818 { "f4", offsetof(CPUState, fpr[4]) },
2819 { "f5", offsetof(CPUState, fpr[5]) },
2820 { "f6", offsetof(CPUState, fpr[6]) },
2821 { "f7", offsetof(CPUState, fpr[7]) },
2822 { "f8", offsetof(CPUState, fpr[8]) },
2823 { "f9", offsetof(CPUState, fpr[9]) },
2824 { "f10", offsetof(CPUState, fpr[10]) },
2825 { "f11", offsetof(CPUState, fpr[11]) },
2826 { "f12", offsetof(CPUState, fpr[12]) },
2827 { "f13", offsetof(CPUState, fpr[13]) },
2828 { "f14", offsetof(CPUState, fpr[14]) },
2829 { "f15", offsetof(CPUState, fpr[15]) },
2830 { "f16", offsetof(CPUState, fpr[16]) },
2831 { "f17", offsetof(CPUState, fpr[17]) },
2832 { "f18", offsetof(CPUState, fpr[18]) },
2833 { "f19", offsetof(CPUState, fpr[19]) },
2834 { "f20", offsetof(CPUState, fpr[20]) },
2835 { "f21", offsetof(CPUState, fpr[21]) },
2836 { "f22", offsetof(CPUState, fpr[22]) },
2837 { "f23", offsetof(CPUState, fpr[23]) },
2838 { "f24", offsetof(CPUState, fpr[24]) },
2839 { "f25", offsetof(CPUState, fpr[25]) },
2840 { "f26", offsetof(CPUState, fpr[26]) },
2841 { "f27", offsetof(CPUState, fpr[27]) },
2842 { "f28", offsetof(CPUState, fpr[28]) },
2843 { "f29", offsetof(CPUState, fpr[29]) },
2844 { "f30", offsetof(CPUState, fpr[30]) },
2845 { "f31", offsetof(CPUState, fpr[31]) },
2846 { "fpscr", offsetof(CPUState, fpscr) },
2847 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002848 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002849 { "lr", offsetof(CPUState, lr) },
2850 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002851 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002852 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002853 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002854 { "msr", 0, &monitor_get_msr, },
2855 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002856 { "tbu", 0, &monitor_get_tbu, },
2857 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002858#if defined(TARGET_PPC64)
2859 /* Address space register */
2860 { "asr", offsetof(CPUState, asr) },
2861#endif
2862 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002863 { "sdr1", offsetof(CPUState, sdr1) },
2864 { "sr0", offsetof(CPUState, sr[0]) },
2865 { "sr1", offsetof(CPUState, sr[1]) },
2866 { "sr2", offsetof(CPUState, sr[2]) },
2867 { "sr3", offsetof(CPUState, sr[3]) },
2868 { "sr4", offsetof(CPUState, sr[4]) },
2869 { "sr5", offsetof(CPUState, sr[5]) },
2870 { "sr6", offsetof(CPUState, sr[6]) },
2871 { "sr7", offsetof(CPUState, sr[7]) },
2872 { "sr8", offsetof(CPUState, sr[8]) },
2873 { "sr9", offsetof(CPUState, sr[9]) },
2874 { "sr10", offsetof(CPUState, sr[10]) },
2875 { "sr11", offsetof(CPUState, sr[11]) },
2876 { "sr12", offsetof(CPUState, sr[12]) },
2877 { "sr13", offsetof(CPUState, sr[13]) },
2878 { "sr14", offsetof(CPUState, sr[14]) },
2879 { "sr15", offsetof(CPUState, sr[15]) },
2880 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002881#elif defined(TARGET_SPARC)
2882 { "g0", offsetof(CPUState, gregs[0]) },
2883 { "g1", offsetof(CPUState, gregs[1]) },
2884 { "g2", offsetof(CPUState, gregs[2]) },
2885 { "g3", offsetof(CPUState, gregs[3]) },
2886 { "g4", offsetof(CPUState, gregs[4]) },
2887 { "g5", offsetof(CPUState, gregs[5]) },
2888 { "g6", offsetof(CPUState, gregs[6]) },
2889 { "g7", offsetof(CPUState, gregs[7]) },
2890 { "o0", 0, monitor_get_reg },
2891 { "o1", 1, monitor_get_reg },
2892 { "o2", 2, monitor_get_reg },
2893 { "o3", 3, monitor_get_reg },
2894 { "o4", 4, monitor_get_reg },
2895 { "o5", 5, monitor_get_reg },
2896 { "o6", 6, monitor_get_reg },
2897 { "o7", 7, monitor_get_reg },
2898 { "l0", 8, monitor_get_reg },
2899 { "l1", 9, monitor_get_reg },
2900 { "l2", 10, monitor_get_reg },
2901 { "l3", 11, monitor_get_reg },
2902 { "l4", 12, monitor_get_reg },
2903 { "l5", 13, monitor_get_reg },
2904 { "l6", 14, monitor_get_reg },
2905 { "l7", 15, monitor_get_reg },
2906 { "i0", 16, monitor_get_reg },
2907 { "i1", 17, monitor_get_reg },
2908 { "i2", 18, monitor_get_reg },
2909 { "i3", 19, monitor_get_reg },
2910 { "i4", 20, monitor_get_reg },
2911 { "i5", 21, monitor_get_reg },
2912 { "i6", 22, monitor_get_reg },
2913 { "i7", 23, monitor_get_reg },
2914 { "pc", offsetof(CPUState, pc) },
2915 { "npc", offsetof(CPUState, npc) },
2916 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002917#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002918 { "psr", 0, &monitor_get_psr, },
2919 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002920#endif
bellarde95c8d52004-09-30 22:22:08 +00002921 { "tbr", offsetof(CPUState, tbr) },
2922 { "fsr", offsetof(CPUState, fsr) },
2923 { "f0", offsetof(CPUState, fpr[0]) },
2924 { "f1", offsetof(CPUState, fpr[1]) },
2925 { "f2", offsetof(CPUState, fpr[2]) },
2926 { "f3", offsetof(CPUState, fpr[3]) },
2927 { "f4", offsetof(CPUState, fpr[4]) },
2928 { "f5", offsetof(CPUState, fpr[5]) },
2929 { "f6", offsetof(CPUState, fpr[6]) },
2930 { "f7", offsetof(CPUState, fpr[7]) },
2931 { "f8", offsetof(CPUState, fpr[8]) },
2932 { "f9", offsetof(CPUState, fpr[9]) },
2933 { "f10", offsetof(CPUState, fpr[10]) },
2934 { "f11", offsetof(CPUState, fpr[11]) },
2935 { "f12", offsetof(CPUState, fpr[12]) },
2936 { "f13", offsetof(CPUState, fpr[13]) },
2937 { "f14", offsetof(CPUState, fpr[14]) },
2938 { "f15", offsetof(CPUState, fpr[15]) },
2939 { "f16", offsetof(CPUState, fpr[16]) },
2940 { "f17", offsetof(CPUState, fpr[17]) },
2941 { "f18", offsetof(CPUState, fpr[18]) },
2942 { "f19", offsetof(CPUState, fpr[19]) },
2943 { "f20", offsetof(CPUState, fpr[20]) },
2944 { "f21", offsetof(CPUState, fpr[21]) },
2945 { "f22", offsetof(CPUState, fpr[22]) },
2946 { "f23", offsetof(CPUState, fpr[23]) },
2947 { "f24", offsetof(CPUState, fpr[24]) },
2948 { "f25", offsetof(CPUState, fpr[25]) },
2949 { "f26", offsetof(CPUState, fpr[26]) },
2950 { "f27", offsetof(CPUState, fpr[27]) },
2951 { "f28", offsetof(CPUState, fpr[28]) },
2952 { "f29", offsetof(CPUState, fpr[29]) },
2953 { "f30", offsetof(CPUState, fpr[30]) },
2954 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002955#ifdef TARGET_SPARC64
2956 { "f32", offsetof(CPUState, fpr[32]) },
2957 { "f34", offsetof(CPUState, fpr[34]) },
2958 { "f36", offsetof(CPUState, fpr[36]) },
2959 { "f38", offsetof(CPUState, fpr[38]) },
2960 { "f40", offsetof(CPUState, fpr[40]) },
2961 { "f42", offsetof(CPUState, fpr[42]) },
2962 { "f44", offsetof(CPUState, fpr[44]) },
2963 { "f46", offsetof(CPUState, fpr[46]) },
2964 { "f48", offsetof(CPUState, fpr[48]) },
2965 { "f50", offsetof(CPUState, fpr[50]) },
2966 { "f52", offsetof(CPUState, fpr[52]) },
2967 { "f54", offsetof(CPUState, fpr[54]) },
2968 { "f56", offsetof(CPUState, fpr[56]) },
2969 { "f58", offsetof(CPUState, fpr[58]) },
2970 { "f60", offsetof(CPUState, fpr[60]) },
2971 { "f62", offsetof(CPUState, fpr[62]) },
2972 { "asi", offsetof(CPUState, asi) },
2973 { "pstate", offsetof(CPUState, pstate) },
2974 { "cansave", offsetof(CPUState, cansave) },
2975 { "canrestore", offsetof(CPUState, canrestore) },
2976 { "otherwin", offsetof(CPUState, otherwin) },
2977 { "wstate", offsetof(CPUState, wstate) },
2978 { "cleanwin", offsetof(CPUState, cleanwin) },
2979 { "fprs", offsetof(CPUState, fprs) },
2980#endif
bellard9307c4c2004-04-04 12:57:25 +00002981#endif
2982 { NULL },
2983};
2984
aliguori376253e2009-03-05 23:01:23 +00002985static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002986{
aliguori376253e2009-03-05 23:01:23 +00002987 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002988 longjmp(expr_env, 1);
2989}
2990
bellard6a00d602005-11-21 23:25:50 +00002991/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002992static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002993{
blueswir18662d652008-10-02 18:32:44 +00002994 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002995 void *ptr;
2996
bellard9307c4c2004-04-04 12:57:25 +00002997 for(md = monitor_defs; md->name != NULL; md++) {
2998 if (compare_cmd(name, md->name)) {
2999 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003000 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003001 } else {
bellard6a00d602005-11-21 23:25:50 +00003002 CPUState *env = mon_get_cpu();
3003 if (!env)
3004 return -2;
3005 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003006 switch(md->type) {
3007 case MD_I32:
3008 *pval = *(int32_t *)ptr;
3009 break;
3010 case MD_TLONG:
3011 *pval = *(target_long *)ptr;
3012 break;
3013 default:
3014 *pval = 0;
3015 break;
3016 }
bellard9307c4c2004-04-04 12:57:25 +00003017 }
3018 return 0;
3019 }
3020 }
3021 return -1;
3022}
3023
3024static void next(void)
3025{
Blue Swirl660f11b2009-07-31 21:16:51 +00003026 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003027 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003028 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003029 pch++;
3030 }
3031}
3032
aliguori376253e2009-03-05 23:01:23 +00003033static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003034
aliguori376253e2009-03-05 23:01:23 +00003035static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003036{
blueswir1c2efc952007-09-25 17:28:42 +00003037 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003038 char *p;
bellard6a00d602005-11-21 23:25:50 +00003039 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003040
3041 switch(*pch) {
3042 case '+':
3043 next();
aliguori376253e2009-03-05 23:01:23 +00003044 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003045 break;
3046 case '-':
3047 next();
aliguori376253e2009-03-05 23:01:23 +00003048 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003049 break;
3050 case '~':
3051 next();
aliguori376253e2009-03-05 23:01:23 +00003052 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003053 break;
3054 case '(':
3055 next();
aliguori376253e2009-03-05 23:01:23 +00003056 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003057 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003058 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003059 }
3060 next();
3061 break;
bellard81d09122004-07-14 17:21:37 +00003062 case '\'':
3063 pch++;
3064 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003065 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003066 n = *pch;
3067 pch++;
3068 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003069 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003070 next();
3071 break;
bellard9307c4c2004-04-04 12:57:25 +00003072 case '$':
3073 {
3074 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003075 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003076
bellard9307c4c2004-04-04 12:57:25 +00003077 pch++;
3078 q = buf;
3079 while ((*pch >= 'a' && *pch <= 'z') ||
3080 (*pch >= 'A' && *pch <= 'Z') ||
3081 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003082 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003083 if ((q - buf) < sizeof(buf) - 1)
3084 *q++ = *pch;
3085 pch++;
3086 }
blueswir1cd390082008-11-16 13:53:32 +00003087 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003088 pch++;
3089 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003090 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00003091 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00003092 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00003093 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00003094 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00003095 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003096 }
3097 break;
3098 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003099 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003100 n = 0;
3101 break;
3102 default:
blueswir17743e582007-09-24 18:39:04 +00003103#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003104 n = strtoull(pch, &p, 0);
3105#else
bellard9307c4c2004-04-04 12:57:25 +00003106 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003107#endif
bellard9307c4c2004-04-04 12:57:25 +00003108 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003109 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003110 }
3111 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003112 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003113 pch++;
3114 break;
3115 }
3116 return n;
3117}
3118
3119
aliguori376253e2009-03-05 23:01:23 +00003120static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003121{
blueswir1c2efc952007-09-25 17:28:42 +00003122 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003123 int op;
ths3b46e622007-09-17 08:09:54 +00003124
aliguori376253e2009-03-05 23:01:23 +00003125 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003126 for(;;) {
3127 op = *pch;
3128 if (op != '*' && op != '/' && op != '%')
3129 break;
3130 next();
aliguori376253e2009-03-05 23:01:23 +00003131 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003132 switch(op) {
3133 default:
3134 case '*':
3135 val *= val2;
3136 break;
3137 case '/':
3138 case '%':
ths5fafdf22007-09-16 21:08:06 +00003139 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003140 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003141 if (op == '/')
3142 val /= val2;
3143 else
3144 val %= val2;
3145 break;
3146 }
3147 }
3148 return val;
3149}
3150
aliguori376253e2009-03-05 23:01:23 +00003151static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003152{
blueswir1c2efc952007-09-25 17:28:42 +00003153 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003154 int op;
bellard9307c4c2004-04-04 12:57:25 +00003155
aliguori376253e2009-03-05 23:01:23 +00003156 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003157 for(;;) {
3158 op = *pch;
3159 if (op != '&' && op != '|' && op != '^')
3160 break;
3161 next();
aliguori376253e2009-03-05 23:01:23 +00003162 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003163 switch(op) {
3164 default:
3165 case '&':
3166 val &= val2;
3167 break;
3168 case '|':
3169 val |= val2;
3170 break;
3171 case '^':
3172 val ^= val2;
3173 break;
3174 }
3175 }
3176 return val;
3177}
3178
aliguori376253e2009-03-05 23:01:23 +00003179static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003180{
blueswir1c2efc952007-09-25 17:28:42 +00003181 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003182 int op;
bellard9307c4c2004-04-04 12:57:25 +00003183
aliguori376253e2009-03-05 23:01:23 +00003184 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003185 for(;;) {
3186 op = *pch;
3187 if (op != '+' && op != '-')
3188 break;
3189 next();
aliguori376253e2009-03-05 23:01:23 +00003190 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003191 if (op == '+')
3192 val += val2;
3193 else
3194 val -= val2;
3195 }
3196 return val;
3197}
3198
aliguori376253e2009-03-05 23:01:23 +00003199static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003200{
3201 pch = *pp;
3202 if (setjmp(expr_env)) {
3203 *pp = pch;
3204 return -1;
3205 }
blueswir1cd390082008-11-16 13:53:32 +00003206 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003207 pch++;
aliguori376253e2009-03-05 23:01:23 +00003208 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003209 *pp = pch;
3210 return 0;
3211}
3212
3213static int get_str(char *buf, int buf_size, const char **pp)
3214{
3215 const char *p;
3216 char *q;
3217 int c;
3218
bellard81d09122004-07-14 17:21:37 +00003219 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003220 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003221 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003222 p++;
3223 if (*p == '\0') {
3224 fail:
bellard81d09122004-07-14 17:21:37 +00003225 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003226 *pp = p;
3227 return -1;
3228 }
bellard9307c4c2004-04-04 12:57:25 +00003229 if (*p == '\"') {
3230 p++;
3231 while (*p != '\0' && *p != '\"') {
3232 if (*p == '\\') {
3233 p++;
3234 c = *p++;
3235 switch(c) {
3236 case 'n':
3237 c = '\n';
3238 break;
3239 case 'r':
3240 c = '\r';
3241 break;
3242 case '\\':
3243 case '\'':
3244 case '\"':
3245 break;
3246 default:
3247 qemu_printf("unsupported escape code: '\\%c'\n", c);
3248 goto fail;
3249 }
3250 if ((q - buf) < buf_size - 1) {
3251 *q++ = c;
3252 }
3253 } else {
3254 if ((q - buf) < buf_size - 1) {
3255 *q++ = *p;
3256 }
3257 p++;
3258 }
3259 }
3260 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003261 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003262 goto fail;
3263 }
3264 p++;
3265 } else {
blueswir1cd390082008-11-16 13:53:32 +00003266 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003267 if ((q - buf) < buf_size - 1) {
3268 *q++ = *p;
3269 }
3270 p++;
3271 }
bellard9307c4c2004-04-04 12:57:25 +00003272 }
bellard81d09122004-07-14 17:21:37 +00003273 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003274 *pp = p;
3275 return 0;
3276}
3277
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003278/*
3279 * Store the command-name in cmdname, and return a pointer to
3280 * the remaining of the command string.
3281 */
3282static const char *get_command_name(const char *cmdline,
3283 char *cmdname, size_t nlen)
3284{
3285 size_t len;
3286 const char *p, *pstart;
3287
3288 p = cmdline;
3289 while (qemu_isspace(*p))
3290 p++;
3291 if (*p == '\0')
3292 return NULL;
3293 pstart = p;
3294 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3295 p++;
3296 len = p - pstart;
3297 if (len > nlen - 1)
3298 len = nlen - 1;
3299 memcpy(cmdname, pstart, len);
3300 cmdname[len] = '\0';
3301 return p;
3302}
3303
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003304/**
3305 * Read key of 'type' into 'key' and return the current
3306 * 'type' pointer.
3307 */
3308static char *key_get_info(const char *type, char **key)
3309{
3310 size_t len;
3311 char *p, *str;
3312
3313 if (*type == ',')
3314 type++;
3315
3316 p = strchr(type, ':');
3317 if (!p) {
3318 *key = NULL;
3319 return NULL;
3320 }
3321 len = p - type;
3322
3323 str = qemu_malloc(len + 1);
3324 memcpy(str, type, len);
3325 str[len] = '\0';
3326
3327 *key = str;
3328 return ++p;
3329}
3330
bellard9307c4c2004-04-04 12:57:25 +00003331static int default_fmt_format = 'x';
3332static int default_fmt_size = 4;
3333
3334#define MAX_ARGS 16
3335
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003336static int is_valid_option(const char *c, const char *typestr)
3337{
3338 char option[3];
3339
3340 option[0] = '-';
3341 option[1] = *c;
3342 option[2] = '\0';
3343
3344 typestr = strstr(typestr, option);
3345 return (typestr != NULL);
3346}
3347
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003348static const mon_cmd_t *monitor_find_command(const char *cmdname)
3349{
3350 const mon_cmd_t *cmd;
3351
3352 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3353 if (compare_cmd(cmdname, cmd->name)) {
3354 return cmd;
3355 }
3356 }
3357
3358 return NULL;
3359}
3360
Anthony Liguoric227f092009-10-01 16:12:16 -05003361static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003362 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003363 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003364{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003365 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003366 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003367 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003368 char cmdname[256];
3369 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003370 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003371
3372#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003373 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003374#endif
ths3b46e622007-09-17 08:09:54 +00003375
bellard9307c4c2004-04-04 12:57:25 +00003376 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003377 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3378 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003379 return NULL;
ths3b46e622007-09-17 08:09:54 +00003380
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003381 cmd = monitor_find_command(cmdname);
3382 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003383 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003384 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003385 }
bellard9307c4c2004-04-04 12:57:25 +00003386
bellard9307c4c2004-04-04 12:57:25 +00003387 /* parse the parameters */
3388 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003389 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003390 typestr = key_get_info(typestr, &key);
3391 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003392 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003393 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003394 typestr++;
3395 switch(c) {
3396 case 'F':
bellard81d09122004-07-14 17:21:37 +00003397 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003398 case 's':
3399 {
3400 int ret;
ths3b46e622007-09-17 08:09:54 +00003401
blueswir1cd390082008-11-16 13:53:32 +00003402 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003403 p++;
3404 if (*typestr == '?') {
3405 typestr++;
3406 if (*p == '\0') {
3407 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003408 break;
bellard9307c4c2004-04-04 12:57:25 +00003409 }
3410 }
3411 ret = get_str(buf, sizeof(buf), &p);
3412 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003413 switch(c) {
3414 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003415 monitor_printf(mon, "%s: filename expected\n",
3416 cmdname);
bellard81d09122004-07-14 17:21:37 +00003417 break;
3418 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003419 monitor_printf(mon, "%s: block device name expected\n",
3420 cmdname);
bellard81d09122004-07-14 17:21:37 +00003421 break;
3422 default:
aliguori376253e2009-03-05 23:01:23 +00003423 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003424 break;
3425 }
bellard9307c4c2004-04-04 12:57:25 +00003426 goto fail;
3427 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003428 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003429 }
3430 break;
3431 case '/':
3432 {
3433 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003434
blueswir1cd390082008-11-16 13:53:32 +00003435 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003436 p++;
3437 if (*p == '/') {
3438 /* format found */
3439 p++;
3440 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003441 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003442 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003443 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003444 count = count * 10 + (*p - '0');
3445 p++;
3446 }
3447 }
3448 size = -1;
3449 format = -1;
3450 for(;;) {
3451 switch(*p) {
3452 case 'o':
3453 case 'd':
3454 case 'u':
3455 case 'x':
3456 case 'i':
3457 case 'c':
3458 format = *p++;
3459 break;
3460 case 'b':
3461 size = 1;
3462 p++;
3463 break;
3464 case 'h':
3465 size = 2;
3466 p++;
3467 break;
3468 case 'w':
3469 size = 4;
3470 p++;
3471 break;
3472 case 'g':
3473 case 'L':
3474 size = 8;
3475 p++;
3476 break;
3477 default:
3478 goto next;
3479 }
3480 }
3481 next:
blueswir1cd390082008-11-16 13:53:32 +00003482 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003483 monitor_printf(mon, "invalid char in format: '%c'\n",
3484 *p);
bellard9307c4c2004-04-04 12:57:25 +00003485 goto fail;
3486 }
bellard9307c4c2004-04-04 12:57:25 +00003487 if (format < 0)
3488 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003489 if (format != 'i') {
3490 /* for 'i', not specifying a size gives -1 as size */
3491 if (size < 0)
3492 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003493 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003494 }
bellard9307c4c2004-04-04 12:57:25 +00003495 default_fmt_format = format;
3496 } else {
3497 count = 1;
3498 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003499 if (format != 'i') {
3500 size = default_fmt_size;
3501 } else {
3502 size = -1;
3503 }
bellard9307c4c2004-04-04 12:57:25 +00003504 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003505 qdict_put(qdict, "count", qint_from_int(count));
3506 qdict_put(qdict, "format", qint_from_int(format));
3507 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003508 }
3509 break;
3510 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003511 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003512 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003513 {
blueswir1c2efc952007-09-25 17:28:42 +00003514 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003515
blueswir1cd390082008-11-16 13:53:32 +00003516 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003517 p++;
bellard34405572004-06-08 00:55:58 +00003518 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003519 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003520 if (*p == '\0') {
3521 typestr++;
3522 break;
3523 }
bellard34405572004-06-08 00:55:58 +00003524 } else {
3525 if (*p == '.') {
3526 p++;
blueswir1cd390082008-11-16 13:53:32 +00003527 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003528 p++;
bellard34405572004-06-08 00:55:58 +00003529 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003530 typestr++;
3531 break;
bellard34405572004-06-08 00:55:58 +00003532 }
3533 }
bellard13224a82006-07-14 22:03:35 +00003534 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003535 }
aliguori376253e2009-03-05 23:01:23 +00003536 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003537 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003538 /* Check if 'i' is greater than 32-bit */
3539 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3540 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3541 monitor_printf(mon, "integer is for 32-bit values\n");
3542 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003543 } else if (c == 'M') {
3544 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003545 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003546 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003547 }
3548 break;
3549 case '-':
3550 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003551 const char *tmp = p;
3552 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003553 /* option */
ths3b46e622007-09-17 08:09:54 +00003554
bellard9307c4c2004-04-04 12:57:25 +00003555 c = *typestr++;
3556 if (c == '\0')
3557 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003558 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003559 p++;
3560 has_option = 0;
3561 if (*p == '-') {
3562 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003563 if(c != *p) {
3564 if(!is_valid_option(p, typestr)) {
3565
3566 monitor_printf(mon, "%s: unsupported option -%c\n",
3567 cmdname, *p);
3568 goto fail;
3569 } else {
3570 skip_key = 1;
3571 }
bellard9307c4c2004-04-04 12:57:25 +00003572 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003573 if(skip_key) {
3574 p = tmp;
3575 } else {
3576 p++;
3577 has_option = 1;
3578 }
bellard9307c4c2004-04-04 12:57:25 +00003579 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003580 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003581 }
3582 break;
3583 default:
3584 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003585 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003586 goto fail;
3587 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003588 qemu_free(key);
3589 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003590 }
3591 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003592 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003593 p++;
3594 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003595 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3596 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003597 goto fail;
3598 }
3599
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003600 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003601
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003602fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003603 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003604 return NULL;
3605}
3606
Luiz Capitulino8204a912009-11-18 23:05:31 -02003607static void monitor_print_error(Monitor *mon)
3608{
3609 qerror_print(mon->error);
3610 QDECREF(mon->error);
3611 mon->error = NULL;
3612}
3613
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003614static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3615 const QDict *params)
3616{
3617 QObject *data = NULL;
3618
3619 cmd->mhandler.cmd_new(mon, params, &data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003620
3621 if (monitor_ctrl_mode(mon)) {
3622 /* Monitor Protocol */
3623 monitor_protocol_emitter(mon, data);
3624 } else {
3625 /* User Protocol */
3626 if (data)
3627 cmd->user_print(mon, data);
3628 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003629
3630 qobject_decref(data);
3631}
3632
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003633static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003634{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003635 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003636 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003637
3638 qdict = qdict_new();
3639
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003640 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003641 if (!cmd)
3642 goto out;
3643
3644 qemu_errors_to_mon(mon);
3645
3646 if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003647 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003648 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003649 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003650 }
3651
Luiz Capitulino8204a912009-11-18 23:05:31 -02003652 if (monitor_has_error(mon))
3653 monitor_print_error(mon);
3654
3655 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003656
3657out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003658 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003659}
3660
bellard81d09122004-07-14 17:21:37 +00003661static void cmd_completion(const char *name, const char *list)
3662{
3663 const char *p, *pstart;
3664 char cmd[128];
3665 int len;
3666
3667 p = list;
3668 for(;;) {
3669 pstart = p;
3670 p = strchr(p, '|');
3671 if (!p)
3672 p = pstart + strlen(pstart);
3673 len = p - pstart;
3674 if (len > sizeof(cmd) - 2)
3675 len = sizeof(cmd) - 2;
3676 memcpy(cmd, pstart, len);
3677 cmd[len] = '\0';
3678 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003679 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003680 }
3681 if (*p == '\0')
3682 break;
3683 p++;
3684 }
3685}
3686
3687static void file_completion(const char *input)
3688{
3689 DIR *ffs;
3690 struct dirent *d;
3691 char path[1024];
3692 char file[1024], file_prefix[1024];
3693 int input_path_len;
3694 const char *p;
3695
ths5fafdf22007-09-16 21:08:06 +00003696 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003697 if (!p) {
3698 input_path_len = 0;
3699 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003700 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003701 } else {
3702 input_path_len = p - input + 1;
3703 memcpy(path, input, input_path_len);
3704 if (input_path_len > sizeof(path) - 1)
3705 input_path_len = sizeof(path) - 1;
3706 path[input_path_len] = '\0';
3707 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3708 }
3709#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003710 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3711 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003712#endif
3713 ffs = opendir(path);
3714 if (!ffs)
3715 return;
3716 for(;;) {
3717 struct stat sb;
3718 d = readdir(ffs);
3719 if (!d)
3720 break;
3721 if (strstart(d->d_name, file_prefix, NULL)) {
3722 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003723 if (input_path_len < sizeof(file))
3724 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3725 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003726 /* stat the file to find out if it's a directory.
3727 * In that case add a slash to speed up typing long paths
3728 */
3729 stat(file, &sb);
3730 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003731 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003732 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003733 }
3734 }
3735 closedir(ffs);
3736}
3737
aliguori51de9762009-03-05 23:00:43 +00003738static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003739{
aliguori51de9762009-03-05 23:00:43 +00003740 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003741 const char *input = opaque;
3742
3743 if (input[0] == '\0' ||
3744 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003745 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003746 }
3747}
3748
3749/* NOTE: this parser is an approximate form of the real command parser */
3750static void parse_cmdline(const char *cmdline,
3751 int *pnb_args, char **args)
3752{
3753 const char *p;
3754 int nb_args, ret;
3755 char buf[1024];
3756
3757 p = cmdline;
3758 nb_args = 0;
3759 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003760 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003761 p++;
3762 if (*p == '\0')
3763 break;
3764 if (nb_args >= MAX_ARGS)
3765 break;
3766 ret = get_str(buf, sizeof(buf), &p);
3767 args[nb_args] = qemu_strdup(buf);
3768 nb_args++;
3769 if (ret < 0)
3770 break;
3771 }
3772 *pnb_args = nb_args;
3773}
3774
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003775static const char *next_arg_type(const char *typestr)
3776{
3777 const char *p = strchr(typestr, ':');
3778 return (p != NULL ? ++p : typestr);
3779}
3780
aliguori4c36ba32009-03-05 23:01:37 +00003781static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003782{
3783 const char *cmdname;
3784 char *args[MAX_ARGS];
3785 int nb_args, i, len;
3786 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003787 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003788 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003789
3790 parse_cmdline(cmdline, &nb_args, args);
3791#ifdef DEBUG_COMPLETION
3792 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003793 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003794 }
3795#endif
3796
3797 /* if the line ends with a space, it means we want to complete the
3798 next arg */
3799 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003800 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003801 if (nb_args >= MAX_ARGS)
3802 return;
3803 args[nb_args++] = qemu_strdup("");
3804 }
3805 if (nb_args <= 1) {
3806 /* command completion */
3807 if (nb_args == 0)
3808 cmdname = "";
3809 else
3810 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003811 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003812 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003813 cmd_completion(cmdname, cmd->name);
3814 }
3815 } else {
3816 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003817 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003818 if (compare_cmd(args[0], cmd->name))
3819 goto found;
3820 }
3821 return;
3822 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003823 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003824 for(i = 0; i < nb_args - 2; i++) {
3825 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003826 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003827 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003828 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003829 }
3830 }
3831 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003832 if (*ptype == '-' && ptype[1] != '\0') {
3833 ptype += 2;
3834 }
bellard81d09122004-07-14 17:21:37 +00003835 switch(*ptype) {
3836 case 'F':
3837 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003838 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003839 file_completion(str);
3840 break;
3841 case 'B':
3842 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003843 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003844 bdrv_iterate(block_completion_it, (void *)str);
3845 break;
bellard7fe48482004-10-09 18:08:01 +00003846 case 's':
3847 /* XXX: more generic ? */
3848 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003849 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003850 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3851 cmd_completion(str, cmd->name);
3852 }
bellard64866c32006-05-07 18:03:31 +00003853 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003854 char *sep = strrchr(str, '-');
3855 if (sep)
3856 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003857 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003858 for(key = key_defs; key->name != NULL; key++) {
3859 cmd_completion(str, key->name);
3860 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003861 } else if (!strcmp(cmd->name, "help|?")) {
3862 readline_set_completion_index(cur_mon->rs, strlen(str));
3863 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3864 cmd_completion(str, cmd->name);
3865 }
bellard7fe48482004-10-09 18:08:01 +00003866 }
3867 break;
bellard81d09122004-07-14 17:21:37 +00003868 default:
3869 break;
3870 }
3871 }
3872 for(i = 0; i < nb_args; i++)
3873 qemu_free(args[i]);
3874}
3875
aliguori731b0362009-03-05 23:01:42 +00003876static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003877{
aliguori731b0362009-03-05 23:01:42 +00003878 Monitor *mon = opaque;
3879
Jan Kiszkac62313b2009-12-04 14:05:29 +01003880 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003881}
3882
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003883typedef struct CmdArgs {
3884 QString *name;
3885 int type;
3886 int flag;
3887 int optional;
3888} CmdArgs;
3889
3890static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
3891{
3892 if (!cmd_args->optional) {
3893 qemu_error_new(QERR_MISSING_PARAMETER, name);
3894 return -1;
3895 }
3896
3897 if (cmd_args->type == '-') {
3898 /* handlers expect a value, they need to be changed */
3899 qdict_put(args, name, qint_from_int(0));
3900 }
3901
3902 return 0;
3903}
3904
3905static int check_arg(const CmdArgs *cmd_args, QDict *args)
3906{
3907 QObject *value;
3908 const char *name;
3909
3910 name = qstring_get_str(cmd_args->name);
3911
3912 if (!args) {
3913 return check_opt(cmd_args, name, args);
3914 }
3915
3916 value = qdict_get(args, name);
3917 if (!value) {
3918 return check_opt(cmd_args, name, args);
3919 }
3920
3921 switch (cmd_args->type) {
3922 case 'F':
3923 case 'B':
3924 case 's':
3925 if (qobject_type(value) != QTYPE_QSTRING) {
3926 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "string");
3927 return -1;
3928 }
3929 break;
3930 case '/': {
3931 int i;
3932 const char *keys[] = { "count", "format", "size", NULL };
3933
3934 for (i = 0; keys[i]; i++) {
3935 QObject *obj = qdict_get(args, keys[i]);
3936 if (!obj) {
3937 qemu_error_new(QERR_MISSING_PARAMETER, name);
3938 return -1;
3939 }
3940 if (qobject_type(obj) != QTYPE_QINT) {
3941 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
3942 return -1;
3943 }
3944 }
3945 break;
3946 }
3947 case 'i':
3948 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003949 case 'M':
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003950 if (qobject_type(value) != QTYPE_QINT) {
3951 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
3952 return -1;
3953 }
3954 break;
3955 case '-':
3956 if (qobject_type(value) != QTYPE_QINT &&
3957 qobject_type(value) != QTYPE_QBOOL) {
3958 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "bool");
3959 return -1;
3960 }
3961 if (qobject_type(value) == QTYPE_QBOOL) {
3962 /* handlers expect a QInt, they need to be changed */
3963 qdict_put(args, name,
3964 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
3965 }
3966 break;
3967 default:
3968 /* impossible */
3969 abort();
3970 }
3971
3972 return 0;
3973}
3974
3975static void cmd_args_init(CmdArgs *cmd_args)
3976{
3977 cmd_args->name = qstring_new();
3978 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
3979}
3980
3981/*
3982 * This is not trivial, we have to parse Monitor command's argument
3983 * type syntax to be able to check the arguments provided by clients.
3984 *
3985 * In the near future we will be using an array for that and will be
3986 * able to drop all this parsing...
3987 */
3988static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
3989{
3990 int err;
3991 const char *p;
3992 CmdArgs cmd_args;
3993
Blue Swirldd5121b2009-12-04 20:52:02 +00003994 if (cmd->args_type == NULL) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003995 return (qdict_size(args) == 0 ? 0 : -1);
3996 }
3997
3998 err = 0;
3999 cmd_args_init(&cmd_args);
4000
4001 for (p = cmd->args_type;; p++) {
4002 if (*p == ':') {
4003 cmd_args.type = *++p;
4004 p++;
4005 if (cmd_args.type == '-') {
4006 cmd_args.flag = *p++;
4007 cmd_args.optional = 1;
4008 } else if (*p == '?') {
4009 cmd_args.optional = 1;
4010 p++;
4011 }
4012
4013 assert(*p == ',' || *p == '\0');
4014 err = check_arg(&cmd_args, args);
4015
4016 QDECREF(cmd_args.name);
4017 cmd_args_init(&cmd_args);
4018
4019 if (err < 0) {
4020 break;
4021 }
4022 } else {
4023 qstring_append_chr(cmd_args.name, *p);
4024 }
4025
4026 if (*p == '\0') {
4027 break;
4028 }
4029 }
4030
4031 QDECREF(cmd_args.name);
4032 return err;
4033}
4034
4035static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4036{
4037 int err;
4038 QObject *obj;
4039 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004040 const mon_cmd_t *cmd;
4041 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004042 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004043
4044 args = NULL;
4045 qemu_errors_to_mon(mon);
4046
4047 obj = json_parser_parse(tokens, NULL);
4048 if (!obj) {
4049 // FIXME: should be triggered in json_parser_parse()
4050 qemu_error_new(QERR_JSON_PARSING);
4051 goto err_out;
4052 } else if (qobject_type(obj) != QTYPE_QDICT) {
4053 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "object");
4054 qobject_decref(obj);
4055 goto err_out;
4056 }
4057
4058 input = qobject_to_qdict(obj);
4059
4060 mon->mc->id = qdict_get(input, "id");
4061 qobject_incref(mon->mc->id);
4062
4063 obj = qdict_get(input, "execute");
4064 if (!obj) {
4065 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4066 goto err_input;
4067 } else if (qobject_type(obj) != QTYPE_QSTRING) {
4068 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "string");
4069 goto err_input;
4070 }
4071
4072 cmd_name = qstring_get_str(qobject_to_qstring(obj));
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004073
4074 /*
4075 * XXX: We need this special case until we get info handlers
4076 * converted into 'query-' commands
4077 */
4078 if (compare_cmd(cmd_name, "info")) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004079 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4080 goto err_input;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004081 } else if (strstart(cmd_name, "query-", &info_item)) {
4082 cmd = monitor_find_command("info");
4083 qdict_put_obj(input, "arguments",
4084 qobject_from_jsonf("{ 'item': %s }", info_item));
4085 } else {
4086 cmd = monitor_find_command(cmd_name);
Luiz Capitulino43e713c2009-12-18 13:24:59 -02004087 if (!cmd || !monitor_handler_ported(cmd)) {
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004088 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4089 goto err_input;
4090 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004091 }
4092
4093 obj = qdict_get(input, "arguments");
4094 if (!obj) {
4095 args = qdict_new();
4096 } else {
4097 args = qobject_to_qdict(obj);
4098 QINCREF(args);
4099 }
4100
4101 QDECREF(input);
4102
4103 err = monitor_check_qmp_args(cmd, args);
4104 if (err < 0) {
4105 goto err_out;
4106 }
4107
4108 monitor_call_handler(mon, cmd, args);
4109 goto out;
4110
4111err_input:
4112 QDECREF(input);
4113err_out:
4114 monitor_protocol_emitter(mon, NULL);
4115out:
4116 QDECREF(args);
4117 qemu_errors_to_previous();
4118}
4119
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004120/**
4121 * monitor_control_read(): Read and handle QMP input
4122 */
4123static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4124{
4125 Monitor *old_mon = cur_mon;
4126
4127 cur_mon = opaque;
4128
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004129 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004130
4131 cur_mon = old_mon;
4132}
4133
aliguori731b0362009-03-05 23:01:42 +00004134static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004135{
aliguori731b0362009-03-05 23:01:42 +00004136 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004137 int i;
aliguori376253e2009-03-05 23:01:23 +00004138
aliguori731b0362009-03-05 23:01:42 +00004139 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004140
aliguoricde76ee2009-03-05 23:01:51 +00004141 if (cur_mon->rs) {
4142 for (i = 0; i < size; i++)
4143 readline_handle_byte(cur_mon->rs, buf[i]);
4144 } else {
4145 if (size == 0 || buf[size - 1] != 0)
4146 monitor_printf(cur_mon, "corrupted command\n");
4147 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004148 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004149 }
aliguori731b0362009-03-05 23:01:42 +00004150
4151 cur_mon = old_mon;
4152}
aliguorid8f44602008-10-06 13:52:44 +00004153
aliguori376253e2009-03-05 23:01:23 +00004154static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004155{
aliguori731b0362009-03-05 23:01:42 +00004156 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004157 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004158 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004159}
4160
aliguoricde76ee2009-03-05 23:01:51 +00004161int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004162{
aliguoricde76ee2009-03-05 23:01:51 +00004163 if (!mon->rs)
4164 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004165 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004166 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004167}
4168
aliguori376253e2009-03-05 23:01:23 +00004169void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004170{
aliguoricde76ee2009-03-05 23:01:51 +00004171 if (!mon->rs)
4172 return;
aliguori731b0362009-03-05 23:01:42 +00004173 if (--mon->suspend_cnt == 0)
4174 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004175}
4176
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004177/**
4178 * monitor_control_event(): Print QMP gretting
4179 */
4180static void monitor_control_event(void *opaque, int event)
4181{
4182 if (event == CHR_EVENT_OPENED) {
4183 QObject *data;
4184 Monitor *mon = opaque;
4185
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004186 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
4187
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004188 data = qobject_from_jsonf("{ 'QMP': { 'capabilities': [] } }");
4189 assert(data != NULL);
4190
4191 monitor_json_emitter(mon, data);
4192 qobject_decref(data);
4193 }
4194}
4195
aliguori731b0362009-03-05 23:01:42 +00004196static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004197{
aliguori376253e2009-03-05 23:01:23 +00004198 Monitor *mon = opaque;
4199
aliguori2724b182009-03-05 23:01:47 +00004200 switch (event) {
4201 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004202 mon->mux_out = 0;
4203 if (mon->reset_seen) {
4204 readline_restart(mon->rs);
4205 monitor_resume(mon);
4206 monitor_flush(mon);
4207 } else {
4208 mon->suspend_cnt = 0;
4209 }
aliguori2724b182009-03-05 23:01:47 +00004210 break;
ths86e94de2007-01-05 22:01:59 +00004211
aliguori2724b182009-03-05 23:01:47 +00004212 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004213 if (mon->reset_seen) {
4214 if (mon->suspend_cnt == 0) {
4215 monitor_printf(mon, "\n");
4216 }
4217 monitor_flush(mon);
4218 monitor_suspend(mon);
4219 } else {
4220 mon->suspend_cnt++;
4221 }
4222 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004223 break;
4224
Amit Shahb6b8df52009-10-07 18:31:16 +05304225 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004226 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4227 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004228 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004229 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004230 }
4231 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004232 break;
4233 }
ths86e94de2007-01-05 22:01:59 +00004234}
4235
aliguori76655d62009-03-06 20:27:37 +00004236
4237/*
4238 * Local variables:
4239 * c-indent-level: 4
4240 * c-basic-offset: 4
4241 * tab-width: 8
4242 * End:
4243 */
4244
aliguori731b0362009-03-05 23:01:42 +00004245void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004246{
aliguori731b0362009-03-05 23:01:42 +00004247 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004248 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004249
4250 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004251 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004252 is_first_init = 0;
4253 }
aliguori87127162009-03-05 23:01:29 +00004254
4255 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004256
aliguori87127162009-03-05 23:01:29 +00004257 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004258 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004259 if (flags & MONITOR_USE_READLINE) {
4260 mon->rs = readline_init(mon, monitor_find_completion);
4261 monitor_read_command(mon, 0);
4262 }
aliguori87127162009-03-05 23:01:29 +00004263
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004264 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004265 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004266 /* Control mode requires special handlers */
4267 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4268 monitor_control_event, mon);
4269 } else {
4270 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4271 monitor_event, mon);
4272 }
aliguori87127162009-03-05 23:01:29 +00004273
Blue Swirl72cf2d42009-09-12 07:36:22 +00004274 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00004275 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00004276 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004277}
4278
aliguori376253e2009-03-05 23:01:23 +00004279static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004280{
aliguoribb5fc202009-03-05 23:01:15 +00004281 BlockDriverState *bs = opaque;
4282 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004283
aliguoribb5fc202009-03-05 23:01:15 +00004284 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004285 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004286 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004287 }
aliguori731b0362009-03-05 23:01:42 +00004288 if (mon->password_completion_cb)
4289 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004290
aliguori731b0362009-03-05 23:01:42 +00004291 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004292}
aliguoric0f4ce72009-03-05 23:01:01 +00004293
aliguori376253e2009-03-05 23:01:23 +00004294void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00004295 BlockDriverCompletionFunc *completion_cb,
4296 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004297{
aliguoricde76ee2009-03-05 23:01:51 +00004298 int err;
4299
aliguoribb5fc202009-03-05 23:01:15 +00004300 if (!bdrv_key_required(bs)) {
4301 if (completion_cb)
4302 completion_cb(opaque, 0);
4303 return;
4304 }
aliguoric0f4ce72009-03-05 23:01:01 +00004305
Luiz Capitulino94171e12009-12-07 21:37:00 +01004306 if (monitor_ctrl_mode(mon)) {
4307 qemu_error_new(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
4308 return;
4309 }
4310
aliguori376253e2009-03-05 23:01:23 +00004311 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4312 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004313
aliguori731b0362009-03-05 23:01:42 +00004314 mon->password_completion_cb = completion_cb;
4315 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004316
aliguoricde76ee2009-03-05 23:01:51 +00004317 err = monitor_read_password(mon, bdrv_password_cb, bs);
4318
4319 if (err && completion_cb)
4320 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00004321}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004322
4323typedef struct QemuErrorSink QemuErrorSink;
4324struct QemuErrorSink {
4325 enum {
4326 ERR_SINK_FILE,
4327 ERR_SINK_MONITOR,
4328 } dest;
4329 union {
4330 FILE *fp;
4331 Monitor *mon;
4332 };
4333 QemuErrorSink *previous;
4334};
4335
Blue Swirl528e93a2009-08-31 15:14:40 +00004336static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004337
4338void qemu_errors_to_file(FILE *fp)
4339{
4340 QemuErrorSink *sink;
4341
4342 sink = qemu_mallocz(sizeof(*sink));
4343 sink->dest = ERR_SINK_FILE;
4344 sink->fp = fp;
4345 sink->previous = qemu_error_sink;
4346 qemu_error_sink = sink;
4347}
4348
4349void qemu_errors_to_mon(Monitor *mon)
4350{
4351 QemuErrorSink *sink;
4352
4353 sink = qemu_mallocz(sizeof(*sink));
4354 sink->dest = ERR_SINK_MONITOR;
4355 sink->mon = mon;
4356 sink->previous = qemu_error_sink;
4357 qemu_error_sink = sink;
4358}
4359
4360void qemu_errors_to_previous(void)
4361{
4362 QemuErrorSink *sink;
4363
4364 assert(qemu_error_sink != NULL);
4365 sink = qemu_error_sink;
4366 qemu_error_sink = sink->previous;
4367 qemu_free(sink);
4368}
4369
4370void qemu_error(const char *fmt, ...)
4371{
4372 va_list args;
4373
4374 assert(qemu_error_sink != NULL);
4375 switch (qemu_error_sink->dest) {
4376 case ERR_SINK_FILE:
4377 va_start(args, fmt);
4378 vfprintf(qemu_error_sink->fp, fmt, args);
4379 va_end(args);
4380 break;
4381 case ERR_SINK_MONITOR:
4382 va_start(args, fmt);
4383 monitor_vprintf(qemu_error_sink->mon, fmt, args);
4384 va_end(args);
4385 break;
4386 }
4387}
Luiz Capitulino8204a912009-11-18 23:05:31 -02004388
4389void qemu_error_internal(const char *file, int linenr, const char *func,
4390 const char *fmt, ...)
4391{
4392 va_list va;
4393 QError *qerror;
4394
4395 assert(qemu_error_sink != NULL);
4396
4397 va_start(va, fmt);
4398 qerror = qerror_from_info(file, linenr, func, fmt, &va);
4399 va_end(va);
4400
4401 switch (qemu_error_sink->dest) {
4402 case ERR_SINK_FILE:
4403 qerror_print(qerror);
4404 QDECREF(qerror);
4405 break;
4406 case ERR_SINK_MONITOR:
4407 assert(qemu_error_sink->mon->error == NULL);
4408 qemu_error_sink->mon->error = qerror;
4409 break;
4410 }
4411}