blob: be34329cfc2abeb86b192a6cf1d5a339a3465c78 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Gerd Hoffmanncae49562009-06-05 15:53:17 +010026#include "hw/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
28#include "hw/pcmcia.h"
29#include "hw/pc.h"
30#include "hw/pci.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010031#include "hw/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020032#include "hw/loader.h"
pbrook87ecb682007-11-17 17:14:51 +000033#include "gdbstub.h"
34#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000035#include "net/slirp.h"
pbrook87ecb682007-11-17 17:14:51 +000036#include "qemu-char.h"
37#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000038#include "monitor.h"
39#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000040#include "console.h"
41#include "block.h"
42#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000043#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000044#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000045#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000046#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000047#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000048#include "acl.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030049#include "qint.h"
Luiz Capitulino8f3cec02009-10-07 13:42:04 -030050#include "qlist.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030051#include "qdict.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020052#include "qbool.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030053#include "qstring.h"
Luiz Capitulino8204a912009-11-18 23:05:31 -020054#include "qerror.h"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020055#include "qjson.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020056#include "json-streamer.h"
57#include "json-parser.h"
Blue Swirld08d6f02009-12-04 18:06:39 +000058#include "osdep.h"
ths6a5bd302007-12-03 17:05:38 +000059
bellard9dc39cb2004-03-14 21:38:27 +000060//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000061//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000062
bellard9307c4c2004-04-04 12:57:25 +000063/*
64 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000065 *
bellard9307c4c2004-04-04 12:57:25 +000066 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000067 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000068 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000069 * 'i' 32 bit integer
70 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000071 * '/' optional gdb-like print format (like "/10x")
72 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030073 * '?' optional type (for all types, except '/')
74 * '.' other form of optional type (for 'i' and 'l')
75 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000076 *
77 */
78
Adam Litke940cc302010-01-25 12:18:44 -060079typedef struct MonitorCompletionData MonitorCompletionData;
80struct MonitorCompletionData {
81 Monitor *mon;
82 void (*user_print)(Monitor *mon, const QObject *data);
83};
84
Anthony Liguoric227f092009-10-01 16:12:16 -050085typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000086 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000087 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +000088 const char *params;
89 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -030090 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -030091 union {
92 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -030093 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -060094 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -030095 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -030096 void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -060097 int (*cmd_async)(Monitor *mon, const QDict *params,
98 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -030099 } mhandler;
Adam Litke940cc302010-01-25 12:18:44 -0600100 int async;
Anthony Liguoric227f092009-10-01 16:12:16 -0500101} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000102
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100103/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500104typedef struct mon_fd_t mon_fd_t;
105struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100106 char *name;
107 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500108 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100109};
110
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200111typedef struct MonitorControl {
112 QObject *id;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200113 int print_enabled;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200114 JSONMessageParser parser;
115} MonitorControl;
116
aliguori87127162009-03-05 23:01:29 +0000117struct Monitor {
118 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200119 int mux_out;
120 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000121 int flags;
122 int suspend_cnt;
123 uint8_t outbuf[1024];
124 int outbuf_index;
125 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200126 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000127 CPUState *mon_cpu;
128 BlockDriverCompletionFunc *password_completion_cb;
129 void *password_opaque;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200130 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500131 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000132 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000133};
134
Blue Swirl72cf2d42009-09-12 07:36:22 +0000135static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000136
Anthony Liguoric227f092009-10-01 16:12:16 -0500137static const mon_cmd_t mon_cmds[];
138static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000139
aliguori87127162009-03-05 23:01:29 +0000140Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000141
aliguori731b0362009-03-05 23:01:42 +0000142static void monitor_command_cb(Monitor *mon, const char *cmdline,
143 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000144
Luiz Capitulino418173c2009-11-26 22:58:51 -0200145/* Return true if in control mode, false otherwise */
146static inline int monitor_ctrl_mode(const Monitor *mon)
147{
148 return (mon->flags & MONITOR_USE_CONTROL);
149}
150
aliguori731b0362009-03-05 23:01:42 +0000151static void monitor_read_command(Monitor *mon, int show_prompt)
152{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200153 if (!mon->rs)
154 return;
155
aliguori731b0362009-03-05 23:01:42 +0000156 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
157 if (show_prompt)
158 readline_show_prompt(mon->rs);
159}
bellard6a00d602005-11-21 23:25:50 +0000160
aliguoricde76ee2009-03-05 23:01:51 +0000161static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
162 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000163{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100164 if (monitor_ctrl_mode(mon)) {
165 qemu_error_new(QERR_MISSING_PARAMETER, "password");
166 return -EINVAL;
167 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000168 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
169 /* prompt is printed on return from the command handler */
170 return 0;
171 } else {
172 monitor_printf(mon, "terminal does not support password prompting\n");
173 return -ENOTTY;
174 }
aliguoribb5fc202009-03-05 23:01:15 +0000175}
176
aliguori376253e2009-03-05 23:01:23 +0000177void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000178{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200179 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000180 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
181 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000182 }
183}
184
185/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000186static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000187{
ths60fe76f2007-12-16 03:02:09 +0000188 char c;
aliguori731b0362009-03-05 23:01:42 +0000189
bellard7e2515e2004-08-01 21:52:19 +0000190 for(;;) {
191 c = *str++;
192 if (c == '\0')
193 break;
bellard7ba12602006-07-14 20:26:42 +0000194 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000195 mon->outbuf[mon->outbuf_index++] = '\r';
196 mon->outbuf[mon->outbuf_index++] = c;
197 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
198 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000199 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000200 }
201}
202
aliguori376253e2009-03-05 23:01:23 +0000203void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000204{
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200205 if (!mon)
206 return;
207
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200208 if (mon->mc && !mon->mc->print_enabled) {
209 qemu_error_new(QERR_UNDEFINED_ERROR);
210 } else {
211 char buf[4096];
212 vsnprintf(buf, sizeof(buf), fmt, ap);
213 monitor_puts(mon, buf);
214 }
bellard7e2515e2004-08-01 21:52:19 +0000215}
216
aliguori376253e2009-03-05 23:01:23 +0000217void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000218{
219 va_list ap;
220 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000221 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000222 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000223}
224
aliguori376253e2009-03-05 23:01:23 +0000225void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000226{
227 int i;
228
229 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000230 switch (filename[i]) {
231 case ' ':
232 case '"':
233 case '\\':
234 monitor_printf(mon, "\\%c", filename[i]);
235 break;
236 case '\t':
237 monitor_printf(mon, "\\t");
238 break;
239 case '\r':
240 monitor_printf(mon, "\\r");
241 break;
242 case '\n':
243 monitor_printf(mon, "\\n");
244 break;
245 default:
246 monitor_printf(mon, "%c", filename[i]);
247 break;
248 }
thsfef30742006-12-22 14:11:32 +0000249 }
250}
251
bellard7fe48482004-10-09 18:08:01 +0000252static int monitor_fprintf(FILE *stream, const char *fmt, ...)
253{
254 va_list ap;
255 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000256 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000257 va_end(ap);
258 return 0;
259}
260
Luiz Capitulino13c74252009-10-07 13:41:55 -0300261static void monitor_user_noop(Monitor *mon, const QObject *data) { }
262
Luiz Capitulino13917be2009-10-07 13:41:54 -0300263static inline int monitor_handler_ported(const mon_cmd_t *cmd)
264{
265 return cmd->user_print != NULL;
266}
267
Adam Litke940cc302010-01-25 12:18:44 -0600268static inline bool monitor_handler_is_async(const mon_cmd_t *cmd)
269{
270 return cmd->async != 0;
271}
272
Luiz Capitulino8204a912009-11-18 23:05:31 -0200273static inline int monitor_has_error(const Monitor *mon)
274{
275 return mon->error != NULL;
276}
277
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200278static void monitor_json_emitter(Monitor *mon, const QObject *data)
279{
280 QString *json;
281
282 json = qobject_to_json(data);
283 assert(json != NULL);
284
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200285 mon->mc->print_enabled = 1;
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200286 monitor_printf(mon, "%s\n", qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200287 mon->mc->print_enabled = 0;
288
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200289 QDECREF(json);
290}
291
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200292static void monitor_protocol_emitter(Monitor *mon, QObject *data)
293{
294 QDict *qmp;
295
296 qmp = qdict_new();
297
298 if (!monitor_has_error(mon)) {
299 /* success response */
300 if (data) {
301 qobject_incref(data);
302 qdict_put_obj(qmp, "return", data);
303 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200304 /* return an empty QDict by default */
305 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200306 }
307 } else {
308 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100309 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200310 qdict_put(qmp, "error", mon->error->error);
311 QINCREF(mon->error->error);
312 QDECREF(mon->error);
313 mon->error = NULL;
314 }
315
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200316 if (mon->mc->id) {
317 qdict_put_obj(qmp, "id", mon->mc->id);
318 mon->mc->id = NULL;
319 }
320
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200321 monitor_json_emitter(mon, QOBJECT(qmp));
322 QDECREF(qmp);
323}
324
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200325static void timestamp_put(QDict *qdict)
326{
327 int err;
328 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000329 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200330
Blue Swirld08d6f02009-12-04 18:06:39 +0000331 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200332 if (err < 0)
333 return;
334
335 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
336 "'microseconds': %" PRId64 " }",
337 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
338 assert(obj != NULL);
339
340 qdict_put_obj(qdict, "timestamp", obj);
341}
342
343/**
344 * monitor_protocol_event(): Generate a Monitor event
345 *
346 * Event-specific data can be emitted through the (optional) 'data' parameter.
347 */
348void monitor_protocol_event(MonitorEvent event, QObject *data)
349{
350 QDict *qmp;
351 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600352 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200353
Blue Swirl242cd002009-12-04 18:05:45 +0000354 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200355
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200356 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000357 case QEVENT_DEBUG:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200358 event_name = "DEBUG";
359 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000360 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200361 event_name = "SHUTDOWN";
362 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000363 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200364 event_name = "RESET";
365 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000366 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200367 event_name = "POWERDOWN";
368 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000369 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200370 event_name = "STOP";
371 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200372 case QEVENT_VNC_CONNECTED:
373 event_name = "VNC_CONNECTED";
374 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200375 case QEVENT_VNC_INITIALIZED:
376 event_name = "VNC_INITIALIZED";
377 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200378 case QEVENT_VNC_DISCONNECTED:
379 event_name = "VNC_DISCONNECTED";
380 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200381 default:
382 abort();
383 break;
384 }
385
386 qmp = qdict_new();
387 timestamp_put(qmp);
388 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200389 if (data) {
390 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200391 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200392 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200393
Adam Litkef039a562010-01-15 08:34:02 -0600394 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200395 if (monitor_ctrl_mode(mon)) {
396 monitor_json_emitter(mon, QOBJECT(qmp));
397 }
Adam Litkef039a562010-01-15 08:34:02 -0600398 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200399 QDECREF(qmp);
400}
401
bellard9dc39cb2004-03-14 21:38:27 +0000402static int compare_cmd(const char *name, const char *list)
403{
404 const char *p, *pstart;
405 int len;
406 len = strlen(name);
407 p = list;
408 for(;;) {
409 pstart = p;
410 p = strchr(p, '|');
411 if (!p)
412 p = pstart + strlen(pstart);
413 if ((p - pstart) == len && !memcmp(pstart, name, len))
414 return 1;
415 if (*p == '\0')
416 break;
417 p++;
418 }
419 return 0;
420}
421
Anthony Liguoric227f092009-10-01 16:12:16 -0500422static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000423 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000424{
Anthony Liguoric227f092009-10-01 16:12:16 -0500425 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000426
427 for(cmd = cmds; cmd->name != NULL; cmd++) {
428 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000429 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
430 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000431 }
432}
433
aliguori376253e2009-03-05 23:01:23 +0000434static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000435{
436 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000437 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000438 } else {
aliguori376253e2009-03-05 23:01:23 +0000439 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000440 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000441 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000442 monitor_printf(mon, "Log items (comma separated):\n");
443 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000444 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000445 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000446 }
447 }
bellard9dc39cb2004-03-14 21:38:27 +0000448 }
449}
450
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300451static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300452{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300453 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300454}
455
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300456static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000457{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200458 int all_devices;
459 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300460 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000461
bellard7954c732006-08-01 15:52:40 +0000462 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000463 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200464 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300465 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200466 continue;
467 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000468 }
469}
470
Adam Litke940cc302010-01-25 12:18:44 -0600471static void user_monitor_complete(void *opaque, QObject *ret_data)
472{
473 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
474
475 if (ret_data) {
476 data->user_print(data->mon, ret_data);
477 }
478 monitor_resume(data->mon);
479 qemu_free(data);
480}
481
482static void qmp_monitor_complete(void *opaque, QObject *ret_data)
483{
484 monitor_protocol_emitter(opaque, ret_data);
485}
486
487static void qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
488 const QDict *params)
489{
490 cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
491}
492
493static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
494{
495 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
496}
497
498static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
499 const QDict *params)
500{
501 int ret;
502
503 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
504 cb_data->mon = mon;
505 cb_data->user_print = cmd->user_print;
506 monitor_suspend(mon);
507 ret = cmd->mhandler.cmd_async(mon, params,
508 user_monitor_complete, cb_data);
509 if (ret < 0) {
510 monitor_resume(mon);
511 qemu_free(cb_data);
512 }
513}
514
515static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
516{
517 int ret;
518
519 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
520 cb_data->mon = mon;
521 cb_data->user_print = cmd->user_print;
522 monitor_suspend(mon);
523 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
524 if (ret < 0) {
525 monitor_resume(mon);
526 qemu_free(cb_data);
527 }
528}
529
Luiz Capitulino13c74252009-10-07 13:41:55 -0300530static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000531{
Anthony Liguoric227f092009-10-01 16:12:16 -0500532 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300533 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000534
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200535 if (!item) {
536 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000537 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200538 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300539
540 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000541 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300542 break;
bellard9dc39cb2004-03-14 21:38:27 +0000543 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300544
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200545 if (cmd->name == NULL) {
546 if (monitor_ctrl_mode(mon)) {
547 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
548 return;
549 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300550 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200551 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300552
Adam Litke940cc302010-01-25 12:18:44 -0600553 if (monitor_handler_is_async(cmd)) {
554 if (monitor_ctrl_mode(mon)) {
555 qmp_async_info_handler(mon, cmd);
556 } else {
557 user_async_info_handler(mon, cmd);
558 }
559 /*
560 * Indicate that this command is asynchronous and will not return any
561 * data (not even empty). Instead, the data will be returned via a
562 * completion callback.
563 */
564 *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
565 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300566 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200567
568 if (!monitor_ctrl_mode(mon)) {
569 /*
570 * User Protocol function is called here, Monitor Protocol is
571 * handled by monitor_call_handler()
572 */
573 if (*ret_data)
574 cmd->user_print(mon, *ret_data);
575 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300576 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200577 if (monitor_ctrl_mode(mon)) {
578 /* handler not converted yet */
579 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
580 } else {
581 cmd->mhandler.info(mon);
582 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300583 }
584
bellard9dc39cb2004-03-14 21:38:27 +0000585 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300586
587help:
588 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000589}
590
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200591static void do_info_version_print(Monitor *mon, const QObject *data)
592{
593 QDict *qdict;
594
595 qdict = qobject_to_qdict(data);
596
597 monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
598 qdict_get_str(qdict, "package"));
599}
600
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300601/**
602 * do_info_version(): Show QEMU version
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200603 *
604 * Return a QDict with the following information:
605 *
606 * - "qemu": QEMU's version
607 * - "package": package's version
608 *
609 * Example:
610 *
611 * { "qemu": "0.11.50", "package": "" }
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300612 */
613static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000614{
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200615 *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
616 QEMU_VERSION, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000617}
618
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200619static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000620{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200621 QDict *qdict;
622
623 qdict = qobject_to_qdict(data);
624 if (qdict_size(qdict) == 0) {
625 return;
626 }
627
628 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
629}
630
631/**
632 * do_info_name(): Show VM name
633 *
634 * Return a QDict with the following information:
635 *
636 * - "name": VM's name (optional)
637 *
638 * Example:
639 *
640 * { "name": "qemu-name" }
641 */
642static void do_info_name(Monitor *mon, QObject **ret_data)
643{
644 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
645 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000646}
647
Luiz Capitulino1a728672009-12-10 17:15:56 -0200648static QObject *get_cmd_dict(const char *name)
649{
650 const char *p;
651
652 /* Remove '|' from some commands */
653 p = strchr(name, '|');
654 if (p) {
655 p++;
656 } else {
657 p = name;
658 }
659
660 return qobject_from_jsonf("{ 'name': %s }", p);
661}
662
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200663/**
664 * do_info_commands(): List QMP available commands
665 *
Luiz Capitulino1a728672009-12-10 17:15:56 -0200666 * Each command is represented by a QDict, the returned QObject is a QList
667 * of all commands.
668 *
669 * The QDict contains:
670 *
671 * - "name": command's name
672 *
673 * Example:
674 *
675 * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] }
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200676 */
677static void do_info_commands(Monitor *mon, QObject **ret_data)
678{
679 QList *cmd_list;
680 const mon_cmd_t *cmd;
681
682 cmd_list = qlist_new();
683
684 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
685 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200686 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200687 }
688 }
689
690 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
691 if (monitor_handler_ported(cmd)) {
692 char buf[128];
693 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200694 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200695 }
696 }
697
698 *ret_data = QOBJECT(cmd_list);
699}
700
aurel32bf4f74c2008-12-18 22:42:34 +0000701#if defined(TARGET_I386)
Luiz Capitulino14f07202009-12-10 17:16:02 -0200702static void do_info_hpet_print(Monitor *mon, const QObject *data)
aliguori16b29ae2008-12-17 23:28:44 +0000703{
aliguori376253e2009-03-05 23:01:23 +0000704 monitor_printf(mon, "HPET is %s by QEMU\n",
Luiz Capitulino14f07202009-12-10 17:16:02 -0200705 qdict_get_bool(qobject_to_qdict(data), "enabled") ?
706 "enabled" : "disabled");
707}
708
709/**
710 * do_info_hpet(): Show HPET state
711 *
712 * Return a QDict with the following information:
713 *
714 * - "enabled": true if hpet if enabled, false otherwise
715 *
716 * Example:
717 *
718 * { "enabled": true }
719 */
720static void do_info_hpet(Monitor *mon, QObject **ret_data)
721{
722 *ret_data = qobject_from_jsonf("{ 'enabled': %i }", !no_hpet);
aliguori16b29ae2008-12-17 23:28:44 +0000723}
aurel32bf4f74c2008-12-18 22:42:34 +0000724#endif
aliguori16b29ae2008-12-17 23:28:44 +0000725
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200726static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000727{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200728 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
729}
730
731/**
732 * do_info_uuid(): Show VM UUID
733 *
734 * Return a QDict with the following information:
735 *
736 * - "UUID": Universally Unique Identifier
737 *
738 * Example:
739 *
740 * { "UUID": "550e8400-e29b-41d4-a716-446655440000" }
741 */
742static void do_info_uuid(Monitor *mon, QObject **ret_data)
743{
744 char uuid[64];
745
746 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000747 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
748 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
749 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
750 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200751 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000752}
753
bellard6a00d602005-11-21 23:25:50 +0000754/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000755static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000756{
757 CPUState *env;
758
759 for(env = first_cpu; env != NULL; env = env->next_cpu) {
760 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000761 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000762 return 0;
763 }
764 }
765 return -1;
766}
767
pbrook9596ebb2007-11-18 01:44:38 +0000768static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000769{
aliguori731b0362009-03-05 23:01:42 +0000770 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000771 mon_set_cpu(0);
772 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300773 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000774 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000775}
776
aliguori376253e2009-03-05 23:01:23 +0000777static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000778{
bellard6a00d602005-11-21 23:25:50 +0000779 CPUState *env;
780 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000781#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000782 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000783 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000784#else
aliguori376253e2009-03-05 23:01:23 +0000785 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000786 0);
bellard9307c4c2004-04-04 12:57:25 +0000787#endif
788}
789
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300790static void print_cpu_iter(QObject *obj, void *opaque)
791{
792 QDict *cpu;
793 int active = ' ';
794 Monitor *mon = opaque;
795
796 assert(qobject_type(obj) == QTYPE_QDICT);
797 cpu = qobject_to_qdict(obj);
798
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200799 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300800 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200801 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300802
803 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
804
805#if defined(TARGET_I386)
806 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
807 (target_ulong) qdict_get_int(cpu, "pc"));
808#elif defined(TARGET_PPC)
809 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
810 (target_long) qdict_get_int(cpu, "nip"));
811#elif defined(TARGET_SPARC)
812 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
813 (target_long) qdict_get_int(cpu, "pc"));
814 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
815 (target_long) qdict_get_int(cpu, "npc"));
816#elif defined(TARGET_MIPS)
817 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
818 (target_long) qdict_get_int(cpu, "PC"));
819#endif
820
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200821 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300822 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200823 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300824
825 monitor_printf(mon, "\n");
826}
827
828static void monitor_print_cpus(Monitor *mon, const QObject *data)
829{
830 QList *cpu_list;
831
832 assert(qobject_type(data) == QTYPE_QLIST);
833 cpu_list = qobject_to_qlist(data);
834 qlist_iter(cpu_list, print_cpu_iter, mon);
835}
836
837/**
838 * do_info_cpus(): Show CPU information
839 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200840 * Return a QList. Each CPU is represented by a QDict, which contains:
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300841 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200842 * - "cpu": CPU index
843 * - "current": true if this is the current CPU, false otherwise
844 * - "halted": true if the cpu is halted, false otherwise
845 * - Current program counter. The key's name depends on the architecture:
846 * "pc": i386/x86)64
847 * "nip": PPC
848 * "pc" and "npc": sparc
849 * "PC": mips
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300850 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200851 * Example:
852 *
853 * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 },
854 * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ]
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300855 */
856static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000857{
858 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300859 QList *cpu_list;
860
861 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000862
863 /* just to set the default cpu if not already done */
864 mon_get_cpu();
865
866 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200867 QDict *cpu;
868 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300869
Avi Kivity4c0960c2009-08-17 23:19:53 +0300870 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300871
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200872 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
873 env->cpu_index, env == mon->mon_cpu,
874 env->halted);
875 assert(obj != NULL);
876
877 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300878
bellard6a00d602005-11-21 23:25:50 +0000879#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300880 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000881#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300882 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000883#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300884 qdict_put(cpu, "pc", qint_from_int(env->pc));
885 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000886#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300887 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000888#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300889
890 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000891 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300892
893 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000894}
895
Markus Armbruster81a1b452010-01-20 13:07:35 +0100896static void do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000897{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300898 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000899 if (mon_set_cpu(index) < 0)
Markus Armbruster81a1b452010-01-20 13:07:35 +0100900 qemu_error_new(QERR_INVALID_CPU_INDEX);
bellard6a00d602005-11-21 23:25:50 +0000901}
902
aliguori376253e2009-03-05 23:01:23 +0000903static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000904{
aliguori376253e2009-03-05 23:01:23 +0000905 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000906}
907
aliguori376253e2009-03-05 23:01:23 +0000908static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000909{
910 int i;
bellard7e2515e2004-08-01 21:52:19 +0000911 const char *str;
ths3b46e622007-09-17 08:09:54 +0000912
aliguoricde76ee2009-03-05 23:01:51 +0000913 if (!mon->rs)
914 return;
bellard7e2515e2004-08-01 21:52:19 +0000915 i = 0;
916 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000917 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000918 if (!str)
919 break;
aliguori376253e2009-03-05 23:01:23 +0000920 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000921 i++;
bellardaa455482004-04-04 13:07:25 +0000922 }
923}
924
j_mayer76a66252007-03-07 08:32:30 +0000925#if defined(TARGET_PPC)
926/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000927static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000928{
929 CPUState *env;
930
931 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000932 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000933}
934#endif
935
Luiz Capitulinob223f352009-10-07 13:41:56 -0300936/**
937 * do_quit(): Quit QEMU execution
938 */
939static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000940{
941 exit(0);
942}
943
aliguori376253e2009-03-05 23:01:23 +0000944static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000945{
946 if (bdrv_is_inserted(bs)) {
947 if (!force) {
948 if (!bdrv_is_removable(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100949 qemu_error_new(QERR_DEVICE_NOT_REMOVABLE,
950 bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000951 return -1;
952 }
953 if (bdrv_is_locked(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100954 qemu_error_new(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000955 return -1;
956 }
957 }
958 bdrv_close(bs);
959 }
960 return 0;
961}
962
Luiz Capitulinoe1c923a2009-10-16 12:23:49 -0300963static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000964{
965 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300966 int force = qdict_get_int(qdict, "force");
Luiz Capitulino78d714e2009-12-14 18:53:21 -0200967 const char *filename = qdict_get_str(qdict, "device");
bellard9dc39cb2004-03-14 21:38:27 +0000968
bellard9307c4c2004-04-04 12:57:25 +0000969 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000970 if (!bs) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100971 qemu_error_new(QERR_DEVICE_NOT_FOUND, filename);
bellard9dc39cb2004-03-14 21:38:27 +0000972 return;
973 }
aliguori376253e2009-03-05 23:01:23 +0000974 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000975}
976
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -0200977static void do_block_set_passwd(Monitor *mon, const QDict *qdict,
978 QObject **ret_data)
979{
980 BlockDriverState *bs;
981
982 bs = bdrv_find(qdict_get_str(qdict, "device"));
983 if (!bs) {
984 qemu_error_new(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
985 return;
986 }
987
988 if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) {
989 qemu_error_new(QERR_INVALID_PASSWORD);
990 }
991}
992
aliguori376253e2009-03-05 23:01:23 +0000993static void do_change_block(Monitor *mon, const char *device,
994 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000995{
996 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000997 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000998
bellard9307c4c2004-04-04 12:57:25 +0000999 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +00001000 if (!bs) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001001 qemu_error_new(QERR_DEVICE_NOT_FOUND, device);
bellard9dc39cb2004-03-14 21:38:27 +00001002 return;
1003 }
aurel322ecea9b2008-06-18 22:10:01 +00001004 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +01001005 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001006 if (!drv) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001007 qemu_error_new(QERR_INVALID_BLOCK_FORMAT, fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001008 return;
1009 }
1010 }
aliguori376253e2009-03-05 23:01:23 +00001011 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +00001012 return;
Naphtali Spreif5edb012010-01-17 16:48:13 +02001013 bdrv_open2(bs, filename, BDRV_O_RDWR, drv);
aliguori376253e2009-03-05 23:01:23 +00001014 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001015}
1016
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001017static void change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001018{
1019 if (vnc_display_password(NULL, password) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001020 qemu_error_new(QERR_SET_PASSWD_FAILED);
aliguoribb5fc202009-03-05 23:01:15 +00001021
Markus Armbruster2895e072009-12-07 21:37:01 +01001022}
1023
1024static void change_vnc_password_cb(Monitor *mon, const char *password,
1025 void *opaque)
1026{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001027 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001028 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001029}
1030
aliguori376253e2009-03-05 23:01:23 +00001031static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001032{
ths70848512007-08-25 01:37:05 +00001033 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001034 strcmp(target, "password") == 0) {
1035 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001036 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001037 strncpy(password, arg, sizeof(password));
1038 password[sizeof(password) - 1] = '\0';
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001039 change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001040 } else {
aliguori376253e2009-03-05 23:01:23 +00001041 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001042 }
ths70848512007-08-25 01:37:05 +00001043 } else {
aliguori28a76be2009-03-06 20:27:40 +00001044 if (vnc_display_open(NULL, target) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001045 qemu_error_new(QERR_VNC_SERVER_FAILED, target);
ths70848512007-08-25 01:37:05 +00001046 }
thse25a5822007-08-25 01:36:20 +00001047}
1048
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001049/**
1050 * do_change(): Change a removable medium, or VNC configuration
1051 */
1052static void do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001053{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001054 const char *device = qdict_get_str(qdict, "device");
1055 const char *target = qdict_get_str(qdict, "target");
1056 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +00001057 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001058 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001059 } else {
aliguori28a76be2009-03-06 20:27:40 +00001060 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001061 }
1062}
1063
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001064static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +00001065{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001066 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +00001067}
1068
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001069static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001070{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001071 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001072}
1073
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001074static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001075{
1076 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001077 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001078
bellard9307c4c2004-04-04 12:57:25 +00001079 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001080 mask = 0;
1081 } else {
bellard9307c4c2004-04-04 12:57:25 +00001082 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001083 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001084 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001085 return;
1086 }
1087 }
1088 cpu_set_log(mask);
1089}
1090
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001091static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001092{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001093 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001094 if (!option || !strcmp(option, "on")) {
1095 singlestep = 1;
1096 } else if (!strcmp(option, "off")) {
1097 singlestep = 0;
1098 } else {
1099 monitor_printf(mon, "unexpected option %s\n", option);
1100 }
1101}
1102
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001103/**
1104 * do_stop(): Stop VM execution
1105 */
1106static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001107{
1108 vm_stop(EXCP_INTERRUPT);
1109}
1110
aliguoribb5fc202009-03-05 23:01:15 +00001111static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001112
aliguori376253e2009-03-05 23:01:23 +00001113struct bdrv_iterate_context {
1114 Monitor *mon;
1115 int err;
1116};
aliguoric0f4ce72009-03-05 23:01:01 +00001117
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001118/**
1119 * do_cont(): Resume emulation.
1120 */
1121static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001122{
1123 struct bdrv_iterate_context context = { mon, 0 };
1124
1125 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001126 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +00001127 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +00001128 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +00001129}
1130
aliguoribb5fc202009-03-05 23:01:15 +00001131static void bdrv_key_cb(void *opaque, int err)
1132{
aliguori376253e2009-03-05 23:01:23 +00001133 Monitor *mon = opaque;
1134
aliguoribb5fc202009-03-05 23:01:15 +00001135 /* another key was set successfully, retry to continue */
1136 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001137 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001138}
1139
1140static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1141{
aliguori376253e2009-03-05 23:01:23 +00001142 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001143
aliguori376253e2009-03-05 23:01:23 +00001144 if (!context->err && bdrv_key_required(bs)) {
1145 context->err = -EBUSY;
1146 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1147 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001148 }
1149}
1150
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001151static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001152{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001153 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001154 if (!device)
1155 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1156 if (gdbserver_start(device) < 0) {
1157 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1158 device);
1159 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001160 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001161 } else {
aliguori59030a82009-04-05 18:43:41 +00001162 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1163 device);
bellard8a7ddc32004-03-31 19:00:16 +00001164 }
1165}
1166
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001167static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001168{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001169 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001170 if (select_watchdog_action(action) == -1) {
1171 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1172 }
1173}
1174
aliguori376253e2009-03-05 23:01:23 +00001175static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001176{
aliguori376253e2009-03-05 23:01:23 +00001177 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001178 switch(c) {
1179 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001180 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001181 break;
1182 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001183 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001184 break;
1185 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001186 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001187 break;
1188 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001189 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001190 break;
1191 default:
1192 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001193 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001194 } else {
aliguori376253e2009-03-05 23:01:23 +00001195 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001196 }
1197 break;
1198 }
aliguori376253e2009-03-05 23:01:23 +00001199 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001200}
1201
aliguori376253e2009-03-05 23:01:23 +00001202static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001203 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001204{
bellard6a00d602005-11-21 23:25:50 +00001205 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001206 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001207 uint8_t buf[16];
1208 uint64_t v;
1209
1210 if (format == 'i') {
1211 int flags;
1212 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001213 env = mon_get_cpu();
Markus Armbruster09b94182010-01-20 13:07:30 +01001214 if (!is_physical)
bellard6a00d602005-11-21 23:25:50 +00001215 return;
bellard9307c4c2004-04-04 12:57:25 +00001216#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001217 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001218 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001219 } else if (wsize == 4) {
1220 flags = 0;
1221 } else {
bellard6a15fd12006-04-12 21:07:07 +00001222 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001223 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001224 if (env) {
1225#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001226 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001227 (env->segs[R_CS].flags & DESC_L_MASK))
1228 flags = 2;
1229 else
1230#endif
1231 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1232 flags = 1;
1233 }
bellard4c27ba22004-04-25 18:05:08 +00001234 }
1235#endif
aliguori376253e2009-03-05 23:01:23 +00001236 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001237 return;
1238 }
1239
1240 len = wsize * count;
1241 if (wsize == 1)
1242 line_size = 8;
1243 else
1244 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001245 max_digits = 0;
1246
1247 switch(format) {
1248 case 'o':
1249 max_digits = (wsize * 8 + 2) / 3;
1250 break;
1251 default:
1252 case 'x':
1253 max_digits = (wsize * 8) / 4;
1254 break;
1255 case 'u':
1256 case 'd':
1257 max_digits = (wsize * 8 * 10 + 32) / 33;
1258 break;
1259 case 'c':
1260 wsize = 1;
1261 break;
1262 }
1263
1264 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001265 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001266 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001267 else
aliguori376253e2009-03-05 23:01:23 +00001268 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001269 l = len;
1270 if (l > line_size)
1271 l = line_size;
1272 if (is_physical) {
1273 cpu_physical_memory_rw(addr, buf, l, 0);
1274 } else {
bellard6a00d602005-11-21 23:25:50 +00001275 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001276 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001277 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001278 break;
1279 }
bellard9307c4c2004-04-04 12:57:25 +00001280 }
ths5fafdf22007-09-16 21:08:06 +00001281 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001282 while (i < l) {
1283 switch(wsize) {
1284 default:
1285 case 1:
1286 v = ldub_raw(buf + i);
1287 break;
1288 case 2:
1289 v = lduw_raw(buf + i);
1290 break;
1291 case 4:
bellard92a31b12005-02-10 22:00:52 +00001292 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001293 break;
1294 case 8:
1295 v = ldq_raw(buf + i);
1296 break;
1297 }
aliguori376253e2009-03-05 23:01:23 +00001298 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001299 switch(format) {
1300 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001301 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001302 break;
1303 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001304 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001305 break;
1306 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001307 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001308 break;
1309 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001310 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001311 break;
1312 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001313 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001314 break;
1315 }
1316 i += wsize;
1317 }
aliguori376253e2009-03-05 23:01:23 +00001318 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001319 addr += l;
1320 len -= l;
1321 }
1322}
1323
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001324static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001325{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001326 int count = qdict_get_int(qdict, "count");
1327 int format = qdict_get_int(qdict, "format");
1328 int size = qdict_get_int(qdict, "size");
1329 target_long addr = qdict_get_int(qdict, "addr");
1330
aliguori376253e2009-03-05 23:01:23 +00001331 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001332}
1333
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001334static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001335{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001336 int count = qdict_get_int(qdict, "count");
1337 int format = qdict_get_int(qdict, "format");
1338 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001339 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001340
aliguori376253e2009-03-05 23:01:23 +00001341 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001342}
1343
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001344static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001345{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001346 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001347 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001348
blueswir17743e582007-09-24 18:39:04 +00001349#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001350 switch(format) {
1351 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001352 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001353 break;
1354 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001355 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001356 break;
1357 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001358 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001359 break;
1360 default:
1361 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001362 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001363 break;
1364 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001365 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001366 break;
1367 }
bellard92a31b12005-02-10 22:00:52 +00001368#else
1369 switch(format) {
1370 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001371 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001372 break;
1373 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001374 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001375 break;
1376 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001377 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001378 break;
1379 default:
1380 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001381 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001382 break;
1383 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001384 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001385 break;
1386 }
1387#endif
aliguori376253e2009-03-05 23:01:23 +00001388 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001389}
1390
Luiz Capitulino57e09452009-10-16 12:23:43 -03001391static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001392{
1393 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001394 uint32_t size = qdict_get_int(qdict, "size");
1395 const char *filename = qdict_get_str(qdict, "filename");
1396 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001397 uint32_t l;
1398 CPUState *env;
1399 uint8_t buf[1024];
1400
1401 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001402
1403 f = fopen(filename, "wb");
1404 if (!f) {
Markus Armbrusterc34ed282010-01-20 13:07:32 +01001405 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
bellardb371dc52007-01-03 15:20:39 +00001406 return;
1407 }
1408 while (size != 0) {
1409 l = sizeof(buf);
1410 if (l > size)
1411 l = size;
1412 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001413 if (fwrite(buf, 1, l, f) != l) {
1414 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1415 goto exit;
1416 }
bellardb371dc52007-01-03 15:20:39 +00001417 addr += l;
1418 size -= l;
1419 }
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001420exit:
bellardb371dc52007-01-03 15:20:39 +00001421 fclose(f);
1422}
1423
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001424static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1425 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001426{
1427 FILE *f;
1428 uint32_t l;
1429 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001430 uint32_t size = qdict_get_int(qdict, "size");
1431 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001432 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +00001433
1434 f = fopen(filename, "wb");
1435 if (!f) {
Markus Armbruster95fada82010-01-20 13:07:33 +01001436 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
aurel32a8bdf7a2008-04-11 21:36:14 +00001437 return;
1438 }
1439 while (size != 0) {
1440 l = sizeof(buf);
1441 if (l > size)
1442 l = size;
1443 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001444 if (fwrite(buf, 1, l, f) != l) {
1445 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1446 goto exit;
1447 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001448 fflush(f);
1449 addr += l;
1450 size -= l;
1451 }
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001452exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001453 fclose(f);
1454}
1455
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001456static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001457{
1458 uint32_t addr;
1459 uint8_t buf[1];
1460 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001461 uint32_t start = qdict_get_int(qdict, "start");
1462 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001463
1464 sum = 0;
1465 for(addr = start; addr < (start + size); addr++) {
1466 cpu_physical_memory_rw(addr, buf, 1, 0);
1467 /* BSD sum algorithm ('sum' Unix command) */
1468 sum = (sum >> 1) | (sum << 15);
1469 sum += buf[0];
1470 }
aliguori376253e2009-03-05 23:01:23 +00001471 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001472}
1473
bellarda3a91a32004-06-04 11:06:21 +00001474typedef struct {
1475 int keycode;
1476 const char *name;
1477} KeyDef;
1478
1479static const KeyDef key_defs[] = {
1480 { 0x2a, "shift" },
1481 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001482
bellarda3a91a32004-06-04 11:06:21 +00001483 { 0x38, "alt" },
1484 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001485 { 0x64, "altgr" },
1486 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001487 { 0x1d, "ctrl" },
1488 { 0x9d, "ctrl_r" },
1489
1490 { 0xdd, "menu" },
1491
1492 { 0x01, "esc" },
1493
1494 { 0x02, "1" },
1495 { 0x03, "2" },
1496 { 0x04, "3" },
1497 { 0x05, "4" },
1498 { 0x06, "5" },
1499 { 0x07, "6" },
1500 { 0x08, "7" },
1501 { 0x09, "8" },
1502 { 0x0a, "9" },
1503 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001504 { 0x0c, "minus" },
1505 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001506 { 0x0e, "backspace" },
1507
1508 { 0x0f, "tab" },
1509 { 0x10, "q" },
1510 { 0x11, "w" },
1511 { 0x12, "e" },
1512 { 0x13, "r" },
1513 { 0x14, "t" },
1514 { 0x15, "y" },
1515 { 0x16, "u" },
1516 { 0x17, "i" },
1517 { 0x18, "o" },
1518 { 0x19, "p" },
1519
1520 { 0x1c, "ret" },
1521
1522 { 0x1e, "a" },
1523 { 0x1f, "s" },
1524 { 0x20, "d" },
1525 { 0x21, "f" },
1526 { 0x22, "g" },
1527 { 0x23, "h" },
1528 { 0x24, "j" },
1529 { 0x25, "k" },
1530 { 0x26, "l" },
1531
1532 { 0x2c, "z" },
1533 { 0x2d, "x" },
1534 { 0x2e, "c" },
1535 { 0x2f, "v" },
1536 { 0x30, "b" },
1537 { 0x31, "n" },
1538 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001539 { 0x33, "comma" },
1540 { 0x34, "dot" },
1541 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001542
balrog4d3b6f62008-02-10 16:33:14 +00001543 { 0x37, "asterisk" },
1544
bellarda3a91a32004-06-04 11:06:21 +00001545 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001546 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001547 { 0x3b, "f1" },
1548 { 0x3c, "f2" },
1549 { 0x3d, "f3" },
1550 { 0x3e, "f4" },
1551 { 0x3f, "f5" },
1552 { 0x40, "f6" },
1553 { 0x41, "f7" },
1554 { 0x42, "f8" },
1555 { 0x43, "f9" },
1556 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001557 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001558 { 0x46, "scroll_lock" },
1559
bellard64866c32006-05-07 18:03:31 +00001560 { 0xb5, "kp_divide" },
1561 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001562 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001563 { 0x4e, "kp_add" },
1564 { 0x9c, "kp_enter" },
1565 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001566 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001567
1568 { 0x52, "kp_0" },
1569 { 0x4f, "kp_1" },
1570 { 0x50, "kp_2" },
1571 { 0x51, "kp_3" },
1572 { 0x4b, "kp_4" },
1573 { 0x4c, "kp_5" },
1574 { 0x4d, "kp_6" },
1575 { 0x47, "kp_7" },
1576 { 0x48, "kp_8" },
1577 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001578
bellarda3a91a32004-06-04 11:06:21 +00001579 { 0x56, "<" },
1580
1581 { 0x57, "f11" },
1582 { 0x58, "f12" },
1583
1584 { 0xb7, "print" },
1585
1586 { 0xc7, "home" },
1587 { 0xc9, "pgup" },
1588 { 0xd1, "pgdn" },
1589 { 0xcf, "end" },
1590
1591 { 0xcb, "left" },
1592 { 0xc8, "up" },
1593 { 0xd0, "down" },
1594 { 0xcd, "right" },
1595
1596 { 0xd2, "insert" },
1597 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001598#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1599 { 0xf0, "stop" },
1600 { 0xf1, "again" },
1601 { 0xf2, "props" },
1602 { 0xf3, "undo" },
1603 { 0xf4, "front" },
1604 { 0xf5, "copy" },
1605 { 0xf6, "open" },
1606 { 0xf7, "paste" },
1607 { 0xf8, "find" },
1608 { 0xf9, "cut" },
1609 { 0xfa, "lf" },
1610 { 0xfb, "help" },
1611 { 0xfc, "meta_l" },
1612 { 0xfd, "meta_r" },
1613 { 0xfe, "compose" },
1614#endif
bellarda3a91a32004-06-04 11:06:21 +00001615 { 0, NULL },
1616};
1617
1618static int get_keycode(const char *key)
1619{
1620 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001621 char *endp;
1622 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001623
1624 for(p = key_defs; p->name != NULL; p++) {
1625 if (!strcmp(key, p->name))
1626 return p->keycode;
1627 }
bellard64866c32006-05-07 18:03:31 +00001628 if (strstart(key, "0x", NULL)) {
1629 ret = strtoul(key, &endp, 0);
1630 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1631 return ret;
1632 }
bellarda3a91a32004-06-04 11:06:21 +00001633 return -1;
1634}
1635
balrogc8256f92008-06-08 22:45:01 +00001636#define MAX_KEYCODES 16
1637static uint8_t keycodes[MAX_KEYCODES];
1638static int nb_pending_keycodes;
1639static QEMUTimer *key_timer;
1640
1641static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001642{
balrogc8256f92008-06-08 22:45:01 +00001643 int keycode;
1644
1645 while (nb_pending_keycodes > 0) {
1646 nb_pending_keycodes--;
1647 keycode = keycodes[nb_pending_keycodes];
1648 if (keycode & 0x80)
1649 kbd_put_keycode(0xe0);
1650 kbd_put_keycode(keycode | 0x80);
1651 }
1652}
1653
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001654static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001655{
balrog3401c0d2008-06-04 10:05:59 +00001656 char keyname_buf[16];
1657 char *separator;
1658 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001659 const char *string = qdict_get_str(qdict, "string");
1660 int has_hold_time = qdict_haskey(qdict, "hold_time");
1661 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001662
balrogc8256f92008-06-08 22:45:01 +00001663 if (nb_pending_keycodes > 0) {
1664 qemu_del_timer(key_timer);
1665 release_keys(NULL);
1666 }
1667 if (!has_hold_time)
1668 hold_time = 100;
1669 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001670 while (1) {
1671 separator = strchr(string, '-');
1672 keyname_len = separator ? separator - string : strlen(string);
1673 if (keyname_len > 0) {
1674 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1675 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001676 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001677 return;
bellarda3a91a32004-06-04 11:06:21 +00001678 }
balrogc8256f92008-06-08 22:45:01 +00001679 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001680 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001681 return;
1682 }
1683 keyname_buf[keyname_len] = 0;
1684 keycode = get_keycode(keyname_buf);
1685 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001686 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001687 return;
1688 }
balrogc8256f92008-06-08 22:45:01 +00001689 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001690 }
balrog3401c0d2008-06-04 10:05:59 +00001691 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001692 break;
balrog3401c0d2008-06-04 10:05:59 +00001693 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001694 }
balrogc8256f92008-06-08 22:45:01 +00001695 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001696 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001697 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001698 keycode = keycodes[i];
1699 if (keycode & 0x80)
1700 kbd_put_keycode(0xe0);
1701 kbd_put_keycode(keycode & 0x7f);
1702 }
balrogc8256f92008-06-08 22:45:01 +00001703 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001704 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001705 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001706}
1707
bellard13224a82006-07-14 22:03:35 +00001708static int mouse_button_state;
1709
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001710static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001711{
1712 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001713 const char *dx_str = qdict_get_str(qdict, "dx_str");
1714 const char *dy_str = qdict_get_str(qdict, "dy_str");
1715 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001716 dx = strtol(dx_str, NULL, 0);
1717 dy = strtol(dy_str, NULL, 0);
1718 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001719 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001720 dz = strtol(dz_str, NULL, 0);
1721 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1722}
1723
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001724static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001725{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001726 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001727 mouse_button_state = button_state;
1728 kbd_mouse_event(0, 0, 0, mouse_button_state);
1729}
1730
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001731static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001732{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001733 int size = qdict_get_int(qdict, "size");
1734 int addr = qdict_get_int(qdict, "addr");
1735 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001736 uint32_t val;
1737 int suffix;
1738
1739 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001740 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001741 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001742 addr++;
1743 }
1744 addr &= 0xffff;
1745
1746 switch(size) {
1747 default:
1748 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001749 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001750 suffix = 'b';
1751 break;
1752 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001753 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001754 suffix = 'w';
1755 break;
1756 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001757 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001758 suffix = 'l';
1759 break;
1760 }
aliguori376253e2009-03-05 23:01:23 +00001761 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1762 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001763}
bellarda3a91a32004-06-04 11:06:21 +00001764
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001765static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001766{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001767 int size = qdict_get_int(qdict, "size");
1768 int addr = qdict_get_int(qdict, "addr");
1769 int val = qdict_get_int(qdict, "val");
1770
Jan Kiszkaf1147842009-07-14 10:20:11 +02001771 addr &= IOPORTS_MASK;
1772
1773 switch (size) {
1774 default:
1775 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001776 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001777 break;
1778 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001779 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001780 break;
1781 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001782 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001783 break;
1784 }
1785}
1786
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001787static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001788{
1789 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001790 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001791
Jan Kiszka76e30d02009-07-02 00:19:02 +02001792 res = qemu_boot_set(bootdevice);
1793 if (res == 0) {
1794 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1795 } else if (res > 0) {
1796 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001797 } else {
aliguori376253e2009-03-05 23:01:23 +00001798 monitor_printf(mon, "no function defined to set boot device list for "
1799 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001800 }
1801}
1802
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001803/**
1804 * do_system_reset(): Issue a machine reset
1805 */
1806static void do_system_reset(Monitor *mon, const QDict *qdict,
1807 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001808{
1809 qemu_system_reset_request();
1810}
1811
Luiz Capitulino43076662009-10-07 13:41:59 -03001812/**
1813 * do_system_powerdown(): Issue a machine powerdown
1814 */
1815static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1816 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001817{
1818 qemu_system_powerdown_request();
1819}
1820
bellardb86bda52004-09-18 19:32:46 +00001821#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001822static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001823{
aliguori376253e2009-03-05 23:01:23 +00001824 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1825 addr,
1826 pte & mask,
1827 pte & PG_GLOBAL_MASK ? 'G' : '-',
1828 pte & PG_PSE_MASK ? 'P' : '-',
1829 pte & PG_DIRTY_MASK ? 'D' : '-',
1830 pte & PG_ACCESSED_MASK ? 'A' : '-',
1831 pte & PG_PCD_MASK ? 'C' : '-',
1832 pte & PG_PWT_MASK ? 'T' : '-',
1833 pte & PG_USER_MASK ? 'U' : '-',
1834 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001835}
1836
aliguori376253e2009-03-05 23:01:23 +00001837static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001838{
bellard6a00d602005-11-21 23:25:50 +00001839 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001840 int l1, l2;
1841 uint32_t pgd, pde, pte;
1842
bellard6a00d602005-11-21 23:25:50 +00001843 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001844
bellardb86bda52004-09-18 19:32:46 +00001845 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001846 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001847 return;
1848 }
1849 pgd = env->cr[3] & ~0xfff;
1850 for(l1 = 0; l1 < 1024; l1++) {
1851 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1852 pde = le32_to_cpu(pde);
1853 if (pde & PG_PRESENT_MASK) {
1854 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001855 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001856 } else {
1857 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001858 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001859 (uint8_t *)&pte, 4);
1860 pte = le32_to_cpu(pte);
1861 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001862 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001863 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001864 ~0xfff);
1865 }
1866 }
1867 }
1868 }
1869 }
1870}
1871
aliguori376253e2009-03-05 23:01:23 +00001872static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001873 uint32_t end, int prot)
1874{
bellard9746b152004-11-11 18:30:24 +00001875 int prot1;
1876 prot1 = *plast_prot;
1877 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001878 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001879 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1880 *pstart, end, end - *pstart,
1881 prot1 & PG_USER_MASK ? 'u' : '-',
1882 'r',
1883 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001884 }
1885 if (prot != 0)
1886 *pstart = end;
1887 else
1888 *pstart = -1;
1889 *plast_prot = prot;
1890 }
1891}
1892
aliguori376253e2009-03-05 23:01:23 +00001893static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001894{
bellard6a00d602005-11-21 23:25:50 +00001895 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001896 int l1, l2, prot, last_prot;
1897 uint32_t pgd, pde, pte, start, end;
1898
bellard6a00d602005-11-21 23:25:50 +00001899 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001900
bellardb86bda52004-09-18 19:32:46 +00001901 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001902 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001903 return;
1904 }
1905 pgd = env->cr[3] & ~0xfff;
1906 last_prot = 0;
1907 start = -1;
1908 for(l1 = 0; l1 < 1024; l1++) {
1909 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1910 pde = le32_to_cpu(pde);
1911 end = l1 << 22;
1912 if (pde & PG_PRESENT_MASK) {
1913 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1914 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001915 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001916 } else {
1917 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001918 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001919 (uint8_t *)&pte, 4);
1920 pte = le32_to_cpu(pte);
1921 end = (l1 << 22) + (l2 << 12);
1922 if (pte & PG_PRESENT_MASK) {
1923 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1924 } else {
1925 prot = 0;
1926 }
aliguori376253e2009-03-05 23:01:23 +00001927 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001928 }
1929 }
1930 } else {
1931 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001932 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001933 }
1934 }
1935}
1936#endif
1937
aurel327c664e22009-03-03 06:12:22 +00001938#if defined(TARGET_SH4)
1939
aliguori376253e2009-03-05 23:01:23 +00001940static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001941{
aliguori376253e2009-03-05 23:01:23 +00001942 monitor_printf(mon, " tlb%i:\t"
1943 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1944 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1945 "dirty=%hhu writethrough=%hhu\n",
1946 idx,
1947 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1948 tlb->v, tlb->sh, tlb->c, tlb->pr,
1949 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001950}
1951
aliguori376253e2009-03-05 23:01:23 +00001952static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001953{
1954 CPUState *env = mon_get_cpu();
1955 int i;
1956
aliguori376253e2009-03-05 23:01:23 +00001957 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001958 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001959 print_tlb (mon, i, &env->itlb[i]);
1960 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001961 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001962 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001963}
1964
1965#endif
1966
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001967static void do_info_kvm_print(Monitor *mon, const QObject *data)
1968{
1969 QDict *qdict;
1970
1971 qdict = qobject_to_qdict(data);
1972
1973 monitor_printf(mon, "kvm support: ");
1974 if (qdict_get_bool(qdict, "present")) {
1975 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
1976 "enabled" : "disabled");
1977 } else {
1978 monitor_printf(mon, "not compiled\n");
1979 }
1980}
1981
1982/**
1983 * do_info_kvm(): Show KVM information
1984 *
1985 * Return a QDict with the following information:
1986 *
1987 * - "enabled": true if KVM support is enabled, false otherwise
1988 * - "present": true if QEMU has KVM support, false otherwise
1989 *
1990 * Example:
1991 *
1992 * { "enabled": true, "present": true }
1993 */
1994static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00001995{
1996#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001997 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
1998 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00001999#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002000 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002001#endif
2002}
2003
aliguori030ea372009-04-21 22:30:47 +00002004static void do_info_numa(Monitor *mon)
2005{
aliguorib28b6232009-04-22 20:20:29 +00002006 int i;
aliguori030ea372009-04-21 22:30:47 +00002007 CPUState *env;
2008
2009 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2010 for (i = 0; i < nb_numa_nodes; i++) {
2011 monitor_printf(mon, "node %d cpus:", i);
2012 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2013 if (env->numa_node == i) {
2014 monitor_printf(mon, " %d", env->cpu_index);
2015 }
2016 }
2017 monitor_printf(mon, "\n");
2018 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2019 node_mem[i] >> 20);
2020 }
2021}
2022
bellard5f1ce942006-02-08 22:40:15 +00002023#ifdef CONFIG_PROFILER
2024
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002025int64_t qemu_time;
2026int64_t dev_time;
2027
aliguori376253e2009-03-05 23:01:23 +00002028static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002029{
2030 int64_t total;
2031 total = qemu_time;
2032 if (total == 0)
2033 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002034 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002035 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002036 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002037 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002038 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002039 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002040}
2041#else
aliguori376253e2009-03-05 23:01:23 +00002042static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002043{
aliguori376253e2009-03-05 23:01:23 +00002044 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002045}
2046#endif
2047
bellardec36b692006-07-16 18:57:03 +00002048/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002049static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002050
aliguori376253e2009-03-05 23:01:23 +00002051static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002052{
2053 int i;
2054 CaptureState *s;
2055
2056 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002057 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002058 s->ops.info (s->opaque);
2059 }
2060}
2061
Blue Swirl23130862009-06-06 08:22:04 +00002062#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002063static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002064{
2065 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002066 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002067 CaptureState *s;
2068
2069 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2070 if (i == n) {
2071 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002072 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002073 qemu_free (s);
2074 return;
2075 }
2076 }
2077}
2078
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002079static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002080{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002081 const char *path = qdict_get_str(qdict, "path");
2082 int has_freq = qdict_haskey(qdict, "freq");
2083 int freq = qdict_get_try_int(qdict, "freq", -1);
2084 int has_bits = qdict_haskey(qdict, "bits");
2085 int bits = qdict_get_try_int(qdict, "bits", -1);
2086 int has_channels = qdict_haskey(qdict, "nchannels");
2087 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002088 CaptureState *s;
2089
2090 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002091
2092 freq = has_freq ? freq : 44100;
2093 bits = has_bits ? bits : 16;
2094 nchannels = has_channels ? nchannels : 2;
2095
2096 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002097 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002098 qemu_free (s);
2099 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002100 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002101}
2102#endif
2103
aurel32dc1c0b72008-04-27 23:52:12 +00002104#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002105static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002106{
2107 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002108 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002109
2110 for (env = first_cpu; env != NULL; env = env->next_cpu)
2111 if (env->cpu_index == cpu_index) {
2112 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2113 break;
2114 }
2115}
2116#endif
2117
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002118static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002119{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002120 QDict *qdict;
2121
2122 qdict = qobject_to_qdict(data);
2123
2124 monitor_printf(mon, "VM status: ");
2125 if (qdict_get_bool(qdict, "running")) {
2126 monitor_printf(mon, "running");
2127 if (qdict_get_bool(qdict, "singlestep")) {
2128 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002129 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002130 } else {
2131 monitor_printf(mon, "paused");
2132 }
2133
2134 monitor_printf(mon, "\n");
2135}
2136
2137/**
2138 * do_info_status(): VM status
2139 *
2140 * Return a QDict with the following information:
2141 *
2142 * - "running": true if the VM is running, or false if it is paused
2143 * - "singlestep": true if the VM is in single step mode, false otherwise
2144 *
2145 * Example:
2146 *
2147 * { "running": true, "singlestep": false }
2148 */
2149static void do_info_status(Monitor *mon, QObject **ret_data)
2150{
2151 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2152 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002153}
2154
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002155static ram_addr_t balloon_get_value(void)
2156{
2157 ram_addr_t actual;
2158
2159 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2160 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2161 return 0;
2162 }
2163
2164 actual = qemu_balloon_status();
2165 if (actual == 0) {
2166 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2167 return 0;
2168 }
2169
2170 return actual;
2171}
2172
Luiz Capitulino83fb1de2009-10-07 13:42:01 -03002173/**
2174 * do_balloon(): Request VM to change its memory allocation
2175 */
2176static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00002177{
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002178 if (balloon_get_value()) {
2179 /* ballooning is active */
Luiz Capitulino3b0bd6e2009-12-18 13:25:05 -02002180 qemu_balloon(qdict_get_int(qdict, "value"));
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002181 }
aliguoridf751fa2008-12-04 20:19:35 +00002182}
2183
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002184static void monitor_print_balloon(Monitor *mon, const QObject *data)
2185{
Luiz Capitulino7f179672009-12-10 17:15:55 -02002186 QDict *qdict;
2187
2188 qdict = qobject_to_qdict(data);
2189
2190 monitor_printf(mon, "balloon: actual=%" PRId64 "\n",
2191 qdict_get_int(qdict, "balloon") >> 20);
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002192}
2193
2194/**
2195 * do_info_balloon(): Balloon information
Luiz Capitulino7f179672009-12-10 17:15:55 -02002196 *
2197 * Return a QDict with the following information:
2198 *
2199 * - "balloon": current balloon value in bytes
2200 *
2201 * Example:
2202 *
2203 * { "balloon": 1073741824 }
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002204 */
2205static void do_info_balloon(Monitor *mon, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00002206{
Anthony Liguoric227f092009-10-01 16:12:16 -05002207 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00002208
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002209 actual = balloon_get_value();
2210 if (actual != 0) {
Luiz Capitulino7f179672009-12-10 17:15:55 -02002211 *ret_data = qobject_from_jsonf("{ 'balloon': %" PRId64 "}",
2212 (int64_t) actual);
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002213 }
aliguoridf751fa2008-12-04 20:19:35 +00002214}
2215
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002216static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002217{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002218 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002219
aliguori76655d62009-03-06 20:27:37 +00002220 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002221 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002222 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002223 return acl;
2224}
aliguori76655d62009-03-06 20:27:37 +00002225
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002226static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002227{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002228 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002229 qemu_acl *acl = find_acl(mon, aclname);
2230 qemu_acl_entry *entry;
2231 int i = 0;
2232
2233 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002234 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002235 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002236 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002237 i++;
2238 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002239 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002240 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002241 }
2242}
2243
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002244static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002245{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002246 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002247 qemu_acl *acl = find_acl(mon, aclname);
2248
2249 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002250 qemu_acl_reset(acl);
2251 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002252 }
2253}
aliguori76655d62009-03-06 20:27:37 +00002254
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002255static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002256{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002257 const char *aclname = qdict_get_str(qdict, "aclname");
2258 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002259 qemu_acl *acl = find_acl(mon, aclname);
2260
2261 if (acl) {
2262 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002263 acl->defaultDeny = 0;
2264 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002265 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002266 acl->defaultDeny = 1;
2267 monitor_printf(mon, "acl: policy set to 'deny'\n");
2268 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002269 monitor_printf(mon, "acl: unknown policy '%s', "
2270 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002271 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002272 }
2273}
aliguori76655d62009-03-06 20:27:37 +00002274
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002275static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002276{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002277 const char *aclname = qdict_get_str(qdict, "aclname");
2278 const char *match = qdict_get_str(qdict, "match");
2279 const char *policy = qdict_get_str(qdict, "policy");
2280 int has_index = qdict_haskey(qdict, "index");
2281 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002282 qemu_acl *acl = find_acl(mon, aclname);
2283 int deny, ret;
2284
2285 if (acl) {
2286 if (strcmp(policy, "allow") == 0) {
2287 deny = 0;
2288 } else if (strcmp(policy, "deny") == 0) {
2289 deny = 1;
2290 } else {
2291 monitor_printf(mon, "acl: unknown policy '%s', "
2292 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002293 return;
2294 }
aliguori28a76be2009-03-06 20:27:40 +00002295 if (has_index)
2296 ret = qemu_acl_insert(acl, deny, match, index);
2297 else
2298 ret = qemu_acl_append(acl, deny, match);
2299 if (ret < 0)
2300 monitor_printf(mon, "acl: unable to add acl entry\n");
2301 else
2302 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002303 }
2304}
aliguori76655d62009-03-06 20:27:37 +00002305
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002306static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002307{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002308 const char *aclname = qdict_get_str(qdict, "aclname");
2309 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002310 qemu_acl *acl = find_acl(mon, aclname);
2311 int ret;
aliguori76655d62009-03-06 20:27:37 +00002312
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002313 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002314 ret = qemu_acl_remove(acl, match);
2315 if (ret < 0)
2316 monitor_printf(mon, "acl: no matching acl entry\n");
2317 else
2318 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002319 }
2320}
2321
Huang Ying79c4f6b2009-06-23 10:05:14 +08002322#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002323static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002324{
2325 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002326 int cpu_index = qdict_get_int(qdict, "cpu_index");
2327 int bank = qdict_get_int(qdict, "bank");
2328 uint64_t status = qdict_get_int(qdict, "status");
2329 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2330 uint64_t addr = qdict_get_int(qdict, "addr");
2331 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002332
2333 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2334 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2335 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2336 break;
2337 }
2338}
2339#endif
2340
Luiz Capitulinof0d60002009-10-16 12:23:50 -03002341static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002342{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002343 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002344 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002345 int fd;
2346
2347 fd = qemu_chr_get_msgfd(mon->chr);
2348 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002349 qemu_error_new(QERR_FD_NOT_SUPPLIED);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002350 return;
2351 }
2352
2353 if (qemu_isdigit(fdname[0])) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002354 qemu_error_new(QERR_INVALID_PARAMETER, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002355 return;
2356 }
2357
2358 fd = dup(fd);
2359 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002360 if (errno == EMFILE)
2361 qemu_error_new(QERR_TOO_MANY_FILES);
2362 else
2363 qemu_error_new(QERR_UNDEFINED_ERROR);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002364 return;
2365 }
2366
Blue Swirl72cf2d42009-09-12 07:36:22 +00002367 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002368 if (strcmp(monfd->name, fdname) != 0) {
2369 continue;
2370 }
2371
2372 close(monfd->fd);
2373 monfd->fd = fd;
2374 return;
2375 }
2376
Anthony Liguoric227f092009-10-01 16:12:16 -05002377 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002378 monfd->name = qemu_strdup(fdname);
2379 monfd->fd = fd;
2380
Blue Swirl72cf2d42009-09-12 07:36:22 +00002381 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002382}
2383
Luiz Capitulino18f3a512009-10-16 12:23:51 -03002384static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002385{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002386 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002387 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002388
Blue Swirl72cf2d42009-09-12 07:36:22 +00002389 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002390 if (strcmp(monfd->name, fdname) != 0) {
2391 continue;
2392 }
2393
Blue Swirl72cf2d42009-09-12 07:36:22 +00002394 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002395 close(monfd->fd);
2396 qemu_free(monfd->name);
2397 qemu_free(monfd);
2398 return;
2399 }
2400
Markus Armbruster063c1a02009-12-07 21:37:11 +01002401 qemu_error_new(QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002402}
2403
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002404static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002405{
2406 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002407 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002408
2409 vm_stop(0);
2410
Juan Quintela05f24012009-08-20 19:42:22 +02002411 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002412 vm_start();
2413}
2414
Mark McLoughlin7768e042009-07-22 09:11:41 +01002415int monitor_get_fd(Monitor *mon, const char *fdname)
2416{
Anthony Liguoric227f092009-10-01 16:12:16 -05002417 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002418
Blue Swirl72cf2d42009-09-12 07:36:22 +00002419 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002420 int fd;
2421
2422 if (strcmp(monfd->name, fdname) != 0) {
2423 continue;
2424 }
2425
2426 fd = monfd->fd;
2427
2428 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002429 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002430 qemu_free(monfd->name);
2431 qemu_free(monfd);
2432
2433 return fd;
2434 }
2435
2436 return -1;
2437}
2438
Anthony Liguoric227f092009-10-01 16:12:16 -05002439static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002440#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002441 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002442};
2443
Blue Swirl23130862009-06-06 08:22:04 +00002444/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002445static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002446 {
2447 .name = "version",
2448 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002449 .params = "",
2450 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002451 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002452 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002453 },
2454 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002455 .name = "commands",
2456 .args_type = "",
2457 .params = "",
2458 .help = "list QMP available commands",
2459 .user_print = monitor_user_noop,
2460 .mhandler.info_new = do_info_commands,
2461 },
2462 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002463 .name = "network",
2464 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002465 .params = "",
2466 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002467 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002468 },
2469 {
2470 .name = "chardev",
2471 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002472 .params = "",
2473 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002474 .user_print = qemu_chr_info_print,
2475 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002476 },
2477 {
2478 .name = "block",
2479 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002480 .params = "",
2481 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002482 .user_print = bdrv_info_print,
2483 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002484 },
2485 {
2486 .name = "blockstats",
2487 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002488 .params = "",
2489 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002490 .user_print = bdrv_stats_print,
2491 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002492 },
2493 {
2494 .name = "registers",
2495 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002496 .params = "",
2497 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002498 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002499 },
2500 {
2501 .name = "cpus",
2502 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002503 .params = "",
2504 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002505 .user_print = monitor_print_cpus,
2506 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002507 },
2508 {
2509 .name = "history",
2510 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002511 .params = "",
2512 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002513 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002514 },
2515 {
2516 .name = "irq",
2517 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002518 .params = "",
2519 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002520 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002521 },
2522 {
2523 .name = "pic",
2524 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002525 .params = "",
2526 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002527 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002528 },
2529 {
2530 .name = "pci",
2531 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002532 .params = "",
2533 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002534 .user_print = do_pci_info_print,
2535 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002536 },
aurel327c664e22009-03-03 06:12:22 +00002537#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002538 {
2539 .name = "tlb",
2540 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002541 .params = "",
2542 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002543 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002544 },
aurel327c664e22009-03-03 06:12:22 +00002545#endif
2546#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002547 {
2548 .name = "mem",
2549 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002550 .params = "",
2551 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002552 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002553 },
2554 {
2555 .name = "hpet",
2556 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002557 .params = "",
2558 .help = "show state of HPET",
Luiz Capitulino14f07202009-12-10 17:16:02 -02002559 .user_print = do_info_hpet_print,
2560 .mhandler.info_new = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002561 },
bellardb86bda52004-09-18 19:32:46 +00002562#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002563 {
2564 .name = "jit",
2565 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002566 .params = "",
2567 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002568 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002569 },
2570 {
2571 .name = "kvm",
2572 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002573 .params = "",
2574 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002575 .user_print = do_info_kvm_print,
2576 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002577 },
2578 {
2579 .name = "numa",
2580 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002581 .params = "",
2582 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002583 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002584 },
2585 {
2586 .name = "usb",
2587 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002588 .params = "",
2589 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002590 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002591 },
2592 {
2593 .name = "usbhost",
2594 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002595 .params = "",
2596 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002597 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002598 },
2599 {
2600 .name = "profile",
2601 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002602 .params = "",
2603 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002604 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002605 },
2606 {
2607 .name = "capture",
2608 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002609 .params = "",
2610 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002611 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002612 },
2613 {
2614 .name = "snapshots",
2615 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002616 .params = "",
2617 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002618 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002619 },
2620 {
2621 .name = "status",
2622 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002623 .params = "",
2624 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002625 .user_print = do_info_status_print,
2626 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002627 },
2628 {
2629 .name = "pcmcia",
2630 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002631 .params = "",
2632 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002633 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002634 },
2635 {
2636 .name = "mice",
2637 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002638 .params = "",
2639 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002640 .user_print = do_info_mice_print,
2641 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002642 },
2643 {
2644 .name = "vnc",
2645 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002646 .params = "",
2647 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002648 .user_print = do_info_vnc_print,
2649 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002650 },
2651 {
2652 .name = "name",
2653 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002654 .params = "",
2655 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002656 .user_print = do_info_name_print,
2657 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002658 },
2659 {
2660 .name = "uuid",
2661 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002662 .params = "",
2663 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002664 .user_print = do_info_uuid_print,
2665 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002666 },
j_mayer76a66252007-03-07 08:32:30 +00002667#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002668 {
2669 .name = "cpustats",
2670 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 .params = "",
2672 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002673 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002674 },
j_mayer76a66252007-03-07 08:32:30 +00002675#endif
blueswir131a60e22007-10-26 18:42:59 +00002676#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002677 {
2678 .name = "usernet",
2679 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002680 .params = "",
2681 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002682 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002683 },
blueswir131a60e22007-10-26 18:42:59 +00002684#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002685 {
2686 .name = "migrate",
2687 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002688 .params = "",
2689 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002690 .user_print = do_info_migrate_print,
2691 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002692 },
2693 {
2694 .name = "balloon",
2695 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002696 .params = "",
2697 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002698 .user_print = monitor_print_balloon,
2699 .mhandler.info_new = do_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002700 },
2701 {
2702 .name = "qtree",
2703 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002704 .params = "",
2705 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002706 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002707 },
2708 {
2709 .name = "qdm",
2710 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002711 .params = "",
2712 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002713 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002714 },
2715 {
2716 .name = "roms",
2717 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002718 .params = "",
2719 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002720 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002721 },
2722 {
2723 .name = NULL,
2724 },
bellard9dc39cb2004-03-14 21:38:27 +00002725};
2726
bellard9307c4c2004-04-04 12:57:25 +00002727/*******************************************************************/
2728
2729static const char *pch;
2730static jmp_buf expr_env;
2731
bellard92a31b12005-02-10 22:00:52 +00002732#define MD_TLONG 0
2733#define MD_I32 1
2734
bellard9307c4c2004-04-04 12:57:25 +00002735typedef struct MonitorDef {
2736 const char *name;
2737 int offset;
blueswir18662d652008-10-02 18:32:44 +00002738 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002739 int type;
bellard9307c4c2004-04-04 12:57:25 +00002740} MonitorDef;
2741
bellard57206fd2004-04-25 18:54:52 +00002742#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002743static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002744{
bellard6a00d602005-11-21 23:25:50 +00002745 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002746 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002747}
2748#endif
2749
bellarda541f292004-04-12 20:39:29 +00002750#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002751static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002752{
bellard6a00d602005-11-21 23:25:50 +00002753 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002754 unsigned int u;
2755 int i;
2756
2757 u = 0;
2758 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002759 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002760
2761 return u;
2762}
2763
blueswir18662d652008-10-02 18:32:44 +00002764static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002765{
bellard6a00d602005-11-21 23:25:50 +00002766 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002767 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002768}
2769
blueswir18662d652008-10-02 18:32:44 +00002770static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002771{
bellard6a00d602005-11-21 23:25:50 +00002772 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002773 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002774}
bellard9fddaa02004-05-21 12:59:32 +00002775
blueswir18662d652008-10-02 18:32:44 +00002776static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002777{
bellard6a00d602005-11-21 23:25:50 +00002778 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002779 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002780}
2781
blueswir18662d652008-10-02 18:32:44 +00002782static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002783{
bellard6a00d602005-11-21 23:25:50 +00002784 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002785 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002786}
2787
blueswir18662d652008-10-02 18:32:44 +00002788static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002789{
bellard6a00d602005-11-21 23:25:50 +00002790 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002791 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002792}
bellarda541f292004-04-12 20:39:29 +00002793#endif
2794
bellarde95c8d52004-09-30 22:22:08 +00002795#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002796#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002797static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002798{
bellard6a00d602005-11-21 23:25:50 +00002799 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002800 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002801}
bellard7b936c02005-10-30 17:05:13 +00002802#endif
bellarde95c8d52004-09-30 22:22:08 +00002803
blueswir18662d652008-10-02 18:32:44 +00002804static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002805{
bellard6a00d602005-11-21 23:25:50 +00002806 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002807 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002808}
2809#endif
2810
blueswir18662d652008-10-02 18:32:44 +00002811static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002812#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002813
2814#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002815 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002816 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002817 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002818
bellard9307c4c2004-04-04 12:57:25 +00002819 { "eax", offsetof(CPUState, regs[0]) },
2820 { "ecx", offsetof(CPUState, regs[1]) },
2821 { "edx", offsetof(CPUState, regs[2]) },
2822 { "ebx", offsetof(CPUState, regs[3]) },
2823 { "esp|sp", offsetof(CPUState, regs[4]) },
2824 { "ebp|fp", offsetof(CPUState, regs[5]) },
2825 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002826 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002827#ifdef TARGET_X86_64
2828 { "r8", offsetof(CPUState, regs[8]) },
2829 { "r9", offsetof(CPUState, regs[9]) },
2830 { "r10", offsetof(CPUState, regs[10]) },
2831 { "r11", offsetof(CPUState, regs[11]) },
2832 { "r12", offsetof(CPUState, regs[12]) },
2833 { "r13", offsetof(CPUState, regs[13]) },
2834 { "r14", offsetof(CPUState, regs[14]) },
2835 { "r15", offsetof(CPUState, regs[15]) },
2836#endif
bellard9307c4c2004-04-04 12:57:25 +00002837 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002838 { "eip", offsetof(CPUState, eip) },
2839 SEG("cs", R_CS)
2840 SEG("ds", R_DS)
2841 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002842 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002843 SEG("fs", R_FS)
2844 SEG("gs", R_GS)
2845 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002846#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002847 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002848 { "r0", offsetof(CPUState, gpr[0]) },
2849 { "r1", offsetof(CPUState, gpr[1]) },
2850 { "r2", offsetof(CPUState, gpr[2]) },
2851 { "r3", offsetof(CPUState, gpr[3]) },
2852 { "r4", offsetof(CPUState, gpr[4]) },
2853 { "r5", offsetof(CPUState, gpr[5]) },
2854 { "r6", offsetof(CPUState, gpr[6]) },
2855 { "r7", offsetof(CPUState, gpr[7]) },
2856 { "r8", offsetof(CPUState, gpr[8]) },
2857 { "r9", offsetof(CPUState, gpr[9]) },
2858 { "r10", offsetof(CPUState, gpr[10]) },
2859 { "r11", offsetof(CPUState, gpr[11]) },
2860 { "r12", offsetof(CPUState, gpr[12]) },
2861 { "r13", offsetof(CPUState, gpr[13]) },
2862 { "r14", offsetof(CPUState, gpr[14]) },
2863 { "r15", offsetof(CPUState, gpr[15]) },
2864 { "r16", offsetof(CPUState, gpr[16]) },
2865 { "r17", offsetof(CPUState, gpr[17]) },
2866 { "r18", offsetof(CPUState, gpr[18]) },
2867 { "r19", offsetof(CPUState, gpr[19]) },
2868 { "r20", offsetof(CPUState, gpr[20]) },
2869 { "r21", offsetof(CPUState, gpr[21]) },
2870 { "r22", offsetof(CPUState, gpr[22]) },
2871 { "r23", offsetof(CPUState, gpr[23]) },
2872 { "r24", offsetof(CPUState, gpr[24]) },
2873 { "r25", offsetof(CPUState, gpr[25]) },
2874 { "r26", offsetof(CPUState, gpr[26]) },
2875 { "r27", offsetof(CPUState, gpr[27]) },
2876 { "r28", offsetof(CPUState, gpr[28]) },
2877 { "r29", offsetof(CPUState, gpr[29]) },
2878 { "r30", offsetof(CPUState, gpr[30]) },
2879 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002880 /* Floating point registers */
2881 { "f0", offsetof(CPUState, fpr[0]) },
2882 { "f1", offsetof(CPUState, fpr[1]) },
2883 { "f2", offsetof(CPUState, fpr[2]) },
2884 { "f3", offsetof(CPUState, fpr[3]) },
2885 { "f4", offsetof(CPUState, fpr[4]) },
2886 { "f5", offsetof(CPUState, fpr[5]) },
2887 { "f6", offsetof(CPUState, fpr[6]) },
2888 { "f7", offsetof(CPUState, fpr[7]) },
2889 { "f8", offsetof(CPUState, fpr[8]) },
2890 { "f9", offsetof(CPUState, fpr[9]) },
2891 { "f10", offsetof(CPUState, fpr[10]) },
2892 { "f11", offsetof(CPUState, fpr[11]) },
2893 { "f12", offsetof(CPUState, fpr[12]) },
2894 { "f13", offsetof(CPUState, fpr[13]) },
2895 { "f14", offsetof(CPUState, fpr[14]) },
2896 { "f15", offsetof(CPUState, fpr[15]) },
2897 { "f16", offsetof(CPUState, fpr[16]) },
2898 { "f17", offsetof(CPUState, fpr[17]) },
2899 { "f18", offsetof(CPUState, fpr[18]) },
2900 { "f19", offsetof(CPUState, fpr[19]) },
2901 { "f20", offsetof(CPUState, fpr[20]) },
2902 { "f21", offsetof(CPUState, fpr[21]) },
2903 { "f22", offsetof(CPUState, fpr[22]) },
2904 { "f23", offsetof(CPUState, fpr[23]) },
2905 { "f24", offsetof(CPUState, fpr[24]) },
2906 { "f25", offsetof(CPUState, fpr[25]) },
2907 { "f26", offsetof(CPUState, fpr[26]) },
2908 { "f27", offsetof(CPUState, fpr[27]) },
2909 { "f28", offsetof(CPUState, fpr[28]) },
2910 { "f29", offsetof(CPUState, fpr[29]) },
2911 { "f30", offsetof(CPUState, fpr[30]) },
2912 { "f31", offsetof(CPUState, fpr[31]) },
2913 { "fpscr", offsetof(CPUState, fpscr) },
2914 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002915 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002916 { "lr", offsetof(CPUState, lr) },
2917 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002918 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002919 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002920 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002921 { "msr", 0, &monitor_get_msr, },
2922 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002923 { "tbu", 0, &monitor_get_tbu, },
2924 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002925#if defined(TARGET_PPC64)
2926 /* Address space register */
2927 { "asr", offsetof(CPUState, asr) },
2928#endif
2929 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002930 { "sdr1", offsetof(CPUState, sdr1) },
2931 { "sr0", offsetof(CPUState, sr[0]) },
2932 { "sr1", offsetof(CPUState, sr[1]) },
2933 { "sr2", offsetof(CPUState, sr[2]) },
2934 { "sr3", offsetof(CPUState, sr[3]) },
2935 { "sr4", offsetof(CPUState, sr[4]) },
2936 { "sr5", offsetof(CPUState, sr[5]) },
2937 { "sr6", offsetof(CPUState, sr[6]) },
2938 { "sr7", offsetof(CPUState, sr[7]) },
2939 { "sr8", offsetof(CPUState, sr[8]) },
2940 { "sr9", offsetof(CPUState, sr[9]) },
2941 { "sr10", offsetof(CPUState, sr[10]) },
2942 { "sr11", offsetof(CPUState, sr[11]) },
2943 { "sr12", offsetof(CPUState, sr[12]) },
2944 { "sr13", offsetof(CPUState, sr[13]) },
2945 { "sr14", offsetof(CPUState, sr[14]) },
2946 { "sr15", offsetof(CPUState, sr[15]) },
2947 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002948#elif defined(TARGET_SPARC)
2949 { "g0", offsetof(CPUState, gregs[0]) },
2950 { "g1", offsetof(CPUState, gregs[1]) },
2951 { "g2", offsetof(CPUState, gregs[2]) },
2952 { "g3", offsetof(CPUState, gregs[3]) },
2953 { "g4", offsetof(CPUState, gregs[4]) },
2954 { "g5", offsetof(CPUState, gregs[5]) },
2955 { "g6", offsetof(CPUState, gregs[6]) },
2956 { "g7", offsetof(CPUState, gregs[7]) },
2957 { "o0", 0, monitor_get_reg },
2958 { "o1", 1, monitor_get_reg },
2959 { "o2", 2, monitor_get_reg },
2960 { "o3", 3, monitor_get_reg },
2961 { "o4", 4, monitor_get_reg },
2962 { "o5", 5, monitor_get_reg },
2963 { "o6", 6, monitor_get_reg },
2964 { "o7", 7, monitor_get_reg },
2965 { "l0", 8, monitor_get_reg },
2966 { "l1", 9, monitor_get_reg },
2967 { "l2", 10, monitor_get_reg },
2968 { "l3", 11, monitor_get_reg },
2969 { "l4", 12, monitor_get_reg },
2970 { "l5", 13, monitor_get_reg },
2971 { "l6", 14, monitor_get_reg },
2972 { "l7", 15, monitor_get_reg },
2973 { "i0", 16, monitor_get_reg },
2974 { "i1", 17, monitor_get_reg },
2975 { "i2", 18, monitor_get_reg },
2976 { "i3", 19, monitor_get_reg },
2977 { "i4", 20, monitor_get_reg },
2978 { "i5", 21, monitor_get_reg },
2979 { "i6", 22, monitor_get_reg },
2980 { "i7", 23, monitor_get_reg },
2981 { "pc", offsetof(CPUState, pc) },
2982 { "npc", offsetof(CPUState, npc) },
2983 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002984#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002985 { "psr", 0, &monitor_get_psr, },
2986 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002987#endif
bellarde95c8d52004-09-30 22:22:08 +00002988 { "tbr", offsetof(CPUState, tbr) },
2989 { "fsr", offsetof(CPUState, fsr) },
2990 { "f0", offsetof(CPUState, fpr[0]) },
2991 { "f1", offsetof(CPUState, fpr[1]) },
2992 { "f2", offsetof(CPUState, fpr[2]) },
2993 { "f3", offsetof(CPUState, fpr[3]) },
2994 { "f4", offsetof(CPUState, fpr[4]) },
2995 { "f5", offsetof(CPUState, fpr[5]) },
2996 { "f6", offsetof(CPUState, fpr[6]) },
2997 { "f7", offsetof(CPUState, fpr[7]) },
2998 { "f8", offsetof(CPUState, fpr[8]) },
2999 { "f9", offsetof(CPUState, fpr[9]) },
3000 { "f10", offsetof(CPUState, fpr[10]) },
3001 { "f11", offsetof(CPUState, fpr[11]) },
3002 { "f12", offsetof(CPUState, fpr[12]) },
3003 { "f13", offsetof(CPUState, fpr[13]) },
3004 { "f14", offsetof(CPUState, fpr[14]) },
3005 { "f15", offsetof(CPUState, fpr[15]) },
3006 { "f16", offsetof(CPUState, fpr[16]) },
3007 { "f17", offsetof(CPUState, fpr[17]) },
3008 { "f18", offsetof(CPUState, fpr[18]) },
3009 { "f19", offsetof(CPUState, fpr[19]) },
3010 { "f20", offsetof(CPUState, fpr[20]) },
3011 { "f21", offsetof(CPUState, fpr[21]) },
3012 { "f22", offsetof(CPUState, fpr[22]) },
3013 { "f23", offsetof(CPUState, fpr[23]) },
3014 { "f24", offsetof(CPUState, fpr[24]) },
3015 { "f25", offsetof(CPUState, fpr[25]) },
3016 { "f26", offsetof(CPUState, fpr[26]) },
3017 { "f27", offsetof(CPUState, fpr[27]) },
3018 { "f28", offsetof(CPUState, fpr[28]) },
3019 { "f29", offsetof(CPUState, fpr[29]) },
3020 { "f30", offsetof(CPUState, fpr[30]) },
3021 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003022#ifdef TARGET_SPARC64
3023 { "f32", offsetof(CPUState, fpr[32]) },
3024 { "f34", offsetof(CPUState, fpr[34]) },
3025 { "f36", offsetof(CPUState, fpr[36]) },
3026 { "f38", offsetof(CPUState, fpr[38]) },
3027 { "f40", offsetof(CPUState, fpr[40]) },
3028 { "f42", offsetof(CPUState, fpr[42]) },
3029 { "f44", offsetof(CPUState, fpr[44]) },
3030 { "f46", offsetof(CPUState, fpr[46]) },
3031 { "f48", offsetof(CPUState, fpr[48]) },
3032 { "f50", offsetof(CPUState, fpr[50]) },
3033 { "f52", offsetof(CPUState, fpr[52]) },
3034 { "f54", offsetof(CPUState, fpr[54]) },
3035 { "f56", offsetof(CPUState, fpr[56]) },
3036 { "f58", offsetof(CPUState, fpr[58]) },
3037 { "f60", offsetof(CPUState, fpr[60]) },
3038 { "f62", offsetof(CPUState, fpr[62]) },
3039 { "asi", offsetof(CPUState, asi) },
3040 { "pstate", offsetof(CPUState, pstate) },
3041 { "cansave", offsetof(CPUState, cansave) },
3042 { "canrestore", offsetof(CPUState, canrestore) },
3043 { "otherwin", offsetof(CPUState, otherwin) },
3044 { "wstate", offsetof(CPUState, wstate) },
3045 { "cleanwin", offsetof(CPUState, cleanwin) },
3046 { "fprs", offsetof(CPUState, fprs) },
3047#endif
bellard9307c4c2004-04-04 12:57:25 +00003048#endif
3049 { NULL },
3050};
3051
aliguori376253e2009-03-05 23:01:23 +00003052static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003053{
aliguori376253e2009-03-05 23:01:23 +00003054 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003055 longjmp(expr_env, 1);
3056}
3057
Markus Armbruster09b94182010-01-20 13:07:30 +01003058/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003059static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003060{
blueswir18662d652008-10-02 18:32:44 +00003061 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003062 void *ptr;
3063
bellard9307c4c2004-04-04 12:57:25 +00003064 for(md = monitor_defs; md->name != NULL; md++) {
3065 if (compare_cmd(name, md->name)) {
3066 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003067 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003068 } else {
bellard6a00d602005-11-21 23:25:50 +00003069 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003070 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003071 switch(md->type) {
3072 case MD_I32:
3073 *pval = *(int32_t *)ptr;
3074 break;
3075 case MD_TLONG:
3076 *pval = *(target_long *)ptr;
3077 break;
3078 default:
3079 *pval = 0;
3080 break;
3081 }
bellard9307c4c2004-04-04 12:57:25 +00003082 }
3083 return 0;
3084 }
3085 }
3086 return -1;
3087}
3088
3089static void next(void)
3090{
Blue Swirl660f11b2009-07-31 21:16:51 +00003091 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003092 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003093 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003094 pch++;
3095 }
3096}
3097
aliguori376253e2009-03-05 23:01:23 +00003098static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003099
aliguori376253e2009-03-05 23:01:23 +00003100static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003101{
blueswir1c2efc952007-09-25 17:28:42 +00003102 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003103 char *p;
bellard6a00d602005-11-21 23:25:50 +00003104 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003105
3106 switch(*pch) {
3107 case '+':
3108 next();
aliguori376253e2009-03-05 23:01:23 +00003109 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003110 break;
3111 case '-':
3112 next();
aliguori376253e2009-03-05 23:01:23 +00003113 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003114 break;
3115 case '~':
3116 next();
aliguori376253e2009-03-05 23:01:23 +00003117 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003118 break;
3119 case '(':
3120 next();
aliguori376253e2009-03-05 23:01:23 +00003121 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003122 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003123 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003124 }
3125 next();
3126 break;
bellard81d09122004-07-14 17:21:37 +00003127 case '\'':
3128 pch++;
3129 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003130 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003131 n = *pch;
3132 pch++;
3133 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003134 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003135 next();
3136 break;
bellard9307c4c2004-04-04 12:57:25 +00003137 case '$':
3138 {
3139 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003140 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003141
bellard9307c4c2004-04-04 12:57:25 +00003142 pch++;
3143 q = buf;
3144 while ((*pch >= 'a' && *pch <= 'z') ||
3145 (*pch >= 'A' && *pch <= 'Z') ||
3146 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003147 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003148 if ((q - buf) < sizeof(buf) - 1)
3149 *q++ = *pch;
3150 pch++;
3151 }
blueswir1cd390082008-11-16 13:53:32 +00003152 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003153 pch++;
3154 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003155 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003156 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003157 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003158 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003159 }
3160 break;
3161 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003162 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003163 n = 0;
3164 break;
3165 default:
blueswir17743e582007-09-24 18:39:04 +00003166#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003167 n = strtoull(pch, &p, 0);
3168#else
bellard9307c4c2004-04-04 12:57:25 +00003169 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003170#endif
bellard9307c4c2004-04-04 12:57:25 +00003171 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003172 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003173 }
3174 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003175 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003176 pch++;
3177 break;
3178 }
3179 return n;
3180}
3181
3182
aliguori376253e2009-03-05 23:01:23 +00003183static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003184{
blueswir1c2efc952007-09-25 17:28:42 +00003185 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003186 int op;
ths3b46e622007-09-17 08:09:54 +00003187
aliguori376253e2009-03-05 23:01:23 +00003188 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003189 for(;;) {
3190 op = *pch;
3191 if (op != '*' && op != '/' && op != '%')
3192 break;
3193 next();
aliguori376253e2009-03-05 23:01:23 +00003194 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003195 switch(op) {
3196 default:
3197 case '*':
3198 val *= val2;
3199 break;
3200 case '/':
3201 case '%':
ths5fafdf22007-09-16 21:08:06 +00003202 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003203 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003204 if (op == '/')
3205 val /= val2;
3206 else
3207 val %= val2;
3208 break;
3209 }
3210 }
3211 return val;
3212}
3213
aliguori376253e2009-03-05 23:01:23 +00003214static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003215{
blueswir1c2efc952007-09-25 17:28:42 +00003216 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003217 int op;
bellard9307c4c2004-04-04 12:57:25 +00003218
aliguori376253e2009-03-05 23:01:23 +00003219 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003220 for(;;) {
3221 op = *pch;
3222 if (op != '&' && op != '|' && op != '^')
3223 break;
3224 next();
aliguori376253e2009-03-05 23:01:23 +00003225 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003226 switch(op) {
3227 default:
3228 case '&':
3229 val &= val2;
3230 break;
3231 case '|':
3232 val |= val2;
3233 break;
3234 case '^':
3235 val ^= val2;
3236 break;
3237 }
3238 }
3239 return val;
3240}
3241
aliguori376253e2009-03-05 23:01:23 +00003242static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003243{
blueswir1c2efc952007-09-25 17:28:42 +00003244 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003245 int op;
bellard9307c4c2004-04-04 12:57:25 +00003246
aliguori376253e2009-03-05 23:01:23 +00003247 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003248 for(;;) {
3249 op = *pch;
3250 if (op != '+' && op != '-')
3251 break;
3252 next();
aliguori376253e2009-03-05 23:01:23 +00003253 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003254 if (op == '+')
3255 val += val2;
3256 else
3257 val -= val2;
3258 }
3259 return val;
3260}
3261
aliguori376253e2009-03-05 23:01:23 +00003262static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003263{
3264 pch = *pp;
3265 if (setjmp(expr_env)) {
3266 *pp = pch;
3267 return -1;
3268 }
blueswir1cd390082008-11-16 13:53:32 +00003269 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003270 pch++;
aliguori376253e2009-03-05 23:01:23 +00003271 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003272 *pp = pch;
3273 return 0;
3274}
3275
3276static int get_str(char *buf, int buf_size, const char **pp)
3277{
3278 const char *p;
3279 char *q;
3280 int c;
3281
bellard81d09122004-07-14 17:21:37 +00003282 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003283 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003284 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003285 p++;
3286 if (*p == '\0') {
3287 fail:
bellard81d09122004-07-14 17:21:37 +00003288 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003289 *pp = p;
3290 return -1;
3291 }
bellard9307c4c2004-04-04 12:57:25 +00003292 if (*p == '\"') {
3293 p++;
3294 while (*p != '\0' && *p != '\"') {
3295 if (*p == '\\') {
3296 p++;
3297 c = *p++;
3298 switch(c) {
3299 case 'n':
3300 c = '\n';
3301 break;
3302 case 'r':
3303 c = '\r';
3304 break;
3305 case '\\':
3306 case '\'':
3307 case '\"':
3308 break;
3309 default:
3310 qemu_printf("unsupported escape code: '\\%c'\n", c);
3311 goto fail;
3312 }
3313 if ((q - buf) < buf_size - 1) {
3314 *q++ = c;
3315 }
3316 } else {
3317 if ((q - buf) < buf_size - 1) {
3318 *q++ = *p;
3319 }
3320 p++;
3321 }
3322 }
3323 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003324 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003325 goto fail;
3326 }
3327 p++;
3328 } else {
blueswir1cd390082008-11-16 13:53:32 +00003329 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003330 if ((q - buf) < buf_size - 1) {
3331 *q++ = *p;
3332 }
3333 p++;
3334 }
bellard9307c4c2004-04-04 12:57:25 +00003335 }
bellard81d09122004-07-14 17:21:37 +00003336 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003337 *pp = p;
3338 return 0;
3339}
3340
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003341/*
3342 * Store the command-name in cmdname, and return a pointer to
3343 * the remaining of the command string.
3344 */
3345static const char *get_command_name(const char *cmdline,
3346 char *cmdname, size_t nlen)
3347{
3348 size_t len;
3349 const char *p, *pstart;
3350
3351 p = cmdline;
3352 while (qemu_isspace(*p))
3353 p++;
3354 if (*p == '\0')
3355 return NULL;
3356 pstart = p;
3357 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3358 p++;
3359 len = p - pstart;
3360 if (len > nlen - 1)
3361 len = nlen - 1;
3362 memcpy(cmdname, pstart, len);
3363 cmdname[len] = '\0';
3364 return p;
3365}
3366
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003367/**
3368 * Read key of 'type' into 'key' and return the current
3369 * 'type' pointer.
3370 */
3371static char *key_get_info(const char *type, char **key)
3372{
3373 size_t len;
3374 char *p, *str;
3375
3376 if (*type == ',')
3377 type++;
3378
3379 p = strchr(type, ':');
3380 if (!p) {
3381 *key = NULL;
3382 return NULL;
3383 }
3384 len = p - type;
3385
3386 str = qemu_malloc(len + 1);
3387 memcpy(str, type, len);
3388 str[len] = '\0';
3389
3390 *key = str;
3391 return ++p;
3392}
3393
bellard9307c4c2004-04-04 12:57:25 +00003394static int default_fmt_format = 'x';
3395static int default_fmt_size = 4;
3396
3397#define MAX_ARGS 16
3398
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003399static int is_valid_option(const char *c, const char *typestr)
3400{
3401 char option[3];
3402
3403 option[0] = '-';
3404 option[1] = *c;
3405 option[2] = '\0';
3406
3407 typestr = strstr(typestr, option);
3408 return (typestr != NULL);
3409}
3410
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003411static const mon_cmd_t *monitor_find_command(const char *cmdname)
3412{
3413 const mon_cmd_t *cmd;
3414
3415 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3416 if (compare_cmd(cmdname, cmd->name)) {
3417 return cmd;
3418 }
3419 }
3420
3421 return NULL;
3422}
3423
Anthony Liguoric227f092009-10-01 16:12:16 -05003424static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003425 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003426 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003427{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003428 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003429 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003430 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003431 char cmdname[256];
3432 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003433 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003434
3435#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003436 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003437#endif
ths3b46e622007-09-17 08:09:54 +00003438
bellard9307c4c2004-04-04 12:57:25 +00003439 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003440 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3441 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003442 return NULL;
ths3b46e622007-09-17 08:09:54 +00003443
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003444 cmd = monitor_find_command(cmdname);
3445 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003446 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003447 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003448 }
bellard9307c4c2004-04-04 12:57:25 +00003449
bellard9307c4c2004-04-04 12:57:25 +00003450 /* parse the parameters */
3451 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003452 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003453 typestr = key_get_info(typestr, &key);
3454 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003455 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003456 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003457 typestr++;
3458 switch(c) {
3459 case 'F':
bellard81d09122004-07-14 17:21:37 +00003460 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003461 case 's':
3462 {
3463 int ret;
ths3b46e622007-09-17 08:09:54 +00003464
blueswir1cd390082008-11-16 13:53:32 +00003465 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003466 p++;
3467 if (*typestr == '?') {
3468 typestr++;
3469 if (*p == '\0') {
3470 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003471 break;
bellard9307c4c2004-04-04 12:57:25 +00003472 }
3473 }
3474 ret = get_str(buf, sizeof(buf), &p);
3475 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003476 switch(c) {
3477 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003478 monitor_printf(mon, "%s: filename expected\n",
3479 cmdname);
bellard81d09122004-07-14 17:21:37 +00003480 break;
3481 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003482 monitor_printf(mon, "%s: block device name expected\n",
3483 cmdname);
bellard81d09122004-07-14 17:21:37 +00003484 break;
3485 default:
aliguori376253e2009-03-05 23:01:23 +00003486 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003487 break;
3488 }
bellard9307c4c2004-04-04 12:57:25 +00003489 goto fail;
3490 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003491 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003492 }
3493 break;
3494 case '/':
3495 {
3496 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003497
blueswir1cd390082008-11-16 13:53:32 +00003498 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003499 p++;
3500 if (*p == '/') {
3501 /* format found */
3502 p++;
3503 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003504 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003505 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003506 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003507 count = count * 10 + (*p - '0');
3508 p++;
3509 }
3510 }
3511 size = -1;
3512 format = -1;
3513 for(;;) {
3514 switch(*p) {
3515 case 'o':
3516 case 'd':
3517 case 'u':
3518 case 'x':
3519 case 'i':
3520 case 'c':
3521 format = *p++;
3522 break;
3523 case 'b':
3524 size = 1;
3525 p++;
3526 break;
3527 case 'h':
3528 size = 2;
3529 p++;
3530 break;
3531 case 'w':
3532 size = 4;
3533 p++;
3534 break;
3535 case 'g':
3536 case 'L':
3537 size = 8;
3538 p++;
3539 break;
3540 default:
3541 goto next;
3542 }
3543 }
3544 next:
blueswir1cd390082008-11-16 13:53:32 +00003545 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003546 monitor_printf(mon, "invalid char in format: '%c'\n",
3547 *p);
bellard9307c4c2004-04-04 12:57:25 +00003548 goto fail;
3549 }
bellard9307c4c2004-04-04 12:57:25 +00003550 if (format < 0)
3551 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003552 if (format != 'i') {
3553 /* for 'i', not specifying a size gives -1 as size */
3554 if (size < 0)
3555 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003556 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003557 }
bellard9307c4c2004-04-04 12:57:25 +00003558 default_fmt_format = format;
3559 } else {
3560 count = 1;
3561 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003562 if (format != 'i') {
3563 size = default_fmt_size;
3564 } else {
3565 size = -1;
3566 }
bellard9307c4c2004-04-04 12:57:25 +00003567 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003568 qdict_put(qdict, "count", qint_from_int(count));
3569 qdict_put(qdict, "format", qint_from_int(format));
3570 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003571 }
3572 break;
3573 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003574 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003575 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003576 {
blueswir1c2efc952007-09-25 17:28:42 +00003577 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003578
blueswir1cd390082008-11-16 13:53:32 +00003579 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003580 p++;
bellard34405572004-06-08 00:55:58 +00003581 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003582 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003583 if (*p == '\0') {
3584 typestr++;
3585 break;
3586 }
bellard34405572004-06-08 00:55:58 +00003587 } else {
3588 if (*p == '.') {
3589 p++;
blueswir1cd390082008-11-16 13:53:32 +00003590 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003591 p++;
bellard34405572004-06-08 00:55:58 +00003592 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003593 typestr++;
3594 break;
bellard34405572004-06-08 00:55:58 +00003595 }
3596 }
bellard13224a82006-07-14 22:03:35 +00003597 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003598 }
aliguori376253e2009-03-05 23:01:23 +00003599 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003600 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003601 /* Check if 'i' is greater than 32-bit */
3602 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3603 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3604 monitor_printf(mon, "integer is for 32-bit values\n");
3605 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003606 } else if (c == 'M') {
3607 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003608 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003609 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003610 }
3611 break;
3612 case '-':
3613 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003614 const char *tmp = p;
3615 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003616 /* option */
ths3b46e622007-09-17 08:09:54 +00003617
bellard9307c4c2004-04-04 12:57:25 +00003618 c = *typestr++;
3619 if (c == '\0')
3620 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003621 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003622 p++;
3623 has_option = 0;
3624 if (*p == '-') {
3625 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003626 if(c != *p) {
3627 if(!is_valid_option(p, typestr)) {
3628
3629 monitor_printf(mon, "%s: unsupported option -%c\n",
3630 cmdname, *p);
3631 goto fail;
3632 } else {
3633 skip_key = 1;
3634 }
bellard9307c4c2004-04-04 12:57:25 +00003635 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003636 if(skip_key) {
3637 p = tmp;
3638 } else {
3639 p++;
3640 has_option = 1;
3641 }
bellard9307c4c2004-04-04 12:57:25 +00003642 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003643 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003644 }
3645 break;
3646 default:
3647 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003648 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003649 goto fail;
3650 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003651 qemu_free(key);
3652 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003653 }
3654 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003655 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003656 p++;
3657 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003658 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3659 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003660 goto fail;
3661 }
3662
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003663 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003664
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003665fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003666 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003667 return NULL;
3668}
3669
Luiz Capitulino8204a912009-11-18 23:05:31 -02003670static void monitor_print_error(Monitor *mon)
3671{
3672 qerror_print(mon->error);
3673 QDECREF(mon->error);
3674 mon->error = NULL;
3675}
3676
Adam Litke940cc302010-01-25 12:18:44 -06003677static int is_async_return(const QObject *data)
3678{
3679 return data && qdict_haskey(qobject_to_qdict(data), "__mon_async");
3680}
3681
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003682static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3683 const QDict *params)
3684{
3685 QObject *data = NULL;
3686
3687 cmd->mhandler.cmd_new(mon, params, &data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003688
Adam Litke940cc302010-01-25 12:18:44 -06003689 if (is_async_return(data)) {
3690 /*
3691 * Asynchronous commands have no initial return data but they can
3692 * generate errors. Data is returned via the async completion handler.
3693 */
3694 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3695 monitor_protocol_emitter(mon, NULL);
3696 }
3697 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003698 /* Monitor Protocol */
3699 monitor_protocol_emitter(mon, data);
3700 } else {
3701 /* User Protocol */
3702 if (data)
3703 cmd->user_print(mon, data);
3704 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003705
3706 qobject_decref(data);
3707}
3708
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003709static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003710{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003711 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003712 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003713
3714 qdict = qdict_new();
3715
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003716 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003717 if (!cmd)
3718 goto out;
3719
3720 qemu_errors_to_mon(mon);
3721
Adam Litke940cc302010-01-25 12:18:44 -06003722 if (monitor_handler_is_async(cmd)) {
3723 user_async_cmd_handler(mon, cmd, qdict);
3724 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003725 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003726 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003727 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003728 }
3729
Luiz Capitulino8204a912009-11-18 23:05:31 -02003730 if (monitor_has_error(mon))
3731 monitor_print_error(mon);
3732
3733 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003734
3735out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003736 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003737}
3738
bellard81d09122004-07-14 17:21:37 +00003739static void cmd_completion(const char *name, const char *list)
3740{
3741 const char *p, *pstart;
3742 char cmd[128];
3743 int len;
3744
3745 p = list;
3746 for(;;) {
3747 pstart = p;
3748 p = strchr(p, '|');
3749 if (!p)
3750 p = pstart + strlen(pstart);
3751 len = p - pstart;
3752 if (len > sizeof(cmd) - 2)
3753 len = sizeof(cmd) - 2;
3754 memcpy(cmd, pstart, len);
3755 cmd[len] = '\0';
3756 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003757 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003758 }
3759 if (*p == '\0')
3760 break;
3761 p++;
3762 }
3763}
3764
3765static void file_completion(const char *input)
3766{
3767 DIR *ffs;
3768 struct dirent *d;
3769 char path[1024];
3770 char file[1024], file_prefix[1024];
3771 int input_path_len;
3772 const char *p;
3773
ths5fafdf22007-09-16 21:08:06 +00003774 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003775 if (!p) {
3776 input_path_len = 0;
3777 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003778 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003779 } else {
3780 input_path_len = p - input + 1;
3781 memcpy(path, input, input_path_len);
3782 if (input_path_len > sizeof(path) - 1)
3783 input_path_len = sizeof(path) - 1;
3784 path[input_path_len] = '\0';
3785 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3786 }
3787#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003788 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3789 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003790#endif
3791 ffs = opendir(path);
3792 if (!ffs)
3793 return;
3794 for(;;) {
3795 struct stat sb;
3796 d = readdir(ffs);
3797 if (!d)
3798 break;
3799 if (strstart(d->d_name, file_prefix, NULL)) {
3800 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003801 if (input_path_len < sizeof(file))
3802 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3803 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003804 /* stat the file to find out if it's a directory.
3805 * In that case add a slash to speed up typing long paths
3806 */
3807 stat(file, &sb);
3808 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003809 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003810 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003811 }
3812 }
3813 closedir(ffs);
3814}
3815
aliguori51de9762009-03-05 23:00:43 +00003816static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003817{
aliguori51de9762009-03-05 23:00:43 +00003818 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003819 const char *input = opaque;
3820
3821 if (input[0] == '\0' ||
3822 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003823 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003824 }
3825}
3826
3827/* NOTE: this parser is an approximate form of the real command parser */
3828static void parse_cmdline(const char *cmdline,
3829 int *pnb_args, char **args)
3830{
3831 const char *p;
3832 int nb_args, ret;
3833 char buf[1024];
3834
3835 p = cmdline;
3836 nb_args = 0;
3837 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003838 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003839 p++;
3840 if (*p == '\0')
3841 break;
3842 if (nb_args >= MAX_ARGS)
3843 break;
3844 ret = get_str(buf, sizeof(buf), &p);
3845 args[nb_args] = qemu_strdup(buf);
3846 nb_args++;
3847 if (ret < 0)
3848 break;
3849 }
3850 *pnb_args = nb_args;
3851}
3852
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003853static const char *next_arg_type(const char *typestr)
3854{
3855 const char *p = strchr(typestr, ':');
3856 return (p != NULL ? ++p : typestr);
3857}
3858
aliguori4c36ba32009-03-05 23:01:37 +00003859static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003860{
3861 const char *cmdname;
3862 char *args[MAX_ARGS];
3863 int nb_args, i, len;
3864 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003865 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003866 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003867
3868 parse_cmdline(cmdline, &nb_args, args);
3869#ifdef DEBUG_COMPLETION
3870 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003871 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003872 }
3873#endif
3874
3875 /* if the line ends with a space, it means we want to complete the
3876 next arg */
3877 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003878 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003879 if (nb_args >= MAX_ARGS)
3880 return;
3881 args[nb_args++] = qemu_strdup("");
3882 }
3883 if (nb_args <= 1) {
3884 /* command completion */
3885 if (nb_args == 0)
3886 cmdname = "";
3887 else
3888 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003889 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003890 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003891 cmd_completion(cmdname, cmd->name);
3892 }
3893 } else {
3894 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003895 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003896 if (compare_cmd(args[0], cmd->name))
3897 goto found;
3898 }
3899 return;
3900 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003901 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003902 for(i = 0; i < nb_args - 2; i++) {
3903 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003904 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003905 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003906 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003907 }
3908 }
3909 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003910 if (*ptype == '-' && ptype[1] != '\0') {
3911 ptype += 2;
3912 }
bellard81d09122004-07-14 17:21:37 +00003913 switch(*ptype) {
3914 case 'F':
3915 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003916 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003917 file_completion(str);
3918 break;
3919 case 'B':
3920 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003921 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003922 bdrv_iterate(block_completion_it, (void *)str);
3923 break;
bellard7fe48482004-10-09 18:08:01 +00003924 case 's':
3925 /* XXX: more generic ? */
3926 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003927 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003928 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3929 cmd_completion(str, cmd->name);
3930 }
bellard64866c32006-05-07 18:03:31 +00003931 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003932 char *sep = strrchr(str, '-');
3933 if (sep)
3934 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003935 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003936 for(key = key_defs; key->name != NULL; key++) {
3937 cmd_completion(str, key->name);
3938 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003939 } else if (!strcmp(cmd->name, "help|?")) {
3940 readline_set_completion_index(cur_mon->rs, strlen(str));
3941 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3942 cmd_completion(str, cmd->name);
3943 }
bellard7fe48482004-10-09 18:08:01 +00003944 }
3945 break;
bellard81d09122004-07-14 17:21:37 +00003946 default:
3947 break;
3948 }
3949 }
3950 for(i = 0; i < nb_args; i++)
3951 qemu_free(args[i]);
3952}
3953
aliguori731b0362009-03-05 23:01:42 +00003954static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003955{
aliguori731b0362009-03-05 23:01:42 +00003956 Monitor *mon = opaque;
3957
Jan Kiszkac62313b2009-12-04 14:05:29 +01003958 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003959}
3960
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003961typedef struct CmdArgs {
3962 QString *name;
3963 int type;
3964 int flag;
3965 int optional;
3966} CmdArgs;
3967
3968static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
3969{
3970 if (!cmd_args->optional) {
3971 qemu_error_new(QERR_MISSING_PARAMETER, name);
3972 return -1;
3973 }
3974
3975 if (cmd_args->type == '-') {
3976 /* handlers expect a value, they need to be changed */
3977 qdict_put(args, name, qint_from_int(0));
3978 }
3979
3980 return 0;
3981}
3982
3983static int check_arg(const CmdArgs *cmd_args, QDict *args)
3984{
3985 QObject *value;
3986 const char *name;
3987
3988 name = qstring_get_str(cmd_args->name);
3989
3990 if (!args) {
3991 return check_opt(cmd_args, name, args);
3992 }
3993
3994 value = qdict_get(args, name);
3995 if (!value) {
3996 return check_opt(cmd_args, name, args);
3997 }
3998
3999 switch (cmd_args->type) {
4000 case 'F':
4001 case 'B':
4002 case 's':
4003 if (qobject_type(value) != QTYPE_QSTRING) {
4004 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "string");
4005 return -1;
4006 }
4007 break;
4008 case '/': {
4009 int i;
4010 const char *keys[] = { "count", "format", "size", NULL };
4011
4012 for (i = 0; keys[i]; i++) {
4013 QObject *obj = qdict_get(args, keys[i]);
4014 if (!obj) {
4015 qemu_error_new(QERR_MISSING_PARAMETER, name);
4016 return -1;
4017 }
4018 if (qobject_type(obj) != QTYPE_QINT) {
4019 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4020 return -1;
4021 }
4022 }
4023 break;
4024 }
4025 case 'i':
4026 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004027 case 'M':
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004028 if (qobject_type(value) != QTYPE_QINT) {
4029 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4030 return -1;
4031 }
4032 break;
4033 case '-':
4034 if (qobject_type(value) != QTYPE_QINT &&
4035 qobject_type(value) != QTYPE_QBOOL) {
4036 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "bool");
4037 return -1;
4038 }
4039 if (qobject_type(value) == QTYPE_QBOOL) {
4040 /* handlers expect a QInt, they need to be changed */
4041 qdict_put(args, name,
4042 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
4043 }
4044 break;
4045 default:
4046 /* impossible */
4047 abort();
4048 }
4049
4050 return 0;
4051}
4052
4053static void cmd_args_init(CmdArgs *cmd_args)
4054{
4055 cmd_args->name = qstring_new();
4056 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
4057}
4058
4059/*
4060 * This is not trivial, we have to parse Monitor command's argument
4061 * type syntax to be able to check the arguments provided by clients.
4062 *
4063 * In the near future we will be using an array for that and will be
4064 * able to drop all this parsing...
4065 */
4066static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
4067{
4068 int err;
4069 const char *p;
4070 CmdArgs cmd_args;
4071
Blue Swirldd5121b2009-12-04 20:52:02 +00004072 if (cmd->args_type == NULL) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004073 return (qdict_size(args) == 0 ? 0 : -1);
4074 }
4075
4076 err = 0;
4077 cmd_args_init(&cmd_args);
4078
4079 for (p = cmd->args_type;; p++) {
4080 if (*p == ':') {
4081 cmd_args.type = *++p;
4082 p++;
4083 if (cmd_args.type == '-') {
4084 cmd_args.flag = *p++;
4085 cmd_args.optional = 1;
4086 } else if (*p == '?') {
4087 cmd_args.optional = 1;
4088 p++;
4089 }
4090
4091 assert(*p == ',' || *p == '\0');
4092 err = check_arg(&cmd_args, args);
4093
4094 QDECREF(cmd_args.name);
4095 cmd_args_init(&cmd_args);
4096
4097 if (err < 0) {
4098 break;
4099 }
4100 } else {
4101 qstring_append_chr(cmd_args.name, *p);
4102 }
4103
4104 if (*p == '\0') {
4105 break;
4106 }
4107 }
4108
4109 QDECREF(cmd_args.name);
4110 return err;
4111}
4112
4113static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4114{
4115 int err;
4116 QObject *obj;
4117 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004118 const mon_cmd_t *cmd;
4119 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004120 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004121
4122 args = NULL;
4123 qemu_errors_to_mon(mon);
4124
4125 obj = json_parser_parse(tokens, NULL);
4126 if (!obj) {
4127 // FIXME: should be triggered in json_parser_parse()
4128 qemu_error_new(QERR_JSON_PARSING);
4129 goto err_out;
4130 } else if (qobject_type(obj) != QTYPE_QDICT) {
4131 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "object");
4132 qobject_decref(obj);
4133 goto err_out;
4134 }
4135
4136 input = qobject_to_qdict(obj);
4137
4138 mon->mc->id = qdict_get(input, "id");
4139 qobject_incref(mon->mc->id);
4140
4141 obj = qdict_get(input, "execute");
4142 if (!obj) {
4143 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4144 goto err_input;
4145 } else if (qobject_type(obj) != QTYPE_QSTRING) {
4146 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "string");
4147 goto err_input;
4148 }
4149
4150 cmd_name = qstring_get_str(qobject_to_qstring(obj));
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004151
4152 /*
4153 * XXX: We need this special case until we get info handlers
4154 * converted into 'query-' commands
4155 */
4156 if (compare_cmd(cmd_name, "info")) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004157 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4158 goto err_input;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004159 } else if (strstart(cmd_name, "query-", &info_item)) {
4160 cmd = monitor_find_command("info");
4161 qdict_put_obj(input, "arguments",
4162 qobject_from_jsonf("{ 'item': %s }", info_item));
4163 } else {
4164 cmd = monitor_find_command(cmd_name);
Luiz Capitulino43e713c2009-12-18 13:24:59 -02004165 if (!cmd || !monitor_handler_ported(cmd)) {
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004166 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4167 goto err_input;
4168 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004169 }
4170
4171 obj = qdict_get(input, "arguments");
4172 if (!obj) {
4173 args = qdict_new();
4174 } else {
4175 args = qobject_to_qdict(obj);
4176 QINCREF(args);
4177 }
4178
4179 QDECREF(input);
4180
4181 err = monitor_check_qmp_args(cmd, args);
4182 if (err < 0) {
4183 goto err_out;
4184 }
4185
Adam Litke940cc302010-01-25 12:18:44 -06004186 if (monitor_handler_is_async(cmd)) {
4187 qmp_async_cmd_handler(mon, cmd, args);
4188 } else {
4189 monitor_call_handler(mon, cmd, args);
4190 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004191 goto out;
4192
4193err_input:
4194 QDECREF(input);
4195err_out:
4196 monitor_protocol_emitter(mon, NULL);
4197out:
4198 QDECREF(args);
4199 qemu_errors_to_previous();
4200}
4201
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004202/**
4203 * monitor_control_read(): Read and handle QMP input
4204 */
4205static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4206{
4207 Monitor *old_mon = cur_mon;
4208
4209 cur_mon = opaque;
4210
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004211 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004212
4213 cur_mon = old_mon;
4214}
4215
aliguori731b0362009-03-05 23:01:42 +00004216static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004217{
aliguori731b0362009-03-05 23:01:42 +00004218 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004219 int i;
aliguori376253e2009-03-05 23:01:23 +00004220
aliguori731b0362009-03-05 23:01:42 +00004221 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004222
aliguoricde76ee2009-03-05 23:01:51 +00004223 if (cur_mon->rs) {
4224 for (i = 0; i < size; i++)
4225 readline_handle_byte(cur_mon->rs, buf[i]);
4226 } else {
4227 if (size == 0 || buf[size - 1] != 0)
4228 monitor_printf(cur_mon, "corrupted command\n");
4229 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004230 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004231 }
aliguori731b0362009-03-05 23:01:42 +00004232
4233 cur_mon = old_mon;
4234}
aliguorid8f44602008-10-06 13:52:44 +00004235
aliguori376253e2009-03-05 23:01:23 +00004236static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004237{
aliguori731b0362009-03-05 23:01:42 +00004238 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004239 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004240 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004241}
4242
aliguoricde76ee2009-03-05 23:01:51 +00004243int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004244{
aliguoricde76ee2009-03-05 23:01:51 +00004245 if (!mon->rs)
4246 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004247 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004248 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004249}
4250
aliguori376253e2009-03-05 23:01:23 +00004251void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004252{
aliguoricde76ee2009-03-05 23:01:51 +00004253 if (!mon->rs)
4254 return;
aliguori731b0362009-03-05 23:01:42 +00004255 if (--mon->suspend_cnt == 0)
4256 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004257}
4258
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004259/**
4260 * monitor_control_event(): Print QMP gretting
4261 */
4262static void monitor_control_event(void *opaque, int event)
4263{
4264 if (event == CHR_EVENT_OPENED) {
4265 QObject *data;
4266 Monitor *mon = opaque;
4267
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004268 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
4269
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004270 data = qobject_from_jsonf("{ 'QMP': { 'capabilities': [] } }");
4271 assert(data != NULL);
4272
4273 monitor_json_emitter(mon, data);
4274 qobject_decref(data);
4275 }
4276}
4277
aliguori731b0362009-03-05 23:01:42 +00004278static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004279{
aliguori376253e2009-03-05 23:01:23 +00004280 Monitor *mon = opaque;
4281
aliguori2724b182009-03-05 23:01:47 +00004282 switch (event) {
4283 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004284 mon->mux_out = 0;
4285 if (mon->reset_seen) {
4286 readline_restart(mon->rs);
4287 monitor_resume(mon);
4288 monitor_flush(mon);
4289 } else {
4290 mon->suspend_cnt = 0;
4291 }
aliguori2724b182009-03-05 23:01:47 +00004292 break;
ths86e94de2007-01-05 22:01:59 +00004293
aliguori2724b182009-03-05 23:01:47 +00004294 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004295 if (mon->reset_seen) {
4296 if (mon->suspend_cnt == 0) {
4297 monitor_printf(mon, "\n");
4298 }
4299 monitor_flush(mon);
4300 monitor_suspend(mon);
4301 } else {
4302 mon->suspend_cnt++;
4303 }
4304 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004305 break;
4306
Amit Shahb6b8df52009-10-07 18:31:16 +05304307 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004308 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4309 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004310 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004311 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004312 }
4313 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004314 break;
4315 }
ths86e94de2007-01-05 22:01:59 +00004316}
4317
aliguori76655d62009-03-06 20:27:37 +00004318
4319/*
4320 * Local variables:
4321 * c-indent-level: 4
4322 * c-basic-offset: 4
4323 * tab-width: 8
4324 * End:
4325 */
4326
aliguori731b0362009-03-05 23:01:42 +00004327void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004328{
aliguori731b0362009-03-05 23:01:42 +00004329 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004330 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004331
4332 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004333 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004334 is_first_init = 0;
4335 }
aliguori87127162009-03-05 23:01:29 +00004336
4337 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004338
aliguori87127162009-03-05 23:01:29 +00004339 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004340 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004341 if (flags & MONITOR_USE_READLINE) {
4342 mon->rs = readline_init(mon, monitor_find_completion);
4343 monitor_read_command(mon, 0);
4344 }
aliguori87127162009-03-05 23:01:29 +00004345
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004346 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004347 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004348 /* Control mode requires special handlers */
4349 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4350 monitor_control_event, mon);
4351 } else {
4352 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4353 monitor_event, mon);
4354 }
aliguori87127162009-03-05 23:01:29 +00004355
Blue Swirl72cf2d42009-09-12 07:36:22 +00004356 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00004357 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00004358 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004359}
4360
aliguori376253e2009-03-05 23:01:23 +00004361static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004362{
aliguoribb5fc202009-03-05 23:01:15 +00004363 BlockDriverState *bs = opaque;
4364 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004365
aliguoribb5fc202009-03-05 23:01:15 +00004366 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004367 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004368 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004369 }
aliguori731b0362009-03-05 23:01:42 +00004370 if (mon->password_completion_cb)
4371 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004372
aliguori731b0362009-03-05 23:01:42 +00004373 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004374}
aliguoric0f4ce72009-03-05 23:01:01 +00004375
aliguori376253e2009-03-05 23:01:23 +00004376void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00004377 BlockDriverCompletionFunc *completion_cb,
4378 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004379{
aliguoricde76ee2009-03-05 23:01:51 +00004380 int err;
4381
aliguoribb5fc202009-03-05 23:01:15 +00004382 if (!bdrv_key_required(bs)) {
4383 if (completion_cb)
4384 completion_cb(opaque, 0);
4385 return;
4386 }
aliguoric0f4ce72009-03-05 23:01:01 +00004387
Luiz Capitulino94171e12009-12-07 21:37:00 +01004388 if (monitor_ctrl_mode(mon)) {
4389 qemu_error_new(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
4390 return;
4391 }
4392
aliguori376253e2009-03-05 23:01:23 +00004393 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4394 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004395
aliguori731b0362009-03-05 23:01:42 +00004396 mon->password_completion_cb = completion_cb;
4397 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004398
aliguoricde76ee2009-03-05 23:01:51 +00004399 err = monitor_read_password(mon, bdrv_password_cb, bs);
4400
4401 if (err && completion_cb)
4402 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00004403}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004404
4405typedef struct QemuErrorSink QemuErrorSink;
4406struct QemuErrorSink {
4407 enum {
4408 ERR_SINK_FILE,
4409 ERR_SINK_MONITOR,
4410 } dest;
4411 union {
4412 FILE *fp;
4413 Monitor *mon;
4414 };
4415 QemuErrorSink *previous;
4416};
4417
Blue Swirl528e93a2009-08-31 15:14:40 +00004418static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004419
4420void qemu_errors_to_file(FILE *fp)
4421{
4422 QemuErrorSink *sink;
4423
4424 sink = qemu_mallocz(sizeof(*sink));
4425 sink->dest = ERR_SINK_FILE;
4426 sink->fp = fp;
4427 sink->previous = qemu_error_sink;
4428 qemu_error_sink = sink;
4429}
4430
4431void qemu_errors_to_mon(Monitor *mon)
4432{
4433 QemuErrorSink *sink;
4434
4435 sink = qemu_mallocz(sizeof(*sink));
4436 sink->dest = ERR_SINK_MONITOR;
4437 sink->mon = mon;
4438 sink->previous = qemu_error_sink;
4439 qemu_error_sink = sink;
4440}
4441
4442void qemu_errors_to_previous(void)
4443{
4444 QemuErrorSink *sink;
4445
4446 assert(qemu_error_sink != NULL);
4447 sink = qemu_error_sink;
4448 qemu_error_sink = sink->previous;
4449 qemu_free(sink);
4450}
4451
4452void qemu_error(const char *fmt, ...)
4453{
4454 va_list args;
4455
4456 assert(qemu_error_sink != NULL);
4457 switch (qemu_error_sink->dest) {
4458 case ERR_SINK_FILE:
4459 va_start(args, fmt);
4460 vfprintf(qemu_error_sink->fp, fmt, args);
4461 va_end(args);
4462 break;
4463 case ERR_SINK_MONITOR:
4464 va_start(args, fmt);
4465 monitor_vprintf(qemu_error_sink->mon, fmt, args);
4466 va_end(args);
4467 break;
4468 }
4469}
Luiz Capitulino8204a912009-11-18 23:05:31 -02004470
4471void qemu_error_internal(const char *file, int linenr, const char *func,
4472 const char *fmt, ...)
4473{
4474 va_list va;
4475 QError *qerror;
4476
4477 assert(qemu_error_sink != NULL);
4478
4479 va_start(va, fmt);
4480 qerror = qerror_from_info(file, linenr, func, fmt, &va);
4481 va_end(va);
4482
4483 switch (qemu_error_sink->dest) {
4484 case ERR_SINK_FILE:
4485 qerror_print(qerror);
4486 QDECREF(qerror);
4487 break;
4488 case ERR_SINK_MONITOR:
4489 assert(qemu_error_sink->mon->error == NULL);
4490 qemu_error_sink->mon->error = qerror;
4491 break;
4492 }
4493}