blob: fb7c57231a58424f5c5539e4e486323d4f448ce1 [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
Adam Litke625a5be2010-01-26 14:17:35 -06002155static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002156{
Adam Litke625a5be2010-01-26 14:17:35 -06002157 Monitor *mon = opaque;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002158
Adam Litke625a5be2010-01-26 14:17:35 -06002159 if (strcmp(key, "actual"))
2160 monitor_printf(mon, ",%s=%" PRId64, key,
2161 qint_get_int(qobject_to_qint(obj)));
aliguoridf751fa2008-12-04 20:19:35 +00002162}
2163
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002164static void monitor_print_balloon(Monitor *mon, const QObject *data)
2165{
Luiz Capitulino7f179672009-12-10 17:15:55 -02002166 QDict *qdict;
2167
2168 qdict = qobject_to_qdict(data);
Adam Litke625a5be2010-01-26 14:17:35 -06002169 if (!qdict_haskey(qdict, "actual"))
2170 return;
Luiz Capitulino7f179672009-12-10 17:15:55 -02002171
Adam Litke625a5be2010-01-26 14:17:35 -06002172 monitor_printf(mon, "balloon: actual=%" PRId64,
2173 qdict_get_int(qdict, "actual") >> 20);
2174 qdict_iter(qdict, print_balloon_stat, mon);
2175 monitor_printf(mon, "\n");
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002176}
2177
2178/**
2179 * do_info_balloon(): Balloon information
Luiz Capitulino7f179672009-12-10 17:15:55 -02002180 *
Adam Litke625a5be2010-01-26 14:17:35 -06002181 * Make an asynchronous request for balloon info. When the request completes
2182 * a QDict will be returned according to the following specification:
Luiz Capitulino7f179672009-12-10 17:15:55 -02002183 *
Adam Litke625a5be2010-01-26 14:17:35 -06002184 * - "actual": current balloon value in bytes
2185 * The following fields may or may not be present:
2186 * - "mem_swapped_in": Amount of memory swapped in (bytes)
2187 * - "mem_swapped_out": Amount of memory swapped out (bytes)
2188 * - "major_page_faults": Number of major faults
2189 * - "minor_page_faults": Number of minor faults
2190 * - "free_mem": Total amount of free and unused memory (bytes)
2191 * - "total_mem": Total amount of available memory (bytes)
Luiz Capitulino7f179672009-12-10 17:15:55 -02002192 *
2193 * Example:
2194 *
Adam Litke625a5be2010-01-26 14:17:35 -06002195 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
2196 * "major_page_faults": 142, "minor_page_faults": 239245,
2197 * "free_mem": 1014185984, "total_mem": 1044668416 }
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002198 */
Adam Litke625a5be2010-01-26 14:17:35 -06002199static int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +00002200{
Adam Litke625a5be2010-01-26 14:17:35 -06002201 int ret;
aliguoridf751fa2008-12-04 20:19:35 +00002202
Adam Litke625a5be2010-01-26 14:17:35 -06002203 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2204 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2205 return -1;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002206 }
Adam Litke625a5be2010-01-26 14:17:35 -06002207
2208 ret = qemu_balloon_status(cb, opaque);
2209 if (!ret) {
2210 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2211 return -1;
2212 }
2213
2214 return 0;
2215}
2216
2217/**
2218 * do_balloon(): Request VM to change its memory allocation
2219 */
2220static int do_balloon(Monitor *mon, const QDict *params,
2221 MonitorCompletion cb, void *opaque)
2222{
2223 int ret;
2224
2225 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2226 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2227 return -1;
2228 }
2229
2230 ret = qemu_balloon(qdict_get_int(params, "value"), cb, opaque);
2231 if (ret == 0) {
2232 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2233 return -1;
2234 }
2235
2236 return 0;
aliguoridf751fa2008-12-04 20:19:35 +00002237}
2238
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002239static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002240{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002241 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002242
aliguori76655d62009-03-06 20:27:37 +00002243 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002244 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002245 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002246 return acl;
2247}
aliguori76655d62009-03-06 20:27:37 +00002248
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002249static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002250{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002251 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002252 qemu_acl *acl = find_acl(mon, aclname);
2253 qemu_acl_entry *entry;
2254 int i = 0;
2255
2256 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002257 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002258 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002259 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002260 i++;
2261 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002262 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002263 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002264 }
2265}
2266
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002267static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002268{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002269 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002270 qemu_acl *acl = find_acl(mon, aclname);
2271
2272 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002273 qemu_acl_reset(acl);
2274 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002275 }
2276}
aliguori76655d62009-03-06 20:27:37 +00002277
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002278static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002279{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002280 const char *aclname = qdict_get_str(qdict, "aclname");
2281 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002282 qemu_acl *acl = find_acl(mon, aclname);
2283
2284 if (acl) {
2285 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002286 acl->defaultDeny = 0;
2287 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002288 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002289 acl->defaultDeny = 1;
2290 monitor_printf(mon, "acl: policy set to 'deny'\n");
2291 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002292 monitor_printf(mon, "acl: unknown policy '%s', "
2293 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002294 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002295 }
2296}
aliguori76655d62009-03-06 20:27:37 +00002297
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002298static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002299{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002300 const char *aclname = qdict_get_str(qdict, "aclname");
2301 const char *match = qdict_get_str(qdict, "match");
2302 const char *policy = qdict_get_str(qdict, "policy");
2303 int has_index = qdict_haskey(qdict, "index");
2304 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002305 qemu_acl *acl = find_acl(mon, aclname);
2306 int deny, ret;
2307
2308 if (acl) {
2309 if (strcmp(policy, "allow") == 0) {
2310 deny = 0;
2311 } else if (strcmp(policy, "deny") == 0) {
2312 deny = 1;
2313 } else {
2314 monitor_printf(mon, "acl: unknown policy '%s', "
2315 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002316 return;
2317 }
aliguori28a76be2009-03-06 20:27:40 +00002318 if (has_index)
2319 ret = qemu_acl_insert(acl, deny, match, index);
2320 else
2321 ret = qemu_acl_append(acl, deny, match);
2322 if (ret < 0)
2323 monitor_printf(mon, "acl: unable to add acl entry\n");
2324 else
2325 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002326 }
2327}
aliguori76655d62009-03-06 20:27:37 +00002328
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002329static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002330{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002331 const char *aclname = qdict_get_str(qdict, "aclname");
2332 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002333 qemu_acl *acl = find_acl(mon, aclname);
2334 int ret;
aliguori76655d62009-03-06 20:27:37 +00002335
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002336 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002337 ret = qemu_acl_remove(acl, match);
2338 if (ret < 0)
2339 monitor_printf(mon, "acl: no matching acl entry\n");
2340 else
2341 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002342 }
2343}
2344
Huang Ying79c4f6b2009-06-23 10:05:14 +08002345#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002346static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002347{
2348 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002349 int cpu_index = qdict_get_int(qdict, "cpu_index");
2350 int bank = qdict_get_int(qdict, "bank");
2351 uint64_t status = qdict_get_int(qdict, "status");
2352 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2353 uint64_t addr = qdict_get_int(qdict, "addr");
2354 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002355
2356 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2357 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2358 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2359 break;
2360 }
2361}
2362#endif
2363
Luiz Capitulinof0d60002009-10-16 12:23:50 -03002364static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002365{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002366 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002367 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002368 int fd;
2369
2370 fd = qemu_chr_get_msgfd(mon->chr);
2371 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002372 qemu_error_new(QERR_FD_NOT_SUPPLIED);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002373 return;
2374 }
2375
2376 if (qemu_isdigit(fdname[0])) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002377 qemu_error_new(QERR_INVALID_PARAMETER, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002378 return;
2379 }
2380
2381 fd = dup(fd);
2382 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002383 if (errno == EMFILE)
2384 qemu_error_new(QERR_TOO_MANY_FILES);
2385 else
2386 qemu_error_new(QERR_UNDEFINED_ERROR);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002387 return;
2388 }
2389
Blue Swirl72cf2d42009-09-12 07:36:22 +00002390 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002391 if (strcmp(monfd->name, fdname) != 0) {
2392 continue;
2393 }
2394
2395 close(monfd->fd);
2396 monfd->fd = fd;
2397 return;
2398 }
2399
Anthony Liguoric227f092009-10-01 16:12:16 -05002400 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002401 monfd->name = qemu_strdup(fdname);
2402 monfd->fd = fd;
2403
Blue Swirl72cf2d42009-09-12 07:36:22 +00002404 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002405}
2406
Luiz Capitulino18f3a512009-10-16 12:23:51 -03002407static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002408{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002409 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002410 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002411
Blue Swirl72cf2d42009-09-12 07:36:22 +00002412 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002413 if (strcmp(monfd->name, fdname) != 0) {
2414 continue;
2415 }
2416
Blue Swirl72cf2d42009-09-12 07:36:22 +00002417 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002418 close(monfd->fd);
2419 qemu_free(monfd->name);
2420 qemu_free(monfd);
2421 return;
2422 }
2423
Markus Armbruster063c1a02009-12-07 21:37:11 +01002424 qemu_error_new(QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002425}
2426
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002427static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002428{
2429 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002430 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002431
2432 vm_stop(0);
2433
Juan Quintela05f24012009-08-20 19:42:22 +02002434 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002435 vm_start();
2436}
2437
Mark McLoughlin7768e042009-07-22 09:11:41 +01002438int monitor_get_fd(Monitor *mon, const char *fdname)
2439{
Anthony Liguoric227f092009-10-01 16:12:16 -05002440 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002441
Blue Swirl72cf2d42009-09-12 07:36:22 +00002442 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002443 int fd;
2444
2445 if (strcmp(monfd->name, fdname) != 0) {
2446 continue;
2447 }
2448
2449 fd = monfd->fd;
2450
2451 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002452 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002453 qemu_free(monfd->name);
2454 qemu_free(monfd);
2455
2456 return fd;
2457 }
2458
2459 return -1;
2460}
2461
Anthony Liguoric227f092009-10-01 16:12:16 -05002462static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002463#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002464 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002465};
2466
Blue Swirl23130862009-06-06 08:22:04 +00002467/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002468static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002469 {
2470 .name = "version",
2471 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002472 .params = "",
2473 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002474 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002475 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002476 },
2477 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002478 .name = "commands",
2479 .args_type = "",
2480 .params = "",
2481 .help = "list QMP available commands",
2482 .user_print = monitor_user_noop,
2483 .mhandler.info_new = do_info_commands,
2484 },
2485 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002486 .name = "network",
2487 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002488 .params = "",
2489 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002490 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002491 },
2492 {
2493 .name = "chardev",
2494 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002495 .params = "",
2496 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002497 .user_print = qemu_chr_info_print,
2498 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002499 },
2500 {
2501 .name = "block",
2502 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002503 .params = "",
2504 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002505 .user_print = bdrv_info_print,
2506 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002507 },
2508 {
2509 .name = "blockstats",
2510 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002511 .params = "",
2512 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002513 .user_print = bdrv_stats_print,
2514 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002515 },
2516 {
2517 .name = "registers",
2518 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002519 .params = "",
2520 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002521 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002522 },
2523 {
2524 .name = "cpus",
2525 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002526 .params = "",
2527 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002528 .user_print = monitor_print_cpus,
2529 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002530 },
2531 {
2532 .name = "history",
2533 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002534 .params = "",
2535 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002536 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002537 },
2538 {
2539 .name = "irq",
2540 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002541 .params = "",
2542 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002543 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002544 },
2545 {
2546 .name = "pic",
2547 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002548 .params = "",
2549 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002550 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002551 },
2552 {
2553 .name = "pci",
2554 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002555 .params = "",
2556 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002557 .user_print = do_pci_info_print,
2558 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002559 },
aurel327c664e22009-03-03 06:12:22 +00002560#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002561 {
2562 .name = "tlb",
2563 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002564 .params = "",
2565 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002566 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002567 },
aurel327c664e22009-03-03 06:12:22 +00002568#endif
2569#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002570 {
2571 .name = "mem",
2572 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002573 .params = "",
2574 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002575 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002576 },
2577 {
2578 .name = "hpet",
2579 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 .params = "",
2581 .help = "show state of HPET",
Luiz Capitulino14f07202009-12-10 17:16:02 -02002582 .user_print = do_info_hpet_print,
2583 .mhandler.info_new = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002584 },
bellardb86bda52004-09-18 19:32:46 +00002585#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002586 {
2587 .name = "jit",
2588 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002589 .params = "",
2590 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002591 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002592 },
2593 {
2594 .name = "kvm",
2595 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002596 .params = "",
2597 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002598 .user_print = do_info_kvm_print,
2599 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002600 },
2601 {
2602 .name = "numa",
2603 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002604 .params = "",
2605 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002606 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002607 },
2608 {
2609 .name = "usb",
2610 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002611 .params = "",
2612 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002613 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002614 },
2615 {
2616 .name = "usbhost",
2617 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002618 .params = "",
2619 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002620 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002621 },
2622 {
2623 .name = "profile",
2624 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002625 .params = "",
2626 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002627 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002628 },
2629 {
2630 .name = "capture",
2631 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002632 .params = "",
2633 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002634 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002635 },
2636 {
2637 .name = "snapshots",
2638 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002639 .params = "",
2640 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002641 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002642 },
2643 {
2644 .name = "status",
2645 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002646 .params = "",
2647 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002648 .user_print = do_info_status_print,
2649 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002650 },
2651 {
2652 .name = "pcmcia",
2653 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002654 .params = "",
2655 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002656 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002657 },
2658 {
2659 .name = "mice",
2660 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002661 .params = "",
2662 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002663 .user_print = do_info_mice_print,
2664 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002665 },
2666 {
2667 .name = "vnc",
2668 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002669 .params = "",
2670 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002671 .user_print = do_info_vnc_print,
2672 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002673 },
2674 {
2675 .name = "name",
2676 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002677 .params = "",
2678 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002679 .user_print = do_info_name_print,
2680 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002681 },
2682 {
2683 .name = "uuid",
2684 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002685 .params = "",
2686 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002687 .user_print = do_info_uuid_print,
2688 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002689 },
j_mayer76a66252007-03-07 08:32:30 +00002690#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002691 {
2692 .name = "cpustats",
2693 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002694 .params = "",
2695 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002696 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002697 },
j_mayer76a66252007-03-07 08:32:30 +00002698#endif
blueswir131a60e22007-10-26 18:42:59 +00002699#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002700 {
2701 .name = "usernet",
2702 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002703 .params = "",
2704 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002705 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002706 },
blueswir131a60e22007-10-26 18:42:59 +00002707#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002708 {
2709 .name = "migrate",
2710 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002711 .params = "",
2712 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002713 .user_print = do_info_migrate_print,
2714 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002715 },
2716 {
2717 .name = "balloon",
2718 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002719 .params = "",
2720 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002721 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002722 .mhandler.info_async = do_info_balloon,
2723 .async = 1,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002724 },
2725 {
2726 .name = "qtree",
2727 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002728 .params = "",
2729 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002730 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002731 },
2732 {
2733 .name = "qdm",
2734 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002735 .params = "",
2736 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002737 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002738 },
2739 {
2740 .name = "roms",
2741 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002742 .params = "",
2743 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002744 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002745 },
2746 {
2747 .name = NULL,
2748 },
bellard9dc39cb2004-03-14 21:38:27 +00002749};
2750
bellard9307c4c2004-04-04 12:57:25 +00002751/*******************************************************************/
2752
2753static const char *pch;
2754static jmp_buf expr_env;
2755
bellard92a31b12005-02-10 22:00:52 +00002756#define MD_TLONG 0
2757#define MD_I32 1
2758
bellard9307c4c2004-04-04 12:57:25 +00002759typedef struct MonitorDef {
2760 const char *name;
2761 int offset;
blueswir18662d652008-10-02 18:32:44 +00002762 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002763 int type;
bellard9307c4c2004-04-04 12:57:25 +00002764} MonitorDef;
2765
bellard57206fd2004-04-25 18:54:52 +00002766#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002767static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002768{
bellard6a00d602005-11-21 23:25:50 +00002769 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002770 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002771}
2772#endif
2773
bellarda541f292004-04-12 20:39:29 +00002774#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002775static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002776{
bellard6a00d602005-11-21 23:25:50 +00002777 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002778 unsigned int u;
2779 int i;
2780
2781 u = 0;
2782 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002783 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002784
2785 return u;
2786}
2787
blueswir18662d652008-10-02 18:32:44 +00002788static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002789{
bellard6a00d602005-11-21 23:25:50 +00002790 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002791 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002792}
2793
blueswir18662d652008-10-02 18:32:44 +00002794static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002795{
bellard6a00d602005-11-21 23:25:50 +00002796 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002797 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002798}
bellard9fddaa02004-05-21 12:59:32 +00002799
blueswir18662d652008-10-02 18:32:44 +00002800static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002801{
bellard6a00d602005-11-21 23:25:50 +00002802 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002803 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002804}
2805
blueswir18662d652008-10-02 18:32:44 +00002806static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002807{
bellard6a00d602005-11-21 23:25:50 +00002808 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002809 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002810}
2811
blueswir18662d652008-10-02 18:32:44 +00002812static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002813{
bellard6a00d602005-11-21 23:25:50 +00002814 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002815 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002816}
bellarda541f292004-04-12 20:39:29 +00002817#endif
2818
bellarde95c8d52004-09-30 22:22:08 +00002819#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002820#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002821static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002822{
bellard6a00d602005-11-21 23:25:50 +00002823 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002824 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002825}
bellard7b936c02005-10-30 17:05:13 +00002826#endif
bellarde95c8d52004-09-30 22:22:08 +00002827
blueswir18662d652008-10-02 18:32:44 +00002828static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002829{
bellard6a00d602005-11-21 23:25:50 +00002830 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002831 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002832}
2833#endif
2834
blueswir18662d652008-10-02 18:32:44 +00002835static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002836#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002837
2838#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002839 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002840 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002841 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002842
bellard9307c4c2004-04-04 12:57:25 +00002843 { "eax", offsetof(CPUState, regs[0]) },
2844 { "ecx", offsetof(CPUState, regs[1]) },
2845 { "edx", offsetof(CPUState, regs[2]) },
2846 { "ebx", offsetof(CPUState, regs[3]) },
2847 { "esp|sp", offsetof(CPUState, regs[4]) },
2848 { "ebp|fp", offsetof(CPUState, regs[5]) },
2849 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002850 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002851#ifdef TARGET_X86_64
2852 { "r8", offsetof(CPUState, regs[8]) },
2853 { "r9", offsetof(CPUState, regs[9]) },
2854 { "r10", offsetof(CPUState, regs[10]) },
2855 { "r11", offsetof(CPUState, regs[11]) },
2856 { "r12", offsetof(CPUState, regs[12]) },
2857 { "r13", offsetof(CPUState, regs[13]) },
2858 { "r14", offsetof(CPUState, regs[14]) },
2859 { "r15", offsetof(CPUState, regs[15]) },
2860#endif
bellard9307c4c2004-04-04 12:57:25 +00002861 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002862 { "eip", offsetof(CPUState, eip) },
2863 SEG("cs", R_CS)
2864 SEG("ds", R_DS)
2865 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002866 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002867 SEG("fs", R_FS)
2868 SEG("gs", R_GS)
2869 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002870#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002871 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002872 { "r0", offsetof(CPUState, gpr[0]) },
2873 { "r1", offsetof(CPUState, gpr[1]) },
2874 { "r2", offsetof(CPUState, gpr[2]) },
2875 { "r3", offsetof(CPUState, gpr[3]) },
2876 { "r4", offsetof(CPUState, gpr[4]) },
2877 { "r5", offsetof(CPUState, gpr[5]) },
2878 { "r6", offsetof(CPUState, gpr[6]) },
2879 { "r7", offsetof(CPUState, gpr[7]) },
2880 { "r8", offsetof(CPUState, gpr[8]) },
2881 { "r9", offsetof(CPUState, gpr[9]) },
2882 { "r10", offsetof(CPUState, gpr[10]) },
2883 { "r11", offsetof(CPUState, gpr[11]) },
2884 { "r12", offsetof(CPUState, gpr[12]) },
2885 { "r13", offsetof(CPUState, gpr[13]) },
2886 { "r14", offsetof(CPUState, gpr[14]) },
2887 { "r15", offsetof(CPUState, gpr[15]) },
2888 { "r16", offsetof(CPUState, gpr[16]) },
2889 { "r17", offsetof(CPUState, gpr[17]) },
2890 { "r18", offsetof(CPUState, gpr[18]) },
2891 { "r19", offsetof(CPUState, gpr[19]) },
2892 { "r20", offsetof(CPUState, gpr[20]) },
2893 { "r21", offsetof(CPUState, gpr[21]) },
2894 { "r22", offsetof(CPUState, gpr[22]) },
2895 { "r23", offsetof(CPUState, gpr[23]) },
2896 { "r24", offsetof(CPUState, gpr[24]) },
2897 { "r25", offsetof(CPUState, gpr[25]) },
2898 { "r26", offsetof(CPUState, gpr[26]) },
2899 { "r27", offsetof(CPUState, gpr[27]) },
2900 { "r28", offsetof(CPUState, gpr[28]) },
2901 { "r29", offsetof(CPUState, gpr[29]) },
2902 { "r30", offsetof(CPUState, gpr[30]) },
2903 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002904 /* Floating point registers */
2905 { "f0", offsetof(CPUState, fpr[0]) },
2906 { "f1", offsetof(CPUState, fpr[1]) },
2907 { "f2", offsetof(CPUState, fpr[2]) },
2908 { "f3", offsetof(CPUState, fpr[3]) },
2909 { "f4", offsetof(CPUState, fpr[4]) },
2910 { "f5", offsetof(CPUState, fpr[5]) },
2911 { "f6", offsetof(CPUState, fpr[6]) },
2912 { "f7", offsetof(CPUState, fpr[7]) },
2913 { "f8", offsetof(CPUState, fpr[8]) },
2914 { "f9", offsetof(CPUState, fpr[9]) },
2915 { "f10", offsetof(CPUState, fpr[10]) },
2916 { "f11", offsetof(CPUState, fpr[11]) },
2917 { "f12", offsetof(CPUState, fpr[12]) },
2918 { "f13", offsetof(CPUState, fpr[13]) },
2919 { "f14", offsetof(CPUState, fpr[14]) },
2920 { "f15", offsetof(CPUState, fpr[15]) },
2921 { "f16", offsetof(CPUState, fpr[16]) },
2922 { "f17", offsetof(CPUState, fpr[17]) },
2923 { "f18", offsetof(CPUState, fpr[18]) },
2924 { "f19", offsetof(CPUState, fpr[19]) },
2925 { "f20", offsetof(CPUState, fpr[20]) },
2926 { "f21", offsetof(CPUState, fpr[21]) },
2927 { "f22", offsetof(CPUState, fpr[22]) },
2928 { "f23", offsetof(CPUState, fpr[23]) },
2929 { "f24", offsetof(CPUState, fpr[24]) },
2930 { "f25", offsetof(CPUState, fpr[25]) },
2931 { "f26", offsetof(CPUState, fpr[26]) },
2932 { "f27", offsetof(CPUState, fpr[27]) },
2933 { "f28", offsetof(CPUState, fpr[28]) },
2934 { "f29", offsetof(CPUState, fpr[29]) },
2935 { "f30", offsetof(CPUState, fpr[30]) },
2936 { "f31", offsetof(CPUState, fpr[31]) },
2937 { "fpscr", offsetof(CPUState, fpscr) },
2938 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002939 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002940 { "lr", offsetof(CPUState, lr) },
2941 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002942 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002943 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002944 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002945 { "msr", 0, &monitor_get_msr, },
2946 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002947 { "tbu", 0, &monitor_get_tbu, },
2948 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002949#if defined(TARGET_PPC64)
2950 /* Address space register */
2951 { "asr", offsetof(CPUState, asr) },
2952#endif
2953 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002954 { "sdr1", offsetof(CPUState, sdr1) },
2955 { "sr0", offsetof(CPUState, sr[0]) },
2956 { "sr1", offsetof(CPUState, sr[1]) },
2957 { "sr2", offsetof(CPUState, sr[2]) },
2958 { "sr3", offsetof(CPUState, sr[3]) },
2959 { "sr4", offsetof(CPUState, sr[4]) },
2960 { "sr5", offsetof(CPUState, sr[5]) },
2961 { "sr6", offsetof(CPUState, sr[6]) },
2962 { "sr7", offsetof(CPUState, sr[7]) },
2963 { "sr8", offsetof(CPUState, sr[8]) },
2964 { "sr9", offsetof(CPUState, sr[9]) },
2965 { "sr10", offsetof(CPUState, sr[10]) },
2966 { "sr11", offsetof(CPUState, sr[11]) },
2967 { "sr12", offsetof(CPUState, sr[12]) },
2968 { "sr13", offsetof(CPUState, sr[13]) },
2969 { "sr14", offsetof(CPUState, sr[14]) },
2970 { "sr15", offsetof(CPUState, sr[15]) },
2971 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002972#elif defined(TARGET_SPARC)
2973 { "g0", offsetof(CPUState, gregs[0]) },
2974 { "g1", offsetof(CPUState, gregs[1]) },
2975 { "g2", offsetof(CPUState, gregs[2]) },
2976 { "g3", offsetof(CPUState, gregs[3]) },
2977 { "g4", offsetof(CPUState, gregs[4]) },
2978 { "g5", offsetof(CPUState, gregs[5]) },
2979 { "g6", offsetof(CPUState, gregs[6]) },
2980 { "g7", offsetof(CPUState, gregs[7]) },
2981 { "o0", 0, monitor_get_reg },
2982 { "o1", 1, monitor_get_reg },
2983 { "o2", 2, monitor_get_reg },
2984 { "o3", 3, monitor_get_reg },
2985 { "o4", 4, monitor_get_reg },
2986 { "o5", 5, monitor_get_reg },
2987 { "o6", 6, monitor_get_reg },
2988 { "o7", 7, monitor_get_reg },
2989 { "l0", 8, monitor_get_reg },
2990 { "l1", 9, monitor_get_reg },
2991 { "l2", 10, monitor_get_reg },
2992 { "l3", 11, monitor_get_reg },
2993 { "l4", 12, monitor_get_reg },
2994 { "l5", 13, monitor_get_reg },
2995 { "l6", 14, monitor_get_reg },
2996 { "l7", 15, monitor_get_reg },
2997 { "i0", 16, monitor_get_reg },
2998 { "i1", 17, monitor_get_reg },
2999 { "i2", 18, monitor_get_reg },
3000 { "i3", 19, monitor_get_reg },
3001 { "i4", 20, monitor_get_reg },
3002 { "i5", 21, monitor_get_reg },
3003 { "i6", 22, monitor_get_reg },
3004 { "i7", 23, monitor_get_reg },
3005 { "pc", offsetof(CPUState, pc) },
3006 { "npc", offsetof(CPUState, npc) },
3007 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003008#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003009 { "psr", 0, &monitor_get_psr, },
3010 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003011#endif
bellarde95c8d52004-09-30 22:22:08 +00003012 { "tbr", offsetof(CPUState, tbr) },
3013 { "fsr", offsetof(CPUState, fsr) },
3014 { "f0", offsetof(CPUState, fpr[0]) },
3015 { "f1", offsetof(CPUState, fpr[1]) },
3016 { "f2", offsetof(CPUState, fpr[2]) },
3017 { "f3", offsetof(CPUState, fpr[3]) },
3018 { "f4", offsetof(CPUState, fpr[4]) },
3019 { "f5", offsetof(CPUState, fpr[5]) },
3020 { "f6", offsetof(CPUState, fpr[6]) },
3021 { "f7", offsetof(CPUState, fpr[7]) },
3022 { "f8", offsetof(CPUState, fpr[8]) },
3023 { "f9", offsetof(CPUState, fpr[9]) },
3024 { "f10", offsetof(CPUState, fpr[10]) },
3025 { "f11", offsetof(CPUState, fpr[11]) },
3026 { "f12", offsetof(CPUState, fpr[12]) },
3027 { "f13", offsetof(CPUState, fpr[13]) },
3028 { "f14", offsetof(CPUState, fpr[14]) },
3029 { "f15", offsetof(CPUState, fpr[15]) },
3030 { "f16", offsetof(CPUState, fpr[16]) },
3031 { "f17", offsetof(CPUState, fpr[17]) },
3032 { "f18", offsetof(CPUState, fpr[18]) },
3033 { "f19", offsetof(CPUState, fpr[19]) },
3034 { "f20", offsetof(CPUState, fpr[20]) },
3035 { "f21", offsetof(CPUState, fpr[21]) },
3036 { "f22", offsetof(CPUState, fpr[22]) },
3037 { "f23", offsetof(CPUState, fpr[23]) },
3038 { "f24", offsetof(CPUState, fpr[24]) },
3039 { "f25", offsetof(CPUState, fpr[25]) },
3040 { "f26", offsetof(CPUState, fpr[26]) },
3041 { "f27", offsetof(CPUState, fpr[27]) },
3042 { "f28", offsetof(CPUState, fpr[28]) },
3043 { "f29", offsetof(CPUState, fpr[29]) },
3044 { "f30", offsetof(CPUState, fpr[30]) },
3045 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003046#ifdef TARGET_SPARC64
3047 { "f32", offsetof(CPUState, fpr[32]) },
3048 { "f34", offsetof(CPUState, fpr[34]) },
3049 { "f36", offsetof(CPUState, fpr[36]) },
3050 { "f38", offsetof(CPUState, fpr[38]) },
3051 { "f40", offsetof(CPUState, fpr[40]) },
3052 { "f42", offsetof(CPUState, fpr[42]) },
3053 { "f44", offsetof(CPUState, fpr[44]) },
3054 { "f46", offsetof(CPUState, fpr[46]) },
3055 { "f48", offsetof(CPUState, fpr[48]) },
3056 { "f50", offsetof(CPUState, fpr[50]) },
3057 { "f52", offsetof(CPUState, fpr[52]) },
3058 { "f54", offsetof(CPUState, fpr[54]) },
3059 { "f56", offsetof(CPUState, fpr[56]) },
3060 { "f58", offsetof(CPUState, fpr[58]) },
3061 { "f60", offsetof(CPUState, fpr[60]) },
3062 { "f62", offsetof(CPUState, fpr[62]) },
3063 { "asi", offsetof(CPUState, asi) },
3064 { "pstate", offsetof(CPUState, pstate) },
3065 { "cansave", offsetof(CPUState, cansave) },
3066 { "canrestore", offsetof(CPUState, canrestore) },
3067 { "otherwin", offsetof(CPUState, otherwin) },
3068 { "wstate", offsetof(CPUState, wstate) },
3069 { "cleanwin", offsetof(CPUState, cleanwin) },
3070 { "fprs", offsetof(CPUState, fprs) },
3071#endif
bellard9307c4c2004-04-04 12:57:25 +00003072#endif
3073 { NULL },
3074};
3075
aliguori376253e2009-03-05 23:01:23 +00003076static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003077{
aliguori376253e2009-03-05 23:01:23 +00003078 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003079 longjmp(expr_env, 1);
3080}
3081
Markus Armbruster09b94182010-01-20 13:07:30 +01003082/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003083static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003084{
blueswir18662d652008-10-02 18:32:44 +00003085 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003086 void *ptr;
3087
bellard9307c4c2004-04-04 12:57:25 +00003088 for(md = monitor_defs; md->name != NULL; md++) {
3089 if (compare_cmd(name, md->name)) {
3090 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003091 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003092 } else {
bellard6a00d602005-11-21 23:25:50 +00003093 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003094 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003095 switch(md->type) {
3096 case MD_I32:
3097 *pval = *(int32_t *)ptr;
3098 break;
3099 case MD_TLONG:
3100 *pval = *(target_long *)ptr;
3101 break;
3102 default:
3103 *pval = 0;
3104 break;
3105 }
bellard9307c4c2004-04-04 12:57:25 +00003106 }
3107 return 0;
3108 }
3109 }
3110 return -1;
3111}
3112
3113static void next(void)
3114{
Blue Swirl660f11b2009-07-31 21:16:51 +00003115 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003116 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003117 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003118 pch++;
3119 }
3120}
3121
aliguori376253e2009-03-05 23:01:23 +00003122static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003123
aliguori376253e2009-03-05 23:01:23 +00003124static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003125{
blueswir1c2efc952007-09-25 17:28:42 +00003126 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003127 char *p;
bellard6a00d602005-11-21 23:25:50 +00003128 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003129
3130 switch(*pch) {
3131 case '+':
3132 next();
aliguori376253e2009-03-05 23:01:23 +00003133 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003134 break;
3135 case '-':
3136 next();
aliguori376253e2009-03-05 23:01:23 +00003137 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003138 break;
3139 case '~':
3140 next();
aliguori376253e2009-03-05 23:01:23 +00003141 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003142 break;
3143 case '(':
3144 next();
aliguori376253e2009-03-05 23:01:23 +00003145 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003146 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003147 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003148 }
3149 next();
3150 break;
bellard81d09122004-07-14 17:21:37 +00003151 case '\'':
3152 pch++;
3153 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003154 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003155 n = *pch;
3156 pch++;
3157 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003158 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003159 next();
3160 break;
bellard9307c4c2004-04-04 12:57:25 +00003161 case '$':
3162 {
3163 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003164 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003165
bellard9307c4c2004-04-04 12:57:25 +00003166 pch++;
3167 q = buf;
3168 while ((*pch >= 'a' && *pch <= 'z') ||
3169 (*pch >= 'A' && *pch <= 'Z') ||
3170 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003171 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003172 if ((q - buf) < sizeof(buf) - 1)
3173 *q++ = *pch;
3174 pch++;
3175 }
blueswir1cd390082008-11-16 13:53:32 +00003176 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003177 pch++;
3178 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003179 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003180 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003181 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003182 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003183 }
3184 break;
3185 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003186 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003187 n = 0;
3188 break;
3189 default:
blueswir17743e582007-09-24 18:39:04 +00003190#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003191 n = strtoull(pch, &p, 0);
3192#else
bellard9307c4c2004-04-04 12:57:25 +00003193 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003194#endif
bellard9307c4c2004-04-04 12:57:25 +00003195 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003196 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003197 }
3198 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003199 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003200 pch++;
3201 break;
3202 }
3203 return n;
3204}
3205
3206
aliguori376253e2009-03-05 23:01:23 +00003207static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003208{
blueswir1c2efc952007-09-25 17:28:42 +00003209 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003210 int op;
ths3b46e622007-09-17 08:09:54 +00003211
aliguori376253e2009-03-05 23:01:23 +00003212 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003213 for(;;) {
3214 op = *pch;
3215 if (op != '*' && op != '/' && op != '%')
3216 break;
3217 next();
aliguori376253e2009-03-05 23:01:23 +00003218 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003219 switch(op) {
3220 default:
3221 case '*':
3222 val *= val2;
3223 break;
3224 case '/':
3225 case '%':
ths5fafdf22007-09-16 21:08:06 +00003226 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003227 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003228 if (op == '/')
3229 val /= val2;
3230 else
3231 val %= val2;
3232 break;
3233 }
3234 }
3235 return val;
3236}
3237
aliguori376253e2009-03-05 23:01:23 +00003238static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003239{
blueswir1c2efc952007-09-25 17:28:42 +00003240 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003241 int op;
bellard9307c4c2004-04-04 12:57:25 +00003242
aliguori376253e2009-03-05 23:01:23 +00003243 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003244 for(;;) {
3245 op = *pch;
3246 if (op != '&' && op != '|' && op != '^')
3247 break;
3248 next();
aliguori376253e2009-03-05 23:01:23 +00003249 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003250 switch(op) {
3251 default:
3252 case '&':
3253 val &= val2;
3254 break;
3255 case '|':
3256 val |= val2;
3257 break;
3258 case '^':
3259 val ^= val2;
3260 break;
3261 }
3262 }
3263 return val;
3264}
3265
aliguori376253e2009-03-05 23:01:23 +00003266static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003267{
blueswir1c2efc952007-09-25 17:28:42 +00003268 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003269 int op;
bellard9307c4c2004-04-04 12:57:25 +00003270
aliguori376253e2009-03-05 23:01:23 +00003271 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003272 for(;;) {
3273 op = *pch;
3274 if (op != '+' && op != '-')
3275 break;
3276 next();
aliguori376253e2009-03-05 23:01:23 +00003277 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003278 if (op == '+')
3279 val += val2;
3280 else
3281 val -= val2;
3282 }
3283 return val;
3284}
3285
aliguori376253e2009-03-05 23:01:23 +00003286static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003287{
3288 pch = *pp;
3289 if (setjmp(expr_env)) {
3290 *pp = pch;
3291 return -1;
3292 }
blueswir1cd390082008-11-16 13:53:32 +00003293 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003294 pch++;
aliguori376253e2009-03-05 23:01:23 +00003295 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003296 *pp = pch;
3297 return 0;
3298}
3299
3300static int get_str(char *buf, int buf_size, const char **pp)
3301{
3302 const char *p;
3303 char *q;
3304 int c;
3305
bellard81d09122004-07-14 17:21:37 +00003306 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003307 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003308 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003309 p++;
3310 if (*p == '\0') {
3311 fail:
bellard81d09122004-07-14 17:21:37 +00003312 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003313 *pp = p;
3314 return -1;
3315 }
bellard9307c4c2004-04-04 12:57:25 +00003316 if (*p == '\"') {
3317 p++;
3318 while (*p != '\0' && *p != '\"') {
3319 if (*p == '\\') {
3320 p++;
3321 c = *p++;
3322 switch(c) {
3323 case 'n':
3324 c = '\n';
3325 break;
3326 case 'r':
3327 c = '\r';
3328 break;
3329 case '\\':
3330 case '\'':
3331 case '\"':
3332 break;
3333 default:
3334 qemu_printf("unsupported escape code: '\\%c'\n", c);
3335 goto fail;
3336 }
3337 if ((q - buf) < buf_size - 1) {
3338 *q++ = c;
3339 }
3340 } else {
3341 if ((q - buf) < buf_size - 1) {
3342 *q++ = *p;
3343 }
3344 p++;
3345 }
3346 }
3347 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003348 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003349 goto fail;
3350 }
3351 p++;
3352 } else {
blueswir1cd390082008-11-16 13:53:32 +00003353 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003354 if ((q - buf) < buf_size - 1) {
3355 *q++ = *p;
3356 }
3357 p++;
3358 }
bellard9307c4c2004-04-04 12:57:25 +00003359 }
bellard81d09122004-07-14 17:21:37 +00003360 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003361 *pp = p;
3362 return 0;
3363}
3364
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003365/*
3366 * Store the command-name in cmdname, and return a pointer to
3367 * the remaining of the command string.
3368 */
3369static const char *get_command_name(const char *cmdline,
3370 char *cmdname, size_t nlen)
3371{
3372 size_t len;
3373 const char *p, *pstart;
3374
3375 p = cmdline;
3376 while (qemu_isspace(*p))
3377 p++;
3378 if (*p == '\0')
3379 return NULL;
3380 pstart = p;
3381 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3382 p++;
3383 len = p - pstart;
3384 if (len > nlen - 1)
3385 len = nlen - 1;
3386 memcpy(cmdname, pstart, len);
3387 cmdname[len] = '\0';
3388 return p;
3389}
3390
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003391/**
3392 * Read key of 'type' into 'key' and return the current
3393 * 'type' pointer.
3394 */
3395static char *key_get_info(const char *type, char **key)
3396{
3397 size_t len;
3398 char *p, *str;
3399
3400 if (*type == ',')
3401 type++;
3402
3403 p = strchr(type, ':');
3404 if (!p) {
3405 *key = NULL;
3406 return NULL;
3407 }
3408 len = p - type;
3409
3410 str = qemu_malloc(len + 1);
3411 memcpy(str, type, len);
3412 str[len] = '\0';
3413
3414 *key = str;
3415 return ++p;
3416}
3417
bellard9307c4c2004-04-04 12:57:25 +00003418static int default_fmt_format = 'x';
3419static int default_fmt_size = 4;
3420
3421#define MAX_ARGS 16
3422
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003423static int is_valid_option(const char *c, const char *typestr)
3424{
3425 char option[3];
3426
3427 option[0] = '-';
3428 option[1] = *c;
3429 option[2] = '\0';
3430
3431 typestr = strstr(typestr, option);
3432 return (typestr != NULL);
3433}
3434
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003435static const mon_cmd_t *monitor_find_command(const char *cmdname)
3436{
3437 const mon_cmd_t *cmd;
3438
3439 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3440 if (compare_cmd(cmdname, cmd->name)) {
3441 return cmd;
3442 }
3443 }
3444
3445 return NULL;
3446}
3447
Anthony Liguoric227f092009-10-01 16:12:16 -05003448static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003449 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003450 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003451{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003452 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003453 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003454 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003455 char cmdname[256];
3456 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003457 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003458
3459#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003460 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003461#endif
ths3b46e622007-09-17 08:09:54 +00003462
bellard9307c4c2004-04-04 12:57:25 +00003463 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003464 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3465 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003466 return NULL;
ths3b46e622007-09-17 08:09:54 +00003467
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003468 cmd = monitor_find_command(cmdname);
3469 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003470 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003471 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003472 }
bellard9307c4c2004-04-04 12:57:25 +00003473
bellard9307c4c2004-04-04 12:57:25 +00003474 /* parse the parameters */
3475 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003476 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003477 typestr = key_get_info(typestr, &key);
3478 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003479 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003480 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003481 typestr++;
3482 switch(c) {
3483 case 'F':
bellard81d09122004-07-14 17:21:37 +00003484 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003485 case 's':
3486 {
3487 int ret;
ths3b46e622007-09-17 08:09:54 +00003488
blueswir1cd390082008-11-16 13:53:32 +00003489 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003490 p++;
3491 if (*typestr == '?') {
3492 typestr++;
3493 if (*p == '\0') {
3494 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003495 break;
bellard9307c4c2004-04-04 12:57:25 +00003496 }
3497 }
3498 ret = get_str(buf, sizeof(buf), &p);
3499 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003500 switch(c) {
3501 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003502 monitor_printf(mon, "%s: filename expected\n",
3503 cmdname);
bellard81d09122004-07-14 17:21:37 +00003504 break;
3505 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003506 monitor_printf(mon, "%s: block device name expected\n",
3507 cmdname);
bellard81d09122004-07-14 17:21:37 +00003508 break;
3509 default:
aliguori376253e2009-03-05 23:01:23 +00003510 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003511 break;
3512 }
bellard9307c4c2004-04-04 12:57:25 +00003513 goto fail;
3514 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003515 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003516 }
3517 break;
3518 case '/':
3519 {
3520 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003521
blueswir1cd390082008-11-16 13:53:32 +00003522 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003523 p++;
3524 if (*p == '/') {
3525 /* format found */
3526 p++;
3527 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003528 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003529 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003530 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003531 count = count * 10 + (*p - '0');
3532 p++;
3533 }
3534 }
3535 size = -1;
3536 format = -1;
3537 for(;;) {
3538 switch(*p) {
3539 case 'o':
3540 case 'd':
3541 case 'u':
3542 case 'x':
3543 case 'i':
3544 case 'c':
3545 format = *p++;
3546 break;
3547 case 'b':
3548 size = 1;
3549 p++;
3550 break;
3551 case 'h':
3552 size = 2;
3553 p++;
3554 break;
3555 case 'w':
3556 size = 4;
3557 p++;
3558 break;
3559 case 'g':
3560 case 'L':
3561 size = 8;
3562 p++;
3563 break;
3564 default:
3565 goto next;
3566 }
3567 }
3568 next:
blueswir1cd390082008-11-16 13:53:32 +00003569 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003570 monitor_printf(mon, "invalid char in format: '%c'\n",
3571 *p);
bellard9307c4c2004-04-04 12:57:25 +00003572 goto fail;
3573 }
bellard9307c4c2004-04-04 12:57:25 +00003574 if (format < 0)
3575 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003576 if (format != 'i') {
3577 /* for 'i', not specifying a size gives -1 as size */
3578 if (size < 0)
3579 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003580 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003581 }
bellard9307c4c2004-04-04 12:57:25 +00003582 default_fmt_format = format;
3583 } else {
3584 count = 1;
3585 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003586 if (format != 'i') {
3587 size = default_fmt_size;
3588 } else {
3589 size = -1;
3590 }
bellard9307c4c2004-04-04 12:57:25 +00003591 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003592 qdict_put(qdict, "count", qint_from_int(count));
3593 qdict_put(qdict, "format", qint_from_int(format));
3594 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003595 }
3596 break;
3597 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003598 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003599 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003600 {
blueswir1c2efc952007-09-25 17:28:42 +00003601 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003602
blueswir1cd390082008-11-16 13:53:32 +00003603 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003604 p++;
bellard34405572004-06-08 00:55:58 +00003605 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003606 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003607 if (*p == '\0') {
3608 typestr++;
3609 break;
3610 }
bellard34405572004-06-08 00:55:58 +00003611 } else {
3612 if (*p == '.') {
3613 p++;
blueswir1cd390082008-11-16 13:53:32 +00003614 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003615 p++;
bellard34405572004-06-08 00:55:58 +00003616 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003617 typestr++;
3618 break;
bellard34405572004-06-08 00:55:58 +00003619 }
3620 }
bellard13224a82006-07-14 22:03:35 +00003621 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003622 }
aliguori376253e2009-03-05 23:01:23 +00003623 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003624 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003625 /* Check if 'i' is greater than 32-bit */
3626 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3627 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3628 monitor_printf(mon, "integer is for 32-bit values\n");
3629 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003630 } else if (c == 'M') {
3631 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003632 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003633 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003634 }
3635 break;
3636 case '-':
3637 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003638 const char *tmp = p;
3639 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003640 /* option */
ths3b46e622007-09-17 08:09:54 +00003641
bellard9307c4c2004-04-04 12:57:25 +00003642 c = *typestr++;
3643 if (c == '\0')
3644 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003645 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003646 p++;
3647 has_option = 0;
3648 if (*p == '-') {
3649 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003650 if(c != *p) {
3651 if(!is_valid_option(p, typestr)) {
3652
3653 monitor_printf(mon, "%s: unsupported option -%c\n",
3654 cmdname, *p);
3655 goto fail;
3656 } else {
3657 skip_key = 1;
3658 }
bellard9307c4c2004-04-04 12:57:25 +00003659 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003660 if(skip_key) {
3661 p = tmp;
3662 } else {
3663 p++;
3664 has_option = 1;
3665 }
bellard9307c4c2004-04-04 12:57:25 +00003666 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003667 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003668 }
3669 break;
3670 default:
3671 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003672 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003673 goto fail;
3674 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003675 qemu_free(key);
3676 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003677 }
3678 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003679 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003680 p++;
3681 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003682 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3683 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003684 goto fail;
3685 }
3686
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003687 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003688
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003689fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003690 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003691 return NULL;
3692}
3693
Luiz Capitulino8204a912009-11-18 23:05:31 -02003694static void monitor_print_error(Monitor *mon)
3695{
3696 qerror_print(mon->error);
3697 QDECREF(mon->error);
3698 mon->error = NULL;
3699}
3700
Adam Litke940cc302010-01-25 12:18:44 -06003701static int is_async_return(const QObject *data)
3702{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003703 if (data && qobject_type(data) == QTYPE_QDICT) {
3704 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3705 }
3706
3707 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003708}
3709
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003710static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3711 const QDict *params)
3712{
3713 QObject *data = NULL;
3714
3715 cmd->mhandler.cmd_new(mon, params, &data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003716
Adam Litke940cc302010-01-25 12:18:44 -06003717 if (is_async_return(data)) {
3718 /*
3719 * Asynchronous commands have no initial return data but they can
3720 * generate errors. Data is returned via the async completion handler.
3721 */
3722 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3723 monitor_protocol_emitter(mon, NULL);
3724 }
3725 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003726 /* Monitor Protocol */
3727 monitor_protocol_emitter(mon, data);
3728 } else {
3729 /* User Protocol */
3730 if (data)
3731 cmd->user_print(mon, data);
3732 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003733
3734 qobject_decref(data);
3735}
3736
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003737static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003738{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003739 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003740 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003741
3742 qdict = qdict_new();
3743
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003744 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003745 if (!cmd)
3746 goto out;
3747
3748 qemu_errors_to_mon(mon);
3749
Adam Litke940cc302010-01-25 12:18:44 -06003750 if (monitor_handler_is_async(cmd)) {
3751 user_async_cmd_handler(mon, cmd, qdict);
3752 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003753 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003754 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003755 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003756 }
3757
Luiz Capitulino8204a912009-11-18 23:05:31 -02003758 if (monitor_has_error(mon))
3759 monitor_print_error(mon);
3760
3761 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003762
3763out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003764 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003765}
3766
bellard81d09122004-07-14 17:21:37 +00003767static void cmd_completion(const char *name, const char *list)
3768{
3769 const char *p, *pstart;
3770 char cmd[128];
3771 int len;
3772
3773 p = list;
3774 for(;;) {
3775 pstart = p;
3776 p = strchr(p, '|');
3777 if (!p)
3778 p = pstart + strlen(pstart);
3779 len = p - pstart;
3780 if (len > sizeof(cmd) - 2)
3781 len = sizeof(cmd) - 2;
3782 memcpy(cmd, pstart, len);
3783 cmd[len] = '\0';
3784 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003785 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003786 }
3787 if (*p == '\0')
3788 break;
3789 p++;
3790 }
3791}
3792
3793static void file_completion(const char *input)
3794{
3795 DIR *ffs;
3796 struct dirent *d;
3797 char path[1024];
3798 char file[1024], file_prefix[1024];
3799 int input_path_len;
3800 const char *p;
3801
ths5fafdf22007-09-16 21:08:06 +00003802 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003803 if (!p) {
3804 input_path_len = 0;
3805 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003806 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003807 } else {
3808 input_path_len = p - input + 1;
3809 memcpy(path, input, input_path_len);
3810 if (input_path_len > sizeof(path) - 1)
3811 input_path_len = sizeof(path) - 1;
3812 path[input_path_len] = '\0';
3813 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3814 }
3815#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003816 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3817 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003818#endif
3819 ffs = opendir(path);
3820 if (!ffs)
3821 return;
3822 for(;;) {
3823 struct stat sb;
3824 d = readdir(ffs);
3825 if (!d)
3826 break;
3827 if (strstart(d->d_name, file_prefix, NULL)) {
3828 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003829 if (input_path_len < sizeof(file))
3830 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3831 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003832 /* stat the file to find out if it's a directory.
3833 * In that case add a slash to speed up typing long paths
3834 */
3835 stat(file, &sb);
3836 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003837 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003838 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003839 }
3840 }
3841 closedir(ffs);
3842}
3843
aliguori51de9762009-03-05 23:00:43 +00003844static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003845{
aliguori51de9762009-03-05 23:00:43 +00003846 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003847 const char *input = opaque;
3848
3849 if (input[0] == '\0' ||
3850 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003851 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003852 }
3853}
3854
3855/* NOTE: this parser is an approximate form of the real command parser */
3856static void parse_cmdline(const char *cmdline,
3857 int *pnb_args, char **args)
3858{
3859 const char *p;
3860 int nb_args, ret;
3861 char buf[1024];
3862
3863 p = cmdline;
3864 nb_args = 0;
3865 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003866 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003867 p++;
3868 if (*p == '\0')
3869 break;
3870 if (nb_args >= MAX_ARGS)
3871 break;
3872 ret = get_str(buf, sizeof(buf), &p);
3873 args[nb_args] = qemu_strdup(buf);
3874 nb_args++;
3875 if (ret < 0)
3876 break;
3877 }
3878 *pnb_args = nb_args;
3879}
3880
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003881static const char *next_arg_type(const char *typestr)
3882{
3883 const char *p = strchr(typestr, ':');
3884 return (p != NULL ? ++p : typestr);
3885}
3886
aliguori4c36ba32009-03-05 23:01:37 +00003887static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003888{
3889 const char *cmdname;
3890 char *args[MAX_ARGS];
3891 int nb_args, i, len;
3892 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003893 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003894 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003895
3896 parse_cmdline(cmdline, &nb_args, args);
3897#ifdef DEBUG_COMPLETION
3898 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003899 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003900 }
3901#endif
3902
3903 /* if the line ends with a space, it means we want to complete the
3904 next arg */
3905 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003906 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003907 if (nb_args >= MAX_ARGS)
3908 return;
3909 args[nb_args++] = qemu_strdup("");
3910 }
3911 if (nb_args <= 1) {
3912 /* command completion */
3913 if (nb_args == 0)
3914 cmdname = "";
3915 else
3916 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003917 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003918 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003919 cmd_completion(cmdname, cmd->name);
3920 }
3921 } else {
3922 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003923 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003924 if (compare_cmd(args[0], cmd->name))
3925 goto found;
3926 }
3927 return;
3928 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003929 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003930 for(i = 0; i < nb_args - 2; i++) {
3931 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003932 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003933 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003934 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003935 }
3936 }
3937 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003938 if (*ptype == '-' && ptype[1] != '\0') {
3939 ptype += 2;
3940 }
bellard81d09122004-07-14 17:21:37 +00003941 switch(*ptype) {
3942 case 'F':
3943 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003944 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003945 file_completion(str);
3946 break;
3947 case 'B':
3948 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003949 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003950 bdrv_iterate(block_completion_it, (void *)str);
3951 break;
bellard7fe48482004-10-09 18:08:01 +00003952 case 's':
3953 /* XXX: more generic ? */
3954 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003955 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003956 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3957 cmd_completion(str, cmd->name);
3958 }
bellard64866c32006-05-07 18:03:31 +00003959 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003960 char *sep = strrchr(str, '-');
3961 if (sep)
3962 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003963 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003964 for(key = key_defs; key->name != NULL; key++) {
3965 cmd_completion(str, key->name);
3966 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003967 } else if (!strcmp(cmd->name, "help|?")) {
3968 readline_set_completion_index(cur_mon->rs, strlen(str));
3969 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3970 cmd_completion(str, cmd->name);
3971 }
bellard7fe48482004-10-09 18:08:01 +00003972 }
3973 break;
bellard81d09122004-07-14 17:21:37 +00003974 default:
3975 break;
3976 }
3977 }
3978 for(i = 0; i < nb_args; i++)
3979 qemu_free(args[i]);
3980}
3981
aliguori731b0362009-03-05 23:01:42 +00003982static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003983{
aliguori731b0362009-03-05 23:01:42 +00003984 Monitor *mon = opaque;
3985
Jan Kiszkac62313b2009-12-04 14:05:29 +01003986 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003987}
3988
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003989typedef struct CmdArgs {
3990 QString *name;
3991 int type;
3992 int flag;
3993 int optional;
3994} CmdArgs;
3995
3996static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
3997{
3998 if (!cmd_args->optional) {
3999 qemu_error_new(QERR_MISSING_PARAMETER, name);
4000 return -1;
4001 }
4002
4003 if (cmd_args->type == '-') {
4004 /* handlers expect a value, they need to be changed */
4005 qdict_put(args, name, qint_from_int(0));
4006 }
4007
4008 return 0;
4009}
4010
4011static int check_arg(const CmdArgs *cmd_args, QDict *args)
4012{
4013 QObject *value;
4014 const char *name;
4015
4016 name = qstring_get_str(cmd_args->name);
4017
4018 if (!args) {
4019 return check_opt(cmd_args, name, args);
4020 }
4021
4022 value = qdict_get(args, name);
4023 if (!value) {
4024 return check_opt(cmd_args, name, args);
4025 }
4026
4027 switch (cmd_args->type) {
4028 case 'F':
4029 case 'B':
4030 case 's':
4031 if (qobject_type(value) != QTYPE_QSTRING) {
4032 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "string");
4033 return -1;
4034 }
4035 break;
4036 case '/': {
4037 int i;
4038 const char *keys[] = { "count", "format", "size", NULL };
4039
4040 for (i = 0; keys[i]; i++) {
4041 QObject *obj = qdict_get(args, keys[i]);
4042 if (!obj) {
4043 qemu_error_new(QERR_MISSING_PARAMETER, name);
4044 return -1;
4045 }
4046 if (qobject_type(obj) != QTYPE_QINT) {
4047 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4048 return -1;
4049 }
4050 }
4051 break;
4052 }
4053 case 'i':
4054 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004055 case 'M':
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004056 if (qobject_type(value) != QTYPE_QINT) {
4057 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4058 return -1;
4059 }
4060 break;
4061 case '-':
4062 if (qobject_type(value) != QTYPE_QINT &&
4063 qobject_type(value) != QTYPE_QBOOL) {
4064 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "bool");
4065 return -1;
4066 }
4067 if (qobject_type(value) == QTYPE_QBOOL) {
4068 /* handlers expect a QInt, they need to be changed */
4069 qdict_put(args, name,
4070 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
4071 }
4072 break;
4073 default:
4074 /* impossible */
4075 abort();
4076 }
4077
4078 return 0;
4079}
4080
4081static void cmd_args_init(CmdArgs *cmd_args)
4082{
4083 cmd_args->name = qstring_new();
4084 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
4085}
4086
4087/*
4088 * This is not trivial, we have to parse Monitor command's argument
4089 * type syntax to be able to check the arguments provided by clients.
4090 *
4091 * In the near future we will be using an array for that and will be
4092 * able to drop all this parsing...
4093 */
4094static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
4095{
4096 int err;
4097 const char *p;
4098 CmdArgs cmd_args;
4099
Blue Swirldd5121b2009-12-04 20:52:02 +00004100 if (cmd->args_type == NULL) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004101 return (qdict_size(args) == 0 ? 0 : -1);
4102 }
4103
4104 err = 0;
4105 cmd_args_init(&cmd_args);
4106
4107 for (p = cmd->args_type;; p++) {
4108 if (*p == ':') {
4109 cmd_args.type = *++p;
4110 p++;
4111 if (cmd_args.type == '-') {
4112 cmd_args.flag = *p++;
4113 cmd_args.optional = 1;
4114 } else if (*p == '?') {
4115 cmd_args.optional = 1;
4116 p++;
4117 }
4118
4119 assert(*p == ',' || *p == '\0');
4120 err = check_arg(&cmd_args, args);
4121
4122 QDECREF(cmd_args.name);
4123 cmd_args_init(&cmd_args);
4124
4125 if (err < 0) {
4126 break;
4127 }
4128 } else {
4129 qstring_append_chr(cmd_args.name, *p);
4130 }
4131
4132 if (*p == '\0') {
4133 break;
4134 }
4135 }
4136
4137 QDECREF(cmd_args.name);
4138 return err;
4139}
4140
4141static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4142{
4143 int err;
4144 QObject *obj;
4145 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004146 const mon_cmd_t *cmd;
4147 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004148 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004149
4150 args = NULL;
4151 qemu_errors_to_mon(mon);
4152
4153 obj = json_parser_parse(tokens, NULL);
4154 if (!obj) {
4155 // FIXME: should be triggered in json_parser_parse()
4156 qemu_error_new(QERR_JSON_PARSING);
4157 goto err_out;
4158 } else if (qobject_type(obj) != QTYPE_QDICT) {
4159 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "object");
4160 qobject_decref(obj);
4161 goto err_out;
4162 }
4163
4164 input = qobject_to_qdict(obj);
4165
4166 mon->mc->id = qdict_get(input, "id");
4167 qobject_incref(mon->mc->id);
4168
4169 obj = qdict_get(input, "execute");
4170 if (!obj) {
4171 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4172 goto err_input;
4173 } else if (qobject_type(obj) != QTYPE_QSTRING) {
4174 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "string");
4175 goto err_input;
4176 }
4177
4178 cmd_name = qstring_get_str(qobject_to_qstring(obj));
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004179
4180 /*
4181 * XXX: We need this special case until we get info handlers
4182 * converted into 'query-' commands
4183 */
4184 if (compare_cmd(cmd_name, "info")) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004185 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4186 goto err_input;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004187 } else if (strstart(cmd_name, "query-", &info_item)) {
4188 cmd = monitor_find_command("info");
4189 qdict_put_obj(input, "arguments",
4190 qobject_from_jsonf("{ 'item': %s }", info_item));
4191 } else {
4192 cmd = monitor_find_command(cmd_name);
Luiz Capitulino43e713c2009-12-18 13:24:59 -02004193 if (!cmd || !monitor_handler_ported(cmd)) {
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004194 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4195 goto err_input;
4196 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004197 }
4198
4199 obj = qdict_get(input, "arguments");
4200 if (!obj) {
4201 args = qdict_new();
4202 } else {
4203 args = qobject_to_qdict(obj);
4204 QINCREF(args);
4205 }
4206
4207 QDECREF(input);
4208
4209 err = monitor_check_qmp_args(cmd, args);
4210 if (err < 0) {
4211 goto err_out;
4212 }
4213
Adam Litke940cc302010-01-25 12:18:44 -06004214 if (monitor_handler_is_async(cmd)) {
4215 qmp_async_cmd_handler(mon, cmd, args);
4216 } else {
4217 monitor_call_handler(mon, cmd, args);
4218 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004219 goto out;
4220
4221err_input:
4222 QDECREF(input);
4223err_out:
4224 monitor_protocol_emitter(mon, NULL);
4225out:
4226 QDECREF(args);
4227 qemu_errors_to_previous();
4228}
4229
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004230/**
4231 * monitor_control_read(): Read and handle QMP input
4232 */
4233static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4234{
4235 Monitor *old_mon = cur_mon;
4236
4237 cur_mon = opaque;
4238
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004239 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004240
4241 cur_mon = old_mon;
4242}
4243
aliguori731b0362009-03-05 23:01:42 +00004244static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004245{
aliguori731b0362009-03-05 23:01:42 +00004246 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004247 int i;
aliguori376253e2009-03-05 23:01:23 +00004248
aliguori731b0362009-03-05 23:01:42 +00004249 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004250
aliguoricde76ee2009-03-05 23:01:51 +00004251 if (cur_mon->rs) {
4252 for (i = 0; i < size; i++)
4253 readline_handle_byte(cur_mon->rs, buf[i]);
4254 } else {
4255 if (size == 0 || buf[size - 1] != 0)
4256 monitor_printf(cur_mon, "corrupted command\n");
4257 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004258 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004259 }
aliguori731b0362009-03-05 23:01:42 +00004260
4261 cur_mon = old_mon;
4262}
aliguorid8f44602008-10-06 13:52:44 +00004263
aliguori376253e2009-03-05 23:01:23 +00004264static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004265{
aliguori731b0362009-03-05 23:01:42 +00004266 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004267 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004268 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004269}
4270
aliguoricde76ee2009-03-05 23:01:51 +00004271int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004272{
aliguoricde76ee2009-03-05 23:01:51 +00004273 if (!mon->rs)
4274 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004275 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004276 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004277}
4278
aliguori376253e2009-03-05 23:01:23 +00004279void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004280{
aliguoricde76ee2009-03-05 23:01:51 +00004281 if (!mon->rs)
4282 return;
aliguori731b0362009-03-05 23:01:42 +00004283 if (--mon->suspend_cnt == 0)
4284 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004285}
4286
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004287/**
4288 * monitor_control_event(): Print QMP gretting
4289 */
4290static void monitor_control_event(void *opaque, int event)
4291{
4292 if (event == CHR_EVENT_OPENED) {
4293 QObject *data;
4294 Monitor *mon = opaque;
4295
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004296 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
4297
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004298 data = qobject_from_jsonf("{ 'QMP': { 'capabilities': [] } }");
4299 assert(data != NULL);
4300
4301 monitor_json_emitter(mon, data);
4302 qobject_decref(data);
4303 }
4304}
4305
aliguori731b0362009-03-05 23:01:42 +00004306static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004307{
aliguori376253e2009-03-05 23:01:23 +00004308 Monitor *mon = opaque;
4309
aliguori2724b182009-03-05 23:01:47 +00004310 switch (event) {
4311 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004312 mon->mux_out = 0;
4313 if (mon->reset_seen) {
4314 readline_restart(mon->rs);
4315 monitor_resume(mon);
4316 monitor_flush(mon);
4317 } else {
4318 mon->suspend_cnt = 0;
4319 }
aliguori2724b182009-03-05 23:01:47 +00004320 break;
ths86e94de2007-01-05 22:01:59 +00004321
aliguori2724b182009-03-05 23:01:47 +00004322 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004323 if (mon->reset_seen) {
4324 if (mon->suspend_cnt == 0) {
4325 monitor_printf(mon, "\n");
4326 }
4327 monitor_flush(mon);
4328 monitor_suspend(mon);
4329 } else {
4330 mon->suspend_cnt++;
4331 }
4332 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004333 break;
4334
Amit Shahb6b8df52009-10-07 18:31:16 +05304335 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004336 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4337 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004338 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004339 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004340 }
4341 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004342 break;
4343 }
ths86e94de2007-01-05 22:01:59 +00004344}
4345
aliguori76655d62009-03-06 20:27:37 +00004346
4347/*
4348 * Local variables:
4349 * c-indent-level: 4
4350 * c-basic-offset: 4
4351 * tab-width: 8
4352 * End:
4353 */
4354
aliguori731b0362009-03-05 23:01:42 +00004355void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004356{
aliguori731b0362009-03-05 23:01:42 +00004357 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004358 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004359
4360 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004361 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004362 is_first_init = 0;
4363 }
aliguori87127162009-03-05 23:01:29 +00004364
4365 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004366
aliguori87127162009-03-05 23:01:29 +00004367 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004368 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004369 if (flags & MONITOR_USE_READLINE) {
4370 mon->rs = readline_init(mon, monitor_find_completion);
4371 monitor_read_command(mon, 0);
4372 }
aliguori87127162009-03-05 23:01:29 +00004373
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004374 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004375 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004376 /* Control mode requires special handlers */
4377 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4378 monitor_control_event, mon);
4379 } else {
4380 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4381 monitor_event, mon);
4382 }
aliguori87127162009-03-05 23:01:29 +00004383
Blue Swirl72cf2d42009-09-12 07:36:22 +00004384 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00004385 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00004386 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004387}
4388
aliguori376253e2009-03-05 23:01:23 +00004389static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004390{
aliguoribb5fc202009-03-05 23:01:15 +00004391 BlockDriverState *bs = opaque;
4392 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004393
aliguoribb5fc202009-03-05 23:01:15 +00004394 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004395 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004396 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004397 }
aliguori731b0362009-03-05 23:01:42 +00004398 if (mon->password_completion_cb)
4399 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004400
aliguori731b0362009-03-05 23:01:42 +00004401 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004402}
aliguoric0f4ce72009-03-05 23:01:01 +00004403
aliguori376253e2009-03-05 23:01:23 +00004404void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00004405 BlockDriverCompletionFunc *completion_cb,
4406 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004407{
aliguoricde76ee2009-03-05 23:01:51 +00004408 int err;
4409
aliguoribb5fc202009-03-05 23:01:15 +00004410 if (!bdrv_key_required(bs)) {
4411 if (completion_cb)
4412 completion_cb(opaque, 0);
4413 return;
4414 }
aliguoric0f4ce72009-03-05 23:01:01 +00004415
Luiz Capitulino94171e12009-12-07 21:37:00 +01004416 if (monitor_ctrl_mode(mon)) {
4417 qemu_error_new(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
4418 return;
4419 }
4420
aliguori376253e2009-03-05 23:01:23 +00004421 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4422 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004423
aliguori731b0362009-03-05 23:01:42 +00004424 mon->password_completion_cb = completion_cb;
4425 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004426
aliguoricde76ee2009-03-05 23:01:51 +00004427 err = monitor_read_password(mon, bdrv_password_cb, bs);
4428
4429 if (err && completion_cb)
4430 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00004431}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004432
4433typedef struct QemuErrorSink QemuErrorSink;
4434struct QemuErrorSink {
4435 enum {
4436 ERR_SINK_FILE,
4437 ERR_SINK_MONITOR,
4438 } dest;
4439 union {
4440 FILE *fp;
4441 Monitor *mon;
4442 };
4443 QemuErrorSink *previous;
4444};
4445
Blue Swirl528e93a2009-08-31 15:14:40 +00004446static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004447
4448void qemu_errors_to_file(FILE *fp)
4449{
4450 QemuErrorSink *sink;
4451
4452 sink = qemu_mallocz(sizeof(*sink));
4453 sink->dest = ERR_SINK_FILE;
4454 sink->fp = fp;
4455 sink->previous = qemu_error_sink;
4456 qemu_error_sink = sink;
4457}
4458
4459void qemu_errors_to_mon(Monitor *mon)
4460{
4461 QemuErrorSink *sink;
4462
4463 sink = qemu_mallocz(sizeof(*sink));
4464 sink->dest = ERR_SINK_MONITOR;
4465 sink->mon = mon;
4466 sink->previous = qemu_error_sink;
4467 qemu_error_sink = sink;
4468}
4469
4470void qemu_errors_to_previous(void)
4471{
4472 QemuErrorSink *sink;
4473
4474 assert(qemu_error_sink != NULL);
4475 sink = qemu_error_sink;
4476 qemu_error_sink = sink->previous;
4477 qemu_free(sink);
4478}
4479
4480void qemu_error(const char *fmt, ...)
4481{
4482 va_list args;
4483
4484 assert(qemu_error_sink != NULL);
4485 switch (qemu_error_sink->dest) {
4486 case ERR_SINK_FILE:
4487 va_start(args, fmt);
4488 vfprintf(qemu_error_sink->fp, fmt, args);
4489 va_end(args);
4490 break;
4491 case ERR_SINK_MONITOR:
4492 va_start(args, fmt);
4493 monitor_vprintf(qemu_error_sink->mon, fmt, args);
4494 va_end(args);
4495 break;
4496 }
4497}
Luiz Capitulino8204a912009-11-18 23:05:31 -02004498
4499void qemu_error_internal(const char *file, int linenr, const char *func,
4500 const char *fmt, ...)
4501{
4502 va_list va;
4503 QError *qerror;
4504
4505 assert(qemu_error_sink != NULL);
4506
4507 va_start(va, fmt);
4508 qerror = qerror_from_info(file, linenr, func, fmt, &va);
4509 va_end(va);
4510
4511 switch (qemu_error_sink->dest) {
4512 case ERR_SINK_FILE:
4513 qerror_print(qerror);
4514 QDECREF(qerror);
4515 break;
4516 case ERR_SINK_MONITOR:
4517 assert(qemu_error_sink->mon->error == NULL);
4518 qemu_error_sink->mon->error = qerror;
4519 break;
4520 }
4521}