blob: ae125b8230ad96573253e0edbde0bfa38667f81d [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"
Markus Armbruster3350a4d2010-01-25 14:23:03 +010050#include "qfloat.h"
Luiz Capitulino8f3cec02009-10-07 13:42:04 -030051#include "qlist.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030052#include "qdict.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020053#include "qbool.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030054#include "qstring.h"
Luiz Capitulino8204a912009-11-18 23:05:31 -020055#include "qerror.h"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020056#include "qjson.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020057#include "json-streamer.h"
58#include "json-parser.h"
Blue Swirld08d6f02009-12-04 18:06:39 +000059#include "osdep.h"
ths6a5bd302007-12-03 17:05:38 +000060
bellard9dc39cb2004-03-14 21:38:27 +000061//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000062//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000063
bellard9307c4c2004-04-04 12:57:25 +000064/*
65 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000066 *
bellard9307c4c2004-04-04 12:57:25 +000067 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000068 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000069 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000070 * 'i' 32 bit integer
71 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010072 * 'M' just like 'l', except in user mode the value is
73 * multiplied by 2^20 (think Mebibyte)
Markus Armbruster3350a4d2010-01-25 14:23:03 +010074 * 'b' double
75 * user mode accepts an optional G, g, M, m, K, k suffix,
76 * which multiplies the value by 2^30 for suffixes G and
77 * g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010078 * 'T' double
79 * user mode accepts an optional ms, us, ns suffix,
80 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +000081 * '/' optional gdb-like print format (like "/10x")
82 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030083 * '?' optional type (for all types, except '/')
84 * '.' other form of optional type (for 'i' and 'l')
85 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000086 *
87 */
88
Adam Litke940cc302010-01-25 12:18:44 -060089typedef struct MonitorCompletionData MonitorCompletionData;
90struct MonitorCompletionData {
91 Monitor *mon;
92 void (*user_print)(Monitor *mon, const QObject *data);
93};
94
Anthony Liguoric227f092009-10-01 16:12:16 -050095typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000096 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000097 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +000098 const char *params;
99 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300100 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300101 union {
102 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300103 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600104 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300105 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -0300106 void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600107 int (*cmd_async)(Monitor *mon, const QDict *params,
108 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300109 } mhandler;
Adam Litke940cc302010-01-25 12:18:44 -0600110 int async;
Anthony Liguoric227f092009-10-01 16:12:16 -0500111} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000112
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100113/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500114typedef struct mon_fd_t mon_fd_t;
115struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100116 char *name;
117 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500118 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100119};
120
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200121typedef struct MonitorControl {
122 QObject *id;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200123 int print_enabled;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200124 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200125 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200126} MonitorControl;
127
aliguori87127162009-03-05 23:01:29 +0000128struct Monitor {
129 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200130 int mux_out;
131 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000132 int flags;
133 int suspend_cnt;
134 uint8_t outbuf[1024];
135 int outbuf_index;
136 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200137 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000138 CPUState *mon_cpu;
139 BlockDriverCompletionFunc *password_completion_cb;
140 void *password_opaque;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200141 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500142 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000143 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000144};
145
Blue Swirl72cf2d42009-09-12 07:36:22 +0000146static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000147
Anthony Liguoric227f092009-10-01 16:12:16 -0500148static const mon_cmd_t mon_cmds[];
149static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000150
aliguori87127162009-03-05 23:01:29 +0000151Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000152
aliguori731b0362009-03-05 23:01:42 +0000153static void monitor_command_cb(Monitor *mon, const char *cmdline,
154 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000155
Luiz Capitulino09069b12010-02-04 18:10:06 -0200156static inline int qmp_cmd_mode(const Monitor *mon)
157{
158 return (mon->mc ? mon->mc->command_mode : 0);
159}
160
Luiz Capitulino418173c2009-11-26 22:58:51 -0200161/* Return true if in control mode, false otherwise */
162static inline int monitor_ctrl_mode(const Monitor *mon)
163{
164 return (mon->flags & MONITOR_USE_CONTROL);
165}
166
aliguori731b0362009-03-05 23:01:42 +0000167static void monitor_read_command(Monitor *mon, int show_prompt)
168{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200169 if (!mon->rs)
170 return;
171
aliguori731b0362009-03-05 23:01:42 +0000172 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
173 if (show_prompt)
174 readline_show_prompt(mon->rs);
175}
bellard6a00d602005-11-21 23:25:50 +0000176
aliguoricde76ee2009-03-05 23:01:51 +0000177static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
178 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000179{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100180 if (monitor_ctrl_mode(mon)) {
181 qemu_error_new(QERR_MISSING_PARAMETER, "password");
182 return -EINVAL;
183 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000184 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
185 /* prompt is printed on return from the command handler */
186 return 0;
187 } else {
188 monitor_printf(mon, "terminal does not support password prompting\n");
189 return -ENOTTY;
190 }
aliguoribb5fc202009-03-05 23:01:15 +0000191}
192
aliguori376253e2009-03-05 23:01:23 +0000193void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000194{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200195 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000196 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
197 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000198 }
199}
200
201/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000202static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000203{
ths60fe76f2007-12-16 03:02:09 +0000204 char c;
aliguori731b0362009-03-05 23:01:42 +0000205
bellard7e2515e2004-08-01 21:52:19 +0000206 for(;;) {
207 c = *str++;
208 if (c == '\0')
209 break;
bellard7ba12602006-07-14 20:26:42 +0000210 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000211 mon->outbuf[mon->outbuf_index++] = '\r';
212 mon->outbuf[mon->outbuf_index++] = c;
213 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
214 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000215 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000216 }
217}
218
aliguori376253e2009-03-05 23:01:23 +0000219void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000220{
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200221 if (!mon)
222 return;
223
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200224 if (mon->mc && !mon->mc->print_enabled) {
225 qemu_error_new(QERR_UNDEFINED_ERROR);
226 } else {
227 char buf[4096];
228 vsnprintf(buf, sizeof(buf), fmt, ap);
229 monitor_puts(mon, buf);
230 }
bellard7e2515e2004-08-01 21:52:19 +0000231}
232
aliguori376253e2009-03-05 23:01:23 +0000233void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000234{
235 va_list ap;
236 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000237 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000238 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000239}
240
aliguori376253e2009-03-05 23:01:23 +0000241void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000242{
243 int i;
244
245 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000246 switch (filename[i]) {
247 case ' ':
248 case '"':
249 case '\\':
250 monitor_printf(mon, "\\%c", filename[i]);
251 break;
252 case '\t':
253 monitor_printf(mon, "\\t");
254 break;
255 case '\r':
256 monitor_printf(mon, "\\r");
257 break;
258 case '\n':
259 monitor_printf(mon, "\\n");
260 break;
261 default:
262 monitor_printf(mon, "%c", filename[i]);
263 break;
264 }
thsfef30742006-12-22 14:11:32 +0000265 }
266}
267
bellard7fe48482004-10-09 18:08:01 +0000268static int monitor_fprintf(FILE *stream, const char *fmt, ...)
269{
270 va_list ap;
271 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000272 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000273 va_end(ap);
274 return 0;
275}
276
Luiz Capitulino13c74252009-10-07 13:41:55 -0300277static void monitor_user_noop(Monitor *mon, const QObject *data) { }
278
Luiz Capitulino13917be2009-10-07 13:41:54 -0300279static inline int monitor_handler_ported(const mon_cmd_t *cmd)
280{
281 return cmd->user_print != NULL;
282}
283
Adam Litke940cc302010-01-25 12:18:44 -0600284static inline bool monitor_handler_is_async(const mon_cmd_t *cmd)
285{
286 return cmd->async != 0;
287}
288
Luiz Capitulino8204a912009-11-18 23:05:31 -0200289static inline int monitor_has_error(const Monitor *mon)
290{
291 return mon->error != NULL;
292}
293
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200294static void monitor_json_emitter(Monitor *mon, const QObject *data)
295{
296 QString *json;
297
298 json = qobject_to_json(data);
299 assert(json != NULL);
300
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200301 mon->mc->print_enabled = 1;
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200302 monitor_printf(mon, "%s\n", qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200303 mon->mc->print_enabled = 0;
304
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200305 QDECREF(json);
306}
307
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200308static void monitor_protocol_emitter(Monitor *mon, QObject *data)
309{
310 QDict *qmp;
311
312 qmp = qdict_new();
313
314 if (!monitor_has_error(mon)) {
315 /* success response */
316 if (data) {
317 qobject_incref(data);
318 qdict_put_obj(qmp, "return", data);
319 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200320 /* return an empty QDict by default */
321 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200322 }
323 } else {
324 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100325 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200326 qdict_put(qmp, "error", mon->error->error);
327 QINCREF(mon->error->error);
328 QDECREF(mon->error);
329 mon->error = NULL;
330 }
331
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200332 if (mon->mc->id) {
333 qdict_put_obj(qmp, "id", mon->mc->id);
334 mon->mc->id = NULL;
335 }
336
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200337 monitor_json_emitter(mon, QOBJECT(qmp));
338 QDECREF(qmp);
339}
340
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200341static void timestamp_put(QDict *qdict)
342{
343 int err;
344 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000345 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200346
Blue Swirld08d6f02009-12-04 18:06:39 +0000347 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200348 if (err < 0)
349 return;
350
351 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
352 "'microseconds': %" PRId64 " }",
353 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200354 qdict_put_obj(qdict, "timestamp", obj);
355}
356
357/**
358 * monitor_protocol_event(): Generate a Monitor event
359 *
360 * Event-specific data can be emitted through the (optional) 'data' parameter.
361 */
362void monitor_protocol_event(MonitorEvent event, QObject *data)
363{
364 QDict *qmp;
365 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600366 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200367
Blue Swirl242cd002009-12-04 18:05:45 +0000368 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200369
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200370 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000371 case QEVENT_DEBUG:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200372 event_name = "DEBUG";
373 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000374 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200375 event_name = "SHUTDOWN";
376 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000377 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200378 event_name = "RESET";
379 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000380 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200381 event_name = "POWERDOWN";
382 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000383 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200384 event_name = "STOP";
385 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200386 case QEVENT_VNC_CONNECTED:
387 event_name = "VNC_CONNECTED";
388 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200389 case QEVENT_VNC_INITIALIZED:
390 event_name = "VNC_INITIALIZED";
391 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200392 case QEVENT_VNC_DISCONNECTED:
393 event_name = "VNC_DISCONNECTED";
394 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200395 case QEVENT_BLOCK_IO_ERROR:
396 event_name = "BLOCK_IO_ERROR";
397 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200398 default:
399 abort();
400 break;
401 }
402
403 qmp = qdict_new();
404 timestamp_put(qmp);
405 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200406 if (data) {
407 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200408 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200409 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200410
Adam Litkef039a562010-01-15 08:34:02 -0600411 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200412 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200413 monitor_json_emitter(mon, QOBJECT(qmp));
414 }
Adam Litkef039a562010-01-15 08:34:02 -0600415 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200416 QDECREF(qmp);
417}
418
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200419static void do_qmp_capabilities(Monitor *mon, const QDict *params,
420 QObject **ret_data)
421{
422 /* Will setup QMP capabilities in the future */
423 if (monitor_ctrl_mode(mon)) {
424 mon->mc->command_mode = 1;
425 }
426}
427
bellard9dc39cb2004-03-14 21:38:27 +0000428static int compare_cmd(const char *name, const char *list)
429{
430 const char *p, *pstart;
431 int len;
432 len = strlen(name);
433 p = list;
434 for(;;) {
435 pstart = p;
436 p = strchr(p, '|');
437 if (!p)
438 p = pstart + strlen(pstart);
439 if ((p - pstart) == len && !memcmp(pstart, name, len))
440 return 1;
441 if (*p == '\0')
442 break;
443 p++;
444 }
445 return 0;
446}
447
Anthony Liguoric227f092009-10-01 16:12:16 -0500448static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000449 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000450{
Anthony Liguoric227f092009-10-01 16:12:16 -0500451 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000452
453 for(cmd = cmds; cmd->name != NULL; cmd++) {
454 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000455 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
456 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000457 }
458}
459
aliguori376253e2009-03-05 23:01:23 +0000460static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000461{
462 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000463 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000464 } else {
aliguori376253e2009-03-05 23:01:23 +0000465 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000466 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000467 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000468 monitor_printf(mon, "Log items (comma separated):\n");
469 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000470 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000471 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000472 }
473 }
bellard9dc39cb2004-03-14 21:38:27 +0000474 }
475}
476
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300477static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300478{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300479 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300480}
481
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300482static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000483{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200484 int all_devices;
485 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300486 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000487
bellard7954c732006-08-01 15:52:40 +0000488 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000489 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200490 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300491 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200492 continue;
493 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000494 }
495}
496
Adam Litke940cc302010-01-25 12:18:44 -0600497static void user_monitor_complete(void *opaque, QObject *ret_data)
498{
499 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
500
501 if (ret_data) {
502 data->user_print(data->mon, ret_data);
503 }
504 monitor_resume(data->mon);
505 qemu_free(data);
506}
507
508static void qmp_monitor_complete(void *opaque, QObject *ret_data)
509{
510 monitor_protocol_emitter(opaque, ret_data);
511}
512
513static void qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
514 const QDict *params)
515{
516 cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
517}
518
519static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
520{
521 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
522}
523
524static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
525 const QDict *params)
526{
527 int ret;
528
529 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
530 cb_data->mon = mon;
531 cb_data->user_print = cmd->user_print;
532 monitor_suspend(mon);
533 ret = cmd->mhandler.cmd_async(mon, params,
534 user_monitor_complete, cb_data);
535 if (ret < 0) {
536 monitor_resume(mon);
537 qemu_free(cb_data);
538 }
539}
540
541static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
542{
543 int ret;
544
545 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
546 cb_data->mon = mon;
547 cb_data->user_print = cmd->user_print;
548 monitor_suspend(mon);
549 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
550 if (ret < 0) {
551 monitor_resume(mon);
552 qemu_free(cb_data);
553 }
554}
555
Luiz Capitulino13c74252009-10-07 13:41:55 -0300556static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000557{
Anthony Liguoric227f092009-10-01 16:12:16 -0500558 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300559 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000560
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200561 if (!item) {
562 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000563 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200564 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300565
566 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000567 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300568 break;
bellard9dc39cb2004-03-14 21:38:27 +0000569 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300570
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200571 if (cmd->name == NULL) {
572 if (monitor_ctrl_mode(mon)) {
573 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
574 return;
575 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300576 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200577 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300578
Adam Litke940cc302010-01-25 12:18:44 -0600579 if (monitor_handler_is_async(cmd)) {
580 if (monitor_ctrl_mode(mon)) {
581 qmp_async_info_handler(mon, cmd);
582 } else {
583 user_async_info_handler(mon, cmd);
584 }
585 /*
586 * Indicate that this command is asynchronous and will not return any
587 * data (not even empty). Instead, the data will be returned via a
588 * completion callback.
589 */
590 *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
591 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300592 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200593
594 if (!monitor_ctrl_mode(mon)) {
595 /*
596 * User Protocol function is called here, Monitor Protocol is
597 * handled by monitor_call_handler()
598 */
599 if (*ret_data)
600 cmd->user_print(mon, *ret_data);
601 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300602 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200603 if (monitor_ctrl_mode(mon)) {
604 /* handler not converted yet */
605 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
606 } else {
607 cmd->mhandler.info(mon);
608 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300609 }
610
bellard9dc39cb2004-03-14 21:38:27 +0000611 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300612
613help:
614 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000615}
616
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200617static void do_info_version_print(Monitor *mon, const QObject *data)
618{
619 QDict *qdict;
620
621 qdict = qobject_to_qdict(data);
622
623 monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
624 qdict_get_str(qdict, "package"));
625}
626
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300627/**
628 * do_info_version(): Show QEMU version
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200629 *
630 * Return a QDict with the following information:
631 *
632 * - "qemu": QEMU's version
633 * - "package": package's version
634 *
635 * Example:
636 *
637 * { "qemu": "0.11.50", "package": "" }
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300638 */
639static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000640{
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200641 *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
642 QEMU_VERSION, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000643}
644
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200645static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000646{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200647 QDict *qdict;
648
649 qdict = qobject_to_qdict(data);
650 if (qdict_size(qdict) == 0) {
651 return;
652 }
653
654 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
655}
656
657/**
658 * do_info_name(): Show VM name
659 *
660 * Return a QDict with the following information:
661 *
662 * - "name": VM's name (optional)
663 *
664 * Example:
665 *
666 * { "name": "qemu-name" }
667 */
668static void do_info_name(Monitor *mon, QObject **ret_data)
669{
670 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
671 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000672}
673
Luiz Capitulino1a728672009-12-10 17:15:56 -0200674static QObject *get_cmd_dict(const char *name)
675{
676 const char *p;
677
678 /* Remove '|' from some commands */
679 p = strchr(name, '|');
680 if (p) {
681 p++;
682 } else {
683 p = name;
684 }
685
686 return qobject_from_jsonf("{ 'name': %s }", p);
687}
688
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200689/**
690 * do_info_commands(): List QMP available commands
691 *
Luiz Capitulino1a728672009-12-10 17:15:56 -0200692 * Each command is represented by a QDict, the returned QObject is a QList
693 * of all commands.
694 *
695 * The QDict contains:
696 *
697 * - "name": command's name
698 *
699 * Example:
700 *
701 * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] }
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200702 */
703static void do_info_commands(Monitor *mon, QObject **ret_data)
704{
705 QList *cmd_list;
706 const mon_cmd_t *cmd;
707
708 cmd_list = qlist_new();
709
710 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
711 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200712 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200713 }
714 }
715
716 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
717 if (monitor_handler_ported(cmd)) {
718 char buf[128];
719 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200720 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200721 }
722 }
723
724 *ret_data = QOBJECT(cmd_list);
725}
726
aurel32bf4f74c2008-12-18 22:42:34 +0000727#if defined(TARGET_I386)
Luiz Capitulino14f07202009-12-10 17:16:02 -0200728static void do_info_hpet_print(Monitor *mon, const QObject *data)
aliguori16b29ae2008-12-17 23:28:44 +0000729{
aliguori376253e2009-03-05 23:01:23 +0000730 monitor_printf(mon, "HPET is %s by QEMU\n",
Luiz Capitulino14f07202009-12-10 17:16:02 -0200731 qdict_get_bool(qobject_to_qdict(data), "enabled") ?
732 "enabled" : "disabled");
733}
734
735/**
736 * do_info_hpet(): Show HPET state
737 *
738 * Return a QDict with the following information:
739 *
740 * - "enabled": true if hpet if enabled, false otherwise
741 *
742 * Example:
743 *
744 * { "enabled": true }
745 */
746static void do_info_hpet(Monitor *mon, QObject **ret_data)
747{
748 *ret_data = qobject_from_jsonf("{ 'enabled': %i }", !no_hpet);
aliguori16b29ae2008-12-17 23:28:44 +0000749}
aurel32bf4f74c2008-12-18 22:42:34 +0000750#endif
aliguori16b29ae2008-12-17 23:28:44 +0000751
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200752static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000753{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200754 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
755}
756
757/**
758 * do_info_uuid(): Show VM UUID
759 *
760 * Return a QDict with the following information:
761 *
762 * - "UUID": Universally Unique Identifier
763 *
764 * Example:
765 *
766 * { "UUID": "550e8400-e29b-41d4-a716-446655440000" }
767 */
768static void do_info_uuid(Monitor *mon, QObject **ret_data)
769{
770 char uuid[64];
771
772 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000773 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
774 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
775 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
776 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200777 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000778}
779
bellard6a00d602005-11-21 23:25:50 +0000780/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000781static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000782{
783 CPUState *env;
784
785 for(env = first_cpu; env != NULL; env = env->next_cpu) {
786 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000787 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000788 return 0;
789 }
790 }
791 return -1;
792}
793
pbrook9596ebb2007-11-18 01:44:38 +0000794static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000795{
aliguori731b0362009-03-05 23:01:42 +0000796 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000797 mon_set_cpu(0);
798 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300799 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000800 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000801}
802
aliguori376253e2009-03-05 23:01:23 +0000803static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000804{
bellard6a00d602005-11-21 23:25:50 +0000805 CPUState *env;
806 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000807#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000808 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000809 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000810#else
aliguori376253e2009-03-05 23:01:23 +0000811 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000812 0);
bellard9307c4c2004-04-04 12:57:25 +0000813#endif
814}
815
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300816static void print_cpu_iter(QObject *obj, void *opaque)
817{
818 QDict *cpu;
819 int active = ' ';
820 Monitor *mon = opaque;
821
822 assert(qobject_type(obj) == QTYPE_QDICT);
823 cpu = qobject_to_qdict(obj);
824
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200825 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300826 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200827 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300828
829 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
830
831#if defined(TARGET_I386)
832 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
833 (target_ulong) qdict_get_int(cpu, "pc"));
834#elif defined(TARGET_PPC)
835 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
836 (target_long) qdict_get_int(cpu, "nip"));
837#elif defined(TARGET_SPARC)
838 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
839 (target_long) qdict_get_int(cpu, "pc"));
840 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
841 (target_long) qdict_get_int(cpu, "npc"));
842#elif defined(TARGET_MIPS)
843 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
844 (target_long) qdict_get_int(cpu, "PC"));
845#endif
846
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200847 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300848 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200849 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300850
851 monitor_printf(mon, "\n");
852}
853
854static void monitor_print_cpus(Monitor *mon, const QObject *data)
855{
856 QList *cpu_list;
857
858 assert(qobject_type(data) == QTYPE_QLIST);
859 cpu_list = qobject_to_qlist(data);
860 qlist_iter(cpu_list, print_cpu_iter, mon);
861}
862
863/**
864 * do_info_cpus(): Show CPU information
865 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200866 * Return a QList. Each CPU is represented by a QDict, which contains:
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300867 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200868 * - "cpu": CPU index
869 * - "current": true if this is the current CPU, false otherwise
870 * - "halted": true if the cpu is halted, false otherwise
871 * - Current program counter. The key's name depends on the architecture:
872 * "pc": i386/x86)64
873 * "nip": PPC
874 * "pc" and "npc": sparc
875 * "PC": mips
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300876 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200877 * Example:
878 *
879 * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 },
880 * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ]
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300881 */
882static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000883{
884 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300885 QList *cpu_list;
886
887 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000888
889 /* just to set the default cpu if not already done */
890 mon_get_cpu();
891
892 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200893 QDict *cpu;
894 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300895
Avi Kivity4c0960c2009-08-17 23:19:53 +0300896 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300897
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200898 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
899 env->cpu_index, env == mon->mon_cpu,
900 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200901
902 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300903
bellard6a00d602005-11-21 23:25:50 +0000904#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300905 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000906#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300907 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000908#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300909 qdict_put(cpu, "pc", qint_from_int(env->pc));
910 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000911#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300912 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000913#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300914
915 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000916 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300917
918 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000919}
920
Markus Armbruster81a1b452010-01-20 13:07:35 +0100921static void do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000922{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300923 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000924 if (mon_set_cpu(index) < 0)
Markus Armbrustercc0c4182010-01-29 10:34:00 +0100925 qemu_error_new(QERR_INVALID_PARAMETER, "index");
bellard6a00d602005-11-21 23:25:50 +0000926}
927
aliguori376253e2009-03-05 23:01:23 +0000928static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000929{
aliguori376253e2009-03-05 23:01:23 +0000930 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000931}
932
aliguori376253e2009-03-05 23:01:23 +0000933static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000934{
935 int i;
bellard7e2515e2004-08-01 21:52:19 +0000936 const char *str;
ths3b46e622007-09-17 08:09:54 +0000937
aliguoricde76ee2009-03-05 23:01:51 +0000938 if (!mon->rs)
939 return;
bellard7e2515e2004-08-01 21:52:19 +0000940 i = 0;
941 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000942 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000943 if (!str)
944 break;
aliguori376253e2009-03-05 23:01:23 +0000945 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000946 i++;
bellardaa455482004-04-04 13:07:25 +0000947 }
948}
949
j_mayer76a66252007-03-07 08:32:30 +0000950#if defined(TARGET_PPC)
951/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000952static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000953{
954 CPUState *env;
955
956 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000957 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000958}
959#endif
960
Luiz Capitulinob223f352009-10-07 13:41:56 -0300961/**
962 * do_quit(): Quit QEMU execution
963 */
964static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000965{
966 exit(0);
967}
968
aliguori376253e2009-03-05 23:01:23 +0000969static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000970{
971 if (bdrv_is_inserted(bs)) {
972 if (!force) {
973 if (!bdrv_is_removable(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100974 qemu_error_new(QERR_DEVICE_NOT_REMOVABLE,
975 bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000976 return -1;
977 }
978 if (bdrv_is_locked(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100979 qemu_error_new(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000980 return -1;
981 }
982 }
983 bdrv_close(bs);
984 }
985 return 0;
986}
987
Luiz Capitulinoe1c923a2009-10-16 12:23:49 -0300988static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000989{
990 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300991 int force = qdict_get_int(qdict, "force");
Luiz Capitulino78d714e2009-12-14 18:53:21 -0200992 const char *filename = qdict_get_str(qdict, "device");
bellard9dc39cb2004-03-14 21:38:27 +0000993
bellard9307c4c2004-04-04 12:57:25 +0000994 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000995 if (!bs) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100996 qemu_error_new(QERR_DEVICE_NOT_FOUND, filename);
bellard9dc39cb2004-03-14 21:38:27 +0000997 return;
998 }
aliguori376253e2009-03-05 23:01:23 +0000999 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +00001000}
1001
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001002static void do_block_set_passwd(Monitor *mon, const QDict *qdict,
1003 QObject **ret_data)
1004{
1005 BlockDriverState *bs;
1006
1007 bs = bdrv_find(qdict_get_str(qdict, "device"));
1008 if (!bs) {
1009 qemu_error_new(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
1010 return;
1011 }
1012
1013 if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) {
1014 qemu_error_new(QERR_INVALID_PASSWORD);
1015 }
1016}
1017
aliguori376253e2009-03-05 23:01:23 +00001018static void do_change_block(Monitor *mon, const char *device,
1019 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +00001020{
1021 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +00001022 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +00001023
bellard9307c4c2004-04-04 12:57:25 +00001024 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +00001025 if (!bs) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001026 qemu_error_new(QERR_DEVICE_NOT_FOUND, device);
bellard9dc39cb2004-03-14 21:38:27 +00001027 return;
1028 }
aurel322ecea9b2008-06-18 22:10:01 +00001029 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +01001030 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001031 if (!drv) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001032 qemu_error_new(QERR_INVALID_BLOCK_FORMAT, fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001033 return;
1034 }
1035 }
aliguori376253e2009-03-05 23:01:23 +00001036 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +00001037 return;
Naphtali Spreif5edb012010-01-17 16:48:13 +02001038 bdrv_open2(bs, filename, BDRV_O_RDWR, drv);
aliguori376253e2009-03-05 23:01:23 +00001039 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001040}
1041
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001042static void change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001043{
1044 if (vnc_display_password(NULL, password) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001045 qemu_error_new(QERR_SET_PASSWD_FAILED);
aliguoribb5fc202009-03-05 23:01:15 +00001046
Markus Armbruster2895e072009-12-07 21:37:01 +01001047}
1048
1049static void change_vnc_password_cb(Monitor *mon, const char *password,
1050 void *opaque)
1051{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001052 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001053 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001054}
1055
aliguori376253e2009-03-05 23:01:23 +00001056static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001057{
ths70848512007-08-25 01:37:05 +00001058 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001059 strcmp(target, "password") == 0) {
1060 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001061 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001062 strncpy(password, arg, sizeof(password));
1063 password[sizeof(password) - 1] = '\0';
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001064 change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001065 } else {
aliguori376253e2009-03-05 23:01:23 +00001066 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001067 }
ths70848512007-08-25 01:37:05 +00001068 } else {
aliguori28a76be2009-03-06 20:27:40 +00001069 if (vnc_display_open(NULL, target) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001070 qemu_error_new(QERR_VNC_SERVER_FAILED, target);
ths70848512007-08-25 01:37:05 +00001071 }
thse25a5822007-08-25 01:36:20 +00001072}
1073
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001074/**
1075 * do_change(): Change a removable medium, or VNC configuration
1076 */
1077static void do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001078{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001079 const char *device = qdict_get_str(qdict, "device");
1080 const char *target = qdict_get_str(qdict, "target");
1081 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +00001082 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001083 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001084 } else {
aliguori28a76be2009-03-06 20:27:40 +00001085 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001086 }
1087}
1088
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001089static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +00001090{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001091 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +00001092}
1093
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001094static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001095{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001096 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001097}
1098
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001099static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001100{
1101 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001102 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001103
bellard9307c4c2004-04-04 12:57:25 +00001104 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001105 mask = 0;
1106 } else {
bellard9307c4c2004-04-04 12:57:25 +00001107 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001108 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001109 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001110 return;
1111 }
1112 }
1113 cpu_set_log(mask);
1114}
1115
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001116static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001117{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001118 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001119 if (!option || !strcmp(option, "on")) {
1120 singlestep = 1;
1121 } else if (!strcmp(option, "off")) {
1122 singlestep = 0;
1123 } else {
1124 monitor_printf(mon, "unexpected option %s\n", option);
1125 }
1126}
1127
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001128/**
1129 * do_stop(): Stop VM execution
1130 */
1131static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001132{
1133 vm_stop(EXCP_INTERRUPT);
1134}
1135
aliguoribb5fc202009-03-05 23:01:15 +00001136static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001137
aliguori376253e2009-03-05 23:01:23 +00001138struct bdrv_iterate_context {
1139 Monitor *mon;
1140 int err;
1141};
aliguoric0f4ce72009-03-05 23:01:01 +00001142
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001143/**
1144 * do_cont(): Resume emulation.
1145 */
1146static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001147{
1148 struct bdrv_iterate_context context = { mon, 0 };
1149
1150 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001151 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +00001152 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +00001153 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +00001154}
1155
aliguoribb5fc202009-03-05 23:01:15 +00001156static void bdrv_key_cb(void *opaque, int err)
1157{
aliguori376253e2009-03-05 23:01:23 +00001158 Monitor *mon = opaque;
1159
aliguoribb5fc202009-03-05 23:01:15 +00001160 /* another key was set successfully, retry to continue */
1161 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001162 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001163}
1164
1165static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1166{
aliguori376253e2009-03-05 23:01:23 +00001167 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001168
aliguori376253e2009-03-05 23:01:23 +00001169 if (!context->err && bdrv_key_required(bs)) {
1170 context->err = -EBUSY;
1171 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1172 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001173 }
1174}
1175
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001176static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001177{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001178 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001179 if (!device)
1180 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1181 if (gdbserver_start(device) < 0) {
1182 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1183 device);
1184 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001185 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001186 } else {
aliguori59030a82009-04-05 18:43:41 +00001187 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1188 device);
bellard8a7ddc32004-03-31 19:00:16 +00001189 }
1190}
1191
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001192static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001193{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001194 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001195 if (select_watchdog_action(action) == -1) {
1196 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1197 }
1198}
1199
aliguori376253e2009-03-05 23:01:23 +00001200static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001201{
aliguori376253e2009-03-05 23:01:23 +00001202 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001203 switch(c) {
1204 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001205 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001206 break;
1207 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001208 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001209 break;
1210 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001211 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001212 break;
1213 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001214 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001215 break;
1216 default:
1217 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001218 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001219 } else {
aliguori376253e2009-03-05 23:01:23 +00001220 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001221 }
1222 break;
1223 }
aliguori376253e2009-03-05 23:01:23 +00001224 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001225}
1226
aliguori376253e2009-03-05 23:01:23 +00001227static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001228 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001229{
bellard6a00d602005-11-21 23:25:50 +00001230 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001231 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001232 uint8_t buf[16];
1233 uint64_t v;
1234
1235 if (format == 'i') {
1236 int flags;
1237 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001238 env = mon_get_cpu();
Markus Armbruster09b94182010-01-20 13:07:30 +01001239 if (!is_physical)
bellard6a00d602005-11-21 23:25:50 +00001240 return;
bellard9307c4c2004-04-04 12:57:25 +00001241#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001242 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001243 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001244 } else if (wsize == 4) {
1245 flags = 0;
1246 } else {
bellard6a15fd12006-04-12 21:07:07 +00001247 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001248 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001249 if (env) {
1250#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001251 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001252 (env->segs[R_CS].flags & DESC_L_MASK))
1253 flags = 2;
1254 else
1255#endif
1256 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1257 flags = 1;
1258 }
bellard4c27ba22004-04-25 18:05:08 +00001259 }
1260#endif
aliguori376253e2009-03-05 23:01:23 +00001261 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001262 return;
1263 }
1264
1265 len = wsize * count;
1266 if (wsize == 1)
1267 line_size = 8;
1268 else
1269 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001270 max_digits = 0;
1271
1272 switch(format) {
1273 case 'o':
1274 max_digits = (wsize * 8 + 2) / 3;
1275 break;
1276 default:
1277 case 'x':
1278 max_digits = (wsize * 8) / 4;
1279 break;
1280 case 'u':
1281 case 'd':
1282 max_digits = (wsize * 8 * 10 + 32) / 33;
1283 break;
1284 case 'c':
1285 wsize = 1;
1286 break;
1287 }
1288
1289 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001290 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001291 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001292 else
aliguori376253e2009-03-05 23:01:23 +00001293 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001294 l = len;
1295 if (l > line_size)
1296 l = line_size;
1297 if (is_physical) {
1298 cpu_physical_memory_rw(addr, buf, l, 0);
1299 } else {
bellard6a00d602005-11-21 23:25:50 +00001300 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001301 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001302 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001303 break;
1304 }
bellard9307c4c2004-04-04 12:57:25 +00001305 }
ths5fafdf22007-09-16 21:08:06 +00001306 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001307 while (i < l) {
1308 switch(wsize) {
1309 default:
1310 case 1:
1311 v = ldub_raw(buf + i);
1312 break;
1313 case 2:
1314 v = lduw_raw(buf + i);
1315 break;
1316 case 4:
bellard92a31b12005-02-10 22:00:52 +00001317 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001318 break;
1319 case 8:
1320 v = ldq_raw(buf + i);
1321 break;
1322 }
aliguori376253e2009-03-05 23:01:23 +00001323 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001324 switch(format) {
1325 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001326 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001327 break;
1328 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001329 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001330 break;
1331 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001332 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001333 break;
1334 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001335 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001336 break;
1337 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001338 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001339 break;
1340 }
1341 i += wsize;
1342 }
aliguori376253e2009-03-05 23:01:23 +00001343 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001344 addr += l;
1345 len -= l;
1346 }
1347}
1348
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001349static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001350{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001351 int count = qdict_get_int(qdict, "count");
1352 int format = qdict_get_int(qdict, "format");
1353 int size = qdict_get_int(qdict, "size");
1354 target_long addr = qdict_get_int(qdict, "addr");
1355
aliguori376253e2009-03-05 23:01:23 +00001356 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001357}
1358
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001359static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001360{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001361 int count = qdict_get_int(qdict, "count");
1362 int format = qdict_get_int(qdict, "format");
1363 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001364 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001365
aliguori376253e2009-03-05 23:01:23 +00001366 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001367}
1368
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001369static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001370{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001371 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001372 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001373
blueswir17743e582007-09-24 18:39:04 +00001374#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001375 switch(format) {
1376 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001377 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001378 break;
1379 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001380 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001381 break;
1382 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001383 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001384 break;
1385 default:
1386 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001387 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001388 break;
1389 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001390 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001391 break;
1392 }
bellard92a31b12005-02-10 22:00:52 +00001393#else
1394 switch(format) {
1395 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001396 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001397 break;
1398 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001399 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001400 break;
1401 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001402 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001403 break;
1404 default:
1405 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001406 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001407 break;
1408 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001409 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001410 break;
1411 }
1412#endif
aliguori376253e2009-03-05 23:01:23 +00001413 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001414}
1415
Luiz Capitulino57e09452009-10-16 12:23:43 -03001416static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001417{
1418 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001419 uint32_t size = qdict_get_int(qdict, "size");
1420 const char *filename = qdict_get_str(qdict, "filename");
1421 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001422 uint32_t l;
1423 CPUState *env;
1424 uint8_t buf[1024];
1425
1426 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001427
1428 f = fopen(filename, "wb");
1429 if (!f) {
Markus Armbrusterc34ed282010-01-20 13:07:32 +01001430 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
bellardb371dc52007-01-03 15:20:39 +00001431 return;
1432 }
1433 while (size != 0) {
1434 l = sizeof(buf);
1435 if (l > size)
1436 l = size;
1437 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001438 if (fwrite(buf, 1, l, f) != l) {
1439 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1440 goto exit;
1441 }
bellardb371dc52007-01-03 15:20:39 +00001442 addr += l;
1443 size -= l;
1444 }
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001445exit:
bellardb371dc52007-01-03 15:20:39 +00001446 fclose(f);
1447}
1448
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001449static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1450 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001451{
1452 FILE *f;
1453 uint32_t l;
1454 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001455 uint32_t size = qdict_get_int(qdict, "size");
1456 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001457 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +00001458
1459 f = fopen(filename, "wb");
1460 if (!f) {
Markus Armbruster95fada82010-01-20 13:07:33 +01001461 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
aurel32a8bdf7a2008-04-11 21:36:14 +00001462 return;
1463 }
1464 while (size != 0) {
1465 l = sizeof(buf);
1466 if (l > size)
1467 l = size;
1468 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001469 if (fwrite(buf, 1, l, f) != l) {
1470 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1471 goto exit;
1472 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001473 fflush(f);
1474 addr += l;
1475 size -= l;
1476 }
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001477exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001478 fclose(f);
1479}
1480
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001481static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001482{
1483 uint32_t addr;
1484 uint8_t buf[1];
1485 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001486 uint32_t start = qdict_get_int(qdict, "start");
1487 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001488
1489 sum = 0;
1490 for(addr = start; addr < (start + size); addr++) {
1491 cpu_physical_memory_rw(addr, buf, 1, 0);
1492 /* BSD sum algorithm ('sum' Unix command) */
1493 sum = (sum >> 1) | (sum << 15);
1494 sum += buf[0];
1495 }
aliguori376253e2009-03-05 23:01:23 +00001496 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001497}
1498
bellarda3a91a32004-06-04 11:06:21 +00001499typedef struct {
1500 int keycode;
1501 const char *name;
1502} KeyDef;
1503
1504static const KeyDef key_defs[] = {
1505 { 0x2a, "shift" },
1506 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001507
bellarda3a91a32004-06-04 11:06:21 +00001508 { 0x38, "alt" },
1509 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001510 { 0x64, "altgr" },
1511 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001512 { 0x1d, "ctrl" },
1513 { 0x9d, "ctrl_r" },
1514
1515 { 0xdd, "menu" },
1516
1517 { 0x01, "esc" },
1518
1519 { 0x02, "1" },
1520 { 0x03, "2" },
1521 { 0x04, "3" },
1522 { 0x05, "4" },
1523 { 0x06, "5" },
1524 { 0x07, "6" },
1525 { 0x08, "7" },
1526 { 0x09, "8" },
1527 { 0x0a, "9" },
1528 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001529 { 0x0c, "minus" },
1530 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001531 { 0x0e, "backspace" },
1532
1533 { 0x0f, "tab" },
1534 { 0x10, "q" },
1535 { 0x11, "w" },
1536 { 0x12, "e" },
1537 { 0x13, "r" },
1538 { 0x14, "t" },
1539 { 0x15, "y" },
1540 { 0x16, "u" },
1541 { 0x17, "i" },
1542 { 0x18, "o" },
1543 { 0x19, "p" },
1544
1545 { 0x1c, "ret" },
1546
1547 { 0x1e, "a" },
1548 { 0x1f, "s" },
1549 { 0x20, "d" },
1550 { 0x21, "f" },
1551 { 0x22, "g" },
1552 { 0x23, "h" },
1553 { 0x24, "j" },
1554 { 0x25, "k" },
1555 { 0x26, "l" },
1556
1557 { 0x2c, "z" },
1558 { 0x2d, "x" },
1559 { 0x2e, "c" },
1560 { 0x2f, "v" },
1561 { 0x30, "b" },
1562 { 0x31, "n" },
1563 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001564 { 0x33, "comma" },
1565 { 0x34, "dot" },
1566 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001567
balrog4d3b6f62008-02-10 16:33:14 +00001568 { 0x37, "asterisk" },
1569
bellarda3a91a32004-06-04 11:06:21 +00001570 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001571 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001572 { 0x3b, "f1" },
1573 { 0x3c, "f2" },
1574 { 0x3d, "f3" },
1575 { 0x3e, "f4" },
1576 { 0x3f, "f5" },
1577 { 0x40, "f6" },
1578 { 0x41, "f7" },
1579 { 0x42, "f8" },
1580 { 0x43, "f9" },
1581 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001582 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001583 { 0x46, "scroll_lock" },
1584
bellard64866c32006-05-07 18:03:31 +00001585 { 0xb5, "kp_divide" },
1586 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001587 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001588 { 0x4e, "kp_add" },
1589 { 0x9c, "kp_enter" },
1590 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001591 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001592
1593 { 0x52, "kp_0" },
1594 { 0x4f, "kp_1" },
1595 { 0x50, "kp_2" },
1596 { 0x51, "kp_3" },
1597 { 0x4b, "kp_4" },
1598 { 0x4c, "kp_5" },
1599 { 0x4d, "kp_6" },
1600 { 0x47, "kp_7" },
1601 { 0x48, "kp_8" },
1602 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001603
bellarda3a91a32004-06-04 11:06:21 +00001604 { 0x56, "<" },
1605
1606 { 0x57, "f11" },
1607 { 0x58, "f12" },
1608
1609 { 0xb7, "print" },
1610
1611 { 0xc7, "home" },
1612 { 0xc9, "pgup" },
1613 { 0xd1, "pgdn" },
1614 { 0xcf, "end" },
1615
1616 { 0xcb, "left" },
1617 { 0xc8, "up" },
1618 { 0xd0, "down" },
1619 { 0xcd, "right" },
1620
1621 { 0xd2, "insert" },
1622 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001623#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1624 { 0xf0, "stop" },
1625 { 0xf1, "again" },
1626 { 0xf2, "props" },
1627 { 0xf3, "undo" },
1628 { 0xf4, "front" },
1629 { 0xf5, "copy" },
1630 { 0xf6, "open" },
1631 { 0xf7, "paste" },
1632 { 0xf8, "find" },
1633 { 0xf9, "cut" },
1634 { 0xfa, "lf" },
1635 { 0xfb, "help" },
1636 { 0xfc, "meta_l" },
1637 { 0xfd, "meta_r" },
1638 { 0xfe, "compose" },
1639#endif
bellarda3a91a32004-06-04 11:06:21 +00001640 { 0, NULL },
1641};
1642
1643static int get_keycode(const char *key)
1644{
1645 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001646 char *endp;
1647 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001648
1649 for(p = key_defs; p->name != NULL; p++) {
1650 if (!strcmp(key, p->name))
1651 return p->keycode;
1652 }
bellard64866c32006-05-07 18:03:31 +00001653 if (strstart(key, "0x", NULL)) {
1654 ret = strtoul(key, &endp, 0);
1655 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1656 return ret;
1657 }
bellarda3a91a32004-06-04 11:06:21 +00001658 return -1;
1659}
1660
balrogc8256f92008-06-08 22:45:01 +00001661#define MAX_KEYCODES 16
1662static uint8_t keycodes[MAX_KEYCODES];
1663static int nb_pending_keycodes;
1664static QEMUTimer *key_timer;
1665
1666static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001667{
balrogc8256f92008-06-08 22:45:01 +00001668 int keycode;
1669
1670 while (nb_pending_keycodes > 0) {
1671 nb_pending_keycodes--;
1672 keycode = keycodes[nb_pending_keycodes];
1673 if (keycode & 0x80)
1674 kbd_put_keycode(0xe0);
1675 kbd_put_keycode(keycode | 0x80);
1676 }
1677}
1678
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001679static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001680{
balrog3401c0d2008-06-04 10:05:59 +00001681 char keyname_buf[16];
1682 char *separator;
1683 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001684 const char *string = qdict_get_str(qdict, "string");
1685 int has_hold_time = qdict_haskey(qdict, "hold_time");
1686 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001687
balrogc8256f92008-06-08 22:45:01 +00001688 if (nb_pending_keycodes > 0) {
1689 qemu_del_timer(key_timer);
1690 release_keys(NULL);
1691 }
1692 if (!has_hold_time)
1693 hold_time = 100;
1694 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001695 while (1) {
1696 separator = strchr(string, '-');
1697 keyname_len = separator ? separator - string : strlen(string);
1698 if (keyname_len > 0) {
1699 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1700 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001701 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001702 return;
bellarda3a91a32004-06-04 11:06:21 +00001703 }
balrogc8256f92008-06-08 22:45:01 +00001704 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001705 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001706 return;
1707 }
1708 keyname_buf[keyname_len] = 0;
1709 keycode = get_keycode(keyname_buf);
1710 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001711 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001712 return;
1713 }
balrogc8256f92008-06-08 22:45:01 +00001714 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001715 }
balrog3401c0d2008-06-04 10:05:59 +00001716 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001717 break;
balrog3401c0d2008-06-04 10:05:59 +00001718 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001719 }
balrogc8256f92008-06-08 22:45:01 +00001720 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001721 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001722 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001723 keycode = keycodes[i];
1724 if (keycode & 0x80)
1725 kbd_put_keycode(0xe0);
1726 kbd_put_keycode(keycode & 0x7f);
1727 }
balrogc8256f92008-06-08 22:45:01 +00001728 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001729 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001730 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001731}
1732
bellard13224a82006-07-14 22:03:35 +00001733static int mouse_button_state;
1734
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001735static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001736{
1737 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001738 const char *dx_str = qdict_get_str(qdict, "dx_str");
1739 const char *dy_str = qdict_get_str(qdict, "dy_str");
1740 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001741 dx = strtol(dx_str, NULL, 0);
1742 dy = strtol(dy_str, NULL, 0);
1743 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001744 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001745 dz = strtol(dz_str, NULL, 0);
1746 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1747}
1748
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001749static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001750{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001751 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001752 mouse_button_state = button_state;
1753 kbd_mouse_event(0, 0, 0, mouse_button_state);
1754}
1755
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001756static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001757{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001758 int size = qdict_get_int(qdict, "size");
1759 int addr = qdict_get_int(qdict, "addr");
1760 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001761 uint32_t val;
1762 int suffix;
1763
1764 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001765 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001766 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001767 addr++;
1768 }
1769 addr &= 0xffff;
1770
1771 switch(size) {
1772 default:
1773 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001774 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001775 suffix = 'b';
1776 break;
1777 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001778 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001779 suffix = 'w';
1780 break;
1781 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001782 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001783 suffix = 'l';
1784 break;
1785 }
aliguori376253e2009-03-05 23:01:23 +00001786 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1787 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001788}
bellarda3a91a32004-06-04 11:06:21 +00001789
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001790static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001791{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001792 int size = qdict_get_int(qdict, "size");
1793 int addr = qdict_get_int(qdict, "addr");
1794 int val = qdict_get_int(qdict, "val");
1795
Jan Kiszkaf1147842009-07-14 10:20:11 +02001796 addr &= IOPORTS_MASK;
1797
1798 switch (size) {
1799 default:
1800 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001801 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001802 break;
1803 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001804 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001805 break;
1806 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001807 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001808 break;
1809 }
1810}
1811
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001812static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001813{
1814 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001815 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001816
Jan Kiszka76e30d02009-07-02 00:19:02 +02001817 res = qemu_boot_set(bootdevice);
1818 if (res == 0) {
1819 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1820 } else if (res > 0) {
1821 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001822 } else {
aliguori376253e2009-03-05 23:01:23 +00001823 monitor_printf(mon, "no function defined to set boot device list for "
1824 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001825 }
1826}
1827
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001828/**
1829 * do_system_reset(): Issue a machine reset
1830 */
1831static void do_system_reset(Monitor *mon, const QDict *qdict,
1832 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001833{
1834 qemu_system_reset_request();
1835}
1836
Luiz Capitulino43076662009-10-07 13:41:59 -03001837/**
1838 * do_system_powerdown(): Issue a machine powerdown
1839 */
1840static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1841 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001842{
1843 qemu_system_powerdown_request();
1844}
1845
bellardb86bda52004-09-18 19:32:46 +00001846#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001847static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001848{
aliguori376253e2009-03-05 23:01:23 +00001849 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1850 addr,
1851 pte & mask,
1852 pte & PG_GLOBAL_MASK ? 'G' : '-',
1853 pte & PG_PSE_MASK ? 'P' : '-',
1854 pte & PG_DIRTY_MASK ? 'D' : '-',
1855 pte & PG_ACCESSED_MASK ? 'A' : '-',
1856 pte & PG_PCD_MASK ? 'C' : '-',
1857 pte & PG_PWT_MASK ? 'T' : '-',
1858 pte & PG_USER_MASK ? 'U' : '-',
1859 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001860}
1861
aliguori376253e2009-03-05 23:01:23 +00001862static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001863{
bellard6a00d602005-11-21 23:25:50 +00001864 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001865 int l1, l2;
1866 uint32_t pgd, pde, pte;
1867
bellard6a00d602005-11-21 23:25:50 +00001868 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001869
bellardb86bda52004-09-18 19:32:46 +00001870 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001871 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001872 return;
1873 }
1874 pgd = env->cr[3] & ~0xfff;
1875 for(l1 = 0; l1 < 1024; l1++) {
1876 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1877 pde = le32_to_cpu(pde);
1878 if (pde & PG_PRESENT_MASK) {
1879 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001880 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001881 } else {
1882 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001883 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001884 (uint8_t *)&pte, 4);
1885 pte = le32_to_cpu(pte);
1886 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001887 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001888 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001889 ~0xfff);
1890 }
1891 }
1892 }
1893 }
1894 }
1895}
1896
aliguori376253e2009-03-05 23:01:23 +00001897static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001898 uint32_t end, int prot)
1899{
bellard9746b152004-11-11 18:30:24 +00001900 int prot1;
1901 prot1 = *plast_prot;
1902 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001903 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001904 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1905 *pstart, end, end - *pstart,
1906 prot1 & PG_USER_MASK ? 'u' : '-',
1907 'r',
1908 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001909 }
1910 if (prot != 0)
1911 *pstart = end;
1912 else
1913 *pstart = -1;
1914 *plast_prot = prot;
1915 }
1916}
1917
aliguori376253e2009-03-05 23:01:23 +00001918static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001919{
bellard6a00d602005-11-21 23:25:50 +00001920 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001921 int l1, l2, prot, last_prot;
1922 uint32_t pgd, pde, pte, start, end;
1923
bellard6a00d602005-11-21 23:25:50 +00001924 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001925
bellardb86bda52004-09-18 19:32:46 +00001926 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001927 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001928 return;
1929 }
1930 pgd = env->cr[3] & ~0xfff;
1931 last_prot = 0;
1932 start = -1;
1933 for(l1 = 0; l1 < 1024; l1++) {
1934 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1935 pde = le32_to_cpu(pde);
1936 end = l1 << 22;
1937 if (pde & PG_PRESENT_MASK) {
1938 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1939 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001940 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001941 } else {
1942 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001943 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001944 (uint8_t *)&pte, 4);
1945 pte = le32_to_cpu(pte);
1946 end = (l1 << 22) + (l2 << 12);
1947 if (pte & PG_PRESENT_MASK) {
1948 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1949 } else {
1950 prot = 0;
1951 }
aliguori376253e2009-03-05 23:01:23 +00001952 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001953 }
1954 }
1955 } else {
1956 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001957 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001958 }
1959 }
1960}
1961#endif
1962
aurel327c664e22009-03-03 06:12:22 +00001963#if defined(TARGET_SH4)
1964
aliguori376253e2009-03-05 23:01:23 +00001965static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001966{
aliguori376253e2009-03-05 23:01:23 +00001967 monitor_printf(mon, " tlb%i:\t"
1968 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1969 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1970 "dirty=%hhu writethrough=%hhu\n",
1971 idx,
1972 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1973 tlb->v, tlb->sh, tlb->c, tlb->pr,
1974 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001975}
1976
aliguori376253e2009-03-05 23:01:23 +00001977static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001978{
1979 CPUState *env = mon_get_cpu();
1980 int i;
1981
aliguori376253e2009-03-05 23:01:23 +00001982 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001983 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001984 print_tlb (mon, i, &env->itlb[i]);
1985 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001986 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001987 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001988}
1989
1990#endif
1991
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001992static void do_info_kvm_print(Monitor *mon, const QObject *data)
1993{
1994 QDict *qdict;
1995
1996 qdict = qobject_to_qdict(data);
1997
1998 monitor_printf(mon, "kvm support: ");
1999 if (qdict_get_bool(qdict, "present")) {
2000 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2001 "enabled" : "disabled");
2002 } else {
2003 monitor_printf(mon, "not compiled\n");
2004 }
2005}
2006
2007/**
2008 * do_info_kvm(): Show KVM information
2009 *
2010 * Return a QDict with the following information:
2011 *
2012 * - "enabled": true if KVM support is enabled, false otherwise
2013 * - "present": true if QEMU has KVM support, false otherwise
2014 *
2015 * Example:
2016 *
2017 * { "enabled": true, "present": true }
2018 */
2019static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002020{
2021#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002022 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2023 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002024#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002025 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002026#endif
2027}
2028
aliguori030ea372009-04-21 22:30:47 +00002029static void do_info_numa(Monitor *mon)
2030{
aliguorib28b6232009-04-22 20:20:29 +00002031 int i;
aliguori030ea372009-04-21 22:30:47 +00002032 CPUState *env;
2033
2034 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2035 for (i = 0; i < nb_numa_nodes; i++) {
2036 monitor_printf(mon, "node %d cpus:", i);
2037 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2038 if (env->numa_node == i) {
2039 monitor_printf(mon, " %d", env->cpu_index);
2040 }
2041 }
2042 monitor_printf(mon, "\n");
2043 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2044 node_mem[i] >> 20);
2045 }
2046}
2047
bellard5f1ce942006-02-08 22:40:15 +00002048#ifdef CONFIG_PROFILER
2049
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002050int64_t qemu_time;
2051int64_t dev_time;
2052
aliguori376253e2009-03-05 23:01:23 +00002053static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002054{
2055 int64_t total;
2056 total = qemu_time;
2057 if (total == 0)
2058 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002059 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002060 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002061 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002062 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002063 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002064 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002065}
2066#else
aliguori376253e2009-03-05 23:01:23 +00002067static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002068{
aliguori376253e2009-03-05 23:01:23 +00002069 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002070}
2071#endif
2072
bellardec36b692006-07-16 18:57:03 +00002073/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002074static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002075
aliguori376253e2009-03-05 23:01:23 +00002076static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002077{
2078 int i;
2079 CaptureState *s;
2080
2081 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002082 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002083 s->ops.info (s->opaque);
2084 }
2085}
2086
Blue Swirl23130862009-06-06 08:22:04 +00002087#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002088static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002089{
2090 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002091 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002092 CaptureState *s;
2093
2094 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2095 if (i == n) {
2096 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002097 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002098 qemu_free (s);
2099 return;
2100 }
2101 }
2102}
2103
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002104static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002105{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002106 const char *path = qdict_get_str(qdict, "path");
2107 int has_freq = qdict_haskey(qdict, "freq");
2108 int freq = qdict_get_try_int(qdict, "freq", -1);
2109 int has_bits = qdict_haskey(qdict, "bits");
2110 int bits = qdict_get_try_int(qdict, "bits", -1);
2111 int has_channels = qdict_haskey(qdict, "nchannels");
2112 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002113 CaptureState *s;
2114
2115 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002116
2117 freq = has_freq ? freq : 44100;
2118 bits = has_bits ? bits : 16;
2119 nchannels = has_channels ? nchannels : 2;
2120
2121 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002122 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002123 qemu_free (s);
2124 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002125 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002126}
2127#endif
2128
aurel32dc1c0b72008-04-27 23:52:12 +00002129#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002130static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002131{
2132 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002133 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002134
2135 for (env = first_cpu; env != NULL; env = env->next_cpu)
2136 if (env->cpu_index == cpu_index) {
2137 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2138 break;
2139 }
2140}
2141#endif
2142
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002143static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002144{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002145 QDict *qdict;
2146
2147 qdict = qobject_to_qdict(data);
2148
2149 monitor_printf(mon, "VM status: ");
2150 if (qdict_get_bool(qdict, "running")) {
2151 monitor_printf(mon, "running");
2152 if (qdict_get_bool(qdict, "singlestep")) {
2153 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002154 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002155 } else {
2156 monitor_printf(mon, "paused");
2157 }
2158
2159 monitor_printf(mon, "\n");
2160}
2161
2162/**
2163 * do_info_status(): VM status
2164 *
2165 * Return a QDict with the following information:
2166 *
2167 * - "running": true if the VM is running, or false if it is paused
2168 * - "singlestep": true if the VM is in single step mode, false otherwise
2169 *
2170 * Example:
2171 *
2172 * { "running": true, "singlestep": false }
2173 */
2174static void do_info_status(Monitor *mon, QObject **ret_data)
2175{
2176 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2177 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002178}
2179
Adam Litke625a5be2010-01-26 14:17:35 -06002180static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002181{
Adam Litke625a5be2010-01-26 14:17:35 -06002182 Monitor *mon = opaque;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002183
Adam Litke625a5be2010-01-26 14:17:35 -06002184 if (strcmp(key, "actual"))
2185 monitor_printf(mon, ",%s=%" PRId64, key,
2186 qint_get_int(qobject_to_qint(obj)));
aliguoridf751fa2008-12-04 20:19:35 +00002187}
2188
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002189static void monitor_print_balloon(Monitor *mon, const QObject *data)
2190{
Luiz Capitulino7f179672009-12-10 17:15:55 -02002191 QDict *qdict;
2192
2193 qdict = qobject_to_qdict(data);
Adam Litke625a5be2010-01-26 14:17:35 -06002194 if (!qdict_haskey(qdict, "actual"))
2195 return;
Luiz Capitulino7f179672009-12-10 17:15:55 -02002196
Adam Litke625a5be2010-01-26 14:17:35 -06002197 monitor_printf(mon, "balloon: actual=%" PRId64,
2198 qdict_get_int(qdict, "actual") >> 20);
2199 qdict_iter(qdict, print_balloon_stat, mon);
2200 monitor_printf(mon, "\n");
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002201}
2202
2203/**
2204 * do_info_balloon(): Balloon information
Luiz Capitulino7f179672009-12-10 17:15:55 -02002205 *
Adam Litke625a5be2010-01-26 14:17:35 -06002206 * Make an asynchronous request for balloon info. When the request completes
2207 * a QDict will be returned according to the following specification:
Luiz Capitulino7f179672009-12-10 17:15:55 -02002208 *
Adam Litke625a5be2010-01-26 14:17:35 -06002209 * - "actual": current balloon value in bytes
2210 * The following fields may or may not be present:
2211 * - "mem_swapped_in": Amount of memory swapped in (bytes)
2212 * - "mem_swapped_out": Amount of memory swapped out (bytes)
2213 * - "major_page_faults": Number of major faults
2214 * - "minor_page_faults": Number of minor faults
2215 * - "free_mem": Total amount of free and unused memory (bytes)
2216 * - "total_mem": Total amount of available memory (bytes)
Luiz Capitulino7f179672009-12-10 17:15:55 -02002217 *
2218 * Example:
2219 *
Adam Litke625a5be2010-01-26 14:17:35 -06002220 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
2221 * "major_page_faults": 142, "minor_page_faults": 239245,
2222 * "free_mem": 1014185984, "total_mem": 1044668416 }
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002223 */
Adam Litke625a5be2010-01-26 14:17:35 -06002224static int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +00002225{
Adam Litke625a5be2010-01-26 14:17:35 -06002226 int ret;
aliguoridf751fa2008-12-04 20:19:35 +00002227
Adam Litke625a5be2010-01-26 14:17:35 -06002228 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2229 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2230 return -1;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002231 }
Adam Litke625a5be2010-01-26 14:17:35 -06002232
2233 ret = qemu_balloon_status(cb, opaque);
2234 if (!ret) {
2235 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2236 return -1;
2237 }
2238
2239 return 0;
2240}
2241
2242/**
2243 * do_balloon(): Request VM to change its memory allocation
2244 */
2245static int do_balloon(Monitor *mon, const QDict *params,
2246 MonitorCompletion cb, void *opaque)
2247{
2248 int ret;
2249
2250 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2251 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2252 return -1;
2253 }
2254
2255 ret = qemu_balloon(qdict_get_int(params, "value"), cb, opaque);
2256 if (ret == 0) {
2257 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2258 return -1;
2259 }
2260
2261 return 0;
aliguoridf751fa2008-12-04 20:19:35 +00002262}
2263
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002264static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002265{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002266 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002267
aliguori76655d62009-03-06 20:27:37 +00002268 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002269 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002270 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002271 return acl;
2272}
aliguori76655d62009-03-06 20:27:37 +00002273
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002274static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002275{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002276 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002277 qemu_acl *acl = find_acl(mon, aclname);
2278 qemu_acl_entry *entry;
2279 int i = 0;
2280
2281 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002282 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002283 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002284 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002285 i++;
2286 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002287 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002288 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002289 }
2290}
2291
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002292static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002293{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002294 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002295 qemu_acl *acl = find_acl(mon, aclname);
2296
2297 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002298 qemu_acl_reset(acl);
2299 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002300 }
2301}
aliguori76655d62009-03-06 20:27:37 +00002302
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002303static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002304{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002305 const char *aclname = qdict_get_str(qdict, "aclname");
2306 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002307 qemu_acl *acl = find_acl(mon, aclname);
2308
2309 if (acl) {
2310 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002311 acl->defaultDeny = 0;
2312 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002313 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002314 acl->defaultDeny = 1;
2315 monitor_printf(mon, "acl: policy set to 'deny'\n");
2316 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002317 monitor_printf(mon, "acl: unknown policy '%s', "
2318 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002319 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002320 }
2321}
aliguori76655d62009-03-06 20:27:37 +00002322
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002323static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002324{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002325 const char *aclname = qdict_get_str(qdict, "aclname");
2326 const char *match = qdict_get_str(qdict, "match");
2327 const char *policy = qdict_get_str(qdict, "policy");
2328 int has_index = qdict_haskey(qdict, "index");
2329 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002330 qemu_acl *acl = find_acl(mon, aclname);
2331 int deny, ret;
2332
2333 if (acl) {
2334 if (strcmp(policy, "allow") == 0) {
2335 deny = 0;
2336 } else if (strcmp(policy, "deny") == 0) {
2337 deny = 1;
2338 } else {
2339 monitor_printf(mon, "acl: unknown policy '%s', "
2340 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002341 return;
2342 }
aliguori28a76be2009-03-06 20:27:40 +00002343 if (has_index)
2344 ret = qemu_acl_insert(acl, deny, match, index);
2345 else
2346 ret = qemu_acl_append(acl, deny, match);
2347 if (ret < 0)
2348 monitor_printf(mon, "acl: unable to add acl entry\n");
2349 else
2350 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002351 }
2352}
aliguori76655d62009-03-06 20:27:37 +00002353
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002354static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002355{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002356 const char *aclname = qdict_get_str(qdict, "aclname");
2357 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002358 qemu_acl *acl = find_acl(mon, aclname);
2359 int ret;
aliguori76655d62009-03-06 20:27:37 +00002360
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002361 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002362 ret = qemu_acl_remove(acl, match);
2363 if (ret < 0)
2364 monitor_printf(mon, "acl: no matching acl entry\n");
2365 else
2366 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002367 }
2368}
2369
Huang Ying79c4f6b2009-06-23 10:05:14 +08002370#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002371static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002372{
2373 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002374 int cpu_index = qdict_get_int(qdict, "cpu_index");
2375 int bank = qdict_get_int(qdict, "bank");
2376 uint64_t status = qdict_get_int(qdict, "status");
2377 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2378 uint64_t addr = qdict_get_int(qdict, "addr");
2379 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002380
2381 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2382 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2383 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2384 break;
2385 }
2386}
2387#endif
2388
Luiz Capitulinof0d60002009-10-16 12:23:50 -03002389static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002390{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002391 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002392 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002393 int fd;
2394
2395 fd = qemu_chr_get_msgfd(mon->chr);
2396 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002397 qemu_error_new(QERR_FD_NOT_SUPPLIED);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002398 return;
2399 }
2400
2401 if (qemu_isdigit(fdname[0])) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002402 qemu_error_new(QERR_INVALID_PARAMETER, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002403 return;
2404 }
2405
2406 fd = dup(fd);
2407 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002408 if (errno == EMFILE)
2409 qemu_error_new(QERR_TOO_MANY_FILES);
2410 else
2411 qemu_error_new(QERR_UNDEFINED_ERROR);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002412 return;
2413 }
2414
Blue Swirl72cf2d42009-09-12 07:36:22 +00002415 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002416 if (strcmp(monfd->name, fdname) != 0) {
2417 continue;
2418 }
2419
2420 close(monfd->fd);
2421 monfd->fd = fd;
2422 return;
2423 }
2424
Anthony Liguoric227f092009-10-01 16:12:16 -05002425 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002426 monfd->name = qemu_strdup(fdname);
2427 monfd->fd = fd;
2428
Blue Swirl72cf2d42009-09-12 07:36:22 +00002429 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002430}
2431
Luiz Capitulino18f3a512009-10-16 12:23:51 -03002432static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002433{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002434 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002435 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002436
Blue Swirl72cf2d42009-09-12 07:36:22 +00002437 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002438 if (strcmp(monfd->name, fdname) != 0) {
2439 continue;
2440 }
2441
Blue Swirl72cf2d42009-09-12 07:36:22 +00002442 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002443 close(monfd->fd);
2444 qemu_free(monfd->name);
2445 qemu_free(monfd);
2446 return;
2447 }
2448
Markus Armbruster063c1a02009-12-07 21:37:11 +01002449 qemu_error_new(QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002450}
2451
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002452static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002453{
2454 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002455 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002456
2457 vm_stop(0);
2458
Juan Quintela05f24012009-08-20 19:42:22 +02002459 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002460 vm_start();
2461}
2462
Mark McLoughlin7768e042009-07-22 09:11:41 +01002463int monitor_get_fd(Monitor *mon, const char *fdname)
2464{
Anthony Liguoric227f092009-10-01 16:12:16 -05002465 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002466
Blue Swirl72cf2d42009-09-12 07:36:22 +00002467 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002468 int fd;
2469
2470 if (strcmp(monfd->name, fdname) != 0) {
2471 continue;
2472 }
2473
2474 fd = monfd->fd;
2475
2476 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002477 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002478 qemu_free(monfd->name);
2479 qemu_free(monfd);
2480
2481 return fd;
2482 }
2483
2484 return -1;
2485}
2486
Anthony Liguoric227f092009-10-01 16:12:16 -05002487static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002488#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002489 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002490};
2491
Blue Swirl23130862009-06-06 08:22:04 +00002492/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002493static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002494 {
2495 .name = "version",
2496 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002497 .params = "",
2498 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002499 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002500 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002501 },
2502 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002503 .name = "commands",
2504 .args_type = "",
2505 .params = "",
2506 .help = "list QMP available commands",
2507 .user_print = monitor_user_noop,
2508 .mhandler.info_new = do_info_commands,
2509 },
2510 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002511 .name = "network",
2512 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002513 .params = "",
2514 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002515 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002516 },
2517 {
2518 .name = "chardev",
2519 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002520 .params = "",
2521 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002522 .user_print = qemu_chr_info_print,
2523 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002524 },
2525 {
2526 .name = "block",
2527 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002528 .params = "",
2529 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002530 .user_print = bdrv_info_print,
2531 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002532 },
2533 {
2534 .name = "blockstats",
2535 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002536 .params = "",
2537 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002538 .user_print = bdrv_stats_print,
2539 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002540 },
2541 {
2542 .name = "registers",
2543 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002544 .params = "",
2545 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002546 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002547 },
2548 {
2549 .name = "cpus",
2550 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002551 .params = "",
2552 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002553 .user_print = monitor_print_cpus,
2554 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002555 },
2556 {
2557 .name = "history",
2558 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002559 .params = "",
2560 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002561 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002562 },
2563 {
2564 .name = "irq",
2565 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002566 .params = "",
2567 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002568 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002569 },
2570 {
2571 .name = "pic",
2572 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002573 .params = "",
2574 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002575 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002576 },
2577 {
2578 .name = "pci",
2579 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 .params = "",
2581 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002582 .user_print = do_pci_info_print,
2583 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002584 },
aurel327c664e22009-03-03 06:12:22 +00002585#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002586 {
2587 .name = "tlb",
2588 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002589 .params = "",
2590 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002591 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002592 },
aurel327c664e22009-03-03 06:12:22 +00002593#endif
2594#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002595 {
2596 .name = "mem",
2597 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002598 .params = "",
2599 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002600 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002601 },
2602 {
2603 .name = "hpet",
2604 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002605 .params = "",
2606 .help = "show state of HPET",
Luiz Capitulino14f07202009-12-10 17:16:02 -02002607 .user_print = do_info_hpet_print,
2608 .mhandler.info_new = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002609 },
bellardb86bda52004-09-18 19:32:46 +00002610#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002611 {
2612 .name = "jit",
2613 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002614 .params = "",
2615 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002616 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002617 },
2618 {
2619 .name = "kvm",
2620 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002621 .params = "",
2622 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002623 .user_print = do_info_kvm_print,
2624 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002625 },
2626 {
2627 .name = "numa",
2628 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002629 .params = "",
2630 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002631 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002632 },
2633 {
2634 .name = "usb",
2635 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002636 .params = "",
2637 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002638 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002639 },
2640 {
2641 .name = "usbhost",
2642 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002643 .params = "",
2644 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002645 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002646 },
2647 {
2648 .name = "profile",
2649 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002650 .params = "",
2651 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002652 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002653 },
2654 {
2655 .name = "capture",
2656 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002657 .params = "",
2658 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002659 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002660 },
2661 {
2662 .name = "snapshots",
2663 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 .params = "",
2665 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002666 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002667 },
2668 {
2669 .name = "status",
2670 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 .params = "",
2672 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002673 .user_print = do_info_status_print,
2674 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002675 },
2676 {
2677 .name = "pcmcia",
2678 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002679 .params = "",
2680 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002681 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002682 },
2683 {
2684 .name = "mice",
2685 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002686 .params = "",
2687 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002688 .user_print = do_info_mice_print,
2689 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002690 },
2691 {
2692 .name = "vnc",
2693 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002694 .params = "",
2695 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002696 .user_print = do_info_vnc_print,
2697 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002698 },
2699 {
2700 .name = "name",
2701 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002702 .params = "",
2703 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002704 .user_print = do_info_name_print,
2705 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002706 },
2707 {
2708 .name = "uuid",
2709 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002710 .params = "",
2711 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002712 .user_print = do_info_uuid_print,
2713 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002714 },
j_mayer76a66252007-03-07 08:32:30 +00002715#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002716 {
2717 .name = "cpustats",
2718 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002719 .params = "",
2720 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002721 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002722 },
j_mayer76a66252007-03-07 08:32:30 +00002723#endif
blueswir131a60e22007-10-26 18:42:59 +00002724#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002725 {
2726 .name = "usernet",
2727 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002728 .params = "",
2729 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002730 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002731 },
blueswir131a60e22007-10-26 18:42:59 +00002732#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002733 {
2734 .name = "migrate",
2735 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002736 .params = "",
2737 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002738 .user_print = do_info_migrate_print,
2739 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002740 },
2741 {
2742 .name = "balloon",
2743 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002744 .params = "",
2745 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002746 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002747 .mhandler.info_async = do_info_balloon,
2748 .async = 1,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002749 },
2750 {
2751 .name = "qtree",
2752 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002753 .params = "",
2754 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002755 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002756 },
2757 {
2758 .name = "qdm",
2759 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 .params = "",
2761 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002762 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002763 },
2764 {
2765 .name = "roms",
2766 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 .params = "",
2768 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002769 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002770 },
2771 {
2772 .name = NULL,
2773 },
bellard9dc39cb2004-03-14 21:38:27 +00002774};
2775
bellard9307c4c2004-04-04 12:57:25 +00002776/*******************************************************************/
2777
2778static const char *pch;
2779static jmp_buf expr_env;
2780
bellard92a31b12005-02-10 22:00:52 +00002781#define MD_TLONG 0
2782#define MD_I32 1
2783
bellard9307c4c2004-04-04 12:57:25 +00002784typedef struct MonitorDef {
2785 const char *name;
2786 int offset;
blueswir18662d652008-10-02 18:32:44 +00002787 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002788 int type;
bellard9307c4c2004-04-04 12:57:25 +00002789} MonitorDef;
2790
bellard57206fd2004-04-25 18:54:52 +00002791#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002792static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002793{
bellard6a00d602005-11-21 23:25:50 +00002794 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002795 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002796}
2797#endif
2798
bellarda541f292004-04-12 20:39:29 +00002799#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002800static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002801{
bellard6a00d602005-11-21 23:25:50 +00002802 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002803 unsigned int u;
2804 int i;
2805
2806 u = 0;
2807 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002808 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002809
2810 return u;
2811}
2812
blueswir18662d652008-10-02 18:32:44 +00002813static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002814{
bellard6a00d602005-11-21 23:25:50 +00002815 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002816 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002817}
2818
blueswir18662d652008-10-02 18:32:44 +00002819static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002820{
bellard6a00d602005-11-21 23:25:50 +00002821 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002822 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002823}
bellard9fddaa02004-05-21 12:59:32 +00002824
blueswir18662d652008-10-02 18:32:44 +00002825static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002826{
bellard6a00d602005-11-21 23:25:50 +00002827 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002828 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002829}
2830
blueswir18662d652008-10-02 18:32:44 +00002831static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002832{
bellard6a00d602005-11-21 23:25:50 +00002833 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002834 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002835}
2836
blueswir18662d652008-10-02 18:32:44 +00002837static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002838{
bellard6a00d602005-11-21 23:25:50 +00002839 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002840 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002841}
bellarda541f292004-04-12 20:39:29 +00002842#endif
2843
bellarde95c8d52004-09-30 22:22:08 +00002844#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002845#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002846static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002847{
bellard6a00d602005-11-21 23:25:50 +00002848 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002849 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002850}
bellard7b936c02005-10-30 17:05:13 +00002851#endif
bellarde95c8d52004-09-30 22:22:08 +00002852
blueswir18662d652008-10-02 18:32:44 +00002853static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002854{
bellard6a00d602005-11-21 23:25:50 +00002855 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002856 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002857}
2858#endif
2859
blueswir18662d652008-10-02 18:32:44 +00002860static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002861#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002862
2863#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002864 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002865 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002866 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002867
bellard9307c4c2004-04-04 12:57:25 +00002868 { "eax", offsetof(CPUState, regs[0]) },
2869 { "ecx", offsetof(CPUState, regs[1]) },
2870 { "edx", offsetof(CPUState, regs[2]) },
2871 { "ebx", offsetof(CPUState, regs[3]) },
2872 { "esp|sp", offsetof(CPUState, regs[4]) },
2873 { "ebp|fp", offsetof(CPUState, regs[5]) },
2874 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002875 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002876#ifdef TARGET_X86_64
2877 { "r8", offsetof(CPUState, regs[8]) },
2878 { "r9", offsetof(CPUState, regs[9]) },
2879 { "r10", offsetof(CPUState, regs[10]) },
2880 { "r11", offsetof(CPUState, regs[11]) },
2881 { "r12", offsetof(CPUState, regs[12]) },
2882 { "r13", offsetof(CPUState, regs[13]) },
2883 { "r14", offsetof(CPUState, regs[14]) },
2884 { "r15", offsetof(CPUState, regs[15]) },
2885#endif
bellard9307c4c2004-04-04 12:57:25 +00002886 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002887 { "eip", offsetof(CPUState, eip) },
2888 SEG("cs", R_CS)
2889 SEG("ds", R_DS)
2890 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002891 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002892 SEG("fs", R_FS)
2893 SEG("gs", R_GS)
2894 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002895#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002896 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002897 { "r0", offsetof(CPUState, gpr[0]) },
2898 { "r1", offsetof(CPUState, gpr[1]) },
2899 { "r2", offsetof(CPUState, gpr[2]) },
2900 { "r3", offsetof(CPUState, gpr[3]) },
2901 { "r4", offsetof(CPUState, gpr[4]) },
2902 { "r5", offsetof(CPUState, gpr[5]) },
2903 { "r6", offsetof(CPUState, gpr[6]) },
2904 { "r7", offsetof(CPUState, gpr[7]) },
2905 { "r8", offsetof(CPUState, gpr[8]) },
2906 { "r9", offsetof(CPUState, gpr[9]) },
2907 { "r10", offsetof(CPUState, gpr[10]) },
2908 { "r11", offsetof(CPUState, gpr[11]) },
2909 { "r12", offsetof(CPUState, gpr[12]) },
2910 { "r13", offsetof(CPUState, gpr[13]) },
2911 { "r14", offsetof(CPUState, gpr[14]) },
2912 { "r15", offsetof(CPUState, gpr[15]) },
2913 { "r16", offsetof(CPUState, gpr[16]) },
2914 { "r17", offsetof(CPUState, gpr[17]) },
2915 { "r18", offsetof(CPUState, gpr[18]) },
2916 { "r19", offsetof(CPUState, gpr[19]) },
2917 { "r20", offsetof(CPUState, gpr[20]) },
2918 { "r21", offsetof(CPUState, gpr[21]) },
2919 { "r22", offsetof(CPUState, gpr[22]) },
2920 { "r23", offsetof(CPUState, gpr[23]) },
2921 { "r24", offsetof(CPUState, gpr[24]) },
2922 { "r25", offsetof(CPUState, gpr[25]) },
2923 { "r26", offsetof(CPUState, gpr[26]) },
2924 { "r27", offsetof(CPUState, gpr[27]) },
2925 { "r28", offsetof(CPUState, gpr[28]) },
2926 { "r29", offsetof(CPUState, gpr[29]) },
2927 { "r30", offsetof(CPUState, gpr[30]) },
2928 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002929 /* Floating point registers */
2930 { "f0", offsetof(CPUState, fpr[0]) },
2931 { "f1", offsetof(CPUState, fpr[1]) },
2932 { "f2", offsetof(CPUState, fpr[2]) },
2933 { "f3", offsetof(CPUState, fpr[3]) },
2934 { "f4", offsetof(CPUState, fpr[4]) },
2935 { "f5", offsetof(CPUState, fpr[5]) },
2936 { "f6", offsetof(CPUState, fpr[6]) },
2937 { "f7", offsetof(CPUState, fpr[7]) },
2938 { "f8", offsetof(CPUState, fpr[8]) },
2939 { "f9", offsetof(CPUState, fpr[9]) },
2940 { "f10", offsetof(CPUState, fpr[10]) },
2941 { "f11", offsetof(CPUState, fpr[11]) },
2942 { "f12", offsetof(CPUState, fpr[12]) },
2943 { "f13", offsetof(CPUState, fpr[13]) },
2944 { "f14", offsetof(CPUState, fpr[14]) },
2945 { "f15", offsetof(CPUState, fpr[15]) },
2946 { "f16", offsetof(CPUState, fpr[16]) },
2947 { "f17", offsetof(CPUState, fpr[17]) },
2948 { "f18", offsetof(CPUState, fpr[18]) },
2949 { "f19", offsetof(CPUState, fpr[19]) },
2950 { "f20", offsetof(CPUState, fpr[20]) },
2951 { "f21", offsetof(CPUState, fpr[21]) },
2952 { "f22", offsetof(CPUState, fpr[22]) },
2953 { "f23", offsetof(CPUState, fpr[23]) },
2954 { "f24", offsetof(CPUState, fpr[24]) },
2955 { "f25", offsetof(CPUState, fpr[25]) },
2956 { "f26", offsetof(CPUState, fpr[26]) },
2957 { "f27", offsetof(CPUState, fpr[27]) },
2958 { "f28", offsetof(CPUState, fpr[28]) },
2959 { "f29", offsetof(CPUState, fpr[29]) },
2960 { "f30", offsetof(CPUState, fpr[30]) },
2961 { "f31", offsetof(CPUState, fpr[31]) },
2962 { "fpscr", offsetof(CPUState, fpscr) },
2963 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002964 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002965 { "lr", offsetof(CPUState, lr) },
2966 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002967 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002968 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002969 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002970 { "msr", 0, &monitor_get_msr, },
2971 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002972 { "tbu", 0, &monitor_get_tbu, },
2973 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002974#if defined(TARGET_PPC64)
2975 /* Address space register */
2976 { "asr", offsetof(CPUState, asr) },
2977#endif
2978 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002979 { "sdr1", offsetof(CPUState, sdr1) },
2980 { "sr0", offsetof(CPUState, sr[0]) },
2981 { "sr1", offsetof(CPUState, sr[1]) },
2982 { "sr2", offsetof(CPUState, sr[2]) },
2983 { "sr3", offsetof(CPUState, sr[3]) },
2984 { "sr4", offsetof(CPUState, sr[4]) },
2985 { "sr5", offsetof(CPUState, sr[5]) },
2986 { "sr6", offsetof(CPUState, sr[6]) },
2987 { "sr7", offsetof(CPUState, sr[7]) },
2988 { "sr8", offsetof(CPUState, sr[8]) },
2989 { "sr9", offsetof(CPUState, sr[9]) },
2990 { "sr10", offsetof(CPUState, sr[10]) },
2991 { "sr11", offsetof(CPUState, sr[11]) },
2992 { "sr12", offsetof(CPUState, sr[12]) },
2993 { "sr13", offsetof(CPUState, sr[13]) },
2994 { "sr14", offsetof(CPUState, sr[14]) },
2995 { "sr15", offsetof(CPUState, sr[15]) },
2996 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002997#elif defined(TARGET_SPARC)
2998 { "g0", offsetof(CPUState, gregs[0]) },
2999 { "g1", offsetof(CPUState, gregs[1]) },
3000 { "g2", offsetof(CPUState, gregs[2]) },
3001 { "g3", offsetof(CPUState, gregs[3]) },
3002 { "g4", offsetof(CPUState, gregs[4]) },
3003 { "g5", offsetof(CPUState, gregs[5]) },
3004 { "g6", offsetof(CPUState, gregs[6]) },
3005 { "g7", offsetof(CPUState, gregs[7]) },
3006 { "o0", 0, monitor_get_reg },
3007 { "o1", 1, monitor_get_reg },
3008 { "o2", 2, monitor_get_reg },
3009 { "o3", 3, monitor_get_reg },
3010 { "o4", 4, monitor_get_reg },
3011 { "o5", 5, monitor_get_reg },
3012 { "o6", 6, monitor_get_reg },
3013 { "o7", 7, monitor_get_reg },
3014 { "l0", 8, monitor_get_reg },
3015 { "l1", 9, monitor_get_reg },
3016 { "l2", 10, monitor_get_reg },
3017 { "l3", 11, monitor_get_reg },
3018 { "l4", 12, monitor_get_reg },
3019 { "l5", 13, monitor_get_reg },
3020 { "l6", 14, monitor_get_reg },
3021 { "l7", 15, monitor_get_reg },
3022 { "i0", 16, monitor_get_reg },
3023 { "i1", 17, monitor_get_reg },
3024 { "i2", 18, monitor_get_reg },
3025 { "i3", 19, monitor_get_reg },
3026 { "i4", 20, monitor_get_reg },
3027 { "i5", 21, monitor_get_reg },
3028 { "i6", 22, monitor_get_reg },
3029 { "i7", 23, monitor_get_reg },
3030 { "pc", offsetof(CPUState, pc) },
3031 { "npc", offsetof(CPUState, npc) },
3032 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003033#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003034 { "psr", 0, &monitor_get_psr, },
3035 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003036#endif
bellarde95c8d52004-09-30 22:22:08 +00003037 { "tbr", offsetof(CPUState, tbr) },
3038 { "fsr", offsetof(CPUState, fsr) },
3039 { "f0", offsetof(CPUState, fpr[0]) },
3040 { "f1", offsetof(CPUState, fpr[1]) },
3041 { "f2", offsetof(CPUState, fpr[2]) },
3042 { "f3", offsetof(CPUState, fpr[3]) },
3043 { "f4", offsetof(CPUState, fpr[4]) },
3044 { "f5", offsetof(CPUState, fpr[5]) },
3045 { "f6", offsetof(CPUState, fpr[6]) },
3046 { "f7", offsetof(CPUState, fpr[7]) },
3047 { "f8", offsetof(CPUState, fpr[8]) },
3048 { "f9", offsetof(CPUState, fpr[9]) },
3049 { "f10", offsetof(CPUState, fpr[10]) },
3050 { "f11", offsetof(CPUState, fpr[11]) },
3051 { "f12", offsetof(CPUState, fpr[12]) },
3052 { "f13", offsetof(CPUState, fpr[13]) },
3053 { "f14", offsetof(CPUState, fpr[14]) },
3054 { "f15", offsetof(CPUState, fpr[15]) },
3055 { "f16", offsetof(CPUState, fpr[16]) },
3056 { "f17", offsetof(CPUState, fpr[17]) },
3057 { "f18", offsetof(CPUState, fpr[18]) },
3058 { "f19", offsetof(CPUState, fpr[19]) },
3059 { "f20", offsetof(CPUState, fpr[20]) },
3060 { "f21", offsetof(CPUState, fpr[21]) },
3061 { "f22", offsetof(CPUState, fpr[22]) },
3062 { "f23", offsetof(CPUState, fpr[23]) },
3063 { "f24", offsetof(CPUState, fpr[24]) },
3064 { "f25", offsetof(CPUState, fpr[25]) },
3065 { "f26", offsetof(CPUState, fpr[26]) },
3066 { "f27", offsetof(CPUState, fpr[27]) },
3067 { "f28", offsetof(CPUState, fpr[28]) },
3068 { "f29", offsetof(CPUState, fpr[29]) },
3069 { "f30", offsetof(CPUState, fpr[30]) },
3070 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003071#ifdef TARGET_SPARC64
3072 { "f32", offsetof(CPUState, fpr[32]) },
3073 { "f34", offsetof(CPUState, fpr[34]) },
3074 { "f36", offsetof(CPUState, fpr[36]) },
3075 { "f38", offsetof(CPUState, fpr[38]) },
3076 { "f40", offsetof(CPUState, fpr[40]) },
3077 { "f42", offsetof(CPUState, fpr[42]) },
3078 { "f44", offsetof(CPUState, fpr[44]) },
3079 { "f46", offsetof(CPUState, fpr[46]) },
3080 { "f48", offsetof(CPUState, fpr[48]) },
3081 { "f50", offsetof(CPUState, fpr[50]) },
3082 { "f52", offsetof(CPUState, fpr[52]) },
3083 { "f54", offsetof(CPUState, fpr[54]) },
3084 { "f56", offsetof(CPUState, fpr[56]) },
3085 { "f58", offsetof(CPUState, fpr[58]) },
3086 { "f60", offsetof(CPUState, fpr[60]) },
3087 { "f62", offsetof(CPUState, fpr[62]) },
3088 { "asi", offsetof(CPUState, asi) },
3089 { "pstate", offsetof(CPUState, pstate) },
3090 { "cansave", offsetof(CPUState, cansave) },
3091 { "canrestore", offsetof(CPUState, canrestore) },
3092 { "otherwin", offsetof(CPUState, otherwin) },
3093 { "wstate", offsetof(CPUState, wstate) },
3094 { "cleanwin", offsetof(CPUState, cleanwin) },
3095 { "fprs", offsetof(CPUState, fprs) },
3096#endif
bellard9307c4c2004-04-04 12:57:25 +00003097#endif
3098 { NULL },
3099};
3100
aliguori376253e2009-03-05 23:01:23 +00003101static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003102{
aliguori376253e2009-03-05 23:01:23 +00003103 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003104 longjmp(expr_env, 1);
3105}
3106
Markus Armbruster09b94182010-01-20 13:07:30 +01003107/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003108static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003109{
blueswir18662d652008-10-02 18:32:44 +00003110 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003111 void *ptr;
3112
bellard9307c4c2004-04-04 12:57:25 +00003113 for(md = monitor_defs; md->name != NULL; md++) {
3114 if (compare_cmd(name, md->name)) {
3115 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003116 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003117 } else {
bellard6a00d602005-11-21 23:25:50 +00003118 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003119 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003120 switch(md->type) {
3121 case MD_I32:
3122 *pval = *(int32_t *)ptr;
3123 break;
3124 case MD_TLONG:
3125 *pval = *(target_long *)ptr;
3126 break;
3127 default:
3128 *pval = 0;
3129 break;
3130 }
bellard9307c4c2004-04-04 12:57:25 +00003131 }
3132 return 0;
3133 }
3134 }
3135 return -1;
3136}
3137
3138static void next(void)
3139{
Blue Swirl660f11b2009-07-31 21:16:51 +00003140 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003141 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003142 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003143 pch++;
3144 }
3145}
3146
aliguori376253e2009-03-05 23:01:23 +00003147static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003148
aliguori376253e2009-03-05 23:01:23 +00003149static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003150{
blueswir1c2efc952007-09-25 17:28:42 +00003151 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003152 char *p;
bellard6a00d602005-11-21 23:25:50 +00003153 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003154
3155 switch(*pch) {
3156 case '+':
3157 next();
aliguori376253e2009-03-05 23:01:23 +00003158 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003159 break;
3160 case '-':
3161 next();
aliguori376253e2009-03-05 23:01:23 +00003162 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003163 break;
3164 case '~':
3165 next();
aliguori376253e2009-03-05 23:01:23 +00003166 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003167 break;
3168 case '(':
3169 next();
aliguori376253e2009-03-05 23:01:23 +00003170 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003171 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003172 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003173 }
3174 next();
3175 break;
bellard81d09122004-07-14 17:21:37 +00003176 case '\'':
3177 pch++;
3178 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003179 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003180 n = *pch;
3181 pch++;
3182 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003183 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003184 next();
3185 break;
bellard9307c4c2004-04-04 12:57:25 +00003186 case '$':
3187 {
3188 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003189 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003190
bellard9307c4c2004-04-04 12:57:25 +00003191 pch++;
3192 q = buf;
3193 while ((*pch >= 'a' && *pch <= 'z') ||
3194 (*pch >= 'A' && *pch <= 'Z') ||
3195 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003196 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003197 if ((q - buf) < sizeof(buf) - 1)
3198 *q++ = *pch;
3199 pch++;
3200 }
blueswir1cd390082008-11-16 13:53:32 +00003201 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003202 pch++;
3203 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003204 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003205 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003206 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003207 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003208 }
3209 break;
3210 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003211 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003212 n = 0;
3213 break;
3214 default:
blueswir17743e582007-09-24 18:39:04 +00003215#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003216 n = strtoull(pch, &p, 0);
3217#else
bellard9307c4c2004-04-04 12:57:25 +00003218 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003219#endif
bellard9307c4c2004-04-04 12:57:25 +00003220 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003221 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003222 }
3223 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003224 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003225 pch++;
3226 break;
3227 }
3228 return n;
3229}
3230
3231
aliguori376253e2009-03-05 23:01:23 +00003232static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003233{
blueswir1c2efc952007-09-25 17:28:42 +00003234 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003235 int op;
ths3b46e622007-09-17 08:09:54 +00003236
aliguori376253e2009-03-05 23:01:23 +00003237 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003238 for(;;) {
3239 op = *pch;
3240 if (op != '*' && op != '/' && op != '%')
3241 break;
3242 next();
aliguori376253e2009-03-05 23:01:23 +00003243 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003244 switch(op) {
3245 default:
3246 case '*':
3247 val *= val2;
3248 break;
3249 case '/':
3250 case '%':
ths5fafdf22007-09-16 21:08:06 +00003251 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003252 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003253 if (op == '/')
3254 val /= val2;
3255 else
3256 val %= val2;
3257 break;
3258 }
3259 }
3260 return val;
3261}
3262
aliguori376253e2009-03-05 23:01:23 +00003263static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003264{
blueswir1c2efc952007-09-25 17:28:42 +00003265 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003266 int op;
bellard9307c4c2004-04-04 12:57:25 +00003267
aliguori376253e2009-03-05 23:01:23 +00003268 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003269 for(;;) {
3270 op = *pch;
3271 if (op != '&' && op != '|' && op != '^')
3272 break;
3273 next();
aliguori376253e2009-03-05 23:01:23 +00003274 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003275 switch(op) {
3276 default:
3277 case '&':
3278 val &= val2;
3279 break;
3280 case '|':
3281 val |= val2;
3282 break;
3283 case '^':
3284 val ^= val2;
3285 break;
3286 }
3287 }
3288 return val;
3289}
3290
aliguori376253e2009-03-05 23:01:23 +00003291static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003292{
blueswir1c2efc952007-09-25 17:28:42 +00003293 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003294 int op;
bellard9307c4c2004-04-04 12:57:25 +00003295
aliguori376253e2009-03-05 23:01:23 +00003296 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003297 for(;;) {
3298 op = *pch;
3299 if (op != '+' && op != '-')
3300 break;
3301 next();
aliguori376253e2009-03-05 23:01:23 +00003302 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003303 if (op == '+')
3304 val += val2;
3305 else
3306 val -= val2;
3307 }
3308 return val;
3309}
3310
aliguori376253e2009-03-05 23:01:23 +00003311static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003312{
3313 pch = *pp;
3314 if (setjmp(expr_env)) {
3315 *pp = pch;
3316 return -1;
3317 }
blueswir1cd390082008-11-16 13:53:32 +00003318 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003319 pch++;
aliguori376253e2009-03-05 23:01:23 +00003320 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003321 *pp = pch;
3322 return 0;
3323}
3324
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003325static int get_double(Monitor *mon, double *pval, const char **pp)
3326{
3327 const char *p = *pp;
3328 char *tailp;
3329 double d;
3330
3331 d = strtod(p, &tailp);
3332 if (tailp == p) {
3333 monitor_printf(mon, "Number expected\n");
3334 return -1;
3335 }
3336 if (d != d || d - d != 0) {
3337 /* NaN or infinity */
3338 monitor_printf(mon, "Bad number\n");
3339 return -1;
3340 }
3341 *pval = d;
3342 *pp = tailp;
3343 return 0;
3344}
3345
bellard9307c4c2004-04-04 12:57:25 +00003346static int get_str(char *buf, int buf_size, const char **pp)
3347{
3348 const char *p;
3349 char *q;
3350 int c;
3351
bellard81d09122004-07-14 17:21:37 +00003352 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003353 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003354 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003355 p++;
3356 if (*p == '\0') {
3357 fail:
bellard81d09122004-07-14 17:21:37 +00003358 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003359 *pp = p;
3360 return -1;
3361 }
bellard9307c4c2004-04-04 12:57:25 +00003362 if (*p == '\"') {
3363 p++;
3364 while (*p != '\0' && *p != '\"') {
3365 if (*p == '\\') {
3366 p++;
3367 c = *p++;
3368 switch(c) {
3369 case 'n':
3370 c = '\n';
3371 break;
3372 case 'r':
3373 c = '\r';
3374 break;
3375 case '\\':
3376 case '\'':
3377 case '\"':
3378 break;
3379 default:
3380 qemu_printf("unsupported escape code: '\\%c'\n", c);
3381 goto fail;
3382 }
3383 if ((q - buf) < buf_size - 1) {
3384 *q++ = c;
3385 }
3386 } else {
3387 if ((q - buf) < buf_size - 1) {
3388 *q++ = *p;
3389 }
3390 p++;
3391 }
3392 }
3393 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003394 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003395 goto fail;
3396 }
3397 p++;
3398 } else {
blueswir1cd390082008-11-16 13:53:32 +00003399 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003400 if ((q - buf) < buf_size - 1) {
3401 *q++ = *p;
3402 }
3403 p++;
3404 }
bellard9307c4c2004-04-04 12:57:25 +00003405 }
bellard81d09122004-07-14 17:21:37 +00003406 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003407 *pp = p;
3408 return 0;
3409}
3410
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003411/*
3412 * Store the command-name in cmdname, and return a pointer to
3413 * the remaining of the command string.
3414 */
3415static const char *get_command_name(const char *cmdline,
3416 char *cmdname, size_t nlen)
3417{
3418 size_t len;
3419 const char *p, *pstart;
3420
3421 p = cmdline;
3422 while (qemu_isspace(*p))
3423 p++;
3424 if (*p == '\0')
3425 return NULL;
3426 pstart = p;
3427 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3428 p++;
3429 len = p - pstart;
3430 if (len > nlen - 1)
3431 len = nlen - 1;
3432 memcpy(cmdname, pstart, len);
3433 cmdname[len] = '\0';
3434 return p;
3435}
3436
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003437/**
3438 * Read key of 'type' into 'key' and return the current
3439 * 'type' pointer.
3440 */
3441static char *key_get_info(const char *type, char **key)
3442{
3443 size_t len;
3444 char *p, *str;
3445
3446 if (*type == ',')
3447 type++;
3448
3449 p = strchr(type, ':');
3450 if (!p) {
3451 *key = NULL;
3452 return NULL;
3453 }
3454 len = p - type;
3455
3456 str = qemu_malloc(len + 1);
3457 memcpy(str, type, len);
3458 str[len] = '\0';
3459
3460 *key = str;
3461 return ++p;
3462}
3463
bellard9307c4c2004-04-04 12:57:25 +00003464static int default_fmt_format = 'x';
3465static int default_fmt_size = 4;
3466
3467#define MAX_ARGS 16
3468
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003469static int is_valid_option(const char *c, const char *typestr)
3470{
3471 char option[3];
3472
3473 option[0] = '-';
3474 option[1] = *c;
3475 option[2] = '\0';
3476
3477 typestr = strstr(typestr, option);
3478 return (typestr != NULL);
3479}
3480
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003481static const mon_cmd_t *monitor_find_command(const char *cmdname)
3482{
3483 const mon_cmd_t *cmd;
3484
3485 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3486 if (compare_cmd(cmdname, cmd->name)) {
3487 return cmd;
3488 }
3489 }
3490
3491 return NULL;
3492}
3493
Anthony Liguoric227f092009-10-01 16:12:16 -05003494static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003495 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003496 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003497{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003498 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003499 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003500 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003501 char cmdname[256];
3502 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003503 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003504
3505#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003506 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003507#endif
ths3b46e622007-09-17 08:09:54 +00003508
bellard9307c4c2004-04-04 12:57:25 +00003509 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003510 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3511 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003512 return NULL;
ths3b46e622007-09-17 08:09:54 +00003513
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003514 cmd = monitor_find_command(cmdname);
3515 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003516 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003517 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003518 }
bellard9307c4c2004-04-04 12:57:25 +00003519
bellard9307c4c2004-04-04 12:57:25 +00003520 /* parse the parameters */
3521 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003522 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003523 typestr = key_get_info(typestr, &key);
3524 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003525 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003526 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003527 typestr++;
3528 switch(c) {
3529 case 'F':
bellard81d09122004-07-14 17:21:37 +00003530 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003531 case 's':
3532 {
3533 int ret;
ths3b46e622007-09-17 08:09:54 +00003534
blueswir1cd390082008-11-16 13:53:32 +00003535 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003536 p++;
3537 if (*typestr == '?') {
3538 typestr++;
3539 if (*p == '\0') {
3540 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003541 break;
bellard9307c4c2004-04-04 12:57:25 +00003542 }
3543 }
3544 ret = get_str(buf, sizeof(buf), &p);
3545 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003546 switch(c) {
3547 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003548 monitor_printf(mon, "%s: filename expected\n",
3549 cmdname);
bellard81d09122004-07-14 17:21:37 +00003550 break;
3551 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003552 monitor_printf(mon, "%s: block device name expected\n",
3553 cmdname);
bellard81d09122004-07-14 17:21:37 +00003554 break;
3555 default:
aliguori376253e2009-03-05 23:01:23 +00003556 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003557 break;
3558 }
bellard9307c4c2004-04-04 12:57:25 +00003559 goto fail;
3560 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003561 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003562 }
3563 break;
3564 case '/':
3565 {
3566 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003567
blueswir1cd390082008-11-16 13:53:32 +00003568 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003569 p++;
3570 if (*p == '/') {
3571 /* format found */
3572 p++;
3573 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003574 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003575 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003576 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003577 count = count * 10 + (*p - '0');
3578 p++;
3579 }
3580 }
3581 size = -1;
3582 format = -1;
3583 for(;;) {
3584 switch(*p) {
3585 case 'o':
3586 case 'd':
3587 case 'u':
3588 case 'x':
3589 case 'i':
3590 case 'c':
3591 format = *p++;
3592 break;
3593 case 'b':
3594 size = 1;
3595 p++;
3596 break;
3597 case 'h':
3598 size = 2;
3599 p++;
3600 break;
3601 case 'w':
3602 size = 4;
3603 p++;
3604 break;
3605 case 'g':
3606 case 'L':
3607 size = 8;
3608 p++;
3609 break;
3610 default:
3611 goto next;
3612 }
3613 }
3614 next:
blueswir1cd390082008-11-16 13:53:32 +00003615 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003616 monitor_printf(mon, "invalid char in format: '%c'\n",
3617 *p);
bellard9307c4c2004-04-04 12:57:25 +00003618 goto fail;
3619 }
bellard9307c4c2004-04-04 12:57:25 +00003620 if (format < 0)
3621 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003622 if (format != 'i') {
3623 /* for 'i', not specifying a size gives -1 as size */
3624 if (size < 0)
3625 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003626 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003627 }
bellard9307c4c2004-04-04 12:57:25 +00003628 default_fmt_format = format;
3629 } else {
3630 count = 1;
3631 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003632 if (format != 'i') {
3633 size = default_fmt_size;
3634 } else {
3635 size = -1;
3636 }
bellard9307c4c2004-04-04 12:57:25 +00003637 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003638 qdict_put(qdict, "count", qint_from_int(count));
3639 qdict_put(qdict, "format", qint_from_int(format));
3640 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003641 }
3642 break;
3643 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003644 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003645 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003646 {
blueswir1c2efc952007-09-25 17:28:42 +00003647 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003648
blueswir1cd390082008-11-16 13:53:32 +00003649 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003650 p++;
bellard34405572004-06-08 00:55:58 +00003651 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003652 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003653 if (*p == '\0') {
3654 typestr++;
3655 break;
3656 }
bellard34405572004-06-08 00:55:58 +00003657 } else {
3658 if (*p == '.') {
3659 p++;
blueswir1cd390082008-11-16 13:53:32 +00003660 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003661 p++;
bellard34405572004-06-08 00:55:58 +00003662 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003663 typestr++;
3664 break;
bellard34405572004-06-08 00:55:58 +00003665 }
3666 }
bellard13224a82006-07-14 22:03:35 +00003667 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003668 }
aliguori376253e2009-03-05 23:01:23 +00003669 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003670 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003671 /* Check if 'i' is greater than 32-bit */
3672 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3673 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3674 monitor_printf(mon, "integer is for 32-bit values\n");
3675 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003676 } else if (c == 'M') {
3677 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003678 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003679 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003680 }
3681 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003682 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003683 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003684 {
3685 double val;
3686
3687 while (qemu_isspace(*p))
3688 p++;
3689 if (*typestr == '?') {
3690 typestr++;
3691 if (*p == '\0') {
3692 break;
3693 }
3694 }
3695 if (get_double(mon, &val, &p) < 0) {
3696 goto fail;
3697 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003698 if (c == 'b' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003699 switch (*p) {
3700 case 'K': case 'k':
3701 val *= 1 << 10; p++; break;
3702 case 'M': case 'm':
3703 val *= 1 << 20; p++; break;
3704 case 'G': case 'g':
3705 val *= 1 << 30; p++; break;
3706 }
3707 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003708 if (c == 'T' && p[0] && p[1] == 's') {
3709 switch (*p) {
3710 case 'm':
3711 val /= 1e3; p += 2; break;
3712 case 'u':
3713 val /= 1e6; p += 2; break;
3714 case 'n':
3715 val /= 1e9; p += 2; break;
3716 }
3717 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003718 if (*p && !qemu_isspace(*p)) {
3719 monitor_printf(mon, "Unknown unit suffix\n");
3720 goto fail;
3721 }
3722 qdict_put(qdict, key, qfloat_from_double(val));
3723 }
3724 break;
bellard9307c4c2004-04-04 12:57:25 +00003725 case '-':
3726 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003727 const char *tmp = p;
3728 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003729 /* option */
ths3b46e622007-09-17 08:09:54 +00003730
bellard9307c4c2004-04-04 12:57:25 +00003731 c = *typestr++;
3732 if (c == '\0')
3733 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003734 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003735 p++;
3736 has_option = 0;
3737 if (*p == '-') {
3738 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003739 if(c != *p) {
3740 if(!is_valid_option(p, typestr)) {
3741
3742 monitor_printf(mon, "%s: unsupported option -%c\n",
3743 cmdname, *p);
3744 goto fail;
3745 } else {
3746 skip_key = 1;
3747 }
bellard9307c4c2004-04-04 12:57:25 +00003748 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003749 if(skip_key) {
3750 p = tmp;
3751 } else {
3752 p++;
3753 has_option = 1;
3754 }
bellard9307c4c2004-04-04 12:57:25 +00003755 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003756 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003757 }
3758 break;
3759 default:
3760 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003761 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003762 goto fail;
3763 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003764 qemu_free(key);
3765 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003766 }
3767 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003768 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003769 p++;
3770 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003771 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3772 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003773 goto fail;
3774 }
3775
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003776 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003777
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003778fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003779 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003780 return NULL;
3781}
3782
Luiz Capitulino8204a912009-11-18 23:05:31 -02003783static void monitor_print_error(Monitor *mon)
3784{
3785 qerror_print(mon->error);
3786 QDECREF(mon->error);
3787 mon->error = NULL;
3788}
3789
Adam Litke940cc302010-01-25 12:18:44 -06003790static int is_async_return(const QObject *data)
3791{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003792 if (data && qobject_type(data) == QTYPE_QDICT) {
3793 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3794 }
3795
3796 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003797}
3798
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003799static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3800 const QDict *params)
3801{
3802 QObject *data = NULL;
3803
3804 cmd->mhandler.cmd_new(mon, params, &data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003805
Adam Litke940cc302010-01-25 12:18:44 -06003806 if (is_async_return(data)) {
3807 /*
3808 * Asynchronous commands have no initial return data but they can
3809 * generate errors. Data is returned via the async completion handler.
3810 */
3811 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3812 monitor_protocol_emitter(mon, NULL);
3813 }
3814 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003815 /* Monitor Protocol */
3816 monitor_protocol_emitter(mon, data);
3817 } else {
3818 /* User Protocol */
3819 if (data)
3820 cmd->user_print(mon, data);
3821 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003822
3823 qobject_decref(data);
3824}
3825
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003826static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003827{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003828 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003829 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003830
3831 qdict = qdict_new();
3832
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003833 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003834 if (!cmd)
3835 goto out;
3836
3837 qemu_errors_to_mon(mon);
3838
Adam Litke940cc302010-01-25 12:18:44 -06003839 if (monitor_handler_is_async(cmd)) {
3840 user_async_cmd_handler(mon, cmd, qdict);
3841 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003842 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003843 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003844 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003845 }
3846
Luiz Capitulino8204a912009-11-18 23:05:31 -02003847 if (monitor_has_error(mon))
3848 monitor_print_error(mon);
3849
3850 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003851
3852out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003853 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003854}
3855
bellard81d09122004-07-14 17:21:37 +00003856static void cmd_completion(const char *name, const char *list)
3857{
3858 const char *p, *pstart;
3859 char cmd[128];
3860 int len;
3861
3862 p = list;
3863 for(;;) {
3864 pstart = p;
3865 p = strchr(p, '|');
3866 if (!p)
3867 p = pstart + strlen(pstart);
3868 len = p - pstart;
3869 if (len > sizeof(cmd) - 2)
3870 len = sizeof(cmd) - 2;
3871 memcpy(cmd, pstart, len);
3872 cmd[len] = '\0';
3873 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003874 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003875 }
3876 if (*p == '\0')
3877 break;
3878 p++;
3879 }
3880}
3881
3882static void file_completion(const char *input)
3883{
3884 DIR *ffs;
3885 struct dirent *d;
3886 char path[1024];
3887 char file[1024], file_prefix[1024];
3888 int input_path_len;
3889 const char *p;
3890
ths5fafdf22007-09-16 21:08:06 +00003891 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003892 if (!p) {
3893 input_path_len = 0;
3894 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003895 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003896 } else {
3897 input_path_len = p - input + 1;
3898 memcpy(path, input, input_path_len);
3899 if (input_path_len > sizeof(path) - 1)
3900 input_path_len = sizeof(path) - 1;
3901 path[input_path_len] = '\0';
3902 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3903 }
3904#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003905 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3906 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003907#endif
3908 ffs = opendir(path);
3909 if (!ffs)
3910 return;
3911 for(;;) {
3912 struct stat sb;
3913 d = readdir(ffs);
3914 if (!d)
3915 break;
3916 if (strstart(d->d_name, file_prefix, NULL)) {
3917 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003918 if (input_path_len < sizeof(file))
3919 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3920 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003921 /* stat the file to find out if it's a directory.
3922 * In that case add a slash to speed up typing long paths
3923 */
3924 stat(file, &sb);
3925 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003926 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003927 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003928 }
3929 }
3930 closedir(ffs);
3931}
3932
aliguori51de9762009-03-05 23:00:43 +00003933static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003934{
aliguori51de9762009-03-05 23:00:43 +00003935 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003936 const char *input = opaque;
3937
3938 if (input[0] == '\0' ||
3939 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003940 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003941 }
3942}
3943
3944/* NOTE: this parser is an approximate form of the real command parser */
3945static void parse_cmdline(const char *cmdline,
3946 int *pnb_args, char **args)
3947{
3948 const char *p;
3949 int nb_args, ret;
3950 char buf[1024];
3951
3952 p = cmdline;
3953 nb_args = 0;
3954 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003955 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003956 p++;
3957 if (*p == '\0')
3958 break;
3959 if (nb_args >= MAX_ARGS)
3960 break;
3961 ret = get_str(buf, sizeof(buf), &p);
3962 args[nb_args] = qemu_strdup(buf);
3963 nb_args++;
3964 if (ret < 0)
3965 break;
3966 }
3967 *pnb_args = nb_args;
3968}
3969
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003970static const char *next_arg_type(const char *typestr)
3971{
3972 const char *p = strchr(typestr, ':');
3973 return (p != NULL ? ++p : typestr);
3974}
3975
aliguori4c36ba32009-03-05 23:01:37 +00003976static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003977{
3978 const char *cmdname;
3979 char *args[MAX_ARGS];
3980 int nb_args, i, len;
3981 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003982 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003983 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003984
3985 parse_cmdline(cmdline, &nb_args, args);
3986#ifdef DEBUG_COMPLETION
3987 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003988 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003989 }
3990#endif
3991
3992 /* if the line ends with a space, it means we want to complete the
3993 next arg */
3994 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003995 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003996 if (nb_args >= MAX_ARGS)
3997 return;
3998 args[nb_args++] = qemu_strdup("");
3999 }
4000 if (nb_args <= 1) {
4001 /* command completion */
4002 if (nb_args == 0)
4003 cmdname = "";
4004 else
4005 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004006 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004007 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004008 cmd_completion(cmdname, cmd->name);
4009 }
4010 } else {
4011 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00004012 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004013 if (compare_cmd(args[0], cmd->name))
4014 goto found;
4015 }
4016 return;
4017 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004018 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004019 for(i = 0; i < nb_args - 2; i++) {
4020 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004021 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004022 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004023 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004024 }
4025 }
4026 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004027 if (*ptype == '-' && ptype[1] != '\0') {
4028 ptype += 2;
4029 }
bellard81d09122004-07-14 17:21:37 +00004030 switch(*ptype) {
4031 case 'F':
4032 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004033 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004034 file_completion(str);
4035 break;
4036 case 'B':
4037 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004038 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004039 bdrv_iterate(block_completion_it, (void *)str);
4040 break;
bellard7fe48482004-10-09 18:08:01 +00004041 case 's':
4042 /* XXX: more generic ? */
4043 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004044 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004045 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4046 cmd_completion(str, cmd->name);
4047 }
bellard64866c32006-05-07 18:03:31 +00004048 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004049 char *sep = strrchr(str, '-');
4050 if (sep)
4051 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004052 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004053 for(key = key_defs; key->name != NULL; key++) {
4054 cmd_completion(str, key->name);
4055 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004056 } else if (!strcmp(cmd->name, "help|?")) {
4057 readline_set_completion_index(cur_mon->rs, strlen(str));
4058 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4059 cmd_completion(str, cmd->name);
4060 }
bellard7fe48482004-10-09 18:08:01 +00004061 }
4062 break;
bellard81d09122004-07-14 17:21:37 +00004063 default:
4064 break;
4065 }
4066 }
4067 for(i = 0; i < nb_args; i++)
4068 qemu_free(args[i]);
4069}
4070
aliguori731b0362009-03-05 23:01:42 +00004071static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004072{
aliguori731b0362009-03-05 23:01:42 +00004073 Monitor *mon = opaque;
4074
Jan Kiszkac62313b2009-12-04 14:05:29 +01004075 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004076}
4077
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004078typedef struct CmdArgs {
4079 QString *name;
4080 int type;
4081 int flag;
4082 int optional;
4083} CmdArgs;
4084
4085static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
4086{
4087 if (!cmd_args->optional) {
4088 qemu_error_new(QERR_MISSING_PARAMETER, name);
4089 return -1;
4090 }
4091
4092 if (cmd_args->type == '-') {
4093 /* handlers expect a value, they need to be changed */
4094 qdict_put(args, name, qint_from_int(0));
4095 }
4096
4097 return 0;
4098}
4099
4100static int check_arg(const CmdArgs *cmd_args, QDict *args)
4101{
4102 QObject *value;
4103 const char *name;
4104
4105 name = qstring_get_str(cmd_args->name);
4106
4107 if (!args) {
4108 return check_opt(cmd_args, name, args);
4109 }
4110
4111 value = qdict_get(args, name);
4112 if (!value) {
4113 return check_opt(cmd_args, name, args);
4114 }
4115
4116 switch (cmd_args->type) {
4117 case 'F':
4118 case 'B':
4119 case 's':
4120 if (qobject_type(value) != QTYPE_QSTRING) {
4121 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "string");
4122 return -1;
4123 }
4124 break;
4125 case '/': {
4126 int i;
4127 const char *keys[] = { "count", "format", "size", NULL };
4128
4129 for (i = 0; keys[i]; i++) {
4130 QObject *obj = qdict_get(args, keys[i]);
4131 if (!obj) {
4132 qemu_error_new(QERR_MISSING_PARAMETER, name);
4133 return -1;
4134 }
4135 if (qobject_type(obj) != QTYPE_QINT) {
4136 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4137 return -1;
4138 }
4139 }
4140 break;
4141 }
4142 case 'i':
4143 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004144 case 'M':
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004145 if (qobject_type(value) != QTYPE_QINT) {
4146 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4147 return -1;
4148 }
4149 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004150 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004151 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004152 if (qobject_type(value) != QTYPE_QINT && qobject_type(value) != QTYPE_QFLOAT) {
4153 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "number");
4154 return -1;
4155 }
4156 break;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004157 case '-':
4158 if (qobject_type(value) != QTYPE_QINT &&
4159 qobject_type(value) != QTYPE_QBOOL) {
4160 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "bool");
4161 return -1;
4162 }
4163 if (qobject_type(value) == QTYPE_QBOOL) {
4164 /* handlers expect a QInt, they need to be changed */
4165 qdict_put(args, name,
4166 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
4167 }
4168 break;
4169 default:
4170 /* impossible */
4171 abort();
4172 }
4173
4174 return 0;
4175}
4176
4177static void cmd_args_init(CmdArgs *cmd_args)
4178{
4179 cmd_args->name = qstring_new();
4180 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
4181}
4182
4183/*
4184 * This is not trivial, we have to parse Monitor command's argument
4185 * type syntax to be able to check the arguments provided by clients.
4186 *
4187 * In the near future we will be using an array for that and will be
4188 * able to drop all this parsing...
4189 */
4190static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
4191{
4192 int err;
4193 const char *p;
4194 CmdArgs cmd_args;
4195
Blue Swirldd5121b2009-12-04 20:52:02 +00004196 if (cmd->args_type == NULL) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004197 return (qdict_size(args) == 0 ? 0 : -1);
4198 }
4199
4200 err = 0;
4201 cmd_args_init(&cmd_args);
4202
4203 for (p = cmd->args_type;; p++) {
4204 if (*p == ':') {
4205 cmd_args.type = *++p;
4206 p++;
4207 if (cmd_args.type == '-') {
4208 cmd_args.flag = *p++;
4209 cmd_args.optional = 1;
4210 } else if (*p == '?') {
4211 cmd_args.optional = 1;
4212 p++;
4213 }
4214
4215 assert(*p == ',' || *p == '\0');
4216 err = check_arg(&cmd_args, args);
4217
4218 QDECREF(cmd_args.name);
4219 cmd_args_init(&cmd_args);
4220
4221 if (err < 0) {
4222 break;
4223 }
4224 } else {
4225 qstring_append_chr(cmd_args.name, *p);
4226 }
4227
4228 if (*p == '\0') {
4229 break;
4230 }
4231 }
4232
4233 QDECREF(cmd_args.name);
4234 return err;
4235}
4236
Luiz Capitulino09069b12010-02-04 18:10:06 -02004237static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4238{
4239 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4240 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4241}
4242
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004243static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4244{
4245 int err;
4246 QObject *obj;
4247 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004248 const mon_cmd_t *cmd;
4249 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004250 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004251
4252 args = NULL;
4253 qemu_errors_to_mon(mon);
4254
4255 obj = json_parser_parse(tokens, NULL);
4256 if (!obj) {
4257 // FIXME: should be triggered in json_parser_parse()
4258 qemu_error_new(QERR_JSON_PARSING);
4259 goto err_out;
4260 } else if (qobject_type(obj) != QTYPE_QDICT) {
4261 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "object");
4262 qobject_decref(obj);
4263 goto err_out;
4264 }
4265
4266 input = qobject_to_qdict(obj);
4267
4268 mon->mc->id = qdict_get(input, "id");
4269 qobject_incref(mon->mc->id);
4270
4271 obj = qdict_get(input, "execute");
4272 if (!obj) {
4273 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4274 goto err_input;
4275 } else if (qobject_type(obj) != QTYPE_QSTRING) {
4276 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "string");
4277 goto err_input;
4278 }
4279
4280 cmd_name = qstring_get_str(qobject_to_qstring(obj));
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004281
Luiz Capitulino09069b12010-02-04 18:10:06 -02004282 if (invalid_qmp_mode(mon, cmd_name)) {
4283 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4284 goto err_input;
4285 }
4286
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004287 /*
4288 * XXX: We need this special case until we get info handlers
4289 * converted into 'query-' commands
4290 */
4291 if (compare_cmd(cmd_name, "info")) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004292 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4293 goto err_input;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004294 } else if (strstart(cmd_name, "query-", &info_item)) {
4295 cmd = monitor_find_command("info");
4296 qdict_put_obj(input, "arguments",
4297 qobject_from_jsonf("{ 'item': %s }", info_item));
4298 } else {
4299 cmd = monitor_find_command(cmd_name);
Luiz Capitulino43e713c2009-12-18 13:24:59 -02004300 if (!cmd || !monitor_handler_ported(cmd)) {
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004301 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4302 goto err_input;
4303 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004304 }
4305
4306 obj = qdict_get(input, "arguments");
4307 if (!obj) {
4308 args = qdict_new();
4309 } else {
4310 args = qobject_to_qdict(obj);
4311 QINCREF(args);
4312 }
4313
4314 QDECREF(input);
4315
4316 err = monitor_check_qmp_args(cmd, args);
4317 if (err < 0) {
4318 goto err_out;
4319 }
4320
Adam Litke940cc302010-01-25 12:18:44 -06004321 if (monitor_handler_is_async(cmd)) {
4322 qmp_async_cmd_handler(mon, cmd, args);
4323 } else {
4324 monitor_call_handler(mon, cmd, args);
4325 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004326 goto out;
4327
4328err_input:
4329 QDECREF(input);
4330err_out:
4331 monitor_protocol_emitter(mon, NULL);
4332out:
4333 QDECREF(args);
4334 qemu_errors_to_previous();
4335}
4336
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004337/**
4338 * monitor_control_read(): Read and handle QMP input
4339 */
4340static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4341{
4342 Monitor *old_mon = cur_mon;
4343
4344 cur_mon = opaque;
4345
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004346 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004347
4348 cur_mon = old_mon;
4349}
4350
aliguori731b0362009-03-05 23:01:42 +00004351static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004352{
aliguori731b0362009-03-05 23:01:42 +00004353 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004354 int i;
aliguori376253e2009-03-05 23:01:23 +00004355
aliguori731b0362009-03-05 23:01:42 +00004356 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004357
aliguoricde76ee2009-03-05 23:01:51 +00004358 if (cur_mon->rs) {
4359 for (i = 0; i < size; i++)
4360 readline_handle_byte(cur_mon->rs, buf[i]);
4361 } else {
4362 if (size == 0 || buf[size - 1] != 0)
4363 monitor_printf(cur_mon, "corrupted command\n");
4364 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004365 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004366 }
aliguori731b0362009-03-05 23:01:42 +00004367
4368 cur_mon = old_mon;
4369}
aliguorid8f44602008-10-06 13:52:44 +00004370
aliguori376253e2009-03-05 23:01:23 +00004371static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004372{
aliguori731b0362009-03-05 23:01:42 +00004373 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004374 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004375 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004376}
4377
aliguoricde76ee2009-03-05 23:01:51 +00004378int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004379{
aliguoricde76ee2009-03-05 23:01:51 +00004380 if (!mon->rs)
4381 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004382 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004383 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004384}
4385
aliguori376253e2009-03-05 23:01:23 +00004386void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004387{
aliguoricde76ee2009-03-05 23:01:51 +00004388 if (!mon->rs)
4389 return;
aliguori731b0362009-03-05 23:01:42 +00004390 if (--mon->suspend_cnt == 0)
4391 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004392}
4393
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004394static QObject *get_qmp_greeting(void)
4395{
4396 QObject *ver;
4397
4398 do_info_version(NULL, &ver);
4399 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4400}
4401
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004402/**
4403 * monitor_control_event(): Print QMP gretting
4404 */
4405static void monitor_control_event(void *opaque, int event)
4406{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004407 QObject *data;
4408 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004409
Luiz Capitulino47116d12010-02-08 17:01:30 -02004410 switch (event) {
4411 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004412 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004413 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004414 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004415 monitor_json_emitter(mon, data);
4416 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004417 break;
4418 case CHR_EVENT_CLOSED:
4419 json_message_parser_destroy(&mon->mc->parser);
4420 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004421 }
4422}
4423
aliguori731b0362009-03-05 23:01:42 +00004424static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004425{
aliguori376253e2009-03-05 23:01:23 +00004426 Monitor *mon = opaque;
4427
aliguori2724b182009-03-05 23:01:47 +00004428 switch (event) {
4429 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004430 mon->mux_out = 0;
4431 if (mon->reset_seen) {
4432 readline_restart(mon->rs);
4433 monitor_resume(mon);
4434 monitor_flush(mon);
4435 } else {
4436 mon->suspend_cnt = 0;
4437 }
aliguori2724b182009-03-05 23:01:47 +00004438 break;
ths86e94de2007-01-05 22:01:59 +00004439
aliguori2724b182009-03-05 23:01:47 +00004440 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004441 if (mon->reset_seen) {
4442 if (mon->suspend_cnt == 0) {
4443 monitor_printf(mon, "\n");
4444 }
4445 monitor_flush(mon);
4446 monitor_suspend(mon);
4447 } else {
4448 mon->suspend_cnt++;
4449 }
4450 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004451 break;
4452
Amit Shahb6b8df52009-10-07 18:31:16 +05304453 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004454 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4455 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004456 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004457 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004458 }
4459 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004460 break;
4461 }
ths86e94de2007-01-05 22:01:59 +00004462}
4463
aliguori76655d62009-03-06 20:27:37 +00004464
4465/*
4466 * Local variables:
4467 * c-indent-level: 4
4468 * c-basic-offset: 4
4469 * tab-width: 8
4470 * End:
4471 */
4472
aliguori731b0362009-03-05 23:01:42 +00004473void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004474{
aliguori731b0362009-03-05 23:01:42 +00004475 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004476 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004477
4478 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004479 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004480 is_first_init = 0;
4481 }
aliguori87127162009-03-05 23:01:29 +00004482
4483 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004484
aliguori87127162009-03-05 23:01:29 +00004485 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004486 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004487 if (flags & MONITOR_USE_READLINE) {
4488 mon->rs = readline_init(mon, monitor_find_completion);
4489 monitor_read_command(mon, 0);
4490 }
aliguori87127162009-03-05 23:01:29 +00004491
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004492 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004493 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004494 /* Control mode requires special handlers */
4495 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4496 monitor_control_event, mon);
4497 } else {
4498 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4499 monitor_event, mon);
4500 }
aliguori87127162009-03-05 23:01:29 +00004501
Blue Swirl72cf2d42009-09-12 07:36:22 +00004502 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00004503 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00004504 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004505}
4506
aliguori376253e2009-03-05 23:01:23 +00004507static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004508{
aliguoribb5fc202009-03-05 23:01:15 +00004509 BlockDriverState *bs = opaque;
4510 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004511
aliguoribb5fc202009-03-05 23:01:15 +00004512 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004513 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004514 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004515 }
aliguori731b0362009-03-05 23:01:42 +00004516 if (mon->password_completion_cb)
4517 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004518
aliguori731b0362009-03-05 23:01:42 +00004519 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004520}
aliguoric0f4ce72009-03-05 23:01:01 +00004521
aliguori376253e2009-03-05 23:01:23 +00004522void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00004523 BlockDriverCompletionFunc *completion_cb,
4524 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004525{
aliguoricde76ee2009-03-05 23:01:51 +00004526 int err;
4527
aliguoribb5fc202009-03-05 23:01:15 +00004528 if (!bdrv_key_required(bs)) {
4529 if (completion_cb)
4530 completion_cb(opaque, 0);
4531 return;
4532 }
aliguoric0f4ce72009-03-05 23:01:01 +00004533
Luiz Capitulino94171e12009-12-07 21:37:00 +01004534 if (monitor_ctrl_mode(mon)) {
4535 qemu_error_new(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
4536 return;
4537 }
4538
aliguori376253e2009-03-05 23:01:23 +00004539 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4540 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004541
aliguori731b0362009-03-05 23:01:42 +00004542 mon->password_completion_cb = completion_cb;
4543 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004544
aliguoricde76ee2009-03-05 23:01:51 +00004545 err = monitor_read_password(mon, bdrv_password_cb, bs);
4546
4547 if (err && completion_cb)
4548 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00004549}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004550
4551typedef struct QemuErrorSink QemuErrorSink;
4552struct QemuErrorSink {
4553 enum {
4554 ERR_SINK_FILE,
4555 ERR_SINK_MONITOR,
4556 } dest;
4557 union {
4558 FILE *fp;
4559 Monitor *mon;
4560 };
4561 QemuErrorSink *previous;
4562};
4563
Blue Swirl528e93a2009-08-31 15:14:40 +00004564static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004565
4566void qemu_errors_to_file(FILE *fp)
4567{
4568 QemuErrorSink *sink;
4569
4570 sink = qemu_mallocz(sizeof(*sink));
4571 sink->dest = ERR_SINK_FILE;
4572 sink->fp = fp;
4573 sink->previous = qemu_error_sink;
4574 qemu_error_sink = sink;
4575}
4576
4577void qemu_errors_to_mon(Monitor *mon)
4578{
4579 QemuErrorSink *sink;
4580
4581 sink = qemu_mallocz(sizeof(*sink));
4582 sink->dest = ERR_SINK_MONITOR;
4583 sink->mon = mon;
4584 sink->previous = qemu_error_sink;
4585 qemu_error_sink = sink;
4586}
4587
4588void qemu_errors_to_previous(void)
4589{
4590 QemuErrorSink *sink;
4591
4592 assert(qemu_error_sink != NULL);
4593 sink = qemu_error_sink;
4594 qemu_error_sink = sink->previous;
4595 qemu_free(sink);
4596}
4597
4598void qemu_error(const char *fmt, ...)
4599{
4600 va_list args;
4601
4602 assert(qemu_error_sink != NULL);
4603 switch (qemu_error_sink->dest) {
4604 case ERR_SINK_FILE:
4605 va_start(args, fmt);
4606 vfprintf(qemu_error_sink->fp, fmt, args);
4607 va_end(args);
4608 break;
4609 case ERR_SINK_MONITOR:
4610 va_start(args, fmt);
4611 monitor_vprintf(qemu_error_sink->mon, fmt, args);
4612 va_end(args);
4613 break;
4614 }
4615}
Luiz Capitulino8204a912009-11-18 23:05:31 -02004616
4617void qemu_error_internal(const char *file, int linenr, const char *func,
4618 const char *fmt, ...)
4619{
4620 va_list va;
4621 QError *qerror;
4622
4623 assert(qemu_error_sink != NULL);
4624
4625 va_start(va, fmt);
4626 qerror = qerror_from_info(file, linenr, func, fmt, &va);
4627 va_end(va);
4628
4629 switch (qemu_error_sink->dest) {
4630 case ERR_SINK_FILE:
4631 qerror_print(qerror);
4632 QDECREF(qerror);
4633 break;
4634 case ERR_SINK_MONITOR:
Luiz Capitulino27a749f2010-02-08 17:01:29 -02004635 /* report only the first error */
4636 if (!qemu_error_sink->mon->error) {
4637 qemu_error_sink->mon->error = qerror;
4638 } else {
4639 /* XXX: warn the programmer */
4640 QDECREF(qerror);
4641 }
Luiz Capitulino8204a912009-11-18 23:05:31 -02004642 break;
4643 }
4644}